cmMakefileLibraryTargetGenerator.cxx 27 KB

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