cmMakefileLibraryTargetGenerator.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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->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->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->ConfigurationName.c_str());
  181. // Construct the full path version of the names.
  182. std::string outpath = this->LocalGenerator->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. const char* forbiddenFlagVar = 0;
  217. switch(this->Target->GetType())
  218. {
  219. case cmTarget::STATIC_LIBRARY:
  220. buildEcho += " static library ";
  221. break;
  222. case cmTarget::SHARED_LIBRARY:
  223. forbiddenFlagVar = "_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS";
  224. buildEcho += " shared library ";
  225. break;
  226. case cmTarget::MODULE_LIBRARY:
  227. forbiddenFlagVar = "_CREATE_SHARED_MODULE_FORBIDDEN_FLAGS";
  228. buildEcho += " shared module ";
  229. break;
  230. default:
  231. buildEcho += " library ";
  232. break;
  233. }
  234. buildEcho += targetOutPath.c_str();
  235. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  236. cmLocalUnixMakefileGenerator3::EchoLink);
  237. // Construct a list of files associated with this library that may
  238. // need to be cleaned.
  239. std::vector<std::string> libCleanFiles;
  240. {
  241. std::string cleanStaticName;
  242. std::string cleanSharedName;
  243. std::string cleanSharedSOName;
  244. std::string cleanSharedRealName;
  245. std::string cleanImportName;
  246. this->Target->GetLibraryCleanNames(
  247. cleanStaticName,
  248. cleanSharedName,
  249. cleanSharedSOName,
  250. cleanSharedRealName,
  251. cleanImportName,
  252. this->LocalGenerator->ConfigurationName.c_str());
  253. std::string cleanFullStaticName = outpath + cleanStaticName;
  254. std::string cleanFullSharedName = outpath + cleanSharedName;
  255. std::string cleanFullSharedSOName = outpath + cleanSharedSOName;
  256. std::string cleanFullSharedRealName = outpath + cleanSharedRealName;
  257. std::string cleanFullImportName = outpath + cleanImportName;
  258. libCleanFiles.push_back
  259. (this->Convert(cleanFullStaticName.c_str(),cmLocalGenerator::START_OUTPUT,
  260. cmLocalGenerator::UNCHANGED));
  261. if(cleanSharedRealName != cleanStaticName)
  262. {
  263. libCleanFiles.push_back(this->Convert(cleanFullSharedRealName.c_str(),
  264. cmLocalGenerator::START_OUTPUT,
  265. cmLocalGenerator::UNCHANGED));
  266. }
  267. if(cleanSharedSOName != cleanStaticName &&
  268. cleanSharedSOName != cleanSharedRealName)
  269. {
  270. libCleanFiles.push_back(this->Convert(cleanFullSharedSOName.c_str(),
  271. cmLocalGenerator::START_OUTPUT,
  272. cmLocalGenerator::UNCHANGED));
  273. }
  274. if(cleanSharedName != cleanStaticName &&
  275. cleanSharedName != cleanSharedSOName &&
  276. cleanSharedName != cleanSharedRealName)
  277. {
  278. libCleanFiles.push_back(this->Convert(cleanFullSharedName.c_str(),
  279. cmLocalGenerator::START_OUTPUT,
  280. cmLocalGenerator::UNCHANGED));
  281. }
  282. if(!cleanImportName.empty() &&
  283. cleanImportName != cleanStaticName &&
  284. cleanImportName != cleanSharedSOName &&
  285. cleanImportName != cleanSharedRealName &&
  286. cleanImportName != cleanSharedName)
  287. {
  288. libCleanFiles.push_back(this->Convert(cleanFullImportName.c_str(),
  289. cmLocalGenerator::START_OUTPUT,
  290. cmLocalGenerator::UNCHANGED));
  291. }
  292. }
  293. // Add a command to remove any existing files for this library.
  294. std::vector<std::string> commands1;
  295. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  296. *this->Target, "target");
  297. this->LocalGenerator->CreateCDCommand(commands1,
  298. this->Makefile->GetStartOutputDirectory(),
  299. this->Makefile->GetHomeOutputDirectory());
  300. commands.insert(commands.end(), commands1.begin(), commands1.end());
  301. commands1.clear();
  302. // Add the pre-build and pre-link rules building but not when relinking.
  303. if(!relink)
  304. {
  305. this->LocalGenerator
  306. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands());
  307. this->LocalGenerator
  308. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands());
  309. }
  310. // Construct the main link rule.
  311. std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
  312. cmSystemTools::ExpandListArgument(linkRule, commands1);
  313. this->LocalGenerator->CreateCDCommand(commands1,
  314. this->Makefile->GetStartOutputDirectory(),
  315. this->Makefile->GetHomeOutputDirectory());
  316. commands.insert(commands.end(), commands1.begin(), commands1.end());
  317. // Add a rule to create necessary symlinks for the library.
  318. if(targetOutPath != targetOutPathReal)
  319. {
  320. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  321. symlink += targetOutPathReal;
  322. symlink += " ";
  323. symlink += targetOutPathSO;
  324. symlink += " ";
  325. symlink += targetOutPath;
  326. commands1.clear();
  327. commands1.push_back(symlink);
  328. this->LocalGenerator->CreateCDCommand(commands1,
  329. this->Makefile->GetStartOutputDirectory(),
  330. this->Makefile->GetHomeOutputDirectory());
  331. commands.insert(commands.end(), commands1.begin(), commands1.end());
  332. }
  333. // Add the post-build rules when building but not when relinking.
  334. if(!relink)
  335. {
  336. this->LocalGenerator->
  337. AppendCustomCommands(commands, this->Target->GetPostBuildCommands());
  338. }
  339. // Collect up flags to link in needed libraries.
  340. cmOStringStream linklibs;
  341. this->LocalGenerator->OutputLinkLibraries(linklibs, *this->Target, relink);
  342. // Construct object file lists that may be needed to expand the
  343. // rule.
  344. std::string variableName;
  345. std::string variableNameExternal;
  346. this->WriteObjectsVariable(variableName, variableNameExternal);
  347. std::string buildObjs = "$(";
  348. buildObjs += variableName;
  349. buildObjs += ") $(";
  350. buildObjs += variableNameExternal;
  351. buildObjs += ")";
  352. std::string cleanObjs = "$(";
  353. cleanObjs += variableName;
  354. cleanObjs += ")";
  355. cmLocalGenerator::RuleVariables vars;
  356. vars.Language = linkLanguage;
  357. vars.Objects = buildObjs.c_str();
  358. vars.Target = targetOutPathReal.c_str();
  359. std::string linkString = linklibs.str();
  360. vars.LinkLibraries = linkString.c_str();
  361. vars.ObjectsQuoted = buildObjs.c_str();
  362. vars.TargetSOName= targetNameSO.c_str();
  363. vars.LinkFlags = linkFlags.c_str();
  364. // Compute the directory portion of the install_name setting.
  365. std::string install_name_dir;
  366. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY)
  367. {
  368. // Select whether to generate an install_name directory for the
  369. // install tree or the build tree.
  370. const char* config = this->LocalGenerator->ConfigurationName.c_str();
  371. if(this->Target->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"))
  372. {
  373. install_name_dir =
  374. this->Target->GetInstallNameDirForInstallTree(config);
  375. }
  376. else
  377. {
  378. install_name_dir =
  379. this->Target->GetInstallNameDirForBuildTree(config);
  380. }
  381. // Set the rule variable replacement value.
  382. if(install_name_dir.empty())
  383. {
  384. vars.TargetInstallNameDir = "";
  385. }
  386. else
  387. {
  388. // Convert to a path for the native build tool.
  389. install_name_dir =
  390. this->LocalGenerator->Convert(install_name_dir.c_str(),
  391. cmLocalGenerator::FULL,
  392. cmLocalGenerator::SHELL, false);
  393. // The Convert method seems to strip trailing slashes, which should
  394. // probably be fixed. Since the only platform supporting install_name
  395. // right now uses forward slashes just add one.
  396. install_name_dir += "/";
  397. vars.TargetInstallNameDir = install_name_dir.c_str();
  398. }
  399. }
  400. std::string langFlags;
  401. this->LocalGenerator
  402. ->AddLanguageFlags(langFlags, linkLanguage,
  403. this->LocalGenerator->ConfigurationName.c_str());
  404. // remove any language flags that might not work with the
  405. // particular os
  406. if(forbiddenFlagVar)
  407. {
  408. this->RemoveForbiddenFlags(forbiddenFlagVar,
  409. linkLanguage, langFlags);
  410. }
  411. vars.LanguageCompileFlags = langFlags.c_str();
  412. // Expand placeholders in the commands.
  413. this->LocalGenerator->TargetImplib = targetOutPathImport;
  414. for(std::vector<std::string>::iterator i = commands.begin();
  415. i != commands.end(); ++i)
  416. {
  417. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  418. }
  419. this->LocalGenerator->TargetImplib = "";
  420. // Write the build rule.
  421. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  422. targetFullPathReal.c_str(),
  423. depends, commands, false);
  424. // The symlink names for the target should depend on the real target
  425. // so if the target version changes it rebuilds and recreates the
  426. // symlinks.
  427. if(targetFullPathSO != targetFullPathReal)
  428. {
  429. depends.clear();
  430. commands.clear();
  431. depends.push_back(targetFullPathReal.c_str());
  432. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  433. targetFullPathSO.c_str(),
  434. depends, commands, false);
  435. }
  436. if(targetFullPath != targetFullPathSO)
  437. {
  438. depends.clear();
  439. commands.clear();
  440. depends.push_back(targetFullPathSO.c_str());
  441. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  442. targetFullPath.c_str(),
  443. depends, commands, false);
  444. }
  445. // Write convenience targets.
  446. std::string dir = this->Makefile->GetStartOutputDirectory();
  447. dir += "/";
  448. dir += this->LocalGenerator->GetTargetDirectory(*this->Target);
  449. std::string buildTargetRuleName = dir;
  450. buildTargetRuleName += relink?"/preinstall":"/build";
  451. buildTargetRuleName =
  452. this->Convert(buildTargetRuleName.c_str(),
  453. cmLocalGenerator::HOME_OUTPUT,cmLocalGenerator::MAKEFILE);
  454. this->LocalGenerator->WriteConvenienceRule(*this->BuildFileStream,
  455. targetFullPath.c_str(),
  456. buildTargetRuleName.c_str());
  457. // Clean all the possible library names and symlinks and object files.
  458. this->CleanFiles.insert(this->CleanFiles.end(),
  459. libCleanFiles.begin(),libCleanFiles.end());
  460. this->CleanFiles.insert(this->CleanFiles.end(),
  461. this->Objects.begin(),
  462. this->Objects.end());
  463. }