cmMakefileLibraryTargetGenerator.cxx 27 KB

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