cmMakefileLibraryTargetGenerator.cxx 20 KB

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