1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmExportCommand.cxx,v $
6 Date: $Date: 2008-02-06 19:20:35 $
7 Version: $Revision: 1.11 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmExportCommand.h"
18 #include "cmGlobalGenerator.h"
19 #include "cmLocalGenerator.h"
20 #include "cmGeneratedFileStream.h"
23 #include "cmExportBuildFileGenerator.h"
25 cmExportCommand::cmExportCommand()
28 ,Targets(&Helper
, "TARGETS")
29 ,Append(&Helper
, "APPEND", &ArgumentGroup
)
30 ,Namespace(&Helper
, "NAMESPACE", &ArgumentGroup
)
31 ,Filename(&Helper
, "FILE", &ArgumentGroup
)
34 this->Targets
.Follows(0);
35 // and after that the other options in any order
36 this->ArgumentGroup
.Follows(&this->Targets
);
42 ::InitialPass(std::vector
<std::string
> const& args
, cmExecutionStatus
&)
46 this->SetError("called with too few arguments");
50 std::vector
<std::string
> unknownArgs
;
51 this->Helper
.Parse(&args
, &unknownArgs
);
53 if (!unknownArgs
.empty())
55 this->SetError("Unknown arguments.");
59 if (this->Targets
.WasFound() == false)
61 this->SetError("TARGETS option missing.");
65 if(!this->Filename
.WasFound())
67 this->SetError("FILE <filename> option missing.");
71 // Make sure the file has a .cmake extension.
72 if(cmSystemTools::GetFilenameLastExtension(this->Filename
.GetCString())
76 e
<< "FILE option given filename \"" << this->Filename
.GetString()
77 << "\" which does not have an extension of \".cmake\".\n";
78 this->SetError(e
.str().c_str());
82 // Get the file to write.
83 std::string fname
= this->Filename
.GetString();
84 if(cmSystemTools::FileIsFullPath(fname
.c_str()))
86 if(!this->Makefile
->CanIWriteThisFile(fname
.c_str()))
89 e
<< "FILE option given filename \"" << fname
90 << "\" which is in the source tree.\n";
91 this->SetError(e
.str().c_str());
97 // Interpret relative paths with respect to the current build dir.
98 fname
= this->Makefile
->GetCurrentOutputDirectory();
100 fname
+= this->Filename
.GetString();
103 // Collect the targets to be exported.
104 std::vector
<cmTarget
*> targets
;
105 for(std::vector
<std::string
>::const_iterator
106 currentTarget
= this->Targets
.GetVector().begin();
107 currentTarget
!= this->Targets
.GetVector().end();
110 if(cmTarget
* target
=
111 this->Makefile
->GetLocalGenerator()->
112 GetGlobalGenerator()->FindTarget(0, currentTarget
->c_str()))
114 if((target
->GetType() == cmTarget::EXECUTABLE
) ||
115 (target
->GetType() == cmTarget::STATIC_LIBRARY
) ||
116 (target
->GetType() == cmTarget::SHARED_LIBRARY
) ||
117 (target
->GetType() == cmTarget::MODULE_LIBRARY
))
119 targets
.push_back(target
);
124 e
<< "given target \"" << *currentTarget
125 << "\" which is not an executable or library.";
126 this->SetError(e
.str().c_str());
133 e
<< "given target \"" << *currentTarget
134 << "\" which is not built by this project.";
135 this->SetError(e
.str().c_str());
140 // Setup export file generation.
141 cmExportBuildFileGenerator ebfg
;
142 ebfg
.SetExportFile(fname
.c_str());
143 ebfg
.SetNamespace(this->Namespace
.GetCString());
144 ebfg
.SetAppendMode(this->Append
.IsEnabled());
145 ebfg
.SetExports(&targets
);
146 ebfg
.SetCommand(this);
148 // Compute the set of configurations exported.
149 if(const char* types
=
150 this->Makefile
->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
152 std::vector
<std::string
> configurationTypes
;
153 cmSystemTools::ExpandListArgument(types
, configurationTypes
);
154 for(std::vector
<std::string
>::const_iterator
155 ci
= configurationTypes
.begin();
156 ci
!= configurationTypes
.end(); ++ci
)
158 ebfg
.AddConfiguration(ci
->c_str());
161 else if(const char* config
=
162 this->Makefile
->GetDefinition("CMAKE_BUILD_TYPE"))
164 ebfg
.AddConfiguration(config
);
168 ebfg
.AddConfiguration("");
171 // Generate the import file.
172 if(!ebfg
.GenerateImportFile() && this->ErrorMessage
.empty())
174 this->SetError("could not write export file.");
178 // Report generated error message if any.
179 if(!this->ErrorMessage
.empty())
181 this->SetError(this->ErrorMessage
.c_str());