cmMakefileLibraryTargetGenerator.cxx 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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->GeneratorTarget->GetType() != cmState::INTERFACE_LIBRARY)
  26. {
  27. this->GeneratorTarget->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->GeneratorTarget->GetType())
  55. {
  56. case cmState::STATIC_LIBRARY:
  57. this->WriteStaticLibraryRules();
  58. break;
  59. case cmState::SHARED_LIBRARY:
  60. this->WriteSharedLibraryRules(false);
  61. if(this->GeneratorTarget->NeedRelinkBeforeInstall(this->ConfigName))
  62. {
  63. // Write rules to link an installable version of the target.
  64. this->WriteSharedLibraryRules(true);
  65. }
  66. break;
  67. case cmState::MODULE_LIBRARY:
  68. this->WriteModuleLibraryRules(false);
  69. if(this->GeneratorTarget->NeedRelinkBeforeInstall(this->ConfigName))
  70. {
  71. // Write rules to link an installable version of the target.
  72. this->WriteModuleLibraryRules(true);
  73. }
  74. break;
  75. case cmState::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->GeneratorTarget->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->GeneratorTarget->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->GeneratorTarget->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->GeneratorTarget->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->GeneratorTarget->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->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY ||
  218. this->GeneratorTarget->GetType() == cmState::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->GeneratorTarget->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->GeneratorTarget->GetDirectory(this->ConfigName);
  238. this->OSXBundleGenerator->CreateFramework(targetName, outpath);
  239. outpath += "/";
  240. }
  241. else if(this->Target->IsCFBundleOnApple())
  242. {
  243. outpath = this->GeneratorTarget->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->GeneratorTarget->GetDirectory(this->ConfigName);
  262. cmSystemTools::MakeDirectory(outpath.c_str());
  263. outpath += "/";
  264. if(!targetNameImport.empty())
  265. {
  266. outpathImp = this->GeneratorTarget->GetDirectory(this->ConfigName, true);
  267. cmSystemTools::MakeDirectory(outpathImp.c_str());
  268. outpathImp += "/";
  269. }
  270. }
  271. std::string compilePdbOutputPath =
  272. this->GeneratorTarget->GetCompilePDBDirectory(this->ConfigName);
  273. cmSystemTools::MakeDirectory(compilePdbOutputPath.c_str());
  274. std::string pdbOutputPath =
  275. this->GeneratorTarget->GetPDBDirectory(this->ConfigName);
  276. cmSystemTools::MakeDirectory(pdbOutputPath.c_str());
  277. pdbOutputPath += "/";
  278. std::string targetFullPath = outpath + targetName;
  279. std::string targetFullPathPDB = pdbOutputPath + targetNamePDB;
  280. std::string targetFullPathSO = outpath + targetNameSO;
  281. std::string targetFullPathReal = outpath + targetNameReal;
  282. std::string targetFullPathImport = outpathImp + targetNameImport;
  283. // Construct the output path version of the names for use in command
  284. // arguments.
  285. std::string targetOutPathPDB =
  286. this->Convert(targetFullPathPDB,cmLocalGenerator::NONE,
  287. cmLocalGenerator::SHELL);
  288. std::string targetOutPath =
  289. this->Convert(targetFullPath,cmLocalGenerator::START_OUTPUT,
  290. cmLocalGenerator::SHELL);
  291. std::string targetOutPathSO =
  292. this->Convert(targetFullPathSO,cmLocalGenerator::START_OUTPUT,
  293. cmLocalGenerator::SHELL);
  294. std::string targetOutPathReal =
  295. this->Convert(targetFullPathReal,cmLocalGenerator::START_OUTPUT,
  296. cmLocalGenerator::SHELL);
  297. std::string targetOutPathImport =
  298. this->Convert(targetFullPathImport,cmLocalGenerator::START_OUTPUT,
  299. cmLocalGenerator::SHELL);
  300. this->NumberOfProgressActions++;
  301. if(!this->NoRuleMessages)
  302. {
  303. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  304. this->MakeEchoProgress(progress);
  305. // Add the link message.
  306. std::string buildEcho = "Linking ";
  307. buildEcho += linkLanguage;
  308. switch(this->GeneratorTarget->GetType())
  309. {
  310. case cmState::STATIC_LIBRARY:
  311. buildEcho += " static library ";
  312. break;
  313. case cmState::SHARED_LIBRARY:
  314. buildEcho += " shared library ";
  315. break;
  316. case cmState::MODULE_LIBRARY:
  317. if (this->Target->IsCFBundleOnApple())
  318. buildEcho += " CFBundle";
  319. buildEcho += " shared module ";
  320. break;
  321. default:
  322. buildEcho += " library ";
  323. break;
  324. }
  325. buildEcho += targetOutPath.c_str();
  326. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  327. cmLocalUnixMakefileGenerator3::EchoLink,
  328. &progress);
  329. }
  330. const char* forbiddenFlagVar = 0;
  331. switch(this->GeneratorTarget->GetType())
  332. {
  333. case cmState::SHARED_LIBRARY:
  334. forbiddenFlagVar = "_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS";
  335. break;
  336. case cmState::MODULE_LIBRARY:
  337. forbiddenFlagVar = "_CREATE_SHARED_MODULE_FORBIDDEN_FLAGS";
  338. break;
  339. default: break;
  340. }
  341. // Clean files associated with this library.
  342. std::vector<std::string> libCleanFiles;
  343. libCleanFiles.push_back(this->Convert(targetFullPath,
  344. cmLocalGenerator::START_OUTPUT,
  345. cmLocalGenerator::UNCHANGED));
  346. if(targetNameReal != targetName)
  347. {
  348. libCleanFiles.push_back(this->Convert(targetFullPathReal,
  349. cmLocalGenerator::START_OUTPUT,
  350. cmLocalGenerator::UNCHANGED));
  351. }
  352. if(targetNameSO != targetName &&
  353. targetNameSO != targetNameReal)
  354. {
  355. libCleanFiles.push_back(this->Convert(targetFullPathSO,
  356. cmLocalGenerator::START_OUTPUT,
  357. cmLocalGenerator::UNCHANGED));
  358. }
  359. if(!targetNameImport.empty())
  360. {
  361. libCleanFiles.push_back(this->Convert(targetFullPathImport,
  362. cmLocalGenerator::START_OUTPUT,
  363. cmLocalGenerator::UNCHANGED));
  364. std::string implib;
  365. if(this->GeneratorTarget->GetImplibGNUtoMS(targetFullPathImport, implib))
  366. {
  367. libCleanFiles.push_back(this->Convert(implib,
  368. cmLocalGenerator::START_OUTPUT,
  369. cmLocalGenerator::UNCHANGED));
  370. }
  371. }
  372. // List the PDB for cleaning only when the whole target is
  373. // cleaned. We do not want to delete the .pdb file just before
  374. // linking the target.
  375. this->CleanFiles.push_back
  376. (this->Convert(targetFullPathPDB,
  377. cmLocalGenerator::START_OUTPUT,
  378. cmLocalGenerator::UNCHANGED));
  379. #ifdef _WIN32
  380. // There may be a manifest file for this target. Add it to the
  381. // clean set just in case.
  382. if(this->GeneratorTarget->GetType() != cmState::STATIC_LIBRARY)
  383. {
  384. libCleanFiles.push_back(
  385. this->Convert((targetFullPath+".manifest").c_str(),
  386. cmLocalGenerator::START_OUTPUT,
  387. cmLocalGenerator::UNCHANGED));
  388. }
  389. #endif
  390. std::vector<std::string> commands1;
  391. // Add a command to remove any existing files for this library.
  392. // for static libs only
  393. if(this->GeneratorTarget->GetType() == cmState::STATIC_LIBRARY)
  394. {
  395. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  396. this->GeneratorTarget, "target");
  397. this->LocalGenerator->CreateCDCommand
  398. (commands1,
  399. this->Makefile->GetCurrentBinaryDirectory(),
  400. cmLocalGenerator::HOME_OUTPUT);
  401. commands.insert(commands.end(), commands1.begin(), commands1.end());
  402. commands1.clear();
  403. }
  404. // Add the pre-build and pre-link rules building but not when relinking.
  405. if(!relink)
  406. {
  407. this->LocalGenerator
  408. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands(),
  409. this->Target);
  410. this->LocalGenerator
  411. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands(),
  412. this->Target);
  413. }
  414. // Determine whether a link script will be used.
  415. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  416. // Select whether to use a response file for objects.
  417. bool useResponseFileForObjects = false;
  418. {
  419. std::string responseVar = "CMAKE_";
  420. responseVar += linkLanguage;
  421. responseVar += "_USE_RESPONSE_FILE_FOR_OBJECTS";
  422. if(this->Makefile->IsOn(responseVar))
  423. {
  424. useResponseFileForObjects = true;
  425. }
  426. }
  427. // Select whether to use a response file for libraries.
  428. bool useResponseFileForLibs = false;
  429. {
  430. std::string responseVar = "CMAKE_";
  431. responseVar += linkLanguage;
  432. responseVar += "_USE_RESPONSE_FILE_FOR_LIBRARIES";
  433. if(this->Makefile->IsOn(responseVar))
  434. {
  435. useResponseFileForLibs = true;
  436. }
  437. }
  438. // For static libraries there might be archiving rules.
  439. bool haveStaticLibraryRule = false;
  440. std::vector<std::string> archiveCreateCommands;
  441. std::vector<std::string> archiveAppendCommands;
  442. std::vector<std::string> archiveFinishCommands;
  443. std::string::size_type archiveCommandLimit = std::string::npos;
  444. if(this->GeneratorTarget->GetType() == cmState::STATIC_LIBRARY)
  445. {
  446. haveStaticLibraryRule =
  447. this->Makefile->GetDefinition(linkRuleVar)? true:false;
  448. std::string arCreateVar = "CMAKE_";
  449. arCreateVar += linkLanguage;
  450. arCreateVar += "_ARCHIVE_CREATE";
  451. if(const char* rule = this->Makefile->GetDefinition(arCreateVar))
  452. {
  453. cmSystemTools::ExpandListArgument(rule, archiveCreateCommands);
  454. }
  455. std::string arAppendVar = "CMAKE_";
  456. arAppendVar += linkLanguage;
  457. arAppendVar += "_ARCHIVE_APPEND";
  458. if(const char* rule = this->Makefile->GetDefinition(arAppendVar))
  459. {
  460. cmSystemTools::ExpandListArgument(rule, archiveAppendCommands);
  461. }
  462. std::string arFinishVar = "CMAKE_";
  463. arFinishVar += linkLanguage;
  464. arFinishVar += "_ARCHIVE_FINISH";
  465. if(const char* rule = this->Makefile->GetDefinition(arFinishVar))
  466. {
  467. cmSystemTools::ExpandListArgument(rule, archiveFinishCommands);
  468. }
  469. }
  470. // Decide whether to use archiving rules.
  471. bool useArchiveRules =
  472. !haveStaticLibraryRule &&
  473. !archiveCreateCommands.empty() && !archiveAppendCommands.empty();
  474. if(useArchiveRules)
  475. {
  476. // Archiving rules are always run with a link script.
  477. useLinkScript = true;
  478. // Archiving rules never use a response file.
  479. useResponseFileForObjects = false;
  480. // Limit the length of individual object lists to less than the
  481. // 32K command line length limit on Windows. We could make this a
  482. // platform file variable but this should work everywhere.
  483. archiveCommandLimit = 30000;
  484. }
  485. // Expand the rule variables.
  486. std::vector<std::string> real_link_commands;
  487. {
  488. bool useWatcomQuote = this->Makefile->IsOn(linkRuleVar+"_USE_WATCOM_QUOTE");
  489. // Set path conversion for link script shells.
  490. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  491. // Collect up flags to link in needed libraries.
  492. std::string linkLibs;
  493. if(this->GeneratorTarget->GetType() != cmState::STATIC_LIBRARY)
  494. {
  495. this->CreateLinkLibs(linkLibs, relink, useResponseFileForLibs, depends,
  496. useWatcomQuote);
  497. }
  498. // Construct object file lists that may be needed to expand the
  499. // rule.
  500. std::string buildObjs;
  501. this->CreateObjectLists(useLinkScript, useArchiveRules,
  502. useResponseFileForObjects, buildObjs, depends,
  503. useWatcomQuote);
  504. // maybe create .def file from list of objects
  505. if (this->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY &&
  506. this->Makefile->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS"))
  507. {
  508. if(this->Target->GetPropertyAsBool("WINDOWS_EXPORT_ALL_SYMBOLS"))
  509. {
  510. std::string name_of_def_file =
  511. this->GeneratorTarget->GetSupportDirectory();
  512. name_of_def_file += std::string("/") +
  513. this->Target->GetName();
  514. name_of_def_file += ".def";
  515. std::string cmd = cmSystemTools::GetCMakeCommand();
  516. cmd = this->Convert(cmd, cmLocalGenerator::NONE,
  517. cmLocalGenerator::SHELL);
  518. cmd += " -E __create_def ";
  519. cmd += this->Convert(name_of_def_file,
  520. cmLocalGenerator::START_OUTPUT,
  521. cmLocalGenerator::SHELL);
  522. cmd += " ";
  523. std::string objlist_file = name_of_def_file;
  524. objlist_file += ".objs";
  525. cmd += this->Convert(objlist_file,
  526. cmLocalGenerator::START_OUTPUT,
  527. cmLocalGenerator::SHELL);
  528. real_link_commands.push_back(cmd);
  529. // create a list of obj files for the -E __create_def to read
  530. cmGeneratedFileStream fout(objlist_file.c_str());
  531. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  532. i != this->Objects.end(); ++i)
  533. {
  534. if(cmHasLiteralSuffix(*i, ".obj"))
  535. {
  536. fout << *i << "\n";
  537. }
  538. }
  539. for(std::vector<std::string>::const_iterator i =
  540. this->ExternalObjects.begin();
  541. i != this->ExternalObjects.end(); ++i)
  542. {
  543. fout << *i << "\n";
  544. }
  545. // now add the def file link flag
  546. linkFlags += " ";
  547. linkFlags +=
  548. this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  549. linkFlags += this->Convert(name_of_def_file,
  550. cmLocalGenerator::START_OUTPUT,
  551. cmLocalGenerator::SHELL);
  552. linkFlags += " ";
  553. }
  554. }
  555. std::string manifests = this->GetManifests();
  556. cmLocalGenerator::RuleVariables vars;
  557. vars.TargetPDB = targetOutPathPDB.c_str();
  558. // Setup the target version.
  559. std::string targetVersionMajor;
  560. std::string targetVersionMinor;
  561. {
  562. std::ostringstream majorStream;
  563. std::ostringstream minorStream;
  564. int major;
  565. int minor;
  566. this->GeneratorTarget->GetTargetVersion(major, minor);
  567. majorStream << major;
  568. minorStream << minor;
  569. targetVersionMajor = majorStream.str();
  570. targetVersionMinor = minorStream.str();
  571. }
  572. vars.TargetVersionMajor = targetVersionMajor.c_str();
  573. vars.TargetVersionMinor = targetVersionMinor.c_str();
  574. vars.RuleLauncher = "RULE_LAUNCH_LINK";
  575. vars.CMTarget = this->Target;
  576. vars.Language = linkLanguage.c_str();
  577. vars.Objects = buildObjs.c_str();
  578. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  579. objectDir = this->Convert(objectDir,
  580. cmLocalGenerator::START_OUTPUT,
  581. cmLocalGenerator::SHELL);
  582. vars.ObjectDir = objectDir.c_str();
  583. cmLocalGenerator::OutputFormat output = (useWatcomQuote) ?
  584. cmLocalGenerator::WATCOMQUOTE : cmLocalGenerator::SHELL;
  585. std::string target = this->Convert(targetFullPathReal,
  586. cmLocalGenerator::START_OUTPUT,
  587. output);
  588. vars.Target = target.c_str();
  589. vars.LinkLibraries = linkLibs.c_str();
  590. vars.ObjectsQuoted = buildObjs.c_str();
  591. if (this->GeneratorTarget->HasSOName(this->ConfigName))
  592. {
  593. vars.SONameFlag = this->Makefile->GetSONameFlag(linkLanguage);
  594. vars.TargetSOName= targetNameSO.c_str();
  595. }
  596. vars.LinkFlags = linkFlags.c_str();
  597. vars.Manifests = manifests.c_str();
  598. // Compute the directory portion of the install_name setting.
  599. std::string install_name_dir;
  600. if(this->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY)
  601. {
  602. // Get the install_name directory for the build tree.
  603. install_name_dir =
  604. this->GeneratorTarget->GetInstallNameDirForBuildTree(this->ConfigName);
  605. // Set the rule variable replacement value.
  606. if(install_name_dir.empty())
  607. {
  608. vars.TargetInstallNameDir = "";
  609. }
  610. else
  611. {
  612. // Convert to a path for the native build tool.
  613. install_name_dir =
  614. this->LocalGenerator->Convert(install_name_dir,
  615. cmLocalGenerator::NONE,
  616. cmLocalGenerator::SHELL);
  617. vars.TargetInstallNameDir = install_name_dir.c_str();
  618. }
  619. }
  620. // Add language feature flags.
  621. std::string langFlags;
  622. this->AddFeatureFlags(langFlags, linkLanguage);
  623. this->LocalGenerator->AddArchitectureFlags(langFlags, this->GeneratorTarget,
  624. linkLanguage, this->ConfigName);
  625. // remove any language flags that might not work with the
  626. // particular os
  627. if(forbiddenFlagVar)
  628. {
  629. this->RemoveForbiddenFlags(forbiddenFlagVar,
  630. linkLanguage, langFlags);
  631. }
  632. vars.LanguageCompileFlags = langFlags.c_str();
  633. // Construct the main link rule and expand placeholders.
  634. this->LocalGenerator->TargetImplib = targetOutPathImport;
  635. if(useArchiveRules)
  636. {
  637. // Construct the individual object list strings.
  638. std::vector<std::string> object_strings;
  639. this->WriteObjectsStrings(object_strings, archiveCommandLimit);
  640. // Create the archive with the first set of objects.
  641. std::vector<std::string>::iterator osi = object_strings.begin();
  642. {
  643. vars.Objects = osi->c_str();
  644. for(std::vector<std::string>::const_iterator
  645. i = archiveCreateCommands.begin();
  646. i != archiveCreateCommands.end(); ++i)
  647. {
  648. std::string cmd = *i;
  649. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  650. real_link_commands.push_back(cmd);
  651. }
  652. }
  653. // Append to the archive with the other object sets.
  654. for(++osi; osi != object_strings.end(); ++osi)
  655. {
  656. vars.Objects = osi->c_str();
  657. for(std::vector<std::string>::const_iterator
  658. i = archiveAppendCommands.begin();
  659. i != archiveAppendCommands.end(); ++i)
  660. {
  661. std::string cmd = *i;
  662. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  663. real_link_commands.push_back(cmd);
  664. }
  665. }
  666. // Finish the archive.
  667. vars.Objects = "";
  668. for(std::vector<std::string>::const_iterator
  669. i = archiveFinishCommands.begin();
  670. i != archiveFinishCommands.end(); ++i)
  671. {
  672. std::string cmd = *i;
  673. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  674. real_link_commands.push_back(cmd);
  675. }
  676. }
  677. else
  678. {
  679. // Get the set of commands.
  680. std::string linkRule = this->GetLinkRule(linkRuleVar);
  681. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  682. // Expand placeholders.
  683. for(std::vector<std::string>::iterator i = real_link_commands.begin();
  684. i != real_link_commands.end(); ++i)
  685. {
  686. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  687. }
  688. }
  689. this->LocalGenerator->TargetImplib = "";
  690. // Restore path conversion to normal shells.
  691. this->LocalGenerator->SetLinkScriptShell(false);
  692. }
  693. // Optionally convert the build rule to use a script to avoid long
  694. // command lines in the make shell.
  695. if(useLinkScript)
  696. {
  697. // Use a link script.
  698. const char* name = (relink? "relink.txt" : "link.txt");
  699. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  700. }
  701. else
  702. {
  703. // No link script. Just use the link rule directly.
  704. commands1 = real_link_commands;
  705. }
  706. this->LocalGenerator->CreateCDCommand
  707. (commands1,
  708. this->Makefile->GetCurrentBinaryDirectory(),
  709. cmLocalGenerator::HOME_OUTPUT);
  710. commands.insert(commands.end(), commands1.begin(), commands1.end());
  711. commands1.clear();
  712. // Add a rule to create necessary symlinks for the library.
  713. // Frameworks are handled by cmOSXBundleGenerator.
  714. if(targetOutPath != targetOutPathReal && !this->Target->IsFrameworkOnApple())
  715. {
  716. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  717. symlink += targetOutPathReal;
  718. symlink += " ";
  719. symlink += targetOutPathSO;
  720. symlink += " ";
  721. symlink += targetOutPath;
  722. commands1.push_back(symlink);
  723. this->LocalGenerator->CreateCDCommand(commands1,
  724. this->Makefile->GetCurrentBinaryDirectory(),
  725. cmLocalGenerator::HOME_OUTPUT);
  726. commands.insert(commands.end(), commands1.begin(), commands1.end());
  727. commands1.clear();
  728. }
  729. // Add the post-build rules when building but not when relinking.
  730. if(!relink)
  731. {
  732. this->LocalGenerator->
  733. AppendCustomCommands(commands, this->Target->GetPostBuildCommands(),
  734. this->Target);
  735. }
  736. // Compute the list of outputs.
  737. std::vector<std::string> outputs(1, targetFullPathReal);
  738. if(targetNameSO != targetNameReal)
  739. {
  740. outputs.push_back(targetFullPathSO);
  741. }
  742. if(targetName != targetNameSO &&
  743. targetName != targetNameReal)
  744. {
  745. outputs.push_back(targetFullPath);
  746. }
  747. // Write the build rule.
  748. this->WriteMakeRule(*this->BuildFileStream, 0, outputs,
  749. depends, commands, false);
  750. // Write the main driver rule to build everything in this target.
  751. this->WriteTargetDriverRule(targetFullPath, relink);
  752. // Clean all the possible library names and symlinks.
  753. this->CleanFiles.insert(this->CleanFiles.end(),
  754. libCleanFiles.begin(),libCleanFiles.end());
  755. }
  756. //----------------------------------------------------------------------------
  757. void
  758. cmMakefileLibraryTargetGenerator
  759. ::AppendOSXVerFlag(std::string& flags, const std::string& lang,
  760. const char* name, bool so)
  761. {
  762. // Lookup the flag to specify the version.
  763. std::string fvar = "CMAKE_";
  764. fvar += lang;
  765. fvar += "_OSX_";
  766. fvar += name;
  767. fvar += "_VERSION_FLAG";
  768. const char* flag = this->Makefile->GetDefinition(fvar);
  769. // Skip if no such flag.
  770. if(!flag)
  771. {
  772. return;
  773. }
  774. // Lookup the target version information.
  775. int major;
  776. int minor;
  777. int patch;
  778. this->GeneratorTarget->GetTargetVersion(so, major, minor, patch);
  779. if(major > 0 || minor > 0 || patch > 0)
  780. {
  781. // Append the flag since a non-zero version is specified.
  782. std::ostringstream vflag;
  783. vflag << flag << major << "." << minor << "." << patch;
  784. this->LocalGenerator->AppendFlags(flags, vflag.str());
  785. }
  786. }