cmNinjaTargetGenerator.cxx 42 KB

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