Skip to content

Commit

Permalink
Fix autodetect CMAKE_SYSTEM_VERSION to convert to Darwin version (#16335
Browse files Browse the repository at this point in the history
)

* add function to convert apple version to darwin

* improve

* improve implementation

* reduce code

* use os.version

* minor changes

* fix format

* fix format

---------

Co-authored-by: czoido <mrgalleta@gmail.com>
  • Loading branch information
juansblanco and czoido committed May 30, 2024
1 parent b6c53b3 commit c9e1494
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
36 changes: 33 additions & 3 deletions conan/tools/cmake/toolchain/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from conans.client.generators import relativize_path
from conans.client.subsystems import deduce_subsystem, WINDOWS
from conan.errors import ConanException
from conans.model.version import Version
from conans.util.files import load


Expand Down Expand Up @@ -931,6 +932,34 @@ def _is_apple_cross_building(self):
return os_host in ('iOS', 'watchOS', 'tvOS', 'visionOS') or (
os_host == 'Macos' and (arch_host != arch_build or os_build != os_host))

def _get_darwin_version(self, os_name, os_version):
# version mapping from https://en.wikipedia.org/wiki/Darwin_(operating_system)
version_mapping = {
"Macos": {
"10.6": "10", "10.7": "11", "10.8": "12", "10.9": "13", "10.10": "14", "10.11": "15",
"10.12": "16", "10.13": "17", "10.14": "18", "10.15": "19", "11": "20", "12": "21",
"13": "22", "14": "23",
},
"iOS": {
"7": "14", "8": "14", "9": "15", "10": "16", "11": "17", "12": "18", "13": "19",
"14": "20", "15": "21", "16": "22", "17": "23"
},
"watchOS": {
"4": "17", "5": "18", "6": "19", "7": "20",
"8": "21", "9": "22", "10": "23"
},
"tvOS": {
"11": "17", "12": "18", "13": "19", "14": "20",
"15": "21", "16": "22", "17": "23"
},
"visionOS": {
"1": "23"
}
}
os_version = Version(os_version).major if os_name != "Macos" or (os_name == "Macos" and Version(
os_version) >= Version("11")) else os_version
return version_mapping.get(os_name, {}).get(str(os_version))

def _get_cross_build(self):
user_toolchain = self._conanfile.conf.get("tools.cmake.cmaketoolchain:user_toolchain")

Expand All @@ -942,6 +971,7 @@ def _get_cross_build(self):
if not user_toolchain and not is_universal_arch(self._conanfile.settings.get_safe("arch"),
self._conanfile.settings.possible_values().get("arch")):
os_host = self._conanfile.settings.get_safe("os")
os_host_version = self._conanfile.settings.get_safe("os.version")
arch_host = self._conanfile.settings.get_safe("arch")
if arch_host == "armv8":
arch_host = {"Windows": "ARM64", "Macos": "arm64"}.get(os_host, "aarch64")
Expand All @@ -952,12 +982,12 @@ def _get_cross_build(self):
if self._is_apple_cross_building():
# cross-build in Macos also for M1
system_name = {'Macos': 'Darwin'}.get(os_host, os_host)
# CMAKE_SYSTEM_VERSION for Apple sets the sdk version, not the os version
_system_version = self._conanfile.settings.get_safe("os.sdk_version")
# CMAKE_SYSTEM_VERSION for Apple sets the Darwin version, not the os version
_system_version = self._get_darwin_version(os_host, os_host_version)
_system_processor = to_apple_arch(self._conanfile)
elif os_host != 'Android':
system_name = self._get_generic_system_name()
_system_version = self._conanfile.settings.get_safe("os.version")
_system_version = os_host_version
_system_processor = arch_host

if system_name is not None and system_version is None:
Expand Down
6 changes: 3 additions & 3 deletions test/integration/toolchains/cmake/test_cmaketoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_cross_build_linux_to_macos():
macos_profile = textwrap.dedent("""
[settings]
os=Macos
os.sdk_version=13.1
os.version=13.1
arch=x86_64
compiler=apple-clang
compiler.version=13
Expand All @@ -85,7 +85,7 @@ def test_cross_build_linux_to_macos():
toolchain = client.load("conan_toolchain.cmake")

assert "set(CMAKE_SYSTEM_NAME Darwin)" in toolchain
assert "set(CMAKE_SYSTEM_VERSION 13.1)" in toolchain
assert "set(CMAKE_SYSTEM_VERSION 22)" in toolchain
assert "set(CMAKE_SYSTEM_PROCESSOR x86_64)" in toolchain


Expand Down Expand Up @@ -437,7 +437,7 @@ def test_cmaketoolchain_cmake_system_processor_cross_apple():
client.run("install hello.py -pr:h=./profile_ios -pr:b=default -g CMakeToolchain")
toolchain = client.load("conan_toolchain.cmake")
assert "set(CMAKE_SYSTEM_NAME iOS)" in toolchain
assert "set(CMAKE_SYSTEM_VERSION 15.0)" in toolchain
assert "set(CMAKE_SYSTEM_VERSION 21)" in toolchain
assert "set(CMAKE_SYSTEM_PROCESSOR arm64)" in toolchain


Expand Down

0 comments on commit c9e1494

Please sign in to comment.