cmMakefileLibraryTargetGenerator.cxx 27 KB

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