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. cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  186. this->GeneratorTarget->GetModuleDefinitionInfo(this->GetConfigName());
  187. if (mdi && !mdi->WindowsExportAllSymbols && !mdi->DefFile.empty()) {
  188. result.push_back(this->ConvertToNinjaPath(mdi->DefFile));
  189. }
  190. // Add a dependency on user-specified manifest files, if any.
  191. std::vector<cmSourceFile const*> manifest_srcs;
  192. this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName);
  193. for (std::vector<cmSourceFile const*>::iterator mi = manifest_srcs.begin();
  194. mi != manifest_srcs.end(); ++mi) {
  195. result.push_back(this->ConvertToNinjaPath((*mi)->GetFullPath()));
  196. }
  197. // Add user-specified dependencies.
  198. if (const char* linkDepends =
  199. this->GeneratorTarget->GetProperty("LINK_DEPENDS")) {
  200. std::vector<std::string> linkDeps;
  201. cmSystemTools::ExpandListArgument(linkDepends, linkDeps);
  202. std::transform(linkDeps.begin(), linkDeps.end(),
  203. std::back_inserter(result), MapToNinjaPath());
  204. }
  205. return result;
  206. }
  207. std::string cmNinjaTargetGenerator::GetSourceFilePath(
  208. cmSourceFile const* source) const
  209. {
  210. return ConvertToNinjaPath(source->GetFullPath());
  211. }
  212. std::string cmNinjaTargetGenerator::GetObjectFilePath(
  213. cmSourceFile const* source) const
  214. {
  215. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  216. if (!path.empty()) {
  217. path += "/";
  218. }
  219. std::string const& objectName = this->GeneratorTarget->GetObjectName(source);
  220. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  221. path += "/";
  222. path += objectName;
  223. return path;
  224. }
  225. std::string cmNinjaTargetGenerator::GetPreprocessedFilePath(
  226. cmSourceFile const* source) const
  227. {
  228. // Choose an extension to compile already-preprocessed source.
  229. std::string ppExt = source->GetExtension();
  230. if (cmHasLiteralPrefix(ppExt, "F")) {
  231. // Some Fortran compilers automatically enable preprocessing for
  232. // upper-case extensions. Since the source is already preprocessed,
  233. // use a lower-case extension.
  234. ppExt = cmSystemTools::LowerCase(ppExt);
  235. }
  236. if (ppExt == "fpp") {
  237. // Some Fortran compilers automatically enable preprocessing for
  238. // the ".fpp" extension. Since the source is already preprocessed,
  239. // use the ".f" extension.
  240. ppExt = "f";
  241. }
  242. // Take the object file name and replace the extension.
  243. std::string const& objName = this->GeneratorTarget->GetObjectName(source);
  244. std::string const& objExt =
  245. this->GetGlobalGenerator()->GetLanguageOutputExtension(*source);
  246. assert(objName.size() >= objExt.size());
  247. std::string const ppName =
  248. objName.substr(0, objName.size() - objExt.size()) + "-pp." + ppExt;
  249. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  250. if (!path.empty()) {
  251. path += "/";
  252. }
  253. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  254. path += "/";
  255. path += ppName;
  256. return path;
  257. }
  258. std::string cmNinjaTargetGenerator::GetDyndepFilePath(
  259. std::string const& lang) const
  260. {
  261. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  262. if (!path.empty()) {
  263. path += "/";
  264. }
  265. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  266. path += "/";
  267. path += lang;
  268. path += ".dd";
  269. return path;
  270. }
  271. std::string cmNinjaTargetGenerator::GetTargetDependInfoPath(
  272. std::string const& lang) const
  273. {
  274. std::string path = this->Makefile->GetCurrentBinaryDirectory();
  275. path += "/";
  276. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  277. path += "/" + lang + "DependInfo.json";
  278. return path;
  279. }
  280. std::string cmNinjaTargetGenerator::GetTargetOutputDir() const
  281. {
  282. std::string dir = this->GeneratorTarget->GetDirectory(this->GetConfigName());
  283. return ConvertToNinjaPath(dir);
  284. }
  285. std::string cmNinjaTargetGenerator::GetTargetFilePath(
  286. const std::string& name) const
  287. {
  288. std::string path = this->GetTargetOutputDir();
  289. if (path.empty() || path == ".") {
  290. return name;
  291. }
  292. path += "/";
  293. path += name;
  294. return path;
  295. }
  296. std::string cmNinjaTargetGenerator::GetTargetName() const
  297. {
  298. return this->GeneratorTarget->GetName();
  299. }
  300. bool cmNinjaTargetGenerator::SetMsvcTargetPdbVariable(cmNinjaVars& vars) const
  301. {
  302. cmMakefile* mf = this->GetMakefile();
  303. if (mf->GetDefinition("MSVC_C_ARCHITECTURE_ID") ||
  304. mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID") ||
  305. mf->GetDefinition("MSVC_CUDA_ARCHITECTURE_ID")) {
  306. std::string pdbPath;
  307. std::string compilePdbPath = this->ComputeTargetCompilePDB();
  308. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE ||
  309. this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  310. this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  311. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  312. pdbPath = this->GeneratorTarget->GetPDBDirectory(this->GetConfigName());
  313. pdbPath += "/";
  314. pdbPath += this->GeneratorTarget->GetPDBName(this->GetConfigName());
  315. }
  316. vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  317. ConvertToNinjaPath(pdbPath), cmOutputConverter::SHELL);
  318. vars["TARGET_COMPILE_PDB"] =
  319. this->GetLocalGenerator()->ConvertToOutputFormat(
  320. ConvertToNinjaPath(compilePdbPath), cmOutputConverter::SHELL);
  321. EnsureParentDirectoryExists(pdbPath);
  322. EnsureParentDirectoryExists(compilePdbPath);
  323. return true;
  324. }
  325. return false;
  326. }
  327. void cmNinjaTargetGenerator::WriteLanguageRules(const std::string& language)
  328. {
  329. #ifdef NINJA_GEN_VERBOSE_FILES
  330. this->GetRulesFileStream() << "# Rules for language " << language << "\n\n";
  331. #endif
  332. this->WriteCompileRule(language);
  333. }
  334. void cmNinjaTargetGenerator::WriteCompileRule(const std::string& lang)
  335. {
  336. cmRulePlaceholderExpander::RuleVariables vars;
  337. vars.CMTargetName = this->GetGeneratorTarget()->GetName().c_str();
  338. vars.CMTargetType =
  339. cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType());
  340. vars.Language = lang.c_str();
  341. vars.Source = "$in";
  342. vars.Object = "$out";
  343. vars.Defines = "$DEFINES";
  344. vars.Includes = "$INCLUDES";
  345. vars.TargetPDB = "$TARGET_PDB";
  346. vars.TargetCompilePDB = "$TARGET_COMPILE_PDB";
  347. vars.ObjectDir = "$OBJECT_DIR";
  348. vars.ObjectFileDir = "$OBJECT_FILE_DIR";
  349. // For some cases we do an explicit preprocessor invocation.
  350. bool const explicitPP = this->NeedExplicitPreprocessing(lang);
  351. bool const needDyndep = this->NeedDyndep(lang);
  352. cmMakefile* mf = this->GetMakefile();
  353. std::string flags = "$FLAGS";
  354. std::string rspfile;
  355. std::string rspcontent;
  356. std::string responseFlag;
  357. if (lang != "RC" && this->ForceResponseFile()) {
  358. rspfile = "$RSP_FILE";
  359. responseFlag = "@" + rspfile;
  360. rspcontent = " $DEFINES $INCLUDES $FLAGS";
  361. flags = responseFlag;
  362. vars.Defines = "";
  363. vars.Includes = "";
  364. }
  365. // Tell ninja dependency format so all deps can be loaded into a database
  366. std::string deptype;
  367. std::string depfile;
  368. std::string cldeps;
  369. if (explicitPP) {
  370. // The explicit preprocessing step will handle dependency scanning.
  371. } else if (this->NeedDepTypeMSVC(lang)) {
  372. deptype = "msvc";
  373. depfile = "";
  374. flags += " /showIncludes";
  375. } else if (mf->IsOn("CMAKE_NINJA_CMCLDEPS_" + lang)) {
  376. // For the MS resource compiler we need cmcldeps, but skip dependencies
  377. // for source-file try_compile cases because they are always fresh.
  378. if (!mf->GetIsSourceFileTryCompile()) {
  379. deptype = "gcc";
  380. depfile = "$DEP_FILE";
  381. const std::string cl = mf->GetDefinition("CMAKE_C_COMPILER")
  382. ? mf->GetSafeDefinition("CMAKE_C_COMPILER")
  383. : mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
  384. cldeps = "\"";
  385. cldeps += cmSystemTools::GetCMClDepsCommand();
  386. cldeps += "\" " + lang + " " + vars.Source + " \"$DEP_FILE\" $out \"";
  387. cldeps += mf->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX");
  388. cldeps += "\" \"" + cl + "\" ";
  389. }
  390. } else {
  391. deptype = "gcc";
  392. const char* langdeptype = mf->GetDefinition("CMAKE_NINJA_DEPTYPE_" + lang);
  393. if (langdeptype) {
  394. deptype = langdeptype;
  395. }
  396. depfile = "$DEP_FILE";
  397. const std::string flagsName = "CMAKE_DEPFILE_FLAGS_" + lang;
  398. std::string depfileFlags = mf->GetSafeDefinition(flagsName);
  399. if (!depfileFlags.empty()) {
  400. cmSystemTools::ReplaceString(depfileFlags, "<DEPFILE>", "$DEP_FILE");
  401. cmSystemTools::ReplaceString(depfileFlags, "<OBJECT>", "$out");
  402. cmSystemTools::ReplaceString(depfileFlags, "<CMAKE_C_COMPILER>",
  403. mf->GetDefinition("CMAKE_C_COMPILER"));
  404. flags += " " + depfileFlags;
  405. }
  406. }
  407. vars.Flags = flags.c_str();
  408. vars.DependencyFile = depfile.c_str();
  409. CM_AUTO_PTR<cmRulePlaceholderExpander> rulePlaceholderExpander(
  410. this->GetLocalGenerator()->CreateRulePlaceholderExpander());
  411. std::string const tdi = this->GetLocalGenerator()->ConvertToOutputFormat(
  412. ConvertToNinjaPath(this->GetTargetDependInfoPath(lang)),
  413. cmLocalGenerator::SHELL);
  414. std::string launcher;
  415. const char* val = this->GetLocalGenerator()->GetRuleLauncher(
  416. this->GetGeneratorTarget(), "RULE_LAUNCH_COMPILE");
  417. if (val && *val) {
  418. launcher = val;
  419. launcher += " ";
  420. }
  421. if (explicitPP) {
  422. // Lookup the explicit preprocessing rule.
  423. std::string const ppVar = "CMAKE_" + lang + "_PREPROCESS_SOURCE";
  424. std::string const ppCmd =
  425. this->GetMakefile()->GetRequiredDefinition(ppVar);
  426. // Explicit preprocessing always uses a depfile.
  427. std::string const ppDeptype; // no deps= for multiple outputs
  428. std::string const ppDepfile = "$DEP_FILE";
  429. cmRulePlaceholderExpander::RuleVariables ppVars;
  430. ppVars.CMTargetName = vars.CMTargetName;
  431. ppVars.CMTargetType = vars.CMTargetType;
  432. ppVars.Language = vars.Language;
  433. ppVars.Object = "$out"; // for RULE_LAUNCH_COMPILE
  434. ppVars.PreprocessedSource = "$out";
  435. ppVars.DependencyFile = ppDepfile.c_str();
  436. // Preprocessing uses the original source,
  437. // compilation uses preprocessed output.
  438. ppVars.Source = vars.Source;
  439. vars.Source = "$in";
  440. // Preprocessing and compilation use the same flags.
  441. ppVars.Flags = vars.Flags;
  442. // Move preprocessor definitions to the preprocessor rule.
  443. ppVars.Defines = vars.Defines;
  444. vars.Defines = "";
  445. // Copy include directories to the preprocessor rule. The Fortran
  446. // compilation rule still needs them for the INCLUDE directive.
  447. ppVars.Includes = vars.Includes;
  448. // Rule for preprocessing source file.
  449. std::vector<std::string> ppCmds;
  450. cmSystemTools::ExpandListArgument(ppCmd, ppCmds);
  451. for (std::vector<std::string>::iterator i = ppCmds.begin();
  452. i != ppCmds.end(); ++i) {
  453. *i = launcher + *i;
  454. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(),
  455. *i, ppVars);
  456. }
  457. // Run CMake dependency scanner on preprocessed output.
  458. std::string const cmake = this->GetLocalGenerator()->ConvertToOutputFormat(
  459. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  460. ppCmds.push_back(
  461. cmake + " -E cmake_ninja_depends"
  462. " --tdi=" +
  463. tdi + " --pp=$out"
  464. " --dep=$DEP_FILE" +
  465. (needDyndep ? " --obj=$OBJ_FILE --ddi=$DYNDEP_INTERMEDIATE_FILE" : ""));
  466. std::string const ppCmdLine =
  467. this->GetLocalGenerator()->BuildCommandLine(ppCmds);
  468. // Write the rule for preprocessing file of the given language.
  469. std::ostringstream ppComment;
  470. ppComment << "Rule for preprocessing " << lang << " files.";
  471. std::ostringstream ppDesc;
  472. ppDesc << "Building " << lang << " preprocessed $out";
  473. this->GetGlobalGenerator()->AddRule(this->LanguagePreprocessRule(lang),
  474. ppCmdLine, ppDesc.str(),
  475. ppComment.str(), ppDepfile, ppDeptype,
  476. /*rspfile*/ "",
  477. /*rspcontent*/ "",
  478. /*restat*/ "",
  479. /*generator*/ false);
  480. }
  481. if (needDyndep) {
  482. // Write the rule for ninja dyndep file generation.
  483. std::vector<std::string> ddCmds;
  484. // Run CMake dependency scanner on preprocessed output.
  485. std::string const cmake = this->GetLocalGenerator()->ConvertToOutputFormat(
  486. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  487. ddCmds.push_back(cmake + " -E cmake_ninja_dyndep"
  488. " --tdi=" +
  489. tdi + " --dd=$out"
  490. " $in");
  491. std::string const ddCmdLine =
  492. this->GetLocalGenerator()->BuildCommandLine(ddCmds);
  493. std::ostringstream ddComment;
  494. ddComment << "Rule to generate ninja dyndep files for " << lang << ".";
  495. std::ostringstream ddDesc;
  496. ddDesc << "Generating " << lang << " dyndep file $out";
  497. this->GetGlobalGenerator()->AddRule(
  498. this->LanguageDyndepRule(lang), ddCmdLine, ddDesc.str(), ddComment.str(),
  499. /*depfile*/ "",
  500. /*deps*/ "",
  501. /*rspfile*/ "",
  502. /*rspcontent*/ "",
  503. /*restat*/ "",
  504. /*generator*/ false);
  505. }
  506. // Rule for compiling object file.
  507. std::vector<std::string> compileCmds;
  508. if (lang == "CUDA") {
  509. std::string cmdVar;
  510. if (this->GeneratorTarget->GetPropertyAsBool(
  511. "CUDA_SEPARABLE_COMPILATION")) {
  512. cmdVar = std::string("CMAKE_CUDA_COMPILE_SEPARABLE_COMPILATION");
  513. } else {
  514. cmdVar = std::string("CMAKE_CUDA_COMPILE_WHOLE_COMPILATION");
  515. }
  516. std::string compileCmd = mf->GetRequiredDefinition(cmdVar);
  517. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  518. } else {
  519. const std::string cmdVar =
  520. std::string("CMAKE_") + lang + "_COMPILE_OBJECT";
  521. std::string compileCmd = mf->GetRequiredDefinition(cmdVar);
  522. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  523. }
  524. // Maybe insert an include-what-you-use runner.
  525. if (!compileCmds.empty() && (lang == "C" || lang == "CXX")) {
  526. std::string const iwyu_prop = lang + "_INCLUDE_WHAT_YOU_USE";
  527. const char* iwyu = this->GeneratorTarget->GetProperty(iwyu_prop);
  528. std::string const tidy_prop = lang + "_CLANG_TIDY";
  529. const char* tidy = this->GeneratorTarget->GetProperty(tidy_prop);
  530. std::string const cpplint_prop = lang + "_CPPLINT";
  531. const char* cpplint = this->GeneratorTarget->GetProperty(cpplint_prop);
  532. if ((iwyu && *iwyu) || (tidy && *tidy) || (cpplint && *cpplint)) {
  533. std::string run_iwyu = this->GetLocalGenerator()->ConvertToOutputFormat(
  534. cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL);
  535. run_iwyu += " -E __run_iwyu";
  536. if (iwyu && *iwyu) {
  537. run_iwyu += " --iwyu=";
  538. run_iwyu += this->GetLocalGenerator()->EscapeForShell(iwyu);
  539. }
  540. if (tidy && *tidy) {
  541. run_iwyu += " --tidy=";
  542. run_iwyu += this->GetLocalGenerator()->EscapeForShell(tidy);
  543. }
  544. if (cpplint && *cpplint) {
  545. run_iwyu += " --cpplint=";
  546. run_iwyu += this->GetLocalGenerator()->EscapeForShell(cpplint);
  547. }
  548. if ((tidy && *tidy) || (cpplint && *cpplint)) {
  549. run_iwyu += " --source=$in";
  550. }
  551. run_iwyu += " -- ";
  552. compileCmds.front().insert(0, run_iwyu);
  553. }
  554. }
  555. // Maybe insert a compiler launcher like ccache or distcc
  556. if (!compileCmds.empty() && (lang == "C" || lang == "CXX")) {
  557. std::string const clauncher_prop = lang + "_COMPILER_LAUNCHER";
  558. const char* clauncher = this->GeneratorTarget->GetProperty(clauncher_prop);
  559. if (clauncher && *clauncher) {
  560. std::vector<std::string> launcher_cmd;
  561. cmSystemTools::ExpandListArgument(clauncher, launcher_cmd, true);
  562. for (std::vector<std::string>::iterator i = launcher_cmd.begin(),
  563. e = launcher_cmd.end();
  564. i != e; ++i) {
  565. *i = this->LocalGenerator->EscapeForShell(*i);
  566. }
  567. std::string const& run_launcher = cmJoin(launcher_cmd, " ") + " ";
  568. compileCmds.front().insert(0, run_launcher);
  569. }
  570. }
  571. if (!compileCmds.empty()) {
  572. compileCmds.front().insert(0, cldeps);
  573. }
  574. for (std::vector<std::string>::iterator i = compileCmds.begin();
  575. i != compileCmds.end(); ++i) {
  576. *i = launcher + *i;
  577. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(), *i,
  578. vars);
  579. }
  580. std::string cmdLine =
  581. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  582. // Write the rule for compiling file of the given language.
  583. std::ostringstream comment;
  584. comment << "Rule for compiling " << lang << " files.";
  585. std::ostringstream description;
  586. description << "Building " << lang << " object $out";
  587. this->GetGlobalGenerator()->AddRule(
  588. this->LanguageCompilerRule(lang), cmdLine, description.str(),
  589. comment.str(), depfile, deptype, rspfile, rspcontent,
  590. /*restat*/ "",
  591. /*generator*/ false);
  592. }
  593. void cmNinjaTargetGenerator::WriteObjectBuildStatements()
  594. {
  595. // Write comments.
  596. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  597. this->GetBuildFileStream()
  598. << "# Object build statements for "
  599. << cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType())
  600. << " target " << this->GetTargetName() << "\n\n";
  601. std::string config = this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  602. std::vector<cmSourceFile const*> customCommands;
  603. this->GeneratorTarget->GetCustomCommands(customCommands, config);
  604. for (std::vector<cmSourceFile const*>::const_iterator si =
  605. customCommands.begin();
  606. si != customCommands.end(); ++si) {
  607. cmCustomCommand const* cc = (*si)->GetCustomCommand();
  608. this->GetLocalGenerator()->AddCustomCommandTarget(
  609. cc, this->GetGeneratorTarget());
  610. // Record the custom commands for this target. The container is used
  611. // in WriteObjectBuildStatement when called in a loop below.
  612. this->CustomCommands.push_back(cc);
  613. }
  614. std::vector<cmSourceFile const*> headerSources;
  615. this->GeneratorTarget->GetHeaderSources(headerSources, config);
  616. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  617. headerSources, this->MacOSXContentGenerator);
  618. std::vector<cmSourceFile const*> extraSources;
  619. this->GeneratorTarget->GetExtraSources(extraSources, config);
  620. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  621. extraSources, this->MacOSXContentGenerator);
  622. std::vector<cmSourceFile const*> externalObjects;
  623. this->GeneratorTarget->GetExternalObjects(externalObjects, config);
  624. for (std::vector<cmSourceFile const*>::const_iterator si =
  625. externalObjects.begin();
  626. si != externalObjects.end(); ++si) {
  627. this->Objects.push_back(this->GetSourceFilePath(*si));
  628. }
  629. cmNinjaDeps orderOnlyDeps;
  630. this->GetLocalGenerator()->AppendTargetDepends(this->GeneratorTarget,
  631. orderOnlyDeps);
  632. // Add order-only dependencies on other files associated with the target.
  633. orderOnlyDeps.insert(orderOnlyDeps.end(), this->ExtraFiles.begin(),
  634. this->ExtraFiles.end());
  635. // Add order-only dependencies on custom command outputs.
  636. for (std::vector<cmCustomCommand const*>::const_iterator cci =
  637. this->CustomCommands.begin();
  638. cci != this->CustomCommands.end(); ++cci) {
  639. cmCustomCommand const* cc = *cci;
  640. cmCustomCommandGenerator ccg(*cc, this->GetConfigName(),
  641. this->GetLocalGenerator());
  642. const std::vector<std::string>& ccoutputs = ccg.GetOutputs();
  643. const std::vector<std::string>& ccbyproducts = ccg.GetByproducts();
  644. std::transform(ccoutputs.begin(), ccoutputs.end(),
  645. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  646. std::transform(ccbyproducts.begin(), ccbyproducts.end(),
  647. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  648. }
  649. if (!orderOnlyDeps.empty()) {
  650. cmNinjaDeps orderOnlyTarget;
  651. orderOnlyTarget.push_back(this->OrderDependsTargetForTarget());
  652. this->GetGlobalGenerator()->WritePhonyBuild(
  653. this->GetBuildFileStream(),
  654. "Order-only phony target for " + this->GetTargetName(), orderOnlyTarget,
  655. cmNinjaDeps(), cmNinjaDeps(), orderOnlyDeps);
  656. }
  657. std::vector<cmSourceFile const*> objectSources;
  658. this->GeneratorTarget->GetObjectSources(objectSources, config);
  659. for (std::vector<cmSourceFile const*>::const_iterator si =
  660. objectSources.begin();
  661. si != objectSources.end(); ++si) {
  662. this->WriteObjectBuildStatement(*si, !orderOnlyDeps.empty());
  663. }
  664. if (!this->DDIFiles.empty()) {
  665. std::string const ddComment;
  666. std::string const ddRule = this->LanguageDyndepRule("Fortran");
  667. cmNinjaDeps ddOutputs;
  668. cmNinjaDeps ddImplicitOuts;
  669. cmNinjaDeps const& ddExplicitDeps = this->DDIFiles;
  670. cmNinjaDeps ddImplicitDeps;
  671. cmNinjaDeps ddOrderOnlyDeps;
  672. cmNinjaVars ddVars;
  673. this->WriteTargetDependInfo("Fortran");
  674. ddOutputs.push_back(this->GetDyndepFilePath("Fortran"));
  675. this->GetGlobalGenerator()->WriteBuild(
  676. this->GetBuildFileStream(), ddComment, ddRule, ddOutputs, ddImplicitOuts,
  677. ddExplicitDeps, ddImplicitDeps, ddOrderOnlyDeps, ddVars);
  678. }
  679. this->GetBuildFileStream() << "\n";
  680. }
  681. void cmNinjaTargetGenerator::WriteObjectBuildStatement(
  682. cmSourceFile const* source, bool writeOrderDependsTargetForTarget)
  683. {
  684. std::string const language = source->GetLanguage();
  685. std::string const sourceFileName =
  686. language == "RC" ? source->GetFullPath() : this->GetSourceFilePath(source);
  687. std::string const objectDir =
  688. this->ConvertToNinjaPath(this->GeneratorTarget->GetSupportDirectory());
  689. std::string const objectFileName =
  690. this->ConvertToNinjaPath(this->GetObjectFilePath(source));
  691. std::string const objectFileDir =
  692. cmSystemTools::GetFilenamePath(objectFileName);
  693. cmNinjaVars vars;
  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. }