cmMakefileExecutableTargetGenerator.cxx 17 KB

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