1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #ifndef cmMakefileTargetGenerator_h
13 #define cmMakefileTargetGenerator_h
15 #include "cmLocalUnixMakefileGenerator3.h"
16 #include "cmOSXBundleGenerator.h"
18 class cmCustomCommand;
19 class cmDependInformation;
21 class cmGeneratorTarget;
22 class cmGeneratedFileStream;
23 class cmGlobalUnixMakefileGenerator3;
24 class cmLocalUnixMakefileGenerator3;
29 /** \class cmMakefileTargetGenerator
30 * \brief Support Routines for writing makefiles
33 class cmMakefileTargetGenerator
36 // constructor to set the ivars
37 cmMakefileTargetGenerator(cmTarget* target);
38 virtual ~cmMakefileTargetGenerator();
40 // construct using this factory call
41 static cmMakefileTargetGenerator *New(cmTarget *tgt);
43 /* the main entry point for this class. Writes the Makefiles associated
45 virtual void WriteRuleFiles() = 0;
47 /* return the number of actions that have progress reporting on them */
48 virtual unsigned long GetNumberOfProgressActions() {
49 return this->NumberOfProgressActions;}
50 std::string GetProgressFileNameFull()
51 { return this->ProgressFileNameFull; }
53 cmTarget* GetTarget() { return this->Target;}
57 // create the file and directory etc
58 void CreateRuleFile();
60 // outputs the rules for object files and custom commands used by
62 void WriteTargetBuildRules();
64 // write some common code at the top of build.make
65 void WriteCommonCodeRules();
66 void WriteTargetLanguageFlags();
68 // write the provide require rules for this target
69 void WriteTargetRequiresRules();
71 // write the clean rules for this target
72 void WriteTargetCleanRules();
74 // write the depend rules for this target
75 void WriteTargetDependRules();
77 // write rules for Mac OS X Application Bundle content.
78 struct MacOSXContentGeneratorType :
79 cmOSXBundleGenerator::MacOSXContentGeneratorType
81 MacOSXContentGeneratorType(cmMakefileTargetGenerator* gen) :
84 void operator()(cmSourceFile& source, const char* pkgloc);
87 cmMakefileTargetGenerator* Generator;
89 friend struct MacOSXContentGeneratorType;
91 // write the rules for an object
92 void WriteObjectRuleFiles(cmSourceFile& source);
94 // write the build rule for an object
95 void WriteObjectBuildFile(std::string &obj,
98 std::vector<std::string>& depends);
100 // write the depend.make file for an object
101 void WriteObjectDependRules(cmSourceFile& source,
102 std::vector<std::string>& depends);
104 // write the build rule for a custom command
105 void GenerateCustomRuleFile(const cmCustomCommand& cc);
107 // write a rule to drive building of more than one output from
109 void GenerateExtraOutput(const char* out, const char* in,
110 bool symbolic = false);
112 void AppendProgress(std::vector<std::string>& commands);
114 // write out the variable that lists the objects for this target
115 void WriteObjectsVariable(std::string& variableName,
116 std::string& variableNameExternal);
117 void WriteObjectsString(std::string& buildObjs);
118 void WriteObjectsStrings(std::vector<std::string>& objStrings,
119 std::string::size_type limit = std::string::npos);
121 // write the driver rule to build target outputs
122 void WriteTargetDriverRule(const char* main_output, bool relink);
124 void DriveCustomCommands(std::vector<std::string>& depends);
126 // Return the a string with -F flags on apple
127 std::string GetFrameworkFlags();
129 void AppendFortranFormatFlags(std::string& flags, cmSourceFile& source);
131 // append intertarget dependencies
132 void AppendTargetDepends(std::vector<std::string>& depends);
134 // Append object file dependencies.
135 void AppendObjectDepends(std::vector<std::string>& depends);
137 // Append link rule dependencies (objects, etc.).
138 void AppendLinkDepends(std::vector<std::string>& depends);
140 // Lookup the link rule for this target.
141 std::string GetLinkRule(const char* linkRuleVar);
143 /** In order to support parallel builds for custom commands with
144 multiple outputs the outputs are given a serial order, and only
145 the first output actually has the build rule. Other outputs
146 just depend on the first one. The check-build-system step must
147 remove a dependee if the depender is missing to make sure both
148 are regenerated properly. This method is used by the local
149 makefile generators to register such pairs. */
150 void AddMultipleOutputPair(const char* depender, const char* dependee);
152 /** Create a script to hold link rules and a command to invoke the
153 script at build time. */
154 void CreateLinkScript(const char* name,
155 std::vector<std::string> const& link_commands,
156 std::vector<std::string>& makefile_commands,
157 std::vector<std::string>& makefile_depends);
159 /** Create a response file with the given set of options. Returns
160 the relative path from the target build working directory to the
161 response file name. */
162 std::string CreateResponseFile(const char* name,
163 std::string const& options,
164 std::vector<std::string>& makefile_depends);
166 /** Create lists of object files for linking and cleaning. */
167 void CreateObjectLists(bool useLinkScript, bool useArchiveRules,
168 bool useResponseFile, std::string& buildObjs,
169 std::vector<std::string>& makefile_depends);
171 void AddIncludeFlags(std::string& flags, const char* lang);
173 virtual void CloseFileStreams();
174 void RemoveForbiddenFlags(const char* flagVar, const char* linkLang,
175 std::string& linkFlags);
177 cmGeneratorTarget* GeneratorTarget;
178 cmLocalUnixMakefileGenerator3 *LocalGenerator;
179 cmGlobalUnixMakefileGenerator3 *GlobalGenerator;
180 cmMakefile *Makefile;
181 const char *ConfigName;
183 enum CustomCommandDriveType { OnBuild, OnDepends, OnUtility };
184 CustomCommandDriveType CustomCommandDriver;
186 // the full path to the build file
187 std::string BuildFileName;
188 std::string BuildFileNameFull;
190 // the full path to the progress file
191 std::string ProgressFileNameFull;
192 unsigned long NumberOfProgressActions;
195 // the path to the directory the build file is in
196 std::string TargetBuildDirectory;
197 std::string TargetBuildDirectoryFull;
199 // the stream for the build file
200 cmGeneratedFileStream *BuildFileStream;
202 // the stream for the flag file
203 std::string FlagFileNameFull;
204 cmGeneratedFileStream *FlagFileStream;
205 class StringList: public std::vector<std::string> {};
206 std::map<cmStdString, StringList> FlagFileDepends;
208 // the stream for the info file
209 std::string InfoFileNameFull;
210 cmGeneratedFileStream *InfoFileStream;
213 std::vector<std::string> CleanFiles;
215 // objects used by this target
216 std::vector<std::string> Objects;
217 std::vector<std::string> ExternalObjects;
219 // Set of object file names that will be built in this directory.
220 std::set<cmStdString> ObjectFiles;
222 // Set of extra output files to be driven by the build.
223 std::set<cmStdString> ExtraFiles;
225 typedef std::map<cmStdString, cmStdString> MultipleOutputPairsType;
226 MultipleOutputPairsType MultipleOutputPairs;
229 std::string TargetNameOut;
230 std::string TargetNameSO;
231 std::string TargetNameReal;
232 std::string TargetNameImport;
233 std::string TargetNamePDB;
235 // Mac OS X content info.
236 std::set<cmStdString> MacContentFolders;
237 cmOSXBundleGenerator* OSXBundleGenerator;
238 MacOSXContentGeneratorType* MacOSXContentGenerator;
240 typedef std::map<cmStdString, cmStdString> ByLanguageMap;
241 std::string GetFlags(const std::string &l);
242 ByLanguageMap FlagsByLanguage;
243 std::string GetDefines(const std::string &l);
244 ByLanguageMap DefinesByLanguage;
246 // Target-wide Fortran module output directory.
247 bool FortranModuleDirectoryComputed;
248 std::string FortranModuleDirectory;
249 const char* GetFortranModuleDirectory();
251 // Compute target-specific Fortran language flags.
252 void AddFortranFlags(std::string& flags);
254 // Helper to add flag for windows .def file.
255 void AddModuleDefinitionFlag(std::string& flags);
257 // Add language feature flags.
258 void AddFeatureFlags(std::string& flags, const char* lang);
260 // Feature query methods.
261 const char* GetFeature(const char* feature);
262 bool GetFeatureAsBool(const char* feature);
264 //==================================================================
265 // Convenience routines that do nothing more than forward to
267 std::string Convert(const char* source,
268 cmLocalGenerator::RelativeRoot relative,
269 cmLocalGenerator::OutputFormat output =
270 cmLocalGenerator::UNCHANGED,
271 bool optional = false)
273 return this->LocalGenerator->Convert(source, relative, output, optional);