cmMakefileLibraryTargetGenerator.cxx 22 KB

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