Ver código fonte

Genex: CompilerId now can match against a list of ids.

This allows for expressions like:
 $<$<CXX_COMPILER_ID:Clang,GNU>:-DMY_PRIVATE_DEFINE>
Robert Maynard 6 anos atrás
pai
commit
ec66af2026

+ 17 - 12
Help/manual/cmake-generator-expressions.7.rst

@@ -114,20 +114,25 @@ Variable Queries
   ``1`` if the CMake's platform id matches ``platform_id``
   otherwise ``0``.
   See also the :variable:`CMAKE_SYSTEM_NAME` variable.
-``$<C_COMPILER_ID:compiler_id>``
-  ``1`` if the CMake's compiler id of the C compiler matches ``compiler_id``,
-  otherwise ``0``.
+``$<C_COMPILER_ID:compiler_ids>``
+  where ``compiler_ids`` is a comma-separated list.
+  ``1`` if the CMake's compiler id of the C compiler matches any one
+  of the entries in ``compiler_ids``, otherwise ``0``.
   See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
-``$<CXX_COMPILER_ID:compiler_id>``
-  ``1`` if the CMake's compiler id of the CXX compiler matches ``compiler_id``,
-  otherwise ``0``.
-``$<CUDA_COMPILER_ID:compiler_id>``
-  ``1`` if the CMake's compiler id of the CUDA compiler matches ``compiler_id``,
-  otherwise ``0``.
+``$<CXX_COMPILER_ID:compiler_ids>``
+  where ``compiler_ids`` is a comma-separated list.
+  ``1`` if the CMake's compiler id of the CXX compiler matches any one
+  of the entries in ``compiler_ids``, otherwise ``0``.
   See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
-``$<Fortran_COMPILER_ID:compiler_id>``
-  ``1`` if the CMake's compiler id of the Fortran compiler matches ``compiler_id``,
-  otherwise ``0``.
+``$<CUDA_COMPILER_ID:compiler_ids>``
+  where ``compiler_ids`` is a comma-separated list.
+  ``1`` if the CMake's compiler id of the CUDA compiler matches any one
+  of the entries in ``compiler_ids``, otherwise ``0``.
+  See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
+``$<Fortran_COMPILER_ID:compiler_ids>``
+  where ``compiler_ids`` is a comma-separated list.
+  ``1`` if the CMake's compiler id of the Fortran compiler matches any one
+  of the entries in ``compiler_ids``, otherwise ``0``.
   See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
 ``$<C_COMPILER_VERSION:version>``
   ``1`` if the version of the C compiler matches ``version``, otherwise ``0``.

+ 28 - 25
Source/cmGeneratorExpressionNode.cxx

@@ -632,7 +632,7 @@ struct CompilerIdNode : public cmGeneratorExpressionNode
   {
   }
 
-  int NumExpectedParameters() const override { return OneOrZeroParameters; }
+  int NumExpectedParameters() const override { return ZeroOrMoreParameters; }
 
   std::string Evaluate(
     const std::vector<std::string>& parameters,
@@ -664,36 +664,39 @@ struct CompilerIdNode : public cmGeneratorExpressionNode
     if (parameters.empty()) {
       return compilerId;
     }
-    static cmsys::RegularExpression compilerIdValidator("^[A-Za-z0-9_]*$");
-    if (!compilerIdValidator.find(parameters.front())) {
-      reportError(context, content->GetOriginalExpression(),
-                  "Expression syntax not recognized.");
-      return std::string();
-    }
     if (compilerId.empty()) {
       return parameters.front().empty() ? "1" : "0";
     }
+    static cmsys::RegularExpression compilerIdValidator("^[A-Za-z0-9_]*$");
 
-    if (strcmp(parameters.front().c_str(), compilerId.c_str()) == 0) {
-      return "1";
-    }
+    for (auto& param : parameters) {
 
-    if (cmsysString_strcasecmp(parameters.front().c_str(),
-                               compilerId.c_str()) == 0) {
-      switch (context->LG->GetPolicyStatus(cmPolicies::CMP0044)) {
-        case cmPolicies::WARN: {
-          std::ostringstream e;
-          e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0044);
-          context->LG->GetCMakeInstance()->IssueMessage(
-            MessageType::AUTHOR_WARNING, e.str(), context->Backtrace);
-          CM_FALLTHROUGH;
+      if (!compilerIdValidator.find(param)) {
+        reportError(context, content->GetOriginalExpression(),
+                    "Expression syntax not recognized.");
+        return std::string();
+      }
+
+      if (strcmp(param.c_str(), compilerId.c_str()) == 0) {
+        return "1";
+      }
+
+      if (cmsysString_strcasecmp(param.c_str(), compilerId.c_str()) == 0) {
+        switch (context->LG->GetPolicyStatus(cmPolicies::CMP0044)) {
+          case cmPolicies::WARN: {
+            std::ostringstream e;
+            e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0044);
+            context->LG->GetCMakeInstance()->IssueMessage(
+              MessageType::AUTHOR_WARNING, e.str(), context->Backtrace);
+            CM_FALLTHROUGH;
+          }
+          case cmPolicies::OLD:
+            return "1";
+          case cmPolicies::NEW:
+          case cmPolicies::REQUIRED_ALWAYS:
+          case cmPolicies::REQUIRED_IF_USED:
+            break;
         }
-        case cmPolicies::OLD:
-          return "1";
-        case cmPolicies::NEW:
-        case cmPolicies::REQUIRED_ALWAYS:
-        case cmPolicies::REQUIRED_IF_USED:
-          break;
       }
     }
     return "0";

+ 13 - 2
Tests/CMakeCommands/target_compile_options/CMakeLists.txt

@@ -7,9 +7,10 @@ add_executable(target_compile_options
   "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp"
 )
 target_compile_options(target_compile_options
-  PRIVATE $<$<CXX_COMPILER_ID:GNU>:-DMY_PRIVATE_DEFINE>
+  PRIVATE $<$<CXX_COMPILER_ID:AppleClang,Clang,GNU>:-DMY_PRIVATE_DEFINE>
   PUBLIC $<$<COMPILE_LANG_AND_ID:CXX,GNU>:-DMY_PUBLIC_DEFINE>
   INTERFACE $<$<CXX_COMPILER_ID:GNU>:-DMY_INTERFACE_DEFINE>
+  INTERFACE $<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-DMY_MULTI_COMP_INTERFACE_DEFINE>
 )
 
 if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
@@ -17,6 +18,11 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
     PRIVATE
       "DO_GNU_TESTS"
   )
+elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
+  target_compile_definitions(target_compile_options
+    PRIVATE
+      "DO_CLANG_TESTS"
+  )
 endif()
 
 add_executable(consumer
@@ -40,7 +46,7 @@ if(CMAKE_GENERATOR MATCHES "Visual Studio")
 endif()
 
 target_compile_options(consumer
-  PRIVATE $<$<CXX_COMPILER_ID:GNU>:$<TARGET_PROPERTY:target_compile_options,INTERFACE_COMPILE_OPTIONS>>
+  PRIVATE $<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:$<TARGET_PROPERTY:target_compile_options,INTERFACE_COMPILE_OPTIONS>>
 )
 
 if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
@@ -48,6 +54,11 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
     PRIVATE
       "DO_GNU_TESTS"
   )
+elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
+  target_compile_definitions(consumer
+    PRIVATE
+      "DO_CLANG_TESTS"
+  )
 endif()
 
 # Test no items

+ 16 - 0
Tests/CMakeCommands/target_compile_options/consumer.cpp

@@ -13,6 +13,22 @@
 #    error Expected MY_INTERFACE_DEFINE
 #  endif
 
+#  ifndef MY_MULTI_COMP_INTERFACE_DEFINE
+#    error Expected MY_MULTI_COMP_INTERFACE_DEFINE
+#  endif
+
+#endif
+
+#ifdef DO_CLANG_TESTS
+
+#  ifdef MY_PRIVATE_DEFINE
+#    error Unexpected MY_PRIVATE_DEFINE
+#  endif
+
+#  ifndef MY_MULTI_COMP_INTERFACE_DEFINE
+#    error Expected MY_MULTI_COMP_INTERFACE_DEFINE
+#  endif
+
 #endif
 
 #ifndef CONSUMER_LANG_CXX

+ 12 - 0
Tests/CMakeCommands/target_compile_options/main.cpp

@@ -15,6 +15,18 @@
 
 #endif
 
+#ifdef DO_CLANG_TESTS
+
+#  ifndef MY_PRIVATE_DEFINE
+#    error Expected MY_PRIVATE_DEFINE
+#  endif
+
+#  ifdef MY_PUBLIC_DEFINE
+#    error Unexpected MY_PUBLIC_DEFINE
+#  endif
+
+#endif
+
 int main()
 {
   return 0;

+ 1 - 1
Tests/RunCMake/PolicyScope/dir1/CMakeLists.txt

@@ -2,4 +2,4 @@
 
 add_library(foo STATIC foo.cpp)
 string(TOLOWER ${CMAKE_CXX_COMPILER_ID} compiler_id)
-target_compile_definitions(foo PRIVATE Foo=$<CXX_COMPILER_ID:${compiler_id}>)
+target_compile_definitions(foo PRIVATE Foo=$<CXX_COMPILER_ID:invalid,${compiler_id}>)