ENH: Restored APPEND option to EXPORT() command in new implementation.

This commit is contained in:
Brad King 2008-01-28 13:21:42 -05:00
parent 611bff2c1b
commit 6388ebceb1
6 changed files with 41 additions and 5 deletions

View File

@ -35,6 +35,8 @@ public:
void SetExports(std::vector<cmTarget*> const* exports) void SetExports(std::vector<cmTarget*> const* exports)
{ this->Exports = exports; } { this->Exports = exports; }
/** Set whether to append generated code to the output file. */
void SetAppendMode(bool append) { this->AppendMode = append; }
protected: protected:
// Implement virtual methods from the superclass. // Implement virtual methods from the superclass.
virtual bool GenerateMainFile(std::ostream& os); virtual bool GenerateMainFile(std::ostream& os);

View File

@ -26,6 +26,7 @@ cmExportCommand::cmExportCommand()
:cmCommand() :cmCommand()
,ArgumentGroup() ,ArgumentGroup()
,Targets(&Helper, "TARGETS") ,Targets(&Helper, "TARGETS")
,Append(&Helper, "APPEND", &ArgumentGroup)
,Namespace(&Helper, "NAMESPACE", &ArgumentGroup) ,Namespace(&Helper, "NAMESPACE", &ArgumentGroup)
,Filename(&Helper, "FILE", &ArgumentGroup) ,Filename(&Helper, "FILE", &ArgumentGroup)
{ {
@ -146,6 +147,7 @@ bool cmExportCommand
cmExportBuildFileGenerator ebfg; cmExportBuildFileGenerator ebfg;
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.SetExports(&targets); ebfg.SetExports(&targets);
// Compute the set of configurations exported. // Compute the set of configurations exported.

View File

@ -65,7 +65,7 @@ public:
{ {
return return
" export(TARGETS [target1 [target2 [...]]] [NAMESPACE <namespace>]\n" " export(TARGETS [target1 [target2 [...]]] [NAMESPACE <namespace>]\n"
" FILE <filename>)\n" " [APPEND] FILE <filename>)\n"
"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. " "import targets from the current project's build tree. "
"This is useful during cross-compiling to build utility executables " "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. " "them into another project being compiled for the target platform. "
"If the NAMESPACE option is given the <namespace> string will be " "If the NAMESPACE option is given the <namespace> string will be "
"prepended to all target names written to the file. " "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 " "If a library target is included in the export but "
"a target to which it links is not included the behavior is " "a target to which it links is not included the behavior is "
"unspecified." "unspecified."
@ -88,6 +90,7 @@ public:
private: private:
cmCommandArgumentGroup ArgumentGroup; cmCommandArgumentGroup ArgumentGroup;
cmCAStringVector Targets; cmCAStringVector Targets;
cmCAEnabler Append;
cmCAString Namespace; cmCAString Namespace;
cmCAString Filename; cmCAString Filename;
}; };

View File

@ -21,6 +21,14 @@
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmTarget.h" #include "cmTarget.h"
#include <cmsys/auto_ptr.hxx>
//----------------------------------------------------------------------------
cmExportFileGenerator::cmExportFileGenerator()
{
this->AppendMode = false;
}
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
void cmExportFileGenerator::AddConfiguration(const char* config) void cmExportFileGenerator::AddConfiguration(const char* config)
{ {
@ -43,8 +51,23 @@ void cmExportFileGenerator::SetExportFile(const char* mainFile)
bool cmExportFileGenerator::GenerateImportFile() bool cmExportFileGenerator::GenerateImportFile()
{ {
// Open the output file to generate it. // Open the output file to generate it.
cmGeneratedFileStream exportFileStream(this->MainImportFile.c_str(), true); cmsys::auto_ptr<std::ofstream> foutPtr;
if(!exportFileStream) if(this->AppendMode)
{
// Open for append.
cmsys::auto_ptr<std::ofstream>
ap(new std::ofstream(this->MainImportFile.c_str(), std::ios::app));
foutPtr = ap;
}
else
{
// Generate atomically and with copy-if-different.
cmsys::auto_ptr<cmGeneratedFileStream>
ap(new cmGeneratedFileStream(this->MainImportFile.c_str(), true));
ap->SetCopyIfDifferent(true);
foutPtr = ap;
}
if(!foutPtr.get() || !*foutPtr)
{ {
std::string se = cmSystemTools::GetLastSystemError(); std::string se = cmSystemTools::GetLastSystemError();
cmOStringStream e; cmOStringStream e;
@ -53,7 +76,7 @@ bool cmExportFileGenerator::GenerateImportFile()
cmSystemTools::Error(e.str().c_str()); cmSystemTools::Error(e.str().c_str());
return false; return false;
} }
std::ostream& os = exportFileStream; std::ostream& os = *foutPtr;
// Start with the import file header. // Start with the import file header.
this->GenerateImportHeaderCode(os); this->GenerateImportHeaderCode(os);

View File

@ -30,6 +30,7 @@
class cmExportFileGenerator class cmExportFileGenerator
{ {
public: public:
cmExportFileGenerator();
virtual ~cmExportFileGenerator() {} virtual ~cmExportFileGenerator() {}
/** Set the full path to the export file to generate. */ /** Set the full path to the export file to generate. */
@ -90,6 +91,7 @@ protected:
std::string FileDir; std::string FileDir;
std::string FileBase; std::string FileBase;
std::string FileExt; std::string FileExt;
bool AppendMode;
// The set of targets included in the export. // The set of targets included in the export.
std::set<cmTarget*> ExportedTargets; std::set<cmTarget*> ExportedTargets;

View File

@ -31,7 +31,11 @@ install(
install(EXPORT exp NAMESPACE exp_ DESTINATION lib/exp) install(EXPORT exp NAMESPACE exp_ DESTINATION lib/exp)
# Export from build tree. # Export from build tree.
export(TARGETS testExe1 testLib1 testLib2 testExe2 testLib3 testLib4 export(TARGETS testExe1 testLib1 testLib2
NAMESPACE bld_ NAMESPACE bld_
FILE ExportBuildTree.cmake FILE ExportBuildTree.cmake
) )
export(TARGETS testExe2 testLib3 testLib4
NAMESPACE bld_
APPEND FILE ExportBuildTree.cmake
)