cmNinjaTargetGenerator.cxx 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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 <sstream>
  11. #include <string.h>
  12. #include "cmAlgorithms.h"
  13. #include "cmComputeLinkInformation.h"
  14. #include "cmCustomCommandGenerator.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmGeneratorExpression.h"
  17. #include "cmGeneratorTarget.h"
  18. #include "cmGlobalNinjaGenerator.h"
  19. #include "cmLocalGenerator.h"
  20. #include "cmLocalNinjaGenerator.h"
  21. #include "cmMakefile.h"
  22. #include "cmNinjaNormalTargetGenerator.h"
  23. #include "cmNinjaUtilityTargetGenerator.h"
  24. #include "cmOutputConverter.h"
  25. #include "cmRulePlaceholderExpander.h"
  26. #include "cmSourceFile.h"
  27. #include "cmState.h"
  28. #include "cmStateTypes.h"
  29. #include "cmSystemTools.h"
  30. #include "cm_auto_ptr.hxx"
  31. #include "cmake.h"
  32. cmNinjaTargetGenerator* cmNinjaTargetGenerator::New(cmGeneratorTarget* target)
  33. {
  34. switch (target->GetType()) {
  35. case cmStateEnums::EXECUTABLE:
  36. case cmStateEnums::SHARED_LIBRARY:
  37. case cmStateEnums::STATIC_LIBRARY:
  38. case cmStateEnums::MODULE_LIBRARY:
  39. case cmStateEnums::OBJECT_LIBRARY:
  40. return new cmNinjaNormalTargetGenerator(target);
  41. case cmStateEnums::UTILITY:
  42. case cmStateEnums::GLOBAL_TARGET:
  43. return new cmNinjaUtilityTargetGenerator(target);
  44. default:
  45. return nullptr;
  46. }
  47. }
  48. cmNinjaTargetGenerator::cmNinjaTargetGenerator(cmGeneratorTarget* target)
  49. : cmCommonTargetGenerator(target)
  50. , MacOSXContentGenerator(nullptr)
  51. , OSXBundleGenerator(nullptr)
  52. , MacContentFolders()
  53. , LocalGenerator(
  54. static_cast<cmLocalNinjaGenerator*>(target->GetLocalGenerator()))
  55. , Objects()
  56. {
  57. MacOSXContentGenerator = new MacOSXContentGeneratorType(this);
  58. }
  59. cmNinjaTargetGenerator::~cmNinjaTargetGenerator()
  60. {
  61. delete this->MacOSXContentGenerator;
  62. }
  63. cmGeneratedFileStream& cmNinjaTargetGenerator::GetBuildFileStream() const
  64. {
  65. return *this->GetGlobalGenerator()->GetBuildFileStream();
  66. }
  67. cmGeneratedFileStream& cmNinjaTargetGenerator::GetRulesFileStream() const
  68. {
  69. return *this->GetGlobalGenerator()->GetRulesFileStream();
  70. }
  71. cmGlobalNinjaGenerator* cmNinjaTargetGenerator::GetGlobalGenerator() const
  72. {
  73. return this->LocalGenerator->GetGlobalNinjaGenerator();
  74. }
  75. std::string cmNinjaTargetGenerator::LanguageCompilerRule(
  76. const std::string& lang) const
  77. {
  78. return lang + "_COMPILER__" +
  79. cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName());
  80. }
  81. std::string cmNinjaTargetGenerator::LanguagePreprocessRule(
  82. std::string const& lang) const
  83. {
  84. return lang + "_PREPROCESS__" +
  85. cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName());
  86. }
  87. bool cmNinjaTargetGenerator::NeedExplicitPreprocessing(
  88. std::string const& lang) const
  89. {
  90. return lang == "Fortran";
  91. }
  92. std::string cmNinjaTargetGenerator::LanguageDyndepRule(
  93. const std::string& lang) const
  94. {
  95. return lang + "_DYNDEP__" +
  96. cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName());
  97. }
  98. bool cmNinjaTargetGenerator::NeedDyndep(std::string const& lang) const
  99. {
  100. return lang == "Fortran";
  101. }
  102. std::string cmNinjaTargetGenerator::OrderDependsTargetForTarget()
  103. {
  104. return "cmake_object_order_depends_target_" + this->GetTargetName();
  105. }
  106. // TODO: Most of the code is picked up from
  107. // void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink),
  108. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  109. // Refactor it.
  110. std::string cmNinjaTargetGenerator::ComputeFlagsForObject(
  111. cmSourceFile const* source, const std::string& language)
  112. {
  113. std::string flags = this->GetFlags(language);
  114. // Add Fortran format flags.
  115. if (language == "Fortran") {
  116. this->AppendFortranFormatFlags(flags, *source);
  117. }
  118. // Add source file specific flags.
  119. if (const char* cflags = source->GetProperty("COMPILE_FLAGS")) {
  120. std::string config = this->LocalGenerator->GetConfigName();
  121. cmGeneratorExpression ge;
  122. CM_AUTO_PTR<cmCompiledGeneratorExpression> cge = ge.Parse(cflags);
  123. const char* evaluatedFlags = cge->Evaluate(this->LocalGenerator, config,
  124. false, this->GeneratorTarget);
  125. this->LocalGenerator->AppendFlags(flags, evaluatedFlags);
  126. }
  127. return flags;
  128. }
  129. void cmNinjaTargetGenerator::AddIncludeFlags(std::string& languageFlags,
  130. std::string const& language)
  131. {
  132. std::vector<std::string> includes;
  133. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  134. language, this->GetConfigName());
  135. // Add include directory flags.
  136. std::string includeFlags = this->LocalGenerator->GetIncludeFlags(
  137. includes, this->GeneratorTarget, language,
  138. language == "RC", // full include paths for RC needed by cmcldeps
  139. false, this->GetConfigName());
  140. if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
  141. std::replace(includeFlags.begin(), includeFlags.end(), '\\', '/');
  142. }
  143. this->LocalGenerator->AppendFlags(languageFlags, includeFlags);
  144. }
  145. bool cmNinjaTargetGenerator::NeedDepTypeMSVC(const std::string& lang) const
  146. {
  147. return strcmp(this->GetMakefile()->GetSafeDefinition("CMAKE_NINJA_DEPTYPE_" +
  148. lang),
  149. "msvc") == 0;
  150. }
  151. // TODO: Refactor with
  152. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
  153. std::string cmNinjaTargetGenerator::ComputeDefines(cmSourceFile const* source,
  154. const std::string& language)
  155. {
  156. std::set<std::string> defines;
  157. this->LocalGenerator->AppendDefines(
  158. defines, source->GetProperty("COMPILE_DEFINITIONS"));
  159. {
  160. std::string defPropName = "COMPILE_DEFINITIONS_";
  161. defPropName += cmSystemTools::UpperCase(this->GetConfigName());
  162. this->LocalGenerator->AppendDefines(defines,
  163. source->GetProperty(defPropName));
  164. }
  165. std::string definesString = this->GetDefines(language);
  166. this->LocalGenerator->JoinDefines(defines, definesString, language);
  167. return definesString;
  168. }
  169. cmNinjaDeps cmNinjaTargetGenerator::ComputeLinkDeps() const
  170. {
  171. // Static libraries never depend on other targets for linking.
  172. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  173. this->GeneratorTarget->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  174. return cmNinjaDeps();
  175. }
  176. cmComputeLinkInformation* cli =
  177. this->GeneratorTarget->GetLinkInformation(this->GetConfigName());
  178. if (!cli) {
  179. return cmNinjaDeps();
  180. }
  181. const std::vector<std::string>& deps = cli->GetDepends();
  182. cmNinjaDeps result(deps.size());
  183. std::transform(deps.begin(), deps.end(), result.begin(), MapToNinjaPath());
  184. // Add a dependency on the link definitions file, if any.
  185. if (cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  186. this->GeneratorTarget->GetModuleDefinitionInfo(
  187. this->GetConfigName())) {
  188. for (std::vector<cmSourceFile const*>::const_iterator i =
  189. mdi->Sources.begin();
  190. i != mdi->Sources.end(); ++i) {
  191. result.push_back(this->ConvertToNinjaPath((*i)->GetFullPath()));
  192. }
  193. }
  194. // Add a dependency on user-specified manifest files, if any.
  195. std::vector<cmSourceFile const*> manifest_srcs;
  196. this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName);
  197. for (std::vector<cmSourceFile const*>::iterator mi = manifest_srcs.begin();
  198. mi != manifest_srcs.end(); ++mi) {
  199. result.push_back(this->ConvertToNinjaPath((*mi)->GetFullPath()));
  200. }
  201. // Add user-specified dependencies.
  202. if (const char* linkDepends =
  203. this->GeneratorTarget->GetProperty("LINK_DEPENDS")) {
  204. std::vector<std::string> linkDeps;
  205. cmSystemTools::ExpandListArgument(linkDepends, linkDeps);
  206. std::transform(linkDeps.begin(), linkDeps.end(),
  207. std::back_inserter(result), MapToNinjaPath());
  208. }
  209. return result;
  210. }
  211. std::string cmNinjaTargetGenerator::GetSourceFilePath(
  212. cmSourceFile const* source) const
  213. {
  214. return ConvertToNinjaPath(source->GetFullPath());
  215. }
  216. std::string cmNinjaTargetGenerator::GetObjectFilePath(
  217. cmSourceFile const* source) const
  218. {
  219. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  220. if (!path.empty()) {
  221. path += "/";
  222. }
  223. std::string const& objectName = this->GeneratorTarget->GetObjectName(source);
  224. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  225. path += "/";
  226. path += objectName;
  227. return path;
  228. }
  229. std::string cmNinjaTargetGenerator::GetPreprocessedFilePath(
  230. cmSourceFile const* source) const
  231. {
  232. // Choose an extension to compile already-preprocessed source.
  233. std::string ppExt = source->GetExtension();
  234. if (cmHasLiteralPrefix(ppExt, "F")) {
  235. // Some Fortran compilers automatically enable preprocessing for
  236. // upper-case extensions. Since the source is already preprocessed,
  237. // use a lower-case extension.
  238. ppExt = cmSystemTools::LowerCase(ppExt);
  239. }
  240. if (ppExt == "fpp") {
  241. // Some Fortran compilers automatically enable preprocessing for
  242. // the ".fpp" extension. Since the source is already preprocessed,
  243. // use the ".f" extension.
  244. ppExt = "f";
  245. }
  246. // Take the object file name and replace the extension.
  247. std::string const& objName = this->GeneratorTarget->GetObjectName(source);
  248. std::string const& objExt =
  249. this->GetGlobalGenerator()->GetLanguageOutputExtension(*source);
  250. assert(objName.size() >= objExt.size());
  251. std::string const ppName =
  252. objName.substr(0, objName.size() - objExt.size()) + "-pp." + ppExt;
  253. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  254. if (!path.empty()) {
  255. path += "/";
  256. }
  257. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  258. path += "/";
  259. path += ppName;
  260. return path;
  261. }
  262. std::string cmNinjaTargetGenerator::GetDyndepFilePath(
  263. std::string const& lang) const
  264. {
  265. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  266. if (!path.empty()) {
  267. path += "/";
  268. }
  269. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  270. path += "/";
  271. path += lang;
  272. path += ".dd";
  273. return path;
  274. }
  275. std::string cmNinjaTargetGenerator::GetTargetDependInfoPath(
  276. std::string const& lang) const
  277. {
  278. std::string path = this->Makefile->GetCurrentBinaryDirectory();
  279. path += "/";
  280. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  281. path += "/" + lang + "DependInfo.json";
  282. return path;
  283. }
  284. std::string cmNinjaTargetGenerator::GetTargetOutputDir() const
  285. {
  286. std::string dir = this->GeneratorTarget->GetDirectory(this->GetConfigName());
  287. return ConvertToNinjaPath(dir);
  288. }
  289. std::string cmNinjaTargetGenerator::GetTargetFilePath(
  290. const std::string& name) const
  291. {
  292. std::string path = this->GetTargetOutputDir();
  293. if (path.empty() || path == ".") {
  294. return name;
  295. }
  296. path += "/";
  297. path += name;
  298. return path;
  299. }
  300. std::string cmNinjaTargetGenerator::GetTargetName() const
  301. {
  302. return this->GeneratorTarget->GetName();
  303. }
  304. bool cmNinjaTargetGenerator::SetMsvcTargetPdbVariable(cmNinjaVars& vars) const
  305. {
  306. cmMakefile* mf = this->GetMakefile();
  307. if (mf->GetDefinition("MSVC_C_ARCHITECTURE_ID") ||
  308. mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID") ||
  309. mf->GetDefinition("MSVC_CUDA_ARCHITECTURE_ID")) {
  310. std::string pdbPath;
  311. std::string compilePdbPath = this->ComputeTargetCompilePDB();
  312. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE ||
  313. this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  314. this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  315. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  316. pdbPath = this->GeneratorTarget->GetPDBDirectory(this->GetConfigName());
  317. pdbPath += "/";
  318. pdbPath += this->GeneratorTarget->GetPDBName(this->GetConfigName());
  319. }
  320. vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  321. ConvertToNinjaPath(pdbPath), cmOutputConverter::SHELL);
  322. vars["TARGET_COMPILE_PDB"] =
  323. this->GetLocalGenerator()->ConvertToOutputFormat(
  324. ConvertToNinjaPath(compilePdbPath), cmOutputConverter::SHELL);
  325. EnsureParentDirectoryExists(pdbPath);
  326. EnsureParentDirectoryExists(compilePdbPath);
  327. return true;
  328. }
  329. return false;
  330. }
  331. void cmNinjaTargetGenerator::WriteLanguageRules(const std::string& language)
  332. {
  333. #ifdef NINJA_GEN_VERBOSE_FILES
  334. this->GetRulesFileStream() << "# Rules for language " << language << "\n\n";
  335. #endif
  336. this->WriteCompileRule(language);
  337. }
  338. void cmNinjaTargetGenerator::WriteCompileRule(const std::string& lang)
  339. {
  340. cmRulePlaceholderExpander::RuleVariables vars;
  341. vars.CMTargetName = this->GetGeneratorTarget()->GetName().c_str();
  342. vars.CMTargetType =
  343. cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType());
  344. vars.Language = lang.c_str();
  345. vars.Source = "$in";
  346. vars.Object = "$out";
  347. vars.Defines = "$DEFINES";
  348. vars.Includes = "$INCLUDES";
  349. vars.TargetPDB = "$TARGET_PDB";
  350. vars.TargetCompilePDB = "$TARGET_COMPILE_PDB";
  351. vars.ObjectDir = "$OBJECT_DIR";
  352. vars.ObjectFileDir = "$OBJECT_FILE_DIR";
  353. // For some cases we do an explicit preprocessor invocation.
  354. bool const explicitPP = this->NeedExplicitPreprocessing(lang);
  355. bool const needDyndep = this->NeedDyndep(lang);
  356. cmMakefile* mf = this->GetMakefile();
  357. std::string flags = "$FLAGS";
  358. std::string rspfile;
  359. std::string rspcontent;
  360. std::string responseFlag;
  361. bool const lang_supports_response = !(lang == "RC" || lang == "CUDA");
  362. if (lang_supports_response && this->ForceResponseFile()) {
  363. rspfile = "$RSP_FILE";
  364. responseFlag = "@" + rspfile;
  365. rspcontent = " $DEFINES $INCLUDES $FLAGS";
  366. flags = responseFlag;
  367. vars.Defines = "";
  368. vars.Includes = "";
  369. }
  370. // Tell ninja dependency format so all deps can be loaded into a database
  371. std::string deptype;
  372. std::string depfile;
  373. std::string cldeps;
  374. if (explicitPP) {
  375. // The explicit preprocessing step will handle dependency scanning.
  376. } else if (this->NeedDepTypeMSVC(lang)) {
  377. deptype = "msvc";
  378. depfile = "";
  379. flags += " /showIncludes";
  380. } else if (mf->IsOn("CMAKE_NINJA_CMCLDEPS_" + lang)) {
  381. // For the MS resource compiler we need cmcldeps, but skip dependencies
  382. // for source-file try_compile cases because they are always fresh.
  383. if (!mf->GetIsSourceFileTryCompile()) {
  384. deptype = "gcc";
  385. depfile = "$DEP_FILE";
  386. const std::string cl = mf->GetDefinition("CMAKE_C_COMPILER")
  387. ? mf->GetSafeDefinition("CMAKE_C_COMPILER")
  388. : mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
  389. cldeps = "\"";
  390. cldeps += cmSystemTools::GetCMClDepsCommand();
  391. cldeps += "\" " + lang + " " + vars.Source + " \"$DEP_FILE\" $out \"";
  392. cldeps += mf->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX");
  393. cldeps += "\" \"" + cl + "\" ";
  394. }
  395. } else {
  396. deptype = "gcc";
  397. const char* langdeptype = mf->GetDefinition("CMAKE_NINJA_DEPTYPE_" + lang);
  398. if (langdeptype) {
  399. deptype = langdeptype;
  400. }
  401. depfile = "$DEP_FILE";
  402. const std::string flagsName = "CMAKE_DEPFILE_FLAGS_" + lang;
  403. std::string depfileFlags = mf->GetSafeDefinition(flagsName);
  404. if (!depfileFlags.empty()) {
  405. cmSystemTools::ReplaceString(depfileFlags, "<DEPFILE>", "$DEP_FILE");
  406. cmSystemTools::ReplaceString(depfileFlags, "<OBJECT>", "$out");
  407. cmSystemTools::ReplaceString(depfileFlags, "<CMAKE_C_COMPILER>",
  408. mf->GetDefinition("CMAKE_C_COMPILER"));
  409. flags += " " + depfileFlags;
  410. }
  411. }
  412. vars.Flags = flags.c_str();
  413. vars.DependencyFile = depfile.c_str();
  414. CM_AUTO_PTR<cmRulePlaceholderExpander> rulePlaceholderExpander(
  415. this->GetLocalGenerator()->CreateRulePlaceholderExpander());
  416. std::string const tdi = this->GetLocalGenerator()->ConvertToOutputFormat(
  417. ConvertToNinjaPath(this->GetTargetDependInfoPath(lang)),
  418. cmLocalGenerator::SHELL);
  419. std::string launcher;
  420. const char* val = this->GetLocalGenerator()->GetRuleLauncher(
  421. this->GetGeneratorTarget(), "RULE_LAUNCH_COMPILE");
  422. if (val && *val) {
  423. launcher = val;
  424. launcher += " ";
  425. }
  426. if (explicitPP) {
  427. // Lookup the explicit preprocessing rule.
  428. std::string const ppVar = "CMAKE_" + lang + "_PREPROCESS_SOURCE";
  429. std::string const ppCmd =
  430. this->GetMakefile()->GetRequiredDefinition(ppVar);
  431. // Explicit preprocessing always uses a depfile.
  432. std::string const ppDeptype; // no deps= for multiple outputs
  433. std::string const ppDepfile = "$DEP_FILE";
  434. cmRulePlaceholderExpander::RuleVariables ppVars;
  435. ppVars.CMTargetName = vars.CMTargetName;
  436. ppVars.CMTargetType = vars.CMTargetType;
  437. ppVars.Language = vars.Language;
  438. ppVars.Object = "$out"; // for RULE_LAUNCH_COMPILE
  439. ppVars.PreprocessedSource = "$out";
  440. ppVars.DependencyFile = ppDepfile.c_str();
  441. // Preprocessing uses the original source,
  442. // compilation uses preprocessed output.
  443. ppVars.Source = vars.Source;
  444. vars.Source = "$in";
  445. // Preprocessing and compilation use the same flags.
  446. ppVars.Flags = vars.Flags;
  447. // Move preprocessor definitions to the preprocessor rule.
  448. ppVars.Defines = vars.Defines;
  449. vars.Defines = "";
  450. // Copy include directories to the preprocessor rule. The Fortran
  451. // compilation rule still needs them for the INCLUDE directive.
  452. ppVars.Includes = vars.Includes;
  453. // Rule for preprocessing source file.
  454. std::vector<std::string> ppCmds;
  455. cmSystemTools::ExpandListArgument(ppCmd, ppCmds);
  456. for (std::vector<std::string>::iterator i = ppCmds.begin();
  457. i != ppCmds.end(); ++i) {
  458. *i = launcher + *i;
  459. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(),
  460. *i, ppVars);
  461. }
  462. // Run CMake dependency scanner on preprocessed output.
  463. std::string const cmake = this->GetLocalGenerator()->ConvertToOutputFormat(
  464. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  465. ppCmds.push_back(
  466. cmake + " -E cmake_ninja_depends"
  467. " --tdi=" +
  468. tdi + " --pp=$out"
  469. " --dep=$DEP_FILE" +
  470. (needDyndep ? " --obj=$OBJ_FILE --ddi=$DYNDEP_INTERMEDIATE_FILE" : ""));
  471. std::string const ppCmdLine =
  472. this->GetLocalGenerator()->BuildCommandLine(ppCmds);
  473. // Write the rule for preprocessing file of the given language.
  474. std::ostringstream ppComment;
  475. ppComment << "Rule for preprocessing " << lang << " files.";
  476. std::ostringstream ppDesc;
  477. ppDesc << "Building " << lang << " preprocessed $out";
  478. this->GetGlobalGenerator()->AddRule(this->LanguagePreprocessRule(lang),
  479. ppCmdLine, ppDesc.str(),
  480. ppComment.str(), ppDepfile, ppDeptype,
  481. /*rspfile*/ "",
  482. /*rspcontent*/ "",
  483. /*restat*/ "",
  484. /*generator*/ false);
  485. }
  486. if (needDyndep) {
  487. // Write the rule for ninja dyndep file generation.
  488. std::vector<std::string> ddCmds;
  489. // Command line length is almost always limited -> use response file for
  490. // dyndep rules
  491. std::string ddRspFile = "$out.rsp";
  492. std::string ddRspContent = "$in";
  493. std::string ddInput = "@" + ddRspFile;
  494. // Run CMake dependency scanner on preprocessed output.
  495. std::string const cmake = this->GetLocalGenerator()->ConvertToOutputFormat(
  496. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  497. ddCmds.push_back(cmake + " -E cmake_ninja_dyndep"
  498. " --tdi=" +
  499. tdi + " --dd=$out"
  500. " " +
  501. ddInput);
  502. std::string const ddCmdLine =
  503. this->GetLocalGenerator()->BuildCommandLine(ddCmds);
  504. std::ostringstream ddComment;
  505. ddComment << "Rule to generate ninja dyndep files for " << lang << ".";
  506. std::ostringstream ddDesc;
  507. ddDesc << "Generating " << lang << " dyndep file $out";
  508. this->GetGlobalGenerator()->AddRule(
  509. this->LanguageDyndepRule(lang), ddCmdLine, ddDesc.str(), ddComment.str(),
  510. /*depfile*/ "",
  511. /*deps*/ "", ddRspFile, ddRspContent,
  512. /*restat*/ "",
  513. /*generator*/ false);
  514. }
  515. // Rule for compiling object file.
  516. std::vector<std::string> compileCmds;
  517. if (lang == "CUDA") {
  518. std::string cmdVar;
  519. if (this->GeneratorTarget->GetPropertyAsBool(
  520. "CUDA_SEPARABLE_COMPILATION")) {
  521. cmdVar = std::string("CMAKE_CUDA_COMPILE_SEPARABLE_COMPILATION");
  522. } else if (this->GeneratorTarget->GetPropertyAsBool(
  523. "CUDA_PTX_COMPILATION")) {
  524. cmdVar = std::string("CMAKE_CUDA_COMPILE_PTX_COMPILATION");
  525. } else {
  526. cmdVar = std::string("CMAKE_CUDA_COMPILE_WHOLE_COMPILATION");
  527. }
  528. std::string compileCmd = mf->GetRequiredDefinition(cmdVar);
  529. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  530. } else {
  531. const std::string cmdVar =
  532. std::string("CMAKE_") + lang + "_COMPILE_OBJECT";
  533. std::string compileCmd = mf->GetRequiredDefinition(cmdVar);
  534. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  535. }
  536. // Maybe insert an include-what-you-use runner.
  537. if (!compileCmds.empty() && (lang == "C" || lang == "CXX")) {
  538. std::string const iwyu_prop = lang + "_INCLUDE_WHAT_YOU_USE";
  539. const char* iwyu = this->GeneratorTarget->GetProperty(iwyu_prop);
  540. std::string const tidy_prop = lang + "_CLANG_TIDY";
  541. const char* tidy = this->GeneratorTarget->GetProperty(tidy_prop);
  542. std::string const cpplint_prop = lang + "_CPPLINT";
  543. const char* cpplint = this->GeneratorTarget->GetProperty(cpplint_prop);
  544. std::string const cppcheck_prop = lang + "_CPPCHECK";
  545. const char* cppcheck = this->GeneratorTarget->GetProperty(cppcheck_prop);
  546. if ((iwyu && *iwyu) || (tidy && *tidy) || (cpplint && *cpplint) ||
  547. (cppcheck && *cppcheck)) {
  548. std::string run_iwyu = this->GetLocalGenerator()->ConvertToOutputFormat(
  549. cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL);
  550. run_iwyu += " -E __run_co_compile";
  551. if (iwyu && *iwyu) {
  552. run_iwyu += " --iwyu=";
  553. run_iwyu += this->GetLocalGenerator()->EscapeForShell(iwyu);
  554. }
  555. if (tidy && *tidy) {
  556. run_iwyu += " --tidy=";
  557. run_iwyu += this->GetLocalGenerator()->EscapeForShell(tidy);
  558. }
  559. if (cpplint && *cpplint) {
  560. run_iwyu += " --cpplint=";
  561. run_iwyu += this->GetLocalGenerator()->EscapeForShell(cpplint);
  562. }
  563. if (cppcheck && *cppcheck) {
  564. run_iwyu += " --cppcheck=";
  565. run_iwyu += this->GetLocalGenerator()->EscapeForShell(cppcheck);
  566. }
  567. if ((tidy && *tidy) || (cpplint && *cpplint) ||
  568. (cppcheck && *cppcheck)) {
  569. run_iwyu += " --source=$in";
  570. }
  571. run_iwyu += " -- ";
  572. compileCmds.front().insert(0, run_iwyu);
  573. }
  574. }
  575. // Maybe insert a compiler launcher like ccache or distcc
  576. if (!compileCmds.empty() &&
  577. (lang == "C" || lang == "CXX" || lang == "CUDA")) {
  578. std::string const clauncher_prop = lang + "_COMPILER_LAUNCHER";
  579. const char* clauncher = this->GeneratorTarget->GetProperty(clauncher_prop);
  580. if (clauncher && *clauncher) {
  581. std::vector<std::string> launcher_cmd;
  582. cmSystemTools::ExpandListArgument(clauncher, launcher_cmd, true);
  583. for (std::vector<std::string>::iterator i = launcher_cmd.begin(),
  584. e = launcher_cmd.end();
  585. i != e; ++i) {
  586. *i = this->LocalGenerator->EscapeForShell(*i);
  587. }
  588. std::string const& run_launcher = cmJoin(launcher_cmd, " ") + " ";
  589. compileCmds.front().insert(0, run_launcher);
  590. }
  591. }
  592. if (!compileCmds.empty()) {
  593. compileCmds.front().insert(0, cldeps);
  594. }
  595. for (std::vector<std::string>::iterator i = compileCmds.begin();
  596. i != compileCmds.end(); ++i) {
  597. *i = launcher + *i;
  598. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(), *i,
  599. vars);
  600. }
  601. std::string cmdLine =
  602. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  603. // Write the rule for compiling file of the given language.
  604. std::ostringstream comment;
  605. comment << "Rule for compiling " << lang << " files.";
  606. std::ostringstream description;
  607. description << "Building " << lang << " object $out";
  608. this->GetGlobalGenerator()->AddRule(
  609. this->LanguageCompilerRule(lang), cmdLine, description.str(),
  610. comment.str(), depfile, deptype, rspfile, rspcontent,
  611. /*restat*/ "",
  612. /*generator*/ false);
  613. }
  614. void cmNinjaTargetGenerator::WriteObjectBuildStatements()
  615. {
  616. // Write comments.
  617. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  618. this->GetBuildFileStream()
  619. << "# Object build statements for "
  620. << cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType())
  621. << " target " << this->GetTargetName() << "\n\n";
  622. std::string config = this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  623. std::vector<cmSourceFile const*> customCommands;
  624. this->GeneratorTarget->GetCustomCommands(customCommands, config);
  625. for (std::vector<cmSourceFile const*>::const_iterator si =
  626. customCommands.begin();
  627. si != customCommands.end(); ++si) {
  628. cmCustomCommand const* cc = (*si)->GetCustomCommand();
  629. this->GetLocalGenerator()->AddCustomCommandTarget(
  630. cc, this->GetGeneratorTarget());
  631. // Record the custom commands for this target. The container is used
  632. // in WriteObjectBuildStatement when called in a loop below.
  633. this->CustomCommands.push_back(cc);
  634. }
  635. std::vector<cmSourceFile const*> headerSources;
  636. this->GeneratorTarget->GetHeaderSources(headerSources, config);
  637. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  638. headerSources, this->MacOSXContentGenerator);
  639. std::vector<cmSourceFile const*> extraSources;
  640. this->GeneratorTarget->GetExtraSources(extraSources, config);
  641. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  642. extraSources, this->MacOSXContentGenerator);
  643. std::vector<cmSourceFile const*> externalObjects;
  644. this->GeneratorTarget->GetExternalObjects(externalObjects, config);
  645. for (std::vector<cmSourceFile const*>::const_iterator si =
  646. externalObjects.begin();
  647. si != externalObjects.end(); ++si) {
  648. this->Objects.push_back(this->GetSourceFilePath(*si));
  649. }
  650. cmNinjaDeps orderOnlyDeps;
  651. this->GetLocalGenerator()->AppendTargetDepends(
  652. this->GeneratorTarget, orderOnlyDeps, DependOnTargetOrdering);
  653. // Add order-only dependencies on other files associated with the target.
  654. orderOnlyDeps.insert(orderOnlyDeps.end(), this->ExtraFiles.begin(),
  655. this->ExtraFiles.end());
  656. // Add order-only dependencies on custom command outputs.
  657. for (std::vector<cmCustomCommand const*>::const_iterator cci =
  658. this->CustomCommands.begin();
  659. cci != this->CustomCommands.end(); ++cci) {
  660. cmCustomCommand const* cc = *cci;
  661. cmCustomCommandGenerator ccg(*cc, this->GetConfigName(),
  662. this->GetLocalGenerator());
  663. const std::vector<std::string>& ccoutputs = ccg.GetOutputs();
  664. const std::vector<std::string>& ccbyproducts = ccg.GetByproducts();
  665. std::transform(ccoutputs.begin(), ccoutputs.end(),
  666. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  667. std::transform(ccbyproducts.begin(), ccbyproducts.end(),
  668. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  669. }
  670. std::sort(orderOnlyDeps.begin(), orderOnlyDeps.end());
  671. orderOnlyDeps.erase(std::unique(orderOnlyDeps.begin(), orderOnlyDeps.end()),
  672. orderOnlyDeps.end());
  673. {
  674. cmNinjaDeps orderOnlyTarget;
  675. orderOnlyTarget.push_back(this->OrderDependsTargetForTarget());
  676. this->GetGlobalGenerator()->WritePhonyBuild(
  677. this->GetBuildFileStream(),
  678. "Order-only phony target for " + this->GetTargetName(), orderOnlyTarget,
  679. cmNinjaDeps(), cmNinjaDeps(), orderOnlyDeps);
  680. }
  681. std::vector<cmSourceFile const*> objectSources;
  682. this->GeneratorTarget->GetObjectSources(objectSources, config);
  683. for (std::vector<cmSourceFile const*>::const_iterator si =
  684. objectSources.begin();
  685. si != objectSources.end(); ++si) {
  686. this->WriteObjectBuildStatement(*si);
  687. }
  688. if (!this->DDIFiles.empty()) {
  689. std::string const ddComment;
  690. std::string const ddRule = this->LanguageDyndepRule("Fortran");
  691. cmNinjaDeps ddOutputs;
  692. cmNinjaDeps ddImplicitOuts;
  693. cmNinjaDeps const& ddExplicitDeps = this->DDIFiles;
  694. cmNinjaDeps ddImplicitDeps;
  695. cmNinjaDeps ddOrderOnlyDeps;
  696. cmNinjaVars ddVars;
  697. this->WriteTargetDependInfo("Fortran");
  698. ddOutputs.push_back(this->GetDyndepFilePath("Fortran"));
  699. // Make sure dyndep files for all our dependencies have already
  700. // been generated so that the 'FortranModules.json' files they
  701. // produced as side-effects are available for us to read.
  702. // Ideally we should depend on the 'FortranModules.json' files
  703. // from our dependencies directly, but we don't know which of
  704. // our dependencies produces them. Fixing this will require
  705. // refactoring the Ninja generator to generate targets in
  706. // dependency order so that we can collect the needed information.
  707. this->GetLocalGenerator()->AppendTargetDepends(
  708. this->GeneratorTarget, ddOrderOnlyDeps, DependOnTargetArtifact);
  709. this->GetGlobalGenerator()->WriteBuild(
  710. this->GetBuildFileStream(), ddComment, ddRule, ddOutputs, ddImplicitOuts,
  711. ddExplicitDeps, ddImplicitDeps, ddOrderOnlyDeps, ddVars);
  712. }
  713. this->GetBuildFileStream() << "\n";
  714. }
  715. void cmNinjaTargetGenerator::WriteObjectBuildStatement(
  716. cmSourceFile const* source)
  717. {
  718. std::string const language = source->GetLanguage();
  719. std::string const sourceFileName =
  720. language == "RC" ? source->GetFullPath() : this->GetSourceFilePath(source);
  721. std::string const objectDir =
  722. this->ConvertToNinjaPath(this->GeneratorTarget->GetSupportDirectory());
  723. std::string const objectFileName =
  724. this->ConvertToNinjaPath(this->GetObjectFilePath(source));
  725. std::string const objectFileDir =
  726. cmSystemTools::GetFilenamePath(objectFileName);
  727. cmNinjaVars vars;
  728. vars["FLAGS"] = this->ComputeFlagsForObject(source, language);
  729. vars["DEFINES"] = this->ComputeDefines(source, language);
  730. vars["INCLUDES"] = this->GetIncludes(language);
  731. if (!this->NeedDepTypeMSVC(language)) {
  732. vars["DEP_FILE"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  733. objectFileName + ".d", cmOutputConverter::SHELL);
  734. }
  735. this->ExportObjectCompileCommand(
  736. language, sourceFileName, objectDir, objectFileName, objectFileDir,
  737. vars["FLAGS"], vars["DEFINES"], vars["INCLUDES"]);
  738. std::string comment;
  739. std::string rule = this->LanguageCompilerRule(language);
  740. cmNinjaDeps outputs;
  741. outputs.push_back(objectFileName);
  742. // Add this object to the list of object files.
  743. this->Objects.push_back(objectFileName);
  744. cmNinjaDeps explicitDeps;
  745. explicitDeps.push_back(sourceFileName);
  746. cmNinjaDeps implicitDeps;
  747. if (const char* objectDeps = source->GetProperty("OBJECT_DEPENDS")) {
  748. std::vector<std::string> depList;
  749. cmSystemTools::ExpandListArgument(objectDeps, depList);
  750. for (std::vector<std::string>::iterator odi = depList.begin();
  751. odi != depList.end(); ++odi) {
  752. if (cmSystemTools::FileIsFullPath(*odi)) {
  753. *odi = cmSystemTools::CollapseFullPath(*odi);
  754. }
  755. }
  756. std::transform(depList.begin(), depList.end(),
  757. std::back_inserter(implicitDeps), MapToNinjaPath());
  758. }
  759. cmNinjaDeps orderOnlyDeps;
  760. orderOnlyDeps.push_back(this->OrderDependsTargetForTarget());
  761. // If the source file is GENERATED and does not have a custom command
  762. // (either attached to this source file or another one), assume that one of
  763. // the target dependencies, OBJECT_DEPENDS or header file custom commands
  764. // will rebuild the file.
  765. if (source->GetPropertyAsBool("GENERATED") && !source->GetCustomCommand() &&
  766. !this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFileName)) {
  767. this->GetGlobalGenerator()->AddAssumedSourceDependencies(sourceFileName,
  768. orderOnlyDeps);
  769. }
  770. // For some cases we need to generate a ninja dyndep file.
  771. bool const needDyndep = this->NeedDyndep(language);
  772. // For some cases we do an explicit preprocessor invocation.
  773. bool const explicitPP = this->NeedExplicitPreprocessing(language);
  774. if (explicitPP) {
  775. std::string const ppComment;
  776. std::string const ppRule = this->LanguagePreprocessRule(language);
  777. cmNinjaDeps ppOutputs;
  778. cmNinjaDeps ppImplicitOuts;
  779. cmNinjaDeps ppExplicitDeps;
  780. cmNinjaDeps ppImplicitDeps;
  781. cmNinjaDeps ppOrderOnlyDeps;
  782. cmNinjaVars ppVars;
  783. std::string const ppFileName =
  784. this->ConvertToNinjaPath(this->GetPreprocessedFilePath(source));
  785. ppOutputs.push_back(ppFileName);
  786. // Move compilation dependencies to the preprocessing build statement.
  787. std::swap(ppExplicitDeps, explicitDeps);
  788. std::swap(ppImplicitDeps, implicitDeps);
  789. std::swap(ppOrderOnlyDeps, orderOnlyDeps);
  790. std::swap(ppVars["IN_ABS"], vars["IN_ABS"]);
  791. // The actual compilation will now use the preprocessed source.
  792. explicitDeps.push_back(ppFileName);
  793. // Preprocessing and compilation generally use the same flags.
  794. ppVars["FLAGS"] = vars["FLAGS"];
  795. // In case compilation requires flags that are incompatible with
  796. // preprocessing, include them here.
  797. std::string const postFlag =
  798. this->Makefile->GetSafeDefinition("CMAKE_Fortran_POSTPROCESS_FLAG");
  799. this->LocalGenerator->AppendFlags(vars["FLAGS"], postFlag);
  800. // Move preprocessor definitions to the preprocessor build statement.
  801. std::swap(ppVars["DEFINES"], vars["DEFINES"]);
  802. // Copy include directories to the preprocessor build statement. The
  803. // Fortran compilation build statement still needs them for the INCLUDE
  804. // directive.
  805. ppVars["INCLUDES"] = vars["INCLUDES"];
  806. // Prepend source file's original directory as an include directory
  807. // so e.g. Fortran INCLUDE statements can look for files in it.
  808. std::vector<std::string> sourceDirectory;
  809. sourceDirectory.push_back(
  810. cmSystemTools::GetParentDirectory(source->GetFullPath()));
  811. std::string sourceDirectoryFlag = this->LocalGenerator->GetIncludeFlags(
  812. sourceDirectory, this->GeneratorTarget, language, false, false,
  813. this->GetConfigName());
  814. vars["INCLUDES"] = sourceDirectoryFlag + " " + vars["INCLUDES"];
  815. // Explicit preprocessing always uses a depfile.
  816. ppVars["DEP_FILE"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  817. ppFileName + ".d", cmOutputConverter::SHELL);
  818. // The actual compilation does not need a depfile because it
  819. // depends on the already-preprocessed source.
  820. vars.erase("DEP_FILE");
  821. if (needDyndep) {
  822. // Tell dependency scanner the object file that will result from
  823. // compiling the preprocessed source.
  824. ppVars["OBJ_FILE"] = objectFileName;
  825. // Tell dependency scanner where to store dyndep intermediate results.
  826. std::string const ddiFile = ppFileName + ".ddi";
  827. ppVars["DYNDEP_INTERMEDIATE_FILE"] = ddiFile;
  828. ppImplicitOuts.push_back(ddiFile);
  829. this->DDIFiles.push_back(ddiFile);
  830. }
  831. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
  832. ppVars);
  833. this->GetGlobalGenerator()->WriteBuild(
  834. this->GetBuildFileStream(), ppComment, ppRule, ppOutputs, ppImplicitOuts,
  835. ppExplicitDeps, ppImplicitDeps, ppOrderOnlyDeps, ppVars);
  836. }
  837. if (needDyndep) {
  838. std::string const dyndep = this->GetDyndepFilePath(language);
  839. orderOnlyDeps.push_back(dyndep);
  840. vars["dyndep"] = dyndep;
  841. }
  842. EnsureParentDirectoryExists(objectFileName);
  843. vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  844. objectDir, cmOutputConverter::SHELL);
  845. vars["OBJECT_FILE_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  846. objectFileDir, cmOutputConverter::SHELL);
  847. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
  848. vars);
  849. this->SetMsvcTargetPdbVariable(vars);
  850. bool const lang_supports_response =
  851. !(language == "RC" || language == "CUDA");
  852. int const commandLineLengthLimit =
  853. ((lang_supports_response && this->ForceResponseFile())) ? -1 : 0;
  854. std::string const rspfile = objectFileName + ".rsp";
  855. this->GetGlobalGenerator()->WriteBuild(
  856. this->GetBuildFileStream(), comment, rule, outputs,
  857. /*implicitOuts=*/cmNinjaDeps(), explicitDeps, implicitDeps, orderOnlyDeps,
  858. vars, rspfile, commandLineLengthLimit);
  859. if (const char* objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
  860. std::vector<std::string> outputList;
  861. cmSystemTools::ExpandListArgument(objectOutputs, outputList);
  862. std::transform(outputList.begin(), outputList.end(), outputList.begin(),
  863. MapToNinjaPath());
  864. this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
  865. "Additional output files.",
  866. outputList, outputs);
  867. }
  868. }
  869. void cmNinjaTargetGenerator::WriteTargetDependInfo(std::string const& lang)
  870. {
  871. Json::Value tdi(Json::objectValue);
  872. tdi["language"] = lang;
  873. tdi["compiler-id"] =
  874. this->Makefile->GetSafeDefinition("CMAKE_" + lang + "_COMPILER_ID");
  875. if (lang == "Fortran") {
  876. std::string mod_dir = this->GeneratorTarget->GetFortranModuleDirectory(
  877. this->Makefile->GetHomeOutputDirectory());
  878. if (mod_dir.empty()) {
  879. mod_dir = this->Makefile->GetCurrentBinaryDirectory();
  880. }
  881. tdi["module-dir"] = mod_dir;
  882. }
  883. tdi["dir-cur-bld"] = this->Makefile->GetCurrentBinaryDirectory();
  884. tdi["dir-cur-src"] = this->Makefile->GetCurrentSourceDirectory();
  885. tdi["dir-top-bld"] = this->Makefile->GetHomeOutputDirectory();
  886. tdi["dir-top-src"] = this->Makefile->GetHomeDirectory();
  887. Json::Value& tdi_include_dirs = tdi["include-dirs"] = Json::arrayValue;
  888. std::vector<std::string> includes;
  889. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  890. lang, this->GetConfigName());
  891. for (std::vector<std::string>::iterator i = includes.begin();
  892. i != includes.end(); ++i) {
  893. // Convert the include directories the same way we do for -I flags.
  894. // See upstream ninja issue 1251.
  895. tdi_include_dirs.append(this->ConvertToNinjaPath(*i));
  896. }
  897. Json::Value& tdi_linked_target_dirs = tdi["linked-target-dirs"] =
  898. Json::arrayValue;
  899. std::vector<std::string> linked = this->GetLinkedTargetDirectories();
  900. for (std::vector<std::string>::iterator i = linked.begin();
  901. i != linked.end(); ++i) {
  902. tdi_linked_target_dirs.append(*i);
  903. }
  904. std::string const tdin = this->GetTargetDependInfoPath(lang);
  905. cmGeneratedFileStream tdif(tdin.c_str());
  906. tdif << tdi;
  907. }
  908. void cmNinjaTargetGenerator::ExportObjectCompileCommand(
  909. std::string const& language, std::string const& sourceFileName,
  910. std::string const& objectDir, std::string const& objectFileName,
  911. std::string const& objectFileDir, std::string const& flags,
  912. std::string const& defines, std::string const& includes)
  913. {
  914. if (!this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS")) {
  915. return;
  916. }
  917. cmRulePlaceholderExpander::RuleVariables compileObjectVars;
  918. compileObjectVars.Language = language.c_str();
  919. std::string escapedSourceFileName = sourceFileName;
  920. if (!cmSystemTools::FileIsFullPath(sourceFileName.c_str())) {
  921. escapedSourceFileName = cmSystemTools::CollapseFullPath(
  922. escapedSourceFileName, this->GetGlobalGenerator()
  923. ->GetCMakeInstance()
  924. ->GetHomeOutputDirectory());
  925. }
  926. escapedSourceFileName = this->LocalGenerator->ConvertToOutputFormat(
  927. escapedSourceFileName, cmOutputConverter::SHELL);
  928. compileObjectVars.Source = escapedSourceFileName.c_str();
  929. compileObjectVars.Object = objectFileName.c_str();
  930. compileObjectVars.ObjectDir = objectDir.c_str();
  931. compileObjectVars.ObjectFileDir = objectFileDir.c_str();
  932. compileObjectVars.Flags = flags.c_str();
  933. compileObjectVars.Defines = defines.c_str();
  934. compileObjectVars.Includes = includes.c_str();
  935. // Rule for compiling object file.
  936. std::vector<std::string> compileCmds;
  937. if (language == "CUDA") {
  938. std::string cmdVar;
  939. if (this->GeneratorTarget->GetPropertyAsBool(
  940. "CUDA_SEPARABLE_COMPILATION")) {
  941. cmdVar = std::string("CMAKE_CUDA_COMPILE_SEPARABLE_COMPILATION");
  942. } else if (this->GeneratorTarget->GetPropertyAsBool(
  943. "CUDA_PTX_COMPILATION")) {
  944. cmdVar = std::string("CMAKE_CUDA_COMPILE_PTX_COMPILATION");
  945. } else {
  946. cmdVar = std::string("CMAKE_CUDA_COMPILE_WHOLE_COMPILATION");
  947. }
  948. std::string compileCmd =
  949. this->GetMakefile()->GetRequiredDefinition(cmdVar);
  950. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  951. } else {
  952. const std::string cmdVar =
  953. std::string("CMAKE_") + language + "_COMPILE_OBJECT";
  954. std::string compileCmd =
  955. this->GetMakefile()->GetRequiredDefinition(cmdVar);
  956. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  957. }
  958. CM_AUTO_PTR<cmRulePlaceholderExpander> rulePlaceholderExpander(
  959. this->GetLocalGenerator()->CreateRulePlaceholderExpander());
  960. for (std::vector<std::string>::iterator i = compileCmds.begin();
  961. i != compileCmds.end(); ++i) {
  962. // no launcher for CMAKE_EXPORT_COMPILE_COMMANDS
  963. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(), *i,
  964. compileObjectVars);
  965. }
  966. std::string cmdLine =
  967. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  968. this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine, sourceFileName);
  969. }
  970. void cmNinjaTargetGenerator::EnsureDirectoryExists(
  971. const std::string& path) const
  972. {
  973. if (cmSystemTools::FileIsFullPath(path.c_str())) {
  974. cmSystemTools::MakeDirectory(path.c_str());
  975. } else {
  976. cmGlobalNinjaGenerator* gg = this->GetGlobalGenerator();
  977. std::string fullPath =
  978. std::string(gg->GetCMakeInstance()->GetHomeOutputDirectory());
  979. // Also ensures their is a trailing slash.
  980. gg->StripNinjaOutputPathPrefixAsSuffix(fullPath);
  981. fullPath += path;
  982. cmSystemTools::MakeDirectory(fullPath.c_str());
  983. }
  984. }
  985. void cmNinjaTargetGenerator::EnsureParentDirectoryExists(
  986. const std::string& path) const
  987. {
  988. EnsureDirectoryExists(cmSystemTools::GetParentDirectory(path));
  989. }
  990. void cmNinjaTargetGenerator::MacOSXContentGeneratorType::operator()(
  991. cmSourceFile const& source, const char* pkgloc)
  992. {
  993. // Skip OS X content when not building a Framework or Bundle.
  994. if (!this->Generator->GetGeneratorTarget()->IsBundleOnApple()) {
  995. return;
  996. }
  997. std::string macdir =
  998. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc);
  999. // Get the input file location.
  1000. std::string input = source.GetFullPath();
  1001. input = this->Generator->GetGlobalGenerator()->ConvertToNinjaPath(input);
  1002. // Get the output file location.
  1003. std::string output = macdir;
  1004. output += "/";
  1005. output += cmSystemTools::GetFilenameName(input);
  1006. output = this->Generator->GetGlobalGenerator()->ConvertToNinjaPath(output);
  1007. // Write a build statement to copy the content into the bundle.
  1008. this->Generator->GetGlobalGenerator()->WriteMacOSXContentBuild(input,
  1009. output);
  1010. // Add as a dependency to the target so that it gets called.
  1011. this->Generator->ExtraFiles.push_back(output);
  1012. }
  1013. void cmNinjaTargetGenerator::addPoolNinjaVariable(
  1014. const std::string& pool_property, cmGeneratorTarget* target,
  1015. cmNinjaVars& vars)
  1016. {
  1017. const char* pool = target->GetProperty(pool_property);
  1018. if (pool) {
  1019. vars["pool"] = pool;
  1020. }
  1021. }
  1022. bool cmNinjaTargetGenerator::ForceResponseFile()
  1023. {
  1024. static std::string const forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
  1025. return (this->GetMakefile()->IsDefinitionSet(forceRspFile) ||
  1026. cmSystemTools::HasEnv(forceRspFile));
  1027. }