cmMakefileExecutableTargetGenerator.cxx 17 KB

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