ENH: put the 64 bit paths first
[cmake.git] / Source / cmMakefileLibraryTargetGenerator.cxx
blob88f3127de5fa5c42a43a89c189337d67d500f259
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmMakefileLibraryTargetGenerator.cxx,v $
5 Language: C++
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"
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 // 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));
521 #ifdef _WIN32
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));
531 #endif
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
541 (commands1,
542 this->Makefile->GetStartOutputDirectory(),
543 cmLocalGenerator::HOME_OUTPUT);
544 commands.insert(commands.end(), commands1.begin(), commands1.end());
545 commands1.clear();
548 // Add the pre-build and pre-link rules building but not when relinking.
549 if(!relink)
551 this->LocalGenerator
552 ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands(),
553 this->Target);
554 this->LocalGenerator
555 ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands(),
556 this->Target);
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();
611 if(useArchiveRules)
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)
635 this->LocalGenerator
636 ->OutputLinkLibraries(linklibs, *this->Target, relink);
639 // Construct object file lists that may be needed to expand the
640 // rule.
641 std::string buildObjs;
642 this->CreateObjectLists(useLinkScript, useArchiveRules, useResponseFile,
643 buildObjs, depends);
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;
654 int major;
655 int minor;
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();
671 objdir += ".dir";
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 = "";
696 else
698 // Convert to a path for the native build tool.
699 install_name_dir =
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;
707 this->LocalGenerator
708 ->AddLanguageFlags(langFlags, linkLanguage,
709 this->LocalGenerator->ConfigurationName.c_str());
710 // remove any language flags that might not work with the
711 // particular os
712 if(forbiddenFlagVar)
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;
721 if(useArchiveRules)
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.
754 vars.Objects = "";
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);
764 else
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.
785 if(useLinkScript)
787 // Use a link script.
788 const char* name = (relink? "relink.txt" : "link.txt");
789 this->CreateLinkScript(name, real_link_commands, commands1, depends);
791 else
793 // No link script. Just use the link rule directly.
794 commands1 = real_link_commands;
796 this->LocalGenerator->CreateCDCommand
797 (commands1,
798 this->Makefile->GetStartOutputDirectory(),
799 cmLocalGenerator::HOME_OUTPUT);
800 commands.insert(commands.end(), commands1.begin(), commands1.end());
801 commands1.clear();
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;
808 symlink += " ";
809 symlink += targetOutPathSO;
810 symlink += " ";
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());
817 commands1.clear();
819 // Add the post-build rules when building but not when relinking.
820 if(!relink)
822 this->LocalGenerator->
823 AppendCustomCommands(commands, this->Target->GetPostBuildCommands(),
824 this->Target);
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 //----------------------------------------------------------------------------
856 void
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_";
863 fvar += lang;
864 fvar += "_OSX_";
865 fvar += name;
866 fvar += "_VERSION_FLAG";
867 const char* flag = this->Makefile->GetDefinition(fvar.c_str());
869 // Skip if no such flag.
870 if(!flag)
872 return;
875 // Lookup the target version information.
876 int major;
877 int minor;
878 int patch;
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());