瀏覽代碼

Add support for IMPORTED GLOBAL targets to be aliased

Issue: #15569
Issue: #17197
Deniz Bahadir 8 年之前
父節點
當前提交
494906a8a2

+ 3 - 2
Help/command/add_executable.rst

@@ -74,8 +74,9 @@ properties for more information.
 Creates an :ref:`Alias Target <Alias Targets>`, such that ``<name>`` can
 Creates an :ref:`Alias Target <Alias Targets>`, such that ``<name>`` can
 be used to refer to ``<target>`` in subsequent commands.  The ``<name>``
 be used to refer to ``<target>`` in subsequent commands.  The ``<name>``
 does not appear in the generated buildsystem as a make target.  The
 does not appear in the generated buildsystem as a make target.  The
-``<target>`` may not be an :ref:`Imported Target <Imported Targets>` or an
-``ALIAS``.  ``ALIAS`` targets can be used as targets to read properties
+``<target>`` may not be a non-``GLOBAL``
+:ref:`Imported Target <Imported Targets>` or an ``ALIAS``.
+``ALIAS`` targets can be used as targets to read properties
 from, executables for custom commands and custom targets.  They can also be
 from, executables for custom commands and custom targets.  They can also be
 tested for existence with the regular :command:`if(TARGET)` subcommand.
 tested for existence with the regular :command:`if(TARGET)` subcommand.
 The ``<name>`` may not be used to modify properties of ``<target>``, that
 The ``<name>`` may not be used to modify properties of ``<target>``, that

+ 2 - 1
Help/command/add_library.rst

@@ -125,7 +125,8 @@ Alias Libraries
 Creates an :ref:`Alias Target <Alias Targets>`, such that ``<name>`` can be
 Creates an :ref:`Alias Target <Alias Targets>`, such that ``<name>`` can be
 used to refer to ``<target>`` in subsequent commands.  The ``<name>`` does
 used to refer to ``<target>`` in subsequent commands.  The ``<name>`` does
 not appear in the generated buildsystem as a make target.  The ``<target>``
 not appear in the generated buildsystem as a make target.  The ``<target>``
-may not be an :ref:`Imported Target <Imported Targets>` or an ``ALIAS``.
+may not be a non-``GLOBAL`` :ref:`Imported Target <Imported Targets>` or an
+``ALIAS``.
 ``ALIAS`` targets can be used as linkable targets and as targets to
 ``ALIAS`` targets can be used as linkable targets and as targets to
 read properties from.  They can also be tested for existence with the
 read properties from.  They can also be tested for existence with the
 regular :command:`if(TARGET)` subcommand.  The ``<name>`` may not be used
 regular :command:`if(TARGET)` subcommand.  The ``<name>`` may not be used

+ 5 - 6
Source/cmAddExecutableCommand.cxx

@@ -140,8 +140,7 @@ bool cmAddExecutableCommand::InitialPass(std::vector<std::string> const& args,
     if (!aliasedTarget) {
     if (!aliasedTarget) {
       std::ostringstream e;
       std::ostringstream e;
       e << "cannot create ALIAS target \"" << exename << "\" because target \""
       e << "cannot create ALIAS target \"" << exename << "\" because target \""
-        << aliasedName << "\" does not already "
-                          "exist.";
+        << aliasedName << "\" does not already exist.";
       this->SetError(e.str());
       this->SetError(e.str());
       return false;
       return false;
     }
     }
@@ -149,15 +148,15 @@ bool cmAddExecutableCommand::InitialPass(std::vector<std::string> const& args,
     if (type != cmStateEnums::EXECUTABLE) {
     if (type != cmStateEnums::EXECUTABLE) {
       std::ostringstream e;
       std::ostringstream e;
       e << "cannot create ALIAS target \"" << exename << "\" because target \""
       e << "cannot create ALIAS target \"" << exename << "\" because target \""
-        << aliasedName << "\" is not an "
-                          "executable.";
+        << aliasedName << "\" is not an executable.";
       this->SetError(e.str());
       this->SetError(e.str());
       return false;
       return false;
     }
     }
-    if (aliasedTarget->IsImported()) {
+    if (aliasedTarget->IsImported() &&
+        !aliasedTarget->IsImportedGloballyVisible()) {
       std::ostringstream e;
       std::ostringstream e;
       e << "cannot create ALIAS target \"" << exename << "\" because target \""
       e << "cannot create ALIAS target \"" << exename << "\" because target \""
-        << aliasedName << "\" is IMPORTED.";
+        << aliasedName << "\" is imported but not globally visible.";
       this->SetError(e.str());
       this->SetError(e.str());
       return false;
       return false;
     }
     }

+ 0 - 7
Source/cmAddLibraryCommand.cxx

@@ -256,13 +256,6 @@ bool cmAddLibraryCommand::InitialPass(std::vector<std::string> const& args,
       this->SetError(e.str());
       this->SetError(e.str());
       return false;
       return false;
     }
     }
-    if (aliasedTarget->IsImported()) {
-      std::ostringstream e;
-      e << "cannot create ALIAS target \"" << libName << "\" because target \""
-        << aliasedName << "\" is IMPORTED.";
-      this->SetError(e.str());
-      return false;
-    }
     this->Makefile->AddAlias(libName, aliasedName);
     this->Makefile->AddAlias(libName, aliasedName);
     return true;
     return true;
   }
   }

+ 1 - 0
Tests/RunCMake/alias_targets/RunCMakeTest.cmake

@@ -6,6 +6,7 @@ run_cmake(exclude-from-all)
 run_cmake(imported)
 run_cmake(imported)
 run_cmake(invalid-name)
 run_cmake(invalid-name)
 run_cmake(invalid-target)
 run_cmake(invalid-target)
+run_cmake(imported-global-target)
 run_cmake(imported-target)
 run_cmake(imported-target)
 run_cmake(alias-target)
 run_cmake(alias-target)
 run_cmake(set_property)
 run_cmake(set_property)

+ 2 - 0
Tests/RunCMake/alias_targets/imported-global-target-stderr.txt

@@ -0,0 +1,2 @@
+^'alias-test-exe' is an alias for 'test-exe' and its name-property contains 'test-exe'.
+'alias-test-lib' is an alias for 'test-lib' and its name-property contains 'test-lib'.$

+ 46 - 0
Tests/RunCMake/alias_targets/imported-global-target.cmake

@@ -0,0 +1,46 @@
+
+enable_language(CXX)
+
+
+add_executable(test-exe IMPORTED GLOBAL)
+add_executable(alias-test-exe ALIAS test-exe)
+
+if(TARGET alias-test-exe)
+  get_target_property(aliased-target alias-test-exe ALIASED_TARGET)
+  if("${aliased-target}" STREQUAL "test-exe")
+    get_target_property(aliased-name alias-test-exe NAME)
+    if("${aliased-name}" STREQUAL "test-exe")
+      message("'alias-test-exe' is an alias for '${aliased-target}'"
+              " and its name-property contains '${aliased-name}'.")
+    else()
+      message("'alias-test-exe' is an alias for '${aliased-target}'"
+              " but its name-property contains '${aliased-name}'!?")
+    endif()
+  else()
+    message("'alias-test-exe' is something but not a real target!?")
+  endif()
+else()
+    message("'alias-test-exe' does not exist!?")
+endif()
+
+
+add_library(test-lib SHARED IMPORTED GLOBAL)
+add_library(alias-test-lib ALIAS test-lib)
+
+if(TARGET alias-test-lib)
+  get_target_property(aliased-target alias-test-lib ALIASED_TARGET)
+  if("${aliased-target}" STREQUAL "test-lib")
+    get_target_property(aliased-name alias-test-lib NAME)
+    if("${aliased-name}" STREQUAL "test-lib")
+      message("'alias-test-lib' is an alias for '${aliased-target}'"
+              " and its name-property contains '${aliased-name}'.")
+    else()
+      message("'alias-test-lib' is an alias for '${aliased-target}'"
+              " but its name-property contains '${aliased-name}'!?")
+    endif()
+  else()
+    message("'alias-test-lib' is something but not a real target!?")
+  endif()
+else()
+    message("'alias-test-lib' does not exist!?")
+endif()

+ 8 - 4
Tests/RunCMake/alias_targets/imported-target-stderr.txt

@@ -1,5 +1,9 @@
-CMake Error at imported-target.cmake:6 \(add_library\):
-  add_library cannot create ALIAS target "alias" because target "foo" is
-  IMPORTED.
+^CMake Error at imported-target.cmake:[0-9]+ \(add_executable\):
+  add_executable cannot create ALIAS target \"alias-test-exe\" because target
+  \"test-exe\" is imported but not globally visible.
 Call Stack \(most recent call first\):
 Call Stack \(most recent call first\):
-  CMakeLists.txt:3 \(include\)
+  CMakeLists.txt:[0-9]+ \(include\)
+
+
+'alias-test-exe' does not exist![?]
+'alias-test-lib' does not exist![?]$

+ 42 - 2
Tests/RunCMake/alias_targets/imported-target.cmake

@@ -1,6 +1,46 @@
 
 
 enable_language(CXX)
 enable_language(CXX)
 
 
-add_library(foo SHARED IMPORTED)
 
 
-add_library(alias ALIAS foo)
+add_executable(test-exe IMPORTED)
+add_executable(alias-test-exe ALIAS test-exe)
+
+if(TARGET alias-test-exe)
+  get_target_property(aliased-target alias-test-exe ALIASED_TARGET)
+  if("${aliased-target}" STREQUAL "test-exe")
+    get_target_property(aliased-name alias-test-exe NAME)
+    if("${aliased-name}" STREQUAL "test-exe")
+      message("'alias-test-exe' is an alias for '${aliased-target}'"
+              " and its name-property contains '${aliased-name}'.")
+    else()
+      message("'alias-test-exe' is an alias for '${aliased-target}'"
+              " but its name-property contains '${aliased-name}'!?")
+    endif()
+  else()
+    message("'alias-test-exe' is something but not a real target!?")
+  endif()
+else()
+    message("'alias-test-exe' does not exist!?")
+endif()
+
+
+add_library(test-lib SHARED IMPORTED)
+add_library(alias-test-lib ALIAS test-lib)
+
+if(TARGET alias-test-lib)
+  get_target_property(aliased-target alias-test-lib ALIASED_TARGET)
+  if("${aliased-target}" STREQUAL "test-lib")
+    get_target_property(aliased-name alias-test-lib NAME)
+    if("${aliased-name}" STREQUAL "test-lib")
+      message("'alias-test-lib' is an alias for '${aliased-target}'"
+              " and its name-property contains '${aliased-name}'.")
+    else()
+      message("'alias-test-lib' is an alias for '${aliased-target}'"
+              " but its name-property contains '${aliased-name}'!?")
+    endif()
+  else()
+    message("'alias-test-lib' is something but not a real target!?")
+  endif()
+else()
+    message("'alias-test-lib' does not exist!?")
+endif()