cmMakefileLibraryTargetGenerator.cxx 20 KB

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