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 cmLocalGenerator_h
13 #define cmLocalGenerator_h
15 #include "cmStandardIncludes.h"
18 class cmGlobalGenerator;
20 class cmTargetManifest;
22 class cmCustomCommand;
24 /** \class cmLocalGenerator
25 * \brief Create required build files for a directory.
27 * Subclasses of this abstract class generate makefiles, DSP, etc for various
28 * platforms. This class should never be constructed directly. A
29 * GlobalGenerator will create it and invoke the appropriate commands on it.
31 class cmLocalGenerator
35 virtual ~cmLocalGenerator();
38 * Generate the makefile for this directory.
40 virtual void Generate() {}
43 * Process the CMakeLists files for this directory to fill in the
46 virtual void Configure();
49 * Calls TraceVSDependencies() on all targets of this generator.
51 virtual void TraceDependencies();
53 virtual void AddHelperCommands() {}
56 * Perform any final calculations prior to generation
58 virtual void ConfigureFinalPass();
61 * Generate the install rules files in this directory.
63 virtual void GenerateInstallRules();
66 * Generate the test files for tests.
68 virtual void GenerateTestFiles();
71 * Generate a manifest of target files that will be built.
73 virtual void GenerateTargetManifest();
75 ///! Get the makefile for this generator
76 cmMakefile *GetMakefile() {
77 return this->Makefile; };
79 ///! Get the makefile for this generator, const version
80 const cmMakefile *GetMakefile() const {
81 return this->Makefile; };
83 ///! Get the GlobalGenerator this is associated with
84 cmGlobalGenerator *GetGlobalGenerator() {
85 return this->GlobalGenerator; };
86 const cmGlobalGenerator *GetGlobalGenerator() const {
87 return this->GlobalGenerator; };
89 ///! Set the Global Generator, done on creation by the GlobalGenerator
90 void SetGlobalGenerator(cmGlobalGenerator *gg);
93 * Convert something to something else. This is a centralized conversion
94 * routine used by the generators to handle relative paths and the like.
95 * The flags determine what is actually done.
97 * relative: treat the argument as a directory and convert it to make it
98 * relative or full or unchanged. If relative (HOME, START etc) then that
99 * specifies what it should be relative to.
101 * output: make the result suitable for output to a...
103 * optional: should any relative path operation be controlled by the rel
106 enum RelativeRoot { NONE, FULL, HOME, START, HOME_OUTPUT, START_OUTPUT };
107 enum OutputFormat { UNCHANGED, MAKEFILE, SHELL, RESPONSE };
108 std::string ConvertToOutputFormat(const char* source, OutputFormat output);
109 std::string Convert(const char* remote, RelativeRoot local,
110 OutputFormat output = UNCHANGED,
111 bool optional = false);
112 std::string Convert(RelativeRoot remote, const char* local,
113 OutputFormat output = UNCHANGED,
114 bool optional = false);
117 * Get path for the specified relative root.
119 const char* GetRelativeRootPath(RelativeRoot relroot);
122 * Convert the given path to an output path that is optionally
123 * relative based on the cache option CMAKE_USE_RELATIVE_PATHS. The
124 * remote path must use forward slashes and not already be escaped
127 std::string ConvertToOptionallyRelativeOutputPath(const char* remote);
129 ///! set/get the parent generator
130 cmLocalGenerator* GetParent(){return this->Parent;}
131 void SetParent(cmLocalGenerator* g) { this->Parent = g; g->AddChild(this); }
133 ///! set/get the children
134 void AddChild(cmLocalGenerator* g) { this->Children.push_back(g); }
135 std::vector<cmLocalGenerator*>& GetChildren() { return this->Children; };
138 void AddArchitectureFlags(std::string& flags, cmTarget* target,
139 const char *lang, const char* config);
141 void AddLanguageFlags(std::string& flags, const char* lang,
143 void AddCMP0018Flags(std::string &flags, cmTarget* target,
144 std::string const& lang);
145 void AddConfigVariableFlags(std::string& flags, const char* var,
147 ///! Append flags to a string.
148 virtual void AppendFlags(std::string& flags, const char* newFlags);
149 ///! Get the include flags for the current makefile and language
150 std::string GetIncludeFlags(const std::vector<std::string> &includes,
151 const char* lang, bool forResponseFile = false);
154 * Encode a list of preprocessor definitions for the compiler
157 void AppendDefines(std::string& defines, const char* defines_list,
160 /** Lookup and append options associated with a particular feature. */
161 void AppendFeatureOptions(std::string& flags, const char* lang,
162 const char* feature);
164 /** \brief Get absolute path to dependency \a name
166 * Translate a dependency as given in CMake code to the name to
167 * appear in a generated build file.
168 * - If \a name is a utility target, returns false.
169 * - If \a name is a CMake target, it will be transformed to the real output
170 * location of that target for the given configuration.
171 * - If \a name is the full path to a file, it will be returned.
172 * - Otherwise \a name is treated as a relative path with respect to
173 * the source directory of this generator. This should only be
174 * used for dependencies of custom commands.
176 bool GetRealDependency(const char* name, const char* config,
179 ///! for existing files convert to output path and short path if spaces
180 std::string ConvertToOutputForExisting(const char* remote,
181 RelativeRoot local = START_OUTPUT);
183 /** For existing path identified by RelativeRoot convert to output
184 path and short path if spaces. */
185 std::string ConvertToOutputForExisting(RelativeRoot remote,
186 const char* local = 0);
188 virtual std::string ConvertToIncludeReference(std::string const& path);
190 /** Called from command-line hook to clear dependencies. */
191 virtual void ClearDependencies(cmMakefile* /* mf */,
192 bool /* verbose */) {}
194 /** Called from command-line hook to update dependencies. */
195 virtual bool UpdateDependencies(const char* /* tgtInfo */,
200 /** Get the include flags for the current makefile and language. */
201 void GetIncludeDirectories(std::vector<std::string>& dirs,
203 const char* lang = "C");
205 /** Compute the language used to compile the given source file. */
206 const char* GetSourceFileLanguage(const cmSourceFile& source);
208 // Fill the vector with the target names for the object files,
209 // preprocessed files and assembly files.
210 virtual void GetIndividualFileTargets(std::vector<std::string>&) {}
212 // Create a struct to hold the varibles passed into
213 // ExpandRuleVariables
218 memset(this, 0, sizeof(*this));
221 const char* TargetPDB;
222 const char* TargetVersionMajor;
223 const char* TargetVersionMinor;
224 const char* Language;
227 const char* LinkLibraries;
229 const char* AssemblySource;
230 const char* PreprocessedSource;
233 const char* ObjectDir;
235 const char* ObjectsQuoted;
236 const char* SONameFlag;
237 const char* TargetSOName;
238 const char* TargetInstallNameDir;
239 const char* LinkFlags;
240 const char* LanguageCompileFlags;
242 const char* RuleLauncher;
243 const char* DependencyFile;
246 /** Set whether to treat conversions to SHELL as a link script shell. */
247 void SetLinkScriptShell(bool b) { this->LinkScriptShell = b; }
249 /** Escape the given string to be used as a command line argument in
250 the native build system shell. Optionally allow the build
251 system to replace make variable references. Optionally adjust
252 escapes for the special case of passing to the native echo
254 std::string EscapeForShell(const char* str, bool makeVars = false,
255 bool forEcho = false);
257 /** Backwards-compatibility version of EscapeForShell. */
258 std::string EscapeForShellOldStyle(const char* str);
260 /** Escape the given string as an argument in a CMake script. */
261 static std::string EscapeForCMake(const char* str);
269 FortranFormat GetFortranFormat(const char* value);
272 * Convert the given remote path to a relative path with respect to
273 * the given local path. The local path must be given in component
274 * form (see SystemTools::SplitPath) without a trailing slash. The
275 * remote path must use forward slashes and not already be escaped
278 std::string ConvertToRelativePath(const std::vector<std::string>& local,
279 const char* remote, bool force=false);
282 * Get the relative path from the generator output directory to a
283 * per-target support directory.
285 virtual std::string GetTargetDirectory(cmTarget const& target) const;
288 * Get the level of backwards compatibility requested by the project
289 * in this directory. This is the value of the CMake variable
290 * CMAKE_BACKWARDS_COMPATIBILITY whose format is
291 * "major.minor[.patch]". The returned integer is encoded as
293 * CMake_VERSION_ENCODE(major, minor, patch)
295 * and is monotonically increasing with the CMake version.
297 unsigned int GetBackwardsCompatibility();
300 * Test whether compatibility is set to a given version or lower.
302 bool NeedBackwardsCompatibility(unsigned int major,
304 unsigned int patch = 0xFFu);
307 * Generate a Mac OS X application bundle Info.plist file.
309 void GenerateAppleInfoPList(cmTarget* target, const char* targetName,
313 * Generate a Mac OS X framework Info.plist file.
315 void GenerateFrameworkInfoPList(cmTarget* target,
316 const char* targetName,
318 /** Construct a comment for a custom command. */
319 std::string ConstructComment(const cmCustomCommand& cc,
320 const char* default_comment = "");
321 // Compute object file names.
322 std::string GetObjectFileNameWithoutTarget(const cmSourceFile& source,
323 std::string const& dir_max,
324 bool* hasSourceExtension = 0);
326 /** Fill out these strings for the given target. Libraries to link,
327 * flags, and linkflags. */
328 void GetTargetFlags(std::string& linkLibs,
330 std::string& linkFlags,
334 ///! put all the libraries for a target on into the given stream
335 virtual void OutputLinkLibraries(std::ostream&, cmTarget&, bool relink);
337 // Expand rule variables in CMake of the type found in language rules
338 void ExpandRuleVariables(std::string& string,
339 const RuleVariables& replaceValues);
340 // Expand rule variables in a single string
341 std::string ExpandRuleVariable(std::string const& variable,
342 const RuleVariables& replaceValues);
344 const char* GetRuleLauncher(cmTarget* target, const char* prop);
345 void InsertRuleLauncher(std::string& s, cmTarget* target,
349 /** Convert a target to a utility target for unsupported
350 * languages of a generator */
351 void AddBuildTargetRule(const char* llang, cmTarget& target);
352 ///! add a custom command to build a .o file that is part of a target
353 void AddCustomCommandToCreateObject(const char* ofname,
355 cmSourceFile& source,
357 // Create Custom Targets and commands for unsupported languages
358 // The set passed in should contain the languages supported by the
359 // generator directly. Any targets containing files that are not
360 // of the types listed will be compiled as custom commands and added
361 // to a custom target.
362 void CreateCustomTargetsAndCommands(std::set<cmStdString> const&);
364 // Handle old-style install rules stored in the targets.
365 void GenerateTargetInstallRules(
366 std::ostream& os, const char* config,
367 std::vector<std::string> const& configurationTypes);
369 std::string& CreateSafeUniqueObjectFileName(const char* sin,
370 std::string const& dir_max);
371 void ComputeObjectMaxPath();
373 void ConfigureRelativePaths();
374 std::string FindRelativePathTopSource();
375 std::string FindRelativePathTopBinary();
376 void SetupPathConversions();
378 virtual std::string ConvertToLinkReference(std::string const& lib);
380 /** Check whether the native build system supports the given
381 definition. Issues a warning. */
382 virtual bool CheckDefinition(std::string const& define) const;
384 /** Read the input CMakeLists.txt file. */
385 void ReadInputFile();
387 cmMakefile *Makefile;
388 cmGlobalGenerator *GlobalGenerator;
389 // members used for relative path function ConvertToMakefilePath
390 std::string RelativePathToSourceDir;
391 std::string RelativePathToBinaryDir;
392 std::vector<std::string> HomeDirectoryComponents;
393 std::vector<std::string> StartDirectoryComponents;
394 std::vector<std::string> HomeOutputDirectoryComponents;
395 std::vector<std::string> StartOutputDirectoryComponents;
396 cmLocalGenerator* Parent;
397 std::vector<cmLocalGenerator*> Children;
398 std::map<cmStdString, cmStdString> UniqueObjectNamesMap;
399 std::string::size_type ObjectPathMax;
400 std::set<cmStdString> ObjectMaxPathViolations;
408 bool LinkScriptShell;
409 bool UseRelativePaths;
410 bool IgnoreLibPrefix;
412 bool EmitUniversalBinaryFlags;
413 // A type flag is not nice. It's used only in TraceDependencies().
414 bool IsMakefileGenerator;
415 // Hack for ExpandRuleVariable until object-oriented version is
417 std::string TargetImplib;
419 // The top-most directories for relative path conversion. Both the
420 // source and destination location of a relative path conversion
421 // must be underneath one of these directories (both under source or
422 // both under binary) in order for the relative path to be evaluated
423 // safely by the build tools.
424 std::string RelativePathTopSource;
425 std::string RelativePathTopBinary;
426 bool RelativePathsConfigured;
427 bool PathConversionsSetup;
429 unsigned int BackwardsCompatibility;
430 bool BackwardsCompatibilityFinal;
432 std::string ConvertToOutputForExistingCommon(const char* remote,
433 std::string const& result);
435 void AddSharedFlags(std::string& flags, const char* lang, bool shared);
436 bool GetShouldUseOldFlags(bool shared, const std::string &lang) const;
437 void AddPositionIndependentFlags(std::string& flags, std::string const& l,