cmNinjaTargetGenerator.cxx 93 KB

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