cmMakefileExecutableTargetGenerator.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 "cmMakefileExecutableTargetGenerator.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGlobalUnixMakefileGenerator3.h"
  16. #include "cmLocalUnixMakefileGenerator3.h"
  17. #include "cmMakefile.h"
  18. #include "cmSourceFile.h"
  19. #include "cmTarget.h"
  20. #include "cmake.h"
  21. //----------------------------------------------------------------------------
  22. cmMakefileExecutableTargetGenerator::cmMakefileExecutableTargetGenerator()
  23. {
  24. this->DriveCustomCommandsOnDepends = true;
  25. }
  26. //----------------------------------------------------------------------------
  27. void cmMakefileExecutableTargetGenerator::WriteRuleFiles()
  28. {
  29. // create the build.make file and directory, put in the common blocks
  30. this->CreateRuleFile();
  31. // write rules used to help build object files
  32. this->WriteCommonCodeRules();
  33. // write in rules for object files and custom commands
  34. this->WriteTargetBuildRules();
  35. // write the per-target per-language flags
  36. this->WriteTargetLanguageFlags();
  37. // Write the dependency generation rule.
  38. this->WriteTargetDependRules();
  39. // write the link rules
  40. this->WriteExecutableRule(false);
  41. if(this->Target->NeedRelinkBeforeInstall())
  42. {
  43. // Write rules to link an installable version of the target.
  44. this->WriteExecutableRule(true);
  45. }
  46. // Write the requires target.
  47. this->WriteTargetRequiresRules();
  48. // Write clean target
  49. this->WriteTargetCleanRules();
  50. // close the streams
  51. this->CloseFileStreams();
  52. }
  53. //----------------------------------------------------------------------------
  54. void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink)
  55. {
  56. std::vector<std::string> commands;
  57. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  58. std::string objTarget;
  59. // Build list of dependencies.
  60. std::vector<std::string> depends;
  61. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  62. obj != this->Objects.end(); ++obj)
  63. {
  64. objTarget = relPath;
  65. // Handle extra content on Mac bundles
  66. if ( this->ExtraContent.find(*obj) != this->ExtraContent.end() )
  67. {
  68. objTarget = "";
  69. }
  70. objTarget += *obj;
  71. depends.push_back(objTarget);
  72. }
  73. // Add dependencies on targets that must be built first.
  74. this->AppendTargetDepends(depends);
  75. // Add a dependency on the rule file itself.
  76. this->LocalGenerator->AppendRuleDepend(depends,
  77. this->BuildFileNameFull.c_str());
  78. for(std::vector<std::string>::const_iterator obj =
  79. this->ExternalObjects.begin();
  80. obj != this->ExternalObjects.end(); ++obj)
  81. {
  82. depends.push_back(*obj);
  83. }
  84. // from here up is the same for exe or lib
  85. // Get the name of the executable to generate.
  86. std::string targetName;
  87. std::string targetNameReal;
  88. std::string targetNameImport;
  89. std::string targetNamePDB;
  90. this->Target->GetExecutableNames
  91. (targetName, targetNameReal, targetNameImport, targetNamePDB,
  92. this->LocalGenerator->ConfigurationName.c_str());
  93. // Construct the full path version of the names.
  94. std::string outpath = this->Target->GetDirectory();
  95. outpath += "/";
  96. #ifdef __APPLE__
  97. if(this->Target->GetPropertyAsBool("MACOSX_BUNDLE"))
  98. {
  99. // Compute bundle directory names.
  100. std::string macdir = outpath;
  101. macdir += targetName;
  102. macdir += ".app/Contents/";
  103. outpath = macdir;
  104. outpath += "MacOS";
  105. cmSystemTools::MakeDirectory(outpath.c_str());
  106. outpath += "/";
  107. // Make bundle directories
  108. std::string f1 =
  109. this->Makefile->GetModulesFile("MacOSXBundleInfo.plist.in");
  110. if ( f1.size() == 0 )
  111. {
  112. cmSystemTools::Error("could not find Mac OSX bundle template file.");
  113. }
  114. std::vector<cmSourceFile*>::iterator sourceIt;
  115. for ( sourceIt = this->Target->GetSourceFiles().begin();
  116. sourceIt != this->Target->GetSourceFiles().end();
  117. ++ sourceIt )
  118. {
  119. const char* subDir =
  120. (*sourceIt)->GetProperty("MACOSX_PACKAGE_LOCATION");
  121. if ( subDir )
  122. {
  123. std::string newDir = macdir;
  124. newDir += subDir;
  125. if ( !cmSystemTools::MakeDirectory(newDir.c_str()) )
  126. {
  127. cmSystemTools::Error("Cannot create a subdirectory for \"",
  128. newDir.c_str(), "\".");
  129. return;
  130. }
  131. }
  132. }
  133. // Configure the Info.plist file. Note that it needs the executable name
  134. // to be set.
  135. std::string f2 = macdir + "Info.plist";
  136. this->Makefile->AddDefinition("MACOSX_BUNDLE_EXECUTABLE_NAME",
  137. targetName.c_str());
  138. this->Makefile->ConfigureFile(f1.c_str(), f2.c_str(),
  139. false, false, false);
  140. }
  141. #endif
  142. std::string outpathImp;
  143. if(relink)
  144. {
  145. outpath = this->Makefile->GetStartOutputDirectory();
  146. outpath += cmake::GetCMakeFilesDirectory();
  147. outpath += "/CMakeRelink.dir";
  148. cmSystemTools::MakeDirectory(outpath.c_str());
  149. outpath += "/";
  150. if(!targetNameImport.empty())
  151. {
  152. outpathImp = outpath;
  153. }
  154. }
  155. else
  156. {
  157. if(!targetNameImport.empty())
  158. {
  159. outpathImp = this->Target->GetDirectory(0, true);
  160. outpathImp += "/";
  161. }
  162. }
  163. std::string targetFullPath = outpath + targetName;
  164. std::string targetFullPathReal = outpath + targetNameReal;
  165. std::string targetFullPathPDB = outpath + targetNamePDB;
  166. std::string targetFullPathImport = outpathImp + targetNameImport;
  167. std::string targetOutPathPDB =
  168. this->Convert(targetFullPathPDB.c_str(),
  169. cmLocalGenerator::FULL,
  170. cmLocalGenerator::SHELL);
  171. // Convert to the output path to use in constructing commands.
  172. std::string targetOutPath =
  173. this->Convert(targetFullPath.c_str(),
  174. cmLocalGenerator::START_OUTPUT,
  175. cmLocalGenerator::SHELL);
  176. std::string targetOutPathReal =
  177. this->Convert(targetFullPathReal.c_str(),
  178. cmLocalGenerator::START_OUTPUT,
  179. cmLocalGenerator::SHELL);
  180. std::string targetOutPathImport =
  181. this->Convert(targetFullPathImport.c_str(),
  182. cmLocalGenerator::START_OUTPUT,
  183. cmLocalGenerator::SHELL);
  184. // Get the language to use for linking this executable.
  185. const char* linkLanguage =
  186. this->Target->GetLinkerLanguage(this->GlobalGenerator);
  187. // Make sure we have a link language.
  188. if(!linkLanguage)
  189. {
  190. cmSystemTools::Error("Cannot determine link language for target \"",
  191. this->Target->GetName(), "\".");
  192. return;
  193. }
  194. // Add the link message.
  195. std::string buildEcho = "Linking ";
  196. buildEcho += linkLanguage;
  197. buildEcho += " executable ";
  198. buildEcho += targetOutPath;
  199. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  200. cmLocalUnixMakefileGenerator3::EchoLink);
  201. // Build a list of compiler flags and linker flags.
  202. std::string flags;
  203. std::string linkFlags;
  204. // Add flags to deal with shared libraries. Any library being
  205. // linked in might be shared, so always use shared flags for an
  206. // executable.
  207. this->LocalGenerator->AddSharedFlags(linkFlags, linkLanguage, true);
  208. // Add flags to create an executable.
  209. this->LocalGenerator->
  210. AddConfigVariableFlags(linkFlags, "CMAKE_EXE_LINKER_FLAGS",
  211. this->LocalGenerator->ConfigurationName.c_str());
  212. if(this->Target->GetPropertyAsBool("WIN32_EXECUTABLE"))
  213. {
  214. this->LocalGenerator->AppendFlags
  215. (linkFlags, this->Makefile->GetDefinition("CMAKE_CREATE_WIN32_EXE"));
  216. }
  217. else
  218. {
  219. this->LocalGenerator->AppendFlags
  220. (linkFlags, this->Makefile->GetDefinition("CMAKE_CREATE_CONSOLE_EXE"));
  221. }
  222. // Add language-specific flags.
  223. this->LocalGenerator
  224. ->AddLanguageFlags(flags, linkLanguage,
  225. this->LocalGenerator->ConfigurationName.c_str());
  226. // Add target-specific linker flags.
  227. this->LocalGenerator->AppendFlags
  228. (linkFlags, this->Target->GetProperty("LINK_FLAGS"));
  229. std::string linkFlagsConfig = "LINK_FLAGS_";
  230. linkFlagsConfig +=
  231. cmSystemTools::UpperCase(this->LocalGenerator->ConfigurationName.c_str());
  232. this->LocalGenerator->AppendFlags
  233. (linkFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  234. // Construct a list of files associated with this executable that
  235. // may need to be cleaned.
  236. std::vector<std::string> exeCleanFiles;
  237. {
  238. std::string cleanName;
  239. std::string cleanRealName;
  240. std::string cleanImportName;
  241. std::string cleanPDBName;
  242. this->Target->GetExecutableCleanNames
  243. (cleanName, cleanRealName, cleanImportName, cleanPDBName,
  244. this->LocalGenerator->ConfigurationName.c_str());
  245. std::string cleanFullName = outpath + cleanName;
  246. std::string cleanFullRealName = outpath + cleanRealName;
  247. std::string cleanFullPDBName = outpath + cleanPDBName;
  248. std::string cleanFullImportName = outpathImp + cleanImportName;
  249. exeCleanFiles.push_back(this->Convert(cleanFullName.c_str(),
  250. cmLocalGenerator::START_OUTPUT,
  251. cmLocalGenerator::UNCHANGED));
  252. #ifdef _WIN32
  253. // There may be a manifest file for this target. Add it to the
  254. // clean set just in case.
  255. exeCleanFiles.push_back(this->Convert((cleanFullName+".manifest").c_str(),
  256. cmLocalGenerator::START_OUTPUT,
  257. cmLocalGenerator::UNCHANGED));
  258. #endif
  259. if(cleanRealName != cleanName)
  260. {
  261. exeCleanFiles.push_back(this->Convert(cleanFullRealName.c_str(),
  262. cmLocalGenerator::START_OUTPUT,
  263. cmLocalGenerator::UNCHANGED));
  264. }
  265. if(!cleanImportName.empty())
  266. {
  267. exeCleanFiles.push_back(this->Convert(cleanFullImportName.c_str(),
  268. cmLocalGenerator::START_OUTPUT,
  269. cmLocalGenerator::UNCHANGED));
  270. }
  271. // List the PDB for cleaning only when the whole target is
  272. // cleaned. We do not want to delete the .pdb file just before
  273. // linking the target.
  274. this->CleanFiles.push_back
  275. (this->Convert(cleanFullPDBName.c_str(),
  276. cmLocalGenerator::START_OUTPUT,
  277. cmLocalGenerator::UNCHANGED));
  278. }
  279. // Add a command to remove any existing files for this executable.
  280. std::vector<std::string> commands1;
  281. this->LocalGenerator->AppendCleanCommand(commands1, exeCleanFiles,
  282. *this->Target, "target");
  283. this->LocalGenerator->CreateCDCommand
  284. (commands1,
  285. this->Makefile->GetStartOutputDirectory(),
  286. this->Makefile->GetHomeOutputDirectory());
  287. commands.insert(commands.end(), commands1.begin(), commands1.end());
  288. commands1.clear();
  289. // Add the pre-build and pre-link rules building but not when relinking.
  290. if(!relink)
  291. {
  292. this->LocalGenerator
  293. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands());
  294. this->LocalGenerator
  295. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands());
  296. }
  297. // Construct the main link rule.
  298. std::string linkRuleVar = "CMAKE_";
  299. linkRuleVar += linkLanguage;
  300. linkRuleVar += "_LINK_EXECUTABLE";
  301. std::string linkRule =
  302. this->Makefile->GetRequiredDefinition(linkRuleVar.c_str());
  303. cmSystemTools::ExpandListArgument(linkRule, commands1);
  304. if(this->Target->GetPropertyAsBool("ENABLE_EXPORTS"))
  305. {
  306. // If a separate rule for creating an import library is specified
  307. // add it now.
  308. std::string implibRuleVar = "CMAKE_";
  309. implibRuleVar += linkLanguage;
  310. implibRuleVar += "_CREATE_IMPORT_LIBRARY";
  311. if(const char* rule =
  312. this->Makefile->GetDefinition(implibRuleVar.c_str()))
  313. {
  314. cmSystemTools::ExpandListArgument(rule, commands1);
  315. }
  316. }
  317. this->LocalGenerator->CreateCDCommand
  318. (commands1,
  319. this->Makefile->GetStartOutputDirectory(),
  320. this->Makefile->GetHomeOutputDirectory());
  321. commands.insert(commands.end(), commands1.begin(), commands1.end());
  322. // Add a rule to create necessary symlinks for the library.
  323. if(targetOutPath != targetOutPathReal)
  324. {
  325. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_executable ";
  326. symlink += targetOutPathReal;
  327. symlink += " ";
  328. symlink += targetOutPath;
  329. commands1.clear();
  330. commands1.push_back(symlink);
  331. this->LocalGenerator->CreateCDCommand(commands1,
  332. this->Makefile->GetStartOutputDirectory(),
  333. this->Makefile->GetHomeOutputDirectory());
  334. commands.insert(commands.end(), commands1.begin(), commands1.end());
  335. }
  336. // Add the post-build rules when building but not when relinking.
  337. if(!relink)
  338. {
  339. this->LocalGenerator->
  340. AppendCustomCommands(commands, this->Target->GetPostBuildCommands());
  341. }
  342. // Collect up flags to link in needed libraries.
  343. cmOStringStream linklibs;
  344. this->LocalGenerator->OutputLinkLibraries(linklibs, *this->Target, relink);
  345. // Construct object file lists that may be needed to expand the
  346. // rule.
  347. std::string variableName;
  348. std::string variableNameExternal;
  349. this->WriteObjectsVariable(variableName, variableNameExternal);
  350. std::string buildObjs = "$(";
  351. buildObjs += variableName;
  352. buildObjs += ") $(";
  353. buildObjs += variableNameExternal;
  354. buildObjs += ")";
  355. std::string cleanObjs = "$(";
  356. cleanObjs += variableName;
  357. cleanObjs += ")";
  358. cmLocalGenerator::RuleVariables vars;
  359. vars.Language = linkLanguage;
  360. vars.Objects = buildObjs.c_str();
  361. vars.Target = targetOutPathReal.c_str();
  362. vars.TargetPDB = targetOutPathPDB.c_str();
  363. // Setup the target version.
  364. std::string targetVersionMajor;
  365. std::string targetVersionMinor;
  366. {
  367. cmOStringStream majorStream;
  368. cmOStringStream minorStream;
  369. int major;
  370. int minor;
  371. this->Target->GetTargetVersion(major, minor);
  372. majorStream << major;
  373. minorStream << minor;
  374. targetVersionMajor = majorStream.str();
  375. targetVersionMinor = minorStream.str();
  376. }
  377. vars.TargetVersionMajor = targetVersionMajor.c_str();
  378. vars.TargetVersionMinor = targetVersionMinor.c_str();
  379. std::string linkString = linklibs.str();
  380. vars.LinkLibraries = linkString.c_str();
  381. vars.Flags = flags.c_str();
  382. vars.LinkFlags = linkFlags.c_str();
  383. // Expand placeholders in the commands.
  384. this->LocalGenerator->TargetImplib = targetOutPathImport;
  385. for(std::vector<std::string>::iterator i = commands.begin();
  386. i != commands.end(); ++i)
  387. {
  388. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  389. }
  390. this->LocalGenerator->TargetImplib = "";
  391. // Write the build rule.
  392. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream,
  393. 0,
  394. targetFullPathReal.c_str(),
  395. depends, commands, false);
  396. // The symlink name for the target should depend on the real target
  397. // so if the target version changes it rebuilds and recreates the
  398. // symlink.
  399. if(targetFullPath != targetFullPathReal)
  400. {
  401. depends.clear();
  402. commands.clear();
  403. depends.push_back(targetFullPathReal.c_str());
  404. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  405. targetFullPath.c_str(),
  406. depends, commands, false);
  407. }
  408. // Write the main driver rule to build everything in this target.
  409. this->WriteTargetDriverRule(targetFullPath.c_str(), relink);
  410. // Clean all the possible executable names and symlinks and object files.
  411. this->CleanFiles.insert(this->CleanFiles.end(),
  412. exeCleanFiles.begin(),
  413. exeCleanFiles.end());
  414. this->CleanFiles.insert(this->CleanFiles.end(),
  415. this->Objects.begin(),
  416. this->Objects.end());
  417. }