Repository: conan-io/conan

Issue:
[feature] Forward the OS X version to CMake helper
Following PR #3091 , there is now an optional `os.version` setting for OS X. Following [this comment](https://github.com/conan-io/conan/issues/2534#issuecomment-399117915) from @lasote, we plan to use it as the minimum osx version (the `-mmacosx-version-min` compiler flag).

CMake makes a provision for that thanks to the [`CMAKE_OSX_DEPLOYMENT_TARGET`](https://cmake.org/cmake/help/latest/variable/CMAKE_OSX_DEPLOYMENT_TARGET.html).

Should `os.version` be automatically used by the CMake helper, like many other settings are implicitly used to define some CMake variables?



Discussion:

Resolving patch:
diff --git a/conans/client/build/cmake_flags.py b/conans/client/build/cmake_flags.py
--- a/conans/client/build/cmake_flags.py
+++ b/conans/client/build/cmake_flags.py
@@ -168,6 +168,8 @@ def _cmake_cross_build_defines(self):
                         ret["CMAKE_SYSTEM_NAME"] = "Generic"
         if os_ver:
             ret["CMAKE_SYSTEM_VERSION"] = os_ver
+            if str(os_) == "Macos":
+                ret["CMAKE_OSX_DEPLOYMENT_TARGET"] = os_ver
 
         # system processor
         cmake_system_processor = os.getenv("CONAN_CMAKE_SYSTEM_PROCESSOR")
@@ -235,7 +237,7 @@ def get_definitions(self):
         ret.update(build_type_definition(build_type, self.generator))
         ret.update(runtime_definition(runtime))
 
-        if str(os_).lower() == "macos":
+        if str(os_) == "Macos":
             if arch == "x86":
                 ret["CMAKE_OSX_ARCHITECTURES"] = "i386"
 


Test patch:
diff --git a/conans/test/build_helpers/cmake_test.py b/conans/test/build_helpers/cmake_test.py
--- a/conans/test/build_helpers/cmake_test.py
+++ b/conans/test/build_helpers/cmake_test.py
@@ -4,6 +4,7 @@
 import sys
 import unittest
 import platform
+import mock
 
 from collections import namedtuple
 
@@ -962,6 +963,43 @@ def test_cmake_system_version_android(self):
             cmake = CMake(conan_file)
             self.assertEquals(cmake.definitions["CMAKE_SYSTEM_VERSION"], "32")
 
+    @mock.patch('platform.system', return_value="Macos")
+    def test_cmake_system_version_osx(self, _):
+        settings = Settings.loads(default_settings_yml)
+        settings.os = "Macos"
+
+        # No version defined
+        conanfile = ConanFileMock()
+        conanfile.settings = settings
+        cmake = CMake(conanfile)
+        self.assertFalse("CMAKE_OSX_DEPLOYMENT_TARGET" in cmake.definitions)
+        self.assertFalse("CMAKE_SYSTEM_NAME" in cmake.definitions)
+        self.assertFalse("CMAKE_SYSTEM_VERSION" in cmake.definitions)
+
+        # Version defined using Conan env variable
+        with tools.environment_append({"CONAN_CMAKE_SYSTEM_VERSION": "23"}):
+            conanfile = ConanFileMock()
+            conanfile.settings = settings
+            cmake = CMake(conanfile)
+            self.assertEqual(cmake.definitions["CMAKE_SYSTEM_VERSION"], "23")
+            self.assertEqual(cmake.definitions["CMAKE_OSX_DEPLOYMENT_TARGET"], "23")
+
+        # Version defined in settings
+        settings.os.version = "10.9"
+        conanfile = ConanFileMock()
+        conanfile.settings = settings
+        cmake = CMake(conanfile)
+        self.assertEqual(cmake.definitions["CMAKE_SYSTEM_VERSION"], "10.9")
+        self.assertEqual(cmake.definitions["CMAKE_OSX_DEPLOYMENT_TARGET"], "10.9")
+
+        # Version defined in settings AND using Conan env variable
+        with tools.environment_append({"CONAN_CMAKE_SYSTEM_VERSION": "23"}):
+            conanfile = ConanFileMock()
+            conanfile.settings = settings
+            cmake = CMake(conanfile)
+            self.assertEqual(cmake.definitions["CMAKE_SYSTEM_VERSION"], "23")
+            self.assertEqual(cmake.definitions["CMAKE_OSX_DEPLOYMENT_TARGET"], "23")
+
     @staticmethod
     def scape(args):
         pattern = "%s" if sys.platform == "win32" else r"'%s'"