cmMakefileLibraryTargetGenerator.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmMakefileLibraryTargetGenerator.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGlobalUnixMakefileGenerator3.h"
  16. #include "cmLocalUnixMakefileGenerator3.h"
  17. #include "cmMakefile.h"
  18. #include "cmSourceFile.h"
  19. #include "cmTarget.h"
  20. #include "cmake.h"
  21. #include <memory> // auto_ptr
  22. //----------------------------------------------------------------------------
  23. cmMakefileLibraryTargetGenerator::cmMakefileLibraryTargetGenerator()
  24. {
  25. this->DriveCustomCommandsOnDepends = true;
  26. }
  27. //----------------------------------------------------------------------------
  28. void cmMakefileLibraryTargetGenerator::WriteRuleFiles()
  29. {
  30. // create the build.make file and directory, put in the common blocks
  31. this->CreateRuleFile();
  32. // write rules used to help build object files
  33. this->WriteCommonCodeRules();
  34. // write in rules for object files and custom commands
  35. this->WriteTargetBuildRules();
  36. // write the per-target per-language flags
  37. this->WriteTargetLanguageFlags();
  38. // Write the dependency generation rule.
  39. this->WriteTargetDependRules();
  40. // write the link rules
  41. // Write the rule for this target type.
  42. switch(this->Target->GetType())
  43. {
  44. case cmTarget::STATIC_LIBRARY:
  45. this->WriteStaticLibraryRules();
  46. break;
  47. case cmTarget::SHARED_LIBRARY:
  48. this->WriteSharedLibraryRules(false);
  49. if(this->Target->NeedRelinkBeforeInstall())
  50. {
  51. // Write rules to link an installable version of the target.
  52. this->WriteSharedLibraryRules(true);
  53. }
  54. break;
  55. case cmTarget::MODULE_LIBRARY:
  56. this->WriteModuleLibraryRules(false);
  57. if(this->Target->NeedRelinkBeforeInstall())
  58. {
  59. // Write rules to link an installable version of the target.
  60. this->WriteModuleLibraryRules(true);
  61. }
  62. break;
  63. default:
  64. // If language is not known, this is an error.
  65. cmSystemTools::Error("Unknown Library Type");
  66. break;
  67. }
  68. // Write the requires target.
  69. this->WriteTargetRequiresRules();
  70. // Write clean target
  71. this->WriteTargetCleanRules();
  72. // close the streams
  73. this->CloseFileStreams();
  74. }
  75. //----------------------------------------------------------------------------
  76. void cmMakefileLibraryTargetGenerator::WriteStaticLibraryRules()
  77. {
  78. const char* linkLanguage =
  79. this->Target->GetLinkerLanguage(this->GlobalGenerator);
  80. std::string linkRuleVar = "CMAKE_";
  81. if (linkLanguage)
  82. {
  83. linkRuleVar += linkLanguage;
  84. }
  85. linkRuleVar += "_CREATE_STATIC_LIBRARY";
  86. std::string extraFlags;
  87. this->LocalGenerator->AppendFlags
  88. (extraFlags,this->Target->GetProperty("STATIC_LIBRARY_FLAGS"));
  89. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), false);
  90. }
  91. //----------------------------------------------------------------------------
  92. void cmMakefileLibraryTargetGenerator::WriteSharedLibraryRules(bool relink)
  93. {
  94. const char* linkLanguage =
  95. this->Target->GetLinkerLanguage(this->GlobalGenerator);
  96. std::string linkRuleVar = "CMAKE_";
  97. if (linkLanguage)
  98. {
  99. linkRuleVar += linkLanguage;
  100. }
  101. linkRuleVar += "_CREATE_SHARED_LIBRARY";
  102. std::string extraFlags;
  103. this->LocalGenerator->AppendFlags
  104. (extraFlags, this->Target->GetProperty("LINK_FLAGS"));
  105. std::string linkFlagsConfig = "LINK_FLAGS_";
  106. linkFlagsConfig +=
  107. cmSystemTools::UpperCase(this->LocalGenerator->ConfigurationName.c_str());
  108. this->LocalGenerator->AppendFlags
  109. (extraFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  110. this->LocalGenerator->AddConfigVariableFlags
  111. (extraFlags, "CMAKE_SHARED_LINKER_FLAGS",
  112. this->LocalGenerator->ConfigurationName.c_str());
  113. if(this->Makefile->IsOn("WIN32") && !(this->Makefile->IsOn("CYGWIN")
  114. || this->Makefile->IsOn("MINGW")))
  115. {
  116. const std::vector<cmSourceFile*>& sources =
  117. this->Target->GetSourceFiles();
  118. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  119. i != sources.end(); ++i)
  120. {
  121. if((*i)->GetSourceExtension() == "def")
  122. {
  123. extraFlags += " ";
  124. extraFlags +=
  125. this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  126. extraFlags +=
  127. this->Convert((*i)->GetFullPath().c_str(),
  128. cmLocalGenerator::START_OUTPUT,
  129. cmLocalGenerator::SHELL);
  130. }
  131. }
  132. }
  133. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), relink);
  134. }
  135. //----------------------------------------------------------------------------
  136. void cmMakefileLibraryTargetGenerator::WriteModuleLibraryRules(bool relink)
  137. {
  138. const char* linkLanguage =
  139. this->Target->GetLinkerLanguage(this->GlobalGenerator);
  140. std::string linkRuleVar = "CMAKE_";
  141. if (linkLanguage)
  142. {
  143. linkRuleVar += linkLanguage;
  144. }
  145. linkRuleVar += "_CREATE_SHARED_MODULE";
  146. std::string extraFlags;
  147. this->LocalGenerator->AppendFlags(extraFlags,
  148. this->Target->GetProperty("LINK_FLAGS"));
  149. std::string linkFlagsConfig = "LINK_FLAGS_";
  150. linkFlagsConfig +=
  151. cmSystemTools::UpperCase(this->LocalGenerator->ConfigurationName.c_str());
  152. this->LocalGenerator->AppendFlags
  153. (extraFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  154. this->LocalGenerator->AddConfigVariableFlags
  155. (extraFlags, "CMAKE_MODULE_LINKER_FLAGS",
  156. this->LocalGenerator->ConfigurationName.c_str());
  157. // TODO: .def files should be supported here also.
  158. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), relink);
  159. }
  160. //----------------------------------------------------------------------------
  161. void cmMakefileLibraryTargetGenerator::WriteLibraryRules
  162. (const char* linkRuleVar, const char* extraFlags, bool relink)
  163. {
  164. // TODO: Merge the methods that call this method to avoid
  165. // code duplication.
  166. std::vector<std::string> commands;
  167. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  168. std::string objTarget;
  169. // Build list of dependencies.
  170. std::vector<std::string> depends;
  171. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  172. obj != this->Objects.end(); ++obj)
  173. {
  174. objTarget = relPath;
  175. objTarget += *obj;
  176. depends.push_back(objTarget);
  177. }
  178. // Add dependencies on targets that must be built first.
  179. this->AppendTargetDepends(depends);
  180. // Add a dependency on the rule file itself.
  181. this->LocalGenerator->AppendRuleDepend(depends,
  182. this->BuildFileNameFull.c_str());
  183. for(std::vector<std::string>::const_iterator obj
  184. = this->ExternalObjects.begin();
  185. obj != this->ExternalObjects.end(); ++obj)
  186. {
  187. depends.push_back(*obj);
  188. }
  189. // Get the language to use for linking this library.
  190. const char* linkLanguage =
  191. this->Target->GetLinkerLanguage(this->GlobalGenerator);
  192. // Make sure we have a link language.
  193. if(!linkLanguage)
  194. {
  195. cmSystemTools::Error("Cannot determine link language for target \"",
  196. this->Target->GetName(), "\".");
  197. return;
  198. }
  199. // Create set of linking flags.
  200. std::string linkFlags;
  201. this->LocalGenerator->AppendFlags(linkFlags, extraFlags);
  202. // Construct the name of the library.
  203. std::string targetName;
  204. std::string targetNameSO;
  205. std::string targetNameReal;
  206. std::string targetNameImport;
  207. std::string targetNamePDB;
  208. this->Target->GetLibraryNames(
  209. targetName, targetNameSO, targetNameReal, targetNameImport, targetNamePDB,
  210. this->LocalGenerator->ConfigurationName.c_str());
  211. // Construct the full path version of the names.
  212. std::string outpath;
  213. std::string outpathImp;
  214. if(relink)
  215. {
  216. outpath = this->Makefile->GetStartOutputDirectory();
  217. outpath += cmake::GetCMakeFilesDirectory();
  218. outpath += "/CMakeRelink.dir";
  219. cmSystemTools::MakeDirectory(outpath.c_str());
  220. outpath += "/";
  221. if(!targetNameImport.empty())
  222. {
  223. outpathImp = outpath;
  224. }
  225. }
  226. else
  227. {
  228. outpath = this->Target->GetDirectory();
  229. outpath += "/";
  230. if(!targetNameImport.empty())
  231. {
  232. outpathImp = this->Target->GetDirectory(0, true);
  233. outpathImp += "/";
  234. }
  235. }
  236. std::string targetFullPath = outpath + targetName;
  237. std::string targetFullPathPDB = outpath + targetNamePDB;
  238. std::string targetFullPathSO = outpath + targetNameSO;
  239. std::string targetFullPathReal = outpath + targetNameReal;
  240. std::string targetFullPathImport = outpathImp + targetNameImport;
  241. // Construct the output path version of the names for use in command
  242. // arguments.
  243. std::string targetOutPathPDB =
  244. this->Convert(targetFullPathPDB.c_str(),cmLocalGenerator::FULL,
  245. cmLocalGenerator::SHELL);
  246. std::string targetOutPath =
  247. this->Convert(targetFullPath.c_str(),cmLocalGenerator::START_OUTPUT,
  248. cmLocalGenerator::SHELL);
  249. std::string targetOutPathSO =
  250. this->Convert(targetFullPathSO.c_str(),cmLocalGenerator::START_OUTPUT,
  251. cmLocalGenerator::SHELL);
  252. std::string targetOutPathReal =
  253. this->Convert(targetFullPathReal.c_str(),cmLocalGenerator::START_OUTPUT,
  254. cmLocalGenerator::SHELL);
  255. std::string targetOutPathImport =
  256. this->Convert(targetFullPathImport.c_str(),cmLocalGenerator::START_OUTPUT,
  257. cmLocalGenerator::SHELL);
  258. // Add the link message.
  259. std::string buildEcho = "Linking ";
  260. buildEcho += linkLanguage;
  261. const char* forbiddenFlagVar = 0;
  262. switch(this->Target->GetType())
  263. {
  264. case cmTarget::STATIC_LIBRARY:
  265. buildEcho += " static library ";
  266. break;
  267. case cmTarget::SHARED_LIBRARY:
  268. forbiddenFlagVar = "_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS";
  269. buildEcho += " shared library ";
  270. break;
  271. case cmTarget::MODULE_LIBRARY:
  272. forbiddenFlagVar = "_CREATE_SHARED_MODULE_FORBIDDEN_FLAGS";
  273. buildEcho += " shared module ";
  274. break;
  275. default:
  276. buildEcho += " library ";
  277. break;
  278. }
  279. buildEcho += targetOutPath.c_str();
  280. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  281. cmLocalUnixMakefileGenerator3::EchoLink);
  282. // Construct a list of files associated with this library that may
  283. // need to be cleaned.
  284. std::vector<std::string> libCleanFiles;
  285. if(this->Target->GetPropertyAsBool("CLEAN_DIRECT_OUTPUT"))
  286. {
  287. // The user has requested that only the files directly built
  288. // by this target be cleaned instead of all possible names.
  289. libCleanFiles.push_back(this->Convert(targetFullPath.c_str(),
  290. cmLocalGenerator::START_OUTPUT,
  291. cmLocalGenerator::UNCHANGED));
  292. if(targetNameReal != targetName)
  293. {
  294. libCleanFiles.push_back(this->Convert(targetFullPathReal.c_str(),
  295. cmLocalGenerator::START_OUTPUT,
  296. cmLocalGenerator::UNCHANGED));
  297. }
  298. if(targetNameSO != targetName &&
  299. targetNameSO != targetNameReal)
  300. {
  301. libCleanFiles.push_back(this->Convert(targetFullPathSO.c_str(),
  302. cmLocalGenerator::START_OUTPUT,
  303. cmLocalGenerator::UNCHANGED));
  304. }
  305. if(!targetNameImport.empty())
  306. {
  307. libCleanFiles.push_back(this->Convert(targetFullPathImport.c_str(),
  308. cmLocalGenerator::START_OUTPUT,
  309. cmLocalGenerator::UNCHANGED));
  310. }
  311. }
  312. else
  313. {
  314. // This target may switch between static and shared based
  315. // on a user option or the BUILD_SHARED_LIBS switch. Clean
  316. // all possible names.
  317. std::string cleanStaticName;
  318. std::string cleanSharedName;
  319. std::string cleanSharedSOName;
  320. std::string cleanSharedRealName;
  321. std::string cleanImportName;
  322. std::string cleanPDBName;
  323. this->Target->GetLibraryCleanNames(
  324. cleanStaticName,
  325. cleanSharedName,
  326. cleanSharedSOName,
  327. cleanSharedRealName,
  328. cleanImportName,
  329. cleanPDBName,
  330. this->LocalGenerator->ConfigurationName.c_str());
  331. std::string cleanFullStaticName = outpath + cleanStaticName;
  332. std::string cleanFullSharedName = outpath + cleanSharedName;
  333. std::string cleanFullSharedSOName = outpath + cleanSharedSOName;
  334. std::string cleanFullSharedRealName = outpath + cleanSharedRealName;
  335. std::string cleanFullImportName = outpathImp + cleanImportName;
  336. std::string cleanFullPDBName = outpath + cleanPDBName;
  337. libCleanFiles.push_back
  338. (this->Convert(cleanFullStaticName.c_str(),
  339. cmLocalGenerator::START_OUTPUT,
  340. cmLocalGenerator::UNCHANGED));
  341. if(cleanSharedRealName != cleanStaticName)
  342. {
  343. libCleanFiles.push_back(this->Convert(cleanFullSharedRealName.c_str(),
  344. cmLocalGenerator::START_OUTPUT,
  345. cmLocalGenerator::UNCHANGED));
  346. }
  347. if(cleanSharedSOName != cleanStaticName &&
  348. cleanSharedSOName != cleanSharedRealName)
  349. {
  350. libCleanFiles.push_back(this->Convert(cleanFullSharedSOName.c_str(),
  351. cmLocalGenerator::START_OUTPUT,
  352. cmLocalGenerator::UNCHANGED));
  353. }
  354. if(cleanSharedName != cleanStaticName &&
  355. cleanSharedName != cleanSharedSOName &&
  356. cleanSharedName != cleanSharedRealName)
  357. {
  358. libCleanFiles.push_back(this->Convert(cleanFullSharedName.c_str(),
  359. cmLocalGenerator::START_OUTPUT,
  360. cmLocalGenerator::UNCHANGED));
  361. }
  362. if(!cleanImportName.empty())
  363. {
  364. libCleanFiles.push_back(this->Convert(cleanFullImportName.c_str(),
  365. cmLocalGenerator::START_OUTPUT,
  366. cmLocalGenerator::UNCHANGED));
  367. }
  368. // List the PDB for cleaning only when the whole target is
  369. // cleaned. We do not want to delete the .pdb file just before
  370. // linking the target.
  371. this->CleanFiles.push_back
  372. (this->Convert(cleanFullPDBName.c_str(),
  373. cmLocalGenerator::START_OUTPUT,
  374. cmLocalGenerator::UNCHANGED));
  375. }
  376. #ifdef _WIN32
  377. // There may be a manifest file for this target. Add it to the
  378. // clean set just in case.
  379. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  380. {
  381. libCleanFiles.push_back(
  382. this->Convert((targetFullPath+".manifest").c_str(),
  383. cmLocalGenerator::START_OUTPUT,
  384. cmLocalGenerator::UNCHANGED));
  385. }
  386. #endif
  387. // Add a command to remove any existing files for this library.
  388. std::vector<std::string> commands1;
  389. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  390. *this->Target, "target");
  391. this->LocalGenerator->CreateCDCommand
  392. (commands1,
  393. this->Makefile->GetStartOutputDirectory(),
  394. this->Makefile->GetHomeOutputDirectory());
  395. commands.insert(commands.end(), commands1.begin(), commands1.end());
  396. commands1.clear();
  397. // Add the pre-build and pre-link rules building but not when relinking.
  398. if(!relink)
  399. {
  400. this->LocalGenerator
  401. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands());
  402. this->LocalGenerator
  403. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands());
  404. }
  405. // Open the link script if it will be used.
  406. bool useLinkScript = false;
  407. std::string linkScriptName;
  408. std::auto_ptr<cmGeneratedFileStream> linkScriptStream;
  409. if(this->GlobalGenerator->GetUseLinkScript() &&
  410. (this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  411. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  412. this->Target->GetType() == cmTarget::MODULE_LIBRARY))
  413. {
  414. useLinkScript = true;
  415. linkScriptName = this->TargetBuildDirectoryFull;
  416. if(relink)
  417. {
  418. linkScriptName += "/relink.txt";
  419. }
  420. else
  421. {
  422. linkScriptName += "/link.txt";
  423. }
  424. std::auto_ptr<cmGeneratedFileStream> lss(
  425. new cmGeneratedFileStream(linkScriptName.c_str()));
  426. linkScriptStream = lss;
  427. }
  428. std::vector<std::string> link_script_commands;
  429. // Construct the main link rule.
  430. std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
  431. if(useLinkScript)
  432. {
  433. cmSystemTools::ExpandListArgument(linkRule, link_script_commands);
  434. std::string link_command = "$(CMAKE_COMMAND) -E cmake_link_script ";
  435. link_command += this->Convert(linkScriptName.c_str(),
  436. cmLocalGenerator::START_OUTPUT,
  437. cmLocalGenerator::SHELL);
  438. link_command += " --verbose=$(VERBOSE)";
  439. commands1.push_back(link_command);
  440. }
  441. else
  442. {
  443. cmSystemTools::ExpandListArgument(linkRule, commands1);
  444. }
  445. this->LocalGenerator->CreateCDCommand
  446. (commands1,
  447. this->Makefile->GetStartOutputDirectory(),
  448. this->Makefile->GetHomeOutputDirectory());
  449. commands.insert(commands.end(), commands1.begin(), commands1.end());
  450. // Add a rule to create necessary symlinks for the library.
  451. if(targetOutPath != targetOutPathReal)
  452. {
  453. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  454. symlink += targetOutPathReal;
  455. symlink += " ";
  456. symlink += targetOutPathSO;
  457. symlink += " ";
  458. symlink += targetOutPath;
  459. commands1.clear();
  460. commands1.push_back(symlink);
  461. this->LocalGenerator->CreateCDCommand(commands1,
  462. this->Makefile->GetStartOutputDirectory(),
  463. this->Makefile->GetHomeOutputDirectory());
  464. commands.insert(commands.end(), commands1.begin(), commands1.end());
  465. }
  466. // Add the post-build rules when building but not when relinking.
  467. if(!relink)
  468. {
  469. this->LocalGenerator->
  470. AppendCustomCommands(commands, this->Target->GetPostBuildCommands());
  471. }
  472. // Collect up flags to link in needed libraries.
  473. cmOStringStream linklibs;
  474. this->LocalGenerator->OutputLinkLibraries(linklibs, *this->Target, relink);
  475. // Construct object file lists that may be needed to expand the
  476. // rule.
  477. std::string variableName;
  478. std::string variableNameExternal;
  479. this->WriteObjectsVariable(variableName, variableNameExternal);
  480. std::string buildObjs;
  481. if(useLinkScript)
  482. {
  483. this->WriteObjectsString(buildObjs);
  484. }
  485. else
  486. {
  487. buildObjs = "$(";
  488. buildObjs += variableName;
  489. buildObjs += ") $(";
  490. buildObjs += variableNameExternal;
  491. buildObjs += ")";
  492. }
  493. std::string cleanObjs = "$(";
  494. cleanObjs += variableName;
  495. cleanObjs += ")";
  496. cmLocalGenerator::RuleVariables vars;
  497. vars.TargetPDB = targetOutPathPDB.c_str();
  498. // Setup the target version.
  499. std::string targetVersionMajor;
  500. std::string targetVersionMinor;
  501. {
  502. cmOStringStream majorStream;
  503. cmOStringStream minorStream;
  504. int major;
  505. int minor;
  506. this->Target->GetTargetVersion(major, minor);
  507. majorStream << major;
  508. minorStream << minor;
  509. targetVersionMajor = majorStream.str();
  510. targetVersionMinor = minorStream.str();
  511. }
  512. vars.TargetVersionMajor = targetVersionMajor.c_str();
  513. vars.TargetVersionMinor = targetVersionMinor.c_str();
  514. vars.Language = linkLanguage;
  515. vars.Objects = buildObjs.c_str();
  516. std::string objdir = cmake::GetCMakeFilesDirectoryPostSlash();
  517. objdir += this->Target->GetName();
  518. objdir += ".dir";
  519. vars.ObjectDir = objdir.c_str();
  520. vars.Target = targetOutPathReal.c_str();
  521. std::string linkString = linklibs.str();
  522. vars.LinkLibraries = linkString.c_str();
  523. vars.ObjectsQuoted = buildObjs.c_str();
  524. vars.TargetSOName= targetNameSO.c_str();
  525. vars.LinkFlags = linkFlags.c_str();
  526. // Compute the directory portion of the install_name setting.
  527. std::string install_name_dir;
  528. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY)
  529. {
  530. // Get the install_name directory for the build tree.
  531. const char* config = this->LocalGenerator->ConfigurationName.c_str();
  532. install_name_dir = this->Target->GetInstallNameDirForBuildTree(config);
  533. // Set the rule variable replacement value.
  534. if(install_name_dir.empty())
  535. {
  536. vars.TargetInstallNameDir = "";
  537. }
  538. else
  539. {
  540. // Convert to a path for the native build tool.
  541. install_name_dir =
  542. this->LocalGenerator->Convert(install_name_dir.c_str(),
  543. cmLocalGenerator::NONE,
  544. cmLocalGenerator::SHELL, false);
  545. vars.TargetInstallNameDir = install_name_dir.c_str();
  546. }
  547. }
  548. std::string langFlags;
  549. this->LocalGenerator
  550. ->AddLanguageFlags(langFlags, linkLanguage,
  551. this->LocalGenerator->ConfigurationName.c_str());
  552. // remove any language flags that might not work with the
  553. // particular os
  554. if(forbiddenFlagVar)
  555. {
  556. this->RemoveForbiddenFlags(forbiddenFlagVar,
  557. linkLanguage, langFlags);
  558. }
  559. vars.LanguageCompileFlags = langFlags.c_str();
  560. // Expand placeholders in the commands.
  561. this->LocalGenerator->TargetImplib = targetOutPathImport;
  562. if(useLinkScript)
  563. {
  564. for(std::vector<std::string>::iterator i = link_script_commands.begin();
  565. i != link_script_commands.end(); ++i)
  566. {
  567. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  568. }
  569. }
  570. else
  571. {
  572. for(std::vector<std::string>::iterator i = commands.begin();
  573. i != commands.end(); ++i)
  574. {
  575. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  576. }
  577. }
  578. this->LocalGenerator->TargetImplib = "";
  579. // Optionally convert the build rule to use a script to avoid long
  580. // command lines in the make shell.
  581. if(useLinkScript)
  582. {
  583. for(std::vector<std::string>::iterator cmd = link_script_commands.begin();
  584. cmd != link_script_commands.end(); ++cmd)
  585. {
  586. // Do not write out empty commands or commands beginning in the
  587. // shell no-op ":".
  588. if(!cmd->empty() && (*cmd)[0] != ':')
  589. {
  590. (*linkScriptStream) << *cmd << "\n";
  591. }
  592. }
  593. }
  594. // Write the build rule.
  595. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  596. targetFullPathReal.c_str(),
  597. depends, commands, false);
  598. // Some targets have more than one output file. Create rules to
  599. // drive the build if any extra outputs are missing.
  600. std::vector<std::string> extraOutputs;
  601. if(targetNameSO != targetNameReal)
  602. {
  603. this->GenerateExtraOutput(targetFullPathSO.c_str(),
  604. targetFullPathReal.c_str());
  605. }
  606. if(targetName != targetNameSO &&
  607. targetName != targetNameReal)
  608. {
  609. this->GenerateExtraOutput(targetFullPath.c_str(),
  610. targetFullPathReal.c_str());
  611. }
  612. // Write the main driver rule to build everything in this target.
  613. this->WriteTargetDriverRule(targetFullPath.c_str(), relink);
  614. // Clean all the possible library names and symlinks and object files.
  615. this->CleanFiles.insert(this->CleanFiles.end(),
  616. libCleanFiles.begin(),libCleanFiles.end());
  617. this->CleanFiles.insert(this->CleanFiles.end(),
  618. this->Objects.begin(),
  619. this->Objects.end());
  620. }