cmMakefileLibraryTargetGenerator.cxx 27 KB

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