Sfoglia il codice sorgente

export: Implement EXPORT subcommand (#9822)

Teach the export command to handle export sets defined by invocations
of install(TARGETS ... EXPORT foo).  This makes maintenance of targets
exported to both the build tree and install tree trivial.
Stephen Kelly 12 anni fa
parent
commit
cbe7e8fae4

+ 20 - 10
Help/command/export.rst

@@ -5,8 +5,7 @@ Export targets from the build tree for use by outside projects.
 
 
 ::
 ::
 
 
-  export(TARGETS [target1 [target2 [...]]] [NAMESPACE <namespace>]
-         [APPEND] FILE <filename> [EXPORT_LINK_INTERFACE_LIBRARIES])
+  export(EXPORT <export-name> [NAMESPACE <namespace>] [FILE <filename>])
 
 
 Create a file <filename> that may be included by outside projects to
 Create a file <filename> that may be included by outside projects to
 import targets from the current project's build tree.  This is useful
 import targets from the current project's build tree.  This is useful
@@ -14,14 +13,10 @@ during cross-compiling to build utility executables that can run on
 the host platform in one project and then import them into another
 the host platform in one project and then import them into another
 project being compiled for the target platform.  If the NAMESPACE
 project being compiled for the target platform.  If the NAMESPACE
 option is given the <namespace> string will be prepended to all target
 option is given the <namespace> string will be prepended to all target
-names written to the file.  If the APPEND option is given the
-generated code will be appended to the file instead of overwriting it.
-The EXPORT_LINK_INTERFACE_LIBRARIES keyword, if present, causes the
-contents of the properties matching
-``(IMPORTED_)?LINK_INTERFACE_LIBRARIES(_<CONFIG>)?`` to be exported, when
-policy CMP0022 is NEW.  If a library target is included in the export
-but a target to which it links is not included the behavior is
-unspecified.
+names written to the file.
+
+Target installations are associated with the export <export-name>
+using the ``EXPORT`` option of the :command:`install(TARGETS)` command.
 
 
 The file created by this command is specific to the build tree and
 The file created by this command is specific to the build tree and
 should never be installed.  See the install(EXPORT) command to export
 should never be installed.  See the install(EXPORT) command to export
@@ -30,6 +25,21 @@ targets from an installation tree.
 The properties set on the generated IMPORTED targets will have the
 The properties set on the generated IMPORTED targets will have the
 same values as the final values of the input TARGETS.
 same values as the final values of the input TARGETS.
 
 
+::
+
+  export(TARGETS [target1 [target2 [...]]] [NAMESPACE <namespace>]
+         [APPEND] FILE <filename> [EXPORT_LINK_INTERFACE_LIBRARIES])
+
+This signature is similar to the ``EXPORT`` signature, but targets are listed
+explicitly rather than specified as an export-name.  If the APPEND option is
+given the generated code will be appended to the file instead of overwriting it.
+The EXPORT_LINK_INTERFACE_LIBRARIES keyword, if present, causes the
+contents of the properties matching
+``(IMPORTED_)?LINK_INTERFACE_LIBRARIES(_<CONFIG>)?`` to be exported, when
+policy CMP0022 is NEW.  If a library target is included in the export
+but a target to which it links is not included the behavior is
+unspecified.
+
 ::
 ::
 
 
   export(PACKAGE <name>)
   export(PACKAGE <name>)

+ 32 - 4
Source/cmExportBuildFileGenerator.cxx

@@ -13,11 +13,14 @@
 
 
 #include "cmLocalGenerator.h"
 #include "cmLocalGenerator.h"
 #include "cmGlobalGenerator.h"
 #include "cmGlobalGenerator.h"
+#include "cmExportSet.h"
+#include "cmTargetExport.h"
 
 
 //----------------------------------------------------------------------------
 //----------------------------------------------------------------------------
 cmExportBuildFileGenerator::cmExportBuildFileGenerator()
 cmExportBuildFileGenerator::cmExportBuildFileGenerator()
 {
 {
   this->Makefile = 0;
   this->Makefile = 0;
+  this->ExportSet = 0;
 }
 }
 
 
 //----------------------------------------------------------------------------
 //----------------------------------------------------------------------------
@@ -26,9 +29,11 @@ bool cmExportBuildFileGenerator::GenerateMainFile(std::ostream& os)
   {
   {
   std::string expectedTargets;
   std::string expectedTargets;
   std::string sep;
   std::string sep;
+  std::vector<std::string> targets;
+  this->GetTargets(targets);
   for(std::vector<std::string>::const_iterator
   for(std::vector<std::string>::const_iterator
-        tei = this->Targets.begin();
-      tei != this->Targets.end(); ++tei)
+        tei = targets.begin();
+      tei != targets.end(); ++tei)
     {
     {
     cmTarget *te = this->Makefile->FindTargetToUse(tei->c_str());
     cmTarget *te = this->Makefile->FindTargetToUse(tei->c_str());
     expectedTargets += sep + this->Namespace + te->GetExportName();
     expectedTargets += sep + this->Namespace + te->GetExportName();
@@ -152,6 +157,12 @@ cmExportBuildFileGenerator
     }
     }
 }
 }
 
 
+//----------------------------------------------------------------------------
+void cmExportBuildFileGenerator::SetExportSet(cmExportSet *exportSet)
+{
+  this->ExportSet = exportSet;
+}
+
 //----------------------------------------------------------------------------
 //----------------------------------------------------------------------------
 void
 void
 cmExportBuildFileGenerator
 cmExportBuildFileGenerator
@@ -231,6 +242,23 @@ cmExportBuildFileGenerator::HandleMissingTarget(
   link_libs += dependee->GetExportName();
   link_libs += dependee->GetExportName();
 }
 }
 
 
+//----------------------------------------------------------------------------
+void cmExportBuildFileGenerator
+::GetTargets(std::vector<std::string> &targets) const
+{
+  if (this->ExportSet)
+    {
+    for(std::vector<cmTargetExport*>::const_iterator
+          tei = this->ExportSet->GetTargetExports()->begin();
+          tei != this->ExportSet->GetTargetExports()->end(); ++tei)
+      {
+      targets.push_back((*tei)->Target->GetName());
+      }
+    return;
+    }
+  targets = this->Targets;
+}
+
 //----------------------------------------------------------------------------
 //----------------------------------------------------------------------------
 std::vector<std::string>
 std::vector<std::string>
 cmExportBuildFileGenerator
 cmExportBuildFileGenerator
@@ -246,8 +274,8 @@ cmExportBuildFileGenerator
       expIt = exportSets.begin(); expIt != exportSets.end(); ++expIt)
       expIt = exportSets.begin(); expIt != exportSets.end(); ++expIt)
     {
     {
     const cmExportBuildFileGenerator* exportSet = expIt->second;
     const cmExportBuildFileGenerator* exportSet = expIt->second;
-    std::vector<std::string> const& targets = exportSet->GetTargets();
-
+    std::vector<std::string> targets;
+    exportSet->GetTargets(targets);
     if (std::find(targets.begin(), targets.end(), name) != targets.end())
     if (std::find(targets.begin(), targets.end(), name) != targets.end())
       {
       {
       namespaces.push_back(exportSet->GetNamespace());
       namespaces.push_back(exportSet->GetNamespace());

+ 5 - 2
Source/cmExportBuildFileGenerator.h

@@ -15,6 +15,8 @@
 #include "cmExportFileGenerator.h"
 #include "cmExportFileGenerator.h"
 #include "cmListFileCache.h"
 #include "cmListFileCache.h"
 
 
+class cmExportSet;
+
 /** \class cmExportBuildFileGenerator
 /** \class cmExportBuildFileGenerator
  * \brief Generate a file exporting targets from a build tree.
  * \brief Generate a file exporting targets from a build tree.
  *
  *
@@ -32,11 +34,11 @@ public:
   /** Set the list of targets to export.  */
   /** Set the list of targets to export.  */
   void SetTargets(std::vector<std::string> const& targets)
   void SetTargets(std::vector<std::string> const& targets)
     { this->Targets = targets; }
     { this->Targets = targets; }
-  std::vector<std::string> const& GetTargets() const
-    { return this->Targets; }
+  void GetTargets(std::vector<std::string> &targets) const;
   void AppendTargets(std::vector<std::string> const& targets)
   void AppendTargets(std::vector<std::string> const& targets)
     { this->Targets.insert(this->Targets.end(),
     { this->Targets.insert(this->Targets.end(),
       targets.begin(), targets.end()); }
       targets.begin(), targets.end()); }
+  void SetExportSet(cmExportSet*);
 
 
   /** Set whether to append generated code to the output file.  */
   /** Set whether to append generated code to the output file.  */
   void SetAppendMode(bool append) { this->AppendMode = append; }
   void SetAppendMode(bool append) { this->AppendMode = append; }
@@ -75,6 +77,7 @@ protected:
   FindNamespaces(cmMakefile* mf, const std::string& name);
   FindNamespaces(cmMakefile* mf, const std::string& name);
 
 
   std::vector<std::string> Targets;
   std::vector<std::string> Targets;
+  cmExportSet *ExportSet;
   std::vector<cmTarget*> Exports;
   std::vector<cmTarget*> Exports;
   cmMakefile* Makefile;
   cmMakefile* Makefile;
   cmListFileBacktrace Backtrace;
   cmListFileBacktrace Backtrace;

+ 107 - 53
Source/cmExportCommand.cxx

@@ -30,14 +30,12 @@ cmExportCommand::cmExportCommand()
 ,ArgumentGroup()
 ,ArgumentGroup()
 ,Targets(&Helper, "TARGETS")
 ,Targets(&Helper, "TARGETS")
 ,Append(&Helper, "APPEND", &ArgumentGroup)
 ,Append(&Helper, "APPEND", &ArgumentGroup)
+,ExportSetName(&Helper, "EXPORT", &ArgumentGroup)
 ,Namespace(&Helper, "NAMESPACE", &ArgumentGroup)
 ,Namespace(&Helper, "NAMESPACE", &ArgumentGroup)
 ,Filename(&Helper, "FILE", &ArgumentGroup)
 ,Filename(&Helper, "FILE", &ArgumentGroup)
 ,ExportOld(&Helper, "EXPORT_LINK_INTERFACE_LIBRARIES", &ArgumentGroup)
 ,ExportOld(&Helper, "EXPORT_LINK_INTERFACE_LIBRARIES", &ArgumentGroup)
 {
 {
-  // at first TARGETS
-  this->Targets.Follows(0);
-  // and after that the other options in any order
-  this->ArgumentGroup.Follows(&this->Targets);
+  this->ExportSet = 0;
 }
 }
 
 
 
 
@@ -55,6 +53,16 @@ bool cmExportCommand
     {
     {
     return this->HandlePackage(args);
     return this->HandlePackage(args);
     }
     }
+  else if (args[0] == "EXPORT")
+    {
+    this->ExportSetName.Follows(0);
+    this->ArgumentGroup.Follows(&this->ExportSetName);
+    }
+  else
+    {
+    this->Targets.Follows(0);
+    this->ArgumentGroup.Follows(&this->Targets);
+    }
 
 
   std::vector<std::string> unknownArgs;
   std::vector<std::string> unknownArgs;
   this->Helper.Parse(&args, &unknownArgs);
   this->Helper.Parse(&args, &unknownArgs);
@@ -65,31 +73,32 @@ bool cmExportCommand
     return false;
     return false;
     }
     }
 
 
-  if (this->Targets.WasFound() == false)
-    {
-    this->SetError("TARGETS option missing.");
-    return false;
-    }
-
+  std::string fname;
   if(!this->Filename.WasFound())
   if(!this->Filename.WasFound())
     {
     {
-    this->SetError("FILE <filename> option missing.");
-    return false;
+    if (args[0] != "EXPORT")
+      {
+      this->SetError("FILE <filename> option missing.");
+      return false;
+      }
+    fname = this->ExportSetName.GetString() + ".cmake";
     }
     }
-
-  // Make sure the file has a .cmake extension.
-  if(cmSystemTools::GetFilenameLastExtension(this->Filename.GetCString())
-     != ".cmake")
+  else
     {
     {
-    cmOStringStream e;
-    e << "FILE option given filename \"" << this->Filename.GetString()
-      << "\" which does not have an extension of \".cmake\".\n";
-    this->SetError(e.str().c_str());
-    return false;
+    // Make sure the file has a .cmake extension.
+    if(cmSystemTools::GetFilenameLastExtension(this->Filename.GetCString())
+      != ".cmake")
+      {
+      cmOStringStream e;
+      e << "FILE option given filename \"" << this->Filename.GetString()
+        << "\" which does not have an extension of \".cmake\".\n";
+      this->SetError(e.str().c_str());
+      return false;
+      }
+    fname = this->Filename.GetString();
     }
     }
 
 
   // Get the file to write.
   // Get the file to write.
-  std::string fname = this->Filename.GetString();
   if(cmSystemTools::FileIsFullPath(fname.c_str()))
   if(cmSystemTools::FileIsFullPath(fname.c_str()))
     {
     {
     if(!this->Makefile->CanIWriteThisFile(fname.c_str()))
     if(!this->Makefile->CanIWriteThisFile(fname.c_str()))
@@ -104,57 +113,95 @@ bool cmExportCommand
   else
   else
     {
     {
     // Interpret relative paths with respect to the current build dir.
     // Interpret relative paths with respect to the current build dir.
-    fname = this->Makefile->GetCurrentOutputDirectory();
-    fname += "/";
-    fname += this->Filename.GetString();
+    std::string dir = this->Makefile->GetCurrentOutputDirectory();
+    fname = dir + "/" + fname;
     }
     }
 
 
-  for(std::vector<std::string>::const_iterator
-      currentTarget = this->Targets.GetVector().begin();
-      currentTarget != this->Targets.GetVector().end();
-      ++currentTarget)
+  std::vector<std::string> targets;
+
+  cmGlobalGenerator *gg = this->Makefile->GetLocalGenerator()
+                                        ->GetGlobalGenerator();
+
+  if(args[0] == "EXPORT")
     {
     {
-    if (this->Makefile->IsAlias(currentTarget->c_str()))
+    if (this->Append.IsEnabled())
+      {
+      cmOStringStream e;
+      e << "EXPORT signature does not recognise the APPEND option.";
+      this->SetError(e.str().c_str());
+      return false;
+      }
+
+    if (this->ExportOld.IsEnabled())
       {
       {
       cmOStringStream e;
       cmOStringStream e;
-      e << "given ALIAS target \"" << *currentTarget
-        << "\" which may not be exported.";
+      e << "EXPORT signature does not recognise the "
+        "EXPORT_LINK_INTERFACE_LIBRARIES option.";
       this->SetError(e.str().c_str());
       this->SetError(e.str().c_str());
       return false;
       return false;
       }
       }
 
 
-    if(cmTarget* target =
-       this->Makefile->GetLocalGenerator()->
-       GetGlobalGenerator()->FindTarget(0, currentTarget->c_str()))
+    cmExportSetMap &setMap = gg->GetExportSets();
+    std::string setName = this->ExportSetName.GetString();
+    if (setMap.find(setName) == setMap.end())
       {
       {
-      if(target->GetType() == cmTarget::OBJECT_LIBRARY)
+      cmOStringStream e;
+      e << "Export set \"" << setName << "\" not found.";
+      this->SetError(e.str().c_str());
+      return false;
+      }
+    this->ExportSet = setMap[setName];
+    }
+  else if (this->Targets.WasFound())
+    {
+    for(std::vector<std::string>::const_iterator
+        currentTarget = this->Targets.GetVector().begin();
+        currentTarget != this->Targets.GetVector().end();
+        ++currentTarget)
+      {
+      if (this->Makefile->IsAlias(currentTarget->c_str()))
         {
         {
         cmOStringStream e;
         cmOStringStream e;
-        e << "given OBJECT library \"" << *currentTarget
+        e << "given ALIAS target \"" << *currentTarget
           << "\" which may not be exported.";
           << "\" which may not be exported.";
         this->SetError(e.str().c_str());
         this->SetError(e.str().c_str());
         return false;
         return false;
         }
         }
+
+      if(cmTarget* target = gg->FindTarget(0, currentTarget->c_str()))
+        {
+        if(target->GetType() == cmTarget::OBJECT_LIBRARY)
+          {
+          cmOStringStream e;
+          e << "given OBJECT library \"" << *currentTarget
+            << "\" which may not be exported.";
+          this->SetError(e.str().c_str());
+          return false;
+          }
+        }
+      else
+        {
+        cmOStringStream e;
+        e << "given target \"" << *currentTarget
+          << "\" which is not built by this project.";
+        this->SetError(e.str().c_str());
+        return false;
+        }
+      targets.push_back(*currentTarget);
       }
       }
-    else
+    if (this->Append.IsEnabled())
       {
       {
-      cmOStringStream e;
-      e << "given target \"" << *currentTarget
-        << "\" which is not built by this project.";
-      this->SetError(e.str().c_str());
-      return false;
+      if (cmExportBuildFileGenerator *ebfg = gg->GetExportedTargetsFile(fname))
+        {
+        ebfg->AppendTargets(targets);
+        return true;
+        }
       }
       }
     }
     }
-
-  cmGlobalGenerator *gg = this->Makefile->GetLocalGenerator()
-                                        ->GetGlobalGenerator();
-  if (this->Append.IsEnabled())
+  else
     {
     {
-    if (cmExportBuildFileGenerator *ebfg = gg->GetExportedTargetsFile(fname))
-      {
-      ebfg->AppendTargets(this->Targets.GetVector());
-      return true;
-      }
+    this->SetError("EXPORT or TARGETS specifier missing.");
+    return false;
     }
     }
 
 
   // Setup export file generation.
   // Setup export file generation.
@@ -162,7 +209,14 @@ bool cmExportCommand
   ebfg->SetExportFile(fname.c_str());
   ebfg->SetExportFile(fname.c_str());
   ebfg->SetNamespace(this->Namespace.GetCString());
   ebfg->SetNamespace(this->Namespace.GetCString());
   ebfg->SetAppendMode(this->Append.IsEnabled());
   ebfg->SetAppendMode(this->Append.IsEnabled());
-  ebfg->SetTargets(this->Targets.GetVector());
+  if (this->ExportSet)
+    {
+    ebfg->SetExportSet(this->ExportSet);
+    }
+  else
+    {
+    ebfg->SetTargets(targets);
+    }
   ebfg->SetMakefile(this->Makefile);
   ebfg->SetMakefile(this->Makefile);
   ebfg->SetExportOld(this->ExportOld.IsEnabled());
   ebfg->SetExportOld(this->ExportOld.IsEnabled());
 
 

+ 4 - 0
Source/cmExportCommand.h

@@ -15,6 +15,7 @@
 #include "cmCommand.h"
 #include "cmCommand.h"
 
 
 class cmExportBuildFileGenerator;
 class cmExportBuildFileGenerator;
+class cmExportSet;
 
 
 /** \class cmExportLibraryDependenciesCommand
 /** \class cmExportLibraryDependenciesCommand
  * \brief Add a test to the lists of tests to run.
  * \brief Add a test to the lists of tests to run.
@@ -52,10 +53,13 @@ private:
   cmCommandArgumentGroup ArgumentGroup;
   cmCommandArgumentGroup ArgumentGroup;
   cmCAStringVector Targets;
   cmCAStringVector Targets;
   cmCAEnabler Append;
   cmCAEnabler Append;
+  cmCAString ExportSetName;
   cmCAString Namespace;
   cmCAString Namespace;
   cmCAString Filename;
   cmCAString Filename;
   cmCAEnabler ExportOld;
   cmCAEnabler ExportOld;
 
 
+  cmExportSet *ExportSet;
+
   friend class cmExportBuildFileGenerator;
   friend class cmExportBuildFileGenerator;
   std::string ErrorMessage;
   std::string ErrorMessage;
 
 

+ 5 - 5
Tests/ExportImport/Export/Interface/CMakeLists.txt

@@ -23,11 +23,6 @@ set_property(TARGET sharedlib PROPERTY INTERFACE_COMPILE_DEFINITIONS "SHAREDLIB_
 add_library(sharediface INTERFACE)
 add_library(sharediface INTERFACE)
 target_link_libraries(sharediface INTERFACE sharedlib)
 target_link_libraries(sharediface INTERFACE sharedlib)
 
 
-export(TARGETS sharediface sharedlib headeronly
-  NAMESPACE bld::
-  FILE ../ExportInterfaceBuildTree.cmake
-)
-
 install(TARGETS headeronly sharediface sharedlib
 install(TARGETS headeronly sharediface sharedlib
   EXPORT expInterface
   EXPORT expInterface
   RUNTIME DESTINATION bin
   RUNTIME DESTINATION bin
@@ -47,3 +42,8 @@ install(FILES
 )
 )
 
 
 install(EXPORT expInterface NAMESPACE exp:: DESTINATION lib/exp)
 install(EXPORT expInterface NAMESPACE exp:: DESTINATION lib/exp)
+
+export(EXPORT expInterface
+  NAMESPACE bld::
+  FILE ../ExportInterfaceBuildTree.cmake
+)

+ 1 - 0
Tests/RunCMake/export/AppendExport-result.txt

@@ -0,0 +1 @@
+1

+ 4 - 0
Tests/RunCMake/export/AppendExport-stderr.txt

@@ -0,0 +1,4 @@
+CMake Error at AppendExport.cmake:8 \(export\):
+  export EXPORT signature does not recognise the APPEND option.
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)

+ 8 - 0
Tests/RunCMake/export/AppendExport.cmake

@@ -0,0 +1,8 @@
+add_library(foo empty.cpp)
+export(TARGETS foo FILE "${CMAKE_CURRENT_BINARY_DIR}/foo.cmake")
+install(TARGETS foo EXPORT fooExport
+  RUNTIME DESTINATION bin
+  LIBRARY DESTINATION lib
+  ARCHIVE DESTINATION lib
+)
+export(EXPORT fooExport APPEND FILE "${CMAKE_CURRENT_BINARY_DIR}/foo.cmake")

+ 1 - 0
Tests/RunCMake/export/NoExportSet-result.txt

@@ -0,0 +1 @@
+1

+ 4 - 0
Tests/RunCMake/export/NoExportSet-stderr.txt

@@ -0,0 +1,4 @@
+CMake Error at NoExportSet.cmake:2 \(export\):
+  export Export set "fooExport" not found.
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)

+ 2 - 0
Tests/RunCMake/export/NoExportSet.cmake

@@ -0,0 +1,2 @@
+
+export(EXPORT fooExport)

+ 1 - 0
Tests/RunCMake/export/OldIface-result.txt

@@ -0,0 +1 @@
+1

+ 5 - 0
Tests/RunCMake/export/OldIface-stderr.txt

@@ -0,0 +1,5 @@
+CMake Error at OldIface.cmake:8 \(export\):
+  export EXPORT signature does not recognise the
+  EXPORT_LINK_INTERFACE_LIBRARIES option.
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)

+ 10 - 0
Tests/RunCMake/export/OldIface.cmake

@@ -0,0 +1,10 @@
+add_library(foo empty.cpp)
+export(TARGETS foo FILE "${CMAKE_CURRENT_BINARY_DIR}/foo.cmake")
+install(TARGETS foo EXPORT fooExport
+  RUNTIME DESTINATION bin
+  LIBRARY DESTINATION lib
+  ARCHIVE DESTINATION lib
+)
+export(EXPORT fooExport
+  EXPORT_LINK_INTERFACE_LIBRARIES
+)

+ 3 - 0
Tests/RunCMake/export/RunCMakeTest.cmake

@@ -1,3 +1,6 @@
 include(RunCMake)
 include(RunCMake)
 
 
 run_cmake(TargetNotFound)
 run_cmake(TargetNotFound)
+run_cmake(AppendExport)
+run_cmake(OldIface)
+run_cmake(NoExportSet)