cmMakefileLibraryTargetGenerator.cxx 28 KB

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