cmNinjaTargetGenerator.cxx 41 KB

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