cmMakefileLibraryTargetGenerator.cxx 28 KB

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