cmMakefileLibraryTargetGenerator.cxx 29 KB

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