cmMakefileLibraryTargetGenerator.cxx 19 KB

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