cmMakefileLibraryTargetGenerator.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 link rules
  30. // Write the rule for this target type.
  31. switch(this->Target->GetType())
  32. {
  33. case cmTarget::STATIC_LIBRARY:
  34. this->WriteStaticLibraryRules();
  35. break;
  36. case cmTarget::SHARED_LIBRARY:
  37. this->WriteSharedLibraryRules();
  38. break;
  39. case cmTarget::MODULE_LIBRARY:
  40. this->WriteModuleLibraryRules();
  41. break;
  42. default:
  43. // If language is not known, this is an error.
  44. cmSystemTools::Error("Unknown Library Type");
  45. break;
  46. }
  47. // Write the requires target.
  48. this->WriteTargetRequiresRules();
  49. // Write clean target
  50. this->WriteTargetCleanRules();
  51. // close the streams
  52. this->CloseFileStreams();
  53. }
  54. //----------------------------------------------------------------------------
  55. void cmMakefileLibraryTargetGenerator::WriteStaticLibraryRules()
  56. {
  57. const char* linkLanguage =
  58. this->Target->GetLinkerLanguage(this->GlobalGenerator);
  59. std::string linkRuleVar = "CMAKE_";
  60. if (linkLanguage)
  61. {
  62. linkRuleVar += linkLanguage;
  63. }
  64. linkRuleVar += "_CREATE_STATIC_LIBRARY";
  65. std::string extraFlags;
  66. this->LocalGenerator->AppendFlags(extraFlags, this->Target->GetProperty("STATIC_LIBRARY_FLAGS"));
  67. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str());
  68. }
  69. //----------------------------------------------------------------------------
  70. void cmMakefileLibraryTargetGenerator::WriteSharedLibraryRules()
  71. {
  72. const char* linkLanguage =
  73. this->Target->GetLinkerLanguage(this->GlobalGenerator);
  74. std::string linkRuleVar = "CMAKE_";
  75. if (linkLanguage)
  76. {
  77. linkRuleVar += linkLanguage;
  78. }
  79. linkRuleVar += "_CREATE_SHARED_LIBRARY";
  80. std::string extraFlags;
  81. this->LocalGenerator->AppendFlags(extraFlags, this->Target->GetProperty("LINK_FLAGS"));
  82. this->LocalGenerator->AddConfigVariableFlags(extraFlags, "CMAKE_SHARED_LINKER_FLAGS");
  83. if(this->Makefile->IsOn("WIN32") && !(this->Makefile->IsOn("CYGWIN") || this->Makefile->IsOn("MINGW")))
  84. {
  85. const std::vector<cmSourceFile*>& sources = this->Target->GetSourceFiles();
  86. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  87. i != sources.end(); ++i)
  88. {
  89. if((*i)->GetSourceExtension() == "def")
  90. {
  91. extraFlags += " ";
  92. extraFlags += this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  93. extraFlags +=
  94. this->Convert((*i)->GetFullPath().c_str(),cmLocalGenerator::START_OUTPUT,cmLocalGenerator::MAKEFILE);
  95. }
  96. }
  97. }
  98. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str());
  99. }
  100. //----------------------------------------------------------------------------
  101. void cmMakefileLibraryTargetGenerator::WriteModuleLibraryRules()
  102. {
  103. const char* linkLanguage =
  104. this->Target->GetLinkerLanguage(this->GlobalGenerator);
  105. std::string linkRuleVar = "CMAKE_";
  106. if (linkLanguage)
  107. {
  108. linkRuleVar += linkLanguage;
  109. }
  110. linkRuleVar += "_CREATE_SHARED_MODULE";
  111. std::string extraFlags;
  112. this->LocalGenerator->AppendFlags(extraFlags, this->Target->GetProperty("LINK_FLAGS"));
  113. this->LocalGenerator->AddConfigVariableFlags(extraFlags, "CMAKE_MODULE_LINKER_FLAGS");
  114. // TODO: .def files should be supported here also.
  115. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str());
  116. }
  117. //----------------------------------------------------------------------------
  118. void cmMakefileLibraryTargetGenerator::WriteLibraryRules
  119. (const char* linkRuleVar, const char* extraFlags)
  120. {
  121. // Write the dependency generation rule.
  122. this->WriteTargetDependRules();
  123. // TODO: Merge the methods that call this method to avoid
  124. // code duplication.
  125. std::vector<std::string> commands;
  126. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  127. std::string objTarget;
  128. // Build list of dependencies.
  129. std::vector<std::string> depends;
  130. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  131. obj != this->Objects.end(); ++obj)
  132. {
  133. objTarget = relPath;
  134. objTarget += *obj;
  135. depends.push_back(objTarget);
  136. }
  137. // Add dependencies on targets that must be built first.
  138. this->AppendTargetDepends(depends);
  139. // Add a dependency on the rule file itself.
  140. this->LocalGenerator->AppendRuleDepend(depends,
  141. this->BuildFileNameFull.c_str());
  142. for(std::vector<std::string>::const_iterator obj
  143. = this->ExternalObjects.begin();
  144. obj != this->ExternalObjects.end(); ++obj)
  145. {
  146. depends.push_back(*obj);
  147. }
  148. // Get the language to use for linking this library.
  149. const char* linkLanguage =
  150. this->Target->GetLinkerLanguage(this->GlobalGenerator);
  151. // Make sure we have a link language.
  152. if(!linkLanguage)
  153. {
  154. cmSystemTools::Error("Cannot determine link language for target \"",
  155. this->Target->GetName(), "\".");
  156. return;
  157. }
  158. // Create set of linking flags.
  159. std::string linkFlags;
  160. this->LocalGenerator->AppendFlags(linkFlags, extraFlags);
  161. // Construct the name of the library.
  162. std::string targetName;
  163. std::string targetNameSO;
  164. std::string targetNameReal;
  165. this->Target->GetLibraryNames(targetName, targetNameSO, targetNameReal,
  166. this->LocalGenerator->m_ConfigurationName.c_str());
  167. // Construct the full path version of the names.
  168. std::string outpath = this->LocalGenerator->m_LibraryOutputPath;
  169. if(outpath.length() == 0)
  170. {
  171. outpath = this->Makefile->GetStartOutputDirectory();
  172. outpath += "/";
  173. }
  174. std::string targetFullPath = outpath + targetName;
  175. std::string targetFullPathSO = outpath + targetNameSO;
  176. std::string targetFullPathReal = outpath + targetNameReal;
  177. // Construct the output path version of the names for use in command
  178. // arguments.
  179. std::string targetOutPath =
  180. this->Convert(targetFullPath.c_str(),cmLocalGenerator::START_OUTPUT,
  181. cmLocalGenerator::MAKEFILE);
  182. std::string targetOutPathSO =
  183. this->Convert(targetFullPathSO.c_str(),cmLocalGenerator::START_OUTPUT,
  184. cmLocalGenerator::MAKEFILE);
  185. std::string targetOutPathReal =
  186. this->Convert(targetFullPathReal.c_str(),cmLocalGenerator::START_OUTPUT,
  187. cmLocalGenerator::MAKEFILE);
  188. // Add the link message.
  189. std::string buildEcho = "Linking ";
  190. buildEcho += linkLanguage;
  191. switch(this->Target->GetType())
  192. {
  193. case cmTarget::STATIC_LIBRARY:
  194. buildEcho += " static library "; break;
  195. case cmTarget::SHARED_LIBRARY:
  196. buildEcho += " shared library "; break;
  197. case cmTarget::MODULE_LIBRARY:
  198. buildEcho += " shared module "; break;
  199. default:
  200. buildEcho += " library "; break;
  201. }
  202. buildEcho += targetOutPath.c_str();
  203. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str());
  204. // Construct a list of files associated with this library that may
  205. // need to be cleaned.
  206. std::vector<std::string> libCleanFiles;
  207. {
  208. std::string cleanStaticName;
  209. std::string cleanSharedName;
  210. std::string cleanSharedSOName;
  211. std::string cleanSharedRealName;
  212. this->Target->GetLibraryCleanNames(cleanStaticName,
  213. cleanSharedName,
  214. cleanSharedSOName,
  215. cleanSharedRealName,
  216. this->LocalGenerator->m_ConfigurationName.c_str());
  217. std::string cleanFullStaticName = outpath + cleanStaticName;
  218. std::string cleanFullSharedName = outpath + cleanSharedName;
  219. std::string cleanFullSharedSOName = outpath + cleanSharedSOName;
  220. std::string cleanFullSharedRealName = outpath + cleanSharedRealName;
  221. libCleanFiles.push_back
  222. (this->Convert(cleanFullStaticName.c_str(),cmLocalGenerator::START_OUTPUT,
  223. cmLocalGenerator::MAKEFILE));
  224. if(cleanSharedRealName != cleanStaticName)
  225. {
  226. libCleanFiles.push_back(this->Convert(cleanFullSharedRealName.c_str(),
  227. cmLocalGenerator::START_OUTPUT,
  228. cmLocalGenerator::MAKEFILE));
  229. }
  230. if(cleanSharedSOName != cleanStaticName &&
  231. cleanSharedSOName != cleanSharedRealName)
  232. {
  233. libCleanFiles.push_back(this->Convert(cleanFullSharedSOName.c_str(),
  234. cmLocalGenerator::START_OUTPUT,
  235. cmLocalGenerator::MAKEFILE));
  236. }
  237. if(cleanSharedName != cleanStaticName &&
  238. cleanSharedName != cleanSharedSOName &&
  239. cleanSharedName != cleanSharedRealName)
  240. {
  241. libCleanFiles.push_back(this->Convert(cleanFullSharedName.c_str(),
  242. cmLocalGenerator::START_OUTPUT,
  243. cmLocalGenerator::MAKEFILE));
  244. }
  245. }
  246. // Add a command to remove any existing files for this library.
  247. std::vector<std::string> commands1;
  248. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles);
  249. this->LocalGenerator->CreateCDCommand(commands1,
  250. this->Makefile->GetStartOutputDirectory(),
  251. this->Makefile->GetHomeOutputDirectory());
  252. commands.insert(commands.end(), commands1.begin(), commands1.end());
  253. commands1.clear();
  254. // Add the pre-build and pre-link rules.
  255. this->LocalGenerator->AppendCustomCommands(commands, this->Target->GetPreBuildCommands());
  256. this->LocalGenerator->AppendCustomCommands(commands, this->Target->GetPreLinkCommands());
  257. // Construct the main link rule.
  258. std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
  259. cmSystemTools::ExpandListArgument(linkRule, commands1);
  260. this->LocalGenerator->CreateCDCommand(commands1,
  261. this->Makefile->GetStartOutputDirectory(),
  262. this->Makefile->GetHomeOutputDirectory());
  263. commands.insert(commands.end(), commands1.begin(), commands1.end());
  264. // Add a rule to create necessary symlinks for the library.
  265. if(targetOutPath != targetOutPathReal)
  266. {
  267. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  268. symlink += targetOutPathReal;
  269. symlink += " ";
  270. symlink += targetOutPathSO;
  271. symlink += " ";
  272. symlink += targetOutPath;
  273. commands1.clear();
  274. commands1.push_back(symlink);
  275. this->LocalGenerator->CreateCDCommand(commands1,
  276. this->Makefile->GetStartOutputDirectory(),
  277. this->Makefile->GetHomeOutputDirectory());
  278. commands.insert(commands.end(), commands1.begin(), commands1.end());
  279. }
  280. // Add the post-build rules.
  281. this->LocalGenerator->AppendCustomCommands(commands, this->Target->GetPostBuildCommands());
  282. // Collect up flags to link in needed libraries.
  283. cmOStringStream linklibs;
  284. this->LocalGenerator->OutputLinkLibraries(linklibs, *this->Target);
  285. // Construct object file lists that may be needed to expand the
  286. // rule.
  287. std::string variableName;
  288. std::string variableNameExternal;
  289. this->WriteObjectsVariable(variableName, variableNameExternal);
  290. std::string buildObjs = "$(";
  291. buildObjs += variableName;
  292. buildObjs += ") $(";
  293. buildObjs += variableNameExternal;
  294. buildObjs += ")";
  295. std::string cleanObjs = "$(";
  296. cleanObjs += variableName;
  297. cleanObjs += ")";
  298. // Expand placeholders in the commands.
  299. for(std::vector<std::string>::iterator i = commands.begin();
  300. i != commands.end(); ++i)
  301. {
  302. this->LocalGenerator->ExpandRuleVariables(*i,
  303. linkLanguage,
  304. buildObjs.c_str(),
  305. targetOutPathReal.c_str(),
  306. linklibs.str().c_str(),
  307. 0, 0, 0, buildObjs.c_str(),
  308. targetNameSO.c_str(),
  309. linkFlags.c_str());
  310. }
  311. // Write the build rule.
  312. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  313. targetFullPathReal.c_str(), depends, commands);
  314. // The symlink names for the target should depend on the real target
  315. // so if the target version changes it rebuilds and recreates the
  316. // symlinks.
  317. if(targetFullPathSO != targetFullPathReal)
  318. {
  319. depends.clear();
  320. commands.clear();
  321. depends.push_back(targetFullPathReal.c_str());
  322. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  323. targetFullPathSO.c_str(), depends, commands);
  324. }
  325. if(targetFullPath != targetFullPathSO)
  326. {
  327. depends.clear();
  328. commands.clear();
  329. depends.push_back(targetFullPathSO.c_str());
  330. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  331. targetFullPath.c_str(), depends, commands);
  332. }
  333. // Write convenience targets.
  334. std::string dir = this->Makefile->GetStartOutputDirectory();
  335. dir += "/";
  336. dir += this->LocalGenerator->GetTargetDirectory(*this->Target);
  337. std::string buildTargetRuleName = dir;
  338. buildTargetRuleName += "/build";
  339. buildTargetRuleName =
  340. this->Convert(buildTargetRuleName.c_str(),
  341. cmLocalGenerator::HOME_OUTPUT,cmLocalGenerator::MAKEFILE);
  342. this->LocalGenerator->WriteConvenienceRule(*this->BuildFileStream,
  343. targetFullPath.c_str(),
  344. buildTargetRuleName.c_str());
  345. // Clean all the possible library names and symlinks and object files.
  346. this->CleanFiles.insert(this->CleanFiles.end(),
  347. libCleanFiles.begin(),libCleanFiles.end());
  348. this->CleanFiles.push_back(cleanObjs);
  349. }