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 "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. targetNameImport != targetName &&
  307. targetNameImport != targetNameReal &&
  308. targetNameImport != targetNameSO)
  309. {
  310. libCleanFiles.push_back(this->Convert(targetFullPathImport.c_str(),
  311. cmLocalGenerator::START_OUTPUT,
  312. cmLocalGenerator::UNCHANGED));
  313. }
  314. }
  315. else
  316. {
  317. // This target may switch between static and shared based
  318. // on a user option or the BUILD_SHARED_LIBS switch. Clean
  319. // all possible names.
  320. std::string cleanStaticName;
  321. std::string cleanSharedName;
  322. std::string cleanSharedSOName;
  323. std::string cleanSharedRealName;
  324. std::string cleanImportName;
  325. std::string cleanPDBName;
  326. this->Target->GetLibraryCleanNames(
  327. cleanStaticName,
  328. cleanSharedName,
  329. cleanSharedSOName,
  330. cleanSharedRealName,
  331. cleanImportName,
  332. cleanPDBName,
  333. this->LocalGenerator->ConfigurationName.c_str());
  334. std::string cleanFullStaticName = outpath + cleanStaticName;
  335. std::string cleanFullSharedName = outpath + cleanSharedName;
  336. std::string cleanFullSharedSOName = outpath + cleanSharedSOName;
  337. std::string cleanFullSharedRealName = outpath + cleanSharedRealName;
  338. std::string cleanFullImportName = outpathImp + cleanImportName;
  339. std::string cleanFullPDBName = outpath + cleanPDBName;
  340. libCleanFiles.push_back
  341. (this->Convert(cleanFullStaticName.c_str(),
  342. cmLocalGenerator::START_OUTPUT,
  343. cmLocalGenerator::UNCHANGED));
  344. if(cleanSharedRealName != cleanStaticName)
  345. {
  346. libCleanFiles.push_back(this->Convert(cleanFullSharedRealName.c_str(),
  347. cmLocalGenerator::START_OUTPUT,
  348. cmLocalGenerator::UNCHANGED));
  349. }
  350. if(cleanSharedSOName != cleanStaticName &&
  351. cleanSharedSOName != cleanSharedRealName)
  352. {
  353. libCleanFiles.push_back(this->Convert(cleanFullSharedSOName.c_str(),
  354. cmLocalGenerator::START_OUTPUT,
  355. cmLocalGenerator::UNCHANGED));
  356. }
  357. if(cleanSharedName != cleanStaticName &&
  358. cleanSharedName != cleanSharedSOName &&
  359. cleanSharedName != cleanSharedRealName)
  360. {
  361. libCleanFiles.push_back(this->Convert(cleanFullSharedName.c_str(),
  362. cmLocalGenerator::START_OUTPUT,
  363. cmLocalGenerator::UNCHANGED));
  364. }
  365. if(!cleanImportName.empty() &&
  366. cleanImportName != cleanStaticName &&
  367. cleanImportName != cleanSharedSOName &&
  368. cleanImportName != cleanSharedRealName &&
  369. cleanImportName != cleanSharedName)
  370. {
  371. libCleanFiles.push_back(this->Convert(cleanFullImportName.c_str(),
  372. cmLocalGenerator::START_OUTPUT,
  373. cmLocalGenerator::UNCHANGED));
  374. }
  375. // List the PDB for cleaning only when the whole target is
  376. // cleaned. We do not want to delete the .pdb file just before
  377. // linking the target.
  378. this->CleanFiles.push_back
  379. (this->Convert(cleanFullPDBName.c_str(),
  380. cmLocalGenerator::START_OUTPUT,
  381. cmLocalGenerator::UNCHANGED));
  382. }
  383. #ifdef _WIN32
  384. // There may be a manifest file for this target. Add it to the
  385. // clean set just in case.
  386. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  387. {
  388. libCleanFiles.push_back(
  389. this->Convert((targetFullPath+".manifest").c_str(),
  390. cmLocalGenerator::START_OUTPUT,
  391. cmLocalGenerator::UNCHANGED));
  392. }
  393. #endif
  394. // Add a command to remove any existing files for this library.
  395. std::vector<std::string> commands1;
  396. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  397. *this->Target, "target");
  398. this->LocalGenerator->CreateCDCommand
  399. (commands1,
  400. this->Makefile->GetStartOutputDirectory(),
  401. this->Makefile->GetHomeOutputDirectory());
  402. commands.insert(commands.end(), commands1.begin(), commands1.end());
  403. commands1.clear();
  404. // Add the pre-build and pre-link rules building but not when relinking.
  405. if(!relink)
  406. {
  407. this->LocalGenerator
  408. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands());
  409. this->LocalGenerator
  410. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands());
  411. }
  412. // Open the link script if it will be used.
  413. bool useLinkScript = false;
  414. std::string linkScriptName;
  415. std::auto_ptr<cmGeneratedFileStream> linkScriptStream;
  416. if(this->GlobalGenerator->GetUseLinkScript() &&
  417. (this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  418. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  419. this->Target->GetType() == cmTarget::MODULE_LIBRARY))
  420. {
  421. useLinkScript = true;
  422. linkScriptName = this->TargetBuildDirectoryFull;
  423. if(relink)
  424. {
  425. linkScriptName += "/relink.txt";
  426. }
  427. else
  428. {
  429. linkScriptName += "/link.txt";
  430. }
  431. std::auto_ptr<cmGeneratedFileStream> lss(
  432. new cmGeneratedFileStream(linkScriptName.c_str()));
  433. linkScriptStream = lss;
  434. }
  435. std::vector<std::string> link_script_commands;
  436. // Construct the main link rule.
  437. std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
  438. if(useLinkScript)
  439. {
  440. cmSystemTools::ExpandListArgument(linkRule, link_script_commands);
  441. std::string link_command = "$(CMAKE_COMMAND) -E cmake_link_script ";
  442. link_command += this->Convert(linkScriptName.c_str(),
  443. cmLocalGenerator::START_OUTPUT,
  444. cmLocalGenerator::SHELL);
  445. link_command += " --verbose=$(VERBOSE)";
  446. commands1.push_back(link_command);
  447. }
  448. else
  449. {
  450. cmSystemTools::ExpandListArgument(linkRule, commands1);
  451. }
  452. this->LocalGenerator->CreateCDCommand
  453. (commands1,
  454. this->Makefile->GetStartOutputDirectory(),
  455. this->Makefile->GetHomeOutputDirectory());
  456. commands.insert(commands.end(), commands1.begin(), commands1.end());
  457. // Add a rule to create necessary symlinks for the library.
  458. if(targetOutPath != targetOutPathReal)
  459. {
  460. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  461. symlink += targetOutPathReal;
  462. symlink += " ";
  463. symlink += targetOutPathSO;
  464. symlink += " ";
  465. symlink += targetOutPath;
  466. commands1.clear();
  467. commands1.push_back(symlink);
  468. this->LocalGenerator->CreateCDCommand(commands1,
  469. this->Makefile->GetStartOutputDirectory(),
  470. this->Makefile->GetHomeOutputDirectory());
  471. commands.insert(commands.end(), commands1.begin(), commands1.end());
  472. }
  473. // Add the post-build rules when building but not when relinking.
  474. if(!relink)
  475. {
  476. this->LocalGenerator->
  477. AppendCustomCommands(commands, this->Target->GetPostBuildCommands());
  478. }
  479. // Collect up flags to link in needed libraries.
  480. cmOStringStream linklibs;
  481. this->LocalGenerator->OutputLinkLibraries(linklibs, *this->Target, relink);
  482. // Construct object file lists that may be needed to expand the
  483. // rule.
  484. std::string variableName;
  485. std::string variableNameExternal;
  486. this->WriteObjectsVariable(variableName, variableNameExternal);
  487. std::string buildObjs;
  488. if(useLinkScript)
  489. {
  490. this->WriteObjectsString(buildObjs);
  491. }
  492. else
  493. {
  494. buildObjs = "$(";
  495. buildObjs += variableName;
  496. buildObjs += ") $(";
  497. buildObjs += variableNameExternal;
  498. buildObjs += ")";
  499. }
  500. std::string cleanObjs = "$(";
  501. cleanObjs += variableName;
  502. cleanObjs += ")";
  503. cmLocalGenerator::RuleVariables vars;
  504. vars.TargetPDB = targetOutPathPDB.c_str();
  505. // Setup the target version.
  506. std::string targetVersionMajor;
  507. std::string targetVersionMinor;
  508. {
  509. cmOStringStream majorStream;
  510. cmOStringStream minorStream;
  511. int major;
  512. int minor;
  513. this->Target->GetTargetVersion(major, minor);
  514. majorStream << major;
  515. minorStream << minor;
  516. targetVersionMajor = majorStream.str();
  517. targetVersionMinor = minorStream.str();
  518. }
  519. vars.TargetVersionMajor = targetVersionMajor.c_str();
  520. vars.TargetVersionMinor = targetVersionMinor.c_str();
  521. vars.Language = linkLanguage;
  522. vars.Objects = buildObjs.c_str();
  523. std::string objdir = cmake::GetCMakeFilesDirectoryPostSlash();
  524. objdir += this->Target->GetName();
  525. objdir += ".dir";
  526. vars.ObjectDir = objdir.c_str();
  527. vars.Target = targetOutPathReal.c_str();
  528. std::string linkString = linklibs.str();
  529. vars.LinkLibraries = linkString.c_str();
  530. vars.ObjectsQuoted = buildObjs.c_str();
  531. vars.TargetSOName= targetNameSO.c_str();
  532. vars.LinkFlags = linkFlags.c_str();
  533. // Compute the directory portion of the install_name setting.
  534. std::string install_name_dir;
  535. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY)
  536. {
  537. // Get the install_name directory for the build tree.
  538. const char* config = this->LocalGenerator->ConfigurationName.c_str();
  539. install_name_dir = this->Target->GetInstallNameDirForBuildTree(config);
  540. // Set the rule variable replacement value.
  541. if(install_name_dir.empty())
  542. {
  543. vars.TargetInstallNameDir = "";
  544. }
  545. else
  546. {
  547. // Convert to a path for the native build tool.
  548. install_name_dir =
  549. this->LocalGenerator->Convert(install_name_dir.c_str(),
  550. cmLocalGenerator::NONE,
  551. cmLocalGenerator::SHELL, false);
  552. vars.TargetInstallNameDir = install_name_dir.c_str();
  553. }
  554. }
  555. std::string langFlags;
  556. this->LocalGenerator
  557. ->AddLanguageFlags(langFlags, linkLanguage,
  558. this->LocalGenerator->ConfigurationName.c_str());
  559. // remove any language flags that might not work with the
  560. // particular os
  561. if(forbiddenFlagVar)
  562. {
  563. this->RemoveForbiddenFlags(forbiddenFlagVar,
  564. linkLanguage, langFlags);
  565. }
  566. vars.LanguageCompileFlags = langFlags.c_str();
  567. // Expand placeholders in the commands.
  568. this->LocalGenerator->TargetImplib = targetOutPathImport;
  569. if(useLinkScript)
  570. {
  571. for(std::vector<std::string>::iterator i = link_script_commands.begin();
  572. i != link_script_commands.end(); ++i)
  573. {
  574. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  575. }
  576. }
  577. else
  578. {
  579. for(std::vector<std::string>::iterator i = commands.begin();
  580. i != commands.end(); ++i)
  581. {
  582. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  583. }
  584. }
  585. this->LocalGenerator->TargetImplib = "";
  586. // Optionally convert the build rule to use a script to avoid long
  587. // command lines in the make shell.
  588. if(useLinkScript)
  589. {
  590. for(std::vector<std::string>::iterator cmd = link_script_commands.begin();
  591. cmd != link_script_commands.end(); ++cmd)
  592. {
  593. // Do not write out empty commands or commands beginning in the
  594. // shell no-op ":".
  595. if(!cmd->empty() && (*cmd)[0] != ':')
  596. {
  597. (*linkScriptStream) << *cmd << "\n";
  598. }
  599. }
  600. }
  601. // Write the build rule.
  602. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  603. targetFullPathReal.c_str(),
  604. depends, commands, false);
  605. // Some targets have more than one output file. Create rules to
  606. // drive the build if any extra outputs are missing.
  607. std::vector<std::string> extraOutputs;
  608. if(targetNameSO != targetNameReal)
  609. {
  610. this->GenerateExtraOutput(targetFullPathSO.c_str(),
  611. targetFullPathReal.c_str());
  612. }
  613. if(targetName != targetNameSO &&
  614. targetName != targetNameReal)
  615. {
  616. this->GenerateExtraOutput(targetFullPath.c_str(),
  617. targetFullPathReal.c_str());
  618. }
  619. // Write the main driver rule to build everything in this target.
  620. this->WriteTargetDriverRule(targetFullPath.c_str(), relink);
  621. // Clean all the possible library names and symlinks and object files.
  622. this->CleanFiles.insert(this->CleanFiles.end(),
  623. libCleanFiles.begin(),libCleanFiles.end());
  624. this->CleanFiles.insert(this->CleanFiles.end(),
  625. this->Objects.begin(),
  626. this->Objects.end());
  627. }