cmMakefileLibraryTargetGenerator.cxx 27 KB

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