cmNinjaTargetGenerator.cxx 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmNinjaTargetGenerator.h"
  4. #include "cm_jsoncpp_value.h"
  5. #include "cm_jsoncpp_writer.h"
  6. #include <algorithm>
  7. #include <assert.h>
  8. #include <iterator>
  9. #include <map>
  10. #include <sstream>
  11. #include <string.h>
  12. #include "cmAlgorithms.h"
  13. #include "cmComputeLinkInformation.h"
  14. #include "cmCustomCommandGenerator.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmGeneratorExpression.h"
  17. #include "cmGeneratorTarget.h"
  18. #include "cmGlobalNinjaGenerator.h"
  19. #include "cmLocalGenerator.h"
  20. #include "cmLocalNinjaGenerator.h"
  21. #include "cmMakefile.h"
  22. #include "cmNinjaNormalTargetGenerator.h"
  23. #include "cmNinjaUtilityTargetGenerator.h"
  24. #include "cmOutputConverter.h"
  25. #include "cmRulePlaceholderExpander.h"
  26. #include "cmSourceFile.h"
  27. #include "cmState.h"
  28. #include "cmStateTypes.h"
  29. #include "cmSystemTools.h"
  30. #include "cm_auto_ptr.hxx"
  31. #include "cmake.h"
  32. cmNinjaTargetGenerator* cmNinjaTargetGenerator::New(cmGeneratorTarget* target)
  33. {
  34. switch (target->GetType()) {
  35. case cmStateEnums::EXECUTABLE:
  36. case cmStateEnums::SHARED_LIBRARY:
  37. case cmStateEnums::STATIC_LIBRARY:
  38. case cmStateEnums::MODULE_LIBRARY:
  39. case cmStateEnums::OBJECT_LIBRARY:
  40. return new cmNinjaNormalTargetGenerator(target);
  41. case cmStateEnums::UTILITY:
  42. case cmStateEnums::GLOBAL_TARGET:
  43. return new cmNinjaUtilityTargetGenerator(target);
  44. default:
  45. return nullptr;
  46. }
  47. }
  48. cmNinjaTargetGenerator::cmNinjaTargetGenerator(cmGeneratorTarget* target)
  49. : cmCommonTargetGenerator(target)
  50. , MacOSXContentGenerator(nullptr)
  51. , OSXBundleGenerator(nullptr)
  52. , MacContentFolders()
  53. , LocalGenerator(
  54. static_cast<cmLocalNinjaGenerator*>(target->GetLocalGenerator()))
  55. , Objects()
  56. {
  57. MacOSXContentGenerator = new MacOSXContentGeneratorType(this);
  58. }
  59. cmNinjaTargetGenerator::~cmNinjaTargetGenerator()
  60. {
  61. delete this->MacOSXContentGenerator;
  62. }
  63. cmGeneratedFileStream& cmNinjaTargetGenerator::GetBuildFileStream() const
  64. {
  65. return *this->GetGlobalGenerator()->GetBuildFileStream();
  66. }
  67. cmGeneratedFileStream& cmNinjaTargetGenerator::GetRulesFileStream() const
  68. {
  69. return *this->GetGlobalGenerator()->GetRulesFileStream();
  70. }
  71. cmGlobalNinjaGenerator* cmNinjaTargetGenerator::GetGlobalGenerator() const
  72. {
  73. return this->LocalGenerator->GetGlobalNinjaGenerator();
  74. }
  75. std::string cmNinjaTargetGenerator::LanguageCompilerRule(
  76. const std::string& lang) const
  77. {
  78. return lang + "_COMPILER__" +
  79. cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName());
  80. }
  81. std::string cmNinjaTargetGenerator::LanguagePreprocessRule(
  82. std::string const& lang) const
  83. {
  84. return lang + "_PREPROCESS__" +
  85. cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName());
  86. }
  87. bool cmNinjaTargetGenerator::NeedExplicitPreprocessing(
  88. std::string const& lang) const
  89. {
  90. return lang == "Fortran";
  91. }
  92. std::string cmNinjaTargetGenerator::LanguageDyndepRule(
  93. const std::string& lang) const
  94. {
  95. return lang + "_DYNDEP__" +
  96. cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName());
  97. }
  98. bool cmNinjaTargetGenerator::NeedDyndep(std::string const& lang) const
  99. {
  100. return lang == "Fortran";
  101. }
  102. std::string cmNinjaTargetGenerator::OrderDependsTargetForTarget()
  103. {
  104. return "cmake_object_order_depends_target_" + this->GetTargetName();
  105. }
  106. // TODO: Most of the code is picked up from
  107. // void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink),
  108. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  109. // Refactor it.
  110. std::string cmNinjaTargetGenerator::ComputeFlagsForObject(
  111. cmSourceFile const* source, const std::string& language)
  112. {
  113. std::string flags = this->GetFlags(language);
  114. // Add Fortran format flags.
  115. if (language == "Fortran") {
  116. this->AppendFortranFormatFlags(flags, *source);
  117. }
  118. // Add source file specific flags.
  119. if (const char* cflags = source->GetProperty("COMPILE_FLAGS")) {
  120. std::string config = this->LocalGenerator->GetConfigName();
  121. cmGeneratorExpression ge;
  122. CM_AUTO_PTR<cmCompiledGeneratorExpression> cge = ge.Parse(cflags);
  123. const char* evaluatedFlags = cge->Evaluate(this->LocalGenerator, config,
  124. false, this->GeneratorTarget);
  125. this->LocalGenerator->AppendFlags(flags, evaluatedFlags);
  126. }
  127. return flags;
  128. }
  129. void cmNinjaTargetGenerator::AddIncludeFlags(std::string& languageFlags,
  130. std::string const& language)
  131. {
  132. std::vector<std::string> includes;
  133. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  134. language, this->GetConfigName());
  135. // Add include directory flags.
  136. std::string includeFlags = this->LocalGenerator->GetIncludeFlags(
  137. includes, this->GeneratorTarget, language,
  138. language == "RC", // full include paths for RC needed by cmcldeps
  139. false, this->GetConfigName());
  140. if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
  141. std::replace(includeFlags.begin(), includeFlags.end(), '\\', '/');
  142. }
  143. this->LocalGenerator->AppendFlags(languageFlags, includeFlags);
  144. }
  145. bool cmNinjaTargetGenerator::NeedDepTypeMSVC(const std::string& lang) const
  146. {
  147. return strcmp(this->GetMakefile()->GetSafeDefinition("CMAKE_NINJA_DEPTYPE_" +
  148. lang),
  149. "msvc") == 0;
  150. }
  151. // TODO: Refactor with
  152. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
  153. std::string cmNinjaTargetGenerator::ComputeDefines(cmSourceFile const* source,
  154. const std::string& language)
  155. {
  156. std::set<std::string> defines;
  157. this->LocalGenerator->AppendDefines(
  158. defines, source->GetProperty("COMPILE_DEFINITIONS"));
  159. {
  160. std::string defPropName = "COMPILE_DEFINITIONS_";
  161. defPropName += cmSystemTools::UpperCase(this->GetConfigName());
  162. this->LocalGenerator->AppendDefines(defines,
  163. source->GetProperty(defPropName));
  164. }
  165. std::string definesString = this->GetDefines(language);
  166. this->LocalGenerator->JoinDefines(defines, definesString, language);
  167. return definesString;
  168. }
  169. cmNinjaDeps cmNinjaTargetGenerator::ComputeLinkDeps() const
  170. {
  171. // Static libraries never depend on other targets for linking.
  172. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  173. this->GeneratorTarget->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  174. return cmNinjaDeps();
  175. }
  176. cmComputeLinkInformation* cli =
  177. this->GeneratorTarget->GetLinkInformation(this->GetConfigName());
  178. if (!cli) {
  179. return cmNinjaDeps();
  180. }
  181. const std::vector<std::string>& deps = cli->GetDepends();
  182. cmNinjaDeps result(deps.size());
  183. std::transform(deps.begin(), deps.end(), result.begin(), MapToNinjaPath());
  184. // Add a dependency on the link definitions file, if any.
  185. if (cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  186. this->GeneratorTarget->GetModuleDefinitionInfo(
  187. this->GetConfigName())) {
  188. for (cmSourceFile const* src : mdi->Sources) {
  189. result.push_back(this->ConvertToNinjaPath(src->GetFullPath()));
  190. }
  191. }
  192. // Add a dependency on user-specified manifest files, if any.
  193. std::vector<cmSourceFile const*> manifest_srcs;
  194. this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName);
  195. for (cmSourceFile const* manifest_src : manifest_srcs) {
  196. result.push_back(this->ConvertToNinjaPath(manifest_src->GetFullPath()));
  197. }
  198. // Add user-specified dependencies.
  199. if (const char* linkDepends =
  200. this->GeneratorTarget->GetProperty("LINK_DEPENDS")) {
  201. std::vector<std::string> linkDeps;
  202. cmSystemTools::ExpandListArgument(linkDepends, linkDeps);
  203. std::transform(linkDeps.begin(), linkDeps.end(),
  204. std::back_inserter(result), MapToNinjaPath());
  205. }
  206. return result;
  207. }
  208. std::string cmNinjaTargetGenerator::GetSourceFilePath(
  209. cmSourceFile const* source) const
  210. {
  211. return ConvertToNinjaPath(source->GetFullPath());
  212. }
  213. std::string cmNinjaTargetGenerator::GetObjectFilePath(
  214. cmSourceFile const* source) const
  215. {
  216. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  217. if (!path.empty()) {
  218. path += "/";
  219. }
  220. std::string const& objectName = this->GeneratorTarget->GetObjectName(source);
  221. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  222. path += "/";
  223. path += objectName;
  224. return path;
  225. }
  226. std::string cmNinjaTargetGenerator::GetPreprocessedFilePath(
  227. cmSourceFile const* source) const
  228. {
  229. // Choose an extension to compile already-preprocessed source.
  230. std::string ppExt = source->GetExtension();
  231. if (cmHasLiteralPrefix(ppExt, "F")) {
  232. // Some Fortran compilers automatically enable preprocessing for
  233. // upper-case extensions. Since the source is already preprocessed,
  234. // use a lower-case extension.
  235. ppExt = cmSystemTools::LowerCase(ppExt);
  236. }
  237. if (ppExt == "fpp") {
  238. // Some Fortran compilers automatically enable preprocessing for
  239. // the ".fpp" extension. Since the source is already preprocessed,
  240. // use the ".f" extension.
  241. ppExt = "f";
  242. }
  243. // Take the object file name and replace the extension.
  244. std::string const& objName = this->GeneratorTarget->GetObjectName(source);
  245. std::string const& objExt =
  246. this->GetGlobalGenerator()->GetLanguageOutputExtension(*source);
  247. assert(objName.size() >= objExt.size());
  248. std::string const ppName =
  249. objName.substr(0, objName.size() - objExt.size()) + "-pp." + ppExt;
  250. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  251. if (!path.empty()) {
  252. path += "/";
  253. }
  254. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  255. path += "/";
  256. path += ppName;
  257. return path;
  258. }
  259. std::string cmNinjaTargetGenerator::GetDyndepFilePath(
  260. std::string const& lang) const
  261. {
  262. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  263. if (!path.empty()) {
  264. path += "/";
  265. }
  266. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  267. path += "/";
  268. path += lang;
  269. path += ".dd";
  270. return path;
  271. }
  272. std::string cmNinjaTargetGenerator::GetTargetDependInfoPath(
  273. std::string const& lang) const
  274. {
  275. std::string path = this->Makefile->GetCurrentBinaryDirectory();
  276. path += "/";
  277. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  278. path += "/" + lang + "DependInfo.json";
  279. return path;
  280. }
  281. std::string cmNinjaTargetGenerator::GetTargetOutputDir() const
  282. {
  283. std::string dir = this->GeneratorTarget->GetDirectory(this->GetConfigName());
  284. return ConvertToNinjaPath(dir);
  285. }
  286. std::string cmNinjaTargetGenerator::GetTargetFilePath(
  287. const std::string& name) const
  288. {
  289. std::string path = this->GetTargetOutputDir();
  290. if (path.empty() || path == ".") {
  291. return name;
  292. }
  293. path += "/";
  294. path += name;
  295. return path;
  296. }
  297. std::string cmNinjaTargetGenerator::GetTargetName() const
  298. {
  299. return this->GeneratorTarget->GetName();
  300. }
  301. bool cmNinjaTargetGenerator::SetMsvcTargetPdbVariable(cmNinjaVars& vars) const
  302. {
  303. cmMakefile* mf = this->GetMakefile();
  304. if (mf->GetDefinition("MSVC_C_ARCHITECTURE_ID") ||
  305. mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID") ||
  306. mf->GetDefinition("MSVC_CUDA_ARCHITECTURE_ID")) {
  307. std::string pdbPath;
  308. std::string compilePdbPath = this->ComputeTargetCompilePDB();
  309. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE ||
  310. this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  311. this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  312. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  313. pdbPath = this->GeneratorTarget->GetPDBDirectory(this->GetConfigName());
  314. pdbPath += "/";
  315. pdbPath += this->GeneratorTarget->GetPDBName(this->GetConfigName());
  316. }
  317. vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  318. ConvertToNinjaPath(pdbPath), cmOutputConverter::SHELL);
  319. vars["TARGET_COMPILE_PDB"] =
  320. this->GetLocalGenerator()->ConvertToOutputFormat(
  321. ConvertToNinjaPath(compilePdbPath), cmOutputConverter::SHELL);
  322. EnsureParentDirectoryExists(pdbPath);
  323. EnsureParentDirectoryExists(compilePdbPath);
  324. return true;
  325. }
  326. return false;
  327. }
  328. void cmNinjaTargetGenerator::WriteLanguageRules(const std::string& language)
  329. {
  330. #ifdef NINJA_GEN_VERBOSE_FILES
  331. this->GetRulesFileStream() << "# Rules for language " << language << "\n\n";
  332. #endif
  333. this->WriteCompileRule(language);
  334. }
  335. void cmNinjaTargetGenerator::WriteCompileRule(const std::string& lang)
  336. {
  337. cmRulePlaceholderExpander::RuleVariables vars;
  338. vars.CMTargetName = this->GetGeneratorTarget()->GetName().c_str();
  339. vars.CMTargetType =
  340. cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType());
  341. vars.Language = lang.c_str();
  342. vars.Source = "$in";
  343. vars.Object = "$out";
  344. vars.Defines = "$DEFINES";
  345. vars.Includes = "$INCLUDES";
  346. vars.TargetPDB = "$TARGET_PDB";
  347. vars.TargetCompilePDB = "$TARGET_COMPILE_PDB";
  348. vars.ObjectDir = "$OBJECT_DIR";
  349. vars.ObjectFileDir = "$OBJECT_FILE_DIR";
  350. // For some cases we do an explicit preprocessor invocation.
  351. bool const explicitPP = this->NeedExplicitPreprocessing(lang);
  352. bool const needDyndep = this->NeedDyndep(lang);
  353. cmMakefile* mf = this->GetMakefile();
  354. std::string flags = "$FLAGS";
  355. std::string rspfile;
  356. std::string rspcontent;
  357. std::string responseFlag;
  358. bool const lang_supports_response = !(lang == "RC" || lang == "CUDA");
  359. if (lang_supports_response && this->ForceResponseFile()) {
  360. rspfile = "$RSP_FILE";
  361. responseFlag = "@" + rspfile;
  362. rspcontent = " $DEFINES $INCLUDES $FLAGS";
  363. flags = responseFlag;
  364. vars.Defines = "";
  365. vars.Includes = "";
  366. }
  367. // Tell ninja dependency format so all deps can be loaded into a database
  368. std::string deptype;
  369. std::string depfile;
  370. std::string cldeps;
  371. if (explicitPP) {
  372. // The explicit preprocessing step will handle dependency scanning.
  373. } else if (this->NeedDepTypeMSVC(lang)) {
  374. deptype = "msvc";
  375. depfile.clear();
  376. flags += " /showIncludes";
  377. } else if (mf->IsOn("CMAKE_NINJA_CMCLDEPS_" + lang)) {
  378. // For the MS resource compiler we need cmcldeps, but skip dependencies
  379. // for source-file try_compile cases because they are always fresh.
  380. if (!mf->GetIsSourceFileTryCompile()) {
  381. deptype = "gcc";
  382. depfile = "$DEP_FILE";
  383. const std::string cl = mf->GetDefinition("CMAKE_C_COMPILER")
  384. ? mf->GetSafeDefinition("CMAKE_C_COMPILER")
  385. : mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
  386. cldeps = "\"";
  387. cldeps += cmSystemTools::GetCMClDepsCommand();
  388. cldeps += "\" " + lang + " " + vars.Source + " \"$DEP_FILE\" $out \"";
  389. cldeps += mf->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX");
  390. cldeps += "\" \"" + cl + "\" ";
  391. }
  392. } else {
  393. deptype = "gcc";
  394. const char* langdeptype = mf->GetDefinition("CMAKE_NINJA_DEPTYPE_" + lang);
  395. if (langdeptype) {
  396. deptype = langdeptype;
  397. }
  398. depfile = "$DEP_FILE";
  399. const std::string flagsName = "CMAKE_DEPFILE_FLAGS_" + lang;
  400. std::string depfileFlags = mf->GetSafeDefinition(flagsName);
  401. if (!depfileFlags.empty()) {
  402. cmSystemTools::ReplaceString(depfileFlags, "<DEPFILE>", "$DEP_FILE");
  403. cmSystemTools::ReplaceString(depfileFlags, "<OBJECT>", "$out");
  404. cmSystemTools::ReplaceString(depfileFlags, "<CMAKE_C_COMPILER>",
  405. mf->GetDefinition("CMAKE_C_COMPILER"));
  406. flags += " " + depfileFlags;
  407. }
  408. }
  409. vars.Flags = flags.c_str();
  410. vars.DependencyFile = depfile.c_str();
  411. CM_AUTO_PTR<cmRulePlaceholderExpander> rulePlaceholderExpander(
  412. this->GetLocalGenerator()->CreateRulePlaceholderExpander());
  413. std::string const tdi = this->GetLocalGenerator()->ConvertToOutputFormat(
  414. ConvertToNinjaPath(this->GetTargetDependInfoPath(lang)),
  415. cmLocalGenerator::SHELL);
  416. std::string launcher;
  417. const char* val = this->GetLocalGenerator()->GetRuleLauncher(
  418. this->GetGeneratorTarget(), "RULE_LAUNCH_COMPILE");
  419. if (val && *val) {
  420. launcher = val;
  421. launcher += " ";
  422. }
  423. if (explicitPP) {
  424. // Lookup the explicit preprocessing rule.
  425. std::string const ppVar = "CMAKE_" + lang + "_PREPROCESS_SOURCE";
  426. std::string const ppCmd =
  427. this->GetMakefile()->GetRequiredDefinition(ppVar);
  428. // Explicit preprocessing always uses a depfile.
  429. std::string const ppDeptype; // no deps= for multiple outputs
  430. std::string const ppDepfile = "$DEP_FILE";
  431. cmRulePlaceholderExpander::RuleVariables ppVars;
  432. ppVars.CMTargetName = vars.CMTargetName;
  433. ppVars.CMTargetType = vars.CMTargetType;
  434. ppVars.Language = vars.Language;
  435. ppVars.Object = "$out"; // for RULE_LAUNCH_COMPILE
  436. ppVars.PreprocessedSource = "$out";
  437. ppVars.DependencyFile = ppDepfile.c_str();
  438. // Preprocessing uses the original source,
  439. // compilation uses preprocessed output.
  440. ppVars.Source = vars.Source;
  441. vars.Source = "$in";
  442. // Preprocessing and compilation use the same flags.
  443. ppVars.Flags = vars.Flags;
  444. // Move preprocessor definitions to the preprocessor rule.
  445. ppVars.Defines = vars.Defines;
  446. vars.Defines = "";
  447. // Copy include directories to the preprocessor rule. The Fortran
  448. // compilation rule still needs them for the INCLUDE directive.
  449. ppVars.Includes = vars.Includes;
  450. // Rule for preprocessing source file.
  451. std::vector<std::string> ppCmds;
  452. cmSystemTools::ExpandListArgument(ppCmd, ppCmds);
  453. for (std::string& i : ppCmds) {
  454. i = launcher + i;
  455. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(),
  456. i, ppVars);
  457. }
  458. // Run CMake dependency scanner on preprocessed output.
  459. std::string const cmake = this->GetLocalGenerator()->ConvertToOutputFormat(
  460. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  461. ppCmds.push_back(
  462. cmake + " -E cmake_ninja_depends"
  463. " --tdi=" +
  464. tdi + " --pp=$out"
  465. " --dep=$DEP_FILE" +
  466. (needDyndep ? " --obj=$OBJ_FILE --ddi=$DYNDEP_INTERMEDIATE_FILE" : ""));
  467. std::string const ppCmdLine =
  468. this->GetLocalGenerator()->BuildCommandLine(ppCmds);
  469. // Write the rule for preprocessing file of the given language.
  470. std::ostringstream ppComment;
  471. ppComment << "Rule for preprocessing " << lang << " files.";
  472. std::ostringstream ppDesc;
  473. ppDesc << "Building " << lang << " preprocessed $out";
  474. this->GetGlobalGenerator()->AddRule(this->LanguagePreprocessRule(lang),
  475. ppCmdLine, ppDesc.str(),
  476. ppComment.str(), ppDepfile, ppDeptype,
  477. /*rspfile*/ "",
  478. /*rspcontent*/ "",
  479. /*restat*/ "",
  480. /*generator*/ false);
  481. }
  482. if (needDyndep) {
  483. // Write the rule for ninja dyndep file generation.
  484. std::vector<std::string> ddCmds;
  485. // Command line length is almost always limited -> use response file for
  486. // dyndep rules
  487. std::string ddRspFile = "$out.rsp";
  488. std::string ddRspContent = "$in";
  489. std::string ddInput = "@" + ddRspFile;
  490. // Run CMake dependency scanner on preprocessed output.
  491. std::string const cmake = this->GetLocalGenerator()->ConvertToOutputFormat(
  492. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  493. ddCmds.push_back(cmake + " -E cmake_ninja_dyndep"
  494. " --tdi=" +
  495. tdi + " --dd=$out"
  496. " " +
  497. ddInput);
  498. std::string const ddCmdLine =
  499. this->GetLocalGenerator()->BuildCommandLine(ddCmds);
  500. std::ostringstream ddComment;
  501. ddComment << "Rule to generate ninja dyndep files for " << lang << ".";
  502. std::ostringstream ddDesc;
  503. ddDesc << "Generating " << lang << " dyndep file $out";
  504. this->GetGlobalGenerator()->AddRule(
  505. this->LanguageDyndepRule(lang), ddCmdLine, ddDesc.str(), ddComment.str(),
  506. /*depfile*/ "",
  507. /*deps*/ "", ddRspFile, ddRspContent,
  508. /*restat*/ "",
  509. /*generator*/ false);
  510. }
  511. // Rule for compiling object file.
  512. std::vector<std::string> compileCmds;
  513. if (lang == "CUDA") {
  514. std::string cmdVar;
  515. if (this->GeneratorTarget->GetPropertyAsBool(
  516. "CUDA_SEPARABLE_COMPILATION")) {
  517. cmdVar = std::string("CMAKE_CUDA_COMPILE_SEPARABLE_COMPILATION");
  518. } else if (this->GeneratorTarget->GetPropertyAsBool(
  519. "CUDA_PTX_COMPILATION")) {
  520. cmdVar = std::string("CMAKE_CUDA_COMPILE_PTX_COMPILATION");
  521. } else {
  522. cmdVar = std::string("CMAKE_CUDA_COMPILE_WHOLE_COMPILATION");
  523. }
  524. std::string compileCmd = mf->GetRequiredDefinition(cmdVar);
  525. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  526. } else {
  527. const std::string cmdVar =
  528. std::string("CMAKE_") + lang + "_COMPILE_OBJECT";
  529. std::string compileCmd = mf->GetRequiredDefinition(cmdVar);
  530. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  531. }
  532. // Maybe insert an include-what-you-use runner.
  533. if (!compileCmds.empty() && (lang == "C" || lang == "CXX")) {
  534. std::string const iwyu_prop = lang + "_INCLUDE_WHAT_YOU_USE";
  535. const char* iwyu = this->GeneratorTarget->GetProperty(iwyu_prop);
  536. std::string const tidy_prop = lang + "_CLANG_TIDY";
  537. const char* tidy = this->GeneratorTarget->GetProperty(tidy_prop);
  538. std::string const cpplint_prop = lang + "_CPPLINT";
  539. const char* cpplint = this->GeneratorTarget->GetProperty(cpplint_prop);
  540. std::string const cppcheck_prop = lang + "_CPPCHECK";
  541. const char* cppcheck = this->GeneratorTarget->GetProperty(cppcheck_prop);
  542. if ((iwyu && *iwyu) || (tidy && *tidy) || (cpplint && *cpplint) ||
  543. (cppcheck && *cppcheck)) {
  544. std::string run_iwyu = this->GetLocalGenerator()->ConvertToOutputFormat(
  545. cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL);
  546. run_iwyu += " -E __run_co_compile";
  547. if (iwyu && *iwyu) {
  548. run_iwyu += " --iwyu=";
  549. run_iwyu += this->GetLocalGenerator()->EscapeForShell(iwyu);
  550. }
  551. if (tidy && *tidy) {
  552. run_iwyu += " --tidy=";
  553. run_iwyu += this->GetLocalGenerator()->EscapeForShell(tidy);
  554. }
  555. if (cpplint && *cpplint) {
  556. run_iwyu += " --cpplint=";
  557. run_iwyu += this->GetLocalGenerator()->EscapeForShell(cpplint);
  558. }
  559. if (cppcheck && *cppcheck) {
  560. run_iwyu += " --cppcheck=";
  561. run_iwyu += this->GetLocalGenerator()->EscapeForShell(cppcheck);
  562. }
  563. if ((tidy && *tidy) || (cpplint && *cpplint) ||
  564. (cppcheck && *cppcheck)) {
  565. run_iwyu += " --source=$in";
  566. }
  567. run_iwyu += " -- ";
  568. compileCmds.front().insert(0, run_iwyu);
  569. }
  570. }
  571. // Maybe insert a compiler launcher like ccache or distcc
  572. if (!compileCmds.empty() &&
  573. (lang == "C" || lang == "CXX" || lang == "CUDA")) {
  574. std::string const clauncher_prop = lang + "_COMPILER_LAUNCHER";
  575. const char* clauncher = this->GeneratorTarget->GetProperty(clauncher_prop);
  576. if (clauncher && *clauncher) {
  577. std::vector<std::string> launcher_cmd;
  578. cmSystemTools::ExpandListArgument(clauncher, launcher_cmd, true);
  579. for (std::string& i : launcher_cmd) {
  580. i = this->LocalGenerator->EscapeForShell(i);
  581. }
  582. std::string const& run_launcher = cmJoin(launcher_cmd, " ") + " ";
  583. compileCmds.front().insert(0, run_launcher);
  584. }
  585. }
  586. if (!compileCmds.empty()) {
  587. compileCmds.front().insert(0, cldeps);
  588. }
  589. for (std::string& i : compileCmds) {
  590. i = launcher + i;
  591. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(), i,
  592. vars);
  593. }
  594. std::string cmdLine =
  595. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  596. // Write the rule for compiling file of the given language.
  597. std::ostringstream comment;
  598. comment << "Rule for compiling " << lang << " files.";
  599. std::ostringstream description;
  600. description << "Building " << lang << " object $out";
  601. this->GetGlobalGenerator()->AddRule(
  602. this->LanguageCompilerRule(lang), cmdLine, description.str(),
  603. comment.str(), depfile, deptype, rspfile, rspcontent,
  604. /*restat*/ "",
  605. /*generator*/ false);
  606. }
  607. void cmNinjaTargetGenerator::WriteObjectBuildStatements()
  608. {
  609. // Write comments.
  610. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  611. this->GetBuildFileStream()
  612. << "# Object build statements for "
  613. << cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType())
  614. << " target " << this->GetTargetName() << "\n\n";
  615. std::string config = this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  616. std::vector<cmSourceFile const*> customCommands;
  617. this->GeneratorTarget->GetCustomCommands(customCommands, config);
  618. for (cmSourceFile const* sf : customCommands) {
  619. cmCustomCommand const* cc = sf->GetCustomCommand();
  620. this->GetLocalGenerator()->AddCustomCommandTarget(
  621. cc, this->GetGeneratorTarget());
  622. // Record the custom commands for this target. The container is used
  623. // in WriteObjectBuildStatement when called in a loop below.
  624. this->CustomCommands.push_back(cc);
  625. }
  626. std::vector<cmSourceFile const*> headerSources;
  627. this->GeneratorTarget->GetHeaderSources(headerSources, config);
  628. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  629. headerSources, this->MacOSXContentGenerator);
  630. std::vector<cmSourceFile const*> extraSources;
  631. this->GeneratorTarget->GetExtraSources(extraSources, config);
  632. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  633. extraSources, this->MacOSXContentGenerator);
  634. std::vector<cmSourceFile const*> externalObjects;
  635. this->GeneratorTarget->GetExternalObjects(externalObjects, config);
  636. for (cmSourceFile const* sf : externalObjects) {
  637. this->Objects.push_back(this->GetSourceFilePath(sf));
  638. }
  639. cmNinjaDeps orderOnlyDeps;
  640. this->GetLocalGenerator()->AppendTargetDepends(
  641. this->GeneratorTarget, orderOnlyDeps, DependOnTargetOrdering);
  642. // Add order-only dependencies on other files associated with the target.
  643. orderOnlyDeps.insert(orderOnlyDeps.end(), this->ExtraFiles.begin(),
  644. this->ExtraFiles.end());
  645. // Add order-only dependencies on custom command outputs.
  646. for (cmCustomCommand const* cc : this->CustomCommands) {
  647. cmCustomCommandGenerator ccg(*cc, this->GetConfigName(),
  648. this->GetLocalGenerator());
  649. const std::vector<std::string>& ccoutputs = ccg.GetOutputs();
  650. const std::vector<std::string>& ccbyproducts = ccg.GetByproducts();
  651. std::transform(ccoutputs.begin(), ccoutputs.end(),
  652. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  653. std::transform(ccbyproducts.begin(), ccbyproducts.end(),
  654. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  655. }
  656. std::sort(orderOnlyDeps.begin(), orderOnlyDeps.end());
  657. orderOnlyDeps.erase(std::unique(orderOnlyDeps.begin(), orderOnlyDeps.end()),
  658. orderOnlyDeps.end());
  659. {
  660. cmNinjaDeps orderOnlyTarget;
  661. orderOnlyTarget.push_back(this->OrderDependsTargetForTarget());
  662. this->GetGlobalGenerator()->WritePhonyBuild(
  663. this->GetBuildFileStream(),
  664. "Order-only phony target for " + this->GetTargetName(), orderOnlyTarget,
  665. cmNinjaDeps(), cmNinjaDeps(), orderOnlyDeps);
  666. }
  667. std::vector<cmSourceFile const*> objectSources;
  668. this->GeneratorTarget->GetObjectSources(objectSources, config);
  669. for (cmSourceFile const* sf : objectSources) {
  670. this->WriteObjectBuildStatement(sf);
  671. }
  672. if (!this->DDIFiles.empty()) {
  673. std::string const ddComment;
  674. std::string const ddRule = this->LanguageDyndepRule("Fortran");
  675. cmNinjaDeps ddOutputs;
  676. cmNinjaDeps ddImplicitOuts;
  677. cmNinjaDeps const& ddExplicitDeps = this->DDIFiles;
  678. cmNinjaDeps ddImplicitDeps;
  679. cmNinjaDeps ddOrderOnlyDeps;
  680. cmNinjaVars ddVars;
  681. this->WriteTargetDependInfo("Fortran");
  682. ddOutputs.push_back(this->GetDyndepFilePath("Fortran"));
  683. // Make sure dyndep files for all our dependencies have already
  684. // been generated so that the 'FortranModules.json' files they
  685. // produced as side-effects are available for us to read.
  686. // Ideally we should depend on the 'FortranModules.json' files
  687. // from our dependencies directly, but we don't know which of
  688. // our dependencies produces them. Fixing this will require
  689. // refactoring the Ninja generator to generate targets in
  690. // dependency order so that we can collect the needed information.
  691. this->GetLocalGenerator()->AppendTargetDepends(
  692. this->GeneratorTarget, ddOrderOnlyDeps, DependOnTargetArtifact);
  693. this->GetGlobalGenerator()->WriteBuild(
  694. this->GetBuildFileStream(), ddComment, ddRule, ddOutputs, ddImplicitOuts,
  695. ddExplicitDeps, ddImplicitDeps, ddOrderOnlyDeps, ddVars);
  696. }
  697. this->GetBuildFileStream() << "\n";
  698. }
  699. void cmNinjaTargetGenerator::WriteObjectBuildStatement(
  700. cmSourceFile const* source)
  701. {
  702. std::string const language = source->GetLanguage();
  703. std::string const sourceFileName =
  704. language == "RC" ? source->GetFullPath() : this->GetSourceFilePath(source);
  705. std::string const objectDir =
  706. this->ConvertToNinjaPath(this->GeneratorTarget->GetSupportDirectory());
  707. std::string const objectFileName =
  708. this->ConvertToNinjaPath(this->GetObjectFilePath(source));
  709. std::string const objectFileDir =
  710. cmSystemTools::GetFilenamePath(objectFileName);
  711. cmNinjaVars vars;
  712. vars["FLAGS"] = this->ComputeFlagsForObject(source, language);
  713. vars["DEFINES"] = this->ComputeDefines(source, language);
  714. vars["INCLUDES"] = this->GetIncludes(language);
  715. if (!this->NeedDepTypeMSVC(language)) {
  716. vars["DEP_FILE"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  717. objectFileName + ".d", cmOutputConverter::SHELL);
  718. }
  719. this->ExportObjectCompileCommand(
  720. language, sourceFileName, objectDir, objectFileName, objectFileDir,
  721. vars["FLAGS"], vars["DEFINES"], vars["INCLUDES"]);
  722. std::string comment;
  723. std::string rule = this->LanguageCompilerRule(language);
  724. cmNinjaDeps outputs;
  725. outputs.push_back(objectFileName);
  726. // Add this object to the list of object files.
  727. this->Objects.push_back(objectFileName);
  728. cmNinjaDeps explicitDeps;
  729. explicitDeps.push_back(sourceFileName);
  730. cmNinjaDeps implicitDeps;
  731. if (const char* objectDeps = source->GetProperty("OBJECT_DEPENDS")) {
  732. std::vector<std::string> depList;
  733. cmSystemTools::ExpandListArgument(objectDeps, depList);
  734. for (std::string& odi : depList) {
  735. if (cmSystemTools::FileIsFullPath(odi)) {
  736. odi = cmSystemTools::CollapseFullPath(odi);
  737. }
  738. }
  739. std::transform(depList.begin(), depList.end(),
  740. std::back_inserter(implicitDeps), MapToNinjaPath());
  741. }
  742. cmNinjaDeps orderOnlyDeps;
  743. orderOnlyDeps.push_back(this->OrderDependsTargetForTarget());
  744. // If the source file is GENERATED and does not have a custom command
  745. // (either attached to this source file or another one), assume that one of
  746. // the target dependencies, OBJECT_DEPENDS or header file custom commands
  747. // will rebuild the file.
  748. if (source->GetPropertyAsBool("GENERATED") && !source->GetCustomCommand() &&
  749. !this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFileName)) {
  750. this->GetGlobalGenerator()->AddAssumedSourceDependencies(sourceFileName,
  751. orderOnlyDeps);
  752. }
  753. // For some cases we need to generate a ninja dyndep file.
  754. bool const needDyndep = this->NeedDyndep(language);
  755. // For some cases we do an explicit preprocessor invocation.
  756. bool const explicitPP = this->NeedExplicitPreprocessing(language);
  757. if (explicitPP) {
  758. std::string const ppComment;
  759. std::string const ppRule = this->LanguagePreprocessRule(language);
  760. cmNinjaDeps ppOutputs;
  761. cmNinjaDeps ppImplicitOuts;
  762. cmNinjaDeps ppExplicitDeps;
  763. cmNinjaDeps ppImplicitDeps;
  764. cmNinjaDeps ppOrderOnlyDeps;
  765. cmNinjaVars ppVars;
  766. std::string const ppFileName =
  767. this->ConvertToNinjaPath(this->GetPreprocessedFilePath(source));
  768. ppOutputs.push_back(ppFileName);
  769. // Move compilation dependencies to the preprocessing build statement.
  770. std::swap(ppExplicitDeps, explicitDeps);
  771. std::swap(ppImplicitDeps, implicitDeps);
  772. std::swap(ppOrderOnlyDeps, orderOnlyDeps);
  773. std::swap(ppVars["IN_ABS"], vars["IN_ABS"]);
  774. // The actual compilation will now use the preprocessed source.
  775. explicitDeps.push_back(ppFileName);
  776. // Preprocessing and compilation generally use the same flags.
  777. ppVars["FLAGS"] = vars["FLAGS"];
  778. // In case compilation requires flags that are incompatible with
  779. // preprocessing, include them here.
  780. std::string const postFlag =
  781. this->Makefile->GetSafeDefinition("CMAKE_Fortran_POSTPROCESS_FLAG");
  782. this->LocalGenerator->AppendFlags(vars["FLAGS"], postFlag);
  783. // Move preprocessor definitions to the preprocessor build statement.
  784. std::swap(ppVars["DEFINES"], vars["DEFINES"]);
  785. // Copy include directories to the preprocessor build statement. The
  786. // Fortran compilation build statement still needs them for the INCLUDE
  787. // directive.
  788. ppVars["INCLUDES"] = vars["INCLUDES"];
  789. // Prepend source file's original directory as an include directory
  790. // so e.g. Fortran INCLUDE statements can look for files in it.
  791. std::vector<std::string> sourceDirectory;
  792. sourceDirectory.push_back(
  793. cmSystemTools::GetParentDirectory(source->GetFullPath()));
  794. std::string sourceDirectoryFlag = this->LocalGenerator->GetIncludeFlags(
  795. sourceDirectory, this->GeneratorTarget, language, false, false,
  796. this->GetConfigName());
  797. vars["INCLUDES"] = sourceDirectoryFlag + " " + vars["INCLUDES"];
  798. // Explicit preprocessing always uses a depfile.
  799. ppVars["DEP_FILE"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  800. ppFileName + ".d", cmOutputConverter::SHELL);
  801. // The actual compilation does not need a depfile because it
  802. // depends on the already-preprocessed source.
  803. vars.erase("DEP_FILE");
  804. if (needDyndep) {
  805. // Tell dependency scanner the object file that will result from
  806. // compiling the preprocessed source.
  807. ppVars["OBJ_FILE"] = objectFileName;
  808. // Tell dependency scanner where to store dyndep intermediate results.
  809. std::string const ddiFile = ppFileName + ".ddi";
  810. ppVars["DYNDEP_INTERMEDIATE_FILE"] = ddiFile;
  811. ppImplicitOuts.push_back(ddiFile);
  812. this->DDIFiles.push_back(ddiFile);
  813. }
  814. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
  815. ppVars);
  816. this->GetGlobalGenerator()->WriteBuild(
  817. this->GetBuildFileStream(), ppComment, ppRule, ppOutputs, ppImplicitOuts,
  818. ppExplicitDeps, ppImplicitDeps, ppOrderOnlyDeps, ppVars);
  819. }
  820. if (needDyndep) {
  821. std::string const dyndep = this->GetDyndepFilePath(language);
  822. orderOnlyDeps.push_back(dyndep);
  823. vars["dyndep"] = dyndep;
  824. }
  825. EnsureParentDirectoryExists(objectFileName);
  826. vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  827. objectDir, cmOutputConverter::SHELL);
  828. vars["OBJECT_FILE_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  829. objectFileDir, cmOutputConverter::SHELL);
  830. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
  831. vars);
  832. this->SetMsvcTargetPdbVariable(vars);
  833. bool const lang_supports_response =
  834. !(language == "RC" || language == "CUDA");
  835. int const commandLineLengthLimit =
  836. ((lang_supports_response && this->ForceResponseFile())) ? -1 : 0;
  837. std::string const rspfile = objectFileName + ".rsp";
  838. this->GetGlobalGenerator()->WriteBuild(
  839. this->GetBuildFileStream(), comment, rule, outputs,
  840. /*implicitOuts=*/cmNinjaDeps(), explicitDeps, implicitDeps, orderOnlyDeps,
  841. vars, rspfile, commandLineLengthLimit);
  842. if (const char* objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
  843. std::vector<std::string> outputList;
  844. cmSystemTools::ExpandListArgument(objectOutputs, outputList);
  845. std::transform(outputList.begin(), outputList.end(), outputList.begin(),
  846. MapToNinjaPath());
  847. this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
  848. "Additional output files.",
  849. outputList, outputs);
  850. }
  851. }
  852. void cmNinjaTargetGenerator::WriteTargetDependInfo(std::string const& lang)
  853. {
  854. Json::Value tdi(Json::objectValue);
  855. tdi["language"] = lang;
  856. tdi["compiler-id"] =
  857. this->Makefile->GetSafeDefinition("CMAKE_" + lang + "_COMPILER_ID");
  858. if (lang == "Fortran") {
  859. std::string mod_dir = this->GeneratorTarget->GetFortranModuleDirectory(
  860. this->Makefile->GetHomeOutputDirectory());
  861. if (mod_dir.empty()) {
  862. mod_dir = this->Makefile->GetCurrentBinaryDirectory();
  863. }
  864. tdi["module-dir"] = mod_dir;
  865. }
  866. tdi["dir-cur-bld"] = this->Makefile->GetCurrentBinaryDirectory();
  867. tdi["dir-cur-src"] = this->Makefile->GetCurrentSourceDirectory();
  868. tdi["dir-top-bld"] = this->Makefile->GetHomeOutputDirectory();
  869. tdi["dir-top-src"] = this->Makefile->GetHomeDirectory();
  870. Json::Value& tdi_include_dirs = tdi["include-dirs"] = Json::arrayValue;
  871. std::vector<std::string> includes;
  872. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  873. lang, this->GetConfigName());
  874. for (std::string const& i : includes) {
  875. // Convert the include directories the same way we do for -I flags.
  876. // See upstream ninja issue 1251.
  877. tdi_include_dirs.append(this->ConvertToNinjaPath(i));
  878. }
  879. Json::Value& tdi_linked_target_dirs = tdi["linked-target-dirs"] =
  880. Json::arrayValue;
  881. std::vector<std::string> linked = this->GetLinkedTargetDirectories();
  882. for (std::string const& l : linked) {
  883. tdi_linked_target_dirs.append(l);
  884. }
  885. std::string const tdin = this->GetTargetDependInfoPath(lang);
  886. cmGeneratedFileStream tdif(tdin.c_str());
  887. tdif << tdi;
  888. }
  889. void cmNinjaTargetGenerator::ExportObjectCompileCommand(
  890. std::string const& language, std::string const& sourceFileName,
  891. std::string const& objectDir, std::string const& objectFileName,
  892. std::string const& objectFileDir, std::string const& flags,
  893. std::string const& defines, std::string const& includes)
  894. {
  895. if (!this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS")) {
  896. return;
  897. }
  898. cmRulePlaceholderExpander::RuleVariables compileObjectVars;
  899. compileObjectVars.Language = language.c_str();
  900. std::string escapedSourceFileName = sourceFileName;
  901. if (!cmSystemTools::FileIsFullPath(sourceFileName.c_str())) {
  902. escapedSourceFileName = cmSystemTools::CollapseFullPath(
  903. escapedSourceFileName, this->GetGlobalGenerator()
  904. ->GetCMakeInstance()
  905. ->GetHomeOutputDirectory());
  906. }
  907. escapedSourceFileName = this->LocalGenerator->ConvertToOutputFormat(
  908. escapedSourceFileName, cmOutputConverter::SHELL);
  909. compileObjectVars.Source = escapedSourceFileName.c_str();
  910. compileObjectVars.Object = objectFileName.c_str();
  911. compileObjectVars.ObjectDir = objectDir.c_str();
  912. compileObjectVars.ObjectFileDir = objectFileDir.c_str();
  913. compileObjectVars.Flags = flags.c_str();
  914. compileObjectVars.Defines = defines.c_str();
  915. compileObjectVars.Includes = includes.c_str();
  916. // Rule for compiling object file.
  917. std::vector<std::string> compileCmds;
  918. if (language == "CUDA") {
  919. std::string cmdVar;
  920. if (this->GeneratorTarget->GetPropertyAsBool(
  921. "CUDA_SEPARABLE_COMPILATION")) {
  922. cmdVar = std::string("CMAKE_CUDA_COMPILE_SEPARABLE_COMPILATION");
  923. } else if (this->GeneratorTarget->GetPropertyAsBool(
  924. "CUDA_PTX_COMPILATION")) {
  925. cmdVar = std::string("CMAKE_CUDA_COMPILE_PTX_COMPILATION");
  926. } else {
  927. cmdVar = std::string("CMAKE_CUDA_COMPILE_WHOLE_COMPILATION");
  928. }
  929. std::string compileCmd =
  930. this->GetMakefile()->GetRequiredDefinition(cmdVar);
  931. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  932. } else {
  933. const std::string cmdVar =
  934. std::string("CMAKE_") + language + "_COMPILE_OBJECT";
  935. std::string compileCmd =
  936. this->GetMakefile()->GetRequiredDefinition(cmdVar);
  937. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  938. }
  939. CM_AUTO_PTR<cmRulePlaceholderExpander> rulePlaceholderExpander(
  940. this->GetLocalGenerator()->CreateRulePlaceholderExpander());
  941. for (std::string& i : compileCmds) {
  942. // no launcher for CMAKE_EXPORT_COMPILE_COMMANDS
  943. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(), i,
  944. compileObjectVars);
  945. }
  946. std::string cmdLine =
  947. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  948. this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine, sourceFileName);
  949. }
  950. void cmNinjaTargetGenerator::EnsureDirectoryExists(
  951. const std::string& path) const
  952. {
  953. if (cmSystemTools::FileIsFullPath(path.c_str())) {
  954. cmSystemTools::MakeDirectory(path.c_str());
  955. } else {
  956. cmGlobalNinjaGenerator* gg = this->GetGlobalGenerator();
  957. std::string fullPath =
  958. std::string(gg->GetCMakeInstance()->GetHomeOutputDirectory());
  959. // Also ensures their is a trailing slash.
  960. gg->StripNinjaOutputPathPrefixAsSuffix(fullPath);
  961. fullPath += path;
  962. cmSystemTools::MakeDirectory(fullPath.c_str());
  963. }
  964. }
  965. void cmNinjaTargetGenerator::EnsureParentDirectoryExists(
  966. const std::string& path) const
  967. {
  968. EnsureDirectoryExists(cmSystemTools::GetParentDirectory(path));
  969. }
  970. void cmNinjaTargetGenerator::MacOSXContentGeneratorType::operator()(
  971. cmSourceFile const& source, const char* pkgloc)
  972. {
  973. // Skip OS X content when not building a Framework or Bundle.
  974. if (!this->Generator->GetGeneratorTarget()->IsBundleOnApple()) {
  975. return;
  976. }
  977. std::string macdir =
  978. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc);
  979. // Get the input file location.
  980. std::string input = source.GetFullPath();
  981. input = this->Generator->GetGlobalGenerator()->ConvertToNinjaPath(input);
  982. // Get the output file location.
  983. std::string output = macdir;
  984. output += "/";
  985. output += cmSystemTools::GetFilenameName(input);
  986. output = this->Generator->GetGlobalGenerator()->ConvertToNinjaPath(output);
  987. // Write a build statement to copy the content into the bundle.
  988. this->Generator->GetGlobalGenerator()->WriteMacOSXContentBuild(input,
  989. output);
  990. // Add as a dependency to the target so that it gets called.
  991. this->Generator->ExtraFiles.push_back(output);
  992. }
  993. void cmNinjaTargetGenerator::addPoolNinjaVariable(
  994. const std::string& pool_property, cmGeneratorTarget* target,
  995. cmNinjaVars& vars)
  996. {
  997. const char* pool = target->GetProperty(pool_property);
  998. if (pool) {
  999. vars["pool"] = pool;
  1000. }
  1001. }
  1002. bool cmNinjaTargetGenerator::ForceResponseFile()
  1003. {
  1004. static std::string const forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
  1005. return (this->GetMakefile()->IsDefinitionSet(forceRspFile) ||
  1006. cmSystemTools::HasEnv(forceRspFile));
  1007. }