cmNinjaTargetGenerator.cxx 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmNinjaTargetGenerator.h"
  4. #include <algorithm>
  5. #include <assert.h>
  6. #include <cm_jsoncpp_value.h>
  7. #include <cm_jsoncpp_writer.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 CM_NULLPTR;
  46. }
  47. }
  48. cmNinjaTargetGenerator::cmNinjaTargetGenerator(cmGeneratorTarget* target)
  49. : cmCommonTargetGenerator(target)
  50. , MacOSXContentGenerator(CM_NULLPTR)
  51. , OSXBundleGenerator(CM_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_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 (this->ModuleDefinitionFile) {
  186. result.push_back(
  187. this->ConvertToNinjaPath(this->ModuleDefinitionFile->GetFullPath()));
  188. }
  189. // Add a dependency on user-specified manifest files, if any.
  190. std::vector<cmSourceFile const*> manifest_srcs;
  191. this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName);
  192. for (std::vector<cmSourceFile const*>::iterator mi = manifest_srcs.begin();
  193. mi != manifest_srcs.end(); ++mi) {
  194. result.push_back(this->ConvertToNinjaPath((*mi)->GetFullPath()));
  195. }
  196. // Add user-specified dependencies.
  197. if (const char* linkDepends =
  198. this->GeneratorTarget->GetProperty("LINK_DEPENDS")) {
  199. std::vector<std::string> linkDeps;
  200. cmSystemTools::ExpandListArgument(linkDepends, linkDeps);
  201. std::transform(linkDeps.begin(), linkDeps.end(),
  202. std::back_inserter(result), MapToNinjaPath());
  203. }
  204. return result;
  205. }
  206. std::string cmNinjaTargetGenerator::GetSourceFilePath(
  207. cmSourceFile const* source) const
  208. {
  209. return ConvertToNinjaPath(source->GetFullPath());
  210. }
  211. std::string cmNinjaTargetGenerator::GetObjectFilePath(
  212. cmSourceFile const* source) const
  213. {
  214. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  215. if (!path.empty()) {
  216. path += "/";
  217. }
  218. std::string const& objectName = this->GeneratorTarget->GetObjectName(source);
  219. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  220. path += "/";
  221. path += objectName;
  222. return path;
  223. }
  224. std::string cmNinjaTargetGenerator::GetPreprocessedFilePath(
  225. cmSourceFile const* source) const
  226. {
  227. // Choose an extension to compile already-preprocessed source.
  228. std::string ppExt = source->GetExtension();
  229. if (cmHasLiteralPrefix(ppExt, "F")) {
  230. // Some Fortran compilers automatically enable preprocessing for
  231. // upper-case extensions. Since the source is already preprocessed,
  232. // use a lower-case extension.
  233. ppExt = cmSystemTools::LowerCase(ppExt);
  234. }
  235. if (ppExt == "fpp") {
  236. // Some Fortran compilers automatically enable preprocessing for
  237. // the ".fpp" extension. Since the source is already preprocessed,
  238. // use the ".f" extension.
  239. ppExt = "f";
  240. }
  241. // Take the object file name and replace the extension.
  242. std::string const& objName = this->GeneratorTarget->GetObjectName(source);
  243. std::string const& objExt =
  244. this->GetGlobalGenerator()->GetLanguageOutputExtension(*source);
  245. assert(objName.size() >= objExt.size());
  246. std::string const ppName =
  247. objName.substr(0, objName.size() - objExt.size()) + "-pp." + ppExt;
  248. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  249. if (!path.empty()) {
  250. path += "/";
  251. }
  252. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  253. path += "/";
  254. path += ppName;
  255. return path;
  256. }
  257. std::string cmNinjaTargetGenerator::GetDyndepFilePath(
  258. std::string const& lang) const
  259. {
  260. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  261. if (!path.empty()) {
  262. path += "/";
  263. }
  264. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  265. path += "/";
  266. path += lang;
  267. path += ".dd";
  268. return path;
  269. }
  270. std::string cmNinjaTargetGenerator::GetTargetDependInfoPath(
  271. std::string const& lang) const
  272. {
  273. std::string path = this->Makefile->GetCurrentBinaryDirectory();
  274. path += "/";
  275. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  276. path += "/" + lang + "DependInfo.json";
  277. return path;
  278. }
  279. std::string cmNinjaTargetGenerator::GetTargetOutputDir() const
  280. {
  281. std::string dir = this->GeneratorTarget->GetDirectory(this->GetConfigName());
  282. return ConvertToNinjaPath(dir);
  283. }
  284. std::string cmNinjaTargetGenerator::GetTargetFilePath(
  285. const std::string& name) const
  286. {
  287. std::string path = this->GetTargetOutputDir();
  288. if (path.empty() || path == ".") {
  289. return name;
  290. }
  291. path += "/";
  292. path += name;
  293. return path;
  294. }
  295. std::string cmNinjaTargetGenerator::GetTargetName() const
  296. {
  297. return this->GeneratorTarget->GetName();
  298. }
  299. bool cmNinjaTargetGenerator::SetMsvcTargetPdbVariable(cmNinjaVars& vars) const
  300. {
  301. cmMakefile* mf = this->GetMakefile();
  302. if (mf->GetDefinition("MSVC_C_ARCHITECTURE_ID") ||
  303. mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID")) {
  304. std::string pdbPath;
  305. std::string compilePdbPath;
  306. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE ||
  307. this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  308. this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  309. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  310. pdbPath = this->GeneratorTarget->GetPDBDirectory(this->GetConfigName());
  311. pdbPath += "/";
  312. pdbPath += this->GeneratorTarget->GetPDBName(this->GetConfigName());
  313. }
  314. if (this->GeneratorTarget->GetType() <= cmStateEnums::OBJECT_LIBRARY) {
  315. compilePdbPath =
  316. this->GeneratorTarget->GetCompilePDBPath(this->GetConfigName());
  317. if (compilePdbPath.empty()) {
  318. compilePdbPath = this->GeneratorTarget->GetSupportDirectory() + "/";
  319. }
  320. }
  321. vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  322. ConvertToNinjaPath(pdbPath), cmOutputConverter::SHELL);
  323. vars["TARGET_COMPILE_PDB"] =
  324. this->GetLocalGenerator()->ConvertToOutputFormat(
  325. ConvertToNinjaPath(compilePdbPath), cmOutputConverter::SHELL);
  326. EnsureParentDirectoryExists(pdbPath);
  327. EnsureParentDirectoryExists(compilePdbPath);
  328. return true;
  329. }
  330. return false;
  331. }
  332. void cmNinjaTargetGenerator::WriteLanguageRules(const std::string& language)
  333. {
  334. #ifdef NINJA_GEN_VERBOSE_FILES
  335. this->GetRulesFileStream() << "# Rules for language " << language << "\n\n";
  336. #endif
  337. this->WriteCompileRule(language);
  338. }
  339. void cmNinjaTargetGenerator::WriteCompileRule(const std::string& lang)
  340. {
  341. cmRulePlaceholderExpander::RuleVariables vars;
  342. vars.CMTargetName = this->GetGeneratorTarget()->GetName().c_str();
  343. vars.CMTargetType =
  344. cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType());
  345. vars.Language = lang.c_str();
  346. vars.Source = "$IN_ABS";
  347. vars.Object = "$out";
  348. vars.Defines = "$DEFINES";
  349. vars.Includes = "$INCLUDES";
  350. vars.TargetPDB = "$TARGET_PDB";
  351. vars.TargetCompilePDB = "$TARGET_COMPILE_PDB";
  352. vars.ObjectDir = "$OBJECT_DIR";
  353. vars.ObjectFileDir = "$OBJECT_FILE_DIR";
  354. // For some cases we do an explicit preprocessor invocation.
  355. bool const explicitPP = this->NeedExplicitPreprocessing(lang);
  356. bool const needDyndep = this->NeedDyndep(lang);
  357. cmMakefile* mf = this->GetMakefile();
  358. std::string flags = "$FLAGS";
  359. std::string rspfile;
  360. std::string rspcontent;
  361. std::string responseFlag;
  362. if (lang != "RC" && 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. // Run CMake dependency scanner on preprocessed output.
  490. std::string const cmake = this->GetLocalGenerator()->ConvertToOutputFormat(
  491. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  492. ddCmds.push_back(cmake + " -E cmake_ninja_dyndep"
  493. " --tdi=" +
  494. tdi + " --dd=$out"
  495. " $in");
  496. std::string const ddCmdLine =
  497. this->GetLocalGenerator()->BuildCommandLine(ddCmds);
  498. std::ostringstream ddComment;
  499. ddComment << "Rule to generate ninja dyndep files for " << lang << ".";
  500. std::ostringstream ddDesc;
  501. ddDesc << "Generating " << lang << " dyndep file $out";
  502. this->GetGlobalGenerator()->AddRule(
  503. this->LanguageDyndepRule(lang), ddCmdLine, ddDesc.str(), ddComment.str(),
  504. /*depfile*/ "",
  505. /*deps*/ "",
  506. /*rspfile*/ "",
  507. /*rspcontent*/ "",
  508. /*restat*/ "",
  509. /*generator*/ false);
  510. }
  511. // Rule for compiling object file.
  512. const std::string cmdVar = std::string("CMAKE_") + lang + "_COMPILE_OBJECT";
  513. std::string compileCmd = mf->GetRequiredDefinition(cmdVar);
  514. std::vector<std::string> compileCmds;
  515. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  516. // Maybe insert an include-what-you-use runner.
  517. if (!compileCmds.empty() && (lang == "C" || lang == "CXX")) {
  518. std::string const iwyu_prop = lang + "_INCLUDE_WHAT_YOU_USE";
  519. const char* iwyu = this->GeneratorTarget->GetProperty(iwyu_prop);
  520. std::string const tidy_prop = lang + "_CLANG_TIDY";
  521. const char* tidy = this->GeneratorTarget->GetProperty(tidy_prop);
  522. if ((iwyu && *iwyu) || (tidy && *tidy)) {
  523. std::string run_iwyu = this->GetLocalGenerator()->ConvertToOutputFormat(
  524. cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL);
  525. run_iwyu += " -E __run_iwyu";
  526. if (iwyu && *iwyu) {
  527. run_iwyu += " --iwyu=";
  528. run_iwyu += this->GetLocalGenerator()->EscapeForShell(iwyu);
  529. }
  530. if (tidy && *tidy) {
  531. run_iwyu += " --tidy=";
  532. run_iwyu += this->GetLocalGenerator()->EscapeForShell(tidy);
  533. run_iwyu += " --source=$in";
  534. }
  535. run_iwyu += " -- ";
  536. compileCmds.front().insert(0, run_iwyu);
  537. }
  538. }
  539. // Maybe insert a compiler launcher like ccache or distcc
  540. if (!compileCmds.empty() && (lang == "C" || lang == "CXX")) {
  541. std::string const clauncher_prop = lang + "_COMPILER_LAUNCHER";
  542. const char* clauncher = this->GeneratorTarget->GetProperty(clauncher_prop);
  543. if (clauncher && *clauncher) {
  544. std::vector<std::string> launcher_cmd;
  545. cmSystemTools::ExpandListArgument(clauncher, launcher_cmd, true);
  546. for (std::vector<std::string>::iterator i = launcher_cmd.begin(),
  547. e = launcher_cmd.end();
  548. i != e; ++i) {
  549. *i = this->LocalGenerator->EscapeForShell(*i);
  550. }
  551. std::string const& run_launcher = cmJoin(launcher_cmd, " ") + " ";
  552. compileCmds.front().insert(0, run_launcher);
  553. }
  554. }
  555. if (!compileCmds.empty()) {
  556. compileCmds.front().insert(0, cldeps);
  557. }
  558. for (std::vector<std::string>::iterator i = compileCmds.begin();
  559. i != compileCmds.end(); ++i) {
  560. *i = launcher + *i;
  561. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(), *i,
  562. vars);
  563. }
  564. std::string cmdLine =
  565. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  566. // Write the rule for compiling file of the given language.
  567. std::ostringstream comment;
  568. comment << "Rule for compiling " << lang << " files.";
  569. std::ostringstream description;
  570. description << "Building " << lang << " object $out";
  571. this->GetGlobalGenerator()->AddRule(
  572. this->LanguageCompilerRule(lang), cmdLine, description.str(),
  573. comment.str(), depfile, deptype, rspfile, rspcontent,
  574. /*restat*/ "",
  575. /*generator*/ false);
  576. }
  577. void cmNinjaTargetGenerator::WriteObjectBuildStatements()
  578. {
  579. // Write comments.
  580. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  581. this->GetBuildFileStream()
  582. << "# Object build statements for "
  583. << cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType())
  584. << " target " << this->GetTargetName() << "\n\n";
  585. std::string config = this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  586. std::vector<cmSourceFile const*> customCommands;
  587. this->GeneratorTarget->GetCustomCommands(customCommands, config);
  588. for (std::vector<cmSourceFile const*>::const_iterator si =
  589. customCommands.begin();
  590. si != customCommands.end(); ++si) {
  591. cmCustomCommand const* cc = (*si)->GetCustomCommand();
  592. this->GetLocalGenerator()->AddCustomCommandTarget(
  593. cc, this->GetGeneratorTarget());
  594. // Record the custom commands for this target. The container is used
  595. // in WriteObjectBuildStatement when called in a loop below.
  596. this->CustomCommands.push_back(cc);
  597. }
  598. std::vector<cmSourceFile const*> headerSources;
  599. this->GeneratorTarget->GetHeaderSources(headerSources, config);
  600. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  601. headerSources, this->MacOSXContentGenerator);
  602. std::vector<cmSourceFile const*> extraSources;
  603. this->GeneratorTarget->GetExtraSources(extraSources, config);
  604. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  605. extraSources, this->MacOSXContentGenerator);
  606. std::vector<cmSourceFile const*> externalObjects;
  607. this->GeneratorTarget->GetExternalObjects(externalObjects, config);
  608. for (std::vector<cmSourceFile const*>::const_iterator si =
  609. externalObjects.begin();
  610. si != externalObjects.end(); ++si) {
  611. this->Objects.push_back(this->GetSourceFilePath(*si));
  612. }
  613. cmNinjaDeps orderOnlyDeps;
  614. this->GetLocalGenerator()->AppendTargetDepends(this->GeneratorTarget,
  615. orderOnlyDeps);
  616. // Add order-only dependencies on other files associated with the target.
  617. orderOnlyDeps.insert(orderOnlyDeps.end(), this->ExtraFiles.begin(),
  618. this->ExtraFiles.end());
  619. // Add order-only dependencies on custom command outputs.
  620. for (std::vector<cmCustomCommand const*>::const_iterator cci =
  621. this->CustomCommands.begin();
  622. cci != this->CustomCommands.end(); ++cci) {
  623. cmCustomCommand const* cc = *cci;
  624. cmCustomCommandGenerator ccg(*cc, this->GetConfigName(),
  625. this->GetLocalGenerator());
  626. const std::vector<std::string>& ccoutputs = ccg.GetOutputs();
  627. const std::vector<std::string>& ccbyproducts = ccg.GetByproducts();
  628. std::transform(ccoutputs.begin(), ccoutputs.end(),
  629. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  630. std::transform(ccbyproducts.begin(), ccbyproducts.end(),
  631. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  632. }
  633. if (!orderOnlyDeps.empty()) {
  634. cmNinjaDeps orderOnlyTarget;
  635. orderOnlyTarget.push_back(this->OrderDependsTargetForTarget());
  636. this->GetGlobalGenerator()->WritePhonyBuild(
  637. this->GetBuildFileStream(),
  638. "Order-only phony target for " + this->GetTargetName(), orderOnlyTarget,
  639. cmNinjaDeps(), cmNinjaDeps(), orderOnlyDeps);
  640. }
  641. std::vector<cmSourceFile const*> objectSources;
  642. this->GeneratorTarget->GetObjectSources(objectSources, config);
  643. for (std::vector<cmSourceFile const*>::const_iterator si =
  644. objectSources.begin();
  645. si != objectSources.end(); ++si) {
  646. this->WriteObjectBuildStatement(*si, !orderOnlyDeps.empty());
  647. }
  648. if (!this->DDIFiles.empty()) {
  649. std::string const ddComment;
  650. std::string const ddRule = this->LanguageDyndepRule("Fortran");
  651. cmNinjaDeps ddOutputs;
  652. cmNinjaDeps ddImplicitOuts;
  653. cmNinjaDeps const& ddExplicitDeps = this->DDIFiles;
  654. cmNinjaDeps ddImplicitDeps;
  655. cmNinjaDeps ddOrderOnlyDeps;
  656. cmNinjaVars ddVars;
  657. this->WriteTargetDependInfo("Fortran");
  658. ddOutputs.push_back(this->GetDyndepFilePath("Fortran"));
  659. this->GetGlobalGenerator()->WriteBuild(
  660. this->GetBuildFileStream(), ddComment, ddRule, ddOutputs, ddImplicitOuts,
  661. ddExplicitDeps, ddImplicitDeps, ddOrderOnlyDeps, ddVars);
  662. }
  663. this->GetBuildFileStream() << "\n";
  664. }
  665. void cmNinjaTargetGenerator::WriteObjectBuildStatement(
  666. cmSourceFile const* source, bool writeOrderDependsTargetForTarget)
  667. {
  668. std::string const language = source->GetLanguage();
  669. std::string const sourceFileName = this->GetSourceFilePath(source);
  670. std::string const objectDir =
  671. this->ConvertToNinjaPath(this->GeneratorTarget->GetSupportDirectory());
  672. std::string const objectFileName =
  673. this->ConvertToNinjaPath(this->GetObjectFilePath(source));
  674. std::string const objectFileDir =
  675. cmSystemTools::GetFilenamePath(objectFileName);
  676. cmNinjaVars vars;
  677. vars["IN_ABS"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  678. source->GetFullPath(), cmOutputConverter::SHELL);
  679. vars["FLAGS"] = this->ComputeFlagsForObject(source, language);
  680. vars["DEFINES"] = this->ComputeDefines(source, language);
  681. vars["INCLUDES"] = this->GetIncludes(language);
  682. if (!this->NeedDepTypeMSVC(language)) {
  683. vars["DEP_FILE"] =
  684. cmGlobalNinjaGenerator::EncodeDepfileSpace(objectFileName + ".d");
  685. }
  686. this->ExportObjectCompileCommand(
  687. language, sourceFileName, objectDir, objectFileName, objectFileDir,
  688. vars["FLAGS"], vars["DEFINES"], vars["INCLUDES"]);
  689. std::string comment;
  690. std::string rule = this->LanguageCompilerRule(language);
  691. cmNinjaDeps outputs;
  692. outputs.push_back(objectFileName);
  693. // Add this object to the list of object files.
  694. this->Objects.push_back(objectFileName);
  695. cmNinjaDeps explicitDeps;
  696. explicitDeps.push_back(sourceFileName);
  697. cmNinjaDeps implicitDeps;
  698. if (const char* objectDeps = source->GetProperty("OBJECT_DEPENDS")) {
  699. std::vector<std::string> depList;
  700. cmSystemTools::ExpandListArgument(objectDeps, depList);
  701. for (std::vector<std::string>::iterator odi = depList.begin();
  702. odi != depList.end(); ++odi) {
  703. if (cmSystemTools::FileIsFullPath(*odi)) {
  704. *odi = cmSystemTools::CollapseFullPath(*odi);
  705. }
  706. }
  707. std::transform(depList.begin(), depList.end(),
  708. std::back_inserter(implicitDeps), MapToNinjaPath());
  709. }
  710. cmNinjaDeps orderOnlyDeps;
  711. if (writeOrderDependsTargetForTarget) {
  712. orderOnlyDeps.push_back(this->OrderDependsTargetForTarget());
  713. }
  714. // If the source file is GENERATED and does not have a custom command
  715. // (either attached to this source file or another one), assume that one of
  716. // the target dependencies, OBJECT_DEPENDS or header file custom commands
  717. // will rebuild the file.
  718. if (source->GetPropertyAsBool("GENERATED") && !source->GetCustomCommand() &&
  719. !this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFileName)) {
  720. this->GetGlobalGenerator()->AddAssumedSourceDependencies(sourceFileName,
  721. orderOnlyDeps);
  722. }
  723. // For some cases we need to generate a ninja dyndep file.
  724. bool const needDyndep = this->NeedDyndep(language);
  725. // For some cases we do an explicit preprocessor invocation.
  726. bool const explicitPP = this->NeedExplicitPreprocessing(language);
  727. if (explicitPP) {
  728. std::string const ppComment;
  729. std::string const ppRule = this->LanguagePreprocessRule(language);
  730. cmNinjaDeps ppOutputs;
  731. cmNinjaDeps ppImplicitOuts;
  732. cmNinjaDeps ppExplicitDeps;
  733. cmNinjaDeps ppImplicitDeps;
  734. cmNinjaDeps ppOrderOnlyDeps;
  735. cmNinjaVars ppVars;
  736. std::string const ppFileName =
  737. this->ConvertToNinjaPath(this->GetPreprocessedFilePath(source));
  738. ppOutputs.push_back(ppFileName);
  739. // Move compilation dependencies to the preprocessing build statement.
  740. std::swap(ppExplicitDeps, explicitDeps);
  741. std::swap(ppImplicitDeps, implicitDeps);
  742. std::swap(ppOrderOnlyDeps, orderOnlyDeps);
  743. std::swap(ppVars["IN_ABS"], vars["IN_ABS"]);
  744. // The actual compilation will now use the preprocessed source.
  745. explicitDeps.push_back(ppFileName);
  746. // Preprocessing and compilation use the same flags.
  747. ppVars["FLAGS"] = vars["FLAGS"];
  748. // Move preprocessor definitions to the preprocessor build statement.
  749. std::swap(ppVars["DEFINES"], vars["DEFINES"]);
  750. // Copy include directories to the preprocessor build statement. The
  751. // Fortran compilation build statement still needs them for the INCLUDE
  752. // directive.
  753. ppVars["INCLUDES"] = vars["INCLUDES"];
  754. // Prepend source file's original directory as an include directory
  755. // so e.g. Fortran INCLUDE statements can look for files in it.
  756. std::vector<std::string> sourceDirectory;
  757. sourceDirectory.push_back(
  758. cmSystemTools::GetParentDirectory(source->GetFullPath()));
  759. std::string sourceDirectoryFlag = this->LocalGenerator->GetIncludeFlags(
  760. sourceDirectory, this->GeneratorTarget, language, false, false,
  761. this->GetConfigName());
  762. vars["INCLUDES"] = sourceDirectoryFlag + " " + vars["INCLUDES"];
  763. // Explicit preprocessing always uses a depfile.
  764. ppVars["DEP_FILE"] =
  765. cmGlobalNinjaGenerator::EncodeDepfileSpace(ppFileName + ".d");
  766. // The actual compilation does not need a depfile because it
  767. // depends on the already-preprocessed source.
  768. vars.erase("DEP_FILE");
  769. if (needDyndep) {
  770. // Tell dependency scanner the object file that will result from
  771. // compiling the preprocessed source.
  772. ppVars["OBJ_FILE"] = objectFileName;
  773. // Tell dependency scanner where to store dyndep intermediate results.
  774. std::string const ddiFile = ppFileName + ".ddi";
  775. ppVars["DYNDEP_INTERMEDIATE_FILE"] = ddiFile;
  776. ppImplicitOuts.push_back(ddiFile);
  777. this->DDIFiles.push_back(ddiFile);
  778. }
  779. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
  780. ppVars);
  781. this->GetGlobalGenerator()->WriteBuild(
  782. this->GetBuildFileStream(), ppComment, ppRule, ppOutputs, ppImplicitOuts,
  783. ppExplicitDeps, ppImplicitDeps, ppOrderOnlyDeps, ppVars);
  784. }
  785. if (needDyndep) {
  786. std::string const dyndep = this->GetDyndepFilePath(language);
  787. orderOnlyDeps.push_back(dyndep);
  788. vars["dyndep"] = dyndep;
  789. }
  790. EnsureParentDirectoryExists(objectFileName);
  791. vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  792. objectDir, cmOutputConverter::SHELL);
  793. vars["OBJECT_FILE_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  794. objectFileDir, cmOutputConverter::SHELL);
  795. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
  796. vars);
  797. this->SetMsvcTargetPdbVariable(vars);
  798. bool const isRC = (language == "RC");
  799. int const commandLineLengthLimit =
  800. ((!isRC && this->ForceResponseFile())) ? -1 : 0;
  801. std::string const rspfile = objectFileName + ".rsp";
  802. this->GetGlobalGenerator()->WriteBuild(
  803. this->GetBuildFileStream(), comment, rule, outputs,
  804. /*implicitOuts=*/cmNinjaDeps(), explicitDeps, implicitDeps, orderOnlyDeps,
  805. vars, rspfile, commandLineLengthLimit);
  806. if (const char* objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
  807. std::vector<std::string> outputList;
  808. cmSystemTools::ExpandListArgument(objectOutputs, outputList);
  809. std::transform(outputList.begin(), outputList.end(), outputList.begin(),
  810. MapToNinjaPath());
  811. this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
  812. "Additional output files.",
  813. outputList, outputs);
  814. }
  815. }
  816. void cmNinjaTargetGenerator::WriteTargetDependInfo(std::string const& lang)
  817. {
  818. Json::Value tdi(Json::objectValue);
  819. tdi["language"] = lang;
  820. tdi["compiler-id"] =
  821. this->Makefile->GetSafeDefinition("CMAKE_" + lang + "_COMPILER_ID");
  822. if (lang == "Fortran") {
  823. std::string mod_dir = this->GeneratorTarget->GetFortranModuleDirectory(
  824. this->Makefile->GetHomeOutputDirectory());
  825. if (mod_dir.empty()) {
  826. mod_dir = this->Makefile->GetCurrentBinaryDirectory();
  827. }
  828. tdi["module-dir"] = mod_dir;
  829. }
  830. tdi["dir-cur-bld"] = this->Makefile->GetCurrentBinaryDirectory();
  831. tdi["dir-cur-src"] = this->Makefile->GetCurrentSourceDirectory();
  832. tdi["dir-top-bld"] = this->Makefile->GetHomeOutputDirectory();
  833. tdi["dir-top-src"] = this->Makefile->GetHomeDirectory();
  834. Json::Value& tdi_include_dirs = tdi["include-dirs"] = Json::arrayValue;
  835. std::vector<std::string> includes;
  836. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  837. lang, this->GetConfigName());
  838. for (std::vector<std::string>::iterator i = includes.begin();
  839. i != includes.end(); ++i) {
  840. tdi_include_dirs.append(*i);
  841. }
  842. Json::Value& tdi_linked_target_dirs = tdi["linked-target-dirs"] =
  843. Json::arrayValue;
  844. std::vector<std::string> linked = this->GetLinkedTargetDirectories();
  845. for (std::vector<std::string>::iterator i = linked.begin();
  846. i != linked.end(); ++i) {
  847. tdi_linked_target_dirs.append(*i);
  848. }
  849. std::string const tdin = this->GetTargetDependInfoPath(lang);
  850. cmGeneratedFileStream tdif(tdin.c_str());
  851. tdif << tdi;
  852. }
  853. void cmNinjaTargetGenerator::ExportObjectCompileCommand(
  854. std::string const& language, std::string const& sourceFileName,
  855. std::string const& objectDir, std::string const& objectFileName,
  856. std::string const& objectFileDir, std::string const& flags,
  857. std::string const& defines, std::string const& includes)
  858. {
  859. if (!this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS")) {
  860. return;
  861. }
  862. cmRulePlaceholderExpander::RuleVariables compileObjectVars;
  863. compileObjectVars.Language = language.c_str();
  864. std::string escapedSourceFileName = sourceFileName;
  865. if (!cmSystemTools::FileIsFullPath(sourceFileName.c_str())) {
  866. escapedSourceFileName = cmSystemTools::CollapseFullPath(
  867. escapedSourceFileName, this->GetGlobalGenerator()
  868. ->GetCMakeInstance()
  869. ->GetHomeOutputDirectory());
  870. }
  871. escapedSourceFileName = this->LocalGenerator->ConvertToOutputFormat(
  872. escapedSourceFileName, cmOutputConverter::SHELL);
  873. compileObjectVars.Source = escapedSourceFileName.c_str();
  874. compileObjectVars.Object = objectFileName.c_str();
  875. compileObjectVars.ObjectDir = objectDir.c_str();
  876. compileObjectVars.ObjectFileDir = objectFileDir.c_str();
  877. compileObjectVars.Flags = flags.c_str();
  878. compileObjectVars.Defines = defines.c_str();
  879. compileObjectVars.Includes = includes.c_str();
  880. // Rule for compiling object file.
  881. std::string compileCmdVar = "CMAKE_";
  882. compileCmdVar += language;
  883. compileCmdVar += "_COMPILE_OBJECT";
  884. std::string compileCmd =
  885. this->GetMakefile()->GetRequiredDefinition(compileCmdVar);
  886. std::vector<std::string> compileCmds;
  887. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  888. CM_AUTO_PTR<cmRulePlaceholderExpander> rulePlaceholderExpander(
  889. this->GetLocalGenerator()->CreateRulePlaceholderExpander());
  890. for (std::vector<std::string>::iterator i = compileCmds.begin();
  891. i != compileCmds.end(); ++i) {
  892. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(), *i,
  893. compileObjectVars);
  894. }
  895. std::string cmdLine =
  896. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  897. this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine, sourceFileName);
  898. }
  899. void cmNinjaTargetGenerator::EnsureDirectoryExists(
  900. const std::string& path) const
  901. {
  902. if (cmSystemTools::FileIsFullPath(path.c_str())) {
  903. cmSystemTools::MakeDirectory(path.c_str());
  904. } else {
  905. cmGlobalNinjaGenerator* gg = this->GetGlobalGenerator();
  906. std::string fullPath =
  907. std::string(gg->GetCMakeInstance()->GetHomeOutputDirectory());
  908. // Also ensures their is a trailing slash.
  909. gg->StripNinjaOutputPathPrefixAsSuffix(fullPath);
  910. fullPath += path;
  911. cmSystemTools::MakeDirectory(fullPath.c_str());
  912. }
  913. }
  914. void cmNinjaTargetGenerator::EnsureParentDirectoryExists(
  915. const std::string& path) const
  916. {
  917. EnsureDirectoryExists(cmSystemTools::GetParentDirectory(path));
  918. }
  919. void cmNinjaTargetGenerator::MacOSXContentGeneratorType::operator()(
  920. cmSourceFile const& source, const char* pkgloc)
  921. {
  922. // Skip OS X content when not building a Framework or Bundle.
  923. if (!this->Generator->GetGeneratorTarget()->IsBundleOnApple()) {
  924. return;
  925. }
  926. std::string macdir =
  927. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc);
  928. // Get the input file location.
  929. std::string input = source.GetFullPath();
  930. input = this->Generator->GetGlobalGenerator()->ConvertToNinjaPath(input);
  931. // Get the output file location.
  932. std::string output = macdir;
  933. output += "/";
  934. output += cmSystemTools::GetFilenameName(input);
  935. output = this->Generator->GetGlobalGenerator()->ConvertToNinjaPath(output);
  936. // Write a build statement to copy the content into the bundle.
  937. this->Generator->GetGlobalGenerator()->WriteMacOSXContentBuild(input,
  938. output);
  939. // Add as a dependency to the target so that it gets called.
  940. this->Generator->ExtraFiles.push_back(output);
  941. }
  942. void cmNinjaTargetGenerator::addPoolNinjaVariable(
  943. const std::string& pool_property, cmGeneratorTarget* target,
  944. cmNinjaVars& vars)
  945. {
  946. const char* pool = target->GetProperty(pool_property);
  947. if (pool) {
  948. vars["pool"] = pool;
  949. }
  950. }
  951. bool cmNinjaTargetGenerator::ForceResponseFile()
  952. {
  953. static std::string const forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
  954. return (this->GetMakefile()->IsDefinitionSet(forceRspFile) ||
  955. cmSystemTools::HasEnv(forceRspFile));
  956. }