cmNinjaTargetGenerator.cxx 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmNinjaTargetGenerator.h"
  4. #include <algorithm>
  5. #include <array>
  6. #include <cassert>
  7. #include <functional>
  8. #include <iterator>
  9. #include <map>
  10. #include <ostream>
  11. #include <unordered_map>
  12. #include <unordered_set>
  13. #include <utility>
  14. #include <cm/memory>
  15. #include <cm/optional>
  16. #include <cm/string_view>
  17. #include <cmext/algorithm>
  18. #include <cmext/string_view>
  19. #include <cm3p/json/value.h>
  20. #include <cm3p/json/writer.h>
  21. #include "cmBuildDatabase.h"
  22. #include "cmComputeLinkInformation.h"
  23. #include "cmCustomCommandGenerator.h"
  24. #include "cmDyndepCollation.h"
  25. #include "cmFileSet.h"
  26. #include "cmGeneratedFileStream.h"
  27. #include "cmGeneratorExpression.h"
  28. #include "cmGeneratorOptions.h"
  29. #include "cmGeneratorTarget.h"
  30. #include "cmGlobalCommonGenerator.h"
  31. #include "cmGlobalNinjaGenerator.h"
  32. #include "cmList.h"
  33. #include "cmListFileCache.h"
  34. #include "cmLocalGenerator.h"
  35. #include "cmLocalNinjaGenerator.h"
  36. #include "cmMakefile.h"
  37. #include "cmMessageType.h"
  38. #include "cmNinjaNormalTargetGenerator.h"
  39. #include "cmNinjaUtilityTargetGenerator.h"
  40. #include "cmOutputConverter.h"
  41. #include "cmPolicies.h"
  42. #include "cmRange.h"
  43. #include "cmRulePlaceholderExpander.h"
  44. #include "cmSourceFile.h"
  45. #include "cmState.h"
  46. #include "cmStateTypes.h"
  47. #include "cmStringAlgorithms.h"
  48. #include "cmSystemTools.h"
  49. #include "cmTarget.h"
  50. #include "cmTargetDepend.h"
  51. #include "cmValue.h"
  52. #include "cmake.h"
  53. class cmCustomCommand;
  54. std::unique_ptr<cmNinjaTargetGenerator> cmNinjaTargetGenerator::New(
  55. cmGeneratorTarget* target)
  56. {
  57. switch (target->GetType()) {
  58. case cmStateEnums::EXECUTABLE:
  59. case cmStateEnums::SHARED_LIBRARY:
  60. case cmStateEnums::STATIC_LIBRARY:
  61. case cmStateEnums::MODULE_LIBRARY:
  62. case cmStateEnums::OBJECT_LIBRARY:
  63. return cm::make_unique<cmNinjaNormalTargetGenerator>(target);
  64. case cmStateEnums::INTERFACE_LIBRARY:
  65. if (target->HaveCxx20ModuleSources()) {
  66. return cm::make_unique<cmNinjaNormalTargetGenerator>(target);
  67. }
  68. CM_FALLTHROUGH;
  69. case cmStateEnums::UTILITY:
  70. case cmStateEnums::GLOBAL_TARGET:
  71. return cm::make_unique<cmNinjaUtilityTargetGenerator>(target);
  72. default:
  73. return std::unique_ptr<cmNinjaTargetGenerator>();
  74. }
  75. }
  76. cmNinjaTargetGenerator::cmNinjaTargetGenerator(cmGeneratorTarget* target)
  77. : cmCommonTargetGenerator(target)
  78. , OSXBundleGenerator(nullptr)
  79. , LocalGenerator(
  80. static_cast<cmLocalNinjaGenerator*>(target->GetLocalGenerator()))
  81. {
  82. for (auto const& fileConfig : this->LocalGenerator->GetConfigNames()) {
  83. this->Configs[fileConfig].MacOSXContentGenerator =
  84. cm::make_unique<MacOSXContentGeneratorType>(this, fileConfig);
  85. }
  86. }
  87. cmNinjaTargetGenerator::~cmNinjaTargetGenerator() = default;
  88. cmGeneratedFileStream& cmNinjaTargetGenerator::GetImplFileStream(
  89. std::string const& config) const
  90. {
  91. return *this->GetGlobalGenerator()->GetImplFileStream(config);
  92. }
  93. cmGeneratedFileStream& cmNinjaTargetGenerator::GetCommonFileStream() const
  94. {
  95. return *this->GetGlobalGenerator()->GetCommonFileStream();
  96. }
  97. cmGeneratedFileStream& cmNinjaTargetGenerator::GetRulesFileStream() const
  98. {
  99. return *this->GetGlobalGenerator()->GetRulesFileStream();
  100. }
  101. cmGlobalNinjaGenerator* cmNinjaTargetGenerator::GetGlobalGenerator() const
  102. {
  103. return this->LocalGenerator->GetGlobalNinjaGenerator();
  104. }
  105. std::string cmNinjaTargetGenerator::LanguageCompilerRule(
  106. std::string const& lang, std::string const& config,
  107. WithScanning withScanning) const
  108. {
  109. return cmStrCat(
  110. lang, "_COMPILER__",
  111. cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName()),
  112. withScanning == WithScanning::Yes ? "_scanned_" : "_unscanned_", config);
  113. }
  114. std::string cmNinjaTargetGenerator::LanguagePreprocessAndScanRule(
  115. std::string const& lang, std::string const& config) const
  116. {
  117. return cmStrCat(
  118. lang, "_PREPROCESS_SCAN__",
  119. cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName()),
  120. '_', config);
  121. }
  122. std::string cmNinjaTargetGenerator::LanguageScanRule(
  123. std::string const& lang, std::string const& config) const
  124. {
  125. return cmStrCat(
  126. lang, "_SCAN__",
  127. cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName()),
  128. '_', config);
  129. }
  130. bool cmNinjaTargetGenerator::NeedExplicitPreprocessing(
  131. std::string const& lang) const
  132. {
  133. return lang == "Fortran" || lang == "Swift";
  134. }
  135. bool cmNinjaTargetGenerator::CompileWithDefines(std::string const& lang) const
  136. {
  137. return this->Makefile->IsOn(
  138. cmStrCat("CMAKE_", lang, "_COMPILE_WITH_DEFINES"));
  139. }
  140. std::string cmNinjaTargetGenerator::LanguageDyndepRule(
  141. std::string const& lang, std::string const& config) const
  142. {
  143. return cmStrCat(
  144. lang, "_DYNDEP__",
  145. cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName()),
  146. '_', config);
  147. }
  148. std::string cmNinjaTargetGenerator::OrderDependsTargetForTarget(
  149. std::string const& config)
  150. {
  151. return this->GetGlobalGenerator()->OrderDependsTargetForTarget(
  152. this->GeneratorTarget, config);
  153. }
  154. std::string cmNinjaTargetGenerator::OrderDependsTargetForTargetPrivate(
  155. std::string const& config)
  156. {
  157. return this->GetGlobalGenerator()->OrderDependsTargetForTargetPrivate(
  158. this->GeneratorTarget, config);
  159. }
  160. // TODO: Most of the code is picked up from
  161. // void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink),
  162. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  163. // Refactor it.
  164. std::string cmNinjaTargetGenerator::ComputeFlagsForObject(
  165. cmSourceFile const* source, std::string const& language,
  166. std::string const& config, std::string const& objectFileName)
  167. {
  168. std::unordered_map<std::string, std::string> pchSources;
  169. std::vector<std::string> pchArchs =
  170. this->GeneratorTarget->GetPchArchs(config, language);
  171. std::string filterArch;
  172. for (std::string const& arch : pchArchs) {
  173. std::string const pchSource =
  174. this->GeneratorTarget->GetPchSource(config, language, arch);
  175. if (pchSource == source->GetFullPath()) {
  176. filterArch = arch;
  177. }
  178. if (!pchSource.empty()) {
  179. pchSources.insert(std::make_pair(pchSource, arch));
  180. }
  181. }
  182. std::string flags;
  183. // Explicitly add the explicit language flag before any other flag
  184. // so user flags can override it.
  185. this->GeneratorTarget->AddExplicitLanguageFlags(flags, *source);
  186. if (!flags.empty()) {
  187. flags += " ";
  188. }
  189. flags += this->GetFlags(language, config, filterArch);
  190. // Add Fortran format flags.
  191. if (language == "Fortran") {
  192. this->AppendFortranFormatFlags(flags, *source);
  193. this->AppendFortranPreprocessFlags(flags, *source,
  194. PreprocessFlagsRequired::NO);
  195. }
  196. // Add source file specific flags.
  197. cmGeneratorExpressionInterpreter genexInterpreter(
  198. this->LocalGenerator, config, this->GeneratorTarget, language);
  199. std::string const COMPILE_FLAGS("COMPILE_FLAGS");
  200. if (cmValue cflags = source->GetProperty(COMPILE_FLAGS)) {
  201. this->LocalGenerator->AppendFlags(
  202. flags, genexInterpreter.Evaluate(*cflags, COMPILE_FLAGS));
  203. }
  204. std::string const COMPILE_OPTIONS("COMPILE_OPTIONS");
  205. if (cmValue coptions = source->GetProperty(COMPILE_OPTIONS)) {
  206. this->LocalGenerator->AppendCompileOptions(
  207. flags, genexInterpreter.Evaluate(*coptions, COMPILE_OPTIONS));
  208. }
  209. // Add precompile headers compile options.
  210. if (!pchSources.empty() && !source->GetProperty("SKIP_PRECOMPILE_HEADERS")) {
  211. std::string pchOptions;
  212. auto pchIt = pchSources.find(source->GetFullPath());
  213. if (pchIt != pchSources.end()) {
  214. pchOptions = this->GeneratorTarget->GetPchCreateCompileOptions(
  215. config, language, pchIt->second);
  216. } else {
  217. pchOptions =
  218. this->GeneratorTarget->GetPchUseCompileOptions(config, language);
  219. }
  220. this->LocalGenerator->AppendCompileOptions(
  221. flags, genexInterpreter.Evaluate(pchOptions, COMPILE_OPTIONS));
  222. }
  223. auto const* fs = this->GeneratorTarget->GetFileSetForSource(config, source);
  224. if (fs && fs->GetType() == "CXX_MODULES"_s) {
  225. if (source->GetLanguage() != "CXX"_s) {
  226. this->GetMakefile()->IssueMessage(
  227. MessageType::FATAL_ERROR,
  228. cmStrCat("Target \"", this->GeneratorTarget->Target->GetName(),
  229. "\" contains the source\n ", source->GetFullPath(),
  230. "\nin a file set of type \"", fs->GetType(),
  231. R"(" but the source is not classified as a "CXX" source.)"));
  232. }
  233. if (!this->GeneratorTarget->Target->IsNormal()) {
  234. auto flag = this->GetMakefile()->GetSafeDefinition(
  235. "CMAKE_CXX_MODULE_BMI_ONLY_FLAG");
  236. cmRulePlaceholderExpander::RuleVariables compileObjectVars;
  237. compileObjectVars.Object = objectFileName.c_str();
  238. auto rulePlaceholderExpander =
  239. this->GetLocalGenerator()->CreateRulePlaceholderExpander();
  240. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(),
  241. flag, compileObjectVars);
  242. this->LocalGenerator->AppendCompileOptions(flags, flag);
  243. }
  244. }
  245. return flags;
  246. }
  247. void cmNinjaTargetGenerator::AddIncludeFlags(std::string& languageFlags,
  248. std::string const& language,
  249. std::string const& config)
  250. {
  251. std::vector<std::string> includes;
  252. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  253. language, config);
  254. // Add include directory flags.
  255. std::string includeFlags = this->LocalGenerator->GetIncludeFlags(
  256. includes, this->GeneratorTarget, language, config, false);
  257. if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
  258. std::replace(includeFlags.begin(), includeFlags.end(), '\\', '/');
  259. }
  260. this->LocalGenerator->AppendFlags(languageFlags, includeFlags);
  261. }
  262. // TODO: Refactor with
  263. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
  264. std::string cmNinjaTargetGenerator::ComputeDefines(cmSourceFile const* source,
  265. std::string const& language,
  266. std::string const& config)
  267. {
  268. std::set<std::string> defines;
  269. cmGeneratorExpressionInterpreter genexInterpreter(
  270. this->LocalGenerator, config, this->GeneratorTarget, language);
  271. // Seriously??
  272. if (this->GetGlobalGenerator()->IsMultiConfig()) {
  273. defines.insert(cmStrCat("CMAKE_INTDIR=\"", config, '"'));
  274. }
  275. std::string const COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
  276. if (cmValue compile_defs = source->GetProperty(COMPILE_DEFINITIONS)) {
  277. this->LocalGenerator->AppendDefines(
  278. defines, genexInterpreter.Evaluate(*compile_defs, COMPILE_DEFINITIONS));
  279. }
  280. std::string defPropName =
  281. cmStrCat("COMPILE_DEFINITIONS_", cmSystemTools::UpperCase(config));
  282. if (cmValue config_compile_defs = source->GetProperty(defPropName)) {
  283. this->LocalGenerator->AppendDefines(
  284. defines,
  285. genexInterpreter.Evaluate(*config_compile_defs, COMPILE_DEFINITIONS));
  286. }
  287. std::string definesString = this->GetDefines(language, config);
  288. this->LocalGenerator->JoinDefines(defines, definesString, language);
  289. return definesString;
  290. }
  291. std::string cmNinjaTargetGenerator::ComputeIncludes(
  292. cmSourceFile const* source, std::string const& language,
  293. std::string const& config)
  294. {
  295. std::vector<std::string> includes;
  296. cmGeneratorExpressionInterpreter genexInterpreter(
  297. this->LocalGenerator, config, this->GeneratorTarget, language);
  298. std::string const INCLUDE_DIRECTORIES("INCLUDE_DIRECTORIES");
  299. if (cmValue cincludes = source->GetProperty(INCLUDE_DIRECTORIES)) {
  300. this->LocalGenerator->AppendIncludeDirectories(
  301. includes, genexInterpreter.Evaluate(*cincludes, INCLUDE_DIRECTORIES),
  302. *source);
  303. }
  304. std::string includesString = this->LocalGenerator->GetIncludeFlags(
  305. includes, this->GeneratorTarget, language, config, false);
  306. this->LocalGenerator->AppendFlags(includesString,
  307. this->GetIncludes(language, config));
  308. return includesString;
  309. }
  310. cmNinjaDeps cmNinjaTargetGenerator::ComputeLinkDeps(
  311. std::string const& linkLanguage, std::string const& config,
  312. bool ignoreType) const
  313. {
  314. // Static libraries never depend on other targets for linking.
  315. if (!ignoreType &&
  316. (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  317. this->GeneratorTarget->GetType() == cmStateEnums::OBJECT_LIBRARY)) {
  318. return cmNinjaDeps();
  319. }
  320. cmComputeLinkInformation* cli =
  321. this->GeneratorTarget->GetLinkInformation(config);
  322. if (!cli) {
  323. return cmNinjaDeps();
  324. }
  325. std::vector<std::string> const& deps = cli->GetDepends();
  326. cmNinjaDeps result(deps.size());
  327. std::transform(deps.begin(), deps.end(), result.begin(),
  328. this->MapToNinjaPath());
  329. // Add a dependency on the link definitions file, if any.
  330. if (cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  331. this->GeneratorTarget->GetModuleDefinitionInfo(config)) {
  332. for (cmSourceFile const* src : mdi->Sources) {
  333. result.push_back(this->ConvertToNinjaPath(src->GetFullPath()));
  334. }
  335. }
  336. // Add a dependency on user-specified manifest files, if any.
  337. std::vector<cmSourceFile const*> manifest_srcs;
  338. this->GeneratorTarget->GetManifests(manifest_srcs, config);
  339. for (cmSourceFile const* manifest_src : manifest_srcs) {
  340. result.push_back(this->ConvertToNinjaPath(manifest_src->GetFullPath()));
  341. }
  342. // Add user-specified dependencies.
  343. std::vector<std::string> linkDeps;
  344. this->GeneratorTarget->GetLinkDepends(linkDeps, config, linkLanguage);
  345. std::transform(linkDeps.begin(), linkDeps.end(), std::back_inserter(result),
  346. this->MapToNinjaPath());
  347. return result;
  348. }
  349. std::string cmNinjaTargetGenerator::GetCompiledSourceNinjaPath(
  350. cmSourceFile const* source) const
  351. {
  352. // Pass source files to the compiler by absolute path.
  353. return this->ConvertToNinjaAbsPath(source->GetFullPath());
  354. }
  355. std::string cmNinjaTargetGenerator::GetObjectFileDir(
  356. std::string const& config) const
  357. {
  358. return this->LocalGenerator->MaybeRelativeToTopBinDir(
  359. cmStrCat(this->GeneratorTarget->GetSupportDirectory(),
  360. this->GetGlobalGenerator()->GetConfigDirectory(config)));
  361. }
  362. std::string cmNinjaTargetGenerator::GetObjectFilePath(
  363. cmSourceFile const* source, std::string const& config) const
  364. {
  365. std::string const& objectName = this->GeneratorTarget->GetObjectName(source);
  366. return cmStrCat(this->GetObjectFileDir(config), '/', objectName);
  367. }
  368. std::string cmNinjaTargetGenerator::GetBmiFilePath(
  369. cmSourceFile const* source, std::string const& config) const
  370. {
  371. auto& importedConfigInfo = this->Configs.at(config).ImportedCxxModules;
  372. if (!importedConfigInfo.Initialized()) {
  373. std::string configUpper = cmSystemTools::UpperCase(config);
  374. std::string propName = cmStrCat("IMPORTED_CXX_MODULES_", configUpper);
  375. auto value = this->GeneratorTarget->GetSafeProperty(propName);
  376. importedConfigInfo.Initialize(value);
  377. }
  378. std::string bmiName =
  379. importedConfigInfo.BmiNameForSource(source->GetFullPath());
  380. return cmStrCat(this->GetObjectFileDir(config), '/', bmiName);
  381. }
  382. std::string cmNinjaTargetGenerator::GetClangTidyReplacementsFilePath(
  383. std::string const& directory, cmSourceFile const& source,
  384. std::string const& config) const
  385. {
  386. auto const& objectName = this->GeneratorTarget->GetObjectName(&source);
  387. return cmStrCat(directory, '/',
  388. this->LocalGenerator->CreateSafeObjectFileName(
  389. this->GetObjectFileDir(config)),
  390. '/', objectName, ".yaml");
  391. }
  392. std::string cmNinjaTargetGenerator::GetPreprocessedFilePath(
  393. cmSourceFile const* source, std::string const& config) const
  394. {
  395. // Choose an extension to compile already-preprocessed source.
  396. std::string ppExt = source->GetExtension();
  397. if (cmHasLiteralPrefix(ppExt, "F")) {
  398. // Some Fortran compilers automatically enable preprocessing for
  399. // upper-case extensions. Since the source is already preprocessed,
  400. // use a lower-case extension.
  401. ppExt = cmSystemTools::LowerCase(ppExt);
  402. }
  403. if (ppExt == "fpp") {
  404. // Some Fortran compilers automatically enable preprocessing for
  405. // the ".fpp" extension. Since the source is already preprocessed,
  406. // use the ".f" extension.
  407. ppExt = "f";
  408. }
  409. // Take the object file name and replace the extension.
  410. std::string const& objName = this->GeneratorTarget->GetObjectName(source);
  411. std::string const& objExt =
  412. this->GetGlobalGenerator()->GetLanguageOutputExtension(*source);
  413. assert(objName.size() >= objExt.size());
  414. std::string const ppName =
  415. cmStrCat(objName.substr(0, objName.size() - objExt.size()), "-pp.", ppExt);
  416. return cmStrCat(this->GetObjectFileDir(config), '/', ppName);
  417. }
  418. std::string cmNinjaTargetGenerator::GetDyndepFilePath(
  419. std::string const& lang, std::string const& config) const
  420. {
  421. return cmStrCat(this->GetObjectFileDir(config), '/', lang, ".dd");
  422. }
  423. std::string cmNinjaTargetGenerator::GetTargetDependInfoPath(
  424. std::string const& lang, std::string const& config) const
  425. {
  426. return cmStrCat(this->GetObjectFileDir(config), '/', lang,
  427. "DependInfo.json");
  428. }
  429. std::string cmNinjaTargetGenerator::GetTargetOutputDir(
  430. std::string const& config) const
  431. {
  432. std::string dir = this->GeneratorTarget->GetDirectory(config);
  433. return this->ConvertToNinjaPath(dir);
  434. }
  435. std::string cmNinjaTargetGenerator::GetTargetFilePath(
  436. std::string const& name, std::string const& config) const
  437. {
  438. std::string path = this->GetTargetOutputDir(config);
  439. if (path.empty() || path == ".") {
  440. return name;
  441. }
  442. path += cmStrCat('/', name);
  443. return path;
  444. }
  445. std::string cmNinjaTargetGenerator::GetTargetName() const
  446. {
  447. return this->GeneratorTarget->GetName();
  448. }
  449. bool cmNinjaTargetGenerator::SetMsvcTargetPdbVariable(
  450. cmNinjaVars& vars, std::string const& config) const
  451. {
  452. cmMakefile* mf = this->GetMakefile();
  453. if (mf->GetDefinition("MSVC_C_ARCHITECTURE_ID") ||
  454. mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID") ||
  455. mf->GetDefinition("MSVC_CUDA_ARCHITECTURE_ID")) {
  456. std::string pdbPath;
  457. std::string compilePdbPath = this->ComputeTargetCompilePDB(config);
  458. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE ||
  459. this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  460. this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  461. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  462. pdbPath = cmStrCat(this->GeneratorTarget->GetPDBDirectory(config), '/',
  463. this->GeneratorTarget->GetPDBName(config));
  464. }
  465. vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  466. this->ConvertToNinjaPath(pdbPath), cmOutputConverter::SHELL);
  467. vars["TARGET_COMPILE_PDB"] =
  468. this->GetLocalGenerator()->ConvertToOutputFormat(
  469. this->ConvertToNinjaPath(compilePdbPath), cmOutputConverter::SHELL);
  470. this->EnsureParentDirectoryExists(pdbPath);
  471. this->EnsureParentDirectoryExists(compilePdbPath);
  472. return true;
  473. }
  474. return false;
  475. }
  476. void cmNinjaTargetGenerator::WriteLanguageRules(std::string const& language,
  477. std::string const& config)
  478. {
  479. #ifdef NINJA_GEN_VERBOSE_FILES
  480. this->GetRulesFileStream() << "# Rules for language " << language << "\n\n";
  481. #endif
  482. this->WriteCompileRule(language, config);
  483. }
  484. namespace {
  485. // Create the command to run the dependency scanner
  486. std::string GetScanCommand(
  487. cm::string_view cmakeCmd, cm::string_view tdi, cm::string_view lang,
  488. cm::string_view srcFile, cm::string_view ddiFile,
  489. cm::optional<cm::string_view> srcOrigFile = cm::nullopt)
  490. {
  491. return cmStrCat(cmakeCmd, " -E cmake_ninja_depends --tdi=", tdi,
  492. " --lang=", lang, " --src=", srcFile,
  493. " --out=$out"
  494. " --dep=$DEP_FILE --obj=$OBJ_FILE --ddi=",
  495. ddiFile,
  496. srcOrigFile ? cmStrCat(" --src-orig=", *srcOrigFile) : "");
  497. }
  498. // Helper function to create dependency scanning rule that may or may
  499. // not perform explicit preprocessing too.
  500. cmNinjaRule GetScanRule(
  501. std::string const& ruleName, std::string const& ppFileName,
  502. std::string const& deptype,
  503. cmRulePlaceholderExpander::RuleVariables const& vars,
  504. std::string const& responseFlag, std::string const& flags,
  505. cmRulePlaceholderExpander* const rulePlaceholderExpander,
  506. cmLocalNinjaGenerator* generator, std::vector<std::string> scanCmds,
  507. std::string const& outputConfig)
  508. {
  509. cmNinjaRule rule(ruleName);
  510. // Scanning always uses a depfile for preprocessor dependencies.
  511. if (deptype == "msvc"_s) {
  512. rule.DepType = deptype;
  513. rule.DepFile.clear();
  514. } else {
  515. rule.DepType.clear(); // no deps= for multiple outputs
  516. rule.DepFile = "$DEP_FILE";
  517. }
  518. cmRulePlaceholderExpander::RuleVariables scanVars;
  519. scanVars.CMTargetName = vars.CMTargetName;
  520. scanVars.CMTargetType = vars.CMTargetType;
  521. scanVars.CMTargetLabels = vars.CMTargetLabels;
  522. scanVars.Language = vars.Language;
  523. scanVars.Object = "$OBJ_FILE";
  524. scanVars.PreprocessedSource = ppFileName.c_str();
  525. scanVars.DynDepFile = "$DYNDEP_INTERMEDIATE_FILE";
  526. scanVars.DependencyFile = rule.DepFile.c_str();
  527. scanVars.DependencyTarget = "$out";
  528. scanVars.Config = vars.Config;
  529. // Scanning needs the same preprocessor settings as direct compilation would.
  530. scanVars.Source = vars.Source;
  531. scanVars.Defines = vars.Defines;
  532. scanVars.Includes = vars.Includes;
  533. // Scanning needs the compilation flags too.
  534. std::string scanFlags = flags;
  535. // If using a response file, move defines, includes, and flags into it.
  536. if (!responseFlag.empty()) {
  537. rule.RspFile = "$RSP_FILE";
  538. rule.RspContent =
  539. cmStrCat(' ', scanVars.Defines, ' ', scanVars.Includes, ' ', scanFlags);
  540. scanFlags = cmStrCat(responseFlag, rule.RspFile);
  541. scanVars.Defines = "";
  542. scanVars.Includes = "";
  543. }
  544. scanVars.Flags = scanFlags.c_str();
  545. // Rule for scanning a source file.
  546. for (std::string& scanCmd : scanCmds) {
  547. rulePlaceholderExpander->ExpandRuleVariables(generator, scanCmd, scanVars);
  548. }
  549. rule.Command =
  550. generator->BuildCommandLine(scanCmds, outputConfig, outputConfig);
  551. return rule;
  552. }
  553. }
  554. void cmNinjaTargetGenerator::WriteCompileRule(std::string const& lang,
  555. std::string const& config)
  556. {
  557. // For some cases we scan to dynamically discover dependencies.
  558. bool const needDyndep = this->GetGeneratorTarget()->NeedDyndep(lang, config);
  559. if (needDyndep) {
  560. this->WriteCompileRule(lang, config, WithScanning::Yes);
  561. }
  562. this->WriteCompileRule(lang, config, WithScanning::No);
  563. }
  564. void cmNinjaTargetGenerator::WriteCompileRule(std::string const& lang,
  565. std::string const& config,
  566. WithScanning withScanning)
  567. {
  568. cmRulePlaceholderExpander::RuleVariables vars;
  569. vars.CMTargetName = this->GetGeneratorTarget()->GetName().c_str();
  570. vars.CMTargetType =
  571. cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType()).c_str();
  572. vars.CMTargetLabels =
  573. this->GetGeneratorTarget()->GetTargetLabelsString().c_str();
  574. vars.Language = lang.c_str();
  575. vars.Source = "$in";
  576. vars.Object = "$out";
  577. vars.Defines = "$DEFINES";
  578. vars.Includes = "$INCLUDES";
  579. vars.TargetPDB = "$TARGET_PDB";
  580. vars.TargetCompilePDB = "$TARGET_COMPILE_PDB";
  581. vars.ObjectDir = "$OBJECT_DIR";
  582. vars.ObjectFileDir = "$OBJECT_FILE_DIR";
  583. vars.CudaCompileMode = "$CUDA_COMPILE_MODE";
  584. vars.ISPCHeader = "$ISPC_HEADER_FILE";
  585. vars.Config = "$CONFIG";
  586. cmMakefile* mf = this->GetMakefile();
  587. // For some cases we scan to dynamically discover dependencies.
  588. bool const compilationPreprocesses = !this->NeedExplicitPreprocessing(lang);
  589. std::string flags = "$FLAGS";
  590. std::string responseFlag;
  591. bool const lang_supports_response = lang != "RC";
  592. if (lang_supports_response && this->ForceResponseFile()) {
  593. std::string const responseFlagVar =
  594. cmStrCat("CMAKE_", lang, "_RESPONSE_FILE_FLAG");
  595. responseFlag = this->Makefile->GetSafeDefinition(responseFlagVar);
  596. if (responseFlag.empty() && lang != "CUDA") {
  597. responseFlag = "@";
  598. }
  599. }
  600. std::string const modmapFormatVar =
  601. cmStrCat("CMAKE_", lang, "_MODULE_MAP_FORMAT");
  602. std::string const modmapFormat =
  603. this->Makefile->GetSafeDefinition(modmapFormatVar);
  604. auto rulePlaceholderExpander =
  605. this->GetLocalGenerator()->CreateRulePlaceholderExpander();
  606. std::string const tdi = this->GetLocalGenerator()->ConvertToOutputFormat(
  607. this->ConvertToNinjaPath(this->GetTargetDependInfoPath(lang, config)),
  608. cmLocalGenerator::SHELL);
  609. std::string launcher;
  610. std::string val = this->GetLocalGenerator()->GetRuleLauncher(
  611. this->GetGeneratorTarget(), "RULE_LAUNCH_COMPILE", config);
  612. if (cmNonempty(val)) {
  613. launcher = cmStrCat(val, ' ');
  614. }
  615. std::string const cmakeCmd =
  616. this->GetLocalGenerator()->ConvertToOutputFormat(
  617. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  618. if (withScanning == WithScanning::Yes) {
  619. auto const& scanDepType = this->GetMakefile()->GetSafeDefinition(
  620. cmStrCat("CMAKE_", lang, "_SCANDEP_DEPFILE_FORMAT"));
  621. // Rule to scan dependencies of sources that need preprocessing.
  622. {
  623. cmList scanCommands;
  624. std::string scanRuleName;
  625. std::string ppFileName;
  626. if (compilationPreprocesses) {
  627. scanRuleName = this->LanguageScanRule(lang, config);
  628. ppFileName = "$PREPROCESSED_OUTPUT_FILE";
  629. std::string const& scanCommand = mf->GetRequiredDefinition(
  630. cmStrCat("CMAKE_", lang, "_SCANDEP_SOURCE"));
  631. scanCommands.assign(scanCommand);
  632. for (auto& i : scanCommands) {
  633. i = cmStrCat(launcher, i);
  634. }
  635. } else {
  636. scanRuleName = this->LanguagePreprocessAndScanRule(lang, config);
  637. ppFileName = "$out";
  638. std::string const& ppCommand = mf->GetRequiredDefinition(
  639. cmStrCat("CMAKE_", lang, "_PREPROCESS_SOURCE"));
  640. scanCommands.assign(ppCommand);
  641. for (auto& i : scanCommands) {
  642. i = cmStrCat(launcher, i);
  643. }
  644. scanCommands.emplace_back(GetScanCommand(
  645. cmakeCmd, tdi, lang, "$out", "$DYNDEP_INTERMEDIATE_FILE", "$in"));
  646. }
  647. auto scanRule = GetScanRule(
  648. scanRuleName, ppFileName, scanDepType, vars, responseFlag, flags,
  649. rulePlaceholderExpander.get(), this->GetLocalGenerator(),
  650. std::move(scanCommands), config);
  651. scanRule.Comment =
  652. cmStrCat("Rule for generating ", lang, " dependencies.");
  653. if (compilationPreprocesses) {
  654. scanRule.Description =
  655. cmStrCat("Scanning $in for ", lang, " dependencies");
  656. } else {
  657. scanRule.Description =
  658. cmStrCat("Building ", lang, " preprocessed $out");
  659. }
  660. this->GetGlobalGenerator()->AddRule(scanRule);
  661. }
  662. if (!compilationPreprocesses) {
  663. // Compilation will not preprocess, so it does not need the defines
  664. // unless the compiler wants them for some other purpose.
  665. if (!this->CompileWithDefines(lang)) {
  666. vars.Defines = "";
  667. }
  668. // Rule to scan dependencies of sources that do not need preprocessing.
  669. std::string const& scanRuleName = this->LanguageScanRule(lang, config);
  670. std::vector<std::string> scanCommands;
  671. scanCommands.emplace_back(
  672. GetScanCommand(cmakeCmd, tdi, lang, "$in", "$out"));
  673. auto scanRule =
  674. GetScanRule(scanRuleName, "", scanDepType, vars, "", flags,
  675. rulePlaceholderExpander.get(), this->GetLocalGenerator(),
  676. std::move(scanCommands), config);
  677. // Write the rule for generating dependencies for the given language.
  678. scanRule.Comment = cmStrCat("Rule for generating ", lang,
  679. " dependencies on non-preprocessed files.");
  680. scanRule.Description =
  681. cmStrCat("Generating ", lang, " dependencies for $in");
  682. this->GetGlobalGenerator()->AddRule(scanRule);
  683. }
  684. // Write the rule for ninja dyndep file generation.
  685. cmNinjaRule rule(this->LanguageDyndepRule(lang, config));
  686. // Command line length is almost always limited -> use response file for
  687. // dyndep rules
  688. rule.RspFile = "$out.rsp";
  689. rule.RspContent = "$in";
  690. // Ninja's collator writes all outputs using `cmGeneratedFileStream`, so
  691. // they are only updated if contents actually change. Avoid running
  692. // dependent jobs if the contents don't change by telling `ninja` to check
  693. // the timestamp again.
  694. rule.Restat = "1";
  695. // Run CMake dependency scanner on the source file (using the preprocessed
  696. // source if that was performed).
  697. std::string ddModmapArg;
  698. if (!modmapFormat.empty()) {
  699. ddModmapArg += cmStrCat(" --modmapfmt=", modmapFormat);
  700. }
  701. {
  702. std::vector<std::string> ddCmds;
  703. {
  704. std::string ccmd = cmStrCat(
  705. cmakeCmd, " -E cmake_ninja_dyndep --tdi=", tdi, " --lang=", lang,
  706. ddModmapArg, " --dd=$out @", rule.RspFile);
  707. ddCmds.emplace_back(std::move(ccmd));
  708. }
  709. rule.Command =
  710. this->GetLocalGenerator()->BuildCommandLine(ddCmds, config, config);
  711. }
  712. rule.Comment =
  713. cmStrCat("Rule to generate ninja dyndep files for ", lang, '.');
  714. rule.Description = cmStrCat("Generating ", lang, " dyndep file $out");
  715. this->GetGlobalGenerator()->AddRule(rule);
  716. }
  717. cmNinjaRule rule(this->LanguageCompilerRule(lang, config, withScanning));
  718. // If using a response file, move defines, includes, and flags into it.
  719. if (!responseFlag.empty()) {
  720. rule.RspFile = "$RSP_FILE";
  721. rule.RspContent =
  722. cmStrCat(' ', vars.Defines, ' ', vars.Includes, ' ', flags);
  723. flags = cmStrCat(responseFlag, rule.RspFile);
  724. vars.Defines = "";
  725. vars.Includes = "";
  726. // Swift consumes all source files in a module at once, which reaches
  727. // command line length limits pretty quickly. Inject source files into the
  728. // response file in this case as well.
  729. if (lang == "Swift") {
  730. rule.RspContent = cmStrCat(rule.RspContent, ' ', vars.Source);
  731. vars.Source = "";
  732. }
  733. }
  734. // Tell ninja dependency format so all deps can be loaded into a database
  735. std::string cldeps;
  736. if (!compilationPreprocesses) {
  737. // The compiler will not do preprocessing, so it has no such dependencies.
  738. } else if (mf->IsOn(cmStrCat("CMAKE_NINJA_CMCLDEPS_", lang))) {
  739. // For the MS resource compiler we need cmcldeps, but skip dependencies
  740. // for source-file try_compile cases because they are always fresh.
  741. if (!mf->GetIsSourceFileTryCompile()) {
  742. rule.DepType = "gcc";
  743. rule.DepFile = "$DEP_FILE";
  744. cmValue d = mf->GetDefinition("CMAKE_C_COMPILER");
  745. std::string const cl =
  746. d ? *d : mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
  747. std::string cmcldepsPath;
  748. cmSystemTools::GetShortPath(cmSystemTools::GetCMClDepsCommand(),
  749. cmcldepsPath);
  750. cldeps = cmStrCat(cmcldepsPath, ' ', lang, ' ', vars.Source,
  751. " $DEP_FILE $out \"",
  752. mf->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX"),
  753. "\" \"", cl, "\" ");
  754. }
  755. } else {
  756. auto const& depType = this->GetMakefile()->GetSafeDefinition(
  757. cmStrCat("CMAKE_", lang, "_DEPFILE_FORMAT"));
  758. if (depType == "msvc"_s) {
  759. rule.DepType = "msvc";
  760. rule.DepFile.clear();
  761. } else {
  762. rule.DepType = "gcc";
  763. rule.DepFile = "$DEP_FILE";
  764. }
  765. vars.DependencyFile = rule.DepFile.c_str();
  766. vars.DependencyTarget = "$out";
  767. std::string const flagsName = cmStrCat("CMAKE_DEPFILE_FLAGS_", lang);
  768. std::string depfileFlags = mf->GetSafeDefinition(flagsName);
  769. if (!depfileFlags.empty()) {
  770. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(),
  771. depfileFlags, vars);
  772. flags += cmStrCat(' ', depfileFlags);
  773. }
  774. }
  775. if (withScanning == WithScanning::Yes && !modmapFormat.empty()) {
  776. std::string modmapFlags =
  777. mf->GetRequiredDefinition(cmStrCat("CMAKE_", lang, "_MODULE_MAP_FLAG"));
  778. cmSystemTools::ReplaceString(modmapFlags, "<MODULE_MAP_FILE>",
  779. "$DYNDEP_MODULE_MAP_FILE");
  780. flags += cmStrCat(' ', modmapFlags);
  781. }
  782. vars.Flags = flags.c_str();
  783. vars.DependencyFile = rule.DepFile.c_str();
  784. std::string cudaCompileMode;
  785. if (lang == "CUDA") {
  786. if (this->GeneratorTarget->GetPropertyAsBool(
  787. "CUDA_SEPARABLE_COMPILATION")) {
  788. std::string const& rdcFlag =
  789. this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_RDC_FLAG");
  790. cudaCompileMode = cmStrCat(cudaCompileMode, rdcFlag, ' ');
  791. }
  792. static std::array<cm::string_view, 4> const compileModes{
  793. { "PTX"_s, "CUBIN"_s, "FATBIN"_s, "OPTIX"_s }
  794. };
  795. bool useNormalCompileMode = true;
  796. for (cm::string_view mode : compileModes) {
  797. auto propName = cmStrCat("CUDA_", mode, "_COMPILATION");
  798. auto defName = cmStrCat("_CMAKE_CUDA_", mode, "_FLAG");
  799. if (this->GeneratorTarget->GetPropertyAsBool(propName)) {
  800. std::string const& flag =
  801. this->Makefile->GetRequiredDefinition(defName);
  802. cudaCompileMode = cmStrCat(cudaCompileMode, flag);
  803. useNormalCompileMode = false;
  804. break;
  805. }
  806. }
  807. if (useNormalCompileMode) {
  808. std::string const& wholeFlag =
  809. this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_WHOLE_FLAG");
  810. cudaCompileMode = cmStrCat(cudaCompileMode, wholeFlag);
  811. }
  812. vars.CudaCompileMode = cudaCompileMode.c_str();
  813. }
  814. // Rule for compiling object file.
  815. std::string const cmdVar = cmStrCat("CMAKE_", lang, "_COMPILE_OBJECT");
  816. std::string const& compileCmd = mf->GetRequiredDefinition(cmdVar);
  817. cmList compileCmds(compileCmd);
  818. if (!compileCmds.empty()) {
  819. compileCmds.front().insert(0, "${CODE_CHECK}");
  820. compileCmds.front().insert(0, "${LAUNCHER}");
  821. }
  822. if (!compileCmds.empty()) {
  823. compileCmds.front().insert(0, cldeps);
  824. }
  825. auto const& extraCommands = this->GetMakefile()->GetSafeDefinition(
  826. cmStrCat("CMAKE_", lang, "_DEPENDS_EXTRA_COMMANDS"));
  827. if (!extraCommands.empty()) {
  828. compileCmds.append(extraCommands);
  829. }
  830. for (auto& i : compileCmds) {
  831. i = cmStrCat(launcher, i);
  832. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(), i,
  833. vars);
  834. }
  835. rule.Command =
  836. this->GetLocalGenerator()->BuildCommandLine(compileCmds, config, config);
  837. // Write the rule for compiling file of the given language.
  838. rule.Comment = cmStrCat("Rule for compiling ", lang, " files.");
  839. rule.Description = cmStrCat("Building ", lang, " object $out");
  840. this->GetGlobalGenerator()->AddRule(rule);
  841. }
  842. void cmNinjaTargetGenerator::WriteObjectBuildStatements(
  843. std::string const& config, std::string const& fileConfig,
  844. bool firstForConfig)
  845. {
  846. this->GeneratorTarget->CheckCxxModuleStatus(config);
  847. // Write comments.
  848. cmGlobalNinjaGenerator::WriteDivider(this->GetImplFileStream(fileConfig));
  849. this->GetImplFileStream(fileConfig)
  850. << "# Object build statements for "
  851. << cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType())
  852. << " target " << this->GetTargetName() << "\n\n";
  853. std::vector<cmCustomCommand const*> customCommands;
  854. {
  855. std::vector<cmSourceFile const*> customCommandSources;
  856. this->GeneratorTarget->GetCustomCommands(customCommandSources, config);
  857. for (cmSourceFile const* sf : customCommandSources) {
  858. cmCustomCommand const* cc = sf->GetCustomCommand();
  859. this->GetLocalGenerator()->AddCustomCommandTarget(
  860. cc, this->GetGeneratorTarget());
  861. customCommands.push_back(cc);
  862. }
  863. }
  864. {
  865. std::vector<cmSourceFile const*> headerSources;
  866. this->GeneratorTarget->GetHeaderSources(headerSources, config);
  867. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  868. headerSources, this->Configs[fileConfig].MacOSXContentGenerator.get(),
  869. config);
  870. }
  871. {
  872. std::vector<cmSourceFile const*> extraSources;
  873. this->GeneratorTarget->GetExtraSources(extraSources, config);
  874. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  875. extraSources, this->Configs[fileConfig].MacOSXContentGenerator.get(),
  876. config);
  877. }
  878. if (firstForConfig) {
  879. cmValue pchExtension =
  880. this->GetMakefile()->GetDefinition("CMAKE_PCH_EXTENSION");
  881. std::vector<cmSourceFile const*> externalObjects;
  882. this->GeneratorTarget->GetExternalObjects(externalObjects, config);
  883. for (cmSourceFile const* sf : externalObjects) {
  884. auto objectFileName = this->GetGlobalGenerator()->ExpandCFGIntDir(
  885. this->ConvertToNinjaPath(sf->GetFullPath()), config);
  886. if (!cmHasSuffix(objectFileName, pchExtension)) {
  887. this->Configs[config].Objects.push_back(objectFileName);
  888. }
  889. }
  890. }
  891. if (!this->GetGlobalGenerator()->SupportsCWDDepend()) {
  892. // Ensure that the object directory exists. If there are no objects in the
  893. // target (e.g., an empty `OBJECT` library), the directory is still listed
  894. // as an order-only depends in the build files. Alternate `ninja`
  895. // implementations may not allow this (such as `samu`). See #25526.
  896. auto const objectDir = this->GetObjectFileDir(config);
  897. this->EnsureDirectoryExists(objectDir);
  898. }
  899. {
  900. cmNinjaBuild build("phony");
  901. build.Comment =
  902. cmStrCat("Order-only phony target for ", this->GetTargetName());
  903. build.Outputs.push_back(this->OrderDependsTargetForTarget(config));
  904. // Gather order-only dependencies on custom command outputs.
  905. std::vector<std::string> ccouts;
  906. std::vector<std::string> ccouts_private;
  907. bool usePrivateGeneratedSources = false;
  908. if (this->GeneratorTarget->Target->HasFileSets()) {
  909. switch (this->GetGeneratorTarget()->GetPolicyStatusCMP0154()) {
  910. case cmPolicies::WARN:
  911. case cmPolicies::OLD:
  912. break;
  913. case cmPolicies::NEW:
  914. usePrivateGeneratedSources = true;
  915. break;
  916. }
  917. }
  918. for (cmCustomCommand const* cc : customCommands) {
  919. cmCustomCommandGenerator ccg(*cc, config, this->GetLocalGenerator());
  920. std::vector<std::string> const& ccoutputs = ccg.GetOutputs();
  921. std::vector<std::string> const& ccbyproducts = ccg.GetByproducts();
  922. auto const nPreviousOutputs = ccouts.size();
  923. ccouts.insert(ccouts.end(), ccoutputs.begin(), ccoutputs.end());
  924. ccouts.insert(ccouts.end(), ccbyproducts.begin(), ccbyproducts.end());
  925. if (usePrivateGeneratedSources) {
  926. auto it = ccouts.begin();
  927. // Skip over outputs that were already detected.
  928. std::advance(it, nPreviousOutputs);
  929. while (it != ccouts.end()) {
  930. cmFileSet const* fileset =
  931. this->GeneratorTarget->GetFileSetForSource(
  932. config, this->Makefile->GetOrCreateGeneratedSource(*it));
  933. bool isVisible = fileset &&
  934. cmFileSetVisibilityIsForInterface(fileset->GetVisibility());
  935. bool isIncludeable =
  936. !fileset || cmFileSetTypeCanBeIncluded(fileset->GetType());
  937. if (fileset && isVisible && isIncludeable) {
  938. ++it;
  939. continue;
  940. }
  941. if (!fileset || isIncludeable) {
  942. ccouts_private.push_back(*it);
  943. }
  944. it = ccouts.erase(it);
  945. }
  946. }
  947. }
  948. cmNinjaDeps& orderOnlyDeps = build.OrderOnlyDeps;
  949. this->GetLocalGenerator()->AppendTargetDepends(
  950. this->GeneratorTarget, orderOnlyDeps, config, fileConfig,
  951. DependOnTargetOrdering);
  952. // Add order-only dependencies on other files associated with the target.
  953. cm::append(orderOnlyDeps, this->Configs[config].ExtraFiles);
  954. // Add order-only dependencies on custom command outputs.
  955. std::transform(ccouts.begin(), ccouts.end(),
  956. std::back_inserter(orderOnlyDeps), this->MapToNinjaPath());
  957. std::sort(orderOnlyDeps.begin(), orderOnlyDeps.end());
  958. orderOnlyDeps.erase(
  959. std::unique(orderOnlyDeps.begin(), orderOnlyDeps.end()),
  960. orderOnlyDeps.end());
  961. // The phony target must depend on at least one input or ninja will explain
  962. // that "output ... of phony edge with no inputs doesn't exist" and
  963. // consider the phony output "dirty".
  964. if (orderOnlyDeps.empty()) {
  965. std::string tgtDir;
  966. if (this->GetGlobalGenerator()->SupportsCWDDepend()) {
  967. tgtDir = ".";
  968. } else {
  969. // Any path that always exists will work here.
  970. tgtDir = this->GetObjectFileDir(config);
  971. }
  972. orderOnlyDeps.push_back(this->ConvertToNinjaPath(tgtDir));
  973. }
  974. this->GetGlobalGenerator()->WriteBuild(this->GetImplFileStream(fileConfig),
  975. build);
  976. // Add order-only dependencies on custom command outputs that are
  977. // private to this target.
  978. this->HasPrivateGeneratedSources = !ccouts_private.empty();
  979. if (this->HasPrivateGeneratedSources) {
  980. cmNinjaBuild buildPrivate("phony");
  981. cmNinjaDeps& orderOnlyDepsPrivate = buildPrivate.OrderOnlyDeps;
  982. orderOnlyDepsPrivate.push_back(
  983. this->OrderDependsTargetForTarget(config));
  984. buildPrivate.Outputs.push_back(
  985. this->OrderDependsTargetForTargetPrivate(config));
  986. std::transform(ccouts_private.begin(), ccouts_private.end(),
  987. std::back_inserter(orderOnlyDepsPrivate),
  988. this->MapToNinjaPath());
  989. std::sort(orderOnlyDepsPrivate.begin(), orderOnlyDepsPrivate.end());
  990. orderOnlyDepsPrivate.erase(
  991. std::unique(orderOnlyDepsPrivate.begin(), orderOnlyDepsPrivate.end()),
  992. orderOnlyDepsPrivate.end());
  993. this->GetGlobalGenerator()->WriteBuild(
  994. this->GetImplFileStream(fileConfig), buildPrivate);
  995. }
  996. }
  997. {
  998. std::vector<cmSourceFile const*> objectSources;
  999. this->GeneratorTarget->GetObjectSources(objectSources, config);
  1000. std::vector<cmSourceFile const*> swiftSources;
  1001. for (cmSourceFile const* sf : objectSources) {
  1002. if (this->GetLocalGenerator()->IsSplitSwiftBuild() &&
  1003. sf->GetLanguage() == "Swift") {
  1004. swiftSources.push_back(sf);
  1005. } else {
  1006. this->WriteObjectBuildStatement(sf, config, fileConfig,
  1007. firstForConfig);
  1008. }
  1009. }
  1010. WriteSwiftObjectBuildStatement(swiftSources, config, fileConfig,
  1011. firstForConfig);
  1012. }
  1013. {
  1014. std::vector<cmSourceFile const*> bmiOnlySources;
  1015. this->GeneratorTarget->GetCxxModuleSources(bmiOnlySources, config);
  1016. for (cmSourceFile const* sf : bmiOnlySources) {
  1017. this->WriteCxxModuleBmiBuildStatement(sf, config, fileConfig,
  1018. firstForConfig);
  1019. }
  1020. }
  1021. // Detect sources in `CXX_MODULES` which are not compiled.
  1022. {
  1023. std::vector<cmSourceFile*> sources;
  1024. this->GeneratorTarget->GetSourceFiles(sources, config);
  1025. for (cmSourceFile const* sf : sources) {
  1026. cmFileSet const* fs =
  1027. this->GeneratorTarget->GetFileSetForSource(config, sf);
  1028. if (!fs) {
  1029. continue;
  1030. }
  1031. if (fs->GetType() != "CXX_MODULES"_s) {
  1032. continue;
  1033. }
  1034. if (sf->GetLanguage().empty()) {
  1035. this->GeneratorTarget->Makefile->IssueMessage(
  1036. MessageType::FATAL_ERROR,
  1037. cmStrCat("Target \"", this->GeneratorTarget->GetName(),
  1038. "\" has source file\n ", sf->GetFullPath(),
  1039. "\nin a \"FILE_SET TYPE CXX_MODULES\" but it is not "
  1040. "scheduled for compilation."));
  1041. }
  1042. }
  1043. }
  1044. // Check if there are Fortran objects which need to participate in forwarding
  1045. // module requirements.
  1046. if (this->GeneratorTarget->HaveFortranSources(config) &&
  1047. !this->Configs[config].ScanningInfo.count("Fortran")) {
  1048. ScanningFiles files;
  1049. this->Configs[config].ScanningInfo["Fortran"].emplace_back(files);
  1050. this->WriteCompileRule("Fortran", config, WithScanning::Yes);
  1051. }
  1052. for (auto const& langScanningFiles : this->Configs[config].ScanningInfo) {
  1053. std::string const& language = langScanningFiles.first;
  1054. std::vector<ScanningFiles> const& scanningFiles = langScanningFiles.second;
  1055. cmNinjaBuild build(this->LanguageDyndepRule(language, config));
  1056. build.Outputs.push_back(this->GetDyndepFilePath(language, config));
  1057. build.ImplicitOuts.emplace_back(
  1058. cmStrCat(this->GetObjectFileDir(config), '/', language, "Modules.json"));
  1059. build.ImplicitDeps.emplace_back(
  1060. this->GetTargetDependInfoPath(language, config));
  1061. {
  1062. auto bdb_path =
  1063. this->GeneratorTarget->BuildDatabasePath(language, config);
  1064. if (!bdb_path.empty()) {
  1065. build.ImplicitOuts.emplace_back(this->ConvertToNinjaPath(bdb_path));
  1066. }
  1067. }
  1068. auto bdb_path = this->GeneratorTarget->BuildDatabasePath(language, config);
  1069. if (!bdb_path.empty()) {
  1070. auto db = cmBuildDatabase::ForTarget(this->GeneratorTarget, config);
  1071. auto mcdb_template_path = cmStrCat(bdb_path, ".in");
  1072. db.Write(mcdb_template_path);
  1073. build.ImplicitDeps.emplace_back(std::move(mcdb_template_path));
  1074. build.ImplicitOuts.emplace_back(std::move(bdb_path));
  1075. }
  1076. for (auto const& scanFiles : scanningFiles) {
  1077. if (!scanFiles.ScanningOutput.empty()) {
  1078. build.ExplicitDeps.push_back(scanFiles.ScanningOutput);
  1079. }
  1080. if (!scanFiles.ModuleMapFile.empty()) {
  1081. build.ImplicitOuts.push_back(scanFiles.ModuleMapFile);
  1082. }
  1083. }
  1084. this->WriteTargetDependInfo(language, config);
  1085. auto const linked_directories =
  1086. this->GetLinkedTargetDirectories(language, config);
  1087. for (std::string const& l : linked_directories.Direct) {
  1088. build.ImplicitDeps.emplace_back(
  1089. this->ConvertToNinjaPath(cmStrCat(l, '/', language, "Modules.json")));
  1090. }
  1091. for (std::string const& l : linked_directories.Forward) {
  1092. build.ImplicitDeps.emplace_back(
  1093. this->ConvertToNinjaPath(cmStrCat(l, '/', language, "Modules.json")));
  1094. }
  1095. this->GetGlobalGenerator()->WriteBuild(this->GetImplFileStream(fileConfig),
  1096. build);
  1097. }
  1098. this->GetImplFileStream(fileConfig) << '\n';
  1099. }
  1100. void cmNinjaTargetGenerator::GenerateSwiftOutputFileMap(
  1101. std::string const& config, std::string& flags)
  1102. {
  1103. if (this->Configs[config].SwiftOutputMap.empty()) {
  1104. return;
  1105. }
  1106. std::string const targetSwiftDepsPath = [this, config]() -> std::string {
  1107. cmGeneratorTarget const* target = this->GeneratorTarget;
  1108. if (cmValue name = target->GetProperty("Swift_DEPENDENCIES_FILE")) {
  1109. return *name;
  1110. }
  1111. return this->ConvertToNinjaPath(cmStrCat(target->GetSupportDirectory(),
  1112. '/', config, '/',
  1113. target->GetName(), ".swiftdeps"));
  1114. }();
  1115. std::string mapFilePath =
  1116. cmStrCat(this->GeneratorTarget->GetSupportDirectory(), '/', config,
  1117. "/"
  1118. "output-file-map.json");
  1119. // build the global target dependencies
  1120. // https://github.com/apple/swift/blob/master/docs/Driver.md#output-file-maps
  1121. Json::Value deps(Json::objectValue);
  1122. deps["swift-dependencies"] = targetSwiftDepsPath;
  1123. this->Configs[config].SwiftOutputMap[""] = deps;
  1124. cmGeneratedFileStream output(mapFilePath);
  1125. output << this->Configs[config].SwiftOutputMap;
  1126. // Add flag
  1127. this->LocalGenerator->AppendFlags(flags, "-output-file-map");
  1128. this->LocalGenerator->AppendFlags(
  1129. flags,
  1130. this->GetLocalGenerator()->ConvertToOutputFormat(
  1131. ConvertToNinjaPath(mapFilePath), cmOutputConverter::SHELL));
  1132. }
  1133. namespace {
  1134. cmNinjaBuild GetScanBuildStatement(std::string const& ruleName,
  1135. std::string const& ppFileName,
  1136. bool compilePP, bool compilePPWithDefines,
  1137. bool compilationPreprocesses,
  1138. cmNinjaBuild& objBuild, cmNinjaVars& vars,
  1139. std::string const& objectFileName,
  1140. cmNinjaTargetGenerator* tg)
  1141. {
  1142. cmNinjaBuild scanBuild(ruleName);
  1143. if (compilePP) {
  1144. // Move compilation dependencies to the scan/preprocessing build statement.
  1145. std::swap(scanBuild.ExplicitDeps, objBuild.ExplicitDeps);
  1146. std::swap(scanBuild.ImplicitDeps, objBuild.ImplicitDeps);
  1147. std::swap(scanBuild.OrderOnlyDeps, objBuild.OrderOnlyDeps);
  1148. std::swap(scanBuild.Variables["IN_ABS"], vars["IN_ABS"]);
  1149. // The actual compilation will now use the preprocessed source.
  1150. objBuild.ExplicitDeps.push_back(ppFileName);
  1151. } else {
  1152. // Copy compilation dependencies to the scan/preprocessing build statement.
  1153. scanBuild.ExplicitDeps = objBuild.ExplicitDeps;
  1154. scanBuild.ImplicitDeps = objBuild.ImplicitDeps;
  1155. scanBuild.OrderOnlyDeps = objBuild.OrderOnlyDeps;
  1156. scanBuild.Variables["IN_ABS"] = vars["IN_ABS"];
  1157. }
  1158. // Scanning and compilation generally use the same flags.
  1159. scanBuild.Variables["FLAGS"] = vars["FLAGS"];
  1160. if (compilePP && !compilePPWithDefines) {
  1161. // Move preprocessor definitions to the scan/preprocessor build statement.
  1162. std::swap(scanBuild.Variables["DEFINES"], vars["DEFINES"]);
  1163. } else {
  1164. // Copy preprocessor definitions to the scan/preprocessor build statement.
  1165. scanBuild.Variables["DEFINES"] = vars["DEFINES"];
  1166. }
  1167. // Copy include directories to the preprocessor build statement. The
  1168. // Fortran compilation build statement still needs them for the INCLUDE
  1169. // directive.
  1170. scanBuild.Variables["INCLUDES"] = vars["INCLUDES"];
  1171. // Tell dependency scanner the object file that will result from
  1172. // compiling the source.
  1173. scanBuild.Variables["OBJ_FILE"] = objectFileName;
  1174. // Tell dependency scanner where to store dyndep intermediate results.
  1175. std::string ddiFileName = cmStrCat(objectFileName, ".ddi");
  1176. scanBuild.Variables["DYNDEP_INTERMEDIATE_FILE"] = ddiFileName;
  1177. scanBuild.RspFile = cmStrCat(ddiFileName, ".rsp");
  1178. // Outputs of the scan/preprocessor build statement.
  1179. if (compilePP) {
  1180. scanBuild.Outputs.push_back(ppFileName);
  1181. scanBuild.ImplicitOuts.push_back(ddiFileName);
  1182. } else {
  1183. scanBuild.Outputs.push_back(ddiFileName);
  1184. scanBuild.Variables["PREPROCESSED_OUTPUT_FILE"] = ppFileName;
  1185. if (!compilationPreprocesses) {
  1186. // Compilation does not preprocess and we are not compiling an
  1187. // already-preprocessed source. Make compilation depend on the scan
  1188. // results to honor implicit dependencies discovered during scanning
  1189. // (such as Fortran INCLUDE directives).
  1190. objBuild.ImplicitDeps.emplace_back(ddiFileName);
  1191. }
  1192. }
  1193. // Scanning always provides a depfile for preprocessor dependencies. This
  1194. // variable is unused in `msvc`-deptype scanners.
  1195. tg->AddDepfileBinding(scanBuild.Variables,
  1196. cmStrCat(scanBuild.Outputs.front(), ".d"));
  1197. if (compilePP) {
  1198. // The actual compilation does not need a depfile because it
  1199. // depends on the already-preprocessed source.
  1200. tg->RemoveDepfileBinding(vars);
  1201. }
  1202. return scanBuild;
  1203. }
  1204. }
  1205. void cmNinjaTargetGenerator::WriteObjectBuildStatement(
  1206. cmSourceFile const* source, std::string const& config,
  1207. std::string const& fileConfig, bool firstForConfig)
  1208. {
  1209. std::string const language = source->GetLanguage();
  1210. std::string const sourceFilePath = this->GetCompiledSourceNinjaPath(source);
  1211. std::string const objectDir =
  1212. this->ConvertToNinjaPath(this->GetObjectFileDir(config));
  1213. std::string const objectFileName =
  1214. this->ConvertToNinjaPath(this->GetObjectFilePath(source, config));
  1215. std::string const objectFileDir =
  1216. cmSystemTools::GetFilenamePath(objectFileName);
  1217. std::string cmakeVarLang = cmStrCat("CMAKE_", language);
  1218. // build response file name
  1219. std::string cmakeLinkVar = cmStrCat(cmakeVarLang, "_RESPONSE_FILE_FLAG");
  1220. cmValue flag = this->GetMakefile()->GetDefinition(cmakeLinkVar);
  1221. bool const lang_supports_response =
  1222. !(language == "RC" || (language == "CUDA" && !flag));
  1223. int const commandLineLengthLimit =
  1224. ((lang_supports_response && this->ForceResponseFile())) ? -1 : 0;
  1225. cmValue pchExtension =
  1226. this->GetMakefile()->GetDefinition("CMAKE_PCH_EXTENSION");
  1227. bool const isPch = cmHasSuffix(objectFileName, pchExtension);
  1228. bool const needDyndep = !isPch &&
  1229. this->GeneratorTarget->NeedDyndepForSource(language, config, source);
  1230. WithScanning withScanning =
  1231. needDyndep ? WithScanning::Yes : WithScanning::No;
  1232. cmNinjaBuild objBuild(
  1233. this->LanguageCompilerRule(language, config, withScanning));
  1234. cmNinjaVars& vars = objBuild.Variables;
  1235. vars["FLAGS"] =
  1236. this->ComputeFlagsForObject(source, language, config, objectFileName);
  1237. vars["DEFINES"] = this->ComputeDefines(source, language, config);
  1238. vars["INCLUDES"] = this->ComputeIncludes(source, language, config);
  1239. vars["CONFIG"] = config;
  1240. auto compilerLauncher = this->GetCompilerLauncher(language, config);
  1241. cmValue const skipCodeCheck = source->GetProperty("SKIP_LINTING");
  1242. if (!skipCodeCheck.IsOn()) {
  1243. auto const cmakeCmd = this->GetLocalGenerator()->ConvertToOutputFormat(
  1244. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  1245. vars["CODE_CHECK"] =
  1246. this->GenerateCodeCheckRules(*source, compilerLauncher, cmakeCmd, config,
  1247. [this](std::string const& path) {
  1248. return this->ConvertToNinjaPath(path);
  1249. });
  1250. }
  1251. // If compiler launcher was specified and not consumed above, it
  1252. // goes to the beginning of the command line.
  1253. if (!compilerLauncher.empty()) {
  1254. cmList args{ compilerLauncher, cmList::EmptyElements::Yes };
  1255. if (!args.empty()) {
  1256. args[0] = this->LocalGenerator->ConvertToOutputFormat(
  1257. args[0], cmOutputConverter::SHELL);
  1258. for (std::string& i : cmMakeRange(args.begin() + 1, args.end())) {
  1259. i = this->LocalGenerator->EscapeForShell(i);
  1260. }
  1261. vars["LAUNCHER"] = args.join(" ") + " ";
  1262. }
  1263. }
  1264. if (this->GetMakefile()->GetSafeDefinition(
  1265. cmStrCat("CMAKE_", language, "_DEPFILE_FORMAT")) != "msvc"_s) {
  1266. bool replaceExt = false;
  1267. if (!language.empty()) {
  1268. std::string repVar =
  1269. cmStrCat("CMAKE_", language, "_DEPFILE_EXTENSION_REPLACE");
  1270. replaceExt = this->Makefile->IsOn(repVar);
  1271. }
  1272. this->AddDepfileBinding(
  1273. vars,
  1274. replaceExt ? cmStrCat(objectFileDir, '/',
  1275. cmSystemTools::GetFilenameWithoutLastExtension(
  1276. objectFileName),
  1277. ".d")
  1278. : cmStrCat(objectFileName, ".d"));
  1279. }
  1280. this->SetMsvcTargetPdbVariable(vars, config);
  1281. if (firstForConfig) {
  1282. this->ExportObjectCompileCommand(
  1283. language, sourceFilePath, objectDir, objectFileName, objectFileDir,
  1284. vars["FLAGS"], vars["DEFINES"], vars["INCLUDES"],
  1285. vars["TARGET_COMPILE_PDB"], vars["TARGET_PDB"], config, withScanning);
  1286. }
  1287. objBuild.Outputs.push_back(objectFileName);
  1288. if (firstForConfig && !isPch) {
  1289. // Add this object to the list of object files.
  1290. this->Configs[config].Objects.push_back(objectFileName);
  1291. }
  1292. objBuild.ExplicitDeps.push_back(sourceFilePath);
  1293. // Add precompile headers dependencies
  1294. std::vector<std::string> depList;
  1295. std::vector<std::string> pchArchs =
  1296. this->GeneratorTarget->GetPchArchs(config, language);
  1297. std::unordered_set<std::string> pchSources;
  1298. for (std::string const& arch : pchArchs) {
  1299. std::string const pchSource =
  1300. this->GeneratorTarget->GetPchSource(config, language, arch);
  1301. if (!pchSource.empty()) {
  1302. pchSources.insert(pchSource);
  1303. }
  1304. }
  1305. if (!pchSources.empty() && !source->GetProperty("SKIP_PRECOMPILE_HEADERS")) {
  1306. for (std::string const& arch : pchArchs) {
  1307. depList.push_back(
  1308. this->GeneratorTarget->GetPchHeader(config, language, arch));
  1309. if (pchSources.find(source->GetFullPath()) == pchSources.end()) {
  1310. depList.push_back(
  1311. this->GeneratorTarget->GetPchFile(config, language, arch));
  1312. }
  1313. }
  1314. }
  1315. if (cmValue objectDeps = source->GetProperty("OBJECT_DEPENDS")) {
  1316. cmList objDepList{ *objectDeps };
  1317. std::copy(objDepList.begin(), objDepList.end(),
  1318. std::back_inserter(depList));
  1319. }
  1320. if (!depList.empty()) {
  1321. for (std::string& odi : depList) {
  1322. if (cmSystemTools::FileIsFullPath(odi)) {
  1323. odi = cmSystemTools::CollapseFullPath(odi);
  1324. }
  1325. }
  1326. std::transform(depList.begin(), depList.end(),
  1327. std::back_inserter(objBuild.ImplicitDeps),
  1328. this->MapToNinjaPath());
  1329. }
  1330. if (this->HasPrivateGeneratedSources) {
  1331. objBuild.OrderOnlyDeps.push_back(
  1332. this->OrderDependsTargetForTargetPrivate(config));
  1333. } else {
  1334. objBuild.OrderOnlyDeps.push_back(
  1335. this->OrderDependsTargetForTarget(config));
  1336. }
  1337. // If the source file is GENERATED and does not have a custom command
  1338. // (either attached to this source file or another one), assume that one of
  1339. // the target dependencies, OBJECT_DEPENDS or header file custom commands
  1340. // will rebuild the file.
  1341. if (source->GetIsGenerated() &&
  1342. !source->GetPropertyAsBool("__CMAKE_GENERATED_BY_CMAKE") &&
  1343. !source->GetCustomCommand() &&
  1344. !this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFilePath)) {
  1345. this->GetGlobalGenerator()->AddAssumedSourceDependencies(
  1346. sourceFilePath, objBuild.OrderOnlyDeps);
  1347. }
  1348. // For some cases we scan to dynamically discover dependencies.
  1349. bool const compilationPreprocesses =
  1350. !this->NeedExplicitPreprocessing(language);
  1351. std::string modmapFormat;
  1352. if (needDyndep) {
  1353. std::string const modmapFormatVar =
  1354. cmStrCat("CMAKE_", language, "_MODULE_MAP_FORMAT");
  1355. modmapFormat = this->Makefile->GetSafeDefinition(modmapFormatVar);
  1356. }
  1357. if (needDyndep) {
  1358. // If source/target has preprocessing turned off, we still need to
  1359. // generate an explicit dependency step
  1360. auto const srcpp = source->GetSafeProperty("Fortran_PREPROCESS");
  1361. cmOutputConverter::FortranPreprocess preprocess =
  1362. cmOutputConverter::GetFortranPreprocess(srcpp);
  1363. if (preprocess == cmOutputConverter::FortranPreprocess::Unset) {
  1364. auto const& tgtpp =
  1365. this->GeneratorTarget->GetSafeProperty("Fortran_PREPROCESS");
  1366. preprocess = cmOutputConverter::GetFortranPreprocess(tgtpp);
  1367. }
  1368. bool const compilePP = !compilationPreprocesses &&
  1369. (preprocess != cmOutputConverter::FortranPreprocess::NotNeeded);
  1370. bool const compilePPWithDefines =
  1371. compilePP && this->CompileWithDefines(language);
  1372. std::string scanRuleName;
  1373. std::string ppFileName;
  1374. if (compilePP) {
  1375. scanRuleName = this->LanguagePreprocessAndScanRule(language, config);
  1376. ppFileName = this->ConvertToNinjaPath(
  1377. this->GetPreprocessedFilePath(source, config));
  1378. } else {
  1379. scanRuleName = this->LanguageScanRule(language, config);
  1380. ppFileName = cmStrCat(objectFileName, ".ddi.i");
  1381. }
  1382. cmNinjaBuild ppBuild = GetScanBuildStatement(
  1383. scanRuleName, ppFileName, compilePP, compilePPWithDefines,
  1384. compilationPreprocesses, objBuild, vars, objectFileName, this);
  1385. if (compilePP) {
  1386. // In case compilation requires flags that are incompatible with
  1387. // preprocessing, include them here.
  1388. std::string const& postFlag = this->Makefile->GetSafeDefinition(
  1389. cmStrCat("CMAKE_", language, "_POSTPROCESS_FLAG"));
  1390. this->LocalGenerator->AppendFlags(vars["FLAGS"], postFlag);
  1391. // Prepend source file's original directory as an include directory
  1392. // so e.g. Fortran INCLUDE statements can look for files in it.
  1393. std::vector<std::string> sourceDirectory;
  1394. sourceDirectory.push_back(
  1395. cmSystemTools::GetParentDirectory(source->GetFullPath()));
  1396. std::string sourceDirectoryFlag = this->LocalGenerator->GetIncludeFlags(
  1397. sourceDirectory, this->GeneratorTarget, language, config, false);
  1398. vars["INCLUDES"] = cmStrCat(sourceDirectoryFlag, ' ', vars["INCLUDES"]);
  1399. }
  1400. ScanningFiles scanningFiles;
  1401. if (firstForConfig) {
  1402. scanningFiles.ScanningOutput = cmStrCat(objectFileName, ".ddi");
  1403. }
  1404. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
  1405. ppBuild.Variables);
  1406. this->GetGlobalGenerator()->WriteBuild(this->GetImplFileStream(fileConfig),
  1407. ppBuild, commandLineLengthLimit);
  1408. std::string const dyndep = this->GetDyndepFilePath(language, config);
  1409. objBuild.OrderOnlyDeps.push_back(dyndep);
  1410. vars["dyndep"] = dyndep;
  1411. if (!modmapFormat.empty()) {
  1412. // XXX(modmap): If changing this path construction, change
  1413. // `cmGlobalNinjaGenerator::WriteDyndep` and
  1414. // `cmNinjaTargetGenerator::ExportObjectCompileCommand` to expect the
  1415. // corresponding file path.
  1416. std::string ddModmapFile = cmStrCat(objectFileName, ".modmap");
  1417. vars["DYNDEP_MODULE_MAP_FILE"] = ddModmapFile;
  1418. objBuild.ImplicitDeps.push_back(ddModmapFile);
  1419. scanningFiles.ModuleMapFile = std::move(ddModmapFile);
  1420. }
  1421. if (!scanningFiles.IsEmpty()) {
  1422. this->Configs[config].ScanningInfo[language].emplace_back(scanningFiles);
  1423. }
  1424. }
  1425. this->EnsureParentDirectoryExists(objectFileName);
  1426. vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  1427. objectDir, cmOutputConverter::SHELL);
  1428. vars["OBJECT_FILE_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  1429. objectFileDir, cmOutputConverter::SHELL);
  1430. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
  1431. vars);
  1432. if (!pchSources.empty() && !source->GetProperty("SKIP_PRECOMPILE_HEADERS")) {
  1433. auto pchIt = pchSources.find(source->GetFullPath());
  1434. if (pchIt != pchSources.end()) {
  1435. this->addPoolNinjaVariable("JOB_POOL_PRECOMPILE_HEADER",
  1436. this->GetGeneratorTarget(), vars);
  1437. }
  1438. }
  1439. objBuild.RspFile = cmStrCat(objectFileName, ".rsp");
  1440. if (language == "ISPC") {
  1441. std::string const& objectName =
  1442. this->GeneratorTarget->GetObjectName(source);
  1443. std::string ispcSource =
  1444. cmSystemTools::GetFilenameWithoutLastExtension(objectName);
  1445. ispcSource = cmSystemTools::GetFilenameWithoutLastExtension(ispcSource);
  1446. cmValue ispcSuffixProp =
  1447. this->GeneratorTarget->GetProperty("ISPC_HEADER_SUFFIX");
  1448. assert(ispcSuffixProp);
  1449. std::string ispcHeaderDirectory =
  1450. this->GeneratorTarget->GetObjectDirectory(config);
  1451. if (cmValue prop =
  1452. this->GeneratorTarget->GetProperty("ISPC_HEADER_DIRECTORY")) {
  1453. ispcHeaderDirectory =
  1454. cmStrCat(this->LocalGenerator->GetBinaryDirectory(), '/', *prop);
  1455. }
  1456. std::string ispcHeader =
  1457. cmStrCat(ispcHeaderDirectory, '/', ispcSource, *ispcSuffixProp);
  1458. ispcHeader = this->ConvertToNinjaPath(ispcHeader);
  1459. // Make sure ninja knows what command generates the header
  1460. objBuild.ImplicitOuts.push_back(ispcHeader);
  1461. // Make sure ninja knows how to clean the generated header
  1462. this->GetGlobalGenerator()->AddAdditionalCleanFile(ispcHeader, config);
  1463. auto ispcSuffixes =
  1464. detail::ComputeISPCObjectSuffixes(this->GeneratorTarget);
  1465. if (ispcSuffixes.size() > 1) {
  1466. std::string rootObjectDir =
  1467. this->GeneratorTarget->GetObjectDirectory(config);
  1468. auto ispcSideEfffectObjects = detail::ComputeISPCExtraObjects(
  1469. objectName, rootObjectDir, ispcSuffixes);
  1470. for (auto sideEffect : ispcSideEfffectObjects) {
  1471. sideEffect = this->ConvertToNinjaPath(sideEffect);
  1472. objBuild.ImplicitOuts.emplace_back(sideEffect);
  1473. this->GetGlobalGenerator()->AddAdditionalCleanFile(sideEffect, config);
  1474. }
  1475. }
  1476. vars["ISPC_HEADER_FILE"] =
  1477. this->GetLocalGenerator()->ConvertToOutputFormat(
  1478. ispcHeader, cmOutputConverter::SHELL);
  1479. } else {
  1480. auto headers = this->GeneratorTarget->GetGeneratedISPCHeaders(config);
  1481. if (!headers.empty()) {
  1482. std::transform(headers.begin(), headers.end(), headers.begin(),
  1483. this->MapToNinjaPath());
  1484. objBuild.OrderOnlyDeps.insert(objBuild.OrderOnlyDeps.end(),
  1485. headers.begin(), headers.end());
  1486. }
  1487. }
  1488. if (language == "Swift") {
  1489. this->EmitSwiftDependencyInfo(source, config);
  1490. } else {
  1491. this->GetGlobalGenerator()->WriteBuild(this->GetImplFileStream(fileConfig),
  1492. objBuild, commandLineLengthLimit);
  1493. }
  1494. if (cmValue objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
  1495. std::string evaluatedObjectOutputs = cmGeneratorExpression::Evaluate(
  1496. *objectOutputs, this->LocalGenerator, config);
  1497. if (!evaluatedObjectOutputs.empty()) {
  1498. cmNinjaBuild build("phony");
  1499. build.Comment = "Additional output files.";
  1500. build.Outputs = cmList{ evaluatedObjectOutputs }.data();
  1501. std::transform(build.Outputs.begin(), build.Outputs.end(),
  1502. build.Outputs.begin(), this->MapToNinjaPath());
  1503. build.ExplicitDeps = objBuild.Outputs;
  1504. this->GetGlobalGenerator()->WriteBuild(
  1505. this->GetImplFileStream(fileConfig), build);
  1506. }
  1507. }
  1508. }
  1509. void cmNinjaTargetGenerator::WriteCxxModuleBmiBuildStatement(
  1510. cmSourceFile const* source, std::string const& config,
  1511. std::string const& fileConfig, bool firstForConfig)
  1512. {
  1513. std::string const language = source->GetLanguage();
  1514. if (language != "CXX"_s) {
  1515. this->GetMakefile()->IssueMessage(
  1516. MessageType::FATAL_ERROR,
  1517. cmStrCat("Source file '", source->GetFullPath(), "' of target '",
  1518. this->GetTargetName(), "' is a '", language,
  1519. "' source but must be 'CXX' in order to have a BMI build "
  1520. "statement generated."));
  1521. return;
  1522. }
  1523. std::string const sourceFilePath = this->GetCompiledSourceNinjaPath(source);
  1524. std::string const bmiDir = this->ConvertToNinjaPath(
  1525. cmStrCat(this->GeneratorTarget->GetSupportDirectory(),
  1526. this->GetGlobalGenerator()->ConfigDirectory(config)));
  1527. std::string const bmiFileName =
  1528. this->ConvertToNinjaPath(this->GetBmiFilePath(source, config));
  1529. std::string const bmiFileDir = cmSystemTools::GetFilenamePath(bmiFileName);
  1530. int const commandLineLengthLimit = this->ForceResponseFile() ? -1 : 0;
  1531. cmNinjaBuild bmiBuild(
  1532. this->LanguageCompilerRule(language, config, WithScanning::Yes));
  1533. cmNinjaVars& vars = bmiBuild.Variables;
  1534. vars["FLAGS"] =
  1535. this->ComputeFlagsForObject(source, language, config, bmiFileName);
  1536. vars["DEFINES"] = this->ComputeDefines(source, language, config);
  1537. vars["INCLUDES"] = this->ComputeIncludes(source, language, config);
  1538. vars["CONFIG"] = config;
  1539. if (this->GetMakefile()->GetSafeDefinition(
  1540. cmStrCat("CMAKE_", language, "_DEPFILE_FORMAT")) != "msvc"_s) {
  1541. bool replaceExt = false;
  1542. if (!language.empty()) {
  1543. std::string repVar =
  1544. cmStrCat("CMAKE_", language, "_DEPFILE_EXTENSION_REPLACE");
  1545. replaceExt = this->Makefile->IsOn(repVar);
  1546. }
  1547. this->AddDepfileBinding(
  1548. vars,
  1549. replaceExt
  1550. ? cmStrCat(bmiFileDir, '/',
  1551. cmSystemTools::GetFilenameWithoutLastExtension(bmiFileName),
  1552. ".d")
  1553. : cmStrCat(bmiFileName, ".d"));
  1554. }
  1555. std::string d =
  1556. this->GeneratorTarget->GetClangTidyExportFixesDirectory(language);
  1557. if (!d.empty()) {
  1558. this->GlobalCommonGenerator->AddClangTidyExportFixesDir(d);
  1559. std::string fixesFile =
  1560. this->GetClangTidyReplacementsFilePath(d, *source, config);
  1561. this->GlobalCommonGenerator->AddClangTidyExportFixesFile(fixesFile);
  1562. cmSystemTools::MakeDirectory(cmSystemTools::GetFilenamePath(fixesFile));
  1563. fixesFile = this->ConvertToNinjaPath(fixesFile);
  1564. vars["CLANG_TIDY_EXPORT_FIXES"] = fixesFile;
  1565. }
  1566. this->SetMsvcTargetPdbVariable(vars, config);
  1567. if (firstForConfig) {
  1568. this->ExportObjectCompileCommand(
  1569. language, sourceFilePath, bmiDir, bmiFileName, bmiFileDir, vars["FLAGS"],
  1570. vars["DEFINES"], vars["INCLUDES"], vars["TARGET_COMPILE_PDB"],
  1571. vars["TARGET_PDB"], config, WithScanning::Yes);
  1572. }
  1573. bmiBuild.Outputs.push_back(bmiFileName);
  1574. bmiBuild.ExplicitDeps.push_back(sourceFilePath);
  1575. std::vector<std::string> depList;
  1576. bmiBuild.OrderOnlyDeps.push_back(this->OrderDependsTargetForTarget(config));
  1577. // For some cases we scan to dynamically discover dependencies.
  1578. std::string modmapFormat;
  1579. if (true) {
  1580. std::string const modmapFormatVar =
  1581. cmStrCat("CMAKE_", language, "_MODULE_MAP_FORMAT");
  1582. modmapFormat = this->Makefile->GetSafeDefinition(modmapFormatVar);
  1583. }
  1584. {
  1585. bool const compilePPWithDefines = this->CompileWithDefines(language);
  1586. std::string scanRuleName = this->LanguageScanRule(language, config);
  1587. std::string ppFileName = cmStrCat(bmiFileName, ".ddi.i");
  1588. cmNinjaBuild ppBuild = GetScanBuildStatement(
  1589. scanRuleName, ppFileName, false, compilePPWithDefines, true, bmiBuild,
  1590. vars, bmiFileName, this);
  1591. ScanningFiles scanningFiles;
  1592. if (firstForConfig) {
  1593. scanningFiles.ScanningOutput = cmStrCat(bmiFileName, ".ddi");
  1594. }
  1595. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
  1596. ppBuild.Variables);
  1597. this->GetGlobalGenerator()->WriteBuild(this->GetImplFileStream(fileConfig),
  1598. ppBuild, commandLineLengthLimit);
  1599. std::string const dyndep = this->GetDyndepFilePath(language, config);
  1600. bmiBuild.OrderOnlyDeps.push_back(dyndep);
  1601. vars["dyndep"] = dyndep;
  1602. if (!modmapFormat.empty()) {
  1603. std::string ddModmapFile = cmStrCat(bmiFileName, ".modmap");
  1604. vars["DYNDEP_MODULE_MAP_FILE"] = ddModmapFile;
  1605. scanningFiles.ModuleMapFile = std::move(ddModmapFile);
  1606. }
  1607. if (!scanningFiles.IsEmpty()) {
  1608. this->Configs[config].ScanningInfo[language].emplace_back(scanningFiles);
  1609. }
  1610. }
  1611. this->EnsureParentDirectoryExists(bmiFileName);
  1612. vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  1613. bmiDir, cmOutputConverter::SHELL);
  1614. vars["OBJECT_FILE_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  1615. bmiFileDir, cmOutputConverter::SHELL);
  1616. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
  1617. vars);
  1618. bmiBuild.RspFile = cmStrCat(bmiFileName, ".rsp");
  1619. this->GetGlobalGenerator()->WriteBuild(this->GetImplFileStream(fileConfig),
  1620. bmiBuild, commandLineLengthLimit);
  1621. }
  1622. void cmNinjaTargetGenerator::WriteSwiftObjectBuildStatement(
  1623. std::vector<cmSourceFile const*> const& sources, std::string const& config,
  1624. std::string const& fileConfig, bool firstForConfig)
  1625. {
  1626. // Swift sources are compiled as a module, not individually like with C/C++.
  1627. // Flags, header search paths, and definitions are passed to the entire
  1628. // module build, but we still need to emit compile-commands for each source
  1629. // file in order to support CMAKE_EXPORT_COMPILE_COMMANDS.
  1630. // In whole-module mode, with a single thread, the Swift compiler will
  1631. // only emit a single object file, but if more than one thread is specified,
  1632. // or building in other modes, the compiler will emit multiple object files.
  1633. // When building a single-output, we do not provide an output-file-map (OFM),
  1634. // and instead pass `-o` to tell the compiler where to write the object.
  1635. // When building multiple outputs, we provide an OFM to tell the compiler
  1636. // where to put each object.
  1637. //
  1638. //
  1639. // Per-Target (module):
  1640. // - Flags
  1641. // - Definitions
  1642. // - Include paths
  1643. // - (single-output) output object filename
  1644. // - Swiftmodule
  1645. //
  1646. // Per-File:
  1647. // - compile-command
  1648. // - (multi-output) OFM data
  1649. // - (multi-output) output object filename
  1650. //
  1651. // Note: Due to the differences in the build models, we are only able to
  1652. // build the object build-graph if we know what mode the target is built in.
  1653. // For that, we need the "NEW" behavior for CMP0157. Otherwise, we have to
  1654. // fall back on the old "linker" build. Otherwise, this should be
  1655. // indistinguishable from the old behavior.
  1656. if (sources.empty()) {
  1657. return;
  1658. }
  1659. cmSwiftCompileMode compileMode;
  1660. if (cm::optional<cmSwiftCompileMode> optionalCompileMode =
  1661. this->LocalGenerator->GetSwiftCompileMode(this->GeneratorTarget,
  1662. config)) {
  1663. compileMode = *optionalCompileMode;
  1664. } else {
  1665. // CMP0157 is not NEW, bailing early!
  1666. return;
  1667. }
  1668. std::string const language = "Swift";
  1669. std::string const objectDir = this->ConvertToNinjaPath(
  1670. cmStrCat(this->GeneratorTarget->GetSupportDirectory(),
  1671. this->GetGlobalGenerator()->ConfigDirectory(config)));
  1672. cmGeneratorTarget const& target = *this->GeneratorTarget;
  1673. cmNinjaBuild objBuild(
  1674. this->LanguageCompilerRule(language, config, WithScanning::No));
  1675. cmNinjaVars& vars = objBuild.Variables;
  1676. // The swift toolchain leaves outputs untouched if there are no meaningful
  1677. // changes to input files (e.g. addition of a comment).
  1678. vars.emplace("restat", "1");
  1679. std::string const moduleName = target.GetSwiftModuleName();
  1680. std::string const moduleFilepath =
  1681. this->ConvertToNinjaPath(target.GetSwiftModulePath(config));
  1682. vars.emplace("description",
  1683. cmStrCat("Building Swift Module '", moduleName, "' with ",
  1684. sources.size(),
  1685. sources.size() == 1 ? " source" : " sources"));
  1686. bool const isSingleOutput = [this, compileMode]() -> bool {
  1687. bool isMultiThread = false;
  1688. if (cmValue numThreadStr =
  1689. this->GetMakefile()->GetDefinition("CMAKE_Swift_NUM_THREADS")) {
  1690. unsigned long numThreads;
  1691. cmStrToULong(*numThreadStr, &numThreads);
  1692. // numThreads == 1 is multi-threaded according to swiftc
  1693. isMultiThread = numThreads > 0;
  1694. }
  1695. return !isMultiThread && compileMode == cmSwiftCompileMode::Wholemodule;
  1696. }();
  1697. // Without `-emit-library` or `-emit-executable`, targets with a single
  1698. // source file parse as a Swift script instead of like normal source. For
  1699. // non-executable targets, append this to ensure that they are parsed like a
  1700. // normal source.
  1701. if (target.GetType() != cmStateEnums::EXECUTABLE) {
  1702. this->LocalGenerator->AppendFlags(vars["FLAGS"], "-parse-as-library");
  1703. }
  1704. if (target.GetType() == cmStateEnums::STATIC_LIBRARY) {
  1705. this->LocalGenerator->AppendFlags(vars["FLAGS"], "-static");
  1706. }
  1707. // Does this swift target emit a module file for importing into other
  1708. // targets?
  1709. auto isImportableTarget = [](cmGeneratorTarget const& tgt) -> bool {
  1710. // Everything except for executables that don't export anything should emit
  1711. // some way to import them.
  1712. if (tgt.GetType() == cmStateEnums::EXECUTABLE) {
  1713. return tgt.IsExecutableWithExports();
  1714. }
  1715. return true;
  1716. };
  1717. // Swift modules only make sense to emit from things that can be imported.
  1718. // Executables that don't export symbols can't be imported, so don't try to
  1719. // emit a swiftmodule for them. It will break.
  1720. if (isImportableTarget(target)) {
  1721. std::string const emitModuleFlag = "-emit-module";
  1722. std::string const modulePathFlag = "-emit-module-path";
  1723. this->LocalGenerator->AppendFlags(
  1724. vars["FLAGS"],
  1725. { emitModuleFlag, modulePathFlag,
  1726. this->LocalGenerator->ConvertToOutputFormat(
  1727. moduleFilepath, cmOutputConverter::SHELL) });
  1728. objBuild.Outputs.push_back(moduleFilepath);
  1729. }
  1730. this->LocalGenerator->AppendFlags(vars["FLAGS"],
  1731. cmStrCat("-module-name ", moduleName));
  1732. if (target.GetType() != cmStateEnums::EXECUTABLE) {
  1733. std::string const libraryLinkNameFlag = "-module-link-name";
  1734. std::string const libraryLinkName =
  1735. this->GetGeneratorTarget()->GetLibraryNames(config).Base;
  1736. this->LocalGenerator->AppendFlags(
  1737. vars["FLAGS"], cmStrCat(libraryLinkNameFlag, ' ', libraryLinkName));
  1738. }
  1739. this->LocalGenerator->AppendFlags(vars["FLAGS"],
  1740. this->GetFlags(language, config));
  1741. vars["DEFINES"] = this->GetDefines(language, config);
  1742. vars["INCLUDES"] = this->GetIncludes(language, config);
  1743. vars["CONFIG"] = config;
  1744. // target-level object filename
  1745. std::string const targetObjectFilename = this->ConvertToNinjaPath(cmStrCat(
  1746. objectDir, '/', moduleName,
  1747. this->GetGlobalGenerator()->GetLanguageOutputExtension(language)));
  1748. objBuild.RspFile = cmStrCat(targetObjectFilename, ".swift.rsp");
  1749. if (isSingleOutput) {
  1750. this->LocalGenerator->AppendFlags(vars["FLAGS"],
  1751. cmStrCat("-o ", targetObjectFilename));
  1752. objBuild.Outputs.push_back(targetObjectFilename);
  1753. this->Configs[config].Objects.push_back(targetObjectFilename);
  1754. }
  1755. for (cmSourceFile const* sf : sources) {
  1756. // Add dependency to object build on each source file
  1757. std::string const sourceFilePath = this->GetCompiledSourceNinjaPath(sf);
  1758. objBuild.ExplicitDeps.push_back(sourceFilePath);
  1759. if (!isSingleOutput) {
  1760. // Object outputs
  1761. std::string const objectFilepath =
  1762. this->ConvertToNinjaPath(this->GetObjectFilePath(sf, config));
  1763. this->EnsureParentDirectoryExists(objectFilepath);
  1764. objBuild.Outputs.push_back(objectFilepath);
  1765. this->Configs[config].Objects.push_back(objectFilepath);
  1766. // Add OFM data
  1767. this->EmitSwiftDependencyInfo(sf, config);
  1768. }
  1769. }
  1770. if (!isSingleOutput) {
  1771. this->GenerateSwiftOutputFileMap(config, vars["FLAGS"]);
  1772. }
  1773. if (firstForConfig) {
  1774. this->ExportSwiftObjectCompileCommand(
  1775. sources, targetObjectFilename, vars["FLAGS"], vars["DEFINES"],
  1776. vars["INCLUDES"], config, isSingleOutput);
  1777. }
  1778. for (cmTargetDepend const& dep :
  1779. this->GetGlobalGenerator()->GetTargetDirectDepends(&target)) {
  1780. if (!dep->IsLanguageUsed("Swift", config)) {
  1781. continue;
  1782. }
  1783. // If the dependency emits a swiftmodule, add a dependency edge on that
  1784. // swiftmodule to the ninja build graph.
  1785. if (isImportableTarget(*dep)) {
  1786. std::string const depModuleFilepath =
  1787. this->ConvertToNinjaPath(dep->GetSwiftModulePath(config));
  1788. objBuild.ImplicitDeps.push_back(depModuleFilepath);
  1789. }
  1790. }
  1791. objBuild.OrderOnlyDeps.push_back(this->OrderDependsTargetForTarget(config));
  1792. // Write object build
  1793. this->GetGlobalGenerator()->WriteBuild(this->GetImplFileStream(fileConfig),
  1794. objBuild,
  1795. this->ForceResponseFile() ? -1 : 0);
  1796. }
  1797. void cmNinjaTargetGenerator::WriteTargetDependInfo(std::string const& lang,
  1798. std::string const& config)
  1799. {
  1800. Json::Value tdi(Json::objectValue);
  1801. tdi["language"] = lang;
  1802. tdi["compiler-id"] = this->Makefile->GetSafeDefinition(
  1803. cmStrCat("CMAKE_", lang, "_COMPILER_ID"));
  1804. tdi["compiler-simulate-id"] = this->Makefile->GetSafeDefinition(
  1805. cmStrCat("CMAKE_", lang, "_SIMULATE_ID"));
  1806. tdi["compiler-frontend-variant"] = this->Makefile->GetSafeDefinition(
  1807. cmStrCat("CMAKE_", lang, "_COMPILER_FRONTEND_VARIANT"));
  1808. std::string mod_dir;
  1809. if (lang == "Fortran") {
  1810. mod_dir = this->GeneratorTarget->GetFortranModuleDirectory(
  1811. this->Makefile->GetHomeOutputDirectory());
  1812. } else if (lang == "CXX") {
  1813. mod_dir = this->GetGlobalGenerator()->ExpandCFGIntDir(
  1814. cmSystemTools::CollapseFullPath(this->GeneratorTarget->ObjectDirectory),
  1815. config);
  1816. }
  1817. if (mod_dir.empty()) {
  1818. mod_dir = this->Makefile->GetCurrentBinaryDirectory();
  1819. }
  1820. tdi["module-dir"] = mod_dir;
  1821. if (lang == "Fortran") {
  1822. tdi["submodule-sep"] =
  1823. this->Makefile->GetSafeDefinition("CMAKE_Fortran_SUBMODULE_SEP");
  1824. tdi["submodule-ext"] =
  1825. this->Makefile->GetSafeDefinition("CMAKE_Fortran_SUBMODULE_EXT");
  1826. } else if (lang == "CXX") {
  1827. // No extra information necessary.
  1828. }
  1829. tdi["dir-cur-bld"] = this->Makefile->GetCurrentBinaryDirectory();
  1830. tdi["dir-cur-src"] = this->Makefile->GetCurrentSourceDirectory();
  1831. tdi["dir-top-bld"] = this->Makefile->GetHomeOutputDirectory();
  1832. tdi["dir-top-src"] = this->Makefile->GetHomeDirectory();
  1833. Json::Value& tdi_include_dirs = tdi["include-dirs"] = Json::arrayValue;
  1834. std::vector<std::string> includes;
  1835. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  1836. lang, config);
  1837. for (std::string const& i : includes) {
  1838. // Convert the include directories the same way we do for -I flags.
  1839. // See upstream ninja issue 1251.
  1840. tdi_include_dirs.append(this->ConvertToNinjaPath(i));
  1841. }
  1842. Json::Value& tdi_linked_target_dirs = tdi["linked-target-dirs"] =
  1843. Json::arrayValue;
  1844. auto const linked_directories =
  1845. this->GetLinkedTargetDirectories(lang, config);
  1846. for (std::string const& l : linked_directories.Direct) {
  1847. tdi_linked_target_dirs.append(l);
  1848. }
  1849. Json::Value& tdi_forward_modules_from_target_dirs =
  1850. tdi["forward-modules-from-target-dirs"] = Json::arrayValue;
  1851. for (std::string const& l : linked_directories.Forward) {
  1852. tdi_forward_modules_from_target_dirs.append(l);
  1853. }
  1854. cmDyndepGeneratorCallbacks cb;
  1855. cb.ObjectFilePath = [this](cmSourceFile const* sf, std::string const& cnf) {
  1856. return this->GetObjectFilePath(sf, cnf);
  1857. };
  1858. cb.BmiFilePath = [this](cmSourceFile const* sf, std::string const& cnf) {
  1859. return this->GetBmiFilePath(sf, cnf);
  1860. };
  1861. #if !defined(CMAKE_BOOTSTRAP)
  1862. cmDyndepCollation::AddCollationInformation(tdi, this->GeneratorTarget,
  1863. config, cb);
  1864. #endif
  1865. std::string const tdin = this->GetTargetDependInfoPath(lang, config);
  1866. cmGeneratedFileStream tdif(tdin);
  1867. tdif << tdi;
  1868. }
  1869. void cmNinjaTargetGenerator::EmitSwiftDependencyInfo(
  1870. cmSourceFile const* source, std::string const& config)
  1871. {
  1872. std::string const sourceFilePath = this->GetCompiledSourceNinjaPath(source);
  1873. std::string const objectFilePath =
  1874. this->ConvertToNinjaPath(this->GetObjectFilePath(source, config));
  1875. std::string const swiftDepsPath = [source, objectFilePath]() -> std::string {
  1876. if (cmValue name = source->GetProperty("Swift_DEPENDENCIES_FILE")) {
  1877. return *name;
  1878. }
  1879. return cmStrCat(objectFilePath, ".swiftdeps");
  1880. }();
  1881. std::string const swiftDiaPath = [source, objectFilePath]() -> std::string {
  1882. if (cmValue name = source->GetProperty("Swift_DIAGNOSTICS_FILE")) {
  1883. return *name;
  1884. }
  1885. return cmStrCat(objectFilePath, ".dia");
  1886. }();
  1887. std::string const makeDepsPath = [this, source, config]() -> std::string {
  1888. cmLocalNinjaGenerator const* local = this->GetLocalGenerator();
  1889. std::string const objectFileName =
  1890. this->ConvertToNinjaPath(this->GetObjectFilePath(source, config));
  1891. std::string const objectFileDir =
  1892. cmSystemTools::GetFilenamePath(objectFileName);
  1893. if (this->Makefile->IsOn("CMAKE_Swift_DEPFLE_EXTNSION_REPLACE")) {
  1894. std::string dependFileName = cmStrCat(
  1895. cmSystemTools::GetFilenameWithoutLastExtension(objectFileName), ".d");
  1896. return local->ConvertToOutputFormat(
  1897. cmStrCat(objectFileDir, '/', dependFileName),
  1898. cmOutputConverter::SHELL);
  1899. }
  1900. return local->ConvertToOutputFormat(cmStrCat(objectFileName, ".d"),
  1901. cmOutputConverter::SHELL);
  1902. }();
  1903. // build the source file mapping
  1904. // https://github.com/apple/swift/blob/master/docs/Driver.md#output-file-maps
  1905. Json::Value entry = Json::Value(Json::objectValue);
  1906. entry["object"] = objectFilePath;
  1907. entry["dependencies"] = makeDepsPath;
  1908. entry["swift-dependencies"] = swiftDepsPath;
  1909. entry["diagnostics"] = swiftDiaPath;
  1910. this->Configs[config].SwiftOutputMap[sourceFilePath] = entry;
  1911. }
  1912. void cmNinjaTargetGenerator::ExportObjectCompileCommand(
  1913. std::string const& language, std::string const& sourceFileName,
  1914. std::string const& objectDir, std::string const& objectFileName,
  1915. std::string const& objectFileDir, std::string const& flags,
  1916. std::string const& defines, std::string const& includes,
  1917. std::string const& targetCompilePdb, std::string const& targetPdb,
  1918. std::string const& outputConfig, WithScanning withScanning)
  1919. {
  1920. if (!this->GeneratorTarget->GetPropertyAsBool("EXPORT_COMPILE_COMMANDS")) {
  1921. return;
  1922. }
  1923. cmRulePlaceholderExpander::RuleVariables compileObjectVars;
  1924. compileObjectVars.Language = language.c_str();
  1925. std::string escapedSourceFileName = sourceFileName;
  1926. if (!cmSystemTools::FileIsFullPath(sourceFileName)) {
  1927. escapedSourceFileName =
  1928. cmSystemTools::CollapseFullPath(escapedSourceFileName,
  1929. this->GetGlobalGenerator()
  1930. ->GetCMakeInstance()
  1931. ->GetHomeOutputDirectory());
  1932. }
  1933. escapedSourceFileName = this->LocalGenerator->ConvertToOutputFormat(
  1934. escapedSourceFileName, cmOutputConverter::SHELL);
  1935. std::string fullFlags = flags;
  1936. if (withScanning == WithScanning::Yes) {
  1937. std::string const modmapFormatVar =
  1938. cmStrCat("CMAKE_", language, "_MODULE_MAP_FORMAT");
  1939. std::string const modmapFormat =
  1940. this->Makefile->GetSafeDefinition(modmapFormatVar);
  1941. if (!modmapFormat.empty()) {
  1942. std::string modmapFlags = this->GetMakefile()->GetRequiredDefinition(
  1943. cmStrCat("CMAKE_", language, "_MODULE_MAP_FLAG"));
  1944. // XXX(modmap): If changing this path construction, change
  1945. // `cmGlobalNinjaGenerator::WriteDyndep` and
  1946. // `cmNinjaTargetGenerator::WriteObjectBuildStatement` to expect the
  1947. // corresponding file path.
  1948. cmSystemTools::ReplaceString(modmapFlags, "<MODULE_MAP_FILE>",
  1949. cmStrCat(objectFileName, ".modmap"));
  1950. fullFlags += cmStrCat(' ', modmapFlags);
  1951. }
  1952. }
  1953. compileObjectVars.Source = escapedSourceFileName.c_str();
  1954. std::string escapedObjectFileName =
  1955. this->LocalGenerator->ConvertToOutputFormat(objectFileName,
  1956. cmOutputConverter::SHELL);
  1957. compileObjectVars.Object = escapedObjectFileName.c_str();
  1958. compileObjectVars.ObjectDir = objectDir.c_str();
  1959. compileObjectVars.ObjectFileDir = objectFileDir.c_str();
  1960. compileObjectVars.Flags = fullFlags.c_str();
  1961. compileObjectVars.Defines = defines.c_str();
  1962. compileObjectVars.Includes = includes.c_str();
  1963. compileObjectVars.TargetCompilePDB = targetCompilePdb.c_str();
  1964. compileObjectVars.TargetPDB = targetPdb.c_str();
  1965. // Rule for compiling object file.
  1966. std::string cudaCompileMode;
  1967. if (language == "CUDA") {
  1968. if (this->GeneratorTarget->GetPropertyAsBool(
  1969. "CUDA_SEPARABLE_COMPILATION")) {
  1970. std::string const& rdcFlag =
  1971. this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_RDC_FLAG");
  1972. cudaCompileMode = cmStrCat(cudaCompileMode, rdcFlag, ' ');
  1973. }
  1974. static std::array<cm::string_view, 4> const compileModes{
  1975. { "PTX"_s, "CUBIN"_s, "FATBIN"_s, "OPTIX"_s }
  1976. };
  1977. bool useNormalCompileMode = true;
  1978. for (cm::string_view mode : compileModes) {
  1979. auto propName = cmStrCat("CUDA_", mode, "_COMPILATION");
  1980. auto defName = cmStrCat("_CMAKE_CUDA_", mode, "_FLAG");
  1981. if (this->GeneratorTarget->GetPropertyAsBool(propName)) {
  1982. std::string const& flag =
  1983. this->Makefile->GetRequiredDefinition(defName);
  1984. cudaCompileMode = cmStrCat(cudaCompileMode, flag);
  1985. useNormalCompileMode = false;
  1986. break;
  1987. }
  1988. }
  1989. if (useNormalCompileMode) {
  1990. std::string const& wholeFlag =
  1991. this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_WHOLE_FLAG");
  1992. cudaCompileMode = cmStrCat(cudaCompileMode, wholeFlag);
  1993. }
  1994. compileObjectVars.CudaCompileMode = cudaCompileMode.c_str();
  1995. }
  1996. std::string const cmdVar = cmStrCat("CMAKE_", language, "_COMPILE_OBJECT");
  1997. std::string const& compileCmd =
  1998. this->Makefile->GetRequiredDefinition(cmdVar);
  1999. cmList compileCmds(compileCmd);
  2000. auto rulePlaceholderExpander =
  2001. this->GetLocalGenerator()->CreateRulePlaceholderExpander();
  2002. for (auto& i : compileCmds) {
  2003. // no launcher for CMAKE_EXPORT_COMPILE_COMMANDS
  2004. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(), i,
  2005. compileObjectVars);
  2006. }
  2007. std::string cmdLine = this->GetLocalGenerator()->BuildCommandLine(
  2008. compileCmds, outputConfig, outputConfig);
  2009. this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine, sourceFileName,
  2010. objectFileName);
  2011. }
  2012. void cmNinjaTargetGenerator::ExportSwiftObjectCompileCommand(
  2013. std::vector<cmSourceFile const*> const& moduleSourceFiles,
  2014. std::string const& moduleObjectFilename, std::string const& flags,
  2015. std::string const& defines, std::string const& includes,
  2016. std::string const& outputConfig, bool singleOutput)
  2017. {
  2018. if (!this->GeneratorTarget->GetPropertyAsBool("EXPORT_COMPILE_COMMANDS")) {
  2019. return;
  2020. }
  2021. auto escapeSourceFileName = [this](std::string srcFilename) -> std::string {
  2022. if (!cmSystemTools::FileIsFullPath(srcFilename)) {
  2023. srcFilename =
  2024. cmSystemTools::CollapseFullPath(srcFilename,
  2025. this->GetGlobalGenerator()
  2026. ->GetCMakeInstance()
  2027. ->GetHomeOutputDirectory());
  2028. }
  2029. return this->LocalGenerator->ConvertToOutputFormat(
  2030. srcFilename, cmOutputConverter::SHELL);
  2031. };
  2032. auto escapedModuleObjectFilename =
  2033. this->ConvertToNinjaPath(moduleObjectFilename);
  2034. cmRulePlaceholderExpander::RuleVariables compileObjectVars;
  2035. compileObjectVars.Language = "Swift";
  2036. compileObjectVars.Flags = flags.c_str();
  2037. compileObjectVars.Defines = defines.c_str();
  2038. compileObjectVars.Includes = includes.c_str();
  2039. // Build up the list of source files in the module
  2040. std::vector<std::string> filenames;
  2041. filenames.reserve(moduleSourceFiles.size());
  2042. for (cmSourceFile const* sf : moduleSourceFiles) {
  2043. filenames.emplace_back(
  2044. escapeSourceFileName(this->GetCompiledSourceNinjaPath(sf)));
  2045. }
  2046. // Note that `escapedSourceFilenames` must remain alive until the
  2047. // compileObjectVars is consumed or Source will be a dangling pointer.
  2048. std::string const escapedSourceFilenames = cmJoin(filenames, " ");
  2049. compileObjectVars.Source = escapedSourceFilenames.c_str();
  2050. std::string const& compileCommand =
  2051. this->Makefile->GetRequiredDefinition("CMAKE_Swift_COMPILE_OBJECT");
  2052. cmList compileCmds(compileCommand);
  2053. auto rulePlaceholderExpander =
  2054. this->GetLocalGenerator()->CreateRulePlaceholderExpander();
  2055. for (cmSourceFile const* sf : moduleSourceFiles) {
  2056. std::string const sourceFilename = this->GetCompiledSourceNinjaPath(sf);
  2057. std::string objectFilename = escapedModuleObjectFilename;
  2058. if (!singleOutput) {
  2059. // If it's not single-output, each source file gets a separate object
  2060. objectFilename =
  2061. this->ConvertToNinjaPath(this->GetObjectFilePath(sf, outputConfig));
  2062. }
  2063. compileObjectVars.Objects = objectFilename.c_str();
  2064. for (std::string& cmd : compileCmds) {
  2065. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(),
  2066. cmd, compileObjectVars);
  2067. }
  2068. std::string commandLine = this->GetLocalGenerator()->BuildCommandLine(
  2069. compileCmds, outputConfig, outputConfig);
  2070. this->GetGlobalGenerator()->AddCXXCompileCommand(
  2071. commandLine, sourceFilename, objectFilename);
  2072. }
  2073. }
  2074. void cmNinjaTargetGenerator::AdditionalCleanFiles(std::string const& config)
  2075. {
  2076. if (cmValue prop_value =
  2077. this->GeneratorTarget->GetProperty("ADDITIONAL_CLEAN_FILES")) {
  2078. cmLocalNinjaGenerator* lg = this->LocalGenerator;
  2079. cmList cleanFiles(cmGeneratorExpression::Evaluate(*prop_value, lg, config,
  2080. this->GeneratorTarget));
  2081. std::string const& binaryDir = lg->GetCurrentBinaryDirectory();
  2082. cmGlobalNinjaGenerator* gg = lg->GetGlobalNinjaGenerator();
  2083. for (auto const& cleanFile : cleanFiles) {
  2084. // Support relative paths
  2085. gg->AddAdditionalCleanFile(
  2086. cmSystemTools::CollapseFullPath(cleanFile, binaryDir), config);
  2087. }
  2088. }
  2089. }
  2090. cmNinjaDeps cmNinjaTargetGenerator::GetObjects(std::string const& config) const
  2091. {
  2092. auto const it = this->Configs.find(config);
  2093. if (it != this->Configs.end()) {
  2094. return it->second.Objects;
  2095. }
  2096. return {};
  2097. }
  2098. void cmNinjaTargetGenerator::EnsureDirectoryExists(
  2099. std::string const& path) const
  2100. {
  2101. if (cmSystemTools::FileIsFullPath(path)) {
  2102. cmSystemTools::MakeDirectory(path);
  2103. } else {
  2104. cmGlobalNinjaGenerator* gg = this->GetGlobalGenerator();
  2105. std::string fullPath = gg->GetCMakeInstance()->GetHomeOutputDirectory();
  2106. // Also ensures there is a trailing slash.
  2107. gg->StripNinjaOutputPathPrefixAsSuffix(fullPath);
  2108. fullPath += path;
  2109. cmSystemTools::MakeDirectory(fullPath);
  2110. }
  2111. }
  2112. void cmNinjaTargetGenerator::EnsureParentDirectoryExists(
  2113. std::string const& path) const
  2114. {
  2115. this->EnsureDirectoryExists(cmSystemTools::GetParentDirectory(path));
  2116. }
  2117. void cmNinjaTargetGenerator::MacOSXContentGeneratorType::operator()(
  2118. cmSourceFile const& source, char const* pkgloc, std::string const& config)
  2119. {
  2120. // Skip OS X content when not building a Framework or Bundle.
  2121. if (!this->Generator->GetGeneratorTarget()->IsBundleOnApple()) {
  2122. return;
  2123. }
  2124. std::string macdir =
  2125. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc,
  2126. config);
  2127. // Reject files that collide with files from the Ninja file's native config.
  2128. if (config != this->FileConfig) {
  2129. std::string nativeMacdir =
  2130. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(
  2131. pkgloc, this->FileConfig);
  2132. if (macdir == nativeMacdir) {
  2133. return;
  2134. }
  2135. }
  2136. // Get the input file location.
  2137. std::string input = source.GetFullPath();
  2138. input = this->Generator->GetGlobalGenerator()->ConvertToNinjaPath(input);
  2139. // Get the output file location.
  2140. std::string output =
  2141. cmStrCat(macdir, '/', cmSystemTools::GetFilenameName(input));
  2142. output = this->Generator->GetGlobalGenerator()->ConvertToNinjaPath(output);
  2143. // Write a build statement to copy the content into the bundle.
  2144. this->Generator->GetGlobalGenerator()->WriteMacOSXContentBuild(
  2145. input, output, this->FileConfig);
  2146. // Add as a dependency to the target so that it gets called.
  2147. this->Generator->Configs[config].ExtraFiles.push_back(std::move(output));
  2148. }
  2149. void cmNinjaTargetGenerator::AddDepfileBinding(cmNinjaVars& vars,
  2150. std::string depfile) const
  2151. {
  2152. std::string depfileForShell =
  2153. this->GetLocalGenerator()->ConvertToOutputFormat(depfile,
  2154. cmOutputConverter::SHELL);
  2155. if (depfile != depfileForShell) {
  2156. vars["depfile"] = std::move(depfile);
  2157. }
  2158. vars["DEP_FILE"] = std::move(depfileForShell);
  2159. }
  2160. void cmNinjaTargetGenerator::RemoveDepfileBinding(cmNinjaVars& vars) const
  2161. {
  2162. vars.erase("DEP_FILE");
  2163. vars.erase("depfile");
  2164. }
  2165. void cmNinjaTargetGenerator::addPoolNinjaVariable(
  2166. std::string const& pool_property, cmGeneratorTarget* target,
  2167. cmNinjaVars& vars)
  2168. {
  2169. cmValue pool = target->GetProperty(pool_property);
  2170. if (pool) {
  2171. vars["pool"] = *pool;
  2172. }
  2173. }
  2174. bool cmNinjaTargetGenerator::ForceResponseFile()
  2175. {
  2176. static std::string const forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
  2177. return (this->GetMakefile()->IsDefinitionSet(forceRspFile) ||
  2178. cmSystemTools::HasEnv(forceRspFile));
  2179. }