cmMakefileLibraryTargetGenerator.cxx 29 KB

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