cmNinjaTargetGenerator.cxx 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  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 <memory>
  11. #include <ostream>
  12. #include <utility>
  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 "cmSystemTools.h"
  32. #include "cmake.h"
  33. std::unique_ptr<cmNinjaTargetGenerator> cmNinjaTargetGenerator::New(
  34. cmGeneratorTarget* target)
  35. {
  36. switch (target->GetType()) {
  37. case cmStateEnums::EXECUTABLE:
  38. case cmStateEnums::SHARED_LIBRARY:
  39. case cmStateEnums::STATIC_LIBRARY:
  40. case cmStateEnums::MODULE_LIBRARY:
  41. case cmStateEnums::OBJECT_LIBRARY:
  42. return cm::make_unique<cmNinjaNormalTargetGenerator>(target);
  43. case cmStateEnums::UTILITY:
  44. case cmStateEnums::GLOBAL_TARGET:
  45. return cm::make_unique<cmNinjaUtilityTargetGenerator>(target);
  46. default:
  47. return std::unique_ptr<cmNinjaTargetGenerator>();
  48. }
  49. }
  50. cmNinjaTargetGenerator::cmNinjaTargetGenerator(cmGeneratorTarget* target)
  51. : cmCommonTargetGenerator(target)
  52. , MacOSXContentGenerator(nullptr)
  53. , OSXBundleGenerator(nullptr)
  54. , LocalGenerator(
  55. static_cast<cmLocalNinjaGenerator*>(target->GetLocalGenerator()))
  56. {
  57. MacOSXContentGenerator = cm::make_unique<MacOSXContentGeneratorType>(this);
  58. }
  59. cmNinjaTargetGenerator::~cmNinjaTargetGenerator() = default;
  60. cmGeneratedFileStream& cmNinjaTargetGenerator::GetBuildFileStream() const
  61. {
  62. return *this->GetGlobalGenerator()->GetBuildFileStream();
  63. }
  64. cmGeneratedFileStream& cmNinjaTargetGenerator::GetRulesFileStream() const
  65. {
  66. return *this->GetGlobalGenerator()->GetRulesFileStream();
  67. }
  68. cmGlobalNinjaGenerator* cmNinjaTargetGenerator::GetGlobalGenerator() const
  69. {
  70. return this->LocalGenerator->GetGlobalNinjaGenerator();
  71. }
  72. std::string cmNinjaTargetGenerator::LanguageCompilerRule(
  73. const std::string& lang) const
  74. {
  75. return lang + "_COMPILER__" +
  76. cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName());
  77. }
  78. std::string cmNinjaTargetGenerator::LanguagePreprocessRule(
  79. std::string const& lang) const
  80. {
  81. return lang + "_PREPROCESS__" +
  82. cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName());
  83. }
  84. bool cmNinjaTargetGenerator::NeedExplicitPreprocessing(
  85. std::string const& lang) const
  86. {
  87. return lang == "Fortran";
  88. }
  89. bool cmNinjaTargetGenerator::UsePreprocessedSource(
  90. std::string const& lang) const
  91. {
  92. return lang == "Fortran";
  93. }
  94. std::string cmNinjaTargetGenerator::LanguageDyndepRule(
  95. const std::string& lang) const
  96. {
  97. return lang + "_DYNDEP__" +
  98. cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName());
  99. }
  100. bool cmNinjaTargetGenerator::NeedDyndep(std::string const& lang) const
  101. {
  102. return lang == "Fortran";
  103. }
  104. std::string cmNinjaTargetGenerator::OrderDependsTargetForTarget()
  105. {
  106. return "cmake_object_order_depends_target_" + this->GetTargetName();
  107. }
  108. // TODO: Most of the code is picked up from
  109. // void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink),
  110. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  111. // Refactor it.
  112. std::string cmNinjaTargetGenerator::ComputeFlagsForObject(
  113. cmSourceFile const* source, const std::string& language)
  114. {
  115. std::string flags = this->GetFlags(language);
  116. // Add Fortran format flags.
  117. if (language == "Fortran") {
  118. this->AppendFortranFormatFlags(flags, *source);
  119. }
  120. // Add source file specific flags.
  121. cmGeneratorExpressionInterpreter genexInterpreter(
  122. this->LocalGenerator, this->LocalGenerator->GetConfigName(),
  123. this->GeneratorTarget, language);
  124. const std::string COMPILE_FLAGS("COMPILE_FLAGS");
  125. if (const char* cflags = source->GetProperty(COMPILE_FLAGS)) {
  126. this->LocalGenerator->AppendFlags(
  127. flags, genexInterpreter.Evaluate(cflags, COMPILE_FLAGS));
  128. }
  129. const std::string COMPILE_OPTIONS("COMPILE_OPTIONS");
  130. if (const char* coptions = source->GetProperty(COMPILE_OPTIONS)) {
  131. this->LocalGenerator->AppendCompileOptions(
  132. flags, genexInterpreter.Evaluate(coptions, COMPILE_OPTIONS));
  133. }
  134. return flags;
  135. }
  136. void cmNinjaTargetGenerator::AddIncludeFlags(std::string& languageFlags,
  137. std::string const& language)
  138. {
  139. std::vector<std::string> includes;
  140. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  141. language, this->GetConfigName());
  142. // Add include directory flags.
  143. std::string includeFlags = this->LocalGenerator->GetIncludeFlags(
  144. includes, this->GeneratorTarget, language,
  145. language == "RC", // full include paths for RC needed by cmcldeps
  146. false, this->GetConfigName());
  147. if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
  148. std::replace(includeFlags.begin(), includeFlags.end(), '\\', '/');
  149. }
  150. this->LocalGenerator->AppendFlags(languageFlags, includeFlags);
  151. }
  152. bool cmNinjaTargetGenerator::NeedDepTypeMSVC(const std::string& lang) const
  153. {
  154. std::string const& deptype =
  155. this->GetMakefile()->GetSafeDefinition("CMAKE_NINJA_DEPTYPE_" + lang);
  156. if (deptype == "msvc") {
  157. return true;
  158. }
  159. if (deptype == "intel") {
  160. // Ninja does not really define "intel", but we use it to switch based
  161. // on whether this environment supports "gcc" or "msvc" deptype.
  162. if (!this->GetGlobalGenerator()->SupportsMultilineDepfile()) {
  163. // This ninja version is too old to support the Intel depfile format.
  164. // Fall back to msvc deptype.
  165. return true;
  166. }
  167. if ((this->Makefile->GetHomeDirectory().find(' ') != std::string::npos) ||
  168. (this->Makefile->GetHomeOutputDirectory().find(' ') !=
  169. std::string::npos)) {
  170. // The Intel compiler does not properly escape spaces in a depfile.
  171. // Fall back to msvc deptype.
  172. return true;
  173. }
  174. }
  175. return false;
  176. }
  177. // TODO: Refactor with
  178. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
  179. std::string cmNinjaTargetGenerator::ComputeDefines(cmSourceFile const* source,
  180. const std::string& language)
  181. {
  182. std::set<std::string> defines;
  183. const std::string config = this->LocalGenerator->GetConfigName();
  184. cmGeneratorExpressionInterpreter genexInterpreter(
  185. this->LocalGenerator, config, this->GeneratorTarget, language);
  186. const std::string COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
  187. if (const char* compile_defs = source->GetProperty(COMPILE_DEFINITIONS)) {
  188. this->LocalGenerator->AppendDefines(
  189. defines, genexInterpreter.Evaluate(compile_defs, COMPILE_DEFINITIONS));
  190. }
  191. std::string defPropName = "COMPILE_DEFINITIONS_";
  192. defPropName += cmSystemTools::UpperCase(config);
  193. if (const char* config_compile_defs = source->GetProperty(defPropName)) {
  194. this->LocalGenerator->AppendDefines(
  195. defines,
  196. genexInterpreter.Evaluate(config_compile_defs, COMPILE_DEFINITIONS));
  197. }
  198. std::string definesString = this->GetDefines(language);
  199. this->LocalGenerator->JoinDefines(defines, definesString, language);
  200. return definesString;
  201. }
  202. std::string cmNinjaTargetGenerator::ComputeIncludes(
  203. cmSourceFile const* source, const std::string& language)
  204. {
  205. std::vector<std::string> includes;
  206. const std::string config = this->LocalGenerator->GetConfigName();
  207. cmGeneratorExpressionInterpreter genexInterpreter(
  208. this->LocalGenerator, config, this->GeneratorTarget, language);
  209. const std::string INCLUDE_DIRECTORIES("INCLUDE_DIRECTORIES");
  210. if (const char* cincludes = source->GetProperty(INCLUDE_DIRECTORIES)) {
  211. this->LocalGenerator->AppendIncludeDirectories(
  212. includes, genexInterpreter.Evaluate(cincludes, INCLUDE_DIRECTORIES),
  213. *source);
  214. }
  215. std::string includesString = this->LocalGenerator->GetIncludeFlags(
  216. includes, this->GeneratorTarget, language, true, false, config);
  217. this->LocalGenerator->AppendFlags(includesString,
  218. this->GetIncludes(language));
  219. return includesString;
  220. }
  221. cmNinjaDeps cmNinjaTargetGenerator::ComputeLinkDeps(
  222. const std::string& linkLanguage) const
  223. {
  224. // Static libraries never depend on other targets for linking.
  225. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  226. this->GeneratorTarget->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  227. return cmNinjaDeps();
  228. }
  229. cmComputeLinkInformation* cli =
  230. this->GeneratorTarget->GetLinkInformation(this->GetConfigName());
  231. if (!cli) {
  232. return cmNinjaDeps();
  233. }
  234. const std::vector<std::string>& deps = cli->GetDepends();
  235. cmNinjaDeps result(deps.size());
  236. std::transform(deps.begin(), deps.end(), result.begin(), MapToNinjaPath());
  237. // Add a dependency on the link definitions file, if any.
  238. if (cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  239. this->GeneratorTarget->GetModuleDefinitionInfo(
  240. this->GetConfigName())) {
  241. for (cmSourceFile const* src : mdi->Sources) {
  242. result.push_back(this->ConvertToNinjaPath(src->GetFullPath()));
  243. }
  244. }
  245. // Add a dependency on user-specified manifest files, if any.
  246. std::vector<cmSourceFile const*> manifest_srcs;
  247. this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName);
  248. for (cmSourceFile const* manifest_src : manifest_srcs) {
  249. result.push_back(this->ConvertToNinjaPath(manifest_src->GetFullPath()));
  250. }
  251. // Add user-specified dependencies.
  252. std::vector<std::string> linkDeps;
  253. this->GeneratorTarget->GetLinkDepends(linkDeps, this->ConfigName,
  254. linkLanguage);
  255. std::transform(linkDeps.begin(), linkDeps.end(), std::back_inserter(result),
  256. MapToNinjaPath());
  257. return result;
  258. }
  259. std::string cmNinjaTargetGenerator::GetSourceFilePath(
  260. cmSourceFile const* source) const
  261. {
  262. return ConvertToNinjaPath(source->GetFullPath());
  263. }
  264. std::string cmNinjaTargetGenerator::GetObjectFilePath(
  265. cmSourceFile const* source) const
  266. {
  267. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  268. if (!path.empty()) {
  269. path += "/";
  270. }
  271. std::string const& objectName = this->GeneratorTarget->GetObjectName(source);
  272. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  273. path += "/";
  274. path += objectName;
  275. return path;
  276. }
  277. std::string cmNinjaTargetGenerator::GetPreprocessedFilePath(
  278. cmSourceFile const* source) const
  279. {
  280. // Choose an extension to compile already-preprocessed source.
  281. std::string ppExt = source->GetExtension();
  282. if (cmHasLiteralPrefix(ppExt, "F")) {
  283. // Some Fortran compilers automatically enable preprocessing for
  284. // upper-case extensions. Since the source is already preprocessed,
  285. // use a lower-case extension.
  286. ppExt = cmSystemTools::LowerCase(ppExt);
  287. }
  288. if (ppExt == "fpp") {
  289. // Some Fortran compilers automatically enable preprocessing for
  290. // the ".fpp" extension. Since the source is already preprocessed,
  291. // use the ".f" extension.
  292. ppExt = "f";
  293. }
  294. // Take the object file name and replace the extension.
  295. std::string const& objName = this->GeneratorTarget->GetObjectName(source);
  296. std::string const& objExt =
  297. this->GetGlobalGenerator()->GetLanguageOutputExtension(*source);
  298. assert(objName.size() >= objExt.size());
  299. std::string const ppName =
  300. objName.substr(0, objName.size() - objExt.size()) + "-pp." + ppExt;
  301. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  302. if (!path.empty()) {
  303. path += "/";
  304. }
  305. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  306. path += "/";
  307. path += ppName;
  308. return path;
  309. }
  310. std::string cmNinjaTargetGenerator::GetDyndepFilePath(
  311. std::string const& lang) const
  312. {
  313. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  314. if (!path.empty()) {
  315. path += "/";
  316. }
  317. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  318. path += "/";
  319. path += lang;
  320. path += ".dd";
  321. return path;
  322. }
  323. std::string cmNinjaTargetGenerator::GetTargetDependInfoPath(
  324. std::string const& lang) const
  325. {
  326. std::string path = this->Makefile->GetCurrentBinaryDirectory();
  327. path += "/";
  328. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  329. path += "/" + lang + "DependInfo.json";
  330. return path;
  331. }
  332. std::string cmNinjaTargetGenerator::GetTargetOutputDir() const
  333. {
  334. std::string dir = this->GeneratorTarget->GetDirectory(this->GetConfigName());
  335. return ConvertToNinjaPath(dir);
  336. }
  337. std::string cmNinjaTargetGenerator::GetTargetFilePath(
  338. const std::string& name) const
  339. {
  340. std::string path = this->GetTargetOutputDir();
  341. if (path.empty() || path == ".") {
  342. return name;
  343. }
  344. path += "/";
  345. path += name;
  346. return path;
  347. }
  348. std::string cmNinjaTargetGenerator::GetTargetName() const
  349. {
  350. return this->GeneratorTarget->GetName();
  351. }
  352. bool cmNinjaTargetGenerator::SetMsvcTargetPdbVariable(cmNinjaVars& vars) const
  353. {
  354. cmMakefile* mf = this->GetMakefile();
  355. if (mf->GetDefinition("MSVC_C_ARCHITECTURE_ID") ||
  356. mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID") ||
  357. mf->GetDefinition("MSVC_CUDA_ARCHITECTURE_ID")) {
  358. std::string pdbPath;
  359. std::string compilePdbPath = this->ComputeTargetCompilePDB();
  360. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE ||
  361. this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  362. this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  363. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  364. pdbPath = this->GeneratorTarget->GetPDBDirectory(this->GetConfigName());
  365. pdbPath += "/";
  366. pdbPath += this->GeneratorTarget->GetPDBName(this->GetConfigName());
  367. }
  368. vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  369. ConvertToNinjaPath(pdbPath), cmOutputConverter::SHELL);
  370. vars["TARGET_COMPILE_PDB"] =
  371. this->GetLocalGenerator()->ConvertToOutputFormat(
  372. ConvertToNinjaPath(compilePdbPath), cmOutputConverter::SHELL);
  373. EnsureParentDirectoryExists(pdbPath);
  374. EnsureParentDirectoryExists(compilePdbPath);
  375. return true;
  376. }
  377. return false;
  378. }
  379. void cmNinjaTargetGenerator::WriteLanguageRules(const std::string& language)
  380. {
  381. #ifdef NINJA_GEN_VERBOSE_FILES
  382. this->GetRulesFileStream() << "# Rules for language " << language << "\n\n";
  383. #endif
  384. this->WriteCompileRule(language);
  385. }
  386. void cmNinjaTargetGenerator::WriteCompileRule(const std::string& lang)
  387. {
  388. cmRulePlaceholderExpander::RuleVariables vars;
  389. vars.CMTargetName = this->GetGeneratorTarget()->GetName().c_str();
  390. vars.CMTargetType =
  391. cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType());
  392. vars.Language = lang.c_str();
  393. vars.Source = "$in";
  394. vars.Object = "$out";
  395. vars.Defines = "$DEFINES";
  396. vars.Includes = "$INCLUDES";
  397. vars.TargetPDB = "$TARGET_PDB";
  398. vars.TargetCompilePDB = "$TARGET_COMPILE_PDB";
  399. vars.ObjectDir = "$OBJECT_DIR";
  400. vars.ObjectFileDir = "$OBJECT_FILE_DIR";
  401. // For some cases we do an explicit preprocessor invocation.
  402. bool const explicitPP = this->NeedExplicitPreprocessing(lang);
  403. bool const needDyndep = this->NeedDyndep(lang);
  404. cmMakefile* mf = this->GetMakefile();
  405. std::string flags = "$FLAGS";
  406. std::string responseFlag;
  407. bool const lang_supports_response = lang != "RC";
  408. if (lang_supports_response && this->ForceResponseFile()) {
  409. std::string const responseFlagVar =
  410. "CMAKE_" + lang + "_RESPONSE_FILE_FLAG";
  411. responseFlag = this->Makefile->GetSafeDefinition(responseFlagVar);
  412. if (responseFlag.empty() && lang != "CUDA") {
  413. responseFlag = "@";
  414. }
  415. }
  416. std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
  417. this->GetLocalGenerator()->CreateRulePlaceholderExpander());
  418. std::string const tdi = this->GetLocalGenerator()->ConvertToOutputFormat(
  419. ConvertToNinjaPath(this->GetTargetDependInfoPath(lang)),
  420. cmLocalGenerator::SHELL);
  421. std::string launcher;
  422. const char* val = this->GetLocalGenerator()->GetRuleLauncher(
  423. this->GetGeneratorTarget(), "RULE_LAUNCH_COMPILE");
  424. if (val && *val) {
  425. launcher = val;
  426. launcher += " ";
  427. }
  428. std::string const cmakeCmd =
  429. this->GetLocalGenerator()->ConvertToOutputFormat(
  430. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  431. if (explicitPP) {
  432. cmNinjaRule rule(this->LanguagePreprocessRule(lang));
  433. // Explicit preprocessing always uses a depfile.
  434. rule.DepType = ""; // no deps= for multiple outputs
  435. rule.DepFile = "$DEP_FILE";
  436. cmRulePlaceholderExpander::RuleVariables ppVars;
  437. ppVars.CMTargetName = vars.CMTargetName;
  438. ppVars.CMTargetType = vars.CMTargetType;
  439. ppVars.Language = vars.Language;
  440. ppVars.Object = "$out"; // for RULE_LAUNCH_COMPILE
  441. ppVars.PreprocessedSource = "$out";
  442. ppVars.DependencyFile = rule.DepFile.c_str();
  443. // Preprocessing uses the original source,
  444. // compilation uses preprocessed output.
  445. ppVars.Source = vars.Source;
  446. vars.Source = "$in";
  447. // Preprocessing and compilation use the same flags.
  448. std::string ppFlags = flags;
  449. // Move preprocessor definitions to the preprocessor rule.
  450. ppVars.Defines = vars.Defines;
  451. vars.Defines = "";
  452. // Copy include directories to the preprocessor rule. The Fortran
  453. // compilation rule still needs them for the INCLUDE directive.
  454. ppVars.Includes = vars.Includes;
  455. // If using a response file, move defines, includes, and flags into it.
  456. if (!responseFlag.empty()) {
  457. rule.RspFile = "$RSP_FILE";
  458. rule.RspContent = " ";
  459. rule.RspContent += ppVars.Defines;
  460. rule.RspContent += " ";
  461. rule.RspContent += ppVars.Includes;
  462. rule.RspContent += " ";
  463. rule.RspContent += ppFlags;
  464. ppFlags = responseFlag + rule.RspFile;
  465. ppVars.Defines = "";
  466. ppVars.Includes = "";
  467. }
  468. ppVars.Flags = ppFlags.c_str();
  469. // Rule for preprocessing source file.
  470. std::vector<std::string> ppCmds;
  471. {
  472. // Lookup the explicit preprocessing rule.
  473. std::string ppVar = "CMAKE_" + lang;
  474. ppVar += "_PREPROCESS_SOURCE";
  475. cmSystemTools::ExpandListArgument(
  476. this->GetMakefile()->GetRequiredDefinition(ppVar), ppCmds);
  477. }
  478. for (std::string& i : ppCmds) {
  479. i = 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. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  605. } else {
  606. const std::string cmdVar = "CMAKE_" + lang + "_COMPILE_OBJECT";
  607. const std::string& compileCmd = mf->GetRequiredDefinition(cmdVar);
  608. cmSystemTools::ExpandListArgument(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. cmSystemTools::ExpandListArgument(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 = 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 = this->ConvertToNinjaPath(
  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 =
  880. cmSystemTools::ExpandedListArgument(objectDeps);
  881. for (std::string& odi : depList) {
  882. if (cmSystemTools::FileIsFullPath(odi)) {
  883. odi = cmSystemTools::CollapseFullPath(odi);
  884. }
  885. }
  886. std::transform(depList.begin(), depList.end(),
  887. std::back_inserter(objBuild.ImplicitDeps),
  888. MapToNinjaPath());
  889. }
  890. objBuild.OrderOnlyDeps.push_back(this->OrderDependsTargetForTarget());
  891. // If the source file is GENERATED and does not have a custom command
  892. // (either attached to this source file or another one), assume that one of
  893. // the target dependencies, OBJECT_DEPENDS or header file custom commands
  894. // will rebuild the file.
  895. if (source->GetIsGenerated() &&
  896. !source->GetPropertyAsBool("__CMAKE_GENERATED_BY_CMAKE") &&
  897. !source->GetCustomCommand() &&
  898. !this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFileName)) {
  899. this->GetGlobalGenerator()->AddAssumedSourceDependencies(
  900. sourceFileName, objBuild.OrderOnlyDeps);
  901. }
  902. // For some cases we need to generate a ninja dyndep file.
  903. bool const needDyndep = this->NeedDyndep(language);
  904. // For some cases we do an explicit preprocessor invocation.
  905. bool const explicitPP = this->NeedExplicitPreprocessing(language);
  906. if (explicitPP) {
  907. cmNinjaBuild ppBuild(this->LanguagePreprocessRule(language));
  908. std::string const ppFileName =
  909. this->ConvertToNinjaPath(this->GetPreprocessedFilePath(source));
  910. ppBuild.Outputs.push_back(ppFileName);
  911. ppBuild.RspFile = ppFileName + ".rsp";
  912. bool const compilePP = this->UsePreprocessedSource(language);
  913. if (compilePP) {
  914. // Move compilation dependencies to the preprocessing build statement.
  915. std::swap(ppBuild.ExplicitDeps, objBuild.ExplicitDeps);
  916. std::swap(ppBuild.ImplicitDeps, objBuild.ImplicitDeps);
  917. std::swap(ppBuild.OrderOnlyDeps, objBuild.OrderOnlyDeps);
  918. std::swap(ppBuild.Variables["IN_ABS"], vars["IN_ABS"]);
  919. // The actual compilation will now use the preprocessed source.
  920. objBuild.ExplicitDeps.push_back(ppFileName);
  921. } else {
  922. // Copy compilation dependencies to the preprocessing build statement.
  923. ppBuild.ExplicitDeps = objBuild.ExplicitDeps;
  924. ppBuild.ImplicitDeps = objBuild.ImplicitDeps;
  925. ppBuild.OrderOnlyDeps = objBuild.OrderOnlyDeps;
  926. ppBuild.Variables["IN_ABS"] = vars["IN_ABS"];
  927. }
  928. // Preprocessing and compilation generally use the same flags.
  929. ppBuild.Variables["FLAGS"] = vars["FLAGS"];
  930. if (compilePP) {
  931. // In case compilation requires flags that are incompatible with
  932. // preprocessing, include them here.
  933. std::string const& postFlag = this->Makefile->GetSafeDefinition(
  934. "CMAKE_" + language + "_POSTPROCESS_FLAG");
  935. this->LocalGenerator->AppendFlags(vars["FLAGS"], postFlag);
  936. }
  937. if (compilePP) {
  938. // Move preprocessor definitions to the preprocessor build statement.
  939. std::swap(ppBuild.Variables["DEFINES"], vars["DEFINES"]);
  940. } else {
  941. // Copy preprocessor definitions to the preprocessor build statement.
  942. ppBuild.Variables["DEFINES"] = vars["DEFINES"];
  943. }
  944. // Copy include directories to the preprocessor build statement. The
  945. // Fortran compilation build statement still needs them for the INCLUDE
  946. // directive.
  947. ppBuild.Variables["INCLUDES"] = vars["INCLUDES"];
  948. if (compilePP) {
  949. // Prepend source file's original directory as an include directory
  950. // so e.g. Fortran INCLUDE statements can look for files in it.
  951. std::vector<std::string> sourceDirectory;
  952. sourceDirectory.push_back(
  953. cmSystemTools::GetParentDirectory(source->GetFullPath()));
  954. std::string sourceDirectoryFlag = this->LocalGenerator->GetIncludeFlags(
  955. sourceDirectory, this->GeneratorTarget, language, false, false,
  956. this->GetConfigName());
  957. vars["INCLUDES"] = sourceDirectoryFlag + " " + vars["INCLUDES"];
  958. }
  959. // Explicit preprocessing always uses a depfile.
  960. ppBuild.Variables["DEP_FILE"] =
  961. this->GetLocalGenerator()->ConvertToOutputFormat(
  962. objectFileName + ".pp.d", cmOutputConverter::SHELL);
  963. if (compilePP) {
  964. // The actual compilation does not need a depfile because it
  965. // depends on the already-preprocessed source.
  966. vars.erase("DEP_FILE");
  967. }
  968. if (needDyndep) {
  969. // Tell dependency scanner the object file that will result from
  970. // compiling the source.
  971. ppBuild.Variables["OBJ_FILE"] = objectFileName;
  972. // Tell dependency scanner where to store dyndep intermediate results.
  973. std::string const ddiFile = objectFileName + ".ddi";
  974. ppBuild.Variables["DYNDEP_INTERMEDIATE_FILE"] = ddiFile;
  975. ppBuild.ImplicitOuts.push_back(ddiFile);
  976. this->DDIFiles[language].push_back(ddiFile);
  977. }
  978. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
  979. ppBuild.Variables);
  980. this->GetGlobalGenerator()->WriteBuild(this->GetBuildFileStream(), ppBuild,
  981. commandLineLengthLimit);
  982. }
  983. if (needDyndep) {
  984. std::string const dyndep = this->GetDyndepFilePath(language);
  985. objBuild.OrderOnlyDeps.push_back(dyndep);
  986. vars["dyndep"] = dyndep;
  987. }
  988. EnsureParentDirectoryExists(objectFileName);
  989. vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  990. objectDir, cmOutputConverter::SHELL);
  991. vars["OBJECT_FILE_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  992. objectFileDir, cmOutputConverter::SHELL);
  993. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
  994. vars);
  995. this->SetMsvcTargetPdbVariable(vars);
  996. objBuild.RspFile = objectFileName + ".rsp";
  997. if (language == "Swift") {
  998. this->EmitSwiftDependencyInfo(source);
  999. } else {
  1000. this->GetGlobalGenerator()->WriteBuild(this->GetBuildFileStream(),
  1001. objBuild, commandLineLengthLimit);
  1002. }
  1003. if (const char* objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
  1004. cmNinjaBuild build("phony");
  1005. build.Comment = "Additional output files.";
  1006. build.Outputs = cmSystemTools::ExpandedListArgument(objectOutputs);
  1007. std::transform(build.Outputs.begin(), build.Outputs.end(),
  1008. build.Outputs.begin(), MapToNinjaPath());
  1009. build.ExplicitDeps = objBuild.Outputs;
  1010. this->GetGlobalGenerator()->WriteBuild(this->GetBuildFileStream(), build);
  1011. }
  1012. }
  1013. void cmNinjaTargetGenerator::WriteTargetDependInfo(std::string const& lang)
  1014. {
  1015. Json::Value tdi(Json::objectValue);
  1016. tdi["language"] = lang;
  1017. tdi["compiler-id"] =
  1018. this->Makefile->GetSafeDefinition("CMAKE_" + lang + "_COMPILER_ID");
  1019. if (lang == "Fortran") {
  1020. std::string mod_dir = this->GeneratorTarget->GetFortranModuleDirectory(
  1021. this->Makefile->GetHomeOutputDirectory());
  1022. if (mod_dir.empty()) {
  1023. mod_dir = this->Makefile->GetCurrentBinaryDirectory();
  1024. }
  1025. tdi["module-dir"] = mod_dir;
  1026. tdi["submodule-sep"] =
  1027. this->Makefile->GetSafeDefinition("CMAKE_Fortran_SUBMODULE_SEP");
  1028. tdi["submodule-ext"] =
  1029. this->Makefile->GetSafeDefinition("CMAKE_Fortran_SUBMODULE_EXT");
  1030. }
  1031. tdi["dir-cur-bld"] = this->Makefile->GetCurrentBinaryDirectory();
  1032. tdi["dir-cur-src"] = this->Makefile->GetCurrentSourceDirectory();
  1033. tdi["dir-top-bld"] = this->Makefile->GetHomeOutputDirectory();
  1034. tdi["dir-top-src"] = this->Makefile->GetHomeDirectory();
  1035. Json::Value& tdi_include_dirs = tdi["include-dirs"] = Json::arrayValue;
  1036. std::vector<std::string> includes;
  1037. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  1038. lang, this->GetConfigName());
  1039. for (std::string const& i : includes) {
  1040. // Convert the include directories the same way we do for -I flags.
  1041. // See upstream ninja issue 1251.
  1042. tdi_include_dirs.append(this->ConvertToNinjaPath(i));
  1043. }
  1044. Json::Value& tdi_linked_target_dirs = tdi["linked-target-dirs"] =
  1045. Json::arrayValue;
  1046. for (std::string const& l : this->GetLinkedTargetDirectories()) {
  1047. tdi_linked_target_dirs.append(l);
  1048. }
  1049. std::string const tdin = this->GetTargetDependInfoPath(lang);
  1050. cmGeneratedFileStream tdif(tdin);
  1051. tdif << tdi;
  1052. }
  1053. void cmNinjaTargetGenerator::EmitSwiftDependencyInfo(
  1054. cmSourceFile const* source)
  1055. {
  1056. std::string const sourceFilePath =
  1057. this->ConvertToNinjaPath(this->GetSourceFilePath(source));
  1058. std::string const objectFilePath =
  1059. this->ConvertToNinjaPath(this->GetObjectFilePath(source));
  1060. std::string const swiftDepsPath = [source, objectFilePath]() -> std::string {
  1061. if (const char* name = source->GetProperty("Swift_DEPENDENCIES_FILE")) {
  1062. return name;
  1063. }
  1064. return objectFilePath + ".swiftdeps";
  1065. }();
  1066. std::string const swiftDiaPath = [source, objectFilePath]() -> std::string {
  1067. if (const char* name = source->GetProperty("Swift_DIAGNOSTICS_FILE")) {
  1068. return name;
  1069. }
  1070. return objectFilePath + ".dia";
  1071. }();
  1072. std::string const makeDepsPath = [this, source]() -> std::string {
  1073. cmLocalNinjaGenerator const* local = this->GetLocalGenerator();
  1074. std::string const objectFileName =
  1075. this->ConvertToNinjaPath(this->GetObjectFilePath(source));
  1076. std::string const objectFileDir =
  1077. cmSystemTools::GetFilenamePath(objectFileName);
  1078. if (this->Makefile->IsOn("CMAKE_Swift_DEPFLE_EXTNSION_REPLACE")) {
  1079. std::string dependFileName =
  1080. cmSystemTools::GetFilenameWithoutLastExtension(objectFileName) + ".d";
  1081. return local->ConvertToOutputFormat(objectFileDir + "/" + dependFileName,
  1082. cmOutputConverter::SHELL);
  1083. }
  1084. return local->ConvertToOutputFormat(objectFileName + ".d",
  1085. cmOutputConverter::SHELL);
  1086. }();
  1087. // build the source file mapping
  1088. // https://github.com/apple/swift/blob/master/docs/Driver.md#output-file-maps
  1089. Json::Value entry = Json::Value(Json::objectValue);
  1090. entry["object"] = objectFilePath;
  1091. entry["dependencies"] = makeDepsPath;
  1092. entry["swift-dependencies"] = swiftDepsPath;
  1093. entry["diagnostics"] = swiftDiaPath;
  1094. SwiftOutputMap[sourceFilePath] = entry;
  1095. }
  1096. void cmNinjaTargetGenerator::ExportObjectCompileCommand(
  1097. std::string const& language, std::string const& sourceFileName,
  1098. std::string const& objectDir, std::string const& objectFileName,
  1099. std::string const& objectFileDir, std::string const& flags,
  1100. std::string const& defines, std::string const& includes)
  1101. {
  1102. if (!this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS")) {
  1103. return;
  1104. }
  1105. cmRulePlaceholderExpander::RuleVariables compileObjectVars;
  1106. compileObjectVars.Language = language.c_str();
  1107. std::string escapedSourceFileName = sourceFileName;
  1108. if (!cmSystemTools::FileIsFullPath(sourceFileName)) {
  1109. escapedSourceFileName =
  1110. cmSystemTools::CollapseFullPath(escapedSourceFileName,
  1111. this->GetGlobalGenerator()
  1112. ->GetCMakeInstance()
  1113. ->GetHomeOutputDirectory());
  1114. }
  1115. escapedSourceFileName = this->LocalGenerator->ConvertToOutputFormat(
  1116. escapedSourceFileName, cmOutputConverter::SHELL);
  1117. compileObjectVars.Source = escapedSourceFileName.c_str();
  1118. compileObjectVars.Object = objectFileName.c_str();
  1119. compileObjectVars.ObjectDir = objectDir.c_str();
  1120. compileObjectVars.ObjectFileDir = objectFileDir.c_str();
  1121. compileObjectVars.Flags = flags.c_str();
  1122. compileObjectVars.Defines = defines.c_str();
  1123. compileObjectVars.Includes = includes.c_str();
  1124. // Rule for compiling object file.
  1125. std::vector<std::string> compileCmds;
  1126. if (language == "CUDA") {
  1127. std::string cmdVar;
  1128. if (this->GeneratorTarget->GetPropertyAsBool(
  1129. "CUDA_SEPARABLE_COMPILATION")) {
  1130. cmdVar = "CMAKE_CUDA_COMPILE_SEPARABLE_COMPILATION";
  1131. } else if (this->GeneratorTarget->GetPropertyAsBool(
  1132. "CUDA_PTX_COMPILATION")) {
  1133. cmdVar = "CMAKE_CUDA_COMPILE_PTX_COMPILATION";
  1134. } else {
  1135. cmdVar = "CMAKE_CUDA_COMPILE_WHOLE_COMPILATION";
  1136. }
  1137. const std::string& compileCmd =
  1138. this->GetMakefile()->GetRequiredDefinition(cmdVar);
  1139. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  1140. } else {
  1141. const std::string cmdVar = "CMAKE_" + language + "_COMPILE_OBJECT";
  1142. const std::string& compileCmd =
  1143. this->GetMakefile()->GetRequiredDefinition(cmdVar);
  1144. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  1145. }
  1146. std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
  1147. this->GetLocalGenerator()->CreateRulePlaceholderExpander());
  1148. for (std::string& i : compileCmds) {
  1149. // no launcher for CMAKE_EXPORT_COMPILE_COMMANDS
  1150. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(), i,
  1151. compileObjectVars);
  1152. }
  1153. std::string cmdLine =
  1154. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  1155. this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine, sourceFileName);
  1156. }
  1157. void cmNinjaTargetGenerator::AdditionalCleanFiles()
  1158. {
  1159. if (const char* prop_value =
  1160. this->GeneratorTarget->GetProperty("ADDITIONAL_CLEAN_FILES")) {
  1161. cmLocalNinjaGenerator* lg = this->LocalGenerator;
  1162. std::vector<std::string> cleanFiles;
  1163. {
  1164. cmGeneratorExpression ge;
  1165. auto cge = ge.Parse(prop_value);
  1166. cmSystemTools::ExpandListArgument(
  1167. cge->Evaluate(lg,
  1168. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"),
  1169. false, this->GeneratorTarget, nullptr, nullptr),
  1170. cleanFiles);
  1171. }
  1172. std::string const& binaryDir = lg->GetCurrentBinaryDirectory();
  1173. cmGlobalNinjaGenerator* gg = lg->GetGlobalNinjaGenerator();
  1174. for (std::string const& cleanFile : cleanFiles) {
  1175. // Support relative paths
  1176. gg->AddAdditionalCleanFile(
  1177. cmSystemTools::CollapseFullPath(cleanFile, binaryDir));
  1178. }
  1179. }
  1180. }
  1181. void cmNinjaTargetGenerator::EnsureDirectoryExists(
  1182. const std::string& path) const
  1183. {
  1184. if (cmSystemTools::FileIsFullPath(path)) {
  1185. cmSystemTools::MakeDirectory(path);
  1186. } else {
  1187. cmGlobalNinjaGenerator* gg = this->GetGlobalGenerator();
  1188. std::string fullPath = gg->GetCMakeInstance()->GetHomeOutputDirectory();
  1189. // Also ensures their is a trailing slash.
  1190. gg->StripNinjaOutputPathPrefixAsSuffix(fullPath);
  1191. fullPath += path;
  1192. cmSystemTools::MakeDirectory(fullPath);
  1193. }
  1194. }
  1195. void cmNinjaTargetGenerator::EnsureParentDirectoryExists(
  1196. const std::string& path) const
  1197. {
  1198. EnsureDirectoryExists(cmSystemTools::GetParentDirectory(path));
  1199. }
  1200. void cmNinjaTargetGenerator::MacOSXContentGeneratorType::operator()(
  1201. cmSourceFile const& source, const char* pkgloc)
  1202. {
  1203. // Skip OS X content when not building a Framework or Bundle.
  1204. if (!this->Generator->GetGeneratorTarget()->IsBundleOnApple()) {
  1205. return;
  1206. }
  1207. std::string macdir =
  1208. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc);
  1209. // Get the input file location.
  1210. std::string input = source.GetFullPath();
  1211. input = this->Generator->GetGlobalGenerator()->ConvertToNinjaPath(input);
  1212. // Get the output file location.
  1213. std::string output = macdir;
  1214. output += "/";
  1215. output += cmSystemTools::GetFilenameName(input);
  1216. output = this->Generator->GetGlobalGenerator()->ConvertToNinjaPath(output);
  1217. // Write a build statement to copy the content into the bundle.
  1218. this->Generator->GetGlobalGenerator()->WriteMacOSXContentBuild(input,
  1219. output);
  1220. // Add as a dependency to the target so that it gets called.
  1221. this->Generator->ExtraFiles.push_back(std::move(output));
  1222. }
  1223. void cmNinjaTargetGenerator::addPoolNinjaVariable(
  1224. const std::string& pool_property, cmGeneratorTarget* target,
  1225. cmNinjaVars& vars)
  1226. {
  1227. const char* pool = target->GetProperty(pool_property);
  1228. if (pool) {
  1229. vars["pool"] = pool;
  1230. }
  1231. }
  1232. bool cmNinjaTargetGenerator::ForceResponseFile()
  1233. {
  1234. static std::string const forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
  1235. return (this->GetMakefile()->IsDefinitionSet(forceRspFile) ||
  1236. cmSystemTools::HasEnv(forceRspFile));
  1237. }