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;
19 class cmGeneratorTarget;
21 class cmTargetManifest;
23 class cmCustomCommand;
25 /** \class cmLocalGenerator
26 * \brief Create required build files for a directory.
28 * Subclasses of this abstract class generate makefiles, DSP, etc for various
29 * platforms. This class should never be constructed directly. A
30 * GlobalGenerator will create it and invoke the appropriate commands on it.
32 class cmLocalGenerator
36 virtual ~cmLocalGenerator();
39 * Generate the makefile for this directory.
41 virtual void Generate() {}
44 * Process the CMakeLists files for this directory to fill in the
47 virtual void Configure();
50 * Calls TraceVSDependencies() on all targets of this generator.
52 void TraceDependencies();
54 virtual void AddHelperCommands() {}
57 * Perform any final calculations prior to generation
59 void ConfigureFinalPass();
62 * Generate the install rules files in this directory.
64 void GenerateInstallRules();
67 * Generate the test files for tests.
69 void GenerateTestFiles();
72 * Generate a manifest of target files that will be built.
74 void GenerateTargetManifest();
76 ///! Get the makefile for this generator
77 cmMakefile *GetMakefile() {
78 return this->Makefile; };
80 ///! Get the makefile for this generator, const version
81 const cmMakefile *GetMakefile() const {
82 return this->Makefile; };
84 ///! Get the GlobalGenerator this is associated with
85 cmGlobalGenerator *GetGlobalGenerator() {
86 return this->GlobalGenerator; };
87 const cmGlobalGenerator *GetGlobalGenerator() const {
88 return this->GlobalGenerator; };
90 ///! Set the Global Generator, done on creation by the GlobalGenerator
91 void SetGlobalGenerator(cmGlobalGenerator *gg);
94 * Convert something to something else. This is a centralized conversion
95 * routine used by the generators to handle relative paths and the like.
96 * The flags determine what is actually done.
98 * relative: treat the argument as a directory and convert it to make it
99 * relative or full or unchanged. If relative (HOME, START etc) then that
100 * specifies what it should be relative to.
102 * output: make the result suitable for output to a...
104 * optional: should any relative path operation be controlled by the rel
107 enum RelativeRoot { NONE, FULL, HOME, START, HOME_OUTPUT, START_OUTPUT };
108 enum OutputFormat { UNCHANGED, MAKEFILE, SHELL, RESPONSE };
109 std::string ConvertToOutputFormat(const char* source, OutputFormat output);
110 std::string Convert(const char* remote, RelativeRoot local,
111 OutputFormat output = UNCHANGED,
112 bool optional = false);
113 std::string Convert(RelativeRoot remote, const char* local,
114 OutputFormat output = UNCHANGED,
115 bool optional = false);
118 * Get path for the specified relative root.
120 const char* GetRelativeRootPath(RelativeRoot relroot);
123 * Convert the given path to an output path that is optionally
124 * relative based on the cache option CMAKE_USE_RELATIVE_PATHS. The
125 * remote path must use forward slashes and not already be escaped
128 std::string ConvertToOptionallyRelativeOutputPath(const char* remote);
130 ///! set/get the parent generator
131 cmLocalGenerator* GetParent(){return this->Parent;}
132 void SetParent(cmLocalGenerator* g) { this->Parent = g; g->AddChild(this); }
134 ///! set/get the children
135 void AddChild(cmLocalGenerator* g) { this->Children.push_back(g); }
136 std::vector<cmLocalGenerator*>& GetChildren() { return this->Children; };
139 void AddArchitectureFlags(std::string& flags, cmGeneratorTarget* target,
140 const char *lang, const char* config);
142 void AddLanguageFlags(std::string& flags, const char* lang,
144 void AddCMP0018Flags(std::string &flags, cmTarget* target,
145 std::string const& lang, const char *config);
146 void AddVisibilityPresetFlags(std::string &flags, cmTarget* target,
148 void AddConfigVariableFlags(std::string& flags, const char* var,
150 ///! Append flags to a string.
151 virtual void AppendFlags(std::string& flags, const char* newFlags);
152 virtual void AppendFlagEscape(std::string& flags, const char* rawFlag);
153 ///! Get the include flags for the current makefile and language
154 std::string GetIncludeFlags(const std::vector<std::string> &includes,
155 cmGeneratorTarget* target,
156 const char* lang, bool forResponseFile = false,
157 const char *config = 0);
160 * Encode a list of preprocessor definitions for the compiler
163 void AppendDefines(std::set<std::string>& defines,
164 const char* defines_list);
165 void AppendDefines(std::set<std::string>& defines,
166 std::string defines_list)
168 this->AppendDefines(defines, defines_list.c_str());
170 void AppendDefines(std::set<std::string>& defines,
171 const std::vector<std::string> &defines_vec);
174 * Join a set of defines into a definesString with a space separator.
176 void JoinDefines(const std::set<std::string>& defines,
177 std::string &definesString,
180 /** Lookup and append options associated with a particular feature. */
181 void AppendFeatureOptions(std::string& flags, const char* lang,
182 const char* feature);
184 /** \brief Get absolute path to dependency \a name
186 * Translate a dependency as given in CMake code to the name to
187 * appear in a generated build file.
188 * - If \a name is a utility target, returns false.
189 * - If \a name is a CMake target, it will be transformed to the real output
190 * location of that target for the given configuration.
191 * - If \a name is the full path to a file, it will be returned.
192 * - Otherwise \a name is treated as a relative path with respect to
193 * the source directory of this generator. This should only be
194 * used for dependencies of custom commands.
196 bool GetRealDependency(const char* name, const char* config,
199 ///! for existing files convert to output path and short path if spaces
200 std::string ConvertToOutputForExisting(const char* remote,
201 RelativeRoot local = START_OUTPUT);
203 /** For existing path identified by RelativeRoot convert to output
204 path and short path if spaces. */
205 std::string ConvertToOutputForExisting(RelativeRoot remote,
206 const char* local = 0);
208 virtual std::string ConvertToIncludeReference(std::string const& path);
210 /** Called from command-line hook to clear dependencies. */
211 virtual void ClearDependencies(cmMakefile* /* mf */,
212 bool /* verbose */) {}
214 /** Called from command-line hook to update dependencies. */
215 virtual bool UpdateDependencies(const char* /* tgtInfo */,
220 /** Get the include flags for the current makefile and language. */
221 void GetIncludeDirectories(std::vector<std::string>& dirs,
222 cmGeneratorTarget* target,
223 const char* lang = "C", const char *config = 0,
224 bool stripImplicitInclDirs = true);
225 void AddCompileOptions(std::string& flags, cmTarget* target,
226 const char* lang, const char* config);
227 void AddCompileDefinitions(std::set<std::string>& defines, cmTarget* target,
230 /** Compute the language used to compile the given source file. */
231 const char* GetSourceFileLanguage(const cmSourceFile& source);
233 // Fill the vector with the target names for the object files,
234 // preprocessed files and assembly files.
235 virtual void GetIndividualFileTargets(std::vector<std::string>&) {}
237 // Create a struct to hold the varibles passed into
238 // ExpandRuleVariables
243 memset(this, 0, sizeof(*this));
246 const char* TargetPDB;
247 const char* TargetVersionMajor;
248 const char* TargetVersionMinor;
249 const char* Language;
252 const char* LinkLibraries;
254 const char* AssemblySource;
255 const char* PreprocessedSource;
258 const char* ObjectDir;
260 const char* ObjectsQuoted;
261 const char* SONameFlag;
262 const char* TargetSOName;
263 const char* TargetInstallNameDir;
264 const char* LinkFlags;
265 const char* LanguageCompileFlags;
267 const char* RuleLauncher;
268 const char* DependencyFile;
271 /** Set whether to treat conversions to SHELL as a link script shell. */
272 void SetLinkScriptShell(bool b) { this->LinkScriptShell = b; }
274 /** Escape the given string to be used as a command line argument in
275 the native build system shell. Optionally allow the build
276 system to replace make variable references. Optionally adjust
277 escapes for the special case of passing to the native echo
279 std::string EscapeForShell(const char* str, bool makeVars = false,
280 bool forEcho = false);
282 /** Backwards-compatibility version of EscapeForShell. */
283 std::string EscapeForShellOldStyle(const char* str);
285 /** Escape the given string as an argument in a CMake script. */
286 static std::string EscapeForCMake(const char* str);
294 FortranFormat GetFortranFormat(const char* value);
297 * Convert the given remote path to a relative path with respect to
298 * the given local path. The local path must be given in component
299 * form (see SystemTools::SplitPath) without a trailing slash. The
300 * remote path must use forward slashes and not already be escaped
303 std::string ConvertToRelativePath(const std::vector<std::string>& local,
304 const char* remote, bool force=false);
307 * Get the relative path from the generator output directory to a
308 * per-target support directory.
310 virtual std::string GetTargetDirectory(cmTarget const& target) const;
313 * Get the level of backwards compatibility requested by the project
314 * in this directory. This is the value of the CMake variable
315 * CMAKE_BACKWARDS_COMPATIBILITY whose format is
316 * "major.minor[.patch]". The returned integer is encoded as
318 * CMake_VERSION_ENCODE(major, minor, patch)
320 * and is monotonically increasing with the CMake version.
322 unsigned int GetBackwardsCompatibility();
325 * Test whether compatibility is set to a given version or lower.
327 bool NeedBackwardsCompatibility(unsigned int major,
329 unsigned int patch = 0xFFu);
332 * Generate a Mac OS X application bundle Info.plist file.
334 void GenerateAppleInfoPList(cmTarget* target, const char* targetName,
338 * Generate a Mac OS X framework Info.plist file.
340 void GenerateFrameworkInfoPList(cmTarget* target,
341 const char* targetName,
343 /** Construct a comment for a custom command. */
344 std::string ConstructComment(const cmCustomCommand& cc,
345 const char* default_comment = "");
346 // Compute object file names.
347 std::string GetObjectFileNameWithoutTarget(const cmSourceFile& source,
348 std::string const& dir_max,
349 bool* hasSourceExtension = 0);
351 /** Fill out the static linker flags for the given target. */
352 void GetStaticLibraryFlags(std::string& flags,
353 std::string const& config,
356 /** Fill out these strings for the given target. Libraries to link,
357 * flags, and linkflags. */
358 void GetTargetFlags(std::string& linkLibs,
360 std::string& linkFlags,
361 std::string& frameworkPath,
362 std::string& linkPath,
363 cmGeneratorTarget* target);
366 ///! put all the libraries for a target on into the given stream
367 virtual void OutputLinkLibraries(std::string& linkLibraries,
368 std::string& frameworkPath,
369 std::string& linkPath,
373 // Expand rule variables in CMake of the type found in language rules
374 void ExpandRuleVariables(std::string& string,
375 const RuleVariables& replaceValues);
376 // Expand rule variables in a single string
377 std::string ExpandRuleVariable(std::string const& variable,
378 const RuleVariables& replaceValues);
380 const char* GetRuleLauncher(cmTarget* target, const char* prop);
381 void InsertRuleLauncher(std::string& s, cmTarget* target,
385 /** Convert a target to a utility target for unsupported
386 * languages of a generator */
387 void AddBuildTargetRule(const char* llang, cmGeneratorTarget& target);
388 ///! add a custom command to build a .o file that is part of a target
389 void AddCustomCommandToCreateObject(const char* ofname,
391 cmSourceFile& source,
392 cmGeneratorTarget& target);
393 // Create Custom Targets and commands for unsupported languages
394 // The set passed in should contain the languages supported by the
395 // generator directly. Any targets containing files that are not
396 // of the types listed will be compiled as custom commands and added
397 // to a custom target.
398 void CreateCustomTargetsAndCommands(std::set<cmStdString> const&);
400 // Handle old-style install rules stored in the targets.
401 void GenerateTargetInstallRules(
402 std::ostream& os, const char* config,
403 std::vector<std::string> const& configurationTypes);
405 std::string& CreateSafeUniqueObjectFileName(const char* sin,
406 std::string const& dir_max);
407 void ComputeObjectMaxPath();
409 void ConfigureRelativePaths();
410 std::string FindRelativePathTopSource();
411 std::string FindRelativePathTopBinary();
412 void SetupPathConversions();
414 virtual std::string ConvertToLinkReference(std::string const& lib);
416 /** Check whether the native build system supports the given
417 definition. Issues a warning. */
418 virtual bool CheckDefinition(std::string const& define) const;
420 /** Read the input CMakeLists.txt file. */
421 void ReadInputFile();
423 cmMakefile *Makefile;
424 cmGlobalGenerator *GlobalGenerator;
425 // members used for relative path function ConvertToMakefilePath
426 std::string RelativePathToSourceDir;
427 std::string RelativePathToBinaryDir;
428 std::vector<std::string> HomeDirectoryComponents;
429 std::vector<std::string> StartDirectoryComponents;
430 std::vector<std::string> HomeOutputDirectoryComponents;
431 std::vector<std::string> StartOutputDirectoryComponents;
432 cmLocalGenerator* Parent;
433 std::vector<cmLocalGenerator*> Children;
434 std::map<cmStdString, cmStdString> UniqueObjectNamesMap;
435 std::string::size_type ObjectPathMax;
436 std::set<cmStdString> ObjectMaxPathViolations;
444 bool LinkScriptShell;
445 bool UseRelativePaths;
446 bool IgnoreLibPrefix;
448 bool EmitUniversalBinaryFlags;
449 // Hack for ExpandRuleVariable until object-oriented version is
451 std::string TargetImplib;
453 // The top-most directories for relative path conversion. Both the
454 // source and destination location of a relative path conversion
455 // must be underneath one of these directories (both under source or
456 // both under binary) in order for the relative path to be evaluated
457 // safely by the build tools.
458 std::string RelativePathTopSource;
459 std::string RelativePathTopBinary;
460 bool RelativePathsConfigured;
461 bool PathConversionsSetup;
463 unsigned int BackwardsCompatibility;
464 bool BackwardsCompatibilityFinal;
466 std::string ConvertToOutputForExistingCommon(const char* remote,
467 std::string const& result);
469 void AddSharedFlags(std::string& flags, const char* lang, bool shared);
470 bool GetShouldUseOldFlags(bool shared, const std::string &lang) const;
471 void AddPositionIndependentFlags(std::string& flags, std::string const& l,