cmNinjaTargetGenerator.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2011 Peter Collingbourne <[email protected]>
  4. Copyright 2011 Nicolas Despres <[email protected]>
  5. Distributed under the OSI-approved BSD License (the "License");
  6. see accompanying file Copyright.txt for details.
  7. This software is distributed WITHOUT ANY WARRANTY; without even the
  8. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. See the License for more information.
  10. ============================================================================*/
  11. #include "cmNinjaTargetGenerator.h"
  12. #include "cmAlgorithms.h"
  13. #include "cmComputeLinkInformation.h"
  14. #include "cmCustomCommandGenerator.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmGeneratorTarget.h"
  17. #include "cmGlobalNinjaGenerator.h"
  18. #include "cmLocalNinjaGenerator.h"
  19. #include "cmMakefile.h"
  20. #include "cmNinjaNormalTargetGenerator.h"
  21. #include "cmNinjaUtilityTargetGenerator.h"
  22. #include "cmSourceFile.h"
  23. #include "cmSystemTools.h"
  24. #include <algorithm>
  25. cmNinjaTargetGenerator* cmNinjaTargetGenerator::New(cmGeneratorTarget* target)
  26. {
  27. switch (target->GetType()) {
  28. case cmState::EXECUTABLE:
  29. case cmState::SHARED_LIBRARY:
  30. case cmState::STATIC_LIBRARY:
  31. case cmState::MODULE_LIBRARY:
  32. case cmState::OBJECT_LIBRARY:
  33. return new cmNinjaNormalTargetGenerator(target);
  34. case cmState::UTILITY:
  35. return new cmNinjaUtilityTargetGenerator(target);
  36. ;
  37. case cmState::GLOBAL_TARGET: {
  38. // We only want to process global targets that live in the home
  39. // (i.e. top-level) directory. CMake creates copies of these targets
  40. // in every directory, which we don't need.
  41. if (strcmp(target->GetLocalGenerator()->GetCurrentSourceDirectory(),
  42. target->GetLocalGenerator()->GetSourceDirectory()) == 0) {
  43. return new cmNinjaUtilityTargetGenerator(target);
  44. }
  45. // else fallthrough
  46. }
  47. default:
  48. return 0;
  49. }
  50. }
  51. cmNinjaTargetGenerator::cmNinjaTargetGenerator(cmGeneratorTarget* target)
  52. : cmCommonTargetGenerator(cmOutputConverter::HOME_OUTPUT, target)
  53. , MacOSXContentGenerator(0)
  54. , OSXBundleGenerator(0)
  55. , MacContentFolders()
  56. , LocalGenerator(
  57. static_cast<cmLocalNinjaGenerator*>(target->GetLocalGenerator()))
  58. , Objects()
  59. {
  60. MacOSXContentGenerator = new MacOSXContentGeneratorType(this);
  61. }
  62. cmNinjaTargetGenerator::~cmNinjaTargetGenerator()
  63. {
  64. delete this->MacOSXContentGenerator;
  65. }
  66. cmGeneratedFileStream& cmNinjaTargetGenerator::GetBuildFileStream() const
  67. {
  68. return *this->GetGlobalGenerator()->GetBuildFileStream();
  69. }
  70. cmGeneratedFileStream& cmNinjaTargetGenerator::GetRulesFileStream() const
  71. {
  72. return *this->GetGlobalGenerator()->GetRulesFileStream();
  73. }
  74. cmGlobalNinjaGenerator* cmNinjaTargetGenerator::GetGlobalGenerator() const
  75. {
  76. return this->LocalGenerator->GetGlobalNinjaGenerator();
  77. }
  78. std::string cmNinjaTargetGenerator::LanguageCompilerRule(
  79. const std::string& lang) const
  80. {
  81. return lang + "_COMPILER__" +
  82. cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName());
  83. }
  84. std::string cmNinjaTargetGenerator::OrderDependsTargetForTarget()
  85. {
  86. return "cmake_order_depends_target_" + this->GetTargetName();
  87. }
  88. // TODO: Most of the code is picked up from
  89. // void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink),
  90. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  91. // Refactor it.
  92. std::string cmNinjaTargetGenerator::ComputeFlagsForObject(
  93. cmSourceFile const* source, const std::string& language)
  94. {
  95. std::string flags = this->GetFlags(language);
  96. // Add Fortran format flags.
  97. if (language == "Fortran") {
  98. this->AppendFortranFormatFlags(flags, *source);
  99. }
  100. // Add source file specific flags.
  101. this->LocalGenerator->AppendFlags(flags,
  102. source->GetProperty("COMPILE_FLAGS"));
  103. return flags;
  104. }
  105. void cmNinjaTargetGenerator::AddIncludeFlags(std::string& languageFlags,
  106. std::string const& language)
  107. {
  108. std::vector<std::string> includes;
  109. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  110. language, this->GetConfigName());
  111. // Add include directory flags.
  112. std::string includeFlags = this->LocalGenerator->GetIncludeFlags(
  113. includes, this->GeneratorTarget, language,
  114. language == "RC", // full include paths for RC needed by cmcldeps
  115. false, this->GetConfigName());
  116. if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
  117. std::replace(includeFlags.begin(), includeFlags.end(), '\\', '/');
  118. }
  119. this->LocalGenerator->AppendFlags(languageFlags, includeFlags);
  120. }
  121. bool cmNinjaTargetGenerator::NeedDepTypeMSVC(const std::string& lang) const
  122. {
  123. return strcmp(this->GetMakefile()->GetSafeDefinition("CMAKE_NINJA_DEPTYPE_" +
  124. lang),
  125. "msvc") == 0;
  126. }
  127. // TODO: Refactor with
  128. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
  129. std::string cmNinjaTargetGenerator::ComputeDefines(cmSourceFile const* source,
  130. const std::string& language)
  131. {
  132. std::set<std::string> defines;
  133. this->LocalGenerator->AppendDefines(
  134. defines, source->GetProperty("COMPILE_DEFINITIONS"));
  135. {
  136. std::string defPropName = "COMPILE_DEFINITIONS_";
  137. defPropName += cmSystemTools::UpperCase(this->GetConfigName());
  138. this->LocalGenerator->AppendDefines(defines,
  139. source->GetProperty(defPropName));
  140. }
  141. std::string definesString = this->GetDefines(language);
  142. this->LocalGenerator->JoinDefines(defines, definesString, language);
  143. return definesString;
  144. }
  145. cmNinjaDeps cmNinjaTargetGenerator::ComputeLinkDeps() const
  146. {
  147. // Static libraries never depend on other targets for linking.
  148. if (this->GeneratorTarget->GetType() == cmState::STATIC_LIBRARY ||
  149. this->GeneratorTarget->GetType() == cmState::OBJECT_LIBRARY) {
  150. return cmNinjaDeps();
  151. }
  152. cmComputeLinkInformation* cli =
  153. this->GeneratorTarget->GetLinkInformation(this->GetConfigName());
  154. if (!cli) {
  155. return cmNinjaDeps();
  156. }
  157. const std::vector<std::string>& deps = cli->GetDepends();
  158. cmNinjaDeps result(deps.size());
  159. std::transform(deps.begin(), deps.end(), result.begin(), MapToNinjaPath());
  160. // Add a dependency on the link definitions file, if any.
  161. if (this->ModuleDefinitionFile) {
  162. result.push_back(
  163. this->ConvertToNinjaPath(this->ModuleDefinitionFile->GetFullPath()));
  164. }
  165. // Add a dependency on user-specified manifest files, if any.
  166. std::vector<cmSourceFile const*> manifest_srcs;
  167. this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName);
  168. for (std::vector<cmSourceFile const*>::iterator mi = manifest_srcs.begin();
  169. mi != manifest_srcs.end(); ++mi) {
  170. result.push_back(this->ConvertToNinjaPath((*mi)->GetFullPath()));
  171. }
  172. // Add user-specified dependencies.
  173. if (const char* linkDepends =
  174. this->GeneratorTarget->GetProperty("LINK_DEPENDS")) {
  175. std::vector<std::string> linkDeps;
  176. cmSystemTools::ExpandListArgument(linkDepends, linkDeps);
  177. std::transform(linkDeps.begin(), linkDeps.end(),
  178. std::back_inserter(result), MapToNinjaPath());
  179. }
  180. return result;
  181. }
  182. std::string cmNinjaTargetGenerator::GetSourceFilePath(
  183. cmSourceFile const* source) const
  184. {
  185. return ConvertToNinjaPath(source->GetFullPath());
  186. }
  187. std::string cmNinjaTargetGenerator::GetObjectFilePath(
  188. cmSourceFile const* source) const
  189. {
  190. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  191. if (!path.empty()) {
  192. path += "/";
  193. }
  194. std::string const& objectName = this->GeneratorTarget->GetObjectName(source);
  195. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  196. path += "/";
  197. path += objectName;
  198. return path;
  199. }
  200. std::string cmNinjaTargetGenerator::GetTargetOutputDir() const
  201. {
  202. std::string dir = this->GeneratorTarget->GetDirectory(this->GetConfigName());
  203. return ConvertToNinjaPath(dir);
  204. }
  205. std::string cmNinjaTargetGenerator::GetTargetFilePath(
  206. const std::string& name) const
  207. {
  208. std::string path = this->GetTargetOutputDir();
  209. if (path.empty() || path == ".") {
  210. return name;
  211. }
  212. path += "/";
  213. path += name;
  214. return path;
  215. }
  216. std::string cmNinjaTargetGenerator::GetTargetName() const
  217. {
  218. return this->GeneratorTarget->GetName();
  219. }
  220. bool cmNinjaTargetGenerator::SetMsvcTargetPdbVariable(cmNinjaVars& vars) const
  221. {
  222. cmMakefile* mf = this->GetMakefile();
  223. if (mf->GetDefinition("MSVC_C_ARCHITECTURE_ID") ||
  224. mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID")) {
  225. std::string pdbPath;
  226. std::string compilePdbPath;
  227. if (this->GeneratorTarget->GetType() == cmState::EXECUTABLE ||
  228. this->GeneratorTarget->GetType() == cmState::STATIC_LIBRARY ||
  229. this->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY ||
  230. this->GeneratorTarget->GetType() == cmState::MODULE_LIBRARY) {
  231. pdbPath = this->GeneratorTarget->GetPDBDirectory(this->GetConfigName());
  232. pdbPath += "/";
  233. pdbPath += this->GeneratorTarget->GetPDBName(this->GetConfigName());
  234. }
  235. if (this->GeneratorTarget->GetType() <= cmState::OBJECT_LIBRARY) {
  236. compilePdbPath =
  237. this->GeneratorTarget->GetCompilePDBPath(this->GetConfigName());
  238. if (compilePdbPath.empty()) {
  239. compilePdbPath = this->GeneratorTarget->GetSupportDirectory() + "/";
  240. }
  241. }
  242. vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  243. ConvertToNinjaPath(pdbPath), cmOutputConverter::SHELL);
  244. vars["TARGET_COMPILE_PDB"] =
  245. this->GetLocalGenerator()->ConvertToOutputFormat(
  246. ConvertToNinjaPath(compilePdbPath), cmOutputConverter::SHELL);
  247. EnsureParentDirectoryExists(pdbPath);
  248. EnsureParentDirectoryExists(compilePdbPath);
  249. return true;
  250. }
  251. return false;
  252. }
  253. void cmNinjaTargetGenerator::WriteLanguageRules(const std::string& language)
  254. {
  255. #ifdef NINJA_GEN_VERBOSE_FILES
  256. this->GetRulesFileStream() << "# Rules for language " << language << "\n\n";
  257. #endif
  258. this->WriteCompileRule(language);
  259. }
  260. void cmNinjaTargetGenerator::WriteCompileRule(const std::string& lang)
  261. {
  262. cmLocalGenerator::RuleVariables vars;
  263. vars.RuleLauncher = "RULE_LAUNCH_COMPILE";
  264. vars.CMTarget = this->GetGeneratorTarget();
  265. vars.Language = lang.c_str();
  266. vars.Source = "$in";
  267. vars.Object = "$out";
  268. vars.Defines = "$DEFINES";
  269. vars.Includes = "$INCLUDES";
  270. vars.TargetPDB = "$TARGET_PDB";
  271. vars.TargetCompilePDB = "$TARGET_COMPILE_PDB";
  272. vars.ObjectDir = "$OBJECT_DIR";
  273. vars.ObjectFileDir = "$OBJECT_FILE_DIR";
  274. cmMakefile* mf = this->GetMakefile();
  275. std::string flags = "$FLAGS";
  276. std::string rspfile;
  277. std::string rspcontent;
  278. std::string responseFlag;
  279. if (this->ForceResponseFile()) {
  280. rspfile = "$RSP_FILE";
  281. responseFlag = "@" + rspfile;
  282. rspcontent = " $DEFINES $INCLUDES $FLAGS";
  283. flags = responseFlag;
  284. vars.Defines = "";
  285. vars.Includes = "";
  286. }
  287. // Tell ninja dependency format so all deps can be loaded into a database
  288. std::string deptype;
  289. std::string depfile;
  290. std::string cldeps;
  291. if (this->NeedDepTypeMSVC(lang)) {
  292. deptype = "msvc";
  293. depfile = "";
  294. flags += " /showIncludes";
  295. } else if (mf->IsOn("CMAKE_NINJA_CMCLDEPS_" + lang)) {
  296. // For the MS resource compiler we need cmcldeps, but skip dependencies
  297. // for source-file try_compile cases because they are always fresh.
  298. if (!mf->GetIsSourceFileTryCompile()) {
  299. deptype = "gcc";
  300. depfile = "$DEP_FILE";
  301. const std::string cl = mf->GetDefinition("CMAKE_C_COMPILER")
  302. ? mf->GetSafeDefinition("CMAKE_C_COMPILER")
  303. : mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
  304. cldeps = "\"";
  305. cldeps += cmSystemTools::GetCMClDepsCommand();
  306. cldeps += "\" " + lang + " $in \"$DEP_FILE\" $out \"";
  307. cldeps += mf->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX");
  308. cldeps += "\" \"" + cl + "\" ";
  309. }
  310. } else {
  311. deptype = "gcc";
  312. const char* langdeptype = mf->GetDefinition("CMAKE_NINJA_DEPTYPE_" + lang);
  313. if (langdeptype) {
  314. deptype = langdeptype;
  315. }
  316. depfile = "$DEP_FILE";
  317. const std::string flagsName = "CMAKE_DEPFILE_FLAGS_" + lang;
  318. std::string depfileFlags = mf->GetSafeDefinition(flagsName);
  319. if (!depfileFlags.empty()) {
  320. cmSystemTools::ReplaceString(depfileFlags, "<DEPFILE>", "$DEP_FILE");
  321. cmSystemTools::ReplaceString(depfileFlags, "<OBJECT>", "$out");
  322. cmSystemTools::ReplaceString(depfileFlags, "<CMAKE_C_COMPILER>",
  323. mf->GetDefinition("CMAKE_C_COMPILER"));
  324. flags += " " + depfileFlags;
  325. }
  326. }
  327. vars.Flags = flags.c_str();
  328. vars.DependencyFile = depfile.c_str();
  329. // Rule for compiling object file.
  330. const std::string cmdVar = std::string("CMAKE_") + lang + "_COMPILE_OBJECT";
  331. std::string compileCmd = mf->GetRequiredDefinition(cmdVar);
  332. std::vector<std::string> compileCmds;
  333. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  334. // Maybe insert an include-what-you-use runner.
  335. if (!compileCmds.empty() && (lang == "C" || lang == "CXX")) {
  336. std::string const iwyu_prop = lang + "_INCLUDE_WHAT_YOU_USE";
  337. const char* iwyu = this->GeneratorTarget->GetProperty(iwyu_prop);
  338. std::string const tidy_prop = lang + "_CLANG_TIDY";
  339. const char* tidy = this->GeneratorTarget->GetProperty(tidy_prop);
  340. if ((iwyu && *iwyu) || (tidy && *tidy)) {
  341. std::string run_iwyu = this->GetLocalGenerator()->ConvertToOutputFormat(
  342. cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL);
  343. run_iwyu += " -E __run_iwyu";
  344. if (iwyu && *iwyu) {
  345. run_iwyu += " --iwyu=";
  346. run_iwyu += this->GetLocalGenerator()->EscapeForShell(iwyu);
  347. }
  348. if (tidy && *tidy) {
  349. run_iwyu += " --tidy=";
  350. run_iwyu += this->GetLocalGenerator()->EscapeForShell(tidy);
  351. run_iwyu += " --source=$in";
  352. }
  353. run_iwyu += " -- ";
  354. compileCmds.front().insert(0, run_iwyu);
  355. }
  356. }
  357. // Maybe insert a compiler launcher like ccache or distcc
  358. if (!compileCmds.empty() && (lang == "C" || lang == "CXX")) {
  359. std::string const clauncher_prop = lang + "_COMPILER_LAUNCHER";
  360. const char* clauncher = this->GeneratorTarget->GetProperty(clauncher_prop);
  361. if (clauncher && *clauncher) {
  362. std::vector<std::string> launcher_cmd;
  363. cmSystemTools::ExpandListArgument(clauncher, launcher_cmd, true);
  364. for (std::vector<std::string>::iterator i = launcher_cmd.begin(),
  365. e = launcher_cmd.end();
  366. i != e; ++i) {
  367. *i = this->LocalGenerator->EscapeForShell(*i);
  368. }
  369. std::string const& run_launcher = cmJoin(launcher_cmd, " ") + " ";
  370. compileCmds.front().insert(0, run_launcher);
  371. }
  372. }
  373. if (!compileCmds.empty()) {
  374. compileCmds.front().insert(0, cldeps);
  375. }
  376. for (std::vector<std::string>::iterator i = compileCmds.begin();
  377. i != compileCmds.end(); ++i) {
  378. this->GetLocalGenerator()->ExpandRuleVariables(*i, vars);
  379. }
  380. std::string cmdLine =
  381. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  382. // Write the rule for compiling file of the given language.
  383. std::ostringstream comment;
  384. comment << "Rule for compiling " << lang << " files.";
  385. std::ostringstream description;
  386. description << "Building " << lang << " object $out";
  387. this->GetGlobalGenerator()->AddRule(
  388. this->LanguageCompilerRule(lang), cmdLine, description.str(),
  389. comment.str(), depfile, deptype, rspfile, rspcontent,
  390. /*restat*/ "",
  391. /*generator*/ false);
  392. }
  393. void cmNinjaTargetGenerator::WriteObjectBuildStatements()
  394. {
  395. // Write comments.
  396. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  397. this->GetBuildFileStream()
  398. << "# Object build statements for "
  399. << cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType())
  400. << " target " << this->GetTargetName() << "\n\n";
  401. std::string config = this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  402. std::vector<cmSourceFile const*> customCommands;
  403. this->GeneratorTarget->GetCustomCommands(customCommands, config);
  404. for (std::vector<cmSourceFile const*>::const_iterator si =
  405. customCommands.begin();
  406. si != customCommands.end(); ++si) {
  407. cmCustomCommand const* cc = (*si)->GetCustomCommand();
  408. this->GetLocalGenerator()->AddCustomCommandTarget(
  409. cc, this->GetGeneratorTarget());
  410. // Record the custom commands for this target. The container is used
  411. // in WriteObjectBuildStatement when called in a loop below.
  412. this->CustomCommands.push_back(cc);
  413. }
  414. std::vector<cmSourceFile const*> headerSources;
  415. this->GeneratorTarget->GetHeaderSources(headerSources, config);
  416. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  417. headerSources, this->MacOSXContentGenerator);
  418. std::vector<cmSourceFile const*> extraSources;
  419. this->GeneratorTarget->GetExtraSources(extraSources, config);
  420. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  421. extraSources, this->MacOSXContentGenerator);
  422. std::vector<cmSourceFile const*> externalObjects;
  423. this->GeneratorTarget->GetExternalObjects(externalObjects, config);
  424. for (std::vector<cmSourceFile const*>::const_iterator si =
  425. externalObjects.begin();
  426. si != externalObjects.end(); ++si) {
  427. this->Objects.push_back(this->GetSourceFilePath(*si));
  428. }
  429. cmNinjaDeps orderOnlyDeps;
  430. this->GetLocalGenerator()->AppendTargetDepends(this->GeneratorTarget,
  431. orderOnlyDeps);
  432. // Add order-only dependencies on custom command outputs.
  433. for (std::vector<cmCustomCommand const*>::const_iterator cci =
  434. this->CustomCommands.begin();
  435. cci != this->CustomCommands.end(); ++cci) {
  436. cmCustomCommand const* cc = *cci;
  437. cmCustomCommandGenerator ccg(*cc, this->GetConfigName(),
  438. this->GetLocalGenerator());
  439. const std::vector<std::string>& ccoutputs = ccg.GetOutputs();
  440. const std::vector<std::string>& ccbyproducts = ccg.GetByproducts();
  441. std::transform(ccoutputs.begin(), ccoutputs.end(),
  442. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  443. std::transform(ccbyproducts.begin(), ccbyproducts.end(),
  444. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  445. }
  446. if (!orderOnlyDeps.empty()) {
  447. cmNinjaDeps orderOnlyTarget;
  448. orderOnlyTarget.push_back(this->OrderDependsTargetForTarget());
  449. this->GetGlobalGenerator()->WritePhonyBuild(
  450. this->GetBuildFileStream(),
  451. "Order-only phony target for " + this->GetTargetName(), orderOnlyTarget,
  452. cmNinjaDeps(), cmNinjaDeps(), orderOnlyDeps);
  453. }
  454. std::vector<cmSourceFile const*> objectSources;
  455. this->GeneratorTarget->GetObjectSources(objectSources, config);
  456. for (std::vector<cmSourceFile const*>::const_iterator si =
  457. objectSources.begin();
  458. si != objectSources.end(); ++si) {
  459. this->WriteObjectBuildStatement(*si, !orderOnlyDeps.empty());
  460. }
  461. this->GetBuildFileStream() << "\n";
  462. }
  463. void cmNinjaTargetGenerator::WriteObjectBuildStatement(
  464. cmSourceFile const* source, bool writeOrderDependsTargetForTarget)
  465. {
  466. std::string const language = source->GetLanguage();
  467. std::string const sourceFileName =
  468. language == "RC" ? source->GetFullPath() : this->GetSourceFilePath(source);
  469. std::string const objectDir =
  470. this->ConvertToNinjaPath(this->GeneratorTarget->GetSupportDirectory());
  471. std::string const objectFileName =
  472. this->ConvertToNinjaPath(this->GetObjectFilePath(source));
  473. std::string const objectFileDir =
  474. cmSystemTools::GetFilenamePath(objectFileName);
  475. cmNinjaVars vars;
  476. vars["FLAGS"] = this->ComputeFlagsForObject(source, language);
  477. vars["DEFINES"] = this->ComputeDefines(source, language);
  478. vars["INCLUDES"] = this->GetIncludes(language);
  479. if (!this->NeedDepTypeMSVC(language)) {
  480. vars["DEP_FILE"] =
  481. cmGlobalNinjaGenerator::EncodeDepfileSpace(objectFileName + ".d");
  482. }
  483. this->ExportObjectCompileCommand(
  484. language, sourceFileName, objectDir, objectFileName, objectFileDir,
  485. vars["FLAGS"], vars["DEFINES"], vars["INCLUDES"]);
  486. std::string comment;
  487. std::string rule = this->LanguageCompilerRule(language);
  488. cmNinjaDeps outputs;
  489. outputs.push_back(objectFileName);
  490. // Add this object to the list of object files.
  491. this->Objects.push_back(objectFileName);
  492. cmNinjaDeps explicitDeps;
  493. explicitDeps.push_back(sourceFileName);
  494. cmNinjaDeps implicitDeps;
  495. if (const char* objectDeps = source->GetProperty("OBJECT_DEPENDS")) {
  496. std::vector<std::string> depList;
  497. cmSystemTools::ExpandListArgument(objectDeps, depList);
  498. for (std::vector<std::string>::iterator odi = depList.begin();
  499. odi != depList.end(); ++odi) {
  500. if (cmSystemTools::FileIsFullPath(*odi)) {
  501. *odi = cmSystemTools::CollapseFullPath(*odi);
  502. }
  503. }
  504. std::transform(depList.begin(), depList.end(),
  505. std::back_inserter(implicitDeps), MapToNinjaPath());
  506. }
  507. cmNinjaDeps orderOnlyDeps;
  508. if (writeOrderDependsTargetForTarget) {
  509. orderOnlyDeps.push_back(this->OrderDependsTargetForTarget());
  510. }
  511. // If the source file is GENERATED and does not have a custom command
  512. // (either attached to this source file or another one), assume that one of
  513. // the target dependencies, OBJECT_DEPENDS or header file custom commands
  514. // will rebuild the file.
  515. if (source->GetPropertyAsBool("GENERATED") && !source->GetCustomCommand() &&
  516. !this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFileName)) {
  517. this->GetGlobalGenerator()->AddAssumedSourceDependencies(sourceFileName,
  518. orderOnlyDeps);
  519. }
  520. EnsureParentDirectoryExists(objectFileName);
  521. vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  522. objectDir, cmOutputConverter::SHELL);
  523. vars["OBJECT_FILE_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  524. objectFileDir, cmOutputConverter::SHELL);
  525. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
  526. vars);
  527. this->SetMsvcTargetPdbVariable(vars);
  528. int const commandLineLengthLimit = this->ForceResponseFile() ? -1 : 0;
  529. std::string const rspfile = objectFileName + ".rsp";
  530. this->GetGlobalGenerator()->WriteBuild(
  531. this->GetBuildFileStream(), comment, rule, outputs, explicitDeps,
  532. implicitDeps, orderOnlyDeps, vars, rspfile, commandLineLengthLimit);
  533. if (const char* objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
  534. std::vector<std::string> outputList;
  535. cmSystemTools::ExpandListArgument(objectOutputs, outputList);
  536. std::transform(outputList.begin(), outputList.end(), outputList.begin(),
  537. MapToNinjaPath());
  538. this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
  539. "Additional output files.",
  540. outputList, outputs);
  541. }
  542. }
  543. void cmNinjaTargetGenerator::ExportObjectCompileCommand(
  544. std::string const& language, std::string const& sourceFileName,
  545. std::string const& objectDir, std::string const& objectFileName,
  546. std::string const& objectFileDir, std::string const& flags,
  547. std::string const& defines, std::string const& includes)
  548. {
  549. if (!this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS")) {
  550. return;
  551. }
  552. cmLocalGenerator::RuleVariables compileObjectVars;
  553. compileObjectVars.Language = language.c_str();
  554. std::string escapedSourceFileName = sourceFileName;
  555. if (!cmSystemTools::FileIsFullPath(sourceFileName.c_str())) {
  556. escapedSourceFileName = cmSystemTools::CollapseFullPath(
  557. escapedSourceFileName, this->GetGlobalGenerator()
  558. ->GetCMakeInstance()
  559. ->GetHomeOutputDirectory());
  560. }
  561. escapedSourceFileName = this->LocalGenerator->ConvertToOutputFormat(
  562. escapedSourceFileName, cmOutputConverter::SHELL);
  563. compileObjectVars.Source = escapedSourceFileName.c_str();
  564. compileObjectVars.Object = objectFileName.c_str();
  565. compileObjectVars.ObjectDir = objectDir.c_str();
  566. compileObjectVars.ObjectFileDir = objectFileDir.c_str();
  567. compileObjectVars.Flags = flags.c_str();
  568. compileObjectVars.Defines = defines.c_str();
  569. compileObjectVars.Includes = includes.c_str();
  570. // Rule for compiling object file.
  571. std::string compileCmdVar = "CMAKE_";
  572. compileCmdVar += language;
  573. compileCmdVar += "_COMPILE_OBJECT";
  574. std::string compileCmd =
  575. this->GetMakefile()->GetRequiredDefinition(compileCmdVar);
  576. std::vector<std::string> compileCmds;
  577. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  578. for (std::vector<std::string>::iterator i = compileCmds.begin();
  579. i != compileCmds.end(); ++i) {
  580. this->GetLocalGenerator()->ExpandRuleVariables(*i, compileObjectVars);
  581. }
  582. std::string cmdLine =
  583. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  584. this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine, sourceFileName);
  585. }
  586. void cmNinjaTargetGenerator::EnsureDirectoryExists(
  587. const std::string& path) const
  588. {
  589. if (cmSystemTools::FileIsFullPath(path.c_str())) {
  590. cmSystemTools::MakeDirectory(path.c_str());
  591. } else {
  592. cmGlobalNinjaGenerator* gg = this->GetGlobalGenerator();
  593. std::string fullPath =
  594. std::string(gg->GetCMakeInstance()->GetHomeOutputDirectory());
  595. // Also ensures their is a trailing slash.
  596. gg->StripNinjaOutputPathPrefixAsSuffix(fullPath);
  597. fullPath += path;
  598. cmSystemTools::MakeDirectory(fullPath.c_str());
  599. }
  600. }
  601. void cmNinjaTargetGenerator::EnsureParentDirectoryExists(
  602. const std::string& path) const
  603. {
  604. EnsureDirectoryExists(cmSystemTools::GetParentDirectory(path));
  605. }
  606. void cmNinjaTargetGenerator::MacOSXContentGeneratorType::operator()(
  607. cmSourceFile const& source, const char* pkgloc)
  608. {
  609. // Skip OS X content when not building a Framework or Bundle.
  610. if (!this->Generator->GetGeneratorTarget()->IsBundleOnApple()) {
  611. return;
  612. }
  613. std::string macdir =
  614. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc);
  615. // Get the input file location.
  616. std::string input = source.GetFullPath();
  617. input = this->Generator->GetGlobalGenerator()->ConvertToNinjaPath(input);
  618. // Get the output file location.
  619. std::string output = macdir;
  620. output += "/";
  621. output += cmSystemTools::GetFilenameName(input);
  622. output = this->Generator->GetGlobalGenerator()->ConvertToNinjaPath(output);
  623. // Write a build statement to copy the content into the bundle.
  624. this->Generator->GetGlobalGenerator()->WriteMacOSXContentBuild(input,
  625. output);
  626. // Add as a dependency of all target so that it gets called.
  627. this->Generator->GetGlobalGenerator()->AddDependencyToAll(output);
  628. }
  629. void cmNinjaTargetGenerator::addPoolNinjaVariable(
  630. const std::string& pool_property, cmGeneratorTarget* target,
  631. cmNinjaVars& vars)
  632. {
  633. const char* pool = target->GetProperty(pool_property);
  634. if (pool) {
  635. vars["pool"] = pool;
  636. }
  637. }
  638. bool cmNinjaTargetGenerator::ForceResponseFile()
  639. {
  640. static std::string const forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
  641. return (this->GetMakefile()->IsDefinitionSet(forceRspFile) ||
  642. cmSystemTools::GetEnv(forceRspFile) != 0);
  643. }