cmNinjaTargetGenerator.cxx 43 KB

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