cmMakefileLibraryTargetGenerator.cxx 27 KB

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