cmMakefileLibraryTargetGenerator.cxx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmMakefileLibraryTargetGenerator.h"
  11. #include "cmGeneratedFileStream.h"
  12. #include "cmGlobalUnixMakefileGenerator3.h"
  13. #include "cmLocalUnixMakefileGenerator3.h"
  14. #include "cmMakefile.h"
  15. #include "cmSourceFile.h"
  16. #include "cmTarget.h"
  17. #include "cmake.h"
  18. #include <memory> // auto_ptr
  19. //----------------------------------------------------------------------------
  20. cmMakefileLibraryTargetGenerator
  21. ::cmMakefileLibraryTargetGenerator(cmTarget* target):
  22. cmMakefileTargetGenerator(target)
  23. {
  24. this->CustomCommandDriver = OnDepends;
  25. this->Target->GetLibraryNames(
  26. this->TargetNameOut, this->TargetNameSO, this->TargetNameReal,
  27. this->TargetNameImport, this->TargetNamePDB, this->ConfigName);
  28. if(this->Target->IsFrameworkOnApple())
  29. {
  30. this->FrameworkVersion = this->Target->GetFrameworkVersion();
  31. this->MacContentDirectory = this->Target->GetDirectory(this->ConfigName);
  32. this->MacContentDirectory += "/";
  33. this->MacContentDirectory += this->TargetNameOut;
  34. this->MacContentDirectory += ".framework/Versions/";
  35. this->MacContentDirectory += this->FrameworkVersion;
  36. this->MacContentDirectory += "/";
  37. }
  38. }
  39. //----------------------------------------------------------------------------
  40. void cmMakefileLibraryTargetGenerator::WriteRuleFiles()
  41. {
  42. // create the build.make file and directory, put in the common blocks
  43. this->CreateRuleFile();
  44. // write rules used to help build object files
  45. this->WriteCommonCodeRules();
  46. // write in rules for object files and custom commands
  47. this->WriteTargetBuildRules();
  48. // write the per-target per-language flags
  49. this->WriteTargetLanguageFlags();
  50. // write the link rules
  51. // Write the rule for this target type.
  52. switch(this->Target->GetType())
  53. {
  54. case cmTarget::STATIC_LIBRARY:
  55. this->WriteStaticLibraryRules();
  56. break;
  57. case cmTarget::SHARED_LIBRARY:
  58. this->WriteSharedLibraryRules(false);
  59. if(this->Target->NeedRelinkBeforeInstall(this->ConfigName))
  60. {
  61. // Write rules to link an installable version of the target.
  62. this->WriteSharedLibraryRules(true);
  63. }
  64. break;
  65. case cmTarget::MODULE_LIBRARY:
  66. this->WriteModuleLibraryRules(false);
  67. if(this->Target->NeedRelinkBeforeInstall(this->ConfigName))
  68. {
  69. // Write rules to link an installable version of the target.
  70. this->WriteModuleLibraryRules(true);
  71. }
  72. break;
  73. default:
  74. // If language is not known, this is an error.
  75. cmSystemTools::Error("Unknown Library Type");
  76. break;
  77. }
  78. // Write the requires target.
  79. this->WriteTargetRequiresRules();
  80. // Write clean target
  81. this->WriteTargetCleanRules();
  82. // Write the dependency generation rule. This must be done last so
  83. // that multiple output pair information is available.
  84. this->WriteTargetDependRules();
  85. // close the streams
  86. this->CloseFileStreams();
  87. }
  88. //----------------------------------------------------------------------------
  89. void cmMakefileLibraryTargetGenerator::WriteStaticLibraryRules()
  90. {
  91. const char* linkLanguage =
  92. this->Target->GetLinkerLanguage(this->ConfigName);
  93. std::string linkRuleVar = "CMAKE_";
  94. if (linkLanguage)
  95. {
  96. linkRuleVar += linkLanguage;
  97. }
  98. linkRuleVar += "_CREATE_STATIC_LIBRARY";
  99. if(this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION") &&
  100. this->Makefile->GetDefinition((linkRuleVar+"_IPO").c_str()))
  101. {
  102. linkRuleVar += "_IPO";
  103. }
  104. std::string extraFlags;
  105. this->LocalGenerator->AppendFlags
  106. (extraFlags,this->Target->GetProperty("STATIC_LIBRARY_FLAGS"));
  107. std::string staticLibraryFlagsConfig = "STATIC_LIBRARY_FLAGS_";
  108. staticLibraryFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  109. this->LocalGenerator->AppendFlags
  110. (extraFlags, this->Target->GetProperty(staticLibraryFlagsConfig.c_str()));
  111. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), false);
  112. }
  113. //----------------------------------------------------------------------------
  114. void cmMakefileLibraryTargetGenerator::WriteSharedLibraryRules(bool relink)
  115. {
  116. if(this->Target->IsFrameworkOnApple())
  117. {
  118. this->WriteFrameworkRules(relink);
  119. return;
  120. }
  121. const char* linkLanguage =
  122. this->Target->GetLinkerLanguage(this->ConfigName);
  123. std::string linkRuleVar = "CMAKE_";
  124. if (linkLanguage)
  125. {
  126. linkRuleVar += linkLanguage;
  127. }
  128. linkRuleVar += "_CREATE_SHARED_LIBRARY";
  129. std::string extraFlags;
  130. this->LocalGenerator->AppendFlags
  131. (extraFlags, this->Target->GetProperty("LINK_FLAGS"));
  132. std::string linkFlagsConfig = "LINK_FLAGS_";
  133. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  134. this->LocalGenerator->AppendFlags
  135. (extraFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  136. this->LocalGenerator->AddConfigVariableFlags
  137. (extraFlags, "CMAKE_SHARED_LINKER_FLAGS", this->ConfigName);
  138. this->AddModuleDefinitionFlag(extraFlags);
  139. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), relink);
  140. }
  141. //----------------------------------------------------------------------------
  142. void cmMakefileLibraryTargetGenerator::WriteModuleLibraryRules(bool relink)
  143. {
  144. const char* linkLanguage =
  145. this->Target->GetLinkerLanguage(this->ConfigName);
  146. std::string linkRuleVar = "CMAKE_";
  147. if (linkLanguage)
  148. {
  149. linkRuleVar += linkLanguage;
  150. }
  151. linkRuleVar += "_CREATE_SHARED_MODULE";
  152. std::string extraFlags;
  153. this->LocalGenerator->AppendFlags(extraFlags,
  154. this->Target->GetProperty("LINK_FLAGS"));
  155. std::string linkFlagsConfig = "LINK_FLAGS_";
  156. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  157. this->LocalGenerator->AppendFlags
  158. (extraFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  159. this->LocalGenerator->AddConfigVariableFlags
  160. (extraFlags, "CMAKE_MODULE_LINKER_FLAGS", this->ConfigName);
  161. this->AddModuleDefinitionFlag(extraFlags);
  162. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), relink);
  163. }
  164. //----------------------------------------------------------------------------
  165. void cmMakefileLibraryTargetGenerator::WriteFrameworkRules(bool relink)
  166. {
  167. const char* linkLanguage =
  168. this->Target->GetLinkerLanguage(this->ConfigName);
  169. std::string linkRuleVar = "CMAKE_";
  170. if (linkLanguage)
  171. {
  172. linkRuleVar += linkLanguage;
  173. }
  174. linkRuleVar += "_CREATE_MACOSX_FRAMEWORK";
  175. std::string extraFlags;
  176. this->LocalGenerator->AppendFlags(extraFlags,
  177. this->Target->GetProperty("LINK_FLAGS"));
  178. std::string linkFlagsConfig = "LINK_FLAGS_";
  179. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  180. this->LocalGenerator->AppendFlags
  181. (extraFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  182. this->LocalGenerator->AddConfigVariableFlags
  183. (extraFlags, "CMAKE_MACOSX_FRAMEWORK_LINKER_FLAGS", this->ConfigName);
  184. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), relink);
  185. }
  186. //----------------------------------------------------------------------------
  187. void
  188. cmMakefileLibraryTargetGenerator
  189. ::CreateFramework(std::string const& targetName)
  190. {
  191. // Configure the Info.plist file into the Resources directory.
  192. this->MacContentFolders.insert("Resources");
  193. std::string plist = this->MacContentDirectory + "Resources/Info.plist";
  194. this->LocalGenerator->GenerateFrameworkInfoPList(this->Target,
  195. targetName.c_str(),
  196. plist.c_str());
  197. // TODO: Use the cmMakefileTargetGenerator::ExtraFiles vector to
  198. // drive rules to create these files at build time.
  199. std::string oldName;
  200. std::string newName;
  201. // Compute the location of the top-level foo.framework directory.
  202. std::string top = this->Target->GetDirectory(this->ConfigName);
  203. top += "/";
  204. top += this->TargetNameOut;
  205. top += ".framework/";
  206. // Make foo.framework/Versions
  207. std::string versions = top;
  208. versions += "Versions";
  209. cmSystemTools::MakeDirectory(versions.c_str());
  210. // Make foo.framework/Versions/version
  211. std::string version = versions;
  212. version += "/";
  213. version += this->FrameworkVersion;
  214. cmSystemTools::MakeDirectory(version.c_str());
  215. // Current -> version
  216. oldName = this->FrameworkVersion;
  217. newName = versions;
  218. newName += "/Current";
  219. cmSystemTools::RemoveFile(newName.c_str());
  220. cmSystemTools::CreateSymlink(oldName.c_str(), newName.c_str());
  221. this->Makefile->AddCMakeOutputFile(newName.c_str());
  222. // foo -> Versions/Current/foo
  223. oldName = "Versions/Current/";
  224. oldName += this->TargetNameOut;
  225. newName = top;
  226. newName += this->TargetNameOut;
  227. cmSystemTools::RemoveFile(newName.c_str());
  228. cmSystemTools::CreateSymlink(oldName.c_str(), newName.c_str());
  229. this->Makefile->AddCMakeOutputFile(newName.c_str());
  230. // Resources -> Versions/Current/Resources
  231. if(this->MacContentFolders.find("Resources") !=
  232. this->MacContentFolders.end())
  233. {
  234. oldName = "Versions/Current/Resources";
  235. newName = top;
  236. newName += "Resources";
  237. cmSystemTools::RemoveFile(newName.c_str());
  238. cmSystemTools::CreateSymlink(oldName.c_str(), newName.c_str());
  239. this->Makefile->AddCMakeOutputFile(newName.c_str());
  240. }
  241. // Headers -> Versions/Current/Headers
  242. if(this->MacContentFolders.find("Headers") !=
  243. this->MacContentFolders.end())
  244. {
  245. oldName = "Versions/Current/Headers";
  246. newName = top;
  247. newName += "Headers";
  248. cmSystemTools::RemoveFile(newName.c_str());
  249. cmSystemTools::CreateSymlink(oldName.c_str(), newName.c_str());
  250. this->Makefile->AddCMakeOutputFile(newName.c_str());
  251. }
  252. // PrivateHeaders -> Versions/Current/PrivateHeaders
  253. if(this->MacContentFolders.find("PrivateHeaders") !=
  254. this->MacContentFolders.end())
  255. {
  256. oldName = "Versions/Current/PrivateHeaders";
  257. newName = top;
  258. newName += "PrivateHeaders";
  259. cmSystemTools::RemoveFile(newName.c_str());
  260. cmSystemTools::CreateSymlink(oldName.c_str(), newName.c_str());
  261. this->Makefile->AddCMakeOutputFile(newName.c_str());
  262. }
  263. }
  264. //----------------------------------------------------------------------------
  265. void cmMakefileLibraryTargetGenerator::WriteLibraryRules
  266. (const char* linkRuleVar, const char* extraFlags, bool relink)
  267. {
  268. // TODO: Merge the methods that call this method to avoid
  269. // code duplication.
  270. std::vector<std::string> commands;
  271. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  272. std::string objTarget;
  273. // Build list of dependencies.
  274. std::vector<std::string> depends;
  275. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  276. obj != this->Objects.end(); ++obj)
  277. {
  278. objTarget = relPath;
  279. objTarget += *obj;
  280. depends.push_back(objTarget);
  281. }
  282. // Add dependencies on targets that must be built first.
  283. this->AppendTargetDepends(depends);
  284. // Add a dependency on the rule file itself.
  285. this->LocalGenerator->AppendRuleDepend(depends,
  286. this->BuildFileNameFull.c_str());
  287. for(std::vector<std::string>::const_iterator obj
  288. = this->ExternalObjects.begin();
  289. obj != this->ExternalObjects.end(); ++obj)
  290. {
  291. depends.push_back(*obj);
  292. }
  293. // Get the language to use for linking this library.
  294. const char* linkLanguage =
  295. this->Target->GetLinkerLanguage(this->ConfigName);
  296. // Make sure we have a link language.
  297. if(!linkLanguage)
  298. {
  299. cmSystemTools::Error("Cannot determine link language for target \"",
  300. this->Target->GetName(), "\".");
  301. return;
  302. }
  303. // Create set of linking flags.
  304. std::string linkFlags;
  305. this->LocalGenerator->AppendFlags(linkFlags, extraFlags);
  306. // Add OSX version flags, if any.
  307. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  308. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  309. {
  310. this->AppendOSXVerFlag(linkFlags, linkLanguage, "COMPATIBILITY", true);
  311. this->AppendOSXVerFlag(linkFlags, linkLanguage, "CURRENT", false);
  312. }
  313. // Construct the name of the library.
  314. std::string targetName;
  315. std::string targetNameSO;
  316. std::string targetNameReal;
  317. std::string targetNameImport;
  318. std::string targetNamePDB;
  319. this->Target->GetLibraryNames(
  320. targetName, targetNameSO, targetNameReal, targetNameImport, targetNamePDB,
  321. this->ConfigName);
  322. // Construct the full path version of the names.
  323. std::string outpath;
  324. std::string outpathImp;
  325. if(this->Target->IsFrameworkOnApple())
  326. {
  327. outpath = this->MacContentDirectory;
  328. this->CreateFramework(targetName);
  329. }
  330. else if(relink)
  331. {
  332. outpath = this->Makefile->GetStartOutputDirectory();
  333. outpath += cmake::GetCMakeFilesDirectory();
  334. outpath += "/CMakeRelink.dir";
  335. cmSystemTools::MakeDirectory(outpath.c_str());
  336. outpath += "/";
  337. if(!targetNameImport.empty())
  338. {
  339. outpathImp = outpath;
  340. }
  341. }
  342. else
  343. {
  344. outpath = this->Target->GetDirectory(this->ConfigName);
  345. cmSystemTools::MakeDirectory(outpath.c_str());
  346. outpath += "/";
  347. if(!targetNameImport.empty())
  348. {
  349. outpathImp = this->Target->GetDirectory(this->ConfigName, true);
  350. cmSystemTools::MakeDirectory(outpathImp.c_str());
  351. outpathImp += "/";
  352. }
  353. }
  354. std::string targetFullPath = outpath + targetName;
  355. std::string targetFullPathPDB = outpath + targetNamePDB;
  356. std::string targetFullPathSO = outpath + targetNameSO;
  357. std::string targetFullPathReal = outpath + targetNameReal;
  358. std::string targetFullPathImport = outpathImp + targetNameImport;
  359. // Construct the output path version of the names for use in command
  360. // arguments.
  361. std::string targetOutPathPDB =
  362. this->Convert(targetFullPathPDB.c_str(),cmLocalGenerator::NONE,
  363. cmLocalGenerator::SHELL);
  364. std::string targetOutPath =
  365. this->Convert(targetFullPath.c_str(),cmLocalGenerator::START_OUTPUT,
  366. cmLocalGenerator::SHELL);
  367. std::string targetOutPathSO =
  368. this->Convert(targetFullPathSO.c_str(),cmLocalGenerator::START_OUTPUT,
  369. cmLocalGenerator::SHELL);
  370. std::string targetOutPathReal =
  371. this->Convert(targetFullPathReal.c_str(),cmLocalGenerator::START_OUTPUT,
  372. cmLocalGenerator::SHELL);
  373. std::string targetOutPathImport =
  374. this->Convert(targetFullPathImport.c_str(),cmLocalGenerator::START_OUTPUT,
  375. cmLocalGenerator::SHELL);
  376. if(!this->NoRuleMessages)
  377. {
  378. // Add the link message.
  379. std::string buildEcho = "Linking ";
  380. buildEcho += linkLanguage;
  381. switch(this->Target->GetType())
  382. {
  383. case cmTarget::STATIC_LIBRARY:
  384. buildEcho += " static library ";
  385. break;
  386. case cmTarget::SHARED_LIBRARY:
  387. buildEcho += " shared library ";
  388. break;
  389. case cmTarget::MODULE_LIBRARY:
  390. buildEcho += " shared module ";
  391. break;
  392. default:
  393. buildEcho += " library ";
  394. break;
  395. }
  396. buildEcho += targetOutPath.c_str();
  397. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  398. cmLocalUnixMakefileGenerator3::EchoLink);
  399. }
  400. const char* forbiddenFlagVar = 0;
  401. switch(this->Target->GetType())
  402. {
  403. case cmTarget::SHARED_LIBRARY:
  404. forbiddenFlagVar = "_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS";
  405. break;
  406. case cmTarget::MODULE_LIBRARY:
  407. forbiddenFlagVar = "_CREATE_SHARED_MODULE_FORBIDDEN_FLAGS";
  408. break;
  409. default: break;
  410. }
  411. // Clean files associated with this library.
  412. std::vector<std::string> libCleanFiles;
  413. libCleanFiles.push_back(this->Convert(targetFullPath.c_str(),
  414. cmLocalGenerator::START_OUTPUT,
  415. cmLocalGenerator::UNCHANGED));
  416. if(targetNameReal != targetName)
  417. {
  418. libCleanFiles.push_back(this->Convert(targetFullPathReal.c_str(),
  419. cmLocalGenerator::START_OUTPUT,
  420. cmLocalGenerator::UNCHANGED));
  421. }
  422. if(targetNameSO != targetName &&
  423. targetNameSO != targetNameReal)
  424. {
  425. libCleanFiles.push_back(this->Convert(targetFullPathSO.c_str(),
  426. cmLocalGenerator::START_OUTPUT,
  427. cmLocalGenerator::UNCHANGED));
  428. }
  429. if(!targetNameImport.empty())
  430. {
  431. libCleanFiles.push_back(this->Convert(targetFullPathImport.c_str(),
  432. cmLocalGenerator::START_OUTPUT,
  433. cmLocalGenerator::UNCHANGED));
  434. }
  435. // List the PDB for cleaning only when the whole target is
  436. // cleaned. We do not want to delete the .pdb file just before
  437. // linking the target.
  438. this->CleanFiles.push_back
  439. (this->Convert(targetFullPathPDB.c_str(),
  440. cmLocalGenerator::START_OUTPUT,
  441. cmLocalGenerator::UNCHANGED));
  442. #ifdef _WIN32
  443. // There may be a manifest file for this target. Add it to the
  444. // clean set just in case.
  445. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  446. {
  447. libCleanFiles.push_back(
  448. this->Convert((targetFullPath+".manifest").c_str(),
  449. cmLocalGenerator::START_OUTPUT,
  450. cmLocalGenerator::UNCHANGED));
  451. }
  452. #endif
  453. std::vector<std::string> commands1;
  454. // Add a command to remove any existing files for this library.
  455. // for static libs only
  456. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  457. {
  458. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  459. *this->Target, "target");
  460. this->LocalGenerator->CreateCDCommand
  461. (commands1,
  462. this->Makefile->GetStartOutputDirectory(),
  463. cmLocalGenerator::HOME_OUTPUT);
  464. commands.insert(commands.end(), commands1.begin(), commands1.end());
  465. commands1.clear();
  466. }
  467. // Add the pre-build and pre-link rules building but not when relinking.
  468. if(!relink)
  469. {
  470. this->LocalGenerator
  471. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands(),
  472. this->Target);
  473. this->LocalGenerator
  474. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands(),
  475. this->Target);
  476. }
  477. // Determine whether a link script will be used.
  478. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  479. // Select whether to use a response file for objects.
  480. bool useResponseFile = false;
  481. {
  482. std::string responseVar = "CMAKE_";
  483. responseVar += linkLanguage;
  484. responseVar += "_USE_RESPONSE_FILE_FOR_OBJECTS";
  485. if(this->Makefile->IsOn(responseVar.c_str()))
  486. {
  487. useResponseFile = true;
  488. }
  489. }
  490. // For static libraries there might be archiving rules.
  491. bool haveStaticLibraryRule = false;
  492. std::vector<std::string> archiveCreateCommands;
  493. std::vector<std::string> archiveAppendCommands;
  494. std::vector<std::string> archiveFinishCommands;
  495. std::string::size_type archiveCommandLimit = std::string::npos;
  496. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  497. {
  498. haveStaticLibraryRule =
  499. this->Makefile->GetDefinition(linkRuleVar)? true:false;
  500. std::string arCreateVar = "CMAKE_";
  501. arCreateVar += linkLanguage;
  502. arCreateVar += "_ARCHIVE_CREATE";
  503. if(const char* rule = this->Makefile->GetDefinition(arCreateVar.c_str()))
  504. {
  505. cmSystemTools::ExpandListArgument(rule, archiveCreateCommands);
  506. }
  507. std::string arAppendVar = "CMAKE_";
  508. arAppendVar += linkLanguage;
  509. arAppendVar += "_ARCHIVE_APPEND";
  510. if(const char* rule = this->Makefile->GetDefinition(arAppendVar.c_str()))
  511. {
  512. cmSystemTools::ExpandListArgument(rule, archiveAppendCommands);
  513. }
  514. std::string arFinishVar = "CMAKE_";
  515. arFinishVar += linkLanguage;
  516. arFinishVar += "_ARCHIVE_FINISH";
  517. if(const char* rule = this->Makefile->GetDefinition(arFinishVar.c_str()))
  518. {
  519. cmSystemTools::ExpandListArgument(rule, archiveFinishCommands);
  520. }
  521. }
  522. // Decide whether to use archiving rules.
  523. bool useArchiveRules =
  524. !haveStaticLibraryRule &&
  525. !archiveCreateCommands.empty() && !archiveAppendCommands.empty();
  526. if(useArchiveRules)
  527. {
  528. // Archiving rules are always run with a link script.
  529. useLinkScript = true;
  530. // Archiving rules never use a response file.
  531. useResponseFile = false;
  532. // Limit the length of individual object lists to less than the
  533. // 32K command line length limit on Windows. We could make this a
  534. // platform file variable but this should work everywhere.
  535. archiveCommandLimit = 30000;
  536. }
  537. // Expand the rule variables.
  538. std::vector<std::string> real_link_commands;
  539. {
  540. // Set path conversion for link script shells.
  541. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  542. // Collect up flags to link in needed libraries.
  543. cmOStringStream linklibs;
  544. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  545. {
  546. this->LocalGenerator
  547. ->OutputLinkLibraries(linklibs, *this->Target, relink);
  548. }
  549. // Construct object file lists that may be needed to expand the
  550. // rule.
  551. std::string buildObjs;
  552. this->CreateObjectLists(useLinkScript, useArchiveRules, useResponseFile,
  553. buildObjs, depends);
  554. cmLocalGenerator::RuleVariables vars;
  555. vars.TargetPDB = targetOutPathPDB.c_str();
  556. // Setup the target version.
  557. std::string targetVersionMajor;
  558. std::string targetVersionMinor;
  559. {
  560. cmOStringStream majorStream;
  561. cmOStringStream minorStream;
  562. int major;
  563. int minor;
  564. this->Target->GetTargetVersion(major, minor);
  565. majorStream << major;
  566. minorStream << minor;
  567. targetVersionMajor = majorStream.str();
  568. targetVersionMinor = minorStream.str();
  569. }
  570. vars.TargetVersionMajor = targetVersionMajor.c_str();
  571. vars.TargetVersionMinor = targetVersionMinor.c_str();
  572. vars.RuleLauncher = "RULE_LAUNCH_LINK";
  573. vars.CMTarget = this->Target;
  574. vars.Language = linkLanguage;
  575. vars.Objects = buildObjs.c_str();
  576. std::string objdir = cmake::GetCMakeFilesDirectoryPostSlash();
  577. objdir += this->Target->GetName();
  578. objdir += ".dir";
  579. objdir = this->Convert(objdir.c_str(),
  580. cmLocalGenerator::START_OUTPUT,
  581. cmLocalGenerator::SHELL);
  582. vars.ObjectDir = objdir.c_str();
  583. vars.Target = targetOutPathReal.c_str();
  584. std::string linkString = linklibs.str();
  585. vars.LinkLibraries = linkString.c_str();
  586. vars.ObjectsQuoted = buildObjs.c_str();
  587. vars.TargetSOName= targetNameSO.c_str();
  588. vars.LinkFlags = linkFlags.c_str();
  589. // Compute the directory portion of the install_name setting.
  590. std::string install_name_dir;
  591. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY)
  592. {
  593. // Get the install_name directory for the build tree.
  594. install_name_dir =
  595. this->Target->GetInstallNameDirForBuildTree(this->ConfigName);
  596. // Set the rule variable replacement value.
  597. if(install_name_dir.empty())
  598. {
  599. vars.TargetInstallNameDir = "";
  600. }
  601. else
  602. {
  603. // Convert to a path for the native build tool.
  604. install_name_dir =
  605. this->LocalGenerator->Convert(install_name_dir.c_str(),
  606. cmLocalGenerator::NONE,
  607. cmLocalGenerator::SHELL, false);
  608. vars.TargetInstallNameDir = install_name_dir.c_str();
  609. }
  610. }
  611. // Add language feature flags.
  612. std::string langFlags;
  613. this->AddFeatureFlags(langFlags, linkLanguage);
  614. this->LocalGenerator->AddArchitectureFlags(langFlags, this->Target,
  615. linkLanguage, this->ConfigName);
  616. // remove any language flags that might not work with the
  617. // particular os
  618. if(forbiddenFlagVar)
  619. {
  620. this->RemoveForbiddenFlags(forbiddenFlagVar,
  621. linkLanguage, langFlags);
  622. }
  623. vars.LanguageCompileFlags = langFlags.c_str();
  624. // Construct the main link rule and expand placeholders.
  625. this->LocalGenerator->TargetImplib = targetOutPathImport;
  626. if(useArchiveRules)
  627. {
  628. // Construct the individual object list strings.
  629. std::vector<std::string> object_strings;
  630. this->WriteObjectsStrings(object_strings, archiveCommandLimit);
  631. // Create the archive with the first set of objects.
  632. std::vector<std::string>::iterator osi = object_strings.begin();
  633. {
  634. vars.Objects = osi->c_str();
  635. for(std::vector<std::string>::const_iterator
  636. i = archiveCreateCommands.begin();
  637. i != archiveCreateCommands.end(); ++i)
  638. {
  639. std::string cmd = *i;
  640. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  641. real_link_commands.push_back(cmd);
  642. }
  643. }
  644. // Append to the archive with the other object sets.
  645. for(++osi; osi != object_strings.end(); ++osi)
  646. {
  647. vars.Objects = osi->c_str();
  648. for(std::vector<std::string>::const_iterator
  649. i = archiveAppendCommands.begin();
  650. i != archiveAppendCommands.end(); ++i)
  651. {
  652. std::string cmd = *i;
  653. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  654. real_link_commands.push_back(cmd);
  655. }
  656. }
  657. // Finish the archive.
  658. vars.Objects = "";
  659. for(std::vector<std::string>::const_iterator
  660. i = archiveFinishCommands.begin();
  661. i != archiveFinishCommands.end(); ++i)
  662. {
  663. std::string cmd = *i;
  664. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  665. real_link_commands.push_back(cmd);
  666. }
  667. }
  668. else
  669. {
  670. // Get the set of commands.
  671. std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
  672. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  673. // Expand placeholders.
  674. for(std::vector<std::string>::iterator i = real_link_commands.begin();
  675. i != real_link_commands.end(); ++i)
  676. {
  677. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  678. }
  679. }
  680. this->LocalGenerator->TargetImplib = "";
  681. // Restore path conversion to normal shells.
  682. this->LocalGenerator->SetLinkScriptShell(false);
  683. }
  684. // Optionally convert the build rule to use a script to avoid long
  685. // command lines in the make shell.
  686. if(useLinkScript)
  687. {
  688. // Use a link script.
  689. const char* name = (relink? "relink.txt" : "link.txt");
  690. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  691. }
  692. else
  693. {
  694. // No link script. Just use the link rule directly.
  695. commands1 = real_link_commands;
  696. }
  697. this->LocalGenerator->CreateCDCommand
  698. (commands1,
  699. this->Makefile->GetStartOutputDirectory(),
  700. cmLocalGenerator::HOME_OUTPUT);
  701. commands.insert(commands.end(), commands1.begin(), commands1.end());
  702. commands1.clear();
  703. // Add a rule to create necessary symlinks for the library.
  704. if(targetOutPath != targetOutPathReal)
  705. {
  706. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  707. symlink += targetOutPathReal;
  708. symlink += " ";
  709. symlink += targetOutPathSO;
  710. symlink += " ";
  711. symlink += targetOutPath;
  712. commands1.push_back(symlink);
  713. this->LocalGenerator->CreateCDCommand(commands1,
  714. this->Makefile->GetStartOutputDirectory(),
  715. cmLocalGenerator::HOME_OUTPUT);
  716. commands.insert(commands.end(), commands1.begin(), commands1.end());
  717. commands1.clear();
  718. }
  719. // Add the post-build rules when building but not when relinking.
  720. if(!relink)
  721. {
  722. this->LocalGenerator->
  723. AppendCustomCommands(commands, this->Target->GetPostBuildCommands(),
  724. this->Target);
  725. }
  726. // Write the build rule.
  727. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  728. targetFullPathReal.c_str(),
  729. depends, commands, false);
  730. // Some targets have more than one output file. Create rules to
  731. // drive the build if any extra outputs are missing.
  732. std::vector<std::string> extraOutputs;
  733. if(targetNameSO != targetNameReal)
  734. {
  735. this->GenerateExtraOutput(targetFullPathSO.c_str(),
  736. targetFullPathReal.c_str());
  737. }
  738. if(targetName != targetNameSO &&
  739. targetName != targetNameReal)
  740. {
  741. this->GenerateExtraOutput(targetFullPath.c_str(),
  742. targetFullPathReal.c_str());
  743. }
  744. // Write the main driver rule to build everything in this target.
  745. this->WriteTargetDriverRule(targetFullPath.c_str(), relink);
  746. // Clean all the possible library names and symlinks.
  747. this->CleanFiles.insert(this->CleanFiles.end(),
  748. libCleanFiles.begin(),libCleanFiles.end());
  749. }
  750. //----------------------------------------------------------------------------
  751. void
  752. cmMakefileLibraryTargetGenerator
  753. ::AppendOSXVerFlag(std::string& flags, const char* lang,
  754. const char* name, bool so)
  755. {
  756. // Lookup the flag to specify the version.
  757. std::string fvar = "CMAKE_";
  758. fvar += lang;
  759. fvar += "_OSX_";
  760. fvar += name;
  761. fvar += "_VERSION_FLAG";
  762. const char* flag = this->Makefile->GetDefinition(fvar.c_str());
  763. // Skip if no such flag.
  764. if(!flag)
  765. {
  766. return;
  767. }
  768. // Lookup the target version information.
  769. int major;
  770. int minor;
  771. int patch;
  772. this->Target->GetTargetVersion(so, major, minor, patch);
  773. if(major > 0 || minor > 0 || patch > 0)
  774. {
  775. // Append the flag since a non-zero version is specified.
  776. cmOStringStream vflag;
  777. vflag << flag << major << "." << minor << "." << patch;
  778. this->LocalGenerator->AppendFlags(flags, vflag.str().c_str());
  779. }
  780. }