cmMakefileLibraryTargetGenerator.cxx 23 KB

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