cmMakefileLibraryTargetGenerator.cxx 29 KB

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