cmNinjaTargetGenerator.cxx 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmNinjaTargetGenerator.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <iterator>
  7. #include <map>
  8. #include <ostream>
  9. #include <utility>
  10. #include <cm/memory>
  11. #include <cmext/algorithm>
  12. #include "cm_jsoncpp_value.h"
  13. #include "cm_jsoncpp_writer.h"
  14. #include "cmComputeLinkInformation.h"
  15. #include "cmCustomCommandGenerator.h"
  16. #include "cmGeneratedFileStream.h"
  17. #include "cmGeneratorExpression.h"
  18. #include "cmGeneratorTarget.h"
  19. #include "cmGlobalNinjaGenerator.h"
  20. #include "cmLocalGenerator.h"
  21. #include "cmLocalNinjaGenerator.h"
  22. #include "cmMakefile.h"
  23. #include "cmNinjaNormalTargetGenerator.h"
  24. #include "cmNinjaUtilityTargetGenerator.h"
  25. #include "cmOutputConverter.h"
  26. #include "cmRange.h"
  27. #include "cmRulePlaceholderExpander.h"
  28. #include "cmSourceFile.h"
  29. #include "cmState.h"
  30. #include "cmStateTypes.h"
  31. #include "cmStringAlgorithms.h"
  32. #include "cmSystemTools.h"
  33. #include "cmake.h"
  34. std::unique_ptr<cmNinjaTargetGenerator> cmNinjaTargetGenerator::New(
  35. cmGeneratorTarget* target)
  36. {
  37. switch (target->GetType()) {
  38. case cmStateEnums::EXECUTABLE:
  39. case cmStateEnums::SHARED_LIBRARY:
  40. case cmStateEnums::STATIC_LIBRARY:
  41. case cmStateEnums::MODULE_LIBRARY:
  42. case cmStateEnums::OBJECT_LIBRARY:
  43. return cm::make_unique<cmNinjaNormalTargetGenerator>(target);
  44. case cmStateEnums::UTILITY:
  45. case cmStateEnums::GLOBAL_TARGET:
  46. return cm::make_unique<cmNinjaUtilityTargetGenerator>(target);
  47. default:
  48. return std::unique_ptr<cmNinjaTargetGenerator>();
  49. }
  50. }
  51. cmNinjaTargetGenerator::cmNinjaTargetGenerator(cmGeneratorTarget* target)
  52. : cmCommonTargetGenerator(target)
  53. , MacOSXContentGenerator(nullptr)
  54. , OSXBundleGenerator(nullptr)
  55. , LocalGenerator(
  56. static_cast<cmLocalNinjaGenerator*>(target->GetLocalGenerator()))
  57. {
  58. MacOSXContentGenerator = cm::make_unique<MacOSXContentGeneratorType>(this);
  59. }
  60. cmNinjaTargetGenerator::~cmNinjaTargetGenerator() = default;
  61. cmGeneratedFileStream& cmNinjaTargetGenerator::GetConfigFileStream(
  62. const std::string& config) const
  63. {
  64. return *this->GetGlobalGenerator()->GetConfigFileStream(config);
  65. }
  66. cmGeneratedFileStream& cmNinjaTargetGenerator::GetCommonFileStream() const
  67. {
  68. return *this->GetGlobalGenerator()->GetCommonFileStream();
  69. }
  70. cmGeneratedFileStream& cmNinjaTargetGenerator::GetRulesFileStream() const
  71. {
  72. return *this->GetGlobalGenerator()->GetRulesFileStream();
  73. }
  74. cmGlobalNinjaGenerator* cmNinjaTargetGenerator::GetGlobalGenerator() const
  75. {
  76. return this->LocalGenerator->GetGlobalNinjaGenerator();
  77. }
  78. std::string cmNinjaTargetGenerator::LanguageCompilerRule(
  79. const std::string& lang, const std::string& config) const
  80. {
  81. return lang + "_COMPILER__" +
  82. cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName()) +
  83. "_" + config;
  84. }
  85. std::string cmNinjaTargetGenerator::LanguagePreprocessRule(
  86. std::string const& lang, const std::string& config) const
  87. {
  88. return lang + "_PREPROCESS__" +
  89. cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName()) +
  90. "_" + config;
  91. }
  92. bool cmNinjaTargetGenerator::NeedExplicitPreprocessing(
  93. std::string const& lang) const
  94. {
  95. return lang == "Fortran";
  96. }
  97. bool cmNinjaTargetGenerator::UsePreprocessedSource(
  98. std::string const& lang) const
  99. {
  100. return lang == "Fortran";
  101. }
  102. bool cmNinjaTargetGenerator::CompilePreprocessedSourceWithDefines(
  103. std::string const& lang) const
  104. {
  105. return this->Makefile->IsOn(
  106. cmStrCat("CMAKE_", lang, "_COMPILE_WITH_DEFINES"));
  107. }
  108. std::string cmNinjaTargetGenerator::LanguageDyndepRule(
  109. const std::string& lang, const std::string& config) const
  110. {
  111. return lang + "_DYNDEP__" +
  112. cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName()) +
  113. "_" + config;
  114. }
  115. bool cmNinjaTargetGenerator::NeedDyndep(std::string const& lang) const
  116. {
  117. return lang == "Fortran";
  118. }
  119. std::string cmNinjaTargetGenerator::OrderDependsTargetForTarget(
  120. const std::string& config)
  121. {
  122. return cmGlobalNinjaGenerator::OrderDependsTargetForTarget(
  123. this->GeneratorTarget, config);
  124. }
  125. // TODO: Most of the code is picked up from
  126. // void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink),
  127. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  128. // Refactor it.
  129. std::string cmNinjaTargetGenerator::ComputeFlagsForObject(
  130. cmSourceFile const* source, const std::string& language,
  131. const std::string& config)
  132. {
  133. std::string flags = this->GetFlags(language, config);
  134. // Add Fortran format flags.
  135. if (language == "Fortran") {
  136. this->AppendFortranFormatFlags(flags, *source);
  137. }
  138. // Add source file specific flags.
  139. cmGeneratorExpressionInterpreter genexInterpreter(
  140. this->LocalGenerator, config, this->GeneratorTarget, language);
  141. const std::string COMPILE_FLAGS("COMPILE_FLAGS");
  142. if (const char* cflags = source->GetProperty(COMPILE_FLAGS)) {
  143. this->LocalGenerator->AppendFlags(
  144. flags, genexInterpreter.Evaluate(cflags, COMPILE_FLAGS));
  145. }
  146. const std::string COMPILE_OPTIONS("COMPILE_OPTIONS");
  147. if (const char* coptions = source->GetProperty(COMPILE_OPTIONS)) {
  148. this->LocalGenerator->AppendCompileOptions(
  149. flags, genexInterpreter.Evaluate(coptions, COMPILE_OPTIONS));
  150. }
  151. // Add precompile headers compile options.
  152. const std::string pchSource =
  153. this->GeneratorTarget->GetPchSource(config, language);
  154. if (!pchSource.empty() && !source->GetProperty("SKIP_PRECOMPILE_HEADERS")) {
  155. std::string pchOptions;
  156. if (source->GetFullPath() == pchSource) {
  157. pchOptions =
  158. this->GeneratorTarget->GetPchCreateCompileOptions(config, language);
  159. } else {
  160. pchOptions =
  161. this->GeneratorTarget->GetPchUseCompileOptions(config, language);
  162. }
  163. this->LocalGenerator->AppendCompileOptions(
  164. flags, genexInterpreter.Evaluate(pchOptions, COMPILE_OPTIONS));
  165. }
  166. return flags;
  167. }
  168. void cmNinjaTargetGenerator::AddIncludeFlags(std::string& languageFlags,
  169. std::string const& language,
  170. const std::string& config)
  171. {
  172. std::vector<std::string> includes;
  173. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  174. language, config);
  175. // Add include directory flags.
  176. std::string includeFlags = this->LocalGenerator->GetIncludeFlags(
  177. includes, this->GeneratorTarget, language,
  178. language == "RC", // full include paths for RC needed by cmcldeps
  179. false, config);
  180. if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
  181. std::replace(includeFlags.begin(), includeFlags.end(), '\\', '/');
  182. }
  183. this->LocalGenerator->AppendFlags(languageFlags, includeFlags);
  184. }
  185. bool cmNinjaTargetGenerator::NeedDepTypeMSVC(const std::string& lang) const
  186. {
  187. std::string const& deptype =
  188. this->GetMakefile()->GetSafeDefinition("CMAKE_NINJA_DEPTYPE_" + lang);
  189. if (deptype == "msvc") {
  190. return true;
  191. }
  192. if (deptype == "intel") {
  193. // Ninja does not really define "intel", but we use it to switch based
  194. // on whether this environment supports "gcc" or "msvc" deptype.
  195. if (!this->GetGlobalGenerator()->SupportsMultilineDepfile()) {
  196. // This ninja version is too old to support the Intel depfile format.
  197. // Fall back to msvc deptype.
  198. return true;
  199. }
  200. if ((this->Makefile->GetHomeDirectory().find(' ') != std::string::npos) ||
  201. (this->Makefile->GetHomeOutputDirectory().find(' ') !=
  202. std::string::npos)) {
  203. // The Intel compiler does not properly escape spaces in a depfile.
  204. // Fall back to msvc deptype.
  205. return true;
  206. }
  207. }
  208. return false;
  209. }
  210. // TODO: Refactor with
  211. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
  212. std::string cmNinjaTargetGenerator::ComputeDefines(cmSourceFile const* source,
  213. const std::string& language,
  214. const std::string& config)
  215. {
  216. std::set<std::string> defines;
  217. cmGeneratorExpressionInterpreter genexInterpreter(
  218. this->LocalGenerator, config, this->GeneratorTarget, language);
  219. // Seriously??
  220. if (this->GetGlobalGenerator()->IsMultiConfig()) {
  221. defines.insert(cmStrCat("CMAKE_INTDIR=\"", config, '"'));
  222. }
  223. const std::string COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
  224. if (const char* compile_defs = source->GetProperty(COMPILE_DEFINITIONS)) {
  225. this->LocalGenerator->AppendDefines(
  226. defines, genexInterpreter.Evaluate(compile_defs, COMPILE_DEFINITIONS));
  227. }
  228. std::string defPropName =
  229. cmStrCat("COMPILE_DEFINITIONS_", cmSystemTools::UpperCase(config));
  230. if (const char* config_compile_defs = source->GetProperty(defPropName)) {
  231. this->LocalGenerator->AppendDefines(
  232. defines,
  233. genexInterpreter.Evaluate(config_compile_defs, COMPILE_DEFINITIONS));
  234. }
  235. std::string definesString = this->GetDefines(language, config);
  236. this->LocalGenerator->JoinDefines(defines, definesString, language);
  237. return definesString;
  238. }
  239. std::string cmNinjaTargetGenerator::ComputeIncludes(
  240. cmSourceFile const* source, const std::string& language,
  241. const std::string& config)
  242. {
  243. std::vector<std::string> includes;
  244. cmGeneratorExpressionInterpreter genexInterpreter(
  245. this->LocalGenerator, config, this->GeneratorTarget, language);
  246. const std::string INCLUDE_DIRECTORIES("INCLUDE_DIRECTORIES");
  247. if (const char* cincludes = source->GetProperty(INCLUDE_DIRECTORIES)) {
  248. this->LocalGenerator->AppendIncludeDirectories(
  249. includes, genexInterpreter.Evaluate(cincludes, INCLUDE_DIRECTORIES),
  250. *source);
  251. }
  252. std::string includesString = this->LocalGenerator->GetIncludeFlags(
  253. includes, this->GeneratorTarget, language, true, false, config);
  254. this->LocalGenerator->AppendFlags(includesString,
  255. this->GetIncludes(language, config));
  256. return includesString;
  257. }
  258. cmNinjaDeps cmNinjaTargetGenerator::ComputeLinkDeps(
  259. const std::string& linkLanguage, const std::string& config) const
  260. {
  261. // Static libraries never depend on other targets for linking.
  262. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  263. this->GeneratorTarget->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  264. return cmNinjaDeps();
  265. }
  266. cmComputeLinkInformation* cli =
  267. this->GeneratorTarget->GetLinkInformation(config);
  268. if (!cli) {
  269. return cmNinjaDeps();
  270. }
  271. const std::vector<std::string>& deps = cli->GetDepends();
  272. cmNinjaDeps result(deps.size());
  273. std::transform(deps.begin(), deps.end(), result.begin(), MapToNinjaPath());
  274. // Add a dependency on the link definitions file, if any.
  275. if (cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  276. this->GeneratorTarget->GetModuleDefinitionInfo(config)) {
  277. for (cmSourceFile const* src : mdi->Sources) {
  278. result.push_back(this->ConvertToNinjaPath(src->GetFullPath()));
  279. }
  280. }
  281. // Add a dependency on user-specified manifest files, if any.
  282. std::vector<cmSourceFile const*> manifest_srcs;
  283. this->GeneratorTarget->GetManifests(manifest_srcs, config);
  284. for (cmSourceFile const* manifest_src : manifest_srcs) {
  285. result.push_back(this->ConvertToNinjaPath(manifest_src->GetFullPath()));
  286. }
  287. // Add user-specified dependencies.
  288. std::vector<std::string> linkDeps;
  289. this->GeneratorTarget->GetLinkDepends(linkDeps, config, linkLanguage);
  290. std::transform(linkDeps.begin(), linkDeps.end(), std::back_inserter(result),
  291. MapToNinjaPath());
  292. return result;
  293. }
  294. std::string cmNinjaTargetGenerator::GetSourceFilePath(
  295. cmSourceFile const* source) const
  296. {
  297. return ConvertToNinjaPath(source->GetFullPath());
  298. }
  299. std::string cmNinjaTargetGenerator::GetObjectFilePath(
  300. cmSourceFile const* source, const std::string& config) const
  301. {
  302. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  303. if (!path.empty()) {
  304. path += "/";
  305. }
  306. std::string const& objectName = this->GeneratorTarget->GetObjectName(source);
  307. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  308. path += this->GetGlobalGenerator()->ConfigDirectory(config);
  309. path += "/";
  310. path += objectName;
  311. return path;
  312. }
  313. std::string cmNinjaTargetGenerator::GetPreprocessedFilePath(
  314. cmSourceFile const* source, const std::string& config) const
  315. {
  316. // Choose an extension to compile already-preprocessed source.
  317. std::string ppExt = source->GetExtension();
  318. if (cmHasLiteralPrefix(ppExt, "F")) {
  319. // Some Fortran compilers automatically enable preprocessing for
  320. // upper-case extensions. Since the source is already preprocessed,
  321. // use a lower-case extension.
  322. ppExt = cmSystemTools::LowerCase(ppExt);
  323. }
  324. if (ppExt == "fpp") {
  325. // Some Fortran compilers automatically enable preprocessing for
  326. // the ".fpp" extension. Since the source is already preprocessed,
  327. // use the ".f" extension.
  328. ppExt = "f";
  329. }
  330. // Take the object file name and replace the extension.
  331. std::string const& objName = this->GeneratorTarget->GetObjectName(source);
  332. std::string const& objExt =
  333. this->GetGlobalGenerator()->GetLanguageOutputExtension(*source);
  334. assert(objName.size() >= objExt.size());
  335. std::string const ppName =
  336. objName.substr(0, objName.size() - objExt.size()) + "-pp." + ppExt;
  337. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  338. if (!path.empty()) {
  339. path += "/";
  340. }
  341. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  342. path += this->GetGlobalGenerator()->ConfigDirectory(config);
  343. path += "/";
  344. path += ppName;
  345. return path;
  346. }
  347. std::string cmNinjaTargetGenerator::GetDyndepFilePath(
  348. std::string const& lang, const std::string& config) const
  349. {
  350. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  351. if (!path.empty()) {
  352. path += "/";
  353. }
  354. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  355. path += this->GetGlobalGenerator()->ConfigDirectory(config);
  356. path += "/";
  357. path += lang;
  358. path += ".dd";
  359. return path;
  360. }
  361. std::string cmNinjaTargetGenerator::GetTargetDependInfoPath(
  362. std::string const& lang, const std::string& config) const
  363. {
  364. std::string path =
  365. cmStrCat(this->Makefile->GetCurrentBinaryDirectory(), '/',
  366. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget),
  367. this->GetGlobalGenerator()->ConfigDirectory(config), '/', lang,
  368. "DependInfo.json");
  369. return path;
  370. }
  371. std::string cmNinjaTargetGenerator::GetTargetOutputDir(
  372. const std::string& config) const
  373. {
  374. std::string dir = this->GeneratorTarget->GetDirectory(config);
  375. return ConvertToNinjaPath(dir);
  376. }
  377. std::string cmNinjaTargetGenerator::GetTargetFilePath(
  378. const std::string& name, const std::string& config) const
  379. {
  380. std::string path = this->GetTargetOutputDir(config);
  381. if (path.empty() || path == ".") {
  382. return name;
  383. }
  384. path += "/";
  385. path += name;
  386. return path;
  387. }
  388. std::string cmNinjaTargetGenerator::GetTargetName() const
  389. {
  390. return this->GeneratorTarget->GetName();
  391. }
  392. bool cmNinjaTargetGenerator::SetMsvcTargetPdbVariable(
  393. cmNinjaVars& vars, const std::string& config) const
  394. {
  395. cmMakefile* mf = this->GetMakefile();
  396. if (mf->GetDefinition("MSVC_C_ARCHITECTURE_ID") ||
  397. mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID") ||
  398. mf->GetDefinition("MSVC_CUDA_ARCHITECTURE_ID")) {
  399. std::string pdbPath;
  400. std::string compilePdbPath = this->ComputeTargetCompilePDB(config);
  401. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE ||
  402. this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  403. this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  404. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  405. pdbPath = cmStrCat(this->GeneratorTarget->GetPDBDirectory(config), '/',
  406. this->GeneratorTarget->GetPDBName(config));
  407. }
  408. vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  409. ConvertToNinjaPath(pdbPath), cmOutputConverter::SHELL);
  410. vars["TARGET_COMPILE_PDB"] =
  411. this->GetLocalGenerator()->ConvertToOutputFormat(
  412. ConvertToNinjaPath(compilePdbPath), cmOutputConverter::SHELL);
  413. EnsureParentDirectoryExists(pdbPath);
  414. EnsureParentDirectoryExists(compilePdbPath);
  415. return true;
  416. }
  417. return false;
  418. }
  419. void cmNinjaTargetGenerator::WriteLanguageRules(const std::string& language,
  420. const std::string& config)
  421. {
  422. #ifdef NINJA_GEN_VERBOSE_FILES
  423. this->GetRulesFileStream() << "# Rules for language " << language << "\n\n";
  424. #endif
  425. this->WriteCompileRule(language, config);
  426. }
  427. void cmNinjaTargetGenerator::WriteCompileRule(const std::string& lang,
  428. const std::string& config)
  429. {
  430. cmRulePlaceholderExpander::RuleVariables vars;
  431. vars.CMTargetName = this->GetGeneratorTarget()->GetName().c_str();
  432. vars.CMTargetType =
  433. cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType());
  434. vars.Language = lang.c_str();
  435. vars.Source = "$in";
  436. vars.Object = "$out";
  437. vars.Defines = "$DEFINES";
  438. vars.Includes = "$INCLUDES";
  439. vars.TargetPDB = "$TARGET_PDB";
  440. vars.TargetCompilePDB = "$TARGET_COMPILE_PDB";
  441. vars.ObjectDir = "$OBJECT_DIR";
  442. vars.ObjectFileDir = "$OBJECT_FILE_DIR";
  443. cmMakefile* mf = this->GetMakefile();
  444. // For some cases we do an explicit preprocessor invocation.
  445. bool const explicitPP = this->NeedExplicitPreprocessing(lang);
  446. bool const compilePPWithDefines = this->UsePreprocessedSource(lang) &&
  447. this->CompilePreprocessedSourceWithDefines(lang);
  448. bool const needDyndep = this->NeedDyndep(lang);
  449. std::string flags = "$FLAGS";
  450. std::string responseFlag;
  451. bool const lang_supports_response = lang != "RC";
  452. if (lang_supports_response && this->ForceResponseFile()) {
  453. std::string const responseFlagVar =
  454. "CMAKE_" + lang + "_RESPONSE_FILE_FLAG";
  455. responseFlag = this->Makefile->GetSafeDefinition(responseFlagVar);
  456. if (responseFlag.empty() && lang != "CUDA") {
  457. responseFlag = "@";
  458. }
  459. }
  460. std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
  461. this->GetLocalGenerator()->CreateRulePlaceholderExpander());
  462. std::string const tdi = this->GetLocalGenerator()->ConvertToOutputFormat(
  463. ConvertToNinjaPath(this->GetTargetDependInfoPath(lang, config)),
  464. cmLocalGenerator::SHELL);
  465. std::string launcher;
  466. const char* val = this->GetLocalGenerator()->GetRuleLauncher(
  467. this->GetGeneratorTarget(), "RULE_LAUNCH_COMPILE");
  468. if (val && *val) {
  469. launcher = cmStrCat(val, ' ');
  470. }
  471. std::string const cmakeCmd =
  472. this->GetLocalGenerator()->ConvertToOutputFormat(
  473. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  474. if (explicitPP) {
  475. cmNinjaRule rule(this->LanguagePreprocessRule(lang, config));
  476. // Explicit preprocessing always uses a depfile.
  477. rule.DepType = ""; // no deps= for multiple outputs
  478. rule.DepFile = "$DEP_FILE";
  479. cmRulePlaceholderExpander::RuleVariables ppVars;
  480. ppVars.CMTargetName = vars.CMTargetName;
  481. ppVars.CMTargetType = vars.CMTargetType;
  482. ppVars.Language = vars.Language;
  483. ppVars.Object = "$out"; // for RULE_LAUNCH_COMPILE
  484. ppVars.PreprocessedSource = "$out";
  485. ppVars.DependencyFile = rule.DepFile.c_str();
  486. // Preprocessing uses the original source,
  487. // compilation uses preprocessed output.
  488. ppVars.Source = vars.Source;
  489. vars.Source = "$in";
  490. // Preprocessing and compilation use the same flags.
  491. std::string ppFlags = flags;
  492. if (!compilePPWithDefines) {
  493. // Move preprocessor definitions to the preprocessor rule.
  494. ppVars.Defines = vars.Defines;
  495. vars.Defines = "";
  496. } else {
  497. // Copy preprocessor definitions to the preprocessor rule.
  498. ppVars.Defines = vars.Defines;
  499. }
  500. // Copy include directories to the preprocessor rule. The Fortran
  501. // compilation rule still needs them for the INCLUDE directive.
  502. ppVars.Includes = vars.Includes;
  503. // If using a response file, move defines, includes, and flags into it.
  504. if (!responseFlag.empty()) {
  505. rule.RspFile = "$RSP_FILE";
  506. rule.RspContent =
  507. cmStrCat(' ', ppVars.Defines, ' ', ppVars.Includes, ' ', ppFlags);
  508. ppFlags = responseFlag + rule.RspFile;
  509. ppVars.Defines = "";
  510. ppVars.Includes = "";
  511. }
  512. ppVars.Flags = ppFlags.c_str();
  513. // Rule for preprocessing source file.
  514. std::vector<std::string> ppCmds;
  515. {
  516. // Lookup the explicit preprocessing rule.
  517. std::string ppVar = cmStrCat("CMAKE_", lang, "_PREPROCESS_SOURCE");
  518. cmExpandList(this->GetMakefile()->GetRequiredDefinition(ppVar), ppCmds);
  519. }
  520. for (std::string& i : ppCmds) {
  521. i = cmStrCat(launcher, i);
  522. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(),
  523. i, ppVars);
  524. }
  525. // Run CMake dependency scanner on preprocessed output.
  526. {
  527. std::string ccmd =
  528. cmStrCat(cmakeCmd, " -E cmake_ninja_depends --tdi=", tdi,
  529. " --lang=", lang, " --pp=$out --dep=$DEP_FILE");
  530. if (needDyndep) {
  531. ccmd += " --obj=$OBJ_FILE --ddi=$DYNDEP_INTERMEDIATE_FILE";
  532. }
  533. ppCmds.emplace_back(std::move(ccmd));
  534. }
  535. rule.Command = this->GetLocalGenerator()->BuildCommandLine(ppCmds);
  536. // Write the rule for preprocessing file of the given language.
  537. rule.Comment = cmStrCat("Rule for preprocessing ", lang, " files.");
  538. rule.Description = cmStrCat("Building ", lang, " preprocessed $out");
  539. this->GetGlobalGenerator()->AddRule(rule);
  540. }
  541. if (needDyndep) {
  542. // Write the rule for ninja dyndep file generation.
  543. cmNinjaRule rule(this->LanguageDyndepRule(lang, config));
  544. // Command line length is almost always limited -> use response file for
  545. // dyndep rules
  546. rule.RspFile = "$out.rsp";
  547. rule.RspContent = "$in";
  548. // Run CMake dependency scanner on the source file (using the preprocessed
  549. // source if that was performed).
  550. {
  551. std::vector<std::string> ddCmds;
  552. {
  553. std::string ccmd =
  554. cmStrCat(cmakeCmd, " -E cmake_ninja_dyndep --tdi=", tdi,
  555. " --lang=", lang, " --dd=$out @", rule.RspFile);
  556. ddCmds.emplace_back(std::move(ccmd));
  557. }
  558. rule.Command = this->GetLocalGenerator()->BuildCommandLine(ddCmds);
  559. }
  560. rule.Comment =
  561. cmStrCat("Rule to generate ninja dyndep files for ", lang, '.');
  562. rule.Description = cmStrCat("Generating ", lang, " dyndep file $out");
  563. this->GetGlobalGenerator()->AddRule(rule);
  564. }
  565. cmNinjaRule rule(this->LanguageCompilerRule(lang, config));
  566. // If using a response file, move defines, includes, and flags into it.
  567. if (!responseFlag.empty()) {
  568. rule.RspFile = "$RSP_FILE";
  569. rule.RspContent =
  570. cmStrCat(' ', vars.Defines, ' ', vars.Includes, ' ', flags);
  571. flags = responseFlag + rule.RspFile;
  572. vars.Defines = "";
  573. vars.Includes = "";
  574. }
  575. // Tell ninja dependency format so all deps can be loaded into a database
  576. std::string cldeps;
  577. if (explicitPP) {
  578. // The explicit preprocessing step will handle dependency scanning.
  579. } else if (this->NeedDepTypeMSVC(lang)) {
  580. rule.DepType = "msvc";
  581. rule.DepFile.clear();
  582. flags += " /showIncludes";
  583. } else if (mf->IsOn("CMAKE_NINJA_CMCLDEPS_" + lang)) {
  584. // For the MS resource compiler we need cmcldeps, but skip dependencies
  585. // for source-file try_compile cases because they are always fresh.
  586. if (!mf->GetIsSourceFileTryCompile()) {
  587. rule.DepType = "gcc";
  588. rule.DepFile = "$DEP_FILE";
  589. const std::string cl = mf->GetDefinition("CMAKE_C_COMPILER")
  590. ? mf->GetSafeDefinition("CMAKE_C_COMPILER")
  591. : mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
  592. cldeps = cmStrCat('"', cmSystemTools::GetCMClDepsCommand(), "\" ", lang,
  593. ' ', vars.Source, " $DEP_FILE $out \"",
  594. mf->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX"),
  595. "\" \"", cl, "\" ");
  596. }
  597. } else {
  598. rule.DepType = "gcc";
  599. rule.DepFile = "$DEP_FILE";
  600. const std::string flagsName = "CMAKE_DEPFILE_FLAGS_" + lang;
  601. std::string depfileFlags = mf->GetSafeDefinition(flagsName);
  602. if (!depfileFlags.empty()) {
  603. cmSystemTools::ReplaceString(depfileFlags, "<DEPFILE>", "$DEP_FILE");
  604. cmSystemTools::ReplaceString(depfileFlags, "<OBJECT>", "$out");
  605. cmSystemTools::ReplaceString(depfileFlags, "<CMAKE_C_COMPILER>",
  606. mf->GetDefinition("CMAKE_C_COMPILER"));
  607. flags += " " + depfileFlags;
  608. }
  609. }
  610. vars.Flags = flags.c_str();
  611. vars.DependencyFile = rule.DepFile.c_str();
  612. // Rule for compiling object file.
  613. std::vector<std::string> compileCmds;
  614. if (lang == "CUDA") {
  615. std::string cmdVar;
  616. if (this->GeneratorTarget->GetPropertyAsBool(
  617. "CUDA_SEPARABLE_COMPILATION")) {
  618. cmdVar = "CMAKE_CUDA_COMPILE_SEPARABLE_COMPILATION";
  619. } else if (this->GeneratorTarget->GetPropertyAsBool(
  620. "CUDA_PTX_COMPILATION")) {
  621. cmdVar = "CMAKE_CUDA_COMPILE_PTX_COMPILATION";
  622. } else {
  623. cmdVar = "CMAKE_CUDA_COMPILE_WHOLE_COMPILATION";
  624. }
  625. const std::string& compileCmd = mf->GetRequiredDefinition(cmdVar);
  626. cmExpandList(compileCmd, compileCmds);
  627. } else {
  628. const std::string cmdVar = "CMAKE_" + lang + "_COMPILE_OBJECT";
  629. const std::string& compileCmd = mf->GetRequiredDefinition(cmdVar);
  630. cmExpandList(compileCmd, compileCmds);
  631. }
  632. // See if we need to use a compiler launcher like ccache or distcc
  633. std::string compilerLauncher;
  634. if (!compileCmds.empty() &&
  635. (lang == "C" || lang == "CXX" || lang == "Fortran" || lang == "CUDA" ||
  636. lang == "OBJC" || lang == "OBJCXX")) {
  637. std::string const clauncher_prop = lang + "_COMPILER_LAUNCHER";
  638. const char* clauncher = this->GeneratorTarget->GetProperty(clauncher_prop);
  639. if (clauncher && *clauncher) {
  640. compilerLauncher = clauncher;
  641. }
  642. }
  643. // Maybe insert an include-what-you-use runner.
  644. if (!compileCmds.empty() && (lang == "C" || lang == "CXX")) {
  645. std::string const iwyu_prop = lang + "_INCLUDE_WHAT_YOU_USE";
  646. const char* iwyu = this->GeneratorTarget->GetProperty(iwyu_prop);
  647. std::string const tidy_prop = lang + "_CLANG_TIDY";
  648. const char* tidy = this->GeneratorTarget->GetProperty(tidy_prop);
  649. std::string const cpplint_prop = lang + "_CPPLINT";
  650. const char* cpplint = this->GeneratorTarget->GetProperty(cpplint_prop);
  651. std::string const cppcheck_prop = lang + "_CPPCHECK";
  652. const char* cppcheck = this->GeneratorTarget->GetProperty(cppcheck_prop);
  653. if ((iwyu && *iwyu) || (tidy && *tidy) || (cpplint && *cpplint) ||
  654. (cppcheck && *cppcheck)) {
  655. std::string run_iwyu = cmStrCat(cmakeCmd, " -E __run_co_compile");
  656. if (!compilerLauncher.empty()) {
  657. // In __run_co_compile case the launcher command is supplied
  658. // via --launcher=<maybe-list> and consumed
  659. run_iwyu += " --launcher=";
  660. run_iwyu += this->LocalGenerator->EscapeForShell(compilerLauncher);
  661. compilerLauncher.clear();
  662. }
  663. if (iwyu && *iwyu) {
  664. run_iwyu += " --iwyu=";
  665. run_iwyu += this->GetLocalGenerator()->EscapeForShell(iwyu);
  666. }
  667. if (tidy && *tidy) {
  668. run_iwyu += " --tidy=";
  669. run_iwyu += this->GetLocalGenerator()->EscapeForShell(tidy);
  670. }
  671. if (cpplint && *cpplint) {
  672. run_iwyu += " --cpplint=";
  673. run_iwyu += this->GetLocalGenerator()->EscapeForShell(cpplint);
  674. }
  675. if (cppcheck && *cppcheck) {
  676. run_iwyu += " --cppcheck=";
  677. run_iwyu += this->GetLocalGenerator()->EscapeForShell(cppcheck);
  678. }
  679. if ((tidy && *tidy) || (cpplint && *cpplint) ||
  680. (cppcheck && *cppcheck)) {
  681. run_iwyu += " --source=$in";
  682. }
  683. run_iwyu += " -- ";
  684. compileCmds.front().insert(0, run_iwyu);
  685. }
  686. }
  687. // If compiler launcher was specified and not consumed above, it
  688. // goes to the beginning of the command line.
  689. if (!compileCmds.empty() && !compilerLauncher.empty()) {
  690. std::vector<std::string> args = cmExpandedList(compilerLauncher, true);
  691. if (!args.empty()) {
  692. args[0] = this->LocalGenerator->ConvertToOutputFormat(
  693. args[0], cmOutputConverter::SHELL);
  694. for (std::string& i : cmMakeRange(args.begin() + 1, args.end())) {
  695. i = this->LocalGenerator->EscapeForShell(i);
  696. }
  697. }
  698. compileCmds.front().insert(0, cmJoin(args, " ") + " ");
  699. }
  700. if (!compileCmds.empty()) {
  701. compileCmds.front().insert(0, cldeps);
  702. }
  703. for (std::string& i : compileCmds) {
  704. i = cmStrCat(launcher, i);
  705. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(), i,
  706. vars);
  707. }
  708. rule.Command = this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  709. // Write the rule for compiling file of the given language.
  710. rule.Comment = cmStrCat("Rule for compiling ", lang, " files.");
  711. rule.Description = cmStrCat("Building ", lang, " object $out");
  712. this->GetGlobalGenerator()->AddRule(rule);
  713. }
  714. void cmNinjaTargetGenerator::WriteObjectBuildStatements(
  715. const std::string& config, const std::string& fileConfig,
  716. bool firstForConfig)
  717. {
  718. // Write comments.
  719. cmGlobalNinjaGenerator::WriteDivider(this->GetConfigFileStream(fileConfig));
  720. this->GetConfigFileStream(fileConfig)
  721. << "# Object build statements for "
  722. << cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType())
  723. << " target " << this->GetTargetName() << "\n\n";
  724. {
  725. std::vector<cmSourceFile const*> customCommands;
  726. this->GeneratorTarget->GetCustomCommands(customCommands, config);
  727. for (cmSourceFile const* sf : customCommands) {
  728. cmCustomCommand const* cc = sf->GetCustomCommand();
  729. this->GetLocalGenerator()->AddCustomCommandTarget(
  730. cc, this->GetGeneratorTarget());
  731. // Record the custom commands for this target. The container is used
  732. // in WriteObjectBuildStatement when called in a loop below.
  733. this->Configs[config].CustomCommands.push_back(cc);
  734. }
  735. }
  736. {
  737. std::vector<cmSourceFile const*> headerSources;
  738. this->GeneratorTarget->GetHeaderSources(headerSources, config);
  739. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  740. headerSources, this->MacOSXContentGenerator.get(), config);
  741. }
  742. {
  743. std::vector<cmSourceFile const*> extraSources;
  744. this->GeneratorTarget->GetExtraSources(extraSources, config);
  745. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  746. extraSources, this->MacOSXContentGenerator.get(), config);
  747. }
  748. if (firstForConfig) {
  749. const char* pchExtension =
  750. GetMakefile()->GetDefinition("CMAKE_PCH_EXTENSION");
  751. std::vector<cmSourceFile const*> externalObjects;
  752. this->GeneratorTarget->GetExternalObjects(externalObjects, config);
  753. for (cmSourceFile const* sf : externalObjects) {
  754. auto objectFileName = this->GetGlobalGenerator()->ExpandCFGIntDir(
  755. this->GetSourceFilePath(sf), config);
  756. if (!cmSystemTools::StringEndsWith(objectFileName, pchExtension)) {
  757. this->Configs[config].Objects.push_back(objectFileName);
  758. }
  759. }
  760. }
  761. {
  762. cmNinjaBuild build("phony");
  763. build.Comment = "Order-only phony target for " + this->GetTargetName();
  764. build.Outputs.push_back(this->OrderDependsTargetForTarget(config));
  765. cmNinjaDeps& orderOnlyDeps = build.OrderOnlyDeps;
  766. this->GetLocalGenerator()->AppendTargetDepends(
  767. this->GeneratorTarget, orderOnlyDeps, config, fileConfig,
  768. DependOnTargetOrdering);
  769. // Add order-only dependencies on other files associated with the target.
  770. cm::append(orderOnlyDeps, this->Configs[config].ExtraFiles);
  771. // Add order-only dependencies on custom command outputs.
  772. for (cmCustomCommand const* cc : this->Configs[config].CustomCommands) {
  773. cmCustomCommandGenerator ccg(*cc, config, this->GetLocalGenerator());
  774. const std::vector<std::string>& ccoutputs = ccg.GetOutputs();
  775. const std::vector<std::string>& ccbyproducts = ccg.GetByproducts();
  776. std::transform(ccoutputs.begin(), ccoutputs.end(),
  777. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  778. std::transform(ccbyproducts.begin(), ccbyproducts.end(),
  779. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  780. }
  781. std::sort(orderOnlyDeps.begin(), orderOnlyDeps.end());
  782. orderOnlyDeps.erase(
  783. std::unique(orderOnlyDeps.begin(), orderOnlyDeps.end()),
  784. orderOnlyDeps.end());
  785. // The phony target must depend on at least one input or ninja will explain
  786. // that "output ... of phony edge with no inputs doesn't exist" and
  787. // consider the phony output "dirty".
  788. if (orderOnlyDeps.empty()) {
  789. // Any path that always exists will work here. It would be nice to
  790. // use just "." but that is not supported by Ninja < 1.7.
  791. std::string tgtDir = cmStrCat(
  792. this->LocalGenerator->GetCurrentBinaryDirectory(), '/',
  793. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget));
  794. orderOnlyDeps.push_back(this->ConvertToNinjaPath(tgtDir));
  795. }
  796. this->GetGlobalGenerator()->WriteBuild(
  797. this->GetConfigFileStream(fileConfig), build);
  798. }
  799. {
  800. std::vector<cmSourceFile const*> objectSources;
  801. this->GeneratorTarget->GetObjectSources(objectSources, config);
  802. for (cmSourceFile const* sf : objectSources) {
  803. this->WriteObjectBuildStatement(sf, config, fileConfig, firstForConfig);
  804. }
  805. }
  806. for (auto const& langDDIFiles : this->Configs[config].DDIFiles) {
  807. std::string const& language = langDDIFiles.first;
  808. cmNinjaDeps const& ddiFiles = langDDIFiles.second;
  809. cmNinjaBuild build(this->LanguageDyndepRule(language, config));
  810. build.Outputs.push_back(this->GetDyndepFilePath(language, config));
  811. build.ExplicitDeps = ddiFiles;
  812. this->WriteTargetDependInfo(language, config);
  813. // Make sure dyndep files for all our dependencies have already
  814. // been generated so that the '<LANG>Modules.json' files they
  815. // produced as side-effects are available for us to read.
  816. // Ideally we should depend on the '<LANG>Modules.json' files
  817. // from our dependencies directly, but we don't know which of
  818. // our dependencies produces them. Fixing this will require
  819. // refactoring the Ninja generator to generate targets in
  820. // dependency order so that we can collect the needed information.
  821. this->GetLocalGenerator()->AppendTargetDepends(
  822. this->GeneratorTarget, build.OrderOnlyDeps, config, fileConfig,
  823. DependOnTargetArtifact);
  824. this->GetGlobalGenerator()->WriteBuild(
  825. this->GetConfigFileStream(fileConfig), build);
  826. }
  827. this->GetConfigFileStream(fileConfig) << "\n";
  828. if (!this->Configs[config].SwiftOutputMap.empty()) {
  829. std::string const mapFilePath =
  830. this->GeneratorTarget->GetSupportDirectory() + "/output-file-map.json";
  831. std::string const targetSwiftDepsPath = [this, config]() -> std::string {
  832. cmGeneratorTarget const* target = this->GeneratorTarget;
  833. if (const char* name = target->GetProperty("Swift_DEPENDENCIES_FILE")) {
  834. return name;
  835. }
  836. return this->ConvertToNinjaPath(target->GetSupportDirectory() + "/" +
  837. config + "/" + target->GetName() +
  838. ".swiftdeps");
  839. }();
  840. // build the global target dependencies
  841. // https://github.com/apple/swift/blob/master/docs/Driver.md#output-file-maps
  842. Json::Value deps(Json::objectValue);
  843. deps["swift-dependencies"] = targetSwiftDepsPath;
  844. this->Configs[config].SwiftOutputMap[""] = deps;
  845. cmGeneratedFileStream output(mapFilePath);
  846. output << this->Configs[config].SwiftOutputMap;
  847. }
  848. }
  849. void cmNinjaTargetGenerator::WriteObjectBuildStatement(
  850. cmSourceFile const* source, const std::string& config,
  851. const std::string& fileConfig, bool firstForConfig)
  852. {
  853. std::string const language = source->GetLanguage();
  854. std::string const sourceFileName =
  855. language == "RC" ? source->GetFullPath() : this->GetSourceFilePath(source);
  856. std::string const objectDir = this->ConvertToNinjaPath(
  857. cmStrCat(this->GeneratorTarget->GetSupportDirectory(),
  858. this->GetGlobalGenerator()->ConfigDirectory(config)));
  859. std::string const objectFileName =
  860. this->ConvertToNinjaPath(this->GetObjectFilePath(source, config));
  861. std::string const objectFileDir =
  862. cmSystemTools::GetFilenamePath(objectFileName);
  863. std::string cmakeVarLang = cmStrCat("CMAKE_", language);
  864. // build response file name
  865. std::string cmakeLinkVar = cmakeVarLang + "_RESPONSE_FILE_FLAG";
  866. const char* flag = GetMakefile()->GetDefinition(cmakeLinkVar);
  867. bool const lang_supports_response =
  868. !(language == "RC" || (language == "CUDA" && !flag));
  869. int const commandLineLengthLimit =
  870. ((lang_supports_response && this->ForceResponseFile())) ? -1 : 0;
  871. cmNinjaBuild objBuild(this->LanguageCompilerRule(language, config));
  872. cmNinjaVars& vars = objBuild.Variables;
  873. vars["FLAGS"] = this->ComputeFlagsForObject(source, language, config);
  874. vars["DEFINES"] = this->ComputeDefines(source, language, config);
  875. vars["INCLUDES"] = this->ComputeIncludes(source, language, config);
  876. if (!this->NeedDepTypeMSVC(language)) {
  877. bool replaceExt(false);
  878. if (!language.empty()) {
  879. std::string repVar =
  880. cmStrCat("CMAKE_", language, "_DEPFILE_EXTENSION_REPLACE");
  881. replaceExt = this->Makefile->IsOn(repVar);
  882. }
  883. if (!replaceExt) {
  884. // use original code
  885. vars["DEP_FILE"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  886. objectFileName + ".d", cmOutputConverter::SHELL);
  887. } else {
  888. // Replace the original source file extension with the
  889. // depend file extension.
  890. std::string dependFileName =
  891. cmSystemTools::GetFilenameWithoutLastExtension(objectFileName) + ".d";
  892. vars["DEP_FILE"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  893. objectFileDir + "/" + dependFileName, cmOutputConverter::SHELL);
  894. }
  895. }
  896. this->ExportObjectCompileCommand(
  897. language, sourceFileName, objectDir, objectFileName, objectFileDir,
  898. vars["FLAGS"], vars["DEFINES"], vars["INCLUDES"]);
  899. objBuild.Outputs.push_back(objectFileName);
  900. if (firstForConfig) {
  901. const char* pchExtension =
  902. this->GetMakefile()->GetDefinition("CMAKE_PCH_EXTENSION");
  903. if (!cmSystemTools::StringEndsWith(objectFileName, pchExtension)) {
  904. // Add this object to the list of object files.
  905. this->Configs[config].Objects.push_back(objectFileName);
  906. }
  907. }
  908. objBuild.ExplicitDeps.push_back(sourceFileName);
  909. // Add precompile headers dependencies
  910. std::vector<std::string> depList;
  911. const std::string pchSource =
  912. this->GeneratorTarget->GetPchSource(config, language);
  913. if (!pchSource.empty() && !source->GetProperty("SKIP_PRECOMPILE_HEADERS")) {
  914. depList.push_back(this->GeneratorTarget->GetPchHeader(config, language));
  915. if (source->GetFullPath() != pchSource) {
  916. depList.push_back(this->GeneratorTarget->GetPchFile(config, language));
  917. }
  918. }
  919. if (const char* objectDeps = source->GetProperty("OBJECT_DEPENDS")) {
  920. std::vector<std::string> objDepList = cmExpandedList(objectDeps);
  921. std::copy(objDepList.begin(), objDepList.end(),
  922. std::back_inserter(depList));
  923. }
  924. if (!depList.empty()) {
  925. for (std::string& odi : depList) {
  926. if (cmSystemTools::FileIsFullPath(odi)) {
  927. odi = cmSystemTools::CollapseFullPath(odi);
  928. }
  929. }
  930. std::transform(depList.begin(), depList.end(),
  931. std::back_inserter(objBuild.ImplicitDeps),
  932. MapToNinjaPath());
  933. }
  934. objBuild.OrderOnlyDeps.push_back(this->OrderDependsTargetForTarget(config));
  935. // If the source file is GENERATED and does not have a custom command
  936. // (either attached to this source file or another one), assume that one of
  937. // the target dependencies, OBJECT_DEPENDS or header file custom commands
  938. // will rebuild the file.
  939. if (source->GetIsGenerated() &&
  940. !source->GetPropertyAsBool("__CMAKE_GENERATED_BY_CMAKE") &&
  941. !source->GetCustomCommand() &&
  942. !this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFileName)) {
  943. this->GetGlobalGenerator()->AddAssumedSourceDependencies(
  944. sourceFileName, objBuild.OrderOnlyDeps);
  945. }
  946. // For some cases we need to generate a ninja dyndep file.
  947. bool const needDyndep = this->NeedDyndep(language);
  948. // For some cases we do an explicit preprocessor invocation.
  949. bool const explicitPP = this->NeedExplicitPreprocessing(language);
  950. if (explicitPP) {
  951. cmNinjaBuild ppBuild(this->LanguagePreprocessRule(language, config));
  952. std::string const ppFileName =
  953. this->ConvertToNinjaPath(this->GetPreprocessedFilePath(source, config));
  954. ppBuild.Outputs.push_back(ppFileName);
  955. ppBuild.RspFile = ppFileName + ".rsp";
  956. bool const compilePP = this->UsePreprocessedSource(language);
  957. bool const compilePPWithDefines =
  958. compilePP && this->CompilePreprocessedSourceWithDefines(language);
  959. if (compilePP) {
  960. // Move compilation dependencies to the preprocessing build statement.
  961. std::swap(ppBuild.ExplicitDeps, objBuild.ExplicitDeps);
  962. std::swap(ppBuild.ImplicitDeps, objBuild.ImplicitDeps);
  963. std::swap(ppBuild.OrderOnlyDeps, objBuild.OrderOnlyDeps);
  964. std::swap(ppBuild.Variables["IN_ABS"], vars["IN_ABS"]);
  965. // The actual compilation will now use the preprocessed source.
  966. objBuild.ExplicitDeps.push_back(ppFileName);
  967. } else {
  968. // Copy compilation dependencies to the preprocessing build statement.
  969. ppBuild.ExplicitDeps = objBuild.ExplicitDeps;
  970. ppBuild.ImplicitDeps = objBuild.ImplicitDeps;
  971. ppBuild.OrderOnlyDeps = objBuild.OrderOnlyDeps;
  972. ppBuild.Variables["IN_ABS"] = vars["IN_ABS"];
  973. }
  974. // Preprocessing and compilation generally use the same flags.
  975. ppBuild.Variables["FLAGS"] = vars["FLAGS"];
  976. if (compilePP) {
  977. // In case compilation requires flags that are incompatible with
  978. // preprocessing, include them here.
  979. std::string const& postFlag = this->Makefile->GetSafeDefinition(
  980. "CMAKE_" + language + "_POSTPROCESS_FLAG");
  981. this->LocalGenerator->AppendFlags(vars["FLAGS"], postFlag);
  982. }
  983. if (compilePP && !compilePPWithDefines) {
  984. // Move preprocessor definitions to the preprocessor build statement.
  985. std::swap(ppBuild.Variables["DEFINES"], vars["DEFINES"]);
  986. } else {
  987. // Copy preprocessor definitions to the preprocessor build statement.
  988. ppBuild.Variables["DEFINES"] = vars["DEFINES"];
  989. }
  990. // Copy include directories to the preprocessor build statement. The
  991. // Fortran compilation build statement still needs them for the INCLUDE
  992. // directive.
  993. ppBuild.Variables["INCLUDES"] = vars["INCLUDES"];
  994. if (compilePP) {
  995. // Prepend source file's original directory as an include directory
  996. // so e.g. Fortran INCLUDE statements can look for files in it.
  997. std::vector<std::string> sourceDirectory;
  998. sourceDirectory.push_back(
  999. cmSystemTools::GetParentDirectory(source->GetFullPath()));
  1000. std::string sourceDirectoryFlag = this->LocalGenerator->GetIncludeFlags(
  1001. sourceDirectory, this->GeneratorTarget, language, false, false,
  1002. config);
  1003. vars["INCLUDES"] = sourceDirectoryFlag + " " + vars["INCLUDES"];
  1004. }
  1005. // Explicit preprocessing always uses a depfile.
  1006. ppBuild.Variables["DEP_FILE"] =
  1007. this->GetLocalGenerator()->ConvertToOutputFormat(
  1008. objectFileName + ".pp.d", cmOutputConverter::SHELL);
  1009. if (compilePP) {
  1010. // The actual compilation does not need a depfile because it
  1011. // depends on the already-preprocessed source.
  1012. vars.erase("DEP_FILE");
  1013. }
  1014. if (needDyndep) {
  1015. // Tell dependency scanner the object file that will result from
  1016. // compiling the source.
  1017. ppBuild.Variables["OBJ_FILE"] = objectFileName;
  1018. // Tell dependency scanner where to store dyndep intermediate results.
  1019. std::string const ddiFile = objectFileName + ".ddi";
  1020. ppBuild.Variables["DYNDEP_INTERMEDIATE_FILE"] = ddiFile;
  1021. ppBuild.ImplicitOuts.push_back(ddiFile);
  1022. if (firstForConfig) {
  1023. this->Configs[config].DDIFiles[language].push_back(ddiFile);
  1024. }
  1025. }
  1026. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
  1027. ppBuild.Variables);
  1028. this->GetGlobalGenerator()->WriteBuild(
  1029. this->GetConfigFileStream(fileConfig), ppBuild, commandLineLengthLimit);
  1030. }
  1031. if (needDyndep) {
  1032. std::string const dyndep = this->GetDyndepFilePath(language, config);
  1033. objBuild.OrderOnlyDeps.push_back(dyndep);
  1034. vars["dyndep"] = dyndep;
  1035. }
  1036. EnsureParentDirectoryExists(objectFileName);
  1037. vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  1038. objectDir, cmOutputConverter::SHELL);
  1039. vars["OBJECT_FILE_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  1040. objectFileDir, cmOutputConverter::SHELL);
  1041. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
  1042. vars);
  1043. if (!pchSource.empty() && !source->GetProperty("SKIP_PRECOMPILE_HEADERS")) {
  1044. if (source->GetFullPath() == pchSource) {
  1045. this->addPoolNinjaVariable("JOB_POOL_PRECOMPILE_HEADER",
  1046. this->GetGeneratorTarget(), vars);
  1047. }
  1048. }
  1049. this->SetMsvcTargetPdbVariable(vars, config);
  1050. objBuild.RspFile = objectFileName + ".rsp";
  1051. if (language == "Swift") {
  1052. this->EmitSwiftDependencyInfo(source, config);
  1053. } else {
  1054. this->GetGlobalGenerator()->WriteBuild(
  1055. this->GetConfigFileStream(fileConfig), objBuild, commandLineLengthLimit);
  1056. }
  1057. if (const char* objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
  1058. cmNinjaBuild build("phony");
  1059. build.Comment = "Additional output files.";
  1060. build.Outputs = cmExpandedList(objectOutputs);
  1061. std::transform(build.Outputs.begin(), build.Outputs.end(),
  1062. build.Outputs.begin(), MapToNinjaPath());
  1063. build.ExplicitDeps = objBuild.Outputs;
  1064. this->GetGlobalGenerator()->WriteBuild(
  1065. this->GetConfigFileStream(fileConfig), build);
  1066. }
  1067. }
  1068. void cmNinjaTargetGenerator::WriteTargetDependInfo(std::string const& lang,
  1069. const std::string& config)
  1070. {
  1071. Json::Value tdi(Json::objectValue);
  1072. tdi["language"] = lang;
  1073. tdi["compiler-id"] =
  1074. this->Makefile->GetSafeDefinition("CMAKE_" + lang + "_COMPILER_ID");
  1075. if (lang == "Fortran") {
  1076. std::string mod_dir = this->GeneratorTarget->GetFortranModuleDirectory(
  1077. this->Makefile->GetHomeOutputDirectory());
  1078. if (mod_dir.empty()) {
  1079. mod_dir = this->Makefile->GetCurrentBinaryDirectory();
  1080. }
  1081. tdi["module-dir"] = mod_dir;
  1082. tdi["submodule-sep"] =
  1083. this->Makefile->GetSafeDefinition("CMAKE_Fortran_SUBMODULE_SEP");
  1084. tdi["submodule-ext"] =
  1085. this->Makefile->GetSafeDefinition("CMAKE_Fortran_SUBMODULE_EXT");
  1086. }
  1087. tdi["dir-cur-bld"] = this->Makefile->GetCurrentBinaryDirectory();
  1088. tdi["dir-cur-src"] = this->Makefile->GetCurrentSourceDirectory();
  1089. tdi["dir-top-bld"] = this->Makefile->GetHomeOutputDirectory();
  1090. tdi["dir-top-src"] = this->Makefile->GetHomeDirectory();
  1091. Json::Value& tdi_include_dirs = tdi["include-dirs"] = Json::arrayValue;
  1092. std::vector<std::string> includes;
  1093. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  1094. lang, config);
  1095. for (std::string const& i : includes) {
  1096. // Convert the include directories the same way we do for -I flags.
  1097. // See upstream ninja issue 1251.
  1098. tdi_include_dirs.append(this->ConvertToNinjaPath(i));
  1099. }
  1100. Json::Value& tdi_linked_target_dirs = tdi["linked-target-dirs"] =
  1101. Json::arrayValue;
  1102. for (std::string const& l : this->GetLinkedTargetDirectories(config)) {
  1103. tdi_linked_target_dirs.append(l);
  1104. }
  1105. std::string const tdin = this->GetTargetDependInfoPath(lang, config);
  1106. cmGeneratedFileStream tdif(tdin);
  1107. tdif << tdi;
  1108. }
  1109. void cmNinjaTargetGenerator::EmitSwiftDependencyInfo(
  1110. cmSourceFile const* source, const std::string& config)
  1111. {
  1112. std::string const sourceFilePath =
  1113. this->ConvertToNinjaPath(this->GetSourceFilePath(source));
  1114. std::string const objectFilePath =
  1115. this->ConvertToNinjaPath(this->GetObjectFilePath(source, config));
  1116. std::string const swiftDepsPath = [source, objectFilePath]() -> std::string {
  1117. if (const char* name = source->GetProperty("Swift_DEPENDENCIES_FILE")) {
  1118. return name;
  1119. }
  1120. return objectFilePath + ".swiftdeps";
  1121. }();
  1122. std::string const swiftDiaPath = [source, objectFilePath]() -> std::string {
  1123. if (const char* name = source->GetProperty("Swift_DIAGNOSTICS_FILE")) {
  1124. return name;
  1125. }
  1126. return objectFilePath + ".dia";
  1127. }();
  1128. std::string const makeDepsPath = [this, source, config]() -> std::string {
  1129. cmLocalNinjaGenerator const* local = this->GetLocalGenerator();
  1130. std::string const objectFileName =
  1131. this->ConvertToNinjaPath(this->GetObjectFilePath(source, config));
  1132. std::string const objectFileDir =
  1133. cmSystemTools::GetFilenamePath(objectFileName);
  1134. if (this->Makefile->IsOn("CMAKE_Swift_DEPFLE_EXTNSION_REPLACE")) {
  1135. std::string dependFileName =
  1136. cmSystemTools::GetFilenameWithoutLastExtension(objectFileName) + ".d";
  1137. return local->ConvertToOutputFormat(objectFileDir + "/" + dependFileName,
  1138. cmOutputConverter::SHELL);
  1139. }
  1140. return local->ConvertToOutputFormat(objectFileName + ".d",
  1141. cmOutputConverter::SHELL);
  1142. }();
  1143. // build the source file mapping
  1144. // https://github.com/apple/swift/blob/master/docs/Driver.md#output-file-maps
  1145. Json::Value entry = Json::Value(Json::objectValue);
  1146. entry["object"] = objectFilePath;
  1147. entry["dependencies"] = makeDepsPath;
  1148. entry["swift-dependencies"] = swiftDepsPath;
  1149. entry["diagnostics"] = swiftDiaPath;
  1150. this->Configs[config].SwiftOutputMap[sourceFilePath] = entry;
  1151. }
  1152. void cmNinjaTargetGenerator::ExportObjectCompileCommand(
  1153. std::string const& language, std::string const& sourceFileName,
  1154. std::string const& objectDir, std::string const& objectFileName,
  1155. std::string const& objectFileDir, std::string const& flags,
  1156. std::string const& defines, std::string const& includes)
  1157. {
  1158. if (!this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS")) {
  1159. return;
  1160. }
  1161. cmRulePlaceholderExpander::RuleVariables compileObjectVars;
  1162. compileObjectVars.Language = language.c_str();
  1163. std::string escapedSourceFileName = sourceFileName;
  1164. if (!cmSystemTools::FileIsFullPath(sourceFileName)) {
  1165. escapedSourceFileName =
  1166. cmSystemTools::CollapseFullPath(escapedSourceFileName,
  1167. this->GetGlobalGenerator()
  1168. ->GetCMakeInstance()
  1169. ->GetHomeOutputDirectory());
  1170. }
  1171. escapedSourceFileName = this->LocalGenerator->ConvertToOutputFormat(
  1172. escapedSourceFileName, cmOutputConverter::SHELL);
  1173. compileObjectVars.Source = escapedSourceFileName.c_str();
  1174. compileObjectVars.Object = objectFileName.c_str();
  1175. compileObjectVars.ObjectDir = objectDir.c_str();
  1176. compileObjectVars.ObjectFileDir = objectFileDir.c_str();
  1177. compileObjectVars.Flags = flags.c_str();
  1178. compileObjectVars.Defines = defines.c_str();
  1179. compileObjectVars.Includes = includes.c_str();
  1180. // Rule for compiling object file.
  1181. std::vector<std::string> compileCmds;
  1182. if (language == "CUDA") {
  1183. std::string cmdVar;
  1184. if (this->GeneratorTarget->GetPropertyAsBool(
  1185. "CUDA_SEPARABLE_COMPILATION")) {
  1186. cmdVar = "CMAKE_CUDA_COMPILE_SEPARABLE_COMPILATION";
  1187. } else if (this->GeneratorTarget->GetPropertyAsBool(
  1188. "CUDA_PTX_COMPILATION")) {
  1189. cmdVar = "CMAKE_CUDA_COMPILE_PTX_COMPILATION";
  1190. } else {
  1191. cmdVar = "CMAKE_CUDA_COMPILE_WHOLE_COMPILATION";
  1192. }
  1193. const std::string& compileCmd =
  1194. this->GetMakefile()->GetRequiredDefinition(cmdVar);
  1195. cmExpandList(compileCmd, compileCmds);
  1196. } else {
  1197. const std::string cmdVar = "CMAKE_" + language + "_COMPILE_OBJECT";
  1198. const std::string& compileCmd =
  1199. this->GetMakefile()->GetRequiredDefinition(cmdVar);
  1200. cmExpandList(compileCmd, compileCmds);
  1201. }
  1202. std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
  1203. this->GetLocalGenerator()->CreateRulePlaceholderExpander());
  1204. for (std::string& i : compileCmds) {
  1205. // no launcher for CMAKE_EXPORT_COMPILE_COMMANDS
  1206. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(), i,
  1207. compileObjectVars);
  1208. }
  1209. std::string cmdLine =
  1210. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  1211. this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine, sourceFileName);
  1212. }
  1213. void cmNinjaTargetGenerator::AdditionalCleanFiles(const std::string& config)
  1214. {
  1215. if (const char* prop_value =
  1216. this->GeneratorTarget->GetProperty("ADDITIONAL_CLEAN_FILES")) {
  1217. cmLocalNinjaGenerator* lg = this->LocalGenerator;
  1218. std::vector<std::string> cleanFiles;
  1219. cmExpandList(cmGeneratorExpression::Evaluate(prop_value, lg, config,
  1220. this->GeneratorTarget),
  1221. cleanFiles);
  1222. std::string const& binaryDir = lg->GetCurrentBinaryDirectory();
  1223. cmGlobalNinjaGenerator* gg = lg->GetGlobalNinjaGenerator();
  1224. for (std::string const& cleanFile : cleanFiles) {
  1225. // Support relative paths
  1226. gg->AddAdditionalCleanFile(
  1227. cmSystemTools::CollapseFullPath(cleanFile, binaryDir), config);
  1228. }
  1229. }
  1230. }
  1231. cmNinjaDeps cmNinjaTargetGenerator::GetObjects(const std::string& config) const
  1232. {
  1233. auto const it = this->Configs.find(config);
  1234. if (it != this->Configs.end()) {
  1235. return it->second.Objects;
  1236. }
  1237. return {};
  1238. }
  1239. void cmNinjaTargetGenerator::EnsureDirectoryExists(
  1240. const std::string& path) const
  1241. {
  1242. if (cmSystemTools::FileIsFullPath(path)) {
  1243. cmSystemTools::MakeDirectory(path);
  1244. } else {
  1245. cmGlobalNinjaGenerator* gg = this->GetGlobalGenerator();
  1246. std::string fullPath = gg->GetCMakeInstance()->GetHomeOutputDirectory();
  1247. // Also ensures their is a trailing slash.
  1248. gg->StripNinjaOutputPathPrefixAsSuffix(fullPath);
  1249. fullPath += path;
  1250. cmSystemTools::MakeDirectory(fullPath);
  1251. }
  1252. }
  1253. void cmNinjaTargetGenerator::EnsureParentDirectoryExists(
  1254. const std::string& path) const
  1255. {
  1256. EnsureDirectoryExists(cmSystemTools::GetParentDirectory(path));
  1257. }
  1258. void cmNinjaTargetGenerator::MacOSXContentGeneratorType::operator()(
  1259. cmSourceFile const& source, const char* pkgloc, const std::string& config)
  1260. {
  1261. // Skip OS X content when not building a Framework or Bundle.
  1262. if (!this->Generator->GetGeneratorTarget()->IsBundleOnApple()) {
  1263. return;
  1264. }
  1265. std::string macdir =
  1266. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc,
  1267. config);
  1268. // Get the input file location.
  1269. std::string input = source.GetFullPath();
  1270. input = this->Generator->GetGlobalGenerator()->ConvertToNinjaPath(input);
  1271. // Get the output file location.
  1272. std::string output =
  1273. cmStrCat(macdir, '/', cmSystemTools::GetFilenameName(input));
  1274. output = this->Generator->GetGlobalGenerator()->ConvertToNinjaPath(output);
  1275. // Write a build statement to copy the content into the bundle.
  1276. this->Generator->GetGlobalGenerator()->WriteMacOSXContentBuild(input, output,
  1277. config);
  1278. // Add as a dependency to the target so that it gets called.
  1279. this->Generator->Configs[config].ExtraFiles.push_back(std::move(output));
  1280. }
  1281. void cmNinjaTargetGenerator::addPoolNinjaVariable(
  1282. const std::string& pool_property, cmGeneratorTarget* target,
  1283. cmNinjaVars& vars)
  1284. {
  1285. const char* pool = target->GetProperty(pool_property);
  1286. if (pool) {
  1287. vars["pool"] = pool;
  1288. }
  1289. }
  1290. bool cmNinjaTargetGenerator::ForceResponseFile()
  1291. {
  1292. static std::string const forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
  1293. return (this->GetMakefile()->IsDefinitionSet(forceRspFile) ||
  1294. cmSystemTools::HasEnv(forceRspFile));
  1295. }