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. case cmTarget::INTERFACE_LIBRARY:
  78. // Nothing to do.
  79. break;
  80. default:
  81. // If language is not known, this is an error.
  82. cmSystemTools::Error("Unknown Library Type");
  83. break;
  84. }
  85. // Write the requires target.
  86. this->WriteTargetRequiresRules();
  87. // Write clean target
  88. this->WriteTargetCleanRules();
  89. // Write the dependency generation rule. This must be done last so
  90. // that multiple output pair information is available.
  91. this->WriteTargetDependRules();
  92. // close the streams
  93. this->CloseFileStreams();
  94. }
  95. //----------------------------------------------------------------------------
  96. void cmMakefileLibraryTargetGenerator::WriteObjectLibraryRules()
  97. {
  98. std::vector<std::string> commands;
  99. std::vector<std::string> depends;
  100. // Add post-build rules.
  101. this->LocalGenerator->
  102. AppendCustomCommands(commands, this->Target->GetPostBuildCommands(),
  103. this->Target);
  104. // Depend on the object files.
  105. this->AppendObjectDepends(depends);
  106. // Write the rule.
  107. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  108. this->Target->GetName(),
  109. depends, commands, true);
  110. // Write the main driver rule to build everything in this target.
  111. this->WriteTargetDriverRule(this->Target->GetName(), false);
  112. }
  113. //----------------------------------------------------------------------------
  114. void cmMakefileLibraryTargetGenerator::WriteStaticLibraryRules()
  115. {
  116. const char* linkLanguage =
  117. this->Target->GetLinkerLanguage(this->ConfigName);
  118. std::string linkRuleVar = "CMAKE_";
  119. if (linkLanguage)
  120. {
  121. linkRuleVar += linkLanguage;
  122. }
  123. linkRuleVar += "_CREATE_STATIC_LIBRARY";
  124. if(this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION") &&
  125. this->Makefile->GetDefinition((linkRuleVar+"_IPO").c_str()))
  126. {
  127. linkRuleVar += "_IPO";
  128. }
  129. std::string extraFlags;
  130. this->LocalGenerator->GetStaticLibraryFlags(extraFlags,
  131. cmSystemTools::UpperCase(this->ConfigName), this->Target);
  132. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), false);
  133. }
  134. //----------------------------------------------------------------------------
  135. void cmMakefileLibraryTargetGenerator::WriteSharedLibraryRules(bool relink)
  136. {
  137. if(this->Target->IsFrameworkOnApple())
  138. {
  139. this->WriteFrameworkRules(relink);
  140. return;
  141. }
  142. const char* linkLanguage =
  143. this->Target->GetLinkerLanguage(this->ConfigName);
  144. std::string linkRuleVar = "CMAKE_";
  145. if (linkLanguage)
  146. {
  147. linkRuleVar += linkLanguage;
  148. }
  149. linkRuleVar += "_CREATE_SHARED_LIBRARY";
  150. std::string extraFlags;
  151. this->LocalGenerator->AppendFlags
  152. (extraFlags, this->Target->GetProperty("LINK_FLAGS"));
  153. std::string linkFlagsConfig = "LINK_FLAGS_";
  154. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  155. this->LocalGenerator->AppendFlags
  156. (extraFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  157. this->LocalGenerator->AddConfigVariableFlags
  158. (extraFlags, "CMAKE_SHARED_LINKER_FLAGS", this->ConfigName);
  159. this->AddModuleDefinitionFlag(extraFlags);
  160. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), relink);
  161. }
  162. //----------------------------------------------------------------------------
  163. void cmMakefileLibraryTargetGenerator::WriteModuleLibraryRules(bool relink)
  164. {
  165. const char* linkLanguage =
  166. this->Target->GetLinkerLanguage(this->ConfigName);
  167. std::string linkRuleVar = "CMAKE_";
  168. if (linkLanguage)
  169. {
  170. linkRuleVar += linkLanguage;
  171. }
  172. linkRuleVar += "_CREATE_SHARED_MODULE";
  173. std::string extraFlags;
  174. this->LocalGenerator->AppendFlags(extraFlags,
  175. this->Target->GetProperty("LINK_FLAGS"));
  176. std::string linkFlagsConfig = "LINK_FLAGS_";
  177. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  178. this->LocalGenerator->AppendFlags
  179. (extraFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  180. this->LocalGenerator->AddConfigVariableFlags
  181. (extraFlags, "CMAKE_MODULE_LINKER_FLAGS", this->ConfigName);
  182. this->AddModuleDefinitionFlag(extraFlags);
  183. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), relink);
  184. }
  185. //----------------------------------------------------------------------------
  186. void cmMakefileLibraryTargetGenerator::WriteFrameworkRules(bool relink)
  187. {
  188. const char* linkLanguage =
  189. this->Target->GetLinkerLanguage(this->ConfigName);
  190. std::string linkRuleVar = "CMAKE_";
  191. if (linkLanguage)
  192. {
  193. linkRuleVar += linkLanguage;
  194. }
  195. linkRuleVar += "_CREATE_MACOSX_FRAMEWORK";
  196. std::string extraFlags;
  197. this->LocalGenerator->AppendFlags(extraFlags,
  198. this->Target->GetProperty("LINK_FLAGS"));
  199. std::string linkFlagsConfig = "LINK_FLAGS_";
  200. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  201. this->LocalGenerator->AppendFlags
  202. (extraFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  203. this->LocalGenerator->AddConfigVariableFlags
  204. (extraFlags, "CMAKE_MACOSX_FRAMEWORK_LINKER_FLAGS", this->ConfigName);
  205. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), relink);
  206. }
  207. //----------------------------------------------------------------------------
  208. void cmMakefileLibraryTargetGenerator::WriteLibraryRules
  209. (const char* linkRuleVar, const char* extraFlags, bool relink)
  210. {
  211. // TODO: Merge the methods that call this method to avoid
  212. // code duplication.
  213. std::vector<std::string> commands;
  214. // Build list of dependencies.
  215. std::vector<std::string> depends;
  216. this->AppendLinkDepends(depends);
  217. // Get the language to use for linking this library.
  218. const char* linkLanguage =
  219. this->Target->GetLinkerLanguage(this->ConfigName);
  220. // Make sure we have a link language.
  221. if(!linkLanguage)
  222. {
  223. cmSystemTools::Error("Cannot determine link language for target \"",
  224. this->Target->GetName(), "\".");
  225. return;
  226. }
  227. // Create set of linking flags.
  228. std::string linkFlags;
  229. this->LocalGenerator->AppendFlags(linkFlags, extraFlags);
  230. // Add OSX version flags, if any.
  231. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  232. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  233. {
  234. this->AppendOSXVerFlag(linkFlags, linkLanguage, "COMPATIBILITY", true);
  235. this->AppendOSXVerFlag(linkFlags, linkLanguage, "CURRENT", false);
  236. }
  237. // Construct the name of the library.
  238. std::string targetName;
  239. std::string targetNameSO;
  240. std::string targetNameReal;
  241. std::string targetNameImport;
  242. std::string targetNamePDB;
  243. this->Target->GetLibraryNames(
  244. targetName, targetNameSO, targetNameReal, targetNameImport, targetNamePDB,
  245. this->ConfigName);
  246. // Construct the full path version of the names.
  247. std::string outpath;
  248. std::string outpathImp;
  249. if(this->Target->IsFrameworkOnApple())
  250. {
  251. outpath = this->Target->GetDirectory(this->ConfigName);
  252. this->OSXBundleGenerator->CreateFramework(targetName, outpath);
  253. outpath += "/";
  254. }
  255. else if(this->Target->IsCFBundleOnApple())
  256. {
  257. outpath = this->Target->GetDirectory(this->ConfigName);
  258. this->OSXBundleGenerator->CreateCFBundle(targetName, outpath);
  259. outpath += "/";
  260. }
  261. else if(relink)
  262. {
  263. outpath = this->Makefile->GetStartOutputDirectory();
  264. outpath += cmake::GetCMakeFilesDirectory();
  265. outpath += "/CMakeRelink.dir";
  266. cmSystemTools::MakeDirectory(outpath.c_str());
  267. outpath += "/";
  268. if(!targetNameImport.empty())
  269. {
  270. outpathImp = outpath;
  271. }
  272. }
  273. else
  274. {
  275. outpath = this->Target->GetDirectory(this->ConfigName);
  276. cmSystemTools::MakeDirectory(outpath.c_str());
  277. outpath += "/";
  278. if(!targetNameImport.empty())
  279. {
  280. outpathImp = this->Target->GetDirectory(this->ConfigName, true);
  281. cmSystemTools::MakeDirectory(outpathImp.c_str());
  282. outpathImp += "/";
  283. }
  284. }
  285. std::string pdbOutputPath = this->Target->GetPDBDirectory();
  286. cmSystemTools::MakeDirectory(pdbOutputPath.c_str());
  287. pdbOutputPath += "/";
  288. std::string targetFullPath = outpath + targetName;
  289. std::string targetFullPathPDB = pdbOutputPath + targetNamePDB;
  290. std::string targetFullPathSO = outpath + targetNameSO;
  291. std::string targetFullPathReal = outpath + targetNameReal;
  292. std::string targetFullPathImport = outpathImp + targetNameImport;
  293. // Construct the output path version of the names for use in command
  294. // arguments.
  295. std::string targetOutPathPDB =
  296. this->Convert(targetFullPathPDB.c_str(),cmLocalGenerator::NONE,
  297. cmLocalGenerator::SHELL);
  298. std::string targetOutPath =
  299. this->Convert(targetFullPath.c_str(),cmLocalGenerator::START_OUTPUT,
  300. cmLocalGenerator::SHELL);
  301. std::string targetOutPathSO =
  302. this->Convert(targetFullPathSO.c_str(),cmLocalGenerator::START_OUTPUT,
  303. cmLocalGenerator::SHELL);
  304. std::string targetOutPathReal =
  305. this->Convert(targetFullPathReal.c_str(),cmLocalGenerator::START_OUTPUT,
  306. cmLocalGenerator::SHELL);
  307. std::string targetOutPathImport =
  308. this->Convert(targetFullPathImport.c_str(),cmLocalGenerator::START_OUTPUT,
  309. cmLocalGenerator::SHELL);
  310. if(!this->NoRuleMessages)
  311. {
  312. // Add the link message.
  313. std::string buildEcho = "Linking ";
  314. buildEcho += linkLanguage;
  315. switch(this->Target->GetType())
  316. {
  317. case cmTarget::STATIC_LIBRARY:
  318. buildEcho += " static library ";
  319. break;
  320. case cmTarget::SHARED_LIBRARY:
  321. buildEcho += " shared library ";
  322. break;
  323. case cmTarget::MODULE_LIBRARY:
  324. if (this->Target->IsCFBundleOnApple())
  325. buildEcho += " CFBundle";
  326. buildEcho += " shared module ";
  327. break;
  328. default:
  329. buildEcho += " library ";
  330. break;
  331. }
  332. buildEcho += targetOutPath.c_str();
  333. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  334. cmLocalUnixMakefileGenerator3::EchoLink);
  335. }
  336. const char* forbiddenFlagVar = 0;
  337. switch(this->Target->GetType())
  338. {
  339. case cmTarget::SHARED_LIBRARY:
  340. forbiddenFlagVar = "_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS";
  341. break;
  342. case cmTarget::MODULE_LIBRARY:
  343. forbiddenFlagVar = "_CREATE_SHARED_MODULE_FORBIDDEN_FLAGS";
  344. break;
  345. default: break;
  346. }
  347. // Clean files associated with this library.
  348. std::vector<std::string> libCleanFiles;
  349. libCleanFiles.push_back(this->Convert(targetFullPath.c_str(),
  350. cmLocalGenerator::START_OUTPUT,
  351. cmLocalGenerator::UNCHANGED));
  352. if(targetNameReal != targetName)
  353. {
  354. libCleanFiles.push_back(this->Convert(targetFullPathReal.c_str(),
  355. cmLocalGenerator::START_OUTPUT,
  356. cmLocalGenerator::UNCHANGED));
  357. }
  358. if(targetNameSO != targetName &&
  359. targetNameSO != targetNameReal)
  360. {
  361. libCleanFiles.push_back(this->Convert(targetFullPathSO.c_str(),
  362. cmLocalGenerator::START_OUTPUT,
  363. cmLocalGenerator::UNCHANGED));
  364. }
  365. if(!targetNameImport.empty())
  366. {
  367. libCleanFiles.push_back(this->Convert(targetFullPathImport.c_str(),
  368. cmLocalGenerator::START_OUTPUT,
  369. cmLocalGenerator::UNCHANGED));
  370. std::string implib;
  371. if(this->Target->GetImplibGNUtoMS(targetFullPathImport, implib))
  372. {
  373. libCleanFiles.push_back(this->Convert(implib.c_str(),
  374. cmLocalGenerator::START_OUTPUT,
  375. cmLocalGenerator::UNCHANGED));
  376. }
  377. }
  378. // List the PDB for cleaning only when the whole target is
  379. // cleaned. We do not want to delete the .pdb file just before
  380. // linking the target.
  381. this->CleanFiles.push_back
  382. (this->Convert(targetFullPathPDB.c_str(),
  383. cmLocalGenerator::START_OUTPUT,
  384. cmLocalGenerator::UNCHANGED));
  385. #ifdef _WIN32
  386. // There may be a manifest file for this target. Add it to the
  387. // clean set just in case.
  388. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  389. {
  390. libCleanFiles.push_back(
  391. this->Convert((targetFullPath+".manifest").c_str(),
  392. cmLocalGenerator::START_OUTPUT,
  393. cmLocalGenerator::UNCHANGED));
  394. }
  395. #endif
  396. std::vector<std::string> commands1;
  397. // Add a command to remove any existing files for this library.
  398. // for static libs only
  399. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  400. {
  401. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  402. *this->Target, "target");
  403. this->LocalGenerator->CreateCDCommand
  404. (commands1,
  405. this->Makefile->GetStartOutputDirectory(),
  406. cmLocalGenerator::HOME_OUTPUT);
  407. commands.insert(commands.end(), commands1.begin(), commands1.end());
  408. commands1.clear();
  409. }
  410. // Add the pre-build and pre-link rules building but not when relinking.
  411. if(!relink)
  412. {
  413. this->LocalGenerator
  414. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands(),
  415. this->Target);
  416. this->LocalGenerator
  417. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands(),
  418. this->Target);
  419. }
  420. // Determine whether a link script will be used.
  421. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  422. // Select whether to use a response file for objects.
  423. bool useResponseFile = false;
  424. {
  425. std::string responseVar = "CMAKE_";
  426. responseVar += linkLanguage;
  427. responseVar += "_USE_RESPONSE_FILE_FOR_OBJECTS";
  428. if(this->Makefile->IsOn(responseVar.c_str()))
  429. {
  430. useResponseFile = true;
  431. }
  432. }
  433. // For static libraries there might be archiving rules.
  434. bool haveStaticLibraryRule = false;
  435. std::vector<std::string> archiveCreateCommands;
  436. std::vector<std::string> archiveAppendCommands;
  437. std::vector<std::string> archiveFinishCommands;
  438. std::string::size_type archiveCommandLimit = std::string::npos;
  439. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  440. {
  441. haveStaticLibraryRule =
  442. this->Makefile->GetDefinition(linkRuleVar)? true:false;
  443. std::string arCreateVar = "CMAKE_";
  444. arCreateVar += linkLanguage;
  445. arCreateVar += "_ARCHIVE_CREATE";
  446. if(const char* rule = this->Makefile->GetDefinition(arCreateVar.c_str()))
  447. {
  448. cmSystemTools::ExpandListArgument(rule, archiveCreateCommands);
  449. }
  450. std::string arAppendVar = "CMAKE_";
  451. arAppendVar += linkLanguage;
  452. arAppendVar += "_ARCHIVE_APPEND";
  453. if(const char* rule = this->Makefile->GetDefinition(arAppendVar.c_str()))
  454. {
  455. cmSystemTools::ExpandListArgument(rule, archiveAppendCommands);
  456. }
  457. std::string arFinishVar = "CMAKE_";
  458. arFinishVar += linkLanguage;
  459. arFinishVar += "_ARCHIVE_FINISH";
  460. if(const char* rule = this->Makefile->GetDefinition(arFinishVar.c_str()))
  461. {
  462. cmSystemTools::ExpandListArgument(rule, archiveFinishCommands);
  463. }
  464. }
  465. // Decide whether to use archiving rules.
  466. bool useArchiveRules =
  467. !haveStaticLibraryRule &&
  468. !archiveCreateCommands.empty() && !archiveAppendCommands.empty();
  469. if(useArchiveRules)
  470. {
  471. // Archiving rules are always run with a link script.
  472. useLinkScript = true;
  473. // Archiving rules never use a response file.
  474. useResponseFile = false;
  475. // Limit the length of individual object lists to less than the
  476. // 32K command line length limit on Windows. We could make this a
  477. // platform file variable but this should work everywhere.
  478. archiveCommandLimit = 30000;
  479. }
  480. // Expand the rule variables.
  481. std::vector<std::string> real_link_commands;
  482. {
  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. std::string frameworkPath;
  490. std::string linkPath;
  491. this->LocalGenerator
  492. ->OutputLinkLibraries(linkLibs, frameworkPath, linkPath,
  493. *this->GeneratorTarget, relink);
  494. linkLibs = frameworkPath + linkPath + linkLibs;
  495. }
  496. // Construct object file lists that may be needed to expand the
  497. // rule.
  498. std::string buildObjs;
  499. this->CreateObjectLists(useLinkScript, useArchiveRules, useResponseFile,
  500. buildObjs, depends);
  501. cmLocalGenerator::RuleVariables vars;
  502. vars.TargetPDB = targetOutPathPDB.c_str();
  503. // Setup the target version.
  504. std::string targetVersionMajor;
  505. std::string targetVersionMinor;
  506. {
  507. cmOStringStream majorStream;
  508. cmOStringStream minorStream;
  509. int major;
  510. int minor;
  511. this->Target->GetTargetVersion(major, minor);
  512. majorStream << major;
  513. minorStream << minor;
  514. targetVersionMajor = majorStream.str();
  515. targetVersionMinor = minorStream.str();
  516. }
  517. vars.TargetVersionMajor = targetVersionMajor.c_str();
  518. vars.TargetVersionMinor = targetVersionMinor.c_str();
  519. vars.RuleLauncher = "RULE_LAUNCH_LINK";
  520. vars.CMTarget = this->Target;
  521. vars.Language = linkLanguage;
  522. vars.Objects = buildObjs.c_str();
  523. std::string objdir = cmake::GetCMakeFilesDirectoryPostSlash();
  524. objdir += this->Target->GetName();
  525. objdir += ".dir";
  526. objdir = this->Convert(objdir.c_str(),
  527. cmLocalGenerator::START_OUTPUT,
  528. cmLocalGenerator::SHELL);
  529. vars.ObjectDir = objdir.c_str();
  530. vars.Target = targetOutPathReal.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.c_str(),
  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. // 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. }