Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: correctly set CMAKE_OSX_SYSROOT and CMAKE_OSX_ARCHITECTURES #7512

Merged
merged 2 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions conans/client/build/cmake_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,18 @@ def get_definitions(self):
definitions.update(build_type_definition(self._forced_build_type, build_type,
self._generator, self._output))

if tools.is_apple_os(os_):
definitions["CMAKE_OSX_ARCHITECTURES"] = tools.to_apple_arch(arch)
# don't attempt to override variables set within toolchain
if tools.is_apple_os(os_) and "CONAN_CMAKE_TOOLCHAIN_FILE" not in os.environ:
SSE4 marked this conversation as resolved.
Show resolved Hide resolved
apple_arch = tools.to_apple_arch(arch)
if apple_arch:
definitions["CMAKE_OSX_ARCHITECTURES"] = apple_arch
# xcrun is only available on macOS, otherwise it's cross-compiling and it needs to be
# set within CMake toolchain
if platform.system() == "Darwin":
definitions["CMAKE_OSX_SYSROOT"] = tools.XCRun(self._conanfile.settings).sdk_path
# set within CMake toolchain. also, if SDKROOT is set, CMake will use it, and it's not
# needed to run xcrun.
if platform.system() == "Darwin" and "SDKROOT" not in os.environ:
sdk_path = tools.XCRun(self._conanfile.settings).sdk_path
if sdk_path:
definitions["CMAKE_OSX_SYSROOT"] = sdk_path

definitions.update(self._cmake_cross_build_defines())
definitions.update(self._get_cpp_standard_vars())
Expand Down
10 changes: 4 additions & 6 deletions conans/client/tools/apple.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ def apple_sdk_name(settings):
'iOS': 'iphonesimulator',
'watchOS': 'watchsimulator',
'tvOS': 'appletvsimulator'}.get(str(os_))
elif str(arch).startswith('arm'):
return {'iOS': 'iphoneos',
'watchOS': 'watchos',
'tvOS': 'appletvos'}.get(str(os_))
else:
return None

return {'Macos': 'macosx',
'iOS': 'iphoneos',
'watchOS': 'watchos',
'tvOS': 'appletvos'}.get(str(os_), None)

def apple_deployment_target_env(os_, os_version):
"""environment variable name which controls deployment target"""
Expand Down
59 changes: 59 additions & 0 deletions conans/test/functional/build_helpers/cmake_apple_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import platform
import unittest
from parameterized import parameterized

from conans.client.build.cmake import CMake
from conans.client.conf import get_default_settings_yml
from conans.model.settings import Settings
from conans.test.utils.mocks import MockSettings, ConanFileMock


@unittest.skipUnless(platform.system() == "Darwin", "Only for MacOS")
class CMakeAppleTest(unittest.TestCase):
@parameterized.expand([('x86', 'Macos', 'i386', 'MacOSX.platform'),
('x86_64', 'Macos', 'x86_64', 'MacOSX.platform'),
('armv7', 'Macos', 'armv7', 'MacOSX.platform'),
('armv8', 'Macos', 'arm64', 'MacOSX.platform'),
('x86', 'iOS', 'i386', 'iPhoneSimulator.platform'),
('x86_64', 'iOS', 'x86_64', 'iPhoneSimulator.platform'),
('armv7', 'iOS', 'armv7', 'iPhoneOS.platform'),
('armv8', 'iOS', 'arm64', 'iPhoneOS.platform'),
('x86', 'watchOS', 'i386', 'WatchSimulator.platform'),
('x86_64', 'watchOS', 'x86_64', 'WatchSimulator.platform'),
('armv7', 'watchOS', 'armv7', 'WatchOS.platform'),
('armv8', 'watchOS', 'arm64', 'WatchOS.platform'),
('x86', 'tvOS', 'i386', 'AppleTVSimulator.platform'),
('x86_64', 'tvOS', 'x86_64', 'AppleTVSimulator.platform'),
('armv7', 'tvOS', 'armv7', 'AppleTVOS.platform'),
('armv8', 'tvOS', 'arm64', 'AppleTVOS.platform')
])
def test_cmake_definitions(self, conan_arch, conan_os, expected_arch, expected_os):
settings = Settings.loads(get_default_settings_yml())
settings.os = conan_os
settings.compiler = "apple-clang"
settings.compiler.version = "11.0"
settings.arch = conan_arch

conanfile = ConanFileMock()
conanfile.settings = settings
cmake = CMake(conanfile)

self.assertEqual(cmake.definitions["CMAKE_OSX_ARCHITECTURES"], expected_arch)
self.assertIn(expected_os, cmake.definitions["CMAKE_OSX_SYSROOT"])

@parameterized.expand([('iOS', 'iPhoneOS.platform'),
('Macos', 'MacOSX.platform'),
('watchOS', 'WatchOS.platform'),
('tvOS', 'AppleTVOS.platform')
])
def test_custom_settings(self, conan_os, expected_os):
settings = MockSettings({"os": conan_os,
"compiler": "apple-clang",
"compiler.version": "11.0",
"arch": "ios_fat"})
conanfile = ConanFileMock()
conanfile.settings = settings
cmake = CMake(conanfile)

self.assertNotIn("CMAKE_OSX_ARCHITECTURES", cmake.definitions)
self.assertIn(expected_os, cmake.definitions["CMAKE_OSX_SYSROOT"])
8 changes: 7 additions & 1 deletion conans/test/unittests/util/apple_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def test_to_apple_arch(self):
self.assertIsNone(tools.to_apple_arch('mips'))

def test_apple_sdk_name(self):

self.assertEqual(tools.apple_sdk_name(FakeSettings('Macos', 'x86')), 'macosx')
self.assertEqual(tools.apple_sdk_name(FakeSettings('Macos', 'x86_64')), 'macosx')
self.assertEqual(tools.apple_sdk_name(FakeSettings('iOS', 'x86_64')), 'iphonesimulator')
Expand All @@ -53,6 +52,13 @@ def test_apple_sdk_name(self):
self.assertEqual(tools.apple_sdk_name(FakeSettings('tvOS', 'armv8')), 'appletvos')
self.assertIsNone(tools.apple_sdk_name(FakeSettings('Windows', 'x86')))

def test_apple_sdk_name_custom_settings(self):
self.assertEqual(tools.apple_sdk_name(FakeSettings('Macos', 'ios_fat')), 'macosx')
self.assertEqual(tools.apple_sdk_name(FakeSettings('iOS', 'ios_fat')), 'iphoneos')
self.assertEqual(tools.apple_sdk_name(FakeSettings('watchOS', 'ios_fat')), 'watchos')
self.assertEqual(tools.apple_sdk_name(FakeSettings('tvOS', 'ios_fat')), 'appletvos')
self.assertIsNone(tools.apple_sdk_name(FakeSettings('ConanOS', 'ios_fat')))

def test_deployment_target_env_name(self):
self.assertEqual(tools.apple_deployment_target_env('Macos', "10.1"),
{"MACOSX_DEPLOYMENT_TARGET": "10.1"})
Expand Down