cmMakefileLibraryTargetGenerator.cxx 18 KB

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