cmMakefileLibraryTargetGenerator.cxx 27 KB

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