Skip to content

Commit

Permalink
Define CMAKE_OSX_DEPLOYMENT_TARGET CMake option + environment
Browse files Browse the repository at this point in the history
  • Loading branch information
tttapa committed Feb 2, 2025
1 parent a8ca9d1 commit 7da939f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/py_build_cmake/commands/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ def prepare_environment(self):
if self.install_settings.prefix is not None:
install_prefix = str(self.install_settings.prefix)
self.environment[f"{pbc}_INSTALL_PREFIX"] = install_prefix
if not self.cross_compiling() and self.plat.machine == "Darwin":
version = self.plat.macos_version_str
self.environment["MACOSX_DEPLOYMENT_TARGET"] = version
if self.conf_settings.environment:
for k, v in self.conf_settings.environment.items():
templ = Template(v)
Expand Down Expand Up @@ -282,11 +285,14 @@ def get_configure_options_make(self) -> list[Option]:

def get_configure_options_toolchain(self) -> list[str]:
"""Sets CMAKE_TOOLCHAIN_FILE."""
return (
["CMAKE_TOOLCHAIN_FILE:FILEPATH=" + str(self.conf_settings.toolchain_file)]
if self.conf_settings.toolchain_file
else []
)
opts = []
if not self.cross_compiling() and self.plat.machine == "Darwin":
version = self.plat.macos_version_str
opts += ["CMAKE_OSX_DEPLOYMENT_TARGET:STRING=" + version]
if self.conf_settings.toolchain_file:
toolchain = str(self.conf_settings.toolchain_file)
opts += ["CMAKE_TOOLCHAIN_FILE:FILEPATH=" + toolchain]
return opts

def get_configure_options_settings(self) -> list[str]:
return [k + "=" + v for k, v in self.conf_settings.options.items()]
Expand Down
5 changes: 5 additions & 0 deletions src/py_build_cmake/common/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ class BuildPlatformInfo:
macos_version: tuple[int, int] | None = None
cmake_generator_platform: str | None = None

@property
def macos_version_str(self) -> str:
assert self.macos_version is not None
return ".".join(map(str, self.macos_version))

@property
def os_name(self) -> OSIdentifier:
"""Get the name of the current platform. For use in file names etc.,
Expand Down
6 changes: 5 additions & 1 deletion src/py_build_cmake/config/quirks/macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,24 @@ def cross_compile_mac(plat: BuildPlatformInfo, config: ValueReference):
"""Update the configuration to include a cross-compilation configuration
that builds for the given architectures."""
assert plat.archs is not None
assert plat.macos_version is not None
assert not config.is_value_set("cross")
logger.info(

Check notice on line 22 in src/py_build_cmake/config/quirks/macos.py

View workflow job for this annotation

GitHub Actions / cibw (macos-13, swig-project)

py_build_cmake.config.quirks.macos:ARCHFLAGS was specified. Automatically enabling cross-compilation for arm64 (native platform: x86_64)

Check notice on line 22 in src/py_build_cmake/config/quirks/macos.py

View workflow job for this annotation

GitHub Actions / cibw (macos-13, swig-project)

py_build_cmake.config.quirks.macos:ARCHFLAGS was specified. Automatically enabling cross-compilation for arm64 (native platform: x86_64)

Check notice on line 22 in src/py_build_cmake/config/quirks/macos.py

View workflow job for this annotation

GitHub Actions / cibw (macos-13, nanobind-project)

py_build_cmake.config.quirks.macos:ARCHFLAGS was specified. Automatically enabling cross-compilation for arm64 (native platform: x86_64)

Check notice on line 22 in src/py_build_cmake/config/quirks/macos.py

View workflow job for this annotation

GitHub Actions / cibw (macos-13, nanobind-project)

py_build_cmake.config.quirks.macos:ARCHFLAGS was specified. Automatically enabling cross-compilation for arm64 (native platform: x86_64)

Check notice on line 22 in src/py_build_cmake/config/quirks/macos.py

View workflow job for this annotation

GitHub Actions / cibw (macos-13, pybind11-project)

py_build_cmake.config.quirks.macos:ARCHFLAGS was specified. Automatically enabling cross-compilation for arm64 (native platform: x86_64)

Check notice on line 22 in src/py_build_cmake/config/quirks/macos.py

View workflow job for this annotation

GitHub Actions / cibw (macos-13, pybind11-project)

py_build_cmake.config.quirks.macos:ARCHFLAGS was specified. Automatically enabling cross-compilation for arm64 (native platform: x86_64)

Check notice on line 22 in src/py_build_cmake/config/quirks/macos.py

View workflow job for this annotation

GitHub Actions / cibw (macos-latest, pybind11-project)

py_build_cmake.config.quirks.macos:ARCHFLAGS was specified. Automatically enabling cross-compilation for x86_64 (native platform: arm64)

Check notice on line 22 in src/py_build_cmake/config/quirks/macos.py

View workflow job for this annotation

GitHub Actions / cibw (macos-latest, pybind11-project)

py_build_cmake.config.quirks.macos:ARCHFLAGS was specified. Automatically enabling cross-compilation for x86_64 (native platform: arm64)

Check notice on line 22 in src/py_build_cmake/config/quirks/macos.py

View workflow job for this annotation

GitHub Actions / cibw (macos-latest, nanobind-project)

py_build_cmake.config.quirks.macos:ARCHFLAGS was specified. Automatically enabling cross-compilation for x86_64 (native platform: arm64)

Check notice on line 22 in src/py_build_cmake/config/quirks/macos.py

View workflow job for this annotation

GitHub Actions / cibw (macos-latest, nanobind-project)

py_build_cmake.config.quirks.macos:ARCHFLAGS was specified. Automatically enabling cross-compilation for x86_64 (native platform: arm64)

Check notice on line 22 in src/py_build_cmake/config/quirks/macos.py

View workflow job for this annotation

GitHub Actions / cibw (macos-latest, swig-project)

py_build_cmake.config.quirks.macos:ARCHFLAGS was specified. Automatically enabling cross-compilation for x86_64 (native platform: arm64)

Check notice on line 22 in src/py_build_cmake/config/quirks/macos.py

View workflow job for this annotation

GitHub Actions / cibw (macos-latest, swig-project)

py_build_cmake.config.quirks.macos:ARCHFLAGS was specified. Automatically enabling cross-compilation for x86_64 (native platform: arm64)
"ARCHFLAGS was specified. Automatically enabling cross-compilation for %s (native platform: %s)",
", ".join(plat.archs),
plat.machine,
)
all = MultiConfigOption.default_index
macos_version = plat.macos_version_str
options = {
"CMAKE_SYSTEM_NAME": CMakeOption.create("Darwin", "STRING"),
"CMAKE_OSX_ARCHITECTURES": CMakeOption.create(list(plat.archs), "STRING"),
"CMAKE_OSX_DEPLOYMENT_TARGET": CMakeOption.create(macos_version, "STRING"),
}
env = {"MACOSX_DEPLOYMENT_TARGET": macos_version}
cross_cfg: dict[str, Any] = {
"os": "mac",
"cmake": {all: {"options": options}},
"cmake": {all: {"options": options, "env": env}},
}
cross_cfg["arch"] = StringOption.create(plat.platform_tag)
# The SETUPTOOLS_EXT_SUFFIX variable is used by e.g. pybind11
Expand Down

0 comments on commit 7da939f

Please sign in to comment.