cmMakefileLibraryTargetGenerator.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. std::string linkLanguage =
  114. this->Target->GetLinkerLanguage(this->ConfigName);
  115. std::string linkRuleVar = "CMAKE_";
  116. linkRuleVar += linkLanguage;
  117. linkRuleVar += "_CREATE_STATIC_LIBRARY";
  118. if(this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION") &&
  119. this->Makefile->GetDefinition(linkRuleVar+"_IPO"))
  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, extraFlags, false);
  127. }
  128. //----------------------------------------------------------------------------
  129. void cmMakefileLibraryTargetGenerator::WriteSharedLibraryRules(bool relink)
  130. {
  131. if(this->Target->IsFrameworkOnApple())
  132. {
  133. this->WriteFrameworkRules(relink);
  134. return;
  135. }
  136. std::string linkLanguage =
  137. this->Target->GetLinkerLanguage(this->ConfigName);
  138. std::string linkRuleVar = "CMAKE_";
  139. linkRuleVar += linkLanguage;
  140. linkRuleVar += "_CREATE_SHARED_LIBRARY";
  141. std::string extraFlags;
  142. this->LocalGenerator->AppendFlags
  143. (extraFlags, this->Target->GetProperty("LINK_FLAGS"));
  144. std::string linkFlagsConfig = "LINK_FLAGS_";
  145. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  146. this->LocalGenerator->AppendFlags
  147. (extraFlags, this->Target->GetProperty(linkFlagsConfig));
  148. this->LocalGenerator->AddConfigVariableFlags
  149. (extraFlags, "CMAKE_SHARED_LINKER_FLAGS", this->ConfigName);
  150. this->AddModuleDefinitionFlag(extraFlags);
  151. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  152. }
  153. //----------------------------------------------------------------------------
  154. void cmMakefileLibraryTargetGenerator::WriteModuleLibraryRules(bool relink)
  155. {
  156. std::string linkLanguage =
  157. this->Target->GetLinkerLanguage(this->ConfigName);
  158. std::string linkRuleVar = "CMAKE_";
  159. linkRuleVar += linkLanguage;
  160. linkRuleVar += "_CREATE_SHARED_MODULE";
  161. std::string extraFlags;
  162. this->LocalGenerator->AppendFlags(extraFlags,
  163. this->Target->GetProperty("LINK_FLAGS"));
  164. std::string linkFlagsConfig = "LINK_FLAGS_";
  165. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  166. this->LocalGenerator->AppendFlags
  167. (extraFlags, this->Target->GetProperty(linkFlagsConfig));
  168. this->LocalGenerator->AddConfigVariableFlags
  169. (extraFlags, "CMAKE_MODULE_LINKER_FLAGS", this->ConfigName);
  170. this->AddModuleDefinitionFlag(extraFlags);
  171. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  172. }
  173. //----------------------------------------------------------------------------
  174. void cmMakefileLibraryTargetGenerator::WriteFrameworkRules(bool relink)
  175. {
  176. std::string linkLanguage =
  177. this->Target->GetLinkerLanguage(this->ConfigName);
  178. std::string linkRuleVar = "CMAKE_";
  179. linkRuleVar += linkLanguage;
  180. linkRuleVar += "_CREATE_MACOSX_FRAMEWORK";
  181. std::string extraFlags;
  182. this->LocalGenerator->AppendFlags(extraFlags,
  183. this->Target->GetProperty("LINK_FLAGS"));
  184. std::string linkFlagsConfig = "LINK_FLAGS_";
  185. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  186. this->LocalGenerator->AppendFlags
  187. (extraFlags, this->Target->GetProperty(linkFlagsConfig));
  188. this->LocalGenerator->AddConfigVariableFlags
  189. (extraFlags, "CMAKE_MACOSX_FRAMEWORK_LINKER_FLAGS", this->ConfigName);
  190. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  191. }
  192. //----------------------------------------------------------------------------
  193. void cmMakefileLibraryTargetGenerator::WriteLibraryRules
  194. (const std::string& linkRuleVar, const std::string& extraFlags, bool relink)
  195. {
  196. // TODO: Merge the methods that call this method to avoid
  197. // code duplication.
  198. std::vector<std::string> commands;
  199. // Build list of dependencies.
  200. std::vector<std::string> depends;
  201. this->AppendLinkDepends(depends);
  202. // Get the language to use for linking this library.
  203. std::string linkLanguage =
  204. this->Target->GetLinkerLanguage(this->ConfigName);
  205. // Make sure we have a link language.
  206. if(linkLanguage.empty())
  207. {
  208. cmSystemTools::Error("Cannot determine link language for target \"",
  209. this->Target->GetName().c_str(), "\".");
  210. return;
  211. }
  212. // Create set of linking flags.
  213. std::string linkFlags;
  214. this->LocalGenerator->AppendFlags(linkFlags, extraFlags);
  215. // Add OSX version flags, if any.
  216. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  217. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  218. {
  219. this->AppendOSXVerFlag(linkFlags, linkLanguage, "COMPATIBILITY", true);
  220. this->AppendOSXVerFlag(linkFlags, linkLanguage, "CURRENT", false);
  221. }
  222. // Construct the name of the library.
  223. std::string targetName;
  224. std::string targetNameSO;
  225. std::string targetNameReal;
  226. std::string targetNameImport;
  227. std::string targetNamePDB;
  228. this->Target->GetLibraryNames(
  229. targetName, targetNameSO, targetNameReal, targetNameImport, targetNamePDB,
  230. this->ConfigName);
  231. // Construct the full path version of the names.
  232. std::string outpath;
  233. std::string outpathImp;
  234. if(this->Target->IsFrameworkOnApple())
  235. {
  236. outpath = this->Target->GetDirectory(this->ConfigName);
  237. this->OSXBundleGenerator->CreateFramework(targetName, outpath);
  238. outpath += "/";
  239. }
  240. else if(this->Target->IsCFBundleOnApple())
  241. {
  242. outpath = this->Target->GetDirectory(this->ConfigName);
  243. this->OSXBundleGenerator->CreateCFBundle(targetName, outpath);
  244. outpath += "/";
  245. }
  246. else if(relink)
  247. {
  248. outpath = this->Makefile->GetStartOutputDirectory();
  249. outpath += cmake::GetCMakeFilesDirectory();
  250. outpath += "/CMakeRelink.dir";
  251. cmSystemTools::MakeDirectory(outpath.c_str());
  252. outpath += "/";
  253. if(!targetNameImport.empty())
  254. {
  255. outpathImp = outpath;
  256. }
  257. }
  258. else
  259. {
  260. outpath = this->Target->GetDirectory(this->ConfigName);
  261. cmSystemTools::MakeDirectory(outpath.c_str());
  262. outpath += "/";
  263. if(!targetNameImport.empty())
  264. {
  265. outpathImp = this->Target->GetDirectory(this->ConfigName, true);
  266. cmSystemTools::MakeDirectory(outpathImp.c_str());
  267. outpathImp += "/";
  268. }
  269. }
  270. std::string compilePdbOutputPath =
  271. this->Target->GetCompilePDBDirectory(this->ConfigName);
  272. cmSystemTools::MakeDirectory(compilePdbOutputPath.c_str());
  273. std::string pdbOutputPath = this->Target->GetPDBDirectory(this->ConfigName);
  274. cmSystemTools::MakeDirectory(pdbOutputPath.c_str());
  275. pdbOutputPath += "/";
  276. std::string targetFullPath = outpath + targetName;
  277. std::string targetFullPathPDB = pdbOutputPath + targetNamePDB;
  278. std::string targetFullPathSO = outpath + targetNameSO;
  279. std::string targetFullPathReal = outpath + targetNameReal;
  280. std::string targetFullPathImport = outpathImp + targetNameImport;
  281. // Construct the output path version of the names for use in command
  282. // arguments.
  283. std::string targetOutPathPDB =
  284. this->Convert(targetFullPathPDB,cmLocalGenerator::NONE,
  285. cmLocalGenerator::SHELL);
  286. std::string targetOutPath =
  287. this->Convert(targetFullPath,cmLocalGenerator::START_OUTPUT,
  288. cmLocalGenerator::SHELL);
  289. std::string targetOutPathSO =
  290. this->Convert(targetFullPathSO,cmLocalGenerator::START_OUTPUT,
  291. cmLocalGenerator::SHELL);
  292. std::string targetOutPathReal =
  293. this->Convert(targetFullPathReal,cmLocalGenerator::START_OUTPUT,
  294. cmLocalGenerator::SHELL);
  295. std::string targetOutPathImport =
  296. this->Convert(targetFullPathImport,cmLocalGenerator::START_OUTPUT,
  297. cmLocalGenerator::SHELL);
  298. this->NumberOfProgressActions++;
  299. if(!this->NoRuleMessages)
  300. {
  301. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  302. this->MakeEchoProgress(progress);
  303. // Add the link message.
  304. std::string buildEcho = "Linking ";
  305. buildEcho += linkLanguage;
  306. switch(this->Target->GetType())
  307. {
  308. case cmTarget::STATIC_LIBRARY:
  309. buildEcho += " static library ";
  310. break;
  311. case cmTarget::SHARED_LIBRARY:
  312. buildEcho += " shared library ";
  313. break;
  314. case cmTarget::MODULE_LIBRARY:
  315. if (this->Target->IsCFBundleOnApple())
  316. buildEcho += " CFBundle";
  317. buildEcho += " shared module ";
  318. break;
  319. default:
  320. buildEcho += " library ";
  321. break;
  322. }
  323. buildEcho += targetOutPath.c_str();
  324. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  325. cmLocalUnixMakefileGenerator3::EchoLink,
  326. &progress);
  327. }
  328. const char* forbiddenFlagVar = 0;
  329. switch(this->Target->GetType())
  330. {
  331. case cmTarget::SHARED_LIBRARY:
  332. forbiddenFlagVar = "_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS";
  333. break;
  334. case cmTarget::MODULE_LIBRARY:
  335. forbiddenFlagVar = "_CREATE_SHARED_MODULE_FORBIDDEN_FLAGS";
  336. break;
  337. default: break;
  338. }
  339. // Clean files associated with this library.
  340. std::vector<std::string> libCleanFiles;
  341. libCleanFiles.push_back(this->Convert(targetFullPath,
  342. cmLocalGenerator::START_OUTPUT,
  343. cmLocalGenerator::UNCHANGED));
  344. if(targetNameReal != targetName)
  345. {
  346. libCleanFiles.push_back(this->Convert(targetFullPathReal,
  347. cmLocalGenerator::START_OUTPUT,
  348. cmLocalGenerator::UNCHANGED));
  349. }
  350. if(targetNameSO != targetName &&
  351. targetNameSO != targetNameReal)
  352. {
  353. libCleanFiles.push_back(this->Convert(targetFullPathSO,
  354. cmLocalGenerator::START_OUTPUT,
  355. cmLocalGenerator::UNCHANGED));
  356. }
  357. if(!targetNameImport.empty())
  358. {
  359. libCleanFiles.push_back(this->Convert(targetFullPathImport,
  360. cmLocalGenerator::START_OUTPUT,
  361. cmLocalGenerator::UNCHANGED));
  362. std::string implib;
  363. if(this->Target->GetImplibGNUtoMS(targetFullPathImport, implib))
  364. {
  365. libCleanFiles.push_back(this->Convert(implib,
  366. cmLocalGenerator::START_OUTPUT,
  367. cmLocalGenerator::UNCHANGED));
  368. }
  369. }
  370. // List the PDB for cleaning only when the whole target is
  371. // cleaned. We do not want to delete the .pdb file just before
  372. // linking the target.
  373. this->CleanFiles.push_back
  374. (this->Convert(targetFullPathPDB,
  375. cmLocalGenerator::START_OUTPUT,
  376. cmLocalGenerator::UNCHANGED));
  377. #ifdef _WIN32
  378. // There may be a manifest file for this target. Add it to the
  379. // clean set just in case.
  380. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  381. {
  382. libCleanFiles.push_back(
  383. this->Convert((targetFullPath+".manifest").c_str(),
  384. cmLocalGenerator::START_OUTPUT,
  385. cmLocalGenerator::UNCHANGED));
  386. }
  387. #endif
  388. std::vector<std::string> commands1;
  389. // Add a command to remove any existing files for this library.
  390. // for static libs only
  391. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  392. {
  393. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  394. *this->Target, "target");
  395. this->LocalGenerator->CreateCDCommand
  396. (commands1,
  397. this->Makefile->GetStartOutputDirectory(),
  398. cmLocalGenerator::HOME_OUTPUT);
  399. commands.insert(commands.end(), commands1.begin(), commands1.end());
  400. commands1.clear();
  401. }
  402. // Add the pre-build and pre-link rules building but not when relinking.
  403. if(!relink)
  404. {
  405. this->LocalGenerator
  406. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands(),
  407. this->Target);
  408. this->LocalGenerator
  409. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands(),
  410. this->Target);
  411. }
  412. // Determine whether a link script will be used.
  413. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  414. // Select whether to use a response file for objects.
  415. bool useResponseFileForObjects = false;
  416. {
  417. std::string responseVar = "CMAKE_";
  418. responseVar += linkLanguage;
  419. responseVar += "_USE_RESPONSE_FILE_FOR_OBJECTS";
  420. if(this->Makefile->IsOn(responseVar))
  421. {
  422. useResponseFileForObjects = true;
  423. }
  424. }
  425. // Select whether to use a response file for libraries.
  426. bool useResponseFileForLibs = false;
  427. {
  428. std::string responseVar = "CMAKE_";
  429. responseVar += linkLanguage;
  430. responseVar += "_USE_RESPONSE_FILE_FOR_LIBRARIES";
  431. if(this->Makefile->IsOn(responseVar))
  432. {
  433. useResponseFileForLibs = true;
  434. }
  435. }
  436. // For static libraries there might be archiving rules.
  437. bool haveStaticLibraryRule = false;
  438. std::vector<std::string> archiveCreateCommands;
  439. std::vector<std::string> archiveAppendCommands;
  440. std::vector<std::string> archiveFinishCommands;
  441. std::string::size_type archiveCommandLimit = std::string::npos;
  442. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  443. {
  444. haveStaticLibraryRule =
  445. this->Makefile->GetDefinition(linkRuleVar)? true:false;
  446. std::string arCreateVar = "CMAKE_";
  447. arCreateVar += linkLanguage;
  448. arCreateVar += "_ARCHIVE_CREATE";
  449. if(const char* rule = this->Makefile->GetDefinition(arCreateVar))
  450. {
  451. cmSystemTools::ExpandListArgument(rule, archiveCreateCommands);
  452. }
  453. std::string arAppendVar = "CMAKE_";
  454. arAppendVar += linkLanguage;
  455. arAppendVar += "_ARCHIVE_APPEND";
  456. if(const char* rule = this->Makefile->GetDefinition(arAppendVar))
  457. {
  458. cmSystemTools::ExpandListArgument(rule, archiveAppendCommands);
  459. }
  460. std::string arFinishVar = "CMAKE_";
  461. arFinishVar += linkLanguage;
  462. arFinishVar += "_ARCHIVE_FINISH";
  463. if(const char* rule = this->Makefile->GetDefinition(arFinishVar))
  464. {
  465. cmSystemTools::ExpandListArgument(rule, archiveFinishCommands);
  466. }
  467. }
  468. // Decide whether to use archiving rules.
  469. bool useArchiveRules =
  470. !haveStaticLibraryRule &&
  471. !archiveCreateCommands.empty() && !archiveAppendCommands.empty();
  472. if(useArchiveRules)
  473. {
  474. // Archiving rules are always run with a link script.
  475. useLinkScript = true;
  476. // Archiving rules never use a response file.
  477. useResponseFileForObjects = false;
  478. // Limit the length of individual object lists to less than the
  479. // 32K command line length limit on Windows. We could make this a
  480. // platform file variable but this should work everywhere.
  481. archiveCommandLimit = 30000;
  482. }
  483. // Expand the rule variables.
  484. std::vector<std::string> real_link_commands;
  485. {
  486. bool useWatcomQuote = this->Makefile->IsOn(linkRuleVar+"_USE_WATCOM_QUOTE");
  487. // Set path conversion for link script shells.
  488. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  489. // Collect up flags to link in needed libraries.
  490. std::string linkLibs;
  491. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  492. {
  493. this->CreateLinkLibs(linkLibs, relink, useResponseFileForLibs, depends,
  494. useWatcomQuote);
  495. }
  496. // Construct object file lists that may be needed to expand the
  497. // rule.
  498. std::string buildObjs;
  499. this->CreateObjectLists(useLinkScript, useArchiveRules,
  500. useResponseFileForObjects, buildObjs, depends,
  501. useWatcomQuote);
  502. cmLocalGenerator::RuleVariables vars;
  503. vars.TargetPDB = targetOutPathPDB.c_str();
  504. // Setup the target version.
  505. std::string targetVersionMajor;
  506. std::string targetVersionMinor;
  507. {
  508. std::ostringstream majorStream;
  509. std::ostringstream minorStream;
  510. int major;
  511. int minor;
  512. this->Target->GetTargetVersion(major, minor);
  513. majorStream << major;
  514. minorStream << minor;
  515. targetVersionMajor = majorStream.str();
  516. targetVersionMinor = minorStream.str();
  517. }
  518. vars.TargetVersionMajor = targetVersionMajor.c_str();
  519. vars.TargetVersionMinor = targetVersionMinor.c_str();
  520. vars.RuleLauncher = "RULE_LAUNCH_LINK";
  521. vars.CMTarget = this->Target;
  522. vars.Language = linkLanguage.c_str();
  523. vars.Objects = buildObjs.c_str();
  524. std::string objectDir = this->Target->GetSupportDirectory();
  525. objectDir = this->Convert(objectDir,
  526. cmLocalGenerator::START_OUTPUT,
  527. cmLocalGenerator::SHELL);
  528. vars.ObjectDir = objectDir.c_str();
  529. cmLocalGenerator::OutputFormat output = (useWatcomQuote) ?
  530. cmLocalGenerator::WATCOMQUOTE : cmLocalGenerator::SHELL;
  531. std::string target = this->Convert(targetFullPathReal,
  532. cmLocalGenerator::START_OUTPUT,
  533. output);
  534. vars.Target = target.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,
  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. // Compute the list of outputs.
  682. std::vector<std::string> outputs(1, targetFullPathReal);
  683. if(targetNameSO != targetNameReal)
  684. {
  685. outputs.push_back(targetFullPathSO);
  686. }
  687. if(targetName != targetNameSO &&
  688. targetName != targetNameReal)
  689. {
  690. outputs.push_back(targetFullPath);
  691. }
  692. // Write the build rule.
  693. this->WriteMakeRule(*this->BuildFileStream, 0, outputs,
  694. depends, commands, false);
  695. // Write the main driver rule to build everything in this target.
  696. this->WriteTargetDriverRule(targetFullPath, relink);
  697. // Clean all the possible library names and symlinks.
  698. this->CleanFiles.insert(this->CleanFiles.end(),
  699. libCleanFiles.begin(),libCleanFiles.end());
  700. }
  701. //----------------------------------------------------------------------------
  702. void
  703. cmMakefileLibraryTargetGenerator
  704. ::AppendOSXVerFlag(std::string& flags, const std::string& lang,
  705. const char* name, bool so)
  706. {
  707. // Lookup the flag to specify the version.
  708. std::string fvar = "CMAKE_";
  709. fvar += lang;
  710. fvar += "_OSX_";
  711. fvar += name;
  712. fvar += "_VERSION_FLAG";
  713. const char* flag = this->Makefile->GetDefinition(fvar);
  714. // Skip if no such flag.
  715. if(!flag)
  716. {
  717. return;
  718. }
  719. // Lookup the target version information.
  720. int major;
  721. int minor;
  722. int patch;
  723. this->Target->GetTargetVersion(so, major, minor, patch);
  724. if(major > 0 || minor > 0 || patch > 0)
  725. {
  726. // Append the flag since a non-zero version is specified.
  727. std::ostringstream vflag;
  728. vflag << flag << major << "." << minor << "." << patch;
  729. this->LocalGenerator->AppendFlags(flags, vflag.str());
  730. }
  731. }