cmMakefileLibraryTargetGenerator.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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 targetFullPath = outpath + targetName;
  288. std::string targetFullPathPDB = outpath + targetNamePDB;
  289. std::string targetFullPathSO = outpath + targetNameSO;
  290. std::string targetFullPathReal = outpath + targetNameReal;
  291. std::string targetFullPathImport = outpathImp + targetNameImport;
  292. // Construct the output path version of the names for use in command
  293. // arguments.
  294. std::string targetOutPathPDB =
  295. this->Convert(targetFullPathPDB.c_str(),cmLocalGenerator::NONE,
  296. cmLocalGenerator::SHELL);
  297. std::string targetOutPath =
  298. this->Convert(targetFullPath.c_str(),cmLocalGenerator::START_OUTPUT,
  299. cmLocalGenerator::SHELL);
  300. std::string targetOutPathSO =
  301. this->Convert(targetFullPathSO.c_str(),cmLocalGenerator::START_OUTPUT,
  302. cmLocalGenerator::SHELL);
  303. std::string targetOutPathReal =
  304. this->Convert(targetFullPathReal.c_str(),cmLocalGenerator::START_OUTPUT,
  305. cmLocalGenerator::SHELL);
  306. std::string targetOutPathImport =
  307. this->Convert(targetFullPathImport.c_str(),cmLocalGenerator::START_OUTPUT,
  308. cmLocalGenerator::SHELL);
  309. if(!this->NoRuleMessages)
  310. {
  311. // Add the link message.
  312. std::string buildEcho = "Linking ";
  313. buildEcho += linkLanguage;
  314. switch(this->Target->GetType())
  315. {
  316. case cmTarget::STATIC_LIBRARY:
  317. buildEcho += " static library ";
  318. break;
  319. case cmTarget::SHARED_LIBRARY:
  320. buildEcho += " shared library ";
  321. break;
  322. case cmTarget::MODULE_LIBRARY:
  323. if (this->Target->IsCFBundleOnApple())
  324. buildEcho += " CFBundle";
  325. buildEcho += " shared module ";
  326. break;
  327. default:
  328. buildEcho += " library ";
  329. break;
  330. }
  331. buildEcho += targetOutPath.c_str();
  332. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  333. cmLocalUnixMakefileGenerator3::EchoLink);
  334. }
  335. const char* forbiddenFlagVar = 0;
  336. switch(this->Target->GetType())
  337. {
  338. case cmTarget::SHARED_LIBRARY:
  339. forbiddenFlagVar = "_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS";
  340. break;
  341. case cmTarget::MODULE_LIBRARY:
  342. forbiddenFlagVar = "_CREATE_SHARED_MODULE_FORBIDDEN_FLAGS";
  343. break;
  344. default: break;
  345. }
  346. // Clean files associated with this library.
  347. std::vector<std::string> libCleanFiles;
  348. libCleanFiles.push_back(this->Convert(targetFullPath.c_str(),
  349. cmLocalGenerator::START_OUTPUT,
  350. cmLocalGenerator::UNCHANGED));
  351. if(targetNameReal != targetName)
  352. {
  353. libCleanFiles.push_back(this->Convert(targetFullPathReal.c_str(),
  354. cmLocalGenerator::START_OUTPUT,
  355. cmLocalGenerator::UNCHANGED));
  356. }
  357. if(targetNameSO != targetName &&
  358. targetNameSO != targetNameReal)
  359. {
  360. libCleanFiles.push_back(this->Convert(targetFullPathSO.c_str(),
  361. cmLocalGenerator::START_OUTPUT,
  362. cmLocalGenerator::UNCHANGED));
  363. }
  364. if(!targetNameImport.empty())
  365. {
  366. libCleanFiles.push_back(this->Convert(targetFullPathImport.c_str(),
  367. cmLocalGenerator::START_OUTPUT,
  368. cmLocalGenerator::UNCHANGED));
  369. std::string implib;
  370. if(this->Target->GetImplibGNUtoMS(targetFullPathImport, implib))
  371. {
  372. libCleanFiles.push_back(this->Convert(implib.c_str(),
  373. cmLocalGenerator::START_OUTPUT,
  374. cmLocalGenerator::UNCHANGED));
  375. }
  376. }
  377. // List the PDB for cleaning only when the whole target is
  378. // cleaned. We do not want to delete the .pdb file just before
  379. // linking the target.
  380. this->CleanFiles.push_back
  381. (this->Convert(targetFullPathPDB.c_str(),
  382. cmLocalGenerator::START_OUTPUT,
  383. cmLocalGenerator::UNCHANGED));
  384. #ifdef _WIN32
  385. // There may be a manifest file for this target. Add it to the
  386. // clean set just in case.
  387. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  388. {
  389. libCleanFiles.push_back(
  390. this->Convert((targetFullPath+".manifest").c_str(),
  391. cmLocalGenerator::START_OUTPUT,
  392. cmLocalGenerator::UNCHANGED));
  393. }
  394. #endif
  395. std::vector<std::string> commands1;
  396. // Add a command to remove any existing files for this library.
  397. // for static libs only
  398. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  399. {
  400. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  401. *this->Target, "target");
  402. this->LocalGenerator->CreateCDCommand
  403. (commands1,
  404. this->Makefile->GetStartOutputDirectory(),
  405. cmLocalGenerator::HOME_OUTPUT);
  406. commands.insert(commands.end(), commands1.begin(), commands1.end());
  407. commands1.clear();
  408. }
  409. // Add the pre-build and pre-link rules building but not when relinking.
  410. if(!relink)
  411. {
  412. this->LocalGenerator
  413. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands(),
  414. this->Target);
  415. this->LocalGenerator
  416. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands(),
  417. this->Target);
  418. }
  419. // Determine whether a link script will be used.
  420. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  421. // Select whether to use a response file for objects.
  422. bool useResponseFile = false;
  423. {
  424. std::string responseVar = "CMAKE_";
  425. responseVar += linkLanguage;
  426. responseVar += "_USE_RESPONSE_FILE_FOR_OBJECTS";
  427. if(this->Makefile->IsOn(responseVar.c_str()))
  428. {
  429. useResponseFile = true;
  430. }
  431. }
  432. // For static libraries there might be archiving rules.
  433. bool haveStaticLibraryRule = false;
  434. std::vector<std::string> archiveCreateCommands;
  435. std::vector<std::string> archiveAppendCommands;
  436. std::vector<std::string> archiveFinishCommands;
  437. std::string::size_type archiveCommandLimit = std::string::npos;
  438. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  439. {
  440. haveStaticLibraryRule =
  441. this->Makefile->GetDefinition(linkRuleVar)? true:false;
  442. std::string arCreateVar = "CMAKE_";
  443. arCreateVar += linkLanguage;
  444. arCreateVar += "_ARCHIVE_CREATE";
  445. if(const char* rule = this->Makefile->GetDefinition(arCreateVar.c_str()))
  446. {
  447. cmSystemTools::ExpandListArgument(rule, archiveCreateCommands);
  448. }
  449. std::string arAppendVar = "CMAKE_";
  450. arAppendVar += linkLanguage;
  451. arAppendVar += "_ARCHIVE_APPEND";
  452. if(const char* rule = this->Makefile->GetDefinition(arAppendVar.c_str()))
  453. {
  454. cmSystemTools::ExpandListArgument(rule, archiveAppendCommands);
  455. }
  456. std::string arFinishVar = "CMAKE_";
  457. arFinishVar += linkLanguage;
  458. arFinishVar += "_ARCHIVE_FINISH";
  459. if(const char* rule = this->Makefile->GetDefinition(arFinishVar.c_str()))
  460. {
  461. cmSystemTools::ExpandListArgument(rule, archiveFinishCommands);
  462. }
  463. }
  464. // Decide whether to use archiving rules.
  465. bool useArchiveRules =
  466. !haveStaticLibraryRule &&
  467. !archiveCreateCommands.empty() && !archiveAppendCommands.empty();
  468. if(useArchiveRules)
  469. {
  470. // Archiving rules are always run with a link script.
  471. useLinkScript = true;
  472. // Archiving rules never use a response file.
  473. useResponseFile = false;
  474. // Limit the length of individual object lists to less than the
  475. // 32K command line length limit on Windows. We could make this a
  476. // platform file variable but this should work everywhere.
  477. archiveCommandLimit = 30000;
  478. }
  479. // Expand the rule variables.
  480. std::vector<std::string> real_link_commands;
  481. {
  482. // Set path conversion for link script shells.
  483. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  484. // Collect up flags to link in needed libraries.
  485. cmOStringStream linklibs;
  486. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  487. {
  488. this->LocalGenerator
  489. ->OutputLinkLibraries(linklibs, *this->GeneratorTarget, relink);
  490. }
  491. // Construct object file lists that may be needed to expand the
  492. // rule.
  493. std::string buildObjs;
  494. this->CreateObjectLists(useLinkScript, useArchiveRules, useResponseFile,
  495. 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 objdir = cmake::GetCMakeFilesDirectoryPostSlash();
  519. objdir += this->Target->GetName();
  520. objdir += ".dir";
  521. objdir = this->Convert(objdir.c_str(),
  522. cmLocalGenerator::START_OUTPUT,
  523. cmLocalGenerator::SHELL);
  524. vars.ObjectDir = objdir.c_str();
  525. vars.Target = targetOutPathReal.c_str();
  526. std::string linkString = linklibs.str();
  527. vars.LinkLibraries = linkString.c_str();
  528. vars.ObjectsQuoted = buildObjs.c_str();
  529. if (this->Target->HasSOName(this->ConfigName))
  530. {
  531. vars.SONameFlag = this->Makefile->GetSONameFlag(linkLanguage);
  532. vars.TargetSOName= targetNameSO.c_str();
  533. }
  534. vars.LinkFlags = linkFlags.c_str();
  535. // Compute the directory portion of the install_name setting.
  536. std::string install_name_dir;
  537. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY)
  538. {
  539. // Get the install_name directory for the build tree.
  540. install_name_dir =
  541. this->Target->GetInstallNameDirForBuildTree(this->ConfigName);
  542. // Set the rule variable replacement value.
  543. if(install_name_dir.empty())
  544. {
  545. vars.TargetInstallNameDir = "";
  546. }
  547. else
  548. {
  549. // Convert to a path for the native build tool.
  550. install_name_dir =
  551. this->LocalGenerator->Convert(install_name_dir.c_str(),
  552. cmLocalGenerator::NONE,
  553. cmLocalGenerator::SHELL, false);
  554. vars.TargetInstallNameDir = install_name_dir.c_str();
  555. }
  556. }
  557. // Add language feature flags.
  558. std::string langFlags;
  559. this->AddFeatureFlags(langFlags, linkLanguage);
  560. this->LocalGenerator->AddArchitectureFlags(langFlags, this->GeneratorTarget,
  561. linkLanguage, this->ConfigName);
  562. // remove any language flags that might not work with the
  563. // particular os
  564. if(forbiddenFlagVar)
  565. {
  566. this->RemoveForbiddenFlags(forbiddenFlagVar,
  567. linkLanguage, langFlags);
  568. }
  569. vars.LanguageCompileFlags = langFlags.c_str();
  570. // Construct the main link rule and expand placeholders.
  571. this->LocalGenerator->TargetImplib = targetOutPathImport;
  572. if(useArchiveRules)
  573. {
  574. // Construct the individual object list strings.
  575. std::vector<std::string> object_strings;
  576. this->WriteObjectsStrings(object_strings, archiveCommandLimit);
  577. // Create the archive with the first set of objects.
  578. std::vector<std::string>::iterator osi = object_strings.begin();
  579. {
  580. vars.Objects = osi->c_str();
  581. for(std::vector<std::string>::const_iterator
  582. i = archiveCreateCommands.begin();
  583. i != archiveCreateCommands.end(); ++i)
  584. {
  585. std::string cmd = *i;
  586. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  587. real_link_commands.push_back(cmd);
  588. }
  589. }
  590. // Append to the archive with the other object sets.
  591. for(++osi; osi != object_strings.end(); ++osi)
  592. {
  593. vars.Objects = osi->c_str();
  594. for(std::vector<std::string>::const_iterator
  595. i = archiveAppendCommands.begin();
  596. i != archiveAppendCommands.end(); ++i)
  597. {
  598. std::string cmd = *i;
  599. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  600. real_link_commands.push_back(cmd);
  601. }
  602. }
  603. // Finish the archive.
  604. vars.Objects = "";
  605. for(std::vector<std::string>::const_iterator
  606. i = archiveFinishCommands.begin();
  607. i != archiveFinishCommands.end(); ++i)
  608. {
  609. std::string cmd = *i;
  610. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  611. real_link_commands.push_back(cmd);
  612. }
  613. }
  614. else
  615. {
  616. // Get the set of commands.
  617. std::string linkRule = this->GetLinkRule(linkRuleVar);
  618. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  619. // Expand placeholders.
  620. for(std::vector<std::string>::iterator i = real_link_commands.begin();
  621. i != real_link_commands.end(); ++i)
  622. {
  623. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  624. }
  625. }
  626. this->LocalGenerator->TargetImplib = "";
  627. // Restore path conversion to normal shells.
  628. this->LocalGenerator->SetLinkScriptShell(false);
  629. }
  630. // Optionally convert the build rule to use a script to avoid long
  631. // command lines in the make shell.
  632. if(useLinkScript)
  633. {
  634. // Use a link script.
  635. const char* name = (relink? "relink.txt" : "link.txt");
  636. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  637. }
  638. else
  639. {
  640. // No link script. Just use the link rule directly.
  641. commands1 = real_link_commands;
  642. }
  643. this->LocalGenerator->CreateCDCommand
  644. (commands1,
  645. this->Makefile->GetStartOutputDirectory(),
  646. cmLocalGenerator::HOME_OUTPUT);
  647. commands.insert(commands.end(), commands1.begin(), commands1.end());
  648. commands1.clear();
  649. // Add a rule to create necessary symlinks for the library.
  650. if(targetOutPath != targetOutPathReal)
  651. {
  652. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  653. symlink += targetOutPathReal;
  654. symlink += " ";
  655. symlink += targetOutPathSO;
  656. symlink += " ";
  657. symlink += targetOutPath;
  658. commands1.push_back(symlink);
  659. this->LocalGenerator->CreateCDCommand(commands1,
  660. this->Makefile->GetStartOutputDirectory(),
  661. cmLocalGenerator::HOME_OUTPUT);
  662. commands.insert(commands.end(), commands1.begin(), commands1.end());
  663. commands1.clear();
  664. }
  665. // Add the post-build rules when building but not when relinking.
  666. if(!relink)
  667. {
  668. this->LocalGenerator->
  669. AppendCustomCommands(commands, this->Target->GetPostBuildCommands(),
  670. this->Target);
  671. }
  672. // Write the build rule.
  673. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  674. targetFullPathReal.c_str(),
  675. depends, commands, false);
  676. // Some targets have more than one output file. Create rules to
  677. // drive the build if any extra outputs are missing.
  678. std::vector<std::string> extraOutputs;
  679. if(targetNameSO != targetNameReal)
  680. {
  681. this->GenerateExtraOutput(targetFullPathSO.c_str(),
  682. targetFullPathReal.c_str());
  683. }
  684. if(targetName != targetNameSO &&
  685. targetName != targetNameReal)
  686. {
  687. this->GenerateExtraOutput(targetFullPath.c_str(),
  688. targetFullPathReal.c_str());
  689. }
  690. // Write the main driver rule to build everything in this target.
  691. this->WriteTargetDriverRule(targetFullPath.c_str(), relink);
  692. // Clean all the possible library names and symlinks.
  693. this->CleanFiles.insert(this->CleanFiles.end(),
  694. libCleanFiles.begin(),libCleanFiles.end());
  695. }
  696. //----------------------------------------------------------------------------
  697. void
  698. cmMakefileLibraryTargetGenerator
  699. ::AppendOSXVerFlag(std::string& flags, const char* lang,
  700. const char* name, bool so)
  701. {
  702. // Lookup the flag to specify the version.
  703. std::string fvar = "CMAKE_";
  704. fvar += lang;
  705. fvar += "_OSX_";
  706. fvar += name;
  707. fvar += "_VERSION_FLAG";
  708. const char* flag = this->Makefile->GetDefinition(fvar.c_str());
  709. // Skip if no such flag.
  710. if(!flag)
  711. {
  712. return;
  713. }
  714. // Lookup the target version information.
  715. int major;
  716. int minor;
  717. int patch;
  718. this->Target->GetTargetVersion(so, major, minor, patch);
  719. if(major > 0 || minor > 0 || patch > 0)
  720. {
  721. // Append the flag since a non-zero version is specified.
  722. cmOStringStream vflag;
  723. vflag << flag << major << "." << minor << "." << patch;
  724. this->LocalGenerator->AppendFlags(flags, vflag.str().c_str());
  725. }
  726. }