cmNinjaTargetGenerator.cxx 41 KB

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