Explorar el Código

Xcode: Add option to set Swift language version

Create a new CMAKE_Swift_LANGUAGE_VERSION variable to specify the
SWIFT_VERSION attribute in a generated Xcode project.  Ideally this
would be a `<LANG>_STANDARD` property but since Swift support is
very minimal we should reserve that property for more complete
treatment later.

Issue: #16326
Brad King hace 9 años
padre
commit
b35568f3f9

+ 1 - 0
Help/manual/cmake-variables.7.rst

@@ -396,6 +396,7 @@ Variables for Languages
    /variable/CMAKE_LANG_SOURCE_FILE_EXTENSIONS
    /variable/CMAKE_LANG_STANDARD_INCLUDE_DIRECTORIES
    /variable/CMAKE_LANG_STANDARD_LIBRARIES
+   /variable/CMAKE_Swift_LANGUAGE_VERSION
    /variable/CMAKE_USER_MAKE_RULES_OVERRIDE_LANG
 
 Variables for CTest

+ 6 - 0
Help/release/dev/xcode-swift-version.rst

@@ -0,0 +1,6 @@
+xcode-swift-version
+-------------------
+
+* The :generator:`Xcode` generator's rudimentary Swift language support
+  learned to honor a new :variable:`CMAKE_Swift_LANGUAGE_VERSION` variable
+  to tell Xcode what version of Swift is used by the source.

+ 5 - 0
Help/variable/CMAKE_Swift_LANGUAGE_VERSION.rst

@@ -0,0 +1,5 @@
+CMAKE_Swift_LANGUAGE_VERSION
+----------------------------
+
+Set to the Swift language version number.  If not set, the legacy "2.3"
+version is assumed.

+ 7 - 1
Source/cmGlobalXCodeGenerator.cxx

@@ -2997,7 +2997,13 @@ bool cmGlobalXCodeGenerator::CreateXCodeObjects(
                                 this->CreateString(this->GeneratorToolset));
   }
   if (this->GetLanguageEnabled("Swift")) {
-    buildSettings->AddAttribute("SWIFT_VERSION", this->CreateString("2.3"));
+    std::string swiftVersion = "2.3";
+    if (const char* vers = this->CurrentMakefile->GetDefinition(
+          "CMAKE_Swift_LANGUAGE_VERSION")) {
+      swiftVersion = vers;
+    }
+    buildSettings->AddAttribute("SWIFT_VERSION",
+                                this->CreateString(swiftVersion));
   }
 
   std::string symroot = root->GetCurrentBinaryDirectory();

+ 4 - 0
Tests/SwiftOnly/CMakeLists.txt

@@ -1,4 +1,8 @@
 cmake_minimum_required(VERSION 3.3)
 project(SwiftOnly Swift)
 
+if(NOT XCODE_VERSION VERSION_LESS 8.0)
+  set(CMAKE_Swift_LANGUAGE_VERSION 3.0)
+endif()
+
 add_executable(SwiftOnly main.swift)