From 6388ebceb12e8eca3ce0e30528f0edaa990c8f7a Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 28 Jan 2008 13:21:42 -0500 Subject: [PATCH] ENH: Restored APPEND option to EXPORT() command in new implementation. --- Source/cmExportBuildFileGenerator.h | 2 ++ Source/cmExportCommand.cxx | 2 ++ Source/cmExportCommand.h | 5 +++- Source/cmExportFileGenerator.cxx | 29 +++++++++++++++++++++--- Source/cmExportFileGenerator.h | 2 ++ Tests/ExportImport/Export/CMakeLists.txt | 6 ++++- 6 files changed, 41 insertions(+), 5 deletions(-) diff --git a/Source/cmExportBuildFileGenerator.h b/Source/cmExportBuildFileGenerator.h index 0bb79cc2a..53423f382 100644 --- a/Source/cmExportBuildFileGenerator.h +++ b/Source/cmExportBuildFileGenerator.h @@ -35,6 +35,8 @@ public: void SetExports(std::vector const* exports) { this->Exports = exports; } + /** Set whether to append generated code to the output file. */ + void SetAppendMode(bool append) { this->AppendMode = append; } protected: // Implement virtual methods from the superclass. virtual bool GenerateMainFile(std::ostream& os); diff --git a/Source/cmExportCommand.cxx b/Source/cmExportCommand.cxx index 70c024e5c..cfc339c33 100644 --- a/Source/cmExportCommand.cxx +++ b/Source/cmExportCommand.cxx @@ -26,6 +26,7 @@ cmExportCommand::cmExportCommand() :cmCommand() ,ArgumentGroup() ,Targets(&Helper, "TARGETS") +,Append(&Helper, "APPEND", &ArgumentGroup) ,Namespace(&Helper, "NAMESPACE", &ArgumentGroup) ,Filename(&Helper, "FILE", &ArgumentGroup) { @@ -146,6 +147,7 @@ bool cmExportCommand cmExportBuildFileGenerator ebfg; ebfg.SetExportFile(fname.c_str()); ebfg.SetNamespace(this->Namespace.GetCString()); + ebfg.SetAppendMode(this->Append.IsEnabled()); ebfg.SetExports(&targets); // Compute the set of configurations exported. diff --git a/Source/cmExportCommand.h b/Source/cmExportCommand.h index f42450118..d0e88c47a 100644 --- a/Source/cmExportCommand.h +++ b/Source/cmExportCommand.h @@ -65,7 +65,7 @@ public: { return " export(TARGETS [target1 [target2 [...]]] [NAMESPACE ]\n" - " FILE )\n" + " [APPEND] FILE )\n" "Create a file that may be included by outside projects to " "import targets from the current project's build tree. " "This is useful during cross-compiling to build utility executables " @@ -73,6 +73,8 @@ public: "them into another project being compiled for the target platform. " "If the NAMESPACE option is given the 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. " "If a library target is included in the export but " "a target to which it links is not included the behavior is " "unspecified." @@ -88,6 +90,7 @@ public: private: cmCommandArgumentGroup ArgumentGroup; cmCAStringVector Targets; + cmCAEnabler Append; cmCAString Namespace; cmCAString Filename; }; diff --git a/Source/cmExportFileGenerator.cxx b/Source/cmExportFileGenerator.cxx index 2c916e54e..dd0c33a6e 100644 --- a/Source/cmExportFileGenerator.cxx +++ b/Source/cmExportFileGenerator.cxx @@ -21,6 +21,14 @@ #include "cmSystemTools.h" #include "cmTarget.h" +#include + +//---------------------------------------------------------------------------- +cmExportFileGenerator::cmExportFileGenerator() +{ + this->AppendMode = false; +} + //---------------------------------------------------------------------------- void cmExportFileGenerator::AddConfiguration(const char* config) { @@ -43,8 +51,23 @@ void cmExportFileGenerator::SetExportFile(const char* mainFile) bool cmExportFileGenerator::GenerateImportFile() { // Open the output file to generate it. - cmGeneratedFileStream exportFileStream(this->MainImportFile.c_str(), true); - if(!exportFileStream) + cmsys::auto_ptr foutPtr; + if(this->AppendMode) + { + // Open for append. + cmsys::auto_ptr + ap(new std::ofstream(this->MainImportFile.c_str(), std::ios::app)); + foutPtr = ap; + } + else + { + // Generate atomically and with copy-if-different. + cmsys::auto_ptr + ap(new cmGeneratedFileStream(this->MainImportFile.c_str(), true)); + ap->SetCopyIfDifferent(true); + foutPtr = ap; + } + if(!foutPtr.get() || !*foutPtr) { std::string se = cmSystemTools::GetLastSystemError(); cmOStringStream e; @@ -53,7 +76,7 @@ bool cmExportFileGenerator::GenerateImportFile() cmSystemTools::Error(e.str().c_str()); return false; } - std::ostream& os = exportFileStream; + std::ostream& os = *foutPtr; // Start with the import file header. this->GenerateImportHeaderCode(os); diff --git a/Source/cmExportFileGenerator.h b/Source/cmExportFileGenerator.h index 92b6896f5..8edf82e30 100644 --- a/Source/cmExportFileGenerator.h +++ b/Source/cmExportFileGenerator.h @@ -30,6 +30,7 @@ class cmExportFileGenerator { public: + cmExportFileGenerator(); virtual ~cmExportFileGenerator() {} /** Set the full path to the export file to generate. */ @@ -90,6 +91,7 @@ protected: std::string FileDir; std::string FileBase; std::string FileExt; + bool AppendMode; // The set of targets included in the export. std::set ExportedTargets; diff --git a/Tests/ExportImport/Export/CMakeLists.txt b/Tests/ExportImport/Export/CMakeLists.txt index 53fe70574..62fe81159 100644 --- a/Tests/ExportImport/Export/CMakeLists.txt +++ b/Tests/ExportImport/Export/CMakeLists.txt @@ -31,7 +31,11 @@ install( install(EXPORT exp NAMESPACE exp_ DESTINATION lib/exp) # Export from build tree. -export(TARGETS testExe1 testLib1 testLib2 testExe2 testLib3 testLib4 +export(TARGETS testExe1 testLib1 testLib2 NAMESPACE bld_ FILE ExportBuildTree.cmake ) +export(TARGETS testExe2 testLib3 testLib4 + NAMESPACE bld_ + APPEND FILE ExportBuildTree.cmake + )