cmNinjaTargetGenerator.cxx 51 KB

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