1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmMakefileExecutableTargetGenerator.cxx,v $
6 Date: $Date: 2008/02/14 20:31:08 $
7 Version: $Revision: 1.42 $
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
136 std::vector
<cmSourceFile
*>::const_iterator sourceIt
;
137 for ( sourceIt
= this->Target
->GetSourceFiles().begin();
138 sourceIt
!= this->Target
->GetSourceFiles().end();
142 (*sourceIt
)->GetProperty("MACOSX_PACKAGE_LOCATION");
145 std::string newDir
= macdir
;
147 if ( !cmSystemTools::MakeDirectory(newDir
.c_str()) )
149 cmSystemTools::Error("Cannot create a subdirectory for \"",
150 newDir
.c_str(), "\".");
156 // Configure the Info.plist file. Note that it needs the executable name
158 std::string plist
= macdir
+ "Info.plist";
159 this->LocalGenerator
->GenerateAppleInfoPList(this->Target
,
164 std::string outpathImp
;
167 outpath
= this->Makefile
->GetStartOutputDirectory();
168 outpath
+= cmake::GetCMakeFilesDirectory();
169 outpath
+= "/CMakeRelink.dir";
170 cmSystemTools::MakeDirectory(outpath
.c_str());
172 if(!targetNameImport
.empty())
174 outpathImp
= outpath
;
179 if(!targetNameImport
.empty())
181 outpathImp
= this->Target
->GetDirectory(0, true);
185 std::string targetFullPath
= outpath
+ targetName
;
186 std::string targetFullPathReal
= outpath
+ targetNameReal
;
187 std::string targetFullPathPDB
= outpath
+ targetNamePDB
;
188 std::string targetFullPathImport
= outpathImp
+ targetNameImport
;
189 std::string targetOutPathPDB
=
190 this->Convert(targetFullPathPDB
.c_str(),
191 cmLocalGenerator::FULL
,
192 cmLocalGenerator::SHELL
);
193 // Convert to the output path to use in constructing commands.
194 std::string targetOutPath
=
195 this->Convert(targetFullPath
.c_str(),
196 cmLocalGenerator::START_OUTPUT
,
197 cmLocalGenerator::SHELL
);
198 std::string targetOutPathReal
=
199 this->Convert(targetFullPathReal
.c_str(),
200 cmLocalGenerator::START_OUTPUT
,
201 cmLocalGenerator::SHELL
);
202 std::string targetOutPathImport
=
203 this->Convert(targetFullPathImport
.c_str(),
204 cmLocalGenerator::START_OUTPUT
,
205 cmLocalGenerator::SHELL
);
207 // Get the language to use for linking this executable.
208 const char* linkLanguage
=
209 this->Target
->GetLinkerLanguage(this->GlobalGenerator
);
211 // Make sure we have a link language.
214 cmSystemTools::Error("Cannot determine link language for target \"",
215 this->Target
->GetName(), "\".");
219 // Add the link message.
220 std::string buildEcho
= "Linking ";
221 buildEcho
+= linkLanguage
;
222 buildEcho
+= " executable ";
223 buildEcho
+= targetOutPath
;
224 this->LocalGenerator
->AppendEcho(commands
, buildEcho
.c_str(),
225 cmLocalUnixMakefileGenerator3::EchoLink
);
227 // Build a list of compiler flags and linker flags.
229 std::string linkFlags
;
231 // Add flags to deal with shared libraries. Any library being
232 // linked in might be shared, so always use shared flags for an
234 this->LocalGenerator
->AddSharedFlags(linkFlags
, linkLanguage
, true);
236 // Add flags to create an executable.
237 this->LocalGenerator
->
238 AddConfigVariableFlags(linkFlags
, "CMAKE_EXE_LINKER_FLAGS",
239 this->LocalGenerator
->ConfigurationName
.c_str());
242 if(this->Target
->GetPropertyAsBool("WIN32_EXECUTABLE"))
244 this->LocalGenerator
->AppendFlags
245 (linkFlags
, this->Makefile
->GetDefinition("CMAKE_CREATE_WIN32_EXE"));
249 this->LocalGenerator
->AppendFlags
250 (linkFlags
, this->Makefile
->GetDefinition("CMAKE_CREATE_CONSOLE_EXE"));
253 // Add symbol export flags if necessary.
254 if(this->Target
->IsExecutableWithExports())
256 std::string export_flag_var
= "CMAKE_EXE_EXPORTS_";
257 export_flag_var
+= linkLanguage
;
258 export_flag_var
+= "_FLAG";
259 this->LocalGenerator
->AppendFlags
260 (linkFlags
, this->Makefile
->GetDefinition(export_flag_var
.c_str()));
263 // Add language-specific flags.
265 ->AddLanguageFlags(flags
, linkLanguage
,
266 this->LocalGenerator
->ConfigurationName
.c_str());
268 // Add target-specific linker flags.
269 this->LocalGenerator
->AppendFlags
270 (linkFlags
, this->Target
->GetProperty("LINK_FLAGS"));
271 std::string linkFlagsConfig
= "LINK_FLAGS_";
273 cmSystemTools::UpperCase(this->LocalGenerator
->ConfigurationName
.c_str());
274 this->LocalGenerator
->AppendFlags
275 (linkFlags
, this->Target
->GetProperty(linkFlagsConfig
.c_str()));
277 // Construct a list of files associated with this executable that
278 // may need to be cleaned.
279 std::vector
<std::string
> exeCleanFiles
;
281 std::string cleanName
;
282 std::string cleanRealName
;
283 std::string cleanImportName
;
284 std::string cleanPDBName
;
285 this->Target
->GetExecutableCleanNames
286 (cleanName
, cleanRealName
, cleanImportName
, cleanPDBName
,
287 this->LocalGenerator
->ConfigurationName
.c_str());
289 std::string cleanFullName
= outpath
+ cleanName
;
290 std::string cleanFullRealName
= outpath
+ cleanRealName
;
291 std::string cleanFullPDBName
= outpath
+ cleanPDBName
;
292 std::string cleanFullImportName
= outpathImp
+ cleanImportName
;
293 exeCleanFiles
.push_back(this->Convert(cleanFullName
.c_str(),
294 cmLocalGenerator::START_OUTPUT
,
295 cmLocalGenerator::UNCHANGED
));
297 // There may be a manifest file for this target. Add it to the
298 // clean set just in case.
299 exeCleanFiles
.push_back(this->Convert((cleanFullName
+".manifest").c_str(),
300 cmLocalGenerator::START_OUTPUT
,
301 cmLocalGenerator::UNCHANGED
));
303 if(cleanRealName
!= cleanName
)
305 exeCleanFiles
.push_back(this->Convert(cleanFullRealName
.c_str(),
306 cmLocalGenerator::START_OUTPUT
,
307 cmLocalGenerator::UNCHANGED
));
309 if(!cleanImportName
.empty())
311 exeCleanFiles
.push_back(this->Convert(cleanFullImportName
.c_str(),
312 cmLocalGenerator::START_OUTPUT
,
313 cmLocalGenerator::UNCHANGED
));
316 // List the PDB for cleaning only when the whole target is
317 // cleaned. We do not want to delete the .pdb file just before
318 // linking the target.
319 this->CleanFiles
.push_back
320 (this->Convert(cleanFullPDBName
.c_str(),
321 cmLocalGenerator::START_OUTPUT
,
322 cmLocalGenerator::UNCHANGED
));
325 // Add the pre-build and pre-link rules building but not when relinking.
329 ->AppendCustomCommands(commands
, this->Target
->GetPreBuildCommands());
331 ->AppendCustomCommands(commands
, this->Target
->GetPreLinkCommands());
334 // Determine whether a link script will be used.
335 bool useLinkScript
= this->GlobalGenerator
->GetUseLinkScript();
337 // Construct the main link rule.
338 std::vector
<std::string
> real_link_commands
;
339 std::string linkRuleVar
= "CMAKE_";
340 linkRuleVar
+= linkLanguage
;
341 linkRuleVar
+= "_LINK_EXECUTABLE";
342 std::string linkRule
=
343 this->Makefile
->GetRequiredDefinition(linkRuleVar
.c_str());
344 std::vector
<std::string
> commands1
;
345 cmSystemTools::ExpandListArgument(linkRule
, real_link_commands
);
346 if(this->Target
->IsExecutableWithExports())
348 // If a separate rule for creating an import library is specified
350 std::string implibRuleVar
= "CMAKE_";
351 implibRuleVar
+= linkLanguage
;
352 implibRuleVar
+= "_CREATE_IMPORT_LIBRARY";
353 if(const char* rule
=
354 this->Makefile
->GetDefinition(implibRuleVar
.c_str()))
356 cmSystemTools::ExpandListArgument(rule
, real_link_commands
);
360 // Expand the rule variables.
362 // Set path conversion for link script shells.
363 this->LocalGenerator
->SetLinkScriptShell(useLinkScript
);
365 // Collect up flags to link in needed libraries.
366 cmOStringStream linklibs
;
367 this->LocalGenerator
->OutputLinkLibraries(linklibs
, *this->Target
, relink
);
369 // Construct object file lists that may be needed to expand the
371 std::string variableName
;
372 std::string variableNameExternal
;
373 this->WriteObjectsVariable(variableName
, variableNameExternal
);
374 std::string buildObjs
;
377 this->WriteObjectsString(buildObjs
);
382 buildObjs
+= variableName
;
384 buildObjs
+= variableNameExternal
;
387 std::string cleanObjs
= "$(";
388 cleanObjs
+= variableName
;
391 cmLocalGenerator::RuleVariables vars
;
392 vars
.Language
= linkLanguage
;
393 vars
.Objects
= buildObjs
.c_str();
394 vars
.Target
= targetOutPathReal
.c_str();
395 vars
.TargetPDB
= targetOutPathPDB
.c_str();
397 // Setup the target version.
398 std::string targetVersionMajor
;
399 std::string targetVersionMinor
;
401 cmOStringStream majorStream
;
402 cmOStringStream minorStream
;
405 this->Target
->GetTargetVersion(major
, minor
);
406 majorStream
<< major
;
407 minorStream
<< minor
;
408 targetVersionMajor
= majorStream
.str();
409 targetVersionMinor
= minorStream
.str();
411 vars
.TargetVersionMajor
= targetVersionMajor
.c_str();
412 vars
.TargetVersionMinor
= targetVersionMinor
.c_str();
414 std::string linkString
= linklibs
.str();
415 vars
.LinkLibraries
= linkString
.c_str();
416 vars
.Flags
= flags
.c_str();
417 vars
.LinkFlags
= linkFlags
.c_str();
418 // Expand placeholders in the commands.
419 this->LocalGenerator
->TargetImplib
= targetOutPathImport
;
420 for(std::vector
<std::string
>::iterator i
= real_link_commands
.begin();
421 i
!= real_link_commands
.end(); ++i
)
423 this->LocalGenerator
->ExpandRuleVariables(*i
, vars
);
425 this->LocalGenerator
->TargetImplib
= "";
427 // Restore path conversion to normal shells.
428 this->LocalGenerator
->SetLinkScriptShell(false);
431 // Optionally convert the build rule to use a script to avoid long
432 // command lines in the make shell.
435 // Use a link script.
436 const char* name
= (relink
? "relink.txt" : "link.txt");
437 this->CreateLinkScript(name
, real_link_commands
, commands1
);
441 // No link script. Just use the link rule directly.
442 commands1
= real_link_commands
;
444 this->LocalGenerator
->CreateCDCommand
446 this->Makefile
->GetStartOutputDirectory(),
447 this->Makefile
->GetHomeOutputDirectory());
448 commands
.insert(commands
.end(), commands1
.begin(), commands1
.end());
451 // Add a rule to create necessary symlinks for the library.
452 if(targetOutPath
!= targetOutPathReal
)
454 std::string symlink
= "$(CMAKE_COMMAND) -E cmake_symlink_executable ";
455 symlink
+= targetOutPathReal
;
457 symlink
+= targetOutPath
;
458 commands1
.push_back(symlink
);
459 this->LocalGenerator
->CreateCDCommand(commands1
,
460 this->Makefile
->GetStartOutputDirectory(),
461 this->Makefile
->GetHomeOutputDirectory());
462 commands
.insert(commands
.end(), commands1
.begin(), commands1
.end());
466 // Add the post-build rules when building but not when relinking.
469 this->LocalGenerator
->
470 AppendCustomCommands(commands
, this->Target
->GetPostBuildCommands());
473 // Write the build rule.
474 this->LocalGenerator
->WriteMakeRule(*this->BuildFileStream
,
476 targetFullPathReal
.c_str(),
477 depends
, commands
, false);
479 // The symlink name for the target should depend on the real target
480 // so if the target version changes it rebuilds and recreates the
482 if(targetFullPath
!= targetFullPathReal
)
486 depends
.push_back(targetFullPathReal
.c_str());
487 this->LocalGenerator
->WriteMakeRule(*this->BuildFileStream
, 0,
488 targetFullPath
.c_str(),
489 depends
, commands
, false);
492 // Write the main driver rule to build everything in this target.
493 this->WriteTargetDriverRule(targetFullPath
.c_str(), relink
);
495 // Clean all the possible executable names and symlinks.
496 this->CleanFiles
.insert(this->CleanFiles
.end(),
497 exeCleanFiles
.begin(),
498 exeCleanFiles
.end());