cmNinjaTargetGenerator.cxx 51 KB

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