Skip to content

Commit

Permalink
removed legacy apple tools (but new ones need to be modernized yet) (#…
Browse files Browse the repository at this point in the history
…10277)

* removed legacy apple tools (but new ones need to be modernized yet)

* removed legacy apple.py file

* commented import

* wip

* fix tests

* removed dot_clean apple tool

* wip

* Refactoring minor things

* changed to function

* Fixing tests

* Using conanfile instead

* Fixed tests

* Simplified test

* XCrun only for testing

* Keeping same behaviour in CMakeToolchain -> AppleSystemBlock

Co-authored-by: Francisco Ramirez de Anton <[email protected]>
  • Loading branch information
memsharded and franramirez688 authored Jan 26, 2022
1 parent db253fe commit be66c49
Show file tree
Hide file tree
Showing 19 changed files with 243 additions and 510 deletions.
138 changes: 23 additions & 115 deletions conan/tools/apple/apple.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from conans.errors import ConanException


def is_apple_os(os_):
"""returns True if OS is Apple one (Macos, iOS, watchOS or tvOS"""
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,137 +15,42 @@ 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 _guess_apple_sdk_name(os_, arch):
if str(arch).startswith('x86'):
return {'Macos': 'macosx',
'iOS': 'iphonesimulator',
'watchOS': 'watchsimulator',
'tvOS': 'appletvsimulator'}.get(str(os_))
else:
return {'Macos': 'macosx',
'iOS': 'iphoneos',
'watchOS': 'watchos',
'tvOS': 'appletvos'}.get(str(os_), None)

def get_apple_sdk_name(conanfile):
"""
Returns the 'os.sdk' (SDK name) field value. Every user should specify it because
there could be several ones depending on the OS architecture.
def apple_sdk_name(settings):
"""returns proper SDK name suitable for OS and architecture
we're building for (considering simulators)"""
arch = settings.get_safe('arch')
os_ = settings.get_safe('os')
os_sdk = settings.get_safe('os.sdk')
return os_sdk or _guess_apple_sdk_name(os_, arch)
Note: In case of MacOS it'll be the same for all the architectures.
"""
os_ = conanfile.settings.get_safe('os')
os_sdk = 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
return "macosx"
elif is_apple_os(os_):
raise ConanException("Please, specify a suitable value for os.sdk.")


def apple_min_version_flag(conanfile):
def apple_min_version_flag(os_version, os_sdk, subsystem):
"""compiler flag name which controls deployment target"""
os_version = conanfile.settings.get_safe("os.version")
if not os_version:
if not os_version or not os_sdk:
return ''

os_ = conanfile.settings.get_safe("os")
os_sdk = conanfile.settings.get_safe("os.sdk")
os_subsystem = conanfile.settings.get_safe("os.subsystem")
arch = conanfile.settings.get_safe("arch")

if not os_version:
return ''
os_sdk = os_sdk if os_sdk else _guess_apple_sdk_name(os_, arch)
# FIXME: This guess seems wrong, nothing has to be guessed, but explicit
flag = {'macosx': '-mmacosx-version-min',
'iphoneos': '-mios-version-min',
'iphonesimulator': '-mios-simulator-version-min',
'watchos': '-mwatchos-version-min',
'watchsimulator': '-mwatchos-simulator-version-min',
'appletvos': '-mtvos-version-min',
'appletvsimulator': '-mtvos-simulator-version-min'}.get(str(os_sdk))
if os_subsystem == 'catalyst':
if subsystem == 'catalyst':
# especial case, despite Catalyst is macOS, it requires an iOS version argument
flag = '-mios-version-min'
if not flag:
return ''
return "%s=%s" % (flag, os_version)


def apple_sdk_path(conanfile):
sdk_path = conanfile.conf["tools.apple:sdk_path"]
if not sdk_path:
sdk_path = XCRun(conanfile.settings).sdk_path
return sdk_path


class XCRun(object):

def __init__(self, settings, sdk=None):
"""sdk=False will skip the flag
sdk=None will try to adjust it automatically"""
if sdk is None and settings:
sdk = apple_sdk_name(settings)
self.sdk = sdk

def _invoke(self, args):
def cmd_output(cmd):
from conans.util.runners import check_output_runner
return check_output_runner(cmd).strip()

command = ['xcrun']
if self.sdk:
command.extend(['-sdk', self.sdk])
command.extend(args)
return cmd_output(command)

def find(self, tool):
"""find SDK tools (e.g. clang, ar, ranlib, lipo, codesign, etc.)"""
return self._invoke(['--find', tool])

@property
def sdk_path(self):
"""obtain sdk path (aka apple sysroot or -isysroot"""
return self._invoke(['--show-sdk-path'])

@property
def sdk_version(self):
"""obtain sdk version"""
return self._invoke(['--show-sdk-version'])

@property
def sdk_platform_path(self):
"""obtain sdk platform path"""
return self._invoke(['--show-sdk-platform-path'])

@property
def sdk_platform_version(self):
"""obtain sdk platform version"""
return self._invoke(['--show-sdk-platform-version'])

@property
def cc(self):
"""path to C compiler (CC)"""
return self.find('clang')

@property
def cxx(self):
"""path to C++ compiler (CXX)"""
return self.find('clang++')

@property
def ar(self):
"""path to archiver (AR)"""
return self.find('ar')

@property
def ranlib(self):
"""path to archive indexer (RANLIB)"""
return self.find('ranlib')

@property
def strip(self):
"""path to symbol removal utility (STRIP)"""
return self.find('strip')

@property
def libtool(self):
"""path to libtool"""
return self.find('libtool')
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
4 changes: 3 additions & 1 deletion conan/tools/build/flags.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from conans.client.tools import to_apple_arch
from conan.tools.apple.apple import to_apple_arch
from conans.model.version import Version


Expand All @@ -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, get_apple_sdk_name
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 @@ -322,41 +322,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, default=arch)
host_os_version = self._conanfile.settings.get_safe("os.version")
host_sdk_name = self._apple_sdk_name()
host_sdk_name = get_apple_sdk_name(self._conanfile)

ctxt_toolchain = {}
if host_sdk_name:
Expand Down
18 changes: 13 additions & 5 deletions conan/tools/gnu/autotoolstoolchain.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from conan.tools import args_to_string
from conan.tools.apple.apple import apple_min_version_flag, to_apple_arch, \
apple_sdk_path
from conan.tools.apple.apple import apple_min_version_flag, to_apple_arch, get_apple_sdk_name
from conan.tools.build.cross_building import cross_building
from conan.tools.build.flags import architecture_flag, build_type_flags, cppstd_flag, libcxx_flag, \
build_type_link_flags
from conan.tools.env import Environment
from conan.tools.files import save_toolchain_args
from conan.tools.gnu.get_gnu_triplet import _get_gnu_triplet
from conans.errors import ConanException


class AutotoolsToolchain:
Expand Down Expand Up @@ -47,7 +47,12 @@ def __init__(self, conanfile, namespace=None):

self.apple_arch_flag = self.apple_isysroot_flag = None

self.apple_min_version_flag = apple_min_version_flag(self._conanfile)
os_sdk = get_apple_sdk_name(conanfile)
os_version = conanfile.settings.get_safe("os.version")
subsystem = conanfile.settings.get_safe("os.subsystem")

self.apple_min_version_flag = apple_min_version_flag(os_version, os_sdk, subsystem)

if cross_building(self._conanfile):
os_host = conanfile.settings.get_safe("os")
arch_host = conanfile.settings.get_safe("arch")
Expand All @@ -59,8 +64,11 @@ def __init__(self, conanfile, namespace=None):

# Apple Stuff
if os_build == "Macos":
sdk_path = apple_sdk_path(conanfile)
apple_arch = to_apple_arch(self._conanfile.settings.get_safe("arch"))
# SDK path is mandatory for cross-building
sdk_path = conanfile.conf["tools.apple:sdk_path"]
if not sdk_path:
raise ConanException("You must provide a valid SDK path for cross-compilation.")
apple_arch = to_apple_arch(arch_host)
# https://man.archlinux.org/man/clang.1.en#Target_Selection_Options
self.apple_arch_flag = "-arch {}".format(apple_arch) if apple_arch else None
# -isysroot makes all includes for your library relative to the build directory
Expand Down
14 changes: 7 additions & 7 deletions conan/tools/meson/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from jinja2 import Template

from conan.tools.build.cross_building import cross_building
from conan.tools.apple.apple import to_apple_arch, is_apple_os, apple_min_version_flag
from conan.tools.apple.apple import to_apple_arch, is_apple_os, apple_min_version_flag, \
get_apple_sdk_name
from conan.tools.env import VirtualBuildEnv
from conan.tools.meson.helpers import *
from conan.tools.microsoft import VCVars, msvc_runtime_flag
Expand Down Expand Up @@ -158,14 +159,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 = get_apple_sdk_name(conanfile)
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
2 changes: 0 additions & 2 deletions conans/client/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# noinspection PyUnresolvedReferences
from .apple import *
# noinspection PyUnresolvedReferences
from .files import *
# noinspection PyUnresolvedReferences
from .scm import *
Expand Down
Loading

0 comments on commit be66c49

Please sign in to comment.