cmNinjaTargetGenerator.cxx 93 KB

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