cmMakefileLibraryTargetGenerator.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. 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::MAKEFILE);
  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. this->Target->GetLibraryNames(
  208. targetName, targetNameSO, targetNameReal, targetNameImport,
  209. this->LocalGenerator->ConfigurationName.c_str());
  210. // Construct the full path version of the names.
  211. std::string outpath = this->LocalGenerator->LibraryOutputPath;
  212. if(outpath.length() == 0)
  213. {
  214. outpath = this->Makefile->GetStartOutputDirectory();
  215. outpath += "/";
  216. }
  217. if(relink)
  218. {
  219. outpath = this->Makefile->GetStartOutputDirectory();
  220. outpath += cmake::GetCMakeFilesDirectory();
  221. outpath += "/CMakeRelink.dir";
  222. cmSystemTools::MakeDirectory(outpath.c_str());
  223. outpath += "/";
  224. }
  225. std::string targetFullPath = outpath + targetName;
  226. std::string targetFullPathPDB =
  227. outpath + this->Target->GetName() + std::string(".pdb");
  228. std::string targetFullPathSO = outpath + targetNameSO;
  229. std::string targetFullPathReal = outpath + targetNameReal;
  230. std::string targetFullPathImport = outpath + targetNameImport;
  231. // Construct the output path version of the names for use in command
  232. // arguments.
  233. std::string targetOutPathPDB =
  234. this->Convert(targetFullPathPDB.c_str(),cmLocalGenerator::FULL,
  235. cmLocalGenerator::MAKEFILE);
  236. std::string targetOutPath =
  237. this->Convert(targetFullPath.c_str(),cmLocalGenerator::START_OUTPUT,
  238. cmLocalGenerator::MAKEFILE);
  239. std::string targetOutPathSO =
  240. this->Convert(targetFullPathSO.c_str(),cmLocalGenerator::START_OUTPUT,
  241. cmLocalGenerator::MAKEFILE);
  242. std::string targetOutPathReal =
  243. this->Convert(targetFullPathReal.c_str(),cmLocalGenerator::START_OUTPUT,
  244. cmLocalGenerator::MAKEFILE);
  245. std::string targetOutPathImport =
  246. this->Convert(targetFullPathImport.c_str(),cmLocalGenerator::START_OUTPUT,
  247. cmLocalGenerator::MAKEFILE);
  248. // Add the link message.
  249. std::string buildEcho = "Linking ";
  250. buildEcho += linkLanguage;
  251. const char* forbiddenFlagVar = 0;
  252. switch(this->Target->GetType())
  253. {
  254. case cmTarget::STATIC_LIBRARY:
  255. buildEcho += " static library ";
  256. break;
  257. case cmTarget::SHARED_LIBRARY:
  258. forbiddenFlagVar = "_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS";
  259. buildEcho += " shared library ";
  260. break;
  261. case cmTarget::MODULE_LIBRARY:
  262. forbiddenFlagVar = "_CREATE_SHARED_MODULE_FORBIDDEN_FLAGS";
  263. buildEcho += " shared module ";
  264. break;
  265. default:
  266. buildEcho += " library ";
  267. break;
  268. }
  269. buildEcho += targetOutPath.c_str();
  270. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  271. cmLocalUnixMakefileGenerator3::EchoLink);
  272. // Construct a list of files associated with this library that may
  273. // need to be cleaned.
  274. std::vector<std::string> libCleanFiles;
  275. if(this->Target->GetPropertyAsBool("CLEAN_DIRECT_OUTPUT"))
  276. {
  277. // The user has requested that only the files directly built
  278. // by this target be cleaned instead of all possible names.
  279. libCleanFiles.push_back(this->Convert(targetFullPath.c_str(),
  280. cmLocalGenerator::START_OUTPUT,
  281. cmLocalGenerator::UNCHANGED));
  282. if(targetNameReal != targetName)
  283. {
  284. libCleanFiles.push_back(this->Convert(targetFullPathReal.c_str(),
  285. cmLocalGenerator::START_OUTPUT,
  286. cmLocalGenerator::UNCHANGED));
  287. }
  288. if(targetNameSO != targetName &&
  289. targetNameSO != targetNameReal)
  290. {
  291. libCleanFiles.push_back(this->Convert(targetFullPathSO.c_str(),
  292. cmLocalGenerator::START_OUTPUT,
  293. cmLocalGenerator::UNCHANGED));
  294. }
  295. if(!targetNameImport.empty() &&
  296. targetNameImport != targetName &&
  297. targetNameImport != targetNameReal &&
  298. targetNameImport != targetNameSO)
  299. {
  300. libCleanFiles.push_back(this->Convert(targetFullPathImport.c_str(),
  301. cmLocalGenerator::START_OUTPUT,
  302. cmLocalGenerator::UNCHANGED));
  303. }
  304. }
  305. else
  306. {
  307. // This target may switch between static and shared based
  308. // on a user option or the BUILD_SHARED_LIBS switch. Clean
  309. // all possible names.
  310. std::string cleanStaticName;
  311. std::string cleanSharedName;
  312. std::string cleanSharedSOName;
  313. std::string cleanSharedRealName;
  314. std::string cleanImportName;
  315. this->Target->GetLibraryCleanNames(
  316. cleanStaticName,
  317. cleanSharedName,
  318. cleanSharedSOName,
  319. cleanSharedRealName,
  320. cleanImportName,
  321. this->LocalGenerator->ConfigurationName.c_str());
  322. std::string cleanFullStaticName = outpath + cleanStaticName;
  323. std::string cleanFullSharedName = outpath + cleanSharedName;
  324. std::string cleanFullSharedSOName = outpath + cleanSharedSOName;
  325. std::string cleanFullSharedRealName = outpath + cleanSharedRealName;
  326. std::string cleanFullImportName = outpath + cleanImportName;
  327. libCleanFiles.push_back
  328. (this->Convert(cleanFullStaticName.c_str(),
  329. cmLocalGenerator::START_OUTPUT,
  330. cmLocalGenerator::UNCHANGED));
  331. if(cleanSharedRealName != cleanStaticName)
  332. {
  333. libCleanFiles.push_back(this->Convert(cleanFullSharedRealName.c_str(),
  334. cmLocalGenerator::START_OUTPUT,
  335. cmLocalGenerator::UNCHANGED));
  336. }
  337. if(cleanSharedSOName != cleanStaticName &&
  338. cleanSharedSOName != cleanSharedRealName)
  339. {
  340. libCleanFiles.push_back(this->Convert(cleanFullSharedSOName.c_str(),
  341. cmLocalGenerator::START_OUTPUT,
  342. cmLocalGenerator::UNCHANGED));
  343. }
  344. if(cleanSharedName != cleanStaticName &&
  345. cleanSharedName != cleanSharedSOName &&
  346. cleanSharedName != cleanSharedRealName)
  347. {
  348. libCleanFiles.push_back(this->Convert(cleanFullSharedName.c_str(),
  349. cmLocalGenerator::START_OUTPUT,
  350. cmLocalGenerator::UNCHANGED));
  351. }
  352. if(!cleanImportName.empty() &&
  353. cleanImportName != cleanStaticName &&
  354. cleanImportName != cleanSharedSOName &&
  355. cleanImportName != cleanSharedRealName &&
  356. cleanImportName != cleanSharedName)
  357. {
  358. libCleanFiles.push_back(this->Convert(cleanFullImportName.c_str(),
  359. cmLocalGenerator::START_OUTPUT,
  360. cmLocalGenerator::UNCHANGED));
  361. }
  362. }
  363. #ifdef _WIN32
  364. // There may be a manifest file for this target. Add it to the
  365. // clean set just in case.
  366. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  367. {
  368. libCleanFiles.push_back(
  369. this->Convert((targetFullPath+".manifest").c_str(),
  370. cmLocalGenerator::START_OUTPUT,
  371. cmLocalGenerator::UNCHANGED));
  372. }
  373. #endif
  374. // Add a command to remove any existing files for this library.
  375. std::vector<std::string> commands1;
  376. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  377. *this->Target, "target");
  378. this->LocalGenerator->CreateCDCommand
  379. (commands1,
  380. this->Makefile->GetStartOutputDirectory(),
  381. this->Makefile->GetHomeOutputDirectory());
  382. commands.insert(commands.end(), commands1.begin(), commands1.end());
  383. commands1.clear();
  384. // Add the pre-build and pre-link rules building but not when relinking.
  385. if(!relink)
  386. {
  387. this->LocalGenerator
  388. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands());
  389. this->LocalGenerator
  390. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands());
  391. }
  392. // Open the link script if it will be used.
  393. bool useLinkScript = false;
  394. std::string linkScriptName;
  395. std::auto_ptr<cmGeneratedFileStream> linkScriptStream;
  396. if(this->GlobalGenerator->GetUseLinkScript() &&
  397. (this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  398. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  399. this->Target->GetType() == cmTarget::MODULE_LIBRARY))
  400. {
  401. useLinkScript = true;
  402. linkScriptName = this->TargetBuildDirectoryFull;
  403. if(relink)
  404. {
  405. linkScriptName += "/relink.txt";
  406. }
  407. else
  408. {
  409. linkScriptName += "/link.txt";
  410. }
  411. std::auto_ptr<cmGeneratedFileStream> lss(
  412. new cmGeneratedFileStream(linkScriptName.c_str()));
  413. linkScriptStream = lss;
  414. }
  415. std::vector<std::string> link_script_commands;
  416. // Construct the main link rule.
  417. std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
  418. if(useLinkScript)
  419. {
  420. cmSystemTools::ExpandListArgument(linkRule, link_script_commands);
  421. std::string link_command = "$(CMAKE_COMMAND) -E cmake_link_script ";
  422. link_command += this->Convert(linkScriptName.c_str(),
  423. cmLocalGenerator::START_OUTPUT,
  424. cmLocalGenerator::SHELL);
  425. link_command += " --verbose=$(VERBOSE)";
  426. commands1.push_back(link_command);
  427. }
  428. else
  429. {
  430. cmSystemTools::ExpandListArgument(linkRule, commands1);
  431. }
  432. this->LocalGenerator->CreateCDCommand
  433. (commands1,
  434. this->Makefile->GetStartOutputDirectory(),
  435. this->Makefile->GetHomeOutputDirectory());
  436. commands.insert(commands.end(), commands1.begin(), commands1.end());
  437. // Add a rule to create necessary symlinks for the library.
  438. if(targetOutPath != targetOutPathReal)
  439. {
  440. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  441. symlink += targetOutPathReal;
  442. symlink += " ";
  443. symlink += targetOutPathSO;
  444. symlink += " ";
  445. symlink += targetOutPath;
  446. commands1.clear();
  447. commands1.push_back(symlink);
  448. this->LocalGenerator->CreateCDCommand(commands1,
  449. this->Makefile->GetStartOutputDirectory(),
  450. this->Makefile->GetHomeOutputDirectory());
  451. commands.insert(commands.end(), commands1.begin(), commands1.end());
  452. }
  453. // Add the post-build rules when building but not when relinking.
  454. if(!relink)
  455. {
  456. this->LocalGenerator->
  457. AppendCustomCommands(commands, this->Target->GetPostBuildCommands());
  458. }
  459. // Collect up flags to link in needed libraries.
  460. cmOStringStream linklibs;
  461. this->LocalGenerator->OutputLinkLibraries(linklibs, *this->Target, relink);
  462. // Construct object file lists that may be needed to expand the
  463. // rule.
  464. std::string variableName;
  465. std::string variableNameExternal;
  466. this->WriteObjectsVariable(variableName, variableNameExternal);
  467. std::string buildObjs;
  468. if(useLinkScript)
  469. {
  470. this->WriteObjectsString(buildObjs);
  471. }
  472. else
  473. {
  474. buildObjs = "$(";
  475. buildObjs += variableName;
  476. buildObjs += ") $(";
  477. buildObjs += variableNameExternal;
  478. buildObjs += ")";
  479. }
  480. std::string cleanObjs = "$(";
  481. cleanObjs += variableName;
  482. cleanObjs += ")";
  483. cmLocalGenerator::RuleVariables vars;
  484. vars.TargetPDB = targetOutPathPDB.c_str();
  485. // Setup the target version.
  486. std::string targetVersionMajor;
  487. std::string targetVersionMinor;
  488. {
  489. cmOStringStream majorStream;
  490. cmOStringStream minorStream;
  491. int major;
  492. int minor;
  493. this->Target->GetTargetVersion(major, minor);
  494. majorStream << major;
  495. minorStream << minor;
  496. targetVersionMajor = majorStream.str();
  497. targetVersionMinor = minorStream.str();
  498. }
  499. vars.TargetVersionMajor = targetVersionMajor.c_str();
  500. vars.TargetVersionMinor = targetVersionMinor.c_str();
  501. vars.Language = linkLanguage;
  502. vars.Objects = buildObjs.c_str();
  503. std::string objdir = cmake::GetCMakeFilesDirectoryPostSlash();
  504. objdir += this->Target->GetName();
  505. objdir += ".dir";
  506. vars.ObjectDir = objdir.c_str();
  507. std::string targetLinkScriptPathReal;
  508. if(useLinkScript)
  509. {
  510. // Paths in the link script are interpreted directly by the shell
  511. // and not make.
  512. targetLinkScriptPathReal =
  513. this->Convert(targetFullPathReal.c_str(),
  514. cmLocalGenerator::START_OUTPUT,
  515. cmLocalGenerator::SHELL);
  516. vars.Target = targetLinkScriptPathReal.c_str();
  517. }
  518. else
  519. {
  520. vars.Target = targetOutPathReal.c_str();
  521. }
  522. std::string linkString = linklibs.str();
  523. vars.LinkLibraries = linkString.c_str();
  524. vars.ObjectsQuoted = buildObjs.c_str();
  525. vars.TargetSOName= targetNameSO.c_str();
  526. vars.LinkFlags = linkFlags.c_str();
  527. // Compute the directory portion of the install_name setting.
  528. std::string install_name_dir;
  529. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY)
  530. {
  531. // Get the install_name directory for the build tree.
  532. const char* config = this->LocalGenerator->ConfigurationName.c_str();
  533. install_name_dir = this->Target->GetInstallNameDirForBuildTree(config);
  534. // Set the rule variable replacement value.
  535. if(install_name_dir.empty())
  536. {
  537. vars.TargetInstallNameDir = "";
  538. }
  539. else
  540. {
  541. // Convert to a path for the native build tool.
  542. install_name_dir =
  543. this->LocalGenerator->Convert(install_name_dir.c_str(),
  544. cmLocalGenerator::NONE,
  545. cmLocalGenerator::SHELL, false);
  546. vars.TargetInstallNameDir = install_name_dir.c_str();
  547. }
  548. }
  549. std::string langFlags;
  550. this->LocalGenerator
  551. ->AddLanguageFlags(langFlags, linkLanguage,
  552. this->LocalGenerator->ConfigurationName.c_str());
  553. // remove any language flags that might not work with the
  554. // particular os
  555. if(forbiddenFlagVar)
  556. {
  557. this->RemoveForbiddenFlags(forbiddenFlagVar,
  558. linkLanguage, langFlags);
  559. }
  560. vars.LanguageCompileFlags = langFlags.c_str();
  561. // Expand placeholders in the commands.
  562. this->LocalGenerator->TargetImplib = targetOutPathImport;
  563. if(useLinkScript)
  564. {
  565. for(std::vector<std::string>::iterator i = link_script_commands.begin();
  566. i != link_script_commands.end(); ++i)
  567. {
  568. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  569. }
  570. }
  571. else
  572. {
  573. for(std::vector<std::string>::iterator i = commands.begin();
  574. i != commands.end(); ++i)
  575. {
  576. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  577. }
  578. }
  579. this->LocalGenerator->TargetImplib = "";
  580. // Optionally convert the build rule to use a script to avoid long
  581. // command lines in the make shell.
  582. if(useLinkScript)
  583. {
  584. for(std::vector<std::string>::iterator cmd = link_script_commands.begin();
  585. cmd != link_script_commands.end(); ++cmd)
  586. {
  587. // Do not write out empty commands or commands beginning in the
  588. // shell no-op ":".
  589. if(!cmd->empty() && (*cmd)[0] != ':')
  590. {
  591. (*linkScriptStream) << *cmd << "\n";
  592. }
  593. }
  594. }
  595. // Write the build rule.
  596. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  597. targetFullPathReal.c_str(),
  598. depends, commands, false);
  599. // The symlink names for the target should depend on the real target
  600. // so if the target version changes it rebuilds and recreates the
  601. // symlinks.
  602. if(targetFullPathSO != targetFullPathReal)
  603. {
  604. depends.clear();
  605. commands.clear();
  606. depends.push_back(targetFullPathReal.c_str());
  607. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  608. targetFullPathSO.c_str(),
  609. depends, commands, false);
  610. }
  611. if(targetFullPath != targetFullPathSO)
  612. {
  613. depends.clear();
  614. commands.clear();
  615. depends.push_back(targetFullPathSO.c_str());
  616. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  617. targetFullPath.c_str(),
  618. depends, commands, false);
  619. }
  620. // Write the main driver rule to build everything in this target.
  621. this->WriteTargetDriverRule(targetFullPath.c_str(), relink);
  622. // Clean all the possible library names and symlinks and object files.
  623. this->CleanFiles.insert(this->CleanFiles.end(),
  624. libCleanFiles.begin(),libCleanFiles.end());
  625. this->CleanFiles.insert(this->CleanFiles.end(),
  626. this->Objects.begin(),
  627. this->Objects.end());
  628. }