Explorar o código

Teach MAP_IMPORTED_CONFIG_<CONFIG> to support configuration-less import

If this property has an empty list entry, check for `IMPORTED_LOCATION`
instead of `IMPORTED_LOCATION_<CONFIG>`.  This allows custom imported
targets to have some configurations mapped and others fall back to a
default location.

Closes: #16280
Jens Weggemann %!s(int64=9) %!d(string=hai) anos
pai
achega
149d49ea7c

+ 7 - 2
Help/prop_tgt/MAP_IMPORTED_CONFIG_CONFIG.rst

@@ -10,8 +10,13 @@ imported from another project may not provide the same set of
 configuration names available in the current project.  Setting this
 property tells CMake what imported configurations are suitable for use
 when building the ``<CONFIG>`` configuration.  The first configuration in
-the list found to be provided by the imported target is selected.  If
-this property is set and no matching configurations are available,
+the list found to be provided by the imported target (i.e. via
+:prop_tgt:`IMPORTED_LOCATION_<CONFIG>` for the mapped-to ``<CONFIG>``)
+is selected.  As a special case, an empty list element refers to the
+configuration-less imported target location
+(i.e. :prop_tgt:`IMPORTED_LOCATION`).
+
+If this property is set and no matching configurations are available,
 then the imported target is considered to be not found.  This property
 is ignored for non-imported targets.
 

+ 6 - 0
Help/release/dev/allow-fallback-config-mapping.rst

@@ -0,0 +1,6 @@
+allow-fallback-config-mapping
+-----------------------------
+
+* The :prop_tgt:`MAP_IMPORTED_CONFIG_<CONFIG>` target property learned
+  to interpret empty list elements as referring to the configuration-less
+  imported location specified by :prop_tgt:`IMPORTED_LOCATION`.

+ 27 - 14
Source/cmTarget.cxx

@@ -1395,7 +1395,7 @@ bool cmTarget::GetMappedConfig(std::string const& desired_config,
     std::string mapProp = "MAP_IMPORTED_CONFIG_";
     mapProp += desired_config;
     if (const char* mapValue = this->GetProperty(mapProp)) {
-      cmSystemTools::ExpandListArgument(mapValue, mappedConfigs);
+      cmSystemTools::ExpandListArgument(mapValue, mappedConfigs, true);
     }
   }
 
@@ -1408,20 +1408,33 @@ bool cmTarget::GetMappedConfig(std::string const& desired_config,
   for (std::vector<std::string>::const_iterator mci = mappedConfigs.begin();
        !*loc && !*imp && mci != mappedConfigs.end(); ++mci) {
     // Look for this configuration.
-    std::string mcUpper = cmSystemTools::UpperCase(*mci);
-    std::string locProp = "IMPORTED_LOCATION_";
-    locProp += mcUpper;
-    *loc = this->GetProperty(locProp);
-    if (allowImp) {
-      std::string impProp = "IMPORTED_IMPLIB_";
-      impProp += mcUpper;
-      *imp = this->GetProperty(impProp);
-    }
+    if (mci->empty()) {
+      // An empty string in the mapping has a special meaning:
+      // look up the config-less properties.
+      *loc = this->GetProperty("IMPORTED_LOCATION");
+      if (allowImp) {
+        *imp = this->GetProperty("IMPORTED_IMPLIB");
+      }
+      // If it was found, set the suffix.
+      if (*loc || *imp) {
+        suffix = "";
+      }
+    } else {
+      std::string mcUpper = cmSystemTools::UpperCase(*mci);
+      std::string locProp = "IMPORTED_LOCATION_";
+      locProp += mcUpper;
+      *loc = this->GetProperty(locProp);
+      if (allowImp) {
+        std::string impProp = "IMPORTED_IMPLIB_";
+        impProp += mcUpper;
+        *imp = this->GetProperty(impProp);
+      }
 
-    // If it was found, use it for all properties below.
-    if (*loc || *imp) {
-      suffix = "_";
-      suffix += mcUpper;
+      // If it was found, use it for all properties below.
+      if (*loc || *imp) {
+        suffix = "_";
+        suffix += mcUpper;
+      }
     }
   }