cmMakefileLibraryTargetGenerator.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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 char* linkRuleVar, const char* 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);
  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. // For static libraries there might be archiving rules.
  434. bool haveStaticLibraryRule = false;
  435. std::vector<std::string> archiveCreateCommands;
  436. std::vector<std::string> archiveAppendCommands;
  437. std::vector<std::string> archiveFinishCommands;
  438. std::string::size_type archiveCommandLimit = std::string::npos;
  439. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  440. {
  441. haveStaticLibraryRule =
  442. this->Makefile->GetDefinition(linkRuleVar)? true:false;
  443. std::string arCreateVar = "CMAKE_";
  444. arCreateVar += linkLanguage;
  445. arCreateVar += "_ARCHIVE_CREATE";
  446. if(const char* rule = this->Makefile->GetDefinition(arCreateVar.c_str()))
  447. {
  448. cmSystemTools::ExpandListArgument(rule, archiveCreateCommands);
  449. }
  450. std::string arAppendVar = "CMAKE_";
  451. arAppendVar += linkLanguage;
  452. arAppendVar += "_ARCHIVE_APPEND";
  453. if(const char* rule = this->Makefile->GetDefinition(arAppendVar.c_str()))
  454. {
  455. cmSystemTools::ExpandListArgument(rule, archiveAppendCommands);
  456. }
  457. std::string arFinishVar = "CMAKE_";
  458. arFinishVar += linkLanguage;
  459. arFinishVar += "_ARCHIVE_FINISH";
  460. if(const char* rule = this->Makefile->GetDefinition(arFinishVar.c_str()))
  461. {
  462. cmSystemTools::ExpandListArgument(rule, archiveFinishCommands);
  463. }
  464. }
  465. // Decide whether to use archiving rules.
  466. bool useArchiveRules =
  467. !haveStaticLibraryRule &&
  468. !archiveCreateCommands.empty() && !archiveAppendCommands.empty();
  469. if(useArchiveRules)
  470. {
  471. // Archiving rules are always run with a link script.
  472. useLinkScript = true;
  473. // Archiving rules never use a response file.
  474. useResponseFileForObjects = false;
  475. // Limit the length of individual object lists to less than the
  476. // 32K command line length limit on Windows. We could make this a
  477. // platform file variable but this should work everywhere.
  478. archiveCommandLimit = 30000;
  479. }
  480. // Expand the rule variables.
  481. std::vector<std::string> real_link_commands;
  482. {
  483. // Set path conversion for link script shells.
  484. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  485. // Collect up flags to link in needed libraries.
  486. std::string linkLibs;
  487. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  488. {
  489. this->CreateLinkLibs(linkLibs, relink, depends);
  490. }
  491. // Construct object file lists that may be needed to expand the
  492. // rule.
  493. std::string buildObjs;
  494. this->CreateObjectLists(useLinkScript, useArchiveRules,
  495. useResponseFileForObjects, buildObjs, depends);
  496. cmLocalGenerator::RuleVariables vars;
  497. vars.TargetPDB = targetOutPathPDB.c_str();
  498. // Setup the target version.
  499. std::string targetVersionMajor;
  500. std::string targetVersionMinor;
  501. {
  502. cmOStringStream majorStream;
  503. cmOStringStream minorStream;
  504. int major;
  505. int minor;
  506. this->Target->GetTargetVersion(major, minor);
  507. majorStream << major;
  508. minorStream << minor;
  509. targetVersionMajor = majorStream.str();
  510. targetVersionMinor = minorStream.str();
  511. }
  512. vars.TargetVersionMajor = targetVersionMajor.c_str();
  513. vars.TargetVersionMinor = targetVersionMinor.c_str();
  514. vars.RuleLauncher = "RULE_LAUNCH_LINK";
  515. vars.CMTarget = this->Target;
  516. vars.Language = linkLanguage;
  517. vars.Objects = buildObjs.c_str();
  518. std::string objectDir = this->Target->GetSupportDirectory();
  519. objectDir = this->Convert(objectDir.c_str(),
  520. cmLocalGenerator::START_OUTPUT,
  521. cmLocalGenerator::SHELL);
  522. vars.ObjectDir = objectDir.c_str();
  523. vars.Target = targetOutPathReal.c_str();
  524. vars.LinkLibraries = linkLibs.c_str();
  525. vars.ObjectsQuoted = buildObjs.c_str();
  526. if (this->Target->HasSOName(this->ConfigName))
  527. {
  528. vars.SONameFlag = this->Makefile->GetSONameFlag(linkLanguage);
  529. vars.TargetSOName= targetNameSO.c_str();
  530. }
  531. vars.LinkFlags = linkFlags.c_str();
  532. // Compute the directory portion of the install_name setting.
  533. std::string install_name_dir;
  534. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY)
  535. {
  536. // Get the install_name directory for the build tree.
  537. install_name_dir =
  538. this->Target->GetInstallNameDirForBuildTree(this->ConfigName);
  539. // Set the rule variable replacement value.
  540. if(install_name_dir.empty())
  541. {
  542. vars.TargetInstallNameDir = "";
  543. }
  544. else
  545. {
  546. // Convert to a path for the native build tool.
  547. install_name_dir =
  548. this->LocalGenerator->Convert(install_name_dir.c_str(),
  549. cmLocalGenerator::NONE,
  550. cmLocalGenerator::SHELL, false);
  551. vars.TargetInstallNameDir = install_name_dir.c_str();
  552. }
  553. }
  554. // Add language feature flags.
  555. std::string langFlags;
  556. this->AddFeatureFlags(langFlags, linkLanguage);
  557. this->LocalGenerator->AddArchitectureFlags(langFlags, this->GeneratorTarget,
  558. linkLanguage, this->ConfigName);
  559. // remove any language flags that might not work with the
  560. // particular os
  561. if(forbiddenFlagVar)
  562. {
  563. this->RemoveForbiddenFlags(forbiddenFlagVar,
  564. linkLanguage, langFlags);
  565. }
  566. vars.LanguageCompileFlags = langFlags.c_str();
  567. // Construct the main link rule and expand placeholders.
  568. this->LocalGenerator->TargetImplib = targetOutPathImport;
  569. if(useArchiveRules)
  570. {
  571. // Construct the individual object list strings.
  572. std::vector<std::string> object_strings;
  573. this->WriteObjectsStrings(object_strings, archiveCommandLimit);
  574. // Create the archive with the first set of objects.
  575. std::vector<std::string>::iterator osi = object_strings.begin();
  576. {
  577. vars.Objects = osi->c_str();
  578. for(std::vector<std::string>::const_iterator
  579. i = archiveCreateCommands.begin();
  580. i != archiveCreateCommands.end(); ++i)
  581. {
  582. std::string cmd = *i;
  583. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  584. real_link_commands.push_back(cmd);
  585. }
  586. }
  587. // Append to the archive with the other object sets.
  588. for(++osi; osi != object_strings.end(); ++osi)
  589. {
  590. vars.Objects = osi->c_str();
  591. for(std::vector<std::string>::const_iterator
  592. i = archiveAppendCommands.begin();
  593. i != archiveAppendCommands.end(); ++i)
  594. {
  595. std::string cmd = *i;
  596. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  597. real_link_commands.push_back(cmd);
  598. }
  599. }
  600. // Finish the archive.
  601. vars.Objects = "";
  602. for(std::vector<std::string>::const_iterator
  603. i = archiveFinishCommands.begin();
  604. i != archiveFinishCommands.end(); ++i)
  605. {
  606. std::string cmd = *i;
  607. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  608. real_link_commands.push_back(cmd);
  609. }
  610. }
  611. else
  612. {
  613. // Get the set of commands.
  614. std::string linkRule = this->GetLinkRule(linkRuleVar);
  615. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  616. // Expand placeholders.
  617. for(std::vector<std::string>::iterator i = real_link_commands.begin();
  618. i != real_link_commands.end(); ++i)
  619. {
  620. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  621. }
  622. }
  623. this->LocalGenerator->TargetImplib = "";
  624. // Restore path conversion to normal shells.
  625. this->LocalGenerator->SetLinkScriptShell(false);
  626. }
  627. // Optionally convert the build rule to use a script to avoid long
  628. // command lines in the make shell.
  629. if(useLinkScript)
  630. {
  631. // Use a link script.
  632. const char* name = (relink? "relink.txt" : "link.txt");
  633. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  634. }
  635. else
  636. {
  637. // No link script. Just use the link rule directly.
  638. commands1 = real_link_commands;
  639. }
  640. this->LocalGenerator->CreateCDCommand
  641. (commands1,
  642. this->Makefile->GetStartOutputDirectory(),
  643. cmLocalGenerator::HOME_OUTPUT);
  644. commands.insert(commands.end(), commands1.begin(), commands1.end());
  645. commands1.clear();
  646. // Add a rule to create necessary symlinks for the library.
  647. // Frameworks are handled by cmOSXBundleGenerator.
  648. if(targetOutPath != targetOutPathReal && !this->Target->IsFrameworkOnApple())
  649. {
  650. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  651. symlink += targetOutPathReal;
  652. symlink += " ";
  653. symlink += targetOutPathSO;
  654. symlink += " ";
  655. symlink += targetOutPath;
  656. commands1.push_back(symlink);
  657. this->LocalGenerator->CreateCDCommand(commands1,
  658. this->Makefile->GetStartOutputDirectory(),
  659. cmLocalGenerator::HOME_OUTPUT);
  660. commands.insert(commands.end(), commands1.begin(), commands1.end());
  661. commands1.clear();
  662. }
  663. // Add the post-build rules when building but not when relinking.
  664. if(!relink)
  665. {
  666. this->LocalGenerator->
  667. AppendCustomCommands(commands, this->Target->GetPostBuildCommands(),
  668. this->Target);
  669. }
  670. // Write the build rule.
  671. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  672. targetFullPathReal.c_str(),
  673. depends, commands, false);
  674. // Some targets have more than one output file. Create rules to
  675. // drive the build if any extra outputs are missing.
  676. std::vector<std::string> extraOutputs;
  677. if(targetNameSO != targetNameReal)
  678. {
  679. this->GenerateExtraOutput(targetFullPathSO.c_str(),
  680. targetFullPathReal.c_str());
  681. }
  682. if(targetName != targetNameSO &&
  683. targetName != targetNameReal)
  684. {
  685. this->GenerateExtraOutput(targetFullPath.c_str(),
  686. targetFullPathReal.c_str());
  687. }
  688. // Write the main driver rule to build everything in this target.
  689. this->WriteTargetDriverRule(targetFullPath.c_str(), relink);
  690. // Clean all the possible library names and symlinks.
  691. this->CleanFiles.insert(this->CleanFiles.end(),
  692. libCleanFiles.begin(),libCleanFiles.end());
  693. }
  694. //----------------------------------------------------------------------------
  695. void
  696. cmMakefileLibraryTargetGenerator
  697. ::AppendOSXVerFlag(std::string& flags, const char* lang,
  698. const char* name, bool so)
  699. {
  700. // Lookup the flag to specify the version.
  701. std::string fvar = "CMAKE_";
  702. fvar += lang;
  703. fvar += "_OSX_";
  704. fvar += name;
  705. fvar += "_VERSION_FLAG";
  706. const char* flag = this->Makefile->GetDefinition(fvar.c_str());
  707. // Skip if no such flag.
  708. if(!flag)
  709. {
  710. return;
  711. }
  712. // Lookup the target version information.
  713. int major;
  714. int minor;
  715. int patch;
  716. this->Target->GetTargetVersion(so, major, minor, patch);
  717. if(major > 0 || minor > 0 || patch > 0)
  718. {
  719. // Append the flag since a non-zero version is specified.
  720. cmOStringStream vflag;
  721. vflag << flag << major << "." << minor << "." << patch;
  722. this->LocalGenerator->AppendFlags(flags, vflag.str().c_str());
  723. }
  724. }