1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmMakefileLibraryTargetGenerator.cxx,v $
6 Date: $Date: 2009-05-01 14:38:28 $
7 Version: $Revision: 1.72 $
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 "cmMakefileLibraryTargetGenerator.h"
19 #include "cmGeneratedFileStream.h"
20 #include "cmGlobalUnixMakefileGenerator3.h"
21 #include "cmLocalUnixMakefileGenerator3.h"
22 #include "cmMakefile.h"
23 #include "cmSourceFile.h"
27 #include <memory> // auto_ptr
29 //----------------------------------------------------------------------------
30 cmMakefileLibraryTargetGenerator
31 ::cmMakefileLibraryTargetGenerator(cmTarget
* target
):
32 cmMakefileTargetGenerator(target
)
34 this->CustomCommandDriver
= OnDepends
;
35 this->Target
->GetLibraryNames(
36 this->TargetNameOut
, this->TargetNameSO
, this->TargetNameReal
,
37 this->TargetNameImport
, this->TargetNamePDB
,
38 this->LocalGenerator
->ConfigurationName
.c_str());
40 if(this->Target
->IsFrameworkOnApple())
42 this->FrameworkVersion
= this->Target
->GetFrameworkVersion();
43 this->MacContentDirectory
= this->Target
->GetDirectory();
44 this->MacContentDirectory
+= "/";
45 this->MacContentDirectory
+= this->TargetNameOut
;
46 this->MacContentDirectory
+= ".framework/Versions/";
47 this->MacContentDirectory
+= this->FrameworkVersion
;
48 this->MacContentDirectory
+= "/";
52 //----------------------------------------------------------------------------
53 void cmMakefileLibraryTargetGenerator::WriteRuleFiles()
55 // create the build.make file and directory, put in the common blocks
56 this->CreateRuleFile();
58 // write rules used to help build object files
59 this->WriteCommonCodeRules();
61 // write in rules for object files and custom commands
62 this->WriteTargetBuildRules();
64 // write the per-target per-language flags
65 this->WriteTargetLanguageFlags();
67 // write the link rules
68 // Write the rule for this target type.
69 switch(this->Target
->GetType())
71 case cmTarget::STATIC_LIBRARY
:
72 this->WriteStaticLibraryRules();
74 case cmTarget::SHARED_LIBRARY
:
75 this->WriteSharedLibraryRules(false);
76 if(this->Target
->NeedRelinkBeforeInstall())
78 // Write rules to link an installable version of the target.
79 this->WriteSharedLibraryRules(true);
82 case cmTarget::MODULE_LIBRARY
:
83 this->WriteModuleLibraryRules(false);
84 if(this->Target
->NeedRelinkBeforeInstall())
86 // Write rules to link an installable version of the target.
87 this->WriteModuleLibraryRules(true);
91 // If language is not known, this is an error.
92 cmSystemTools::Error("Unknown Library Type");
96 // Write the requires target.
97 this->WriteTargetRequiresRules();
100 this->WriteTargetCleanRules();
102 // Write the dependency generation rule. This must be done last so
103 // that multiple output pair information is available.
104 this->WriteTargetDependRules();
107 this->CloseFileStreams();
110 //----------------------------------------------------------------------------
111 void cmMakefileLibraryTargetGenerator::WriteStaticLibraryRules()
113 const char* linkLanguage
=
114 this->Target
->GetLinkerLanguage(this->GlobalGenerator
);
115 std::string linkRuleVar
= "CMAKE_";
118 linkRuleVar
+= linkLanguage
;
120 linkRuleVar
+= "_CREATE_STATIC_LIBRARY";
122 std::string extraFlags
;
123 this->LocalGenerator
->AppendFlags
124 (extraFlags
,this->Target
->GetProperty("STATIC_LIBRARY_FLAGS"));
125 this->WriteLibraryRules(linkRuleVar
.c_str(), extraFlags
.c_str(), false);
128 //----------------------------------------------------------------------------
129 void cmMakefileLibraryTargetGenerator::WriteSharedLibraryRules(bool relink
)
131 if(this->Target
->IsFrameworkOnApple())
133 this->WriteFrameworkRules(relink
);
136 const char* linkLanguage
=
137 this->Target
->GetLinkerLanguage(this->GlobalGenerator
);
138 std::string linkRuleVar
= "CMAKE_";
141 linkRuleVar
+= linkLanguage
;
143 linkRuleVar
+= "_CREATE_SHARED_LIBRARY";
145 std::string extraFlags
;
146 this->LocalGenerator
->AppendFlags
147 (extraFlags
, this->Target
->GetProperty("LINK_FLAGS"));
148 std::string linkFlagsConfig
= "LINK_FLAGS_";
150 cmSystemTools::UpperCase(this->LocalGenerator
->ConfigurationName
.c_str());
151 this->LocalGenerator
->AppendFlags
152 (extraFlags
, this->Target
->GetProperty(linkFlagsConfig
.c_str()));
154 this->LocalGenerator
->AddConfigVariableFlags
155 (extraFlags
, "CMAKE_SHARED_LINKER_FLAGS",
156 this->LocalGenerator
->ConfigurationName
.c_str());
157 if(this->Makefile
->IsOn("WIN32") && !(this->Makefile
->IsOn("CYGWIN")
158 || this->Makefile
->IsOn("MINGW")))
160 const std::vector
<cmSourceFile
*>& sources
=
161 this->Target
->GetSourceFiles();
162 for(std::vector
<cmSourceFile
*>::const_iterator i
= sources
.begin();
163 i
!= sources
.end(); ++i
)
165 cmSourceFile
* sf
= *i
;
166 if(sf
->GetExtension() == "def")
170 this->Makefile
->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
172 this->Convert(sf
->GetFullPath().c_str(),
173 cmLocalGenerator::START_OUTPUT
,
174 cmLocalGenerator::SHELL
);
178 this->WriteLibraryRules(linkRuleVar
.c_str(), extraFlags
.c_str(), relink
);
181 //----------------------------------------------------------------------------
182 void cmMakefileLibraryTargetGenerator::WriteModuleLibraryRules(bool relink
)
184 const char* linkLanguage
=
185 this->Target
->GetLinkerLanguage(this->GlobalGenerator
);
186 std::string linkRuleVar
= "CMAKE_";
189 linkRuleVar
+= linkLanguage
;
191 linkRuleVar
+= "_CREATE_SHARED_MODULE";
193 std::string extraFlags
;
194 this->LocalGenerator
->AppendFlags(extraFlags
,
195 this->Target
->GetProperty("LINK_FLAGS"));
196 std::string linkFlagsConfig
= "LINK_FLAGS_";
198 cmSystemTools::UpperCase(this->LocalGenerator
->ConfigurationName
.c_str());
199 this->LocalGenerator
->AppendFlags
200 (extraFlags
, this->Target
->GetProperty(linkFlagsConfig
.c_str()));
201 this->LocalGenerator
->AddConfigVariableFlags
202 (extraFlags
, "CMAKE_MODULE_LINKER_FLAGS",
203 this->LocalGenerator
->ConfigurationName
.c_str());
205 // TODO: .def files should be supported here also.
206 this->WriteLibraryRules(linkRuleVar
.c_str(), extraFlags
.c_str(), relink
);
209 //----------------------------------------------------------------------------
210 void cmMakefileLibraryTargetGenerator::WriteFrameworkRules(bool relink
)
212 const char* linkLanguage
=
213 this->Target
->GetLinkerLanguage(this->GlobalGenerator
);
214 std::string linkRuleVar
= "CMAKE_";
217 linkRuleVar
+= linkLanguage
;
219 linkRuleVar
+= "_CREATE_MACOSX_FRAMEWORK";
221 std::string extraFlags
;
222 this->LocalGenerator
->AppendFlags(extraFlags
,
223 this->Target
->GetProperty("LINK_FLAGS"));
224 std::string linkFlagsConfig
= "LINK_FLAGS_";
226 cmSystemTools::UpperCase(this->LocalGenerator
->ConfigurationName
.c_str());
227 this->LocalGenerator
->AppendFlags
228 (extraFlags
, this->Target
->GetProperty(linkFlagsConfig
.c_str()));
229 this->LocalGenerator
->AddConfigVariableFlags
230 (extraFlags
, "CMAKE_MACOSX_FRAMEWORK_LINKER_FLAGS",
231 this->LocalGenerator
->ConfigurationName
.c_str());
233 // TODO: .def files should be supported here also.
234 this->WriteLibraryRules(linkRuleVar
.c_str(), extraFlags
.c_str(), relink
);
237 //----------------------------------------------------------------------------
239 cmMakefileLibraryTargetGenerator
240 ::CreateFramework(std::string
const& targetName
)
242 // Configure the Info.plist file into the Resources directory.
243 this->MacContentFolders
.insert("Resources");
244 std::string plist
= this->MacContentDirectory
+ "Resources/Info.plist";
245 this->LocalGenerator
->GenerateFrameworkInfoPList(this->Target
,
249 // TODO: Use the cmMakefileTargetGenerator::ExtraFiles vector to
250 // drive rules to create these files at build time.
254 // Compute the location of the top-level foo.framework directory.
255 std::string top
= this->Target
->GetDirectory();
257 top
+= this->TargetNameOut
;
258 top
+= ".framework/";
260 // Make foo.framework/Versions
261 std::string versions
= top
;
262 versions
+= "Versions";
263 cmSystemTools::MakeDirectory(versions
.c_str());
265 // Make foo.framework/Versions/version
266 std::string version
= versions
;
268 version
+= this->FrameworkVersion
;
269 cmSystemTools::MakeDirectory(version
.c_str());
271 // Current -> version
272 oldName
= this->FrameworkVersion
;
274 newName
+= "/Current";
275 cmSystemTools::RemoveFile(newName
.c_str());
276 cmSystemTools::CreateSymlink(oldName
.c_str(), newName
.c_str());
277 this->Makefile
->AddCMakeOutputFile(newName
.c_str());
279 // foo -> Versions/Current/foo
280 oldName
= "Versions/Current/";
281 oldName
+= this->TargetNameOut
;
283 newName
+= this->TargetNameOut
;
284 cmSystemTools::RemoveFile(newName
.c_str());
285 cmSystemTools::CreateSymlink(oldName
.c_str(), newName
.c_str());
286 this->Makefile
->AddCMakeOutputFile(newName
.c_str());
288 // Resources -> Versions/Current/Resources
289 if(this->MacContentFolders
.find("Resources") !=
290 this->MacContentFolders
.end())
292 oldName
= "Versions/Current/Resources";
294 newName
+= "Resources";
295 cmSystemTools::RemoveFile(newName
.c_str());
296 cmSystemTools::CreateSymlink(oldName
.c_str(), newName
.c_str());
297 this->Makefile
->AddCMakeOutputFile(newName
.c_str());
300 // Headers -> Versions/Current/Headers
301 if(this->MacContentFolders
.find("Headers") !=
302 this->MacContentFolders
.end())
304 oldName
= "Versions/Current/Headers";
306 newName
+= "Headers";
307 cmSystemTools::RemoveFile(newName
.c_str());
308 cmSystemTools::CreateSymlink(oldName
.c_str(), newName
.c_str());
309 this->Makefile
->AddCMakeOutputFile(newName
.c_str());
312 // PrivateHeaders -> Versions/Current/PrivateHeaders
313 if(this->MacContentFolders
.find("PrivateHeaders") !=
314 this->MacContentFolders
.end())
316 oldName
= "Versions/Current/PrivateHeaders";
318 newName
+= "PrivateHeaders";
319 cmSystemTools::RemoveFile(newName
.c_str());
320 cmSystemTools::CreateSymlink(oldName
.c_str(), newName
.c_str());
321 this->Makefile
->AddCMakeOutputFile(newName
.c_str());
325 //----------------------------------------------------------------------------
326 void cmMakefileLibraryTargetGenerator::WriteLibraryRules
327 (const char* linkRuleVar
, const char* extraFlags
, bool relink
)
329 // TODO: Merge the methods that call this method to avoid
331 std::vector
<std::string
> commands
;
333 std::string relPath
= this->LocalGenerator
->GetHomeRelativeOutputPath();
334 std::string objTarget
;
336 // Build list of dependencies.
337 std::vector
<std::string
> depends
;
338 for(std::vector
<std::string
>::const_iterator obj
= this->Objects
.begin();
339 obj
!= this->Objects
.end(); ++obj
)
343 depends
.push_back(objTarget
);
346 // Add dependencies on targets that must be built first.
347 this->AppendTargetDepends(depends
);
349 // Add a dependency on the rule file itself.
350 this->LocalGenerator
->AppendRuleDepend(depends
,
351 this->BuildFileNameFull
.c_str());
353 for(std::vector
<std::string
>::const_iterator obj
354 = this->ExternalObjects
.begin();
355 obj
!= this->ExternalObjects
.end(); ++obj
)
357 depends
.push_back(*obj
);
360 // Get the language to use for linking this library.
361 const char* linkLanguage
=
362 this->Target
->GetLinkerLanguage(this->GlobalGenerator
);
364 // Make sure we have a link language.
367 cmSystemTools::Error("Cannot determine link language for target \"",
368 this->Target
->GetName(), "\".");
372 // Create set of linking flags.
373 std::string linkFlags
;
374 this->LocalGenerator
->AppendFlags(linkFlags
, extraFlags
);
376 // Add OSX version flags, if any.
377 if(this->Target
->GetType() == cmTarget::SHARED_LIBRARY
||
378 this->Target
->GetType() == cmTarget::MODULE_LIBRARY
)
380 this->AppendOSXVerFlag(linkFlags
, linkLanguage
, "COMPATIBILITY", true);
381 this->AppendOSXVerFlag(linkFlags
, linkLanguage
, "CURRENT", false);
384 // Construct the name of the library.
385 std::string targetName
;
386 std::string targetNameSO
;
387 std::string targetNameReal
;
388 std::string targetNameImport
;
389 std::string targetNamePDB
;
390 this->Target
->GetLibraryNames(
391 targetName
, targetNameSO
, targetNameReal
, targetNameImport
, targetNamePDB
,
392 this->LocalGenerator
->ConfigurationName
.c_str());
394 // Construct the full path version of the names.
396 std::string outpathImp
;
397 if(this->Target
->IsFrameworkOnApple())
399 outpath
= this->MacContentDirectory
;
400 this->CreateFramework(targetName
);
404 outpath
= this->Makefile
->GetStartOutputDirectory();
405 outpath
+= cmake::GetCMakeFilesDirectory();
406 outpath
+= "/CMakeRelink.dir";
407 cmSystemTools::MakeDirectory(outpath
.c_str());
409 if(!targetNameImport
.empty())
411 outpathImp
= outpath
;
416 outpath
= this->Target
->GetDirectory();
417 cmSystemTools::MakeDirectory(outpath
.c_str());
419 if(!targetNameImport
.empty())
421 outpathImp
= this->Target
->GetDirectory(0, true);
422 cmSystemTools::MakeDirectory(outpathImp
.c_str());
427 std::string targetFullPath
= outpath
+ targetName
;
428 std::string targetFullPathPDB
= outpath
+ targetNamePDB
;
429 std::string targetFullPathSO
= outpath
+ targetNameSO
;
430 std::string targetFullPathReal
= outpath
+ targetNameReal
;
431 std::string targetFullPathImport
= outpathImp
+ targetNameImport
;
433 // Construct the output path version of the names for use in command
435 std::string targetOutPathPDB
=
436 this->Convert(targetFullPathPDB
.c_str(),cmLocalGenerator::FULL
,
437 cmLocalGenerator::SHELL
);
438 std::string targetOutPath
=
439 this->Convert(targetFullPath
.c_str(),cmLocalGenerator::START_OUTPUT
,
440 cmLocalGenerator::SHELL
);
441 std::string targetOutPathSO
=
442 this->Convert(targetFullPathSO
.c_str(),cmLocalGenerator::START_OUTPUT
,
443 cmLocalGenerator::SHELL
);
444 std::string targetOutPathReal
=
445 this->Convert(targetFullPathReal
.c_str(),cmLocalGenerator::START_OUTPUT
,
446 cmLocalGenerator::SHELL
);
447 std::string targetOutPathImport
=
448 this->Convert(targetFullPathImport
.c_str(),cmLocalGenerator::START_OUTPUT
,
449 cmLocalGenerator::SHELL
);
451 if(!this->NoRuleMessages
)
453 // Add the link message.
454 std::string buildEcho
= "Linking ";
455 buildEcho
+= linkLanguage
;
456 switch(this->Target
->GetType())
458 case cmTarget::STATIC_LIBRARY
:
459 buildEcho
+= " static library ";
461 case cmTarget::SHARED_LIBRARY
:
462 buildEcho
+= " shared library ";
464 case cmTarget::MODULE_LIBRARY
:
465 buildEcho
+= " shared module ";
468 buildEcho
+= " library ";
471 buildEcho
+= targetOutPath
.c_str();
472 this->LocalGenerator
->AppendEcho(commands
, buildEcho
.c_str(),
473 cmLocalUnixMakefileGenerator3::EchoLink
);
476 const char* forbiddenFlagVar
= 0;
477 switch(this->Target
->GetType())
479 case cmTarget::SHARED_LIBRARY
:
480 forbiddenFlagVar
= "_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS";
482 case cmTarget::MODULE_LIBRARY
:
483 forbiddenFlagVar
= "_CREATE_SHARED_MODULE_FORBIDDEN_FLAGS";
488 // Clean files associated with this library.
489 std::vector
<std::string
> libCleanFiles
;
490 libCleanFiles
.push_back(this->Convert(targetFullPath
.c_str(),
491 cmLocalGenerator::START_OUTPUT
,
492 cmLocalGenerator::UNCHANGED
));
493 if(targetNameReal
!= targetName
)
495 libCleanFiles
.push_back(this->Convert(targetFullPathReal
.c_str(),
496 cmLocalGenerator::START_OUTPUT
,
497 cmLocalGenerator::UNCHANGED
));
499 if(targetNameSO
!= targetName
&&
500 targetNameSO
!= targetNameReal
)
502 libCleanFiles
.push_back(this->Convert(targetFullPathSO
.c_str(),
503 cmLocalGenerator::START_OUTPUT
,
504 cmLocalGenerator::UNCHANGED
));
506 if(!targetNameImport
.empty())
508 libCleanFiles
.push_back(this->Convert(targetFullPathImport
.c_str(),
509 cmLocalGenerator::START_OUTPUT
,
510 cmLocalGenerator::UNCHANGED
));
513 // List the PDB for cleaning only when the whole target is
514 // cleaned. We do not want to delete the .pdb file just before
515 // linking the target.
516 this->CleanFiles
.push_back
517 (this->Convert(targetFullPathPDB
.c_str(),
518 cmLocalGenerator::START_OUTPUT
,
519 cmLocalGenerator::UNCHANGED
));
522 // There may be a manifest file for this target. Add it to the
523 // clean set just in case.
524 if(this->Target
->GetType() != cmTarget::STATIC_LIBRARY
)
526 libCleanFiles
.push_back(
527 this->Convert((targetFullPath
+".manifest").c_str(),
528 cmLocalGenerator::START_OUTPUT
,
529 cmLocalGenerator::UNCHANGED
));
533 std::vector
<std::string
> commands1
;
534 // Add a command to remove any existing files for this library.
535 // for static libs only
536 if(this->Target
->GetType() == cmTarget::STATIC_LIBRARY
)
538 this->LocalGenerator
->AppendCleanCommand(commands1
, libCleanFiles
,
539 *this->Target
, "target");
540 this->LocalGenerator
->CreateCDCommand
542 this->Makefile
->GetStartOutputDirectory(),
543 cmLocalGenerator::HOME_OUTPUT
);
544 commands
.insert(commands
.end(), commands1
.begin(), commands1
.end());
548 // Add the pre-build and pre-link rules building but not when relinking.
552 ->AppendCustomCommands(commands
, this->Target
->GetPreBuildCommands(),
555 ->AppendCustomCommands(commands
, this->Target
->GetPreLinkCommands(),
559 // Determine whether a link script will be used.
560 bool useLinkScript
= this->GlobalGenerator
->GetUseLinkScript();
562 // Select whether to use a response file for objects.
563 bool useResponseFile
= false;
565 std::string responseVar
= "CMAKE_";
566 responseVar
+= linkLanguage
;
567 responseVar
+= "_USE_RESPONSE_FILE_FOR_OBJECTS";
568 if(this->Makefile
->IsOn(responseVar
.c_str()))
570 useResponseFile
= true;
574 // For static libraries there might be archiving rules.
575 bool haveStaticLibraryRule
= false;
576 std::vector
<std::string
> archiveCreateCommands
;
577 std::vector
<std::string
> archiveAppendCommands
;
578 std::vector
<std::string
> archiveFinishCommands
;
579 std::string::size_type archiveCommandLimit
= std::string::npos
;
580 if(this->Target
->GetType() == cmTarget::STATIC_LIBRARY
)
582 haveStaticLibraryRule
=
583 this->Makefile
->GetDefinition(linkRuleVar
)? true:false;
584 std::string arCreateVar
= "CMAKE_";
585 arCreateVar
+= linkLanguage
;
586 arCreateVar
+= "_ARCHIVE_CREATE";
587 if(const char* rule
= this->Makefile
->GetDefinition(arCreateVar
.c_str()))
589 cmSystemTools::ExpandListArgument(rule
, archiveCreateCommands
);
591 std::string arAppendVar
= "CMAKE_";
592 arAppendVar
+= linkLanguage
;
593 arAppendVar
+= "_ARCHIVE_APPEND";
594 if(const char* rule
= this->Makefile
->GetDefinition(arAppendVar
.c_str()))
596 cmSystemTools::ExpandListArgument(rule
, archiveAppendCommands
);
598 std::string arFinishVar
= "CMAKE_";
599 arFinishVar
+= linkLanguage
;
600 arFinishVar
+= "_ARCHIVE_FINISH";
601 if(const char* rule
= this->Makefile
->GetDefinition(arFinishVar
.c_str()))
603 cmSystemTools::ExpandListArgument(rule
, archiveFinishCommands
);
607 // Decide whether to use archiving rules.
608 bool useArchiveRules
=
609 !haveStaticLibraryRule
&&
610 !archiveCreateCommands
.empty() && !archiveAppendCommands
.empty();
613 // Archiving rules are always run with a link script.
614 useLinkScript
= true;
616 // Archiving rules never use a response file.
617 useResponseFile
= false;
619 // Limit the length of individual object lists to less than the
620 // 32K command line length limit on Windows. We could make this a
621 // platform file variable but this should work everywhere.
622 archiveCommandLimit
= 30000;
625 // Expand the rule variables.
626 std::vector
<std::string
> real_link_commands
;
628 // Set path conversion for link script shells.
629 this->LocalGenerator
->SetLinkScriptShell(useLinkScript
);
631 // Collect up flags to link in needed libraries.
632 cmOStringStream linklibs
;
633 if(this->Target
->GetType() != cmTarget::STATIC_LIBRARY
)
636 ->OutputLinkLibraries(linklibs
, *this->Target
, relink
);
639 // Construct object file lists that may be needed to expand the
641 std::string buildObjs
;
642 this->CreateObjectLists(useLinkScript
, useArchiveRules
, useResponseFile
,
645 cmLocalGenerator::RuleVariables vars
;
646 vars
.TargetPDB
= targetOutPathPDB
.c_str();
648 // Setup the target version.
649 std::string targetVersionMajor
;
650 std::string targetVersionMinor
;
652 cmOStringStream majorStream
;
653 cmOStringStream minorStream
;
656 this->Target
->GetTargetVersion(major
, minor
);
657 majorStream
<< major
;
658 minorStream
<< minor
;
659 targetVersionMajor
= majorStream
.str();
660 targetVersionMinor
= minorStream
.str();
662 vars
.TargetVersionMajor
= targetVersionMajor
.c_str();
663 vars
.TargetVersionMinor
= targetVersionMinor
.c_str();
665 vars
.RuleLauncher
= "RULE_LAUNCH_LINK";
666 vars
.CMTarget
= this->Target
;
667 vars
.Language
= linkLanguage
;
668 vars
.Objects
= buildObjs
.c_str();
669 std::string objdir
= cmake::GetCMakeFilesDirectoryPostSlash();
670 objdir
+= this->Target
->GetName();
672 objdir
= this->Convert(objdir
.c_str(),
673 cmLocalGenerator::START_OUTPUT
,
674 cmLocalGenerator::SHELL
);
675 vars
.ObjectDir
= objdir
.c_str();
676 vars
.Target
= targetOutPathReal
.c_str();
677 std::string linkString
= linklibs
.str();
678 vars
.LinkLibraries
= linkString
.c_str();
679 vars
.ObjectsQuoted
= buildObjs
.c_str();
680 vars
.TargetSOName
= targetNameSO
.c_str();
681 vars
.LinkFlags
= linkFlags
.c_str();
683 // Compute the directory portion of the install_name setting.
684 std::string install_name_dir
;
685 if(this->Target
->GetType() == cmTarget::SHARED_LIBRARY
)
687 // Get the install_name directory for the build tree.
688 const char* config
= this->LocalGenerator
->ConfigurationName
.c_str();
689 install_name_dir
= this->Target
->GetInstallNameDirForBuildTree(config
);
691 // Set the rule variable replacement value.
692 if(install_name_dir
.empty())
694 vars
.TargetInstallNameDir
= "";
698 // Convert to a path for the native build tool.
700 this->LocalGenerator
->Convert(install_name_dir
.c_str(),
701 cmLocalGenerator::NONE
,
702 cmLocalGenerator::SHELL
, false);
703 vars
.TargetInstallNameDir
= install_name_dir
.c_str();
706 std::string langFlags
;
708 ->AddLanguageFlags(langFlags
, linkLanguage
,
709 this->LocalGenerator
->ConfigurationName
.c_str());
710 // remove any language flags that might not work with the
714 this->RemoveForbiddenFlags(forbiddenFlagVar
,
715 linkLanguage
, langFlags
);
717 vars
.LanguageCompileFlags
= langFlags
.c_str();
719 // Construct the main link rule and expand placeholders.
720 this->LocalGenerator
->TargetImplib
= targetOutPathImport
;
723 // Construct the individual object list strings.
724 std::vector
<std::string
> object_strings
;
725 this->WriteObjectsStrings(object_strings
, archiveCommandLimit
);
727 // Create the archive with the first set of objects.
728 std::vector
<std::string
>::iterator osi
= object_strings
.begin();
730 vars
.Objects
= osi
->c_str();
731 for(std::vector
<std::string
>::const_iterator
732 i
= archiveCreateCommands
.begin();
733 i
!= archiveCreateCommands
.end(); ++i
)
735 std::string cmd
= *i
;
736 this->LocalGenerator
->ExpandRuleVariables(cmd
, vars
);
737 real_link_commands
.push_back(cmd
);
740 // Append to the archive with the other object sets.
741 for(++osi
; osi
!= object_strings
.end(); ++osi
)
743 vars
.Objects
= osi
->c_str();
744 for(std::vector
<std::string
>::const_iterator
745 i
= archiveAppendCommands
.begin();
746 i
!= archiveAppendCommands
.end(); ++i
)
748 std::string cmd
= *i
;
749 this->LocalGenerator
->ExpandRuleVariables(cmd
, vars
);
750 real_link_commands
.push_back(cmd
);
753 // Finish the archive.
755 for(std::vector
<std::string
>::const_iterator
756 i
= archiveFinishCommands
.begin();
757 i
!= archiveFinishCommands
.end(); ++i
)
759 std::string cmd
= *i
;
760 this->LocalGenerator
->ExpandRuleVariables(cmd
, vars
);
761 real_link_commands
.push_back(cmd
);
766 // Get the set of commands.
767 std::string linkRule
= this->Makefile
->GetRequiredDefinition(linkRuleVar
);
768 cmSystemTools::ExpandListArgument(linkRule
, real_link_commands
);
770 // Expand placeholders.
771 for(std::vector
<std::string
>::iterator i
= real_link_commands
.begin();
772 i
!= real_link_commands
.end(); ++i
)
774 this->LocalGenerator
->ExpandRuleVariables(*i
, vars
);
777 this->LocalGenerator
->TargetImplib
= "";
779 // Restore path conversion to normal shells.
780 this->LocalGenerator
->SetLinkScriptShell(false);
783 // Optionally convert the build rule to use a script to avoid long
784 // command lines in the make shell.
787 // Use a link script.
788 const char* name
= (relink
? "relink.txt" : "link.txt");
789 this->CreateLinkScript(name
, real_link_commands
, commands1
, depends
);
793 // No link script. Just use the link rule directly.
794 commands1
= real_link_commands
;
796 this->LocalGenerator
->CreateCDCommand
798 this->Makefile
->GetStartOutputDirectory(),
799 cmLocalGenerator::HOME_OUTPUT
);
800 commands
.insert(commands
.end(), commands1
.begin(), commands1
.end());
803 // Add a rule to create necessary symlinks for the library.
804 if(targetOutPath
!= targetOutPathReal
)
806 std::string symlink
= "$(CMAKE_COMMAND) -E cmake_symlink_library ";
807 symlink
+= targetOutPathReal
;
809 symlink
+= targetOutPathSO
;
811 symlink
+= targetOutPath
;
812 commands1
.push_back(symlink
);
813 this->LocalGenerator
->CreateCDCommand(commands1
,
814 this->Makefile
->GetStartOutputDirectory(),
815 cmLocalGenerator::HOME_OUTPUT
);
816 commands
.insert(commands
.end(), commands1
.begin(), commands1
.end());
819 // Add the post-build rules when building but not when relinking.
822 this->LocalGenerator
->
823 AppendCustomCommands(commands
, this->Target
->GetPostBuildCommands(),
827 // Write the build rule.
828 this->LocalGenerator
->WriteMakeRule(*this->BuildFileStream
, 0,
829 targetFullPathReal
.c_str(),
830 depends
, commands
, false);
832 // Some targets have more than one output file. Create rules to
833 // drive the build if any extra outputs are missing.
834 std::vector
<std::string
> extraOutputs
;
835 if(targetNameSO
!= targetNameReal
)
837 this->GenerateExtraOutput(targetFullPathSO
.c_str(),
838 targetFullPathReal
.c_str());
840 if(targetName
!= targetNameSO
&&
841 targetName
!= targetNameReal
)
843 this->GenerateExtraOutput(targetFullPath
.c_str(),
844 targetFullPathReal
.c_str());
847 // Write the main driver rule to build everything in this target.
848 this->WriteTargetDriverRule(targetFullPath
.c_str(), relink
);
850 // Clean all the possible library names and symlinks.
851 this->CleanFiles
.insert(this->CleanFiles
.end(),
852 libCleanFiles
.begin(),libCleanFiles
.end());
855 //----------------------------------------------------------------------------
857 cmMakefileLibraryTargetGenerator
858 ::AppendOSXVerFlag(std::string
& flags
, const char* lang
,
859 const char* name
, bool so
)
861 // Lookup the flag to specify the version.
862 std::string fvar
= "CMAKE_";
866 fvar
+= "_VERSION_FLAG";
867 const char* flag
= this->Makefile
->GetDefinition(fvar
.c_str());
869 // Skip if no such flag.
875 // Lookup the target version information.
879 this->Target
->GetTargetVersion(so
, major
, minor
, patch
);
880 if(major
> 0 || minor
> 0 || patch
> 0)
882 // Append the flag since a non-zero version is specified.
883 cmOStringStream vflag
;
884 vflag
<< flag
<< major
<< "." << minor
<< "." << patch
;
885 this->LocalGenerator
->AppendFlags(flags
, vflag
.str().c_str());