1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmMakefileExecutableTargetGenerator.cxx,v $
6 Date: $Date: 2007-12-28 19:59:06 $
7 Version: $Revision: 1.39 $
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 "cmMakefileExecutableTargetGenerator.h"
19 #include "cmGeneratedFileStream.h"
20 #include "cmGlobalUnixMakefileGenerator3.h"
21 #include "cmLocalUnixMakefileGenerator3.h"
22 #include "cmMakefile.h"
23 #include "cmSourceFile.h"
27 //----------------------------------------------------------------------------
28 cmMakefileExecutableTargetGenerator::cmMakefileExecutableTargetGenerator()
30 this->CustomCommandDriver
= OnDepends
;
33 //----------------------------------------------------------------------------
34 void cmMakefileExecutableTargetGenerator::WriteRuleFiles()
36 // create the build.make file and directory, put in the common blocks
37 this->CreateRuleFile();
39 // write rules used to help build object files
40 this->WriteCommonCodeRules();
42 // write in rules for object files and custom commands
43 this->WriteTargetBuildRules();
45 // write the per-target per-language flags
46 this->WriteTargetLanguageFlags();
48 // write the link rules
49 this->WriteExecutableRule(false);
50 if(this->Target
->NeedRelinkBeforeInstall())
52 // Write rules to link an installable version of the target.
53 this->WriteExecutableRule(true);
56 // Write the requires target.
57 this->WriteTargetRequiresRules();
60 this->WriteTargetCleanRules();
62 // Write the dependency generation rule. This must be done last so
63 // that multiple output pair information is available.
64 this->WriteTargetDependRules();
67 this->CloseFileStreams();
72 //----------------------------------------------------------------------------
73 void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink
)
75 std::vector
<std::string
> commands
;
77 std::string relPath
= this->LocalGenerator
->GetHomeRelativeOutputPath();
78 std::string objTarget
;
80 // Build list of dependencies.
81 std::vector
<std::string
> depends
;
82 for(std::vector
<std::string
>::const_iterator obj
= this->Objects
.begin();
83 obj
!= this->Objects
.end(); ++obj
)
86 // Handle extra content on Mac bundles
87 if ( this->ExtraContent
.find(*obj
) != this->ExtraContent
.end() )
92 depends
.push_back(objTarget
);
95 // Add dependencies on targets that must be built first.
96 this->AppendTargetDepends(depends
);
98 // Add a dependency on the rule file itself.
99 this->LocalGenerator
->AppendRuleDepend(depends
,
100 this->BuildFileNameFull
.c_str());
102 for(std::vector
<std::string
>::const_iterator obj
=
103 this->ExternalObjects
.begin();
104 obj
!= this->ExternalObjects
.end(); ++obj
)
106 depends
.push_back(*obj
);
109 // from here up is the same for exe or lib
111 // Get the name of the executable to generate.
112 std::string targetName
;
113 std::string targetNameReal
;
114 std::string targetNameImport
;
115 std::string targetNamePDB
;
116 this->Target
->GetExecutableNames
117 (targetName
, targetNameReal
, targetNameImport
, targetNamePDB
,
118 this->LocalGenerator
->ConfigurationName
.c_str());
120 // Construct the full path version of the names.
121 std::string outpath
= this->Target
->GetDirectory();
124 if(this->Target
->GetPropertyAsBool("MACOSX_BUNDLE"))
126 // Compute bundle directory names.
127 std::string macdir
= outpath
;
128 macdir
+= targetName
;
129 macdir
+= ".app/Contents/";
132 cmSystemTools::MakeDirectory(outpath
.c_str());
135 // Make bundle directories
137 this->Makefile
->GetModulesFile("MacOSXBundleInfo.plist.in");
138 if ( f1
.size() == 0 )
140 cmSystemTools::Error("could not find Mac OSX bundle template file.");
143 std::vector
<cmSourceFile
*>::const_iterator sourceIt
;
144 for ( sourceIt
= this->Target
->GetSourceFiles().begin();
145 sourceIt
!= this->Target
->GetSourceFiles().end();
149 (*sourceIt
)->GetProperty("MACOSX_PACKAGE_LOCATION");
152 std::string newDir
= macdir
;
154 if ( !cmSystemTools::MakeDirectory(newDir
.c_str()) )
156 cmSystemTools::Error("Cannot create a subdirectory for \"",
157 newDir
.c_str(), "\".");
163 // Configure the Info.plist file. Note that it needs the executable name
165 std::string f2
= macdir
+ "Info.plist";
166 this->Makefile
->AddDefinition("MACOSX_BUNDLE_EXECUTABLE_NAME",
168 this->Makefile
->ConfigureFile(f1
.c_str(), f2
.c_str(),
169 false, false, false);
172 std::string outpathImp
;
175 outpath
= this->Makefile
->GetStartOutputDirectory();
176 outpath
+= cmake::GetCMakeFilesDirectory();
177 outpath
+= "/CMakeRelink.dir";
178 cmSystemTools::MakeDirectory(outpath
.c_str());
180 if(!targetNameImport
.empty())
182 outpathImp
= outpath
;
187 if(!targetNameImport
.empty())
189 outpathImp
= this->Target
->GetDirectory(0, true);
193 std::string targetFullPath
= outpath
+ targetName
;
194 std::string targetFullPathReal
= outpath
+ targetNameReal
;
195 std::string targetFullPathPDB
= outpath
+ targetNamePDB
;
196 std::string targetFullPathImport
= outpathImp
+ targetNameImport
;
197 std::string targetOutPathPDB
=
198 this->Convert(targetFullPathPDB
.c_str(),
199 cmLocalGenerator::FULL
,
200 cmLocalGenerator::SHELL
);
201 // Convert to the output path to use in constructing commands.
202 std::string targetOutPath
=
203 this->Convert(targetFullPath
.c_str(),
204 cmLocalGenerator::START_OUTPUT
,
205 cmLocalGenerator::SHELL
);
206 std::string targetOutPathReal
=
207 this->Convert(targetFullPathReal
.c_str(),
208 cmLocalGenerator::START_OUTPUT
,
209 cmLocalGenerator::SHELL
);
210 std::string targetOutPathImport
=
211 this->Convert(targetFullPathImport
.c_str(),
212 cmLocalGenerator::START_OUTPUT
,
213 cmLocalGenerator::SHELL
);
215 // Get the language to use for linking this executable.
216 const char* linkLanguage
=
217 this->Target
->GetLinkerLanguage(this->GlobalGenerator
);
219 // Make sure we have a link language.
222 cmSystemTools::Error("Cannot determine link language for target \"",
223 this->Target
->GetName(), "\".");
227 // Add the link message.
228 std::string buildEcho
= "Linking ";
229 buildEcho
+= linkLanguage
;
230 buildEcho
+= " executable ";
231 buildEcho
+= targetOutPath
;
232 this->LocalGenerator
->AppendEcho(commands
, buildEcho
.c_str(),
233 cmLocalUnixMakefileGenerator3::EchoLink
);
235 // Build a list of compiler flags and linker flags.
237 std::string linkFlags
;
239 // Add flags to deal with shared libraries. Any library being
240 // linked in might be shared, so always use shared flags for an
242 this->LocalGenerator
->AddSharedFlags(linkFlags
, linkLanguage
, true);
244 // Add flags to create an executable.
245 this->LocalGenerator
->
246 AddConfigVariableFlags(linkFlags
, "CMAKE_EXE_LINKER_FLAGS",
247 this->LocalGenerator
->ConfigurationName
.c_str());
250 if(this->Target
->GetPropertyAsBool("WIN32_EXECUTABLE"))
252 this->LocalGenerator
->AppendFlags
253 (linkFlags
, this->Makefile
->GetDefinition("CMAKE_CREATE_WIN32_EXE"));
257 this->LocalGenerator
->AppendFlags
258 (linkFlags
, this->Makefile
->GetDefinition("CMAKE_CREATE_CONSOLE_EXE"));
261 // Add symbol export flags if necessary.
262 if(this->Target
->GetPropertyAsBool("ENABLE_EXPORTS"))
264 std::string export_flag_var
= "CMAKE_EXE_EXPORTS_";
265 export_flag_var
+= linkLanguage
;
266 export_flag_var
+= "_FLAG";
267 this->LocalGenerator
->AppendFlags
268 (linkFlags
, this->Makefile
->GetDefinition(export_flag_var
.c_str()));
271 // Add language-specific flags.
273 ->AddLanguageFlags(flags
, linkLanguage
,
274 this->LocalGenerator
->ConfigurationName
.c_str());
276 // Add target-specific linker flags.
277 this->LocalGenerator
->AppendFlags
278 (linkFlags
, this->Target
->GetProperty("LINK_FLAGS"));
279 std::string linkFlagsConfig
= "LINK_FLAGS_";
281 cmSystemTools::UpperCase(this->LocalGenerator
->ConfigurationName
.c_str());
282 this->LocalGenerator
->AppendFlags
283 (linkFlags
, this->Target
->GetProperty(linkFlagsConfig
.c_str()));
285 // Construct a list of files associated with this executable that
286 // may need to be cleaned.
287 std::vector
<std::string
> exeCleanFiles
;
289 std::string cleanName
;
290 std::string cleanRealName
;
291 std::string cleanImportName
;
292 std::string cleanPDBName
;
293 this->Target
->GetExecutableCleanNames
294 (cleanName
, cleanRealName
, cleanImportName
, cleanPDBName
,
295 this->LocalGenerator
->ConfigurationName
.c_str());
297 std::string cleanFullName
= outpath
+ cleanName
;
298 std::string cleanFullRealName
= outpath
+ cleanRealName
;
299 std::string cleanFullPDBName
= outpath
+ cleanPDBName
;
300 std::string cleanFullImportName
= outpathImp
+ cleanImportName
;
301 exeCleanFiles
.push_back(this->Convert(cleanFullName
.c_str(),
302 cmLocalGenerator::START_OUTPUT
,
303 cmLocalGenerator::UNCHANGED
));
305 // There may be a manifest file for this target. Add it to the
306 // clean set just in case.
307 exeCleanFiles
.push_back(this->Convert((cleanFullName
+".manifest").c_str(),
308 cmLocalGenerator::START_OUTPUT
,
309 cmLocalGenerator::UNCHANGED
));
311 if(cleanRealName
!= cleanName
)
313 exeCleanFiles
.push_back(this->Convert(cleanFullRealName
.c_str(),
314 cmLocalGenerator::START_OUTPUT
,
315 cmLocalGenerator::UNCHANGED
));
317 if(!cleanImportName
.empty())
319 exeCleanFiles
.push_back(this->Convert(cleanFullImportName
.c_str(),
320 cmLocalGenerator::START_OUTPUT
,
321 cmLocalGenerator::UNCHANGED
));
324 // List the PDB for cleaning only when the whole target is
325 // cleaned. We do not want to delete the .pdb file just before
326 // linking the target.
327 this->CleanFiles
.push_back
328 (this->Convert(cleanFullPDBName
.c_str(),
329 cmLocalGenerator::START_OUTPUT
,
330 cmLocalGenerator::UNCHANGED
));
333 // Add the pre-build and pre-link rules building but not when relinking.
337 ->AppendCustomCommands(commands
, this->Target
->GetPreBuildCommands());
339 ->AppendCustomCommands(commands
, this->Target
->GetPreLinkCommands());
342 // Determine whether a link script will be used.
343 bool useLinkScript
= this->GlobalGenerator
->GetUseLinkScript();
345 // Construct the main link rule.
346 std::vector
<std::string
> real_link_commands
;
347 std::string linkRuleVar
= "CMAKE_";
348 linkRuleVar
+= linkLanguage
;
349 linkRuleVar
+= "_LINK_EXECUTABLE";
350 std::string linkRule
=
351 this->Makefile
->GetRequiredDefinition(linkRuleVar
.c_str());
352 std::vector
<std::string
> commands1
;
353 cmSystemTools::ExpandListArgument(linkRule
, real_link_commands
);
354 if(this->Target
->GetPropertyAsBool("ENABLE_EXPORTS"))
356 // If a separate rule for creating an import library is specified
358 std::string implibRuleVar
= "CMAKE_";
359 implibRuleVar
+= linkLanguage
;
360 implibRuleVar
+= "_CREATE_IMPORT_LIBRARY";
361 if(const char* rule
=
362 this->Makefile
->GetDefinition(implibRuleVar
.c_str()))
364 cmSystemTools::ExpandListArgument(rule
, real_link_commands
);
368 // Expand the rule variables.
370 // Collect up flags to link in needed libraries.
371 cmOStringStream linklibs
;
372 this->LocalGenerator
->OutputLinkLibraries(linklibs
, *this->Target
, relink
);
374 // Construct object file lists that may be needed to expand the
376 std::string variableName
;
377 std::string variableNameExternal
;
378 this->WriteObjectsVariable(variableName
, variableNameExternal
);
379 std::string buildObjs
;
382 this->WriteObjectsString(buildObjs
);
387 buildObjs
+= variableName
;
389 buildObjs
+= variableNameExternal
;
392 std::string cleanObjs
= "$(";
393 cleanObjs
+= variableName
;
396 cmLocalGenerator::RuleVariables vars
;
397 vars
.Language
= linkLanguage
;
398 vars
.Objects
= buildObjs
.c_str();
399 vars
.Target
= targetOutPathReal
.c_str();
400 vars
.TargetPDB
= targetOutPathPDB
.c_str();
402 // Setup the target version.
403 std::string targetVersionMajor
;
404 std::string targetVersionMinor
;
406 cmOStringStream majorStream
;
407 cmOStringStream minorStream
;
410 this->Target
->GetTargetVersion(major
, minor
);
411 majorStream
<< major
;
412 minorStream
<< minor
;
413 targetVersionMajor
= majorStream
.str();
414 targetVersionMinor
= minorStream
.str();
416 vars
.TargetVersionMajor
= targetVersionMajor
.c_str();
417 vars
.TargetVersionMinor
= targetVersionMinor
.c_str();
419 std::string linkString
= linklibs
.str();
420 vars
.LinkLibraries
= linkString
.c_str();
421 vars
.Flags
= flags
.c_str();
422 vars
.LinkFlags
= linkFlags
.c_str();
423 // Expand placeholders in the commands.
424 this->LocalGenerator
->TargetImplib
= targetOutPathImport
;
425 for(std::vector
<std::string
>::iterator i
= real_link_commands
.begin();
426 i
!= real_link_commands
.end(); ++i
)
428 this->LocalGenerator
->ExpandRuleVariables(*i
, vars
);
430 this->LocalGenerator
->TargetImplib
= "";
433 // Optionally convert the build rule to use a script to avoid long
434 // command lines in the make shell.
437 // Use a link script.
438 const char* name
= (relink
? "relink.txt" : "link.txt");
439 this->CreateLinkScript(name
, real_link_commands
, commands1
);
443 // No link script. Just use the link rule directly.
444 commands1
= real_link_commands
;
446 this->LocalGenerator
->CreateCDCommand
448 this->Makefile
->GetStartOutputDirectory(),
449 this->Makefile
->GetHomeOutputDirectory());
450 commands
.insert(commands
.end(), commands1
.begin(), commands1
.end());
453 // Add a rule to create necessary symlinks for the library.
454 if(targetOutPath
!= targetOutPathReal
)
456 std::string symlink
= "$(CMAKE_COMMAND) -E cmake_symlink_executable ";
457 symlink
+= targetOutPathReal
;
459 symlink
+= targetOutPath
;
460 commands1
.push_back(symlink
);
461 this->LocalGenerator
->CreateCDCommand(commands1
,
462 this->Makefile
->GetStartOutputDirectory(),
463 this->Makefile
->GetHomeOutputDirectory());
464 commands
.insert(commands
.end(), commands1
.begin(), commands1
.end());
468 // Add the post-build rules when building but not when relinking.
471 this->LocalGenerator
->
472 AppendCustomCommands(commands
, this->Target
->GetPostBuildCommands());
475 // Write the build rule.
476 this->LocalGenerator
->WriteMakeRule(*this->BuildFileStream
,
478 targetFullPathReal
.c_str(),
479 depends
, commands
, false);
481 // The symlink name for the target should depend on the real target
482 // so if the target version changes it rebuilds and recreates the
484 if(targetFullPath
!= targetFullPathReal
)
488 depends
.push_back(targetFullPathReal
.c_str());
489 this->LocalGenerator
->WriteMakeRule(*this->BuildFileStream
, 0,
490 targetFullPath
.c_str(),
491 depends
, commands
, false);
494 // Write the main driver rule to build everything in this target.
495 this->WriteTargetDriverRule(targetFullPath
.c_str(), relink
);
497 // Clean all the possible executable names and symlinks.
498 this->CleanFiles
.insert(this->CleanFiles
.end(),
499 exeCleanFiles
.begin(),
500 exeCleanFiles
.end());