cmMakefileExecutableTargetGenerator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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::vector<cmSourceFile*>::const_iterator sourceIt;
  110. for ( sourceIt = this->Target->GetSourceFiles().begin();
  111. sourceIt != this->Target->GetSourceFiles().end();
  112. ++ sourceIt )
  113. {
  114. const char* subDir =
  115. (*sourceIt)->GetProperty("MACOSX_PACKAGE_LOCATION");
  116. if ( subDir )
  117. {
  118. std::string newDir = macdir;
  119. newDir += subDir;
  120. if ( !cmSystemTools::MakeDirectory(newDir.c_str()) )
  121. {
  122. cmSystemTools::Error("Cannot create a subdirectory for \"",
  123. newDir.c_str(), "\".");
  124. return;
  125. }
  126. }
  127. }
  128. // Configure the Info.plist file. Note that it needs the executable name
  129. // to be set.
  130. std::string plist = macdir + "Info.plist";
  131. this->LocalGenerator->GenerateAppleInfoPList(this->Target,
  132. targetName.c_str(),
  133. plist.c_str());
  134. }
  135. #endif
  136. std::string outpathImp;
  137. if(relink)
  138. {
  139. outpath = this->Makefile->GetStartOutputDirectory();
  140. outpath += cmake::GetCMakeFilesDirectory();
  141. outpath += "/CMakeRelink.dir";
  142. cmSystemTools::MakeDirectory(outpath.c_str());
  143. outpath += "/";
  144. if(!targetNameImport.empty())
  145. {
  146. outpathImp = outpath;
  147. }
  148. }
  149. else
  150. {
  151. if(!targetNameImport.empty())
  152. {
  153. outpathImp = this->Target->GetDirectory(0, true);
  154. outpathImp += "/";
  155. }
  156. }
  157. std::string targetFullPath = outpath + targetName;
  158. std::string targetFullPathReal = outpath + targetNameReal;
  159. std::string targetFullPathPDB = outpath + targetNamePDB;
  160. std::string targetFullPathImport = outpathImp + targetNameImport;
  161. std::string targetOutPathPDB =
  162. this->Convert(targetFullPathPDB.c_str(),
  163. cmLocalGenerator::FULL,
  164. cmLocalGenerator::SHELL);
  165. // Convert to the output path to use in constructing commands.
  166. std::string targetOutPath =
  167. this->Convert(targetFullPath.c_str(),
  168. cmLocalGenerator::START_OUTPUT,
  169. cmLocalGenerator::SHELL);
  170. std::string targetOutPathReal =
  171. this->Convert(targetFullPathReal.c_str(),
  172. cmLocalGenerator::START_OUTPUT,
  173. cmLocalGenerator::SHELL);
  174. std::string targetOutPathImport =
  175. this->Convert(targetFullPathImport.c_str(),
  176. cmLocalGenerator::START_OUTPUT,
  177. cmLocalGenerator::SHELL);
  178. // Get the language to use for linking this executable.
  179. const char* linkLanguage =
  180. this->Target->GetLinkerLanguage(this->GlobalGenerator);
  181. // Make sure we have a link language.
  182. if(!linkLanguage)
  183. {
  184. cmSystemTools::Error("Cannot determine link language for target \"",
  185. this->Target->GetName(), "\".");
  186. return;
  187. }
  188. // Add the link message.
  189. std::string buildEcho = "Linking ";
  190. buildEcho += linkLanguage;
  191. buildEcho += " executable ";
  192. buildEcho += targetOutPath;
  193. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  194. cmLocalUnixMakefileGenerator3::EchoLink);
  195. // Build a list of compiler flags and linker flags.
  196. std::string flags;
  197. std::string linkFlags;
  198. // Add flags to deal with shared libraries. Any library being
  199. // linked in might be shared, so always use shared flags for an
  200. // executable.
  201. this->LocalGenerator->AddSharedFlags(linkFlags, linkLanguage, true);
  202. // Add flags to create an executable.
  203. this->LocalGenerator->
  204. AddConfigVariableFlags(linkFlags, "CMAKE_EXE_LINKER_FLAGS",
  205. this->LocalGenerator->ConfigurationName.c_str());
  206. if(this->Target->GetPropertyAsBool("WIN32_EXECUTABLE"))
  207. {
  208. this->LocalGenerator->AppendFlags
  209. (linkFlags, this->Makefile->GetDefinition("CMAKE_CREATE_WIN32_EXE"));
  210. }
  211. else
  212. {
  213. this->LocalGenerator->AppendFlags
  214. (linkFlags, this->Makefile->GetDefinition("CMAKE_CREATE_CONSOLE_EXE"));
  215. }
  216. // Add symbol export flags if necessary.
  217. if(this->Target->IsExecutableWithExports())
  218. {
  219. std::string export_flag_var = "CMAKE_EXE_EXPORTS_";
  220. export_flag_var += linkLanguage;
  221. export_flag_var += "_FLAG";
  222. this->LocalGenerator->AppendFlags
  223. (linkFlags, this->Makefile->GetDefinition(export_flag_var.c_str()));
  224. }
  225. // Add language-specific flags.
  226. this->LocalGenerator
  227. ->AddLanguageFlags(flags, linkLanguage,
  228. this->LocalGenerator->ConfigurationName.c_str());
  229. // Add target-specific linker flags.
  230. this->LocalGenerator->AppendFlags
  231. (linkFlags, this->Target->GetProperty("LINK_FLAGS"));
  232. std::string linkFlagsConfig = "LINK_FLAGS_";
  233. linkFlagsConfig +=
  234. cmSystemTools::UpperCase(this->LocalGenerator->ConfigurationName.c_str());
  235. this->LocalGenerator->AppendFlags
  236. (linkFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  237. // Construct a list of files associated with this executable that
  238. // may need to be cleaned.
  239. std::vector<std::string> exeCleanFiles;
  240. {
  241. std::string cleanName;
  242. std::string cleanRealName;
  243. std::string cleanImportName;
  244. std::string cleanPDBName;
  245. this->Target->GetExecutableCleanNames
  246. (cleanName, cleanRealName, cleanImportName, cleanPDBName,
  247. this->LocalGenerator->ConfigurationName.c_str());
  248. std::string cleanFullName = outpath + cleanName;
  249. std::string cleanFullRealName = outpath + cleanRealName;
  250. std::string cleanFullPDBName = outpath + cleanPDBName;
  251. std::string cleanFullImportName = outpathImp + cleanImportName;
  252. exeCleanFiles.push_back(this->Convert(cleanFullName.c_str(),
  253. cmLocalGenerator::START_OUTPUT,
  254. cmLocalGenerator::UNCHANGED));
  255. #ifdef _WIN32
  256. // There may be a manifest file for this target. Add it to the
  257. // clean set just in case.
  258. exeCleanFiles.push_back(this->Convert((cleanFullName+".manifest").c_str(),
  259. cmLocalGenerator::START_OUTPUT,
  260. cmLocalGenerator::UNCHANGED));
  261. #endif
  262. if(cleanRealName != cleanName)
  263. {
  264. exeCleanFiles.push_back(this->Convert(cleanFullRealName.c_str(),
  265. cmLocalGenerator::START_OUTPUT,
  266. cmLocalGenerator::UNCHANGED));
  267. }
  268. if(!cleanImportName.empty())
  269. {
  270. exeCleanFiles.push_back(this->Convert(cleanFullImportName.c_str(),
  271. cmLocalGenerator::START_OUTPUT,
  272. cmLocalGenerator::UNCHANGED));
  273. }
  274. // List the PDB for cleaning only when the whole target is
  275. // cleaned. We do not want to delete the .pdb file just before
  276. // linking the target.
  277. this->CleanFiles.push_back
  278. (this->Convert(cleanFullPDBName.c_str(),
  279. cmLocalGenerator::START_OUTPUT,
  280. cmLocalGenerator::UNCHANGED));
  281. }
  282. // Add the pre-build and pre-link rules building but not when relinking.
  283. if(!relink)
  284. {
  285. this->LocalGenerator
  286. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands());
  287. this->LocalGenerator
  288. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands());
  289. }
  290. // Determine whether a link script will be used.
  291. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  292. // Construct the main link rule.
  293. std::vector<std::string> real_link_commands;
  294. std::string linkRuleVar = "CMAKE_";
  295. linkRuleVar += linkLanguage;
  296. linkRuleVar += "_LINK_EXECUTABLE";
  297. std::string linkRule =
  298. this->Makefile->GetRequiredDefinition(linkRuleVar.c_str());
  299. std::vector<std::string> commands1;
  300. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  301. if(this->Target->IsExecutableWithExports())
  302. {
  303. // If a separate rule for creating an import library is specified
  304. // add it now.
  305. std::string implibRuleVar = "CMAKE_";
  306. implibRuleVar += linkLanguage;
  307. implibRuleVar += "_CREATE_IMPORT_LIBRARY";
  308. if(const char* rule =
  309. this->Makefile->GetDefinition(implibRuleVar.c_str()))
  310. {
  311. cmSystemTools::ExpandListArgument(rule, real_link_commands);
  312. }
  313. }
  314. // Expand the rule variables.
  315. {
  316. // Set path conversion for link script shells.
  317. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  318. // Collect up flags to link in needed libraries.
  319. cmOStringStream linklibs;
  320. this->LocalGenerator->OutputLinkLibraries(linklibs, *this->Target, relink);
  321. // Construct object file lists that may be needed to expand the
  322. // rule.
  323. std::string variableName;
  324. std::string variableNameExternal;
  325. this->WriteObjectsVariable(variableName, variableNameExternal);
  326. std::string buildObjs;
  327. if(useLinkScript)
  328. {
  329. this->WriteObjectsString(buildObjs);
  330. }
  331. else
  332. {
  333. buildObjs = "$(";
  334. buildObjs += variableName;
  335. buildObjs += ") $(";
  336. buildObjs += variableNameExternal;
  337. buildObjs += ")";
  338. }
  339. std::string cleanObjs = "$(";
  340. cleanObjs += variableName;
  341. cleanObjs += ")";
  342. cmLocalGenerator::RuleVariables vars;
  343. vars.Language = linkLanguage;
  344. vars.Objects = buildObjs.c_str();
  345. vars.Target = targetOutPathReal.c_str();
  346. vars.TargetPDB = targetOutPathPDB.c_str();
  347. // Setup the target version.
  348. std::string targetVersionMajor;
  349. std::string targetVersionMinor;
  350. {
  351. cmOStringStream majorStream;
  352. cmOStringStream minorStream;
  353. int major;
  354. int minor;
  355. this->Target->GetTargetVersion(major, minor);
  356. majorStream << major;
  357. minorStream << minor;
  358. targetVersionMajor = majorStream.str();
  359. targetVersionMinor = minorStream.str();
  360. }
  361. vars.TargetVersionMajor = targetVersionMajor.c_str();
  362. vars.TargetVersionMinor = targetVersionMinor.c_str();
  363. std::string linkString = linklibs.str();
  364. vars.LinkLibraries = linkString.c_str();
  365. vars.Flags = flags.c_str();
  366. vars.LinkFlags = linkFlags.c_str();
  367. // Expand placeholders in the commands.
  368. this->LocalGenerator->TargetImplib = targetOutPathImport;
  369. for(std::vector<std::string>::iterator i = real_link_commands.begin();
  370. i != real_link_commands.end(); ++i)
  371. {
  372. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  373. }
  374. this->LocalGenerator->TargetImplib = "";
  375. // Restore path conversion to normal shells.
  376. this->LocalGenerator->SetLinkScriptShell(false);
  377. }
  378. // Optionally convert the build rule to use a script to avoid long
  379. // command lines in the make shell.
  380. if(useLinkScript)
  381. {
  382. // Use a link script.
  383. const char* name = (relink? "relink.txt" : "link.txt");
  384. this->CreateLinkScript(name, real_link_commands, commands1);
  385. }
  386. else
  387. {
  388. // No link script. Just use the link rule directly.
  389. commands1 = real_link_commands;
  390. }
  391. this->LocalGenerator->CreateCDCommand
  392. (commands1,
  393. this->Makefile->GetStartOutputDirectory(),
  394. this->Makefile->GetHomeOutputDirectory());
  395. commands.insert(commands.end(), commands1.begin(), commands1.end());
  396. commands1.clear();
  397. // Add a rule to create necessary symlinks for the library.
  398. if(targetOutPath != targetOutPathReal)
  399. {
  400. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_executable ";
  401. symlink += targetOutPathReal;
  402. symlink += " ";
  403. symlink += targetOutPath;
  404. commands1.push_back(symlink);
  405. this->LocalGenerator->CreateCDCommand(commands1,
  406. this->Makefile->GetStartOutputDirectory(),
  407. this->Makefile->GetHomeOutputDirectory());
  408. commands.insert(commands.end(), commands1.begin(), commands1.end());
  409. commands1.clear();
  410. }
  411. // Add the post-build rules when building but not when relinking.
  412. if(!relink)
  413. {
  414. this->LocalGenerator->
  415. AppendCustomCommands(commands, this->Target->GetPostBuildCommands());
  416. }
  417. // Write the build rule.
  418. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream,
  419. 0,
  420. targetFullPathReal.c_str(),
  421. depends, commands, false);
  422. // The symlink name for the target should depend on the real target
  423. // so if the target version changes it rebuilds and recreates the
  424. // symlink.
  425. if(targetFullPath != targetFullPathReal)
  426. {
  427. depends.clear();
  428. commands.clear();
  429. depends.push_back(targetFullPathReal.c_str());
  430. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  431. targetFullPath.c_str(),
  432. depends, commands, false);
  433. }
  434. // Write the main driver rule to build everything in this target.
  435. this->WriteTargetDriverRule(targetFullPath.c_str(), relink);
  436. // Clean all the possible executable names and symlinks.
  437. this->CleanFiles.insert(this->CleanFiles.end(),
  438. exeCleanFiles.begin(),
  439. exeCleanFiles.end());
  440. }