1
0

cmMakefileLibraryTargetGenerator.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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. outpathImp = outpath;
  222. }
  223. else
  224. {
  225. outpath = this->Target->GetDirectory();
  226. outpath += "/";
  227. outpathImp = this->Target->GetDirectory(0, true);
  228. outpathImp += "/";
  229. }
  230. std::string targetFullPath = outpath + targetName;
  231. std::string targetFullPathPDB = outpath + targetNamePDB;
  232. std::string targetFullPathSO = outpath + targetNameSO;
  233. std::string targetFullPathReal = outpath + targetNameReal;
  234. std::string targetFullPathImport = outpathImp + targetNameImport;
  235. // Construct the output path version of the names for use in command
  236. // arguments.
  237. std::string targetOutPathPDB =
  238. this->Convert(targetFullPathPDB.c_str(),cmLocalGenerator::FULL,
  239. cmLocalGenerator::SHELL);
  240. std::string targetOutPath =
  241. this->Convert(targetFullPath.c_str(),cmLocalGenerator::START_OUTPUT,
  242. cmLocalGenerator::SHELL);
  243. std::string targetOutPathSO =
  244. this->Convert(targetFullPathSO.c_str(),cmLocalGenerator::START_OUTPUT,
  245. cmLocalGenerator::SHELL);
  246. std::string targetOutPathReal =
  247. this->Convert(targetFullPathReal.c_str(),cmLocalGenerator::START_OUTPUT,
  248. cmLocalGenerator::SHELL);
  249. std::string targetOutPathImport =
  250. this->Convert(targetFullPathImport.c_str(),cmLocalGenerator::START_OUTPUT,
  251. cmLocalGenerator::SHELL);
  252. // Add the link message.
  253. std::string buildEcho = "Linking ";
  254. buildEcho += linkLanguage;
  255. const char* forbiddenFlagVar = 0;
  256. switch(this->Target->GetType())
  257. {
  258. case cmTarget::STATIC_LIBRARY:
  259. buildEcho += " static library ";
  260. break;
  261. case cmTarget::SHARED_LIBRARY:
  262. forbiddenFlagVar = "_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS";
  263. buildEcho += " shared library ";
  264. break;
  265. case cmTarget::MODULE_LIBRARY:
  266. forbiddenFlagVar = "_CREATE_SHARED_MODULE_FORBIDDEN_FLAGS";
  267. buildEcho += " shared module ";
  268. break;
  269. default:
  270. buildEcho += " library ";
  271. break;
  272. }
  273. buildEcho += targetOutPath.c_str();
  274. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  275. cmLocalUnixMakefileGenerator3::EchoLink);
  276. // Construct a list of files associated with this library that may
  277. // need to be cleaned.
  278. std::vector<std::string> libCleanFiles;
  279. if(this->Target->GetPropertyAsBool("CLEAN_DIRECT_OUTPUT"))
  280. {
  281. // The user has requested that only the files directly built
  282. // by this target be cleaned instead of all possible names.
  283. libCleanFiles.push_back(this->Convert(targetFullPath.c_str(),
  284. cmLocalGenerator::START_OUTPUT,
  285. cmLocalGenerator::UNCHANGED));
  286. if(targetNameReal != targetName)
  287. {
  288. libCleanFiles.push_back(this->Convert(targetFullPathReal.c_str(),
  289. cmLocalGenerator::START_OUTPUT,
  290. cmLocalGenerator::UNCHANGED));
  291. }
  292. if(targetNameSO != targetName &&
  293. targetNameSO != targetNameReal)
  294. {
  295. libCleanFiles.push_back(this->Convert(targetFullPathSO.c_str(),
  296. cmLocalGenerator::START_OUTPUT,
  297. cmLocalGenerator::UNCHANGED));
  298. }
  299. if(!targetNameImport.empty() &&
  300. targetNameImport != targetName &&
  301. targetNameImport != targetNameReal &&
  302. targetNameImport != targetNameSO)
  303. {
  304. libCleanFiles.push_back(this->Convert(targetFullPathImport.c_str(),
  305. cmLocalGenerator::START_OUTPUT,
  306. cmLocalGenerator::UNCHANGED));
  307. }
  308. }
  309. else
  310. {
  311. // This target may switch between static and shared based
  312. // on a user option or the BUILD_SHARED_LIBS switch. Clean
  313. // all possible names.
  314. std::string cleanStaticName;
  315. std::string cleanSharedName;
  316. std::string cleanSharedSOName;
  317. std::string cleanSharedRealName;
  318. std::string cleanImportName;
  319. std::string cleanPDBName;
  320. this->Target->GetLibraryCleanNames(
  321. cleanStaticName,
  322. cleanSharedName,
  323. cleanSharedSOName,
  324. cleanSharedRealName,
  325. cleanImportName,
  326. cleanPDBName,
  327. this->LocalGenerator->ConfigurationName.c_str());
  328. std::string cleanFullStaticName = outpath + cleanStaticName;
  329. std::string cleanFullSharedName = outpath + cleanSharedName;
  330. std::string cleanFullSharedSOName = outpath + cleanSharedSOName;
  331. std::string cleanFullSharedRealName = outpath + cleanSharedRealName;
  332. std::string cleanFullImportName = outpath + cleanImportName;
  333. std::string cleanFullPDBName = outpath + cleanPDBName;
  334. libCleanFiles.push_back
  335. (this->Convert(cleanFullStaticName.c_str(),
  336. cmLocalGenerator::START_OUTPUT,
  337. cmLocalGenerator::UNCHANGED));
  338. if(cleanSharedRealName != cleanStaticName)
  339. {
  340. libCleanFiles.push_back(this->Convert(cleanFullSharedRealName.c_str(),
  341. cmLocalGenerator::START_OUTPUT,
  342. cmLocalGenerator::UNCHANGED));
  343. }
  344. if(cleanSharedSOName != cleanStaticName &&
  345. cleanSharedSOName != cleanSharedRealName)
  346. {
  347. libCleanFiles.push_back(this->Convert(cleanFullSharedSOName.c_str(),
  348. cmLocalGenerator::START_OUTPUT,
  349. cmLocalGenerator::UNCHANGED));
  350. }
  351. if(cleanSharedName != cleanStaticName &&
  352. cleanSharedName != cleanSharedSOName &&
  353. cleanSharedName != cleanSharedRealName)
  354. {
  355. libCleanFiles.push_back(this->Convert(cleanFullSharedName.c_str(),
  356. cmLocalGenerator::START_OUTPUT,
  357. cmLocalGenerator::UNCHANGED));
  358. }
  359. if(!cleanImportName.empty() &&
  360. cleanImportName != cleanStaticName &&
  361. cleanImportName != cleanSharedSOName &&
  362. cleanImportName != cleanSharedRealName &&
  363. cleanImportName != cleanSharedName)
  364. {
  365. libCleanFiles.push_back(this->Convert(cleanFullImportName.c_str(),
  366. cmLocalGenerator::START_OUTPUT,
  367. cmLocalGenerator::UNCHANGED));
  368. }
  369. // List the PDB for cleaning only when the whole target is
  370. // cleaned. We do not want to delete the .pdb file just before
  371. // linking the target.
  372. this->CleanFiles.push_back
  373. (this->Convert(cleanFullPDBName.c_str(),
  374. cmLocalGenerator::START_OUTPUT,
  375. cmLocalGenerator::UNCHANGED));
  376. }
  377. #ifdef _WIN32
  378. // There may be a manifest file for this target. Add it to the
  379. // clean set just in case.
  380. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  381. {
  382. libCleanFiles.push_back(
  383. this->Convert((targetFullPath+".manifest").c_str(),
  384. cmLocalGenerator::START_OUTPUT,
  385. cmLocalGenerator::UNCHANGED));
  386. }
  387. #endif
  388. // Add a command to remove any existing files for this library.
  389. std::vector<std::string> commands1;
  390. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  391. *this->Target, "target");
  392. this->LocalGenerator->CreateCDCommand
  393. (commands1,
  394. this->Makefile->GetStartOutputDirectory(),
  395. this->Makefile->GetHomeOutputDirectory());
  396. commands.insert(commands.end(), commands1.begin(), commands1.end());
  397. commands1.clear();
  398. // Add the pre-build and pre-link rules building but not when relinking.
  399. if(!relink)
  400. {
  401. this->LocalGenerator
  402. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands());
  403. this->LocalGenerator
  404. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands());
  405. }
  406. // Open the link script if it will be used.
  407. bool useLinkScript = false;
  408. std::string linkScriptName;
  409. std::auto_ptr<cmGeneratedFileStream> linkScriptStream;
  410. if(this->GlobalGenerator->GetUseLinkScript() &&
  411. (this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  412. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  413. this->Target->GetType() == cmTarget::MODULE_LIBRARY))
  414. {
  415. useLinkScript = true;
  416. linkScriptName = this->TargetBuildDirectoryFull;
  417. if(relink)
  418. {
  419. linkScriptName += "/relink.txt";
  420. }
  421. else
  422. {
  423. linkScriptName += "/link.txt";
  424. }
  425. std::auto_ptr<cmGeneratedFileStream> lss(
  426. new cmGeneratedFileStream(linkScriptName.c_str()));
  427. linkScriptStream = lss;
  428. }
  429. std::vector<std::string> link_script_commands;
  430. // Construct the main link rule.
  431. std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
  432. if(useLinkScript)
  433. {
  434. cmSystemTools::ExpandListArgument(linkRule, link_script_commands);
  435. std::string link_command = "$(CMAKE_COMMAND) -E cmake_link_script ";
  436. link_command += this->Convert(linkScriptName.c_str(),
  437. cmLocalGenerator::START_OUTPUT,
  438. cmLocalGenerator::SHELL);
  439. link_command += " --verbose=$(VERBOSE)";
  440. commands1.push_back(link_command);
  441. }
  442. else
  443. {
  444. cmSystemTools::ExpandListArgument(linkRule, commands1);
  445. }
  446. this->LocalGenerator->CreateCDCommand
  447. (commands1,
  448. this->Makefile->GetStartOutputDirectory(),
  449. this->Makefile->GetHomeOutputDirectory());
  450. commands.insert(commands.end(), commands1.begin(), commands1.end());
  451. // Add a rule to create necessary symlinks for the library.
  452. if(targetOutPath != targetOutPathReal)
  453. {
  454. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  455. symlink += targetOutPathReal;
  456. symlink += " ";
  457. symlink += targetOutPathSO;
  458. symlink += " ";
  459. symlink += targetOutPath;
  460. commands1.clear();
  461. commands1.push_back(symlink);
  462. this->LocalGenerator->CreateCDCommand(commands1,
  463. this->Makefile->GetStartOutputDirectory(),
  464. this->Makefile->GetHomeOutputDirectory());
  465. commands.insert(commands.end(), commands1.begin(), commands1.end());
  466. }
  467. // Add the post-build rules when building but not when relinking.
  468. if(!relink)
  469. {
  470. this->LocalGenerator->
  471. AppendCustomCommands(commands, this->Target->GetPostBuildCommands());
  472. }
  473. // Collect up flags to link in needed libraries.
  474. cmOStringStream linklibs;
  475. this->LocalGenerator->OutputLinkLibraries(linklibs, *this->Target, relink);
  476. // Construct object file lists that may be needed to expand the
  477. // rule.
  478. std::string variableName;
  479. std::string variableNameExternal;
  480. this->WriteObjectsVariable(variableName, variableNameExternal);
  481. std::string buildObjs;
  482. if(useLinkScript)
  483. {
  484. this->WriteObjectsString(buildObjs);
  485. }
  486. else
  487. {
  488. buildObjs = "$(";
  489. buildObjs += variableName;
  490. buildObjs += ") $(";
  491. buildObjs += variableNameExternal;
  492. buildObjs += ")";
  493. }
  494. std::string cleanObjs = "$(";
  495. cleanObjs += variableName;
  496. cleanObjs += ")";
  497. cmLocalGenerator::RuleVariables vars;
  498. vars.TargetPDB = targetOutPathPDB.c_str();
  499. // Setup the target version.
  500. std::string targetVersionMajor;
  501. std::string targetVersionMinor;
  502. {
  503. cmOStringStream majorStream;
  504. cmOStringStream minorStream;
  505. int major;
  506. int minor;
  507. this->Target->GetTargetVersion(major, minor);
  508. majorStream << major;
  509. minorStream << minor;
  510. targetVersionMajor = majorStream.str();
  511. targetVersionMinor = minorStream.str();
  512. }
  513. vars.TargetVersionMajor = targetVersionMajor.c_str();
  514. vars.TargetVersionMinor = targetVersionMinor.c_str();
  515. vars.Language = linkLanguage;
  516. vars.Objects = buildObjs.c_str();
  517. std::string objdir = cmake::GetCMakeFilesDirectoryPostSlash();
  518. objdir += this->Target->GetName();
  519. objdir += ".dir";
  520. vars.ObjectDir = objdir.c_str();
  521. vars.Target = targetOutPathReal.c_str();
  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. }