cmMakefileLibraryTargetGenerator.cxx 15 KB

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