Skip to content

Commit

Permalink
Refactoring minor things
Browse files Browse the repository at this point in the history
  • Loading branch information
franramirez688 committed Jan 24, 2022
1 parent 79bbc24 commit 247caf3
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 84 deletions.
6 changes: 3 additions & 3 deletions conan/tools/apple/apple.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def is_apple_os(os_):
return str(os_) in ['Macos', 'iOS', 'watchOS', 'tvOS']


def to_apple_arch(arch):
def to_apple_arch(arch, default=None):
"""converts conan-style architecture into Apple-style arch"""
return {'x86': 'i386',
'x86_64': 'x86_64',
Expand All @@ -12,12 +12,12 @@ def to_apple_arch(arch):
'armv8_32': 'arm64_32',
'armv8.3': 'arm64e',
'armv7s': 'armv7s',
'armv7k': 'armv7k'}.get(str(arch))
'armv7k': 'armv7k'}.get(arch, default)


def apple_min_version_flag(os_version, os_sdk, subsystem):
"""compiler flag name which controls deployment target"""
if not os_version:
if not os_version or not os_sdk:
return ''

# FIXME: This guess seems wrong, nothing has to be guessed, but explicit
Expand Down
2 changes: 1 addition & 1 deletion conan/tools/apple/xcodedeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(self, conanfile):
self.configuration = conanfile.settings.get_safe("build_type")

arch = conanfile.settings.get_safe("arch")
self.architecture = to_apple_arch(arch) or arch
self.architecture = to_apple_arch(arch, default=arch)

# TODO: check if it makes sense to add a subsetting for sdk version
# related to: https://github.com/conan-io/conan/issues/9608
Expand Down
2 changes: 2 additions & 0 deletions conan/tools/build/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def architecture_flag(settings):
# FIXME: This might be conflicting with Autotools --target cli arg
apple_arch = to_apple_arch(arch)
if apple_arch:
# TODO: Could we define anything like `to_apple_target()`?
# Check https://github.com/rust-lang/rust/issues/48862
return '--target=%s-apple-ios-macabi' % apple_arch
elif arch in ['x86_64', 'sparcv9', 's390x']:
return '-m64'
Expand Down
37 changes: 7 additions & 30 deletions conan/tools/cmake/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from jinja2 import Template

from conan.tools.apple.apple import is_apple_os
from conan.tools.apple.apple import is_apple_os, to_apple_arch
from conan.tools.build.flags import architecture_flag, libcxx_flag
from conan.tools.build import build_jobs, use_win_mingw, cross_building
from conan.tools.cmake.utils import is_multi_configuration
Expand Down Expand Up @@ -321,41 +321,18 @@ class AppleSystemBlock(Block):
{% endif %}
""")

def _get_architecture(self):
# check valid combinations of architecture - os ?
# for iOS a FAT library valid for simulator and device
# can be generated if multiple archs are specified:
# "-DCMAKE_OSX_ARCHITECTURES=armv7;armv7s;arm64;i386;x86_64"
arch = self._conanfile.settings.get_safe("arch")
return {"x86": "i386",
"x86_64": "x86_64",
"armv8": "arm64",
"armv8_32": "arm64_32"}.get(arch, arch)

def _apple_sdk_name(self):
"""
Returns the 'os.sdk' (SDK name) field value. Every user should specify it because
there could be several ones depending on the OS architecture.
Note: In case of MacOS it'll be the same for all the architectures.
"""
os_ = self._conanfile.settings.get_safe('os')
os_sdk = self._conanfile.settings.get_safe('os.sdk')
if os_sdk:
return os_sdk
elif os_ == "Macos": # it has only a single value for all the architectures for now
return "macosx"
else:
raise ConanException("Please, specify a suitable value for os.sdk.")

def context(self):
os_ = self._conanfile.settings.get_safe("os")
if os_ not in ['Macos', 'iOS', 'watchOS', 'tvOS']:
return None

host_architecture = self._get_architecture()
arch = self._conanfile.settings.get_safe("arch")
# check valid combinations of architecture - os ?
# for iOS a FAT library valid for simulator and device can be generated
# if multiple archs are specified "-DCMAKE_OSX_ARCHITECTURES=armv7;armv7s;arm64;i386;x86_64"
host_architecture = to_apple_arch(arch)
host_os_version = self._conanfile.settings.get_safe("os.version")
host_sdk_name = self._apple_sdk_name()
host_sdk_name = self._conanfile.settings.get_safe('os.sdk', "macosx")

ctxt_toolchain = {}
if host_sdk_name:
Expand Down
11 changes: 5 additions & 6 deletions conan/tools/meson/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,13 @@ def _add_apple_flags(self):
if not sdk_path and self.cross_build:
raise ConanException("You must provide a valid SDK path for cross-compilation.")

# TODO: Delete this os_sdk check whenever the _guess_apple_sdk_name() function disappears
os_sdk = conanfile.settings.get_safe('os.sdk')
if not os_sdk and os_ != "Macos":
raise ConanException("Please, specify a suitable value for os.sdk.")

os_sdk = conanfile.settings.get_safe("os.sdk", "macosx")
arch = to_apple_arch(conanfile.settings.get_safe("arch"))
os_version = conanfile.settings.get_safe("os.version")
subsystem = conanfile.settings.get_safe("os.subsystem")

# Calculating the main Apple flags
deployment_target_flag = apple_min_version_flag(conanfile)
deployment_target_flag = apple_min_version_flag(os_version, os_sdk, subsystem)
sysroot_flag = "-isysroot " + sdk_path if sdk_path else ""
arch_flag = "-arch " + arch if arch else ""

Expand Down
7 changes: 3 additions & 4 deletions conans/client/conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
Linux:
Macos:
version: [None, "10.6", "10.7", "10.8", "10.9", "10.10", "10.11", "10.12", "10.13", "10.14", "10.15", "11.0", "12.0", "13.0"]
sdk: [None, "macosx"]
subsystem: [None, catalyst]
Android:
api_level: ANY
Expand All @@ -27,16 +26,16 @@
"11.0", "11.1", "11.2", "11.3", "11.4", "12.0", "12.1", "12.2", "12.3", "12.4",
"13.0", "13.1", "13.2", "13.3", "13.4", "13.5", "13.6", "13.7",
"14.0", "14.1", "14.2", "14.3", "14.4", "14.5", "14.6", "14.7", "14.8", "15.0", "15.1"]
sdk: [None, "iphoneos", "iphonesimulator"]
sdk: ["iphoneos", "iphonesimulator"]
watchOS:
version: ["4.0", "4.1", "4.2", "4.3", "5.0", "5.1", "5.2", "5.3", "6.0", "6.1", "6.2",
"7.0", "7.1", "7.2", "7.3", "7.4", "7.5", "7.6", "8.0", "8.1"]
sdk: [None, "watchos", "watchsimulator"]
sdk: ["watchos", "watchsimulator"]
tvOS:
version: ["11.0", "11.1", "11.2", "11.3", "11.4", "12.0", "12.1", "12.2", "12.3", "12.4",
"13.0", "13.2", "13.3", "13.4", "14.0", "14.2", "14.3", "14.4", "14.5", "14.6", "14.7",
"15.0", "15.1"]
sdk: [None, "appletvos", "appletvsimulator"]
sdk: ["appletvos", "appletvsimulator"]
FreeBSD:
SunOS:
AIX:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest
from parameterized import parameterized

from conans.client.tools.apple import XCRun, to_apple_arch
from conan.tools.apple.apple import XCRun, to_apple_arch
from conans.test.assets.sources import gen_function_cpp, gen_function_h
from conans.test.utils.tools import TestClient

Expand Down Expand Up @@ -54,21 +54,21 @@ def build(self):
@pytest.mark.tool_meson
@pytest.mark.skipif(sys.version_info.major == 2, reason="Meson not supported in Py2")
@pytest.mark.skipif(platform.system() != "Darwin", reason="requires Xcode")
@pytest.mark.parametrize("arch, os_, os_version, sdk", [
@pytest.mark.parametrize("arch, os_, os_version, os_sdk", [
('armv8', 'iOS', '10.0', 'iphoneos'),
('armv7', 'iOS', '10.0', 'iphoneos'),
('x86', 'iOS', '10.0', 'iphonesimulator'),
('x86_64', 'iOS', '10.0', 'iphonesimulator'),
('armv8', 'Macos', None, None) # MacOS M1
])
def test_apple_meson_toolchain_cross_compiling(arch, os_, os_version, sdk):
def test_apple_meson_toolchain_cross_compiling(arch, os_, os_version, os_sdk):
profile = textwrap.dedent("""
include(default)
[settings]
os = {os}
os.version = {os_version}
os.sdk = {os_sdk}
{os_sdk}
arch = {arch}
compiler = apple-clang
compiler.version = 12.0
Expand All @@ -78,7 +78,7 @@ def test_apple_meson_toolchain_cross_compiling(arch, os_, os_version, sdk):
tools.apple:sdk_path={sdk_path}
""")

xcrun = XCRun(None, sdk)
xcrun = XCRun(None, os_sdk)
sdk_path = xcrun.sdk_path

hello_h = gen_function_h(name="hello")
Expand All @@ -87,7 +87,7 @@ def test_apple_meson_toolchain_cross_compiling(arch, os_, os_version, sdk):
profile = profile.format(
os=os_,
os_version=os_version,
os_sdk=sdk,
os_sdk="os.sdk = " + os_sdk if os_sdk else "",
arch=arch,
sdk_path=sdk_path)

Expand Down
41 changes: 7 additions & 34 deletions conans/test/unittests/util/apple_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@

import pytest

from conan.tools.apple.apple import apple_sdk_name, to_apple_arch, XCRun, apple_min_version_flag, \
from conan.tools.apple.apple import to_apple_arch, XCRun, apple_min_version_flag, \
is_apple_os
from conans.test.utils.mocks import MockConanfile


class FakeSettings(object):
Expand All @@ -35,23 +34,20 @@ def get_safe(self, name):

class TestApple:
@pytest.mark.parametrize("os_, version, sdk, subsystem, flag",
[("Macos", "10.1", None, None, '-mmacosx-version-min=10.1'),
("Macos", "10.1", "macosx", None, '-mmacosx-version-min=10.1'),
("iOS", "10.1", None, None, '-mios-version-min=10.1'),
[("Macos", "10.1", "macosx", None, '-mmacosx-version-min=10.1'),
("iOS", "10.1", "iphoneos", None, '-mios-version-min=10.1'),
("iOS", "10.1", "iphonesimulator", None,
'-mios-simulator-version-min=10.1'),
("watchOS", "10.1", None, None, '-mwatchos-version-min=10.1'),
("watchOS", "10.1", "watchos", None, '-mwatchos-version-min=10.1'),
("watchOS", "10.1", "watchsimulator", None,
'-mwatchos-simulator-version-min=10.1'),
("tvOS", "10.1", None, None, '-mtvos-version-min=10.1'),
("tvOS", "10.1", "appletvos", None, '-mtvos-version-min=10.1'),
("tvOS", "10.1", "appletvsimulator", None,
'-mtvos-simulator-version-min=10.1'),
("Macos", "10.1", None, "catalyst", '-mios-version-min=10.1'),
("Macos", "10.1", "macosx", "catalyst", '-mios-version-min=10.1'),
("Solaris", "10.1", None, None, ''),
("Macos", "10.1", None, None, ''),
("Macos", None, "macosx", None, '')
])
def test_deployment_target_flag_name(self, os_, version, sdk, subsystem, flag):
assert apple_min_version_flag(version, sdk, subsystem) == flag
Expand All @@ -77,30 +73,7 @@ def test_to_apple_arch(self):
self.assertEqual(to_apple_arch('armv8.3'), 'arm64e')
self.assertEqual(to_apple_arch('armv8_32'), 'arm64_32')
self.assertIsNone(to_apple_arch('mips'))

def test_apple_sdk_name(self):
self.assertEqual(apple_sdk_name(FakeSettings('Macos', 'x86')), 'macosx')
self.assertEqual(apple_sdk_name(FakeSettings('Macos', 'x86_64')), 'macosx')
self.assertEqual(apple_sdk_name(FakeSettings('iOS', 'x86_64')), 'iphonesimulator')
self.assertEqual(apple_sdk_name(FakeSettings('iOS', 'armv7')), 'iphoneos')
self.assertEqual(apple_sdk_name(FakeSettings('watchOS', 'x86_64')), 'watchsimulator')
self.assertEqual(apple_sdk_name(FakeSettings('watchOS', 'armv7k')), 'watchos')
self.assertEqual(apple_sdk_name(FakeSettings('tvOS', 'x86')), 'appletvsimulator')
self.assertEqual(apple_sdk_name(FakeSettings('tvOS', 'armv8')), 'appletvos')
self.assertIsNone(apple_sdk_name(FakeSettings('Windows', 'x86')))

self.assertEqual(apple_sdk_name(FakeSettings('iOS', 'armv8')), 'iphoneos')
self.assertEqual(apple_sdk_name(FakeSettings('iOS', 'armv8', 'iphoneos')),
'iphoneos')
self.assertEqual(apple_sdk_name(FakeSettings('iOS', 'armv8', 'iphonesimulator')),
'iphonesimulator')

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

@pytest.mark.skipif(platform.system() != "Darwin", reason="Requires OSX")
def test_xcrun(self):
Expand All @@ -124,12 +97,12 @@ def _common_asserts(xcrun_):
# Simulator
self.assertNotIn("iPhoneOS", xcrun.sdk_path)

settings = FakeSettings('iOS', 'armv7')
settings = FakeSettings('iOS', 'armv7', os_sdk="iphoneos")
xcrun = XCRun(settings)
_common_asserts(xcrun)
self.assertIn("iPhoneOS", xcrun.sdk_path)

settings = FakeSettings('watchOS', 'armv7')
settings = FakeSettings('watchOS', 'armv7', os_sdk="watchos")
xcrun = XCRun(settings)
_common_asserts(xcrun)
self.assertIn("WatchOS", xcrun.sdk_path)
Expand Down

0 comments on commit 247caf3

Please sign in to comment.