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

Promote to_apple_arch in conan.tools.apple #11915

Merged
merged 2 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions conan/tools/apple/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
# from conan.tools.apple.apple import apple_dot_clean
# from conan.tools.apple.apple import apple_sdk_name
# from conan.tools.apple.apple import apple_deployment_target_flag
# from conan.tools.apple.apple import to_apple_arch
from conan.tools.apple.apple import fix_apple_shared_install_name, is_apple_os
from conan.tools.apple.apple import fix_apple_shared_install_name, is_apple_os, to_apple_arch
from conan.tools.apple.xcodedeps import XcodeDeps
from conan.tools.apple.xcodebuild import XcodeBuild
from conan.tools.apple.xcodetoolchain import XcodeToolchain
13 changes: 4 additions & 9 deletions conan/tools/apple/apple.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os

from conans.util.runners import check_output_runner
from conans.client.tools.apple import to_apple_arch as _to_apple_arch


def is_apple_os(conanfile):
Expand All @@ -11,16 +12,10 @@ def is_apple_os(conanfile):
return str(os_) in ['Macos', 'iOS', 'watchOS', 'tvOS']


def to_apple_arch(arch):
def to_apple_arch(conanfile):
"""converts conan-style architecture into Apple-style arch"""
return {'x86': 'i386',
'x86_64': 'x86_64',
'armv7': 'armv7',
'armv8': 'arm64',
'armv8_32': 'arm64_32',
'armv8.3': 'arm64e',
'armv7s': 'armv7s',
'armv7k': 'armv7k'}.get(str(arch))
arch_ = conanfile.settings.get_safe("arch")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am slightly concerned about some recent tools similar to this, as I am wondering what will happen if at some point users want to use it for the build context. They will not be able. So the options would be:

  • To expose the underlying dict, something like apple_archs(arch)
  • To create a new to_apple_arch_build(conanfile) (a bit ugly?)

I am fine with this change at this moment, but something to have in mind and maybe discuss.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point! If there's a risk that we may end up with too many small tool functions that have a similar issue, I'd be more inclined to try and sort this out sooner rather than later. to_apple_arch has very few uses in Conan Center at the moment, so we may have an opportunity to get it right to cover all cases (although we also want to avoid a repeat of the situations where recipes are using it from the "private" module apple.apple)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are some other alternatives, like:

  • change context in scope via RAII, e.g. with context_switch(ctx="build"): do something
  • provide a nice moniker like if is_apple_arch(conanfile.build) where conanfile.build is ConanFIle instance with context switched to build

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related #11926

return _to_apple_arch(arch_)


def _guess_apple_sdk_name(os_, arch):
Expand Down
4 changes: 2 additions & 2 deletions conan/tools/apple/xcodebuild.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import os

from conan.tools.apple.apple import to_apple_arch
from conan.tools.apple import to_apple_arch
from conans.errors import ConanException


class XcodeBuild(object):
def __init__(self, conanfile):
self._conanfile = conanfile
self._build_type = conanfile.settings.get_safe("build_type")
self._arch = to_apple_arch(conanfile.settings.get_safe("arch"))
self._arch = to_apple_arch(self._conanfile)
self._sdk = conanfile.settings.get_safe("os.sdk") or ""
self._sdk_version = conanfile.settings.get_safe("os.sdk_version") or ""

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 @@ -7,7 +7,7 @@
from conan.tools._check_build_profile import check_using_build_profile
from conans.errors import ConanException
from conans.util.files import load, save
from conan.tools.apple.apple import to_apple_arch
from conans.client.tools.apple import to_apple_arch

GLOBAL_XCCONFIG_TEMPLATE = textwrap.dedent("""\
// Includes both the toolchain and the dependencies
Expand Down
4 changes: 2 additions & 2 deletions conan/tools/apple/xcodetoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from conan.tools._check_build_profile import check_using_build_profile
from conan.tools._compilers import cppstd_flag
from conan.tools.apple.apple import to_apple_arch
from conan.tools.apple import to_apple_arch
from conan.tools.apple.xcodedeps import GLOBAL_XCCONFIG_FILENAME, GLOBAL_XCCONFIG_TEMPLATE, \
_add_includes_to_file_or_create, _xcconfig_settings_filename, _xcconfig_conditional
from conans.util.files import save
Expand Down Expand Up @@ -36,7 +36,7 @@ class XcodeToolchain(object):
def __init__(self, conanfile):
self._conanfile = conanfile
arch = conanfile.settings.get_safe("arch")
self.architecture = to_apple_arch(arch) or arch
self.architecture = to_apple_arch(self._conanfile) or arch
self.configuration = conanfile.settings.build_type
self.sdk = conanfile.settings.get_safe("os.sdk")
self.sdk_version = conanfile.settings.get_safe("os.sdk_version")
Expand Down
5 changes: 2 additions & 3 deletions conan/tools/cmake/toolchain/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,7 @@ def context(self):
if not is_apple_os(self._conanfile):
return None

arch = self._conanfile.settings.get_safe("arch")
host_architecture = to_apple_arch(arch)
host_architecture = to_apple_arch(self._conanfile)
host_os_version = self._conanfile.settings.get_safe("os.version")
host_sdk_name = self._apple_sdk_name()
is_debug = self._conanfile.settings.get_safe('build_type') == "Debug"
Expand Down Expand Up @@ -808,7 +807,7 @@ def _get_cross_build(self):
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")
_system_processor = to_apple_arch(arch_host)
_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")
Expand Down
2 changes: 1 addition & 1 deletion conan/tools/gnu/autotoolstoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ 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"))
apple_arch = to_apple_arch(self._conanfile)
# 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
2 changes: 1 addition & 1 deletion conan/tools/meson/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def _resolve_apple_flags_and_variables(self, build_env):
raise ConanException("Please, specify a suitable value for os.sdk.")

# Calculating the main Apple flags
arch = to_apple_arch(self._conanfile.settings.get_safe("arch"))
arch = to_apple_arch(self._conanfile)
self.apple_arch_flag = ["-arch", arch] if arch else []
self.apple_isysroot_flag = ["-isysroot", sdk_path] if sdk_path else []
self.apple_min_version_flag = [apple_min_version_flag(self._conanfile)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from conan.tools.apple.apple import to_apple_arch
from conans.client.tools.apple import to_apple_arch
from conans.test.assets.autotools import gen_makefile
from conans.test.assets.sources import gen_function_h, gen_function_cpp
from conans.test.utils.tools import TestClient
Expand Down
24 changes: 24 additions & 0 deletions conans/test/unittests/tools/apple/test_apple_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from conans.test.utils.mocks import ConanFileMock, MockSettings
from conan.tools.apple import is_apple_os, to_apple_arch

def test_tools_apple_is_apple_os():
conanfile = ConanFileMock()

conanfile.settings = MockSettings({"os": "Macos"})
assert is_apple_os(conanfile) == True

conanfile.settings = MockSettings({"os": "watchOS"})
assert is_apple_os(conanfile) == True

conanfile.settings = MockSettings({"os": "Windows"})
assert is_apple_os(conanfile) == False


def test_tools_apple_to_apple_arch():
conanfile = ConanFileMock()

conanfile.settings = MockSettings({"arch": "armv8"})
assert to_apple_arch(conanfile) == "arm64"

conanfile.settings = MockSettings({"arch": "x86_64"})
assert to_apple_arch(conanfile) == "x86_64"