Skip to content

Commit

Permalink
Feature: Use CMAKE_OSX_DEPLOYMENT_TARGET to get -version-min set in C…
Browse files Browse the repository at this point in the history
…MakeToolchain (#9301)

* - set CMAKE_OSX_DEPLOYMENT_TARGET

Signed-off-by: SSE4 <tomskside@gmail.com>

* - add unit test

Signed-off-by: SSE4 <tomskside@gmail.com>
  • Loading branch information
SSE4 committed Jul 21, 2021
1 parent ae7732b commit 2f79057
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
13 changes: 9 additions & 4 deletions conan/tools/cmake/toolchain.py
Expand Up @@ -313,12 +313,14 @@ class AppleSystemBlock(Block):
{% if CMAKE_SYSTEM_VERSION is defined %}
set(CMAKE_SYSTEM_VERSION {{ CMAKE_SYSTEM_VERSION }})
{% endif %}
set(DEPLOYMENT_TARGET ${CONAN_SETTINGS_HOST_MIN_OS_VERSION})
# Set the architectures for which to build.
set(CMAKE_OSX_ARCHITECTURES {{ CMAKE_OSX_ARCHITECTURES }} CACHE STRING "" FORCE)
# Setting CMAKE_OSX_SYSROOT SDK, when using Xcode generator the name is enough
# but full path is necessary for others
set(CMAKE_OSX_SYSROOT {{ CMAKE_OSX_SYSROOT }} CACHE STRING "" FORCE)
{% if CMAKE_OSX_DEPLOYMENT_TARGET is defined %}
set(CMAKE_OSX_DEPLOYMENT_TARGET {{ CMAKE_OSX_DEPLOYMENT_TARGET }})
{% endif %}
""")

def _get_architecture(self):
Expand Down Expand Up @@ -358,9 +360,6 @@ def context(self):
host_os_version = self._conanfile.settings.get_safe("os.version")
host_sdk_name = self._apple_sdk_name()

# TODO: Discuss how to handle CMAKE_OSX_DEPLOYMENT_TARGET to set min-version
# add a setting? check an option and if not present set a default?
# default to os.version?
ctxt_toolchain = {}
if host_sdk_name:
ctxt_toolchain["CMAKE_OSX_SYSROOT"] = host_sdk_name
Expand All @@ -371,6 +370,12 @@ def context(self):
ctxt_toolchain["CMAKE_SYSTEM_NAME"] = os_
ctxt_toolchain["CMAKE_SYSTEM_VERSION"] = host_os_version

if host_os_version:
# https://cmake.org/cmake/help/latest/variable/CMAKE_OSX_DEPLOYMENT_TARGET.html
# Despite the OSX part in the variable name(s) they apply also to other SDKs than
# macOS like iOS, tvOS, or watchOS.
ctxt_toolchain["CMAKE_OSX_DEPLOYMENT_TARGET"] = host_os_version

return ctxt_toolchain


Expand Down
25 changes: 25 additions & 0 deletions conans/test/unittests/tools/cmake/test_cmaketoolchain.py
Expand Up @@ -140,3 +140,28 @@ def test_user_toolchain(conanfile):
toolchain = CMakeToolchain(conanfile)
content = toolchain.content
assert 'include(' not in content

@pytest.fixture
def conanfile_apple():
c = ConanFile(Mock(), None)
c.settings = "os", "compiler", "build_type", "arch"
c.initialize(Settings({"os": {"Macos": {"version": ["10.15"]}},
"compiler": {"apple-clang": {"libcxx": ["libc++"]}},
"build_type": ["Release"],
"arch": ["x86"]}), EnvValues())
c.settings.build_type = "Release"
c.settings.arch = "x86"
c.settings.compiler = "apple-clang"
c.settings.compiler.libcxx = "libc++"
c.settings.os = "Macos"
c.settings.os.version = "10.15"
c.conf = Conf()
c.folders.set_base_generators(".")
c._conan_node = Mock()
c._conan_node.dependencies = []
return c

def test_osx_deployment_target(conanfile_apple):
toolchain = CMakeToolchain(conanfile_apple)
content = toolchain.content
assert 'set(CMAKE_OSX_DEPLOYMENT_TARGET 10.15)' in content

0 comments on commit 2f79057

Please sign in to comment.