Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmMakefileLibraryTargetGenerator.cxx
blob2de5ee5807dbcc229745c35fc7ca00bcb2a19fdf
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmMakefileLibraryTargetGenerator.cxx,v $
5 Language: C++
6 Date: $Date: 2009-03-16 20:55:58 $
7 Version: $Revision: 1.71 $
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"
24 #include "cmTarget.h"
25 #include "cmake.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();
73 break;
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);
81 break;
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);
89 break;
90 default:
91 // If language is not known, this is an error.
92 cmSystemTools::Error("Unknown Library Type");
93 break;
96 // Write the requires target.
97 this->WriteTargetRequiresRules();
99 // Write clean target
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();
106 // close the streams
107 this->CloseFileStreams();
110 //----------------------------------------------------------------------------
111 void cmMakefileLibraryTargetGenerator::WriteStaticLibraryRules()
113 const char* linkLanguage =
114 this->Target->GetLinkerLanguage(this->GlobalGenerator);
115 std::string linkRuleVar = "CMAKE_";
116 if (linkLanguage)
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);
134 return;
136 const char* linkLanguage =
137 this->Target->GetLinkerLanguage(this->GlobalGenerator);
138 std::string linkRuleVar = "CMAKE_";
139 if (linkLanguage)
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_";
149 linkFlagsConfig +=
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")
168 extraFlags += " ";
169 extraFlags +=
170 this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
171 extraFlags +=
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_";
187 if (linkLanguage)
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_";
197 linkFlagsConfig +=
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_";
215 if (linkLanguage)
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_";
225 linkFlagsConfig +=
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 //----------------------------------------------------------------------------
238 void
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,
246 targetName.c_str(),
247 plist.c_str());
249 // TODO: Use the cmMakefileTargetGenerator::ExtraFiles vector to
250 // drive rules to create these files at build time.
251 std::string oldName;
252 std::string newName;
254 // Compute the location of the top-level foo.framework directory.
255 std::string top = this->Target->GetDirectory();
256 top += "/";
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;
267 version += "/";
268 version += this->FrameworkVersion;
269 cmSystemTools::MakeDirectory(version.c_str());
271 // Current -> version
272 oldName = this->FrameworkVersion;
273 newName = versions;
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;
282 newName = top;
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";
293 newName = top;
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";
305 newName = top;
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";
317 newName = top;
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
330 // code duplication.
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)
341 objTarget = relPath;
342 objTarget += *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.
365 if(!linkLanguage)
367 cmSystemTools::Error("Cannot determine link language for target \"",
368 this->Target->GetName(), "\".");
369 return;
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.
395 std::string outpath;
396 std::string outpathImp;
397 if(this->Target->IsFrameworkOnApple())
399 outpath = this->MacContentDirectory;
400 this->CreateFramework(targetName);
402 else if(relink)
404 outpath = this->Makefile->GetStartOutputDirectory();
405 outpath += cmake::GetCMakeFilesDirectory();
406 outpath += "/CMakeRelink.dir";
407 cmSystemTools::MakeDirectory(outpath.c_str());
408 outpath += "/";
409 if(!targetNameImport.empty())
411 outpathImp = outpath;
414 else
416 outpath = this->Target->GetDirectory();
417 cmSystemTools::MakeDirectory(outpath.c_str());
418 outpath += "/";
419 if(!targetNameImport.empty())
421 outpathImp = this->Target->GetDirectory(0, true);
422 cmSystemTools::MakeDirectory(outpathImp.c_str());
423 outpathImp += "/";
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
434 // arguments.
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 ";
460 break;
461 case cmTarget::SHARED_LIBRARY:
462 buildEcho += " shared library ";
463 break;
464 case cmTarget::MODULE_LIBRARY:
465 buildEcho += " shared module ";
466 break;
467 default:
468 buildEcho += " library ";
469 break;
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";
481 break;
482 case cmTarget::MODULE_LIBRARY:
483 forbiddenFlagVar = "_CREATE_SHARED_MODULE_FORBIDDEN_FLAGS";
484 break;
485 default: break;
488 // Construct a list of files associated with this library that may
489 // need to be cleaned.
490 std::vector<std::string> libCleanFiles;
491 if(this->Target->GetPropertyAsBool("CLEAN_DIRECT_OUTPUT"))
493 // The user has requested that only the files directly built
494 // by this target be cleaned instead of all possible names.
495 libCleanFiles.push_back(this->Convert(targetFullPath.c_str(),
496 cmLocalGenerator::START_OUTPUT,
497 cmLocalGenerator::UNCHANGED));
498 if(targetNameReal != targetName)
500 libCleanFiles.push_back(this->Convert(targetFullPathReal.c_str(),
501 cmLocalGenerator::START_OUTPUT,
502 cmLocalGenerator::UNCHANGED));
504 if(targetNameSO != targetName &&
505 targetNameSO != targetNameReal)
507 libCleanFiles.push_back(this->Convert(targetFullPathSO.c_str(),
508 cmLocalGenerator::START_OUTPUT,
509 cmLocalGenerator::UNCHANGED));
511 if(!targetNameImport.empty())
513 libCleanFiles.push_back(this->Convert(targetFullPathImport.c_str(),
514 cmLocalGenerator::START_OUTPUT,
515 cmLocalGenerator::UNCHANGED));
518 else
520 // This target may switch between static and shared based
521 // on a user option or the BUILD_SHARED_LIBS switch. Clean
522 // all possible names.
523 std::string cleanStaticName;
524 std::string cleanSharedName;
525 std::string cleanSharedSOName;
526 std::string cleanSharedRealName;
527 std::string cleanImportName;
528 std::string cleanPDBName;
529 this->Target->GetLibraryCleanNames(
530 cleanStaticName,
531 cleanSharedName,
532 cleanSharedSOName,
533 cleanSharedRealName,
534 cleanImportName,
535 cleanPDBName,
536 this->LocalGenerator->ConfigurationName.c_str());
537 std::string cleanFullStaticName = outpath + cleanStaticName;
538 std::string cleanFullSharedName = outpath + cleanSharedName;
539 std::string cleanFullSharedSOName = outpath + cleanSharedSOName;
540 std::string cleanFullSharedRealName = outpath + cleanSharedRealName;
541 std::string cleanFullImportName = outpathImp + cleanImportName;
542 std::string cleanFullPDBName = outpath + cleanPDBName;
543 libCleanFiles.push_back
544 (this->Convert(cleanFullStaticName.c_str(),
545 cmLocalGenerator::START_OUTPUT,
546 cmLocalGenerator::UNCHANGED));
547 if(cleanSharedRealName != cleanStaticName)
549 libCleanFiles.push_back(this->Convert(cleanFullSharedRealName.c_str(),
550 cmLocalGenerator::START_OUTPUT,
551 cmLocalGenerator::UNCHANGED));
553 if(cleanSharedSOName != cleanStaticName &&
554 cleanSharedSOName != cleanSharedRealName)
556 libCleanFiles.push_back(this->Convert(cleanFullSharedSOName.c_str(),
557 cmLocalGenerator::START_OUTPUT,
558 cmLocalGenerator::UNCHANGED));
560 if(cleanSharedName != cleanStaticName &&
561 cleanSharedName != cleanSharedSOName &&
562 cleanSharedName != cleanSharedRealName)
564 libCleanFiles.push_back(this->Convert(cleanFullSharedName.c_str(),
565 cmLocalGenerator::START_OUTPUT,
566 cmLocalGenerator::UNCHANGED));
568 if(!cleanImportName.empty())
570 libCleanFiles.push_back(this->Convert(cleanFullImportName.c_str(),
571 cmLocalGenerator::START_OUTPUT,
572 cmLocalGenerator::UNCHANGED));
575 // List the PDB for cleaning only when the whole target is
576 // cleaned. We do not want to delete the .pdb file just before
577 // linking the target.
578 this->CleanFiles.push_back
579 (this->Convert(cleanFullPDBName.c_str(),
580 cmLocalGenerator::START_OUTPUT,
581 cmLocalGenerator::UNCHANGED));
584 #ifdef _WIN32
585 // There may be a manifest file for this target. Add it to the
586 // clean set just in case.
587 if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
589 libCleanFiles.push_back(
590 this->Convert((targetFullPath+".manifest").c_str(),
591 cmLocalGenerator::START_OUTPUT,
592 cmLocalGenerator::UNCHANGED));
594 #endif
596 std::vector<std::string> commands1;
597 // Add a command to remove any existing files for this library.
598 // for static libs only
599 if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
601 this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
602 *this->Target, "target");
603 this->LocalGenerator->CreateCDCommand
604 (commands1,
605 this->Makefile->GetStartOutputDirectory(),
606 cmLocalGenerator::HOME_OUTPUT);
607 commands.insert(commands.end(), commands1.begin(), commands1.end());
608 commands1.clear();
611 // Add the pre-build and pre-link rules building but not when relinking.
612 if(!relink)
614 this->LocalGenerator
615 ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands(),
616 this->Target);
617 this->LocalGenerator
618 ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands(),
619 this->Target);
622 // Determine whether a link script will be used.
623 bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
625 // Select whether to use a response file for objects.
626 bool useResponseFile = false;
628 std::string responseVar = "CMAKE_";
629 responseVar += linkLanguage;
630 responseVar += "_USE_RESPONSE_FILE_FOR_OBJECTS";
631 if(this->Makefile->IsOn(responseVar.c_str()))
633 useResponseFile = true;
637 // For static libraries there might be archiving rules.
638 bool haveStaticLibraryRule = false;
639 std::vector<std::string> archiveCreateCommands;
640 std::vector<std::string> archiveAppendCommands;
641 std::vector<std::string> archiveFinishCommands;
642 std::string::size_type archiveCommandLimit = std::string::npos;
643 if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
645 haveStaticLibraryRule =
646 this->Makefile->GetDefinition(linkRuleVar)? true:false;
647 std::string arCreateVar = "CMAKE_";
648 arCreateVar += linkLanguage;
649 arCreateVar += "_ARCHIVE_CREATE";
650 if(const char* rule = this->Makefile->GetDefinition(arCreateVar.c_str()))
652 cmSystemTools::ExpandListArgument(rule, archiveCreateCommands);
654 std::string arAppendVar = "CMAKE_";
655 arAppendVar += linkLanguage;
656 arAppendVar += "_ARCHIVE_APPEND";
657 if(const char* rule = this->Makefile->GetDefinition(arAppendVar.c_str()))
659 cmSystemTools::ExpandListArgument(rule, archiveAppendCommands);
661 std::string arFinishVar = "CMAKE_";
662 arFinishVar += linkLanguage;
663 arFinishVar += "_ARCHIVE_FINISH";
664 if(const char* rule = this->Makefile->GetDefinition(arFinishVar.c_str()))
666 cmSystemTools::ExpandListArgument(rule, archiveFinishCommands);
670 // Decide whether to use archiving rules.
671 bool useArchiveRules =
672 !haveStaticLibraryRule &&
673 !archiveCreateCommands.empty() && !archiveAppendCommands.empty();
674 if(useArchiveRules)
676 // Archiving rules are always run with a link script.
677 useLinkScript = true;
679 // Archiving rules never use a response file.
680 useResponseFile = false;
682 // Limit the length of individual object lists to less than the
683 // 32K command line length limit on Windows. We could make this a
684 // platform file variable but this should work everywhere.
685 archiveCommandLimit = 30000;
688 // Expand the rule variables.
689 std::vector<std::string> real_link_commands;
691 // Set path conversion for link script shells.
692 this->LocalGenerator->SetLinkScriptShell(useLinkScript);
694 // Collect up flags to link in needed libraries.
695 cmOStringStream linklibs;
696 if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
698 this->LocalGenerator
699 ->OutputLinkLibraries(linklibs, *this->Target, relink);
702 // Construct object file lists that may be needed to expand the
703 // rule.
704 std::string buildObjs;
705 this->CreateObjectLists(useLinkScript, useArchiveRules, useResponseFile,
706 buildObjs, depends);
708 cmLocalGenerator::RuleVariables vars;
709 vars.TargetPDB = targetOutPathPDB.c_str();
711 // Setup the target version.
712 std::string targetVersionMajor;
713 std::string targetVersionMinor;
715 cmOStringStream majorStream;
716 cmOStringStream minorStream;
717 int major;
718 int minor;
719 this->Target->GetTargetVersion(major, minor);
720 majorStream << major;
721 minorStream << minor;
722 targetVersionMajor = majorStream.str();
723 targetVersionMinor = minorStream.str();
725 vars.TargetVersionMajor = targetVersionMajor.c_str();
726 vars.TargetVersionMinor = targetVersionMinor.c_str();
728 vars.RuleLauncher = "RULE_LAUNCH_LINK";
729 vars.CMTarget = this->Target;
730 vars.Language = linkLanguage;
731 vars.Objects = buildObjs.c_str();
732 std::string objdir = cmake::GetCMakeFilesDirectoryPostSlash();
733 objdir += this->Target->GetName();
734 objdir += ".dir";
735 objdir = this->Convert(objdir.c_str(),
736 cmLocalGenerator::START_OUTPUT,
737 cmLocalGenerator::SHELL);
738 vars.ObjectDir = objdir.c_str();
739 vars.Target = targetOutPathReal.c_str();
740 std::string linkString = linklibs.str();
741 vars.LinkLibraries = linkString.c_str();
742 vars.ObjectsQuoted = buildObjs.c_str();
743 vars.TargetSOName= targetNameSO.c_str();
744 vars.LinkFlags = linkFlags.c_str();
746 // Compute the directory portion of the install_name setting.
747 std::string install_name_dir;
748 if(this->Target->GetType() == cmTarget::SHARED_LIBRARY)
750 // Get the install_name directory for the build tree.
751 const char* config = this->LocalGenerator->ConfigurationName.c_str();
752 install_name_dir = this->Target->GetInstallNameDirForBuildTree(config);
754 // Set the rule variable replacement value.
755 if(install_name_dir.empty())
757 vars.TargetInstallNameDir = "";
759 else
761 // Convert to a path for the native build tool.
762 install_name_dir =
763 this->LocalGenerator->Convert(install_name_dir.c_str(),
764 cmLocalGenerator::NONE,
765 cmLocalGenerator::SHELL, false);
766 vars.TargetInstallNameDir = install_name_dir.c_str();
769 std::string langFlags;
770 this->LocalGenerator
771 ->AddLanguageFlags(langFlags, linkLanguage,
772 this->LocalGenerator->ConfigurationName.c_str());
773 // remove any language flags that might not work with the
774 // particular os
775 if(forbiddenFlagVar)
777 this->RemoveForbiddenFlags(forbiddenFlagVar,
778 linkLanguage, langFlags);
780 vars.LanguageCompileFlags = langFlags.c_str();
782 // Construct the main link rule and expand placeholders.
783 this->LocalGenerator->TargetImplib = targetOutPathImport;
784 if(useArchiveRules)
786 // Construct the individual object list strings.
787 std::vector<std::string> object_strings;
788 this->WriteObjectsStrings(object_strings, archiveCommandLimit);
790 // Create the archive with the first set of objects.
791 std::vector<std::string>::iterator osi = object_strings.begin();
793 vars.Objects = osi->c_str();
794 for(std::vector<std::string>::const_iterator
795 i = archiveCreateCommands.begin();
796 i != archiveCreateCommands.end(); ++i)
798 std::string cmd = *i;
799 this->LocalGenerator->ExpandRuleVariables(cmd, vars);
800 real_link_commands.push_back(cmd);
803 // Append to the archive with the other object sets.
804 for(++osi; osi != object_strings.end(); ++osi)
806 vars.Objects = osi->c_str();
807 for(std::vector<std::string>::const_iterator
808 i = archiveAppendCommands.begin();
809 i != archiveAppendCommands.end(); ++i)
811 std::string cmd = *i;
812 this->LocalGenerator->ExpandRuleVariables(cmd, vars);
813 real_link_commands.push_back(cmd);
816 // Finish the archive.
817 vars.Objects = "";
818 for(std::vector<std::string>::const_iterator
819 i = archiveFinishCommands.begin();
820 i != archiveFinishCommands.end(); ++i)
822 std::string cmd = *i;
823 this->LocalGenerator->ExpandRuleVariables(cmd, vars);
824 real_link_commands.push_back(cmd);
827 else
829 // Get the set of commands.
830 std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
831 cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
833 // Expand placeholders.
834 for(std::vector<std::string>::iterator i = real_link_commands.begin();
835 i != real_link_commands.end(); ++i)
837 this->LocalGenerator->ExpandRuleVariables(*i, vars);
840 this->LocalGenerator->TargetImplib = "";
842 // Restore path conversion to normal shells.
843 this->LocalGenerator->SetLinkScriptShell(false);
846 // Optionally convert the build rule to use a script to avoid long
847 // command lines in the make shell.
848 if(useLinkScript)
850 // Use a link script.
851 const char* name = (relink? "relink.txt" : "link.txt");
852 this->CreateLinkScript(name, real_link_commands, commands1, depends);
854 else
856 // No link script. Just use the link rule directly.
857 commands1 = real_link_commands;
859 this->LocalGenerator->CreateCDCommand
860 (commands1,
861 this->Makefile->GetStartOutputDirectory(),
862 cmLocalGenerator::HOME_OUTPUT);
863 commands.insert(commands.end(), commands1.begin(), commands1.end());
864 commands1.clear();
866 // Add a rule to create necessary symlinks for the library.
867 if(targetOutPath != targetOutPathReal)
869 std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
870 symlink += targetOutPathReal;
871 symlink += " ";
872 symlink += targetOutPathSO;
873 symlink += " ";
874 symlink += targetOutPath;
875 commands1.push_back(symlink);
876 this->LocalGenerator->CreateCDCommand(commands1,
877 this->Makefile->GetStartOutputDirectory(),
878 cmLocalGenerator::HOME_OUTPUT);
879 commands.insert(commands.end(), commands1.begin(), commands1.end());
880 commands1.clear();
882 // Add the post-build rules when building but not when relinking.
883 if(!relink)
885 this->LocalGenerator->
886 AppendCustomCommands(commands, this->Target->GetPostBuildCommands(),
887 this->Target);
890 // Write the build rule.
891 this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
892 targetFullPathReal.c_str(),
893 depends, commands, false);
895 // Some targets have more than one output file. Create rules to
896 // drive the build if any extra outputs are missing.
897 std::vector<std::string> extraOutputs;
898 if(targetNameSO != targetNameReal)
900 this->GenerateExtraOutput(targetFullPathSO.c_str(),
901 targetFullPathReal.c_str());
903 if(targetName != targetNameSO &&
904 targetName != targetNameReal)
906 this->GenerateExtraOutput(targetFullPath.c_str(),
907 targetFullPathReal.c_str());
910 // Write the main driver rule to build everything in this target.
911 this->WriteTargetDriverRule(targetFullPath.c_str(), relink);
913 // Clean all the possible library names and symlinks.
914 this->CleanFiles.insert(this->CleanFiles.end(),
915 libCleanFiles.begin(),libCleanFiles.end());
918 //----------------------------------------------------------------------------
919 void
920 cmMakefileLibraryTargetGenerator
921 ::AppendOSXVerFlag(std::string& flags, const char* lang,
922 const char* name, bool so)
924 // Lookup the flag to specify the version.
925 std::string fvar = "CMAKE_";
926 fvar += lang;
927 fvar += "_OSX_";
928 fvar += name;
929 fvar += "_VERSION_FLAG";
930 const char* flag = this->Makefile->GetDefinition(fvar.c_str());
932 // Skip if no such flag.
933 if(!flag)
935 return;
938 // Lookup the target version information.
939 int major;
940 int minor;
941 int patch;
942 this->Target->GetTargetVersion(so, major, minor, patch);
943 if(major > 0 || minor > 0 || patch > 0)
945 // Append the flag since a non-zero version is specified.
946 cmOStringStream vflag;
947 vflag << flag << major << "." << minor << "." << patch;
948 this->LocalGenerator->AppendFlags(flags, vflag.str().c_str());