cmNinjaTargetGenerator.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2011 Peter Collingbourne <[email protected]>
  4. Copyright 2011 Nicolas Despres <[email protected]>
  5. Distributed under the OSI-approved BSD License (the "License");
  6. see accompanying file Copyright.txt for details.
  7. This software is distributed WITHOUT ANY WARRANTY; without even the
  8. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. See the License for more information.
  10. ============================================================================*/
  11. #include "cmNinjaTargetGenerator.h"
  12. #include "cmAlgorithms.h"
  13. #include "cmComputeLinkInformation.h"
  14. #include "cmCustomCommandGenerator.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmGeneratorTarget.h"
  17. #include "cmGlobalNinjaGenerator.h"
  18. #include "cmLocalNinjaGenerator.h"
  19. #include "cmMakefile.h"
  20. #include "cmNinjaNormalTargetGenerator.h"
  21. #include "cmNinjaUtilityTargetGenerator.h"
  22. #include "cmSourceFile.h"
  23. #include "cmSystemTools.h"
  24. #include <algorithm>
  25. cmNinjaTargetGenerator* cmNinjaTargetGenerator::New(cmGeneratorTarget* target)
  26. {
  27. switch (target->GetType()) {
  28. case cmState::EXECUTABLE:
  29. case cmState::SHARED_LIBRARY:
  30. case cmState::STATIC_LIBRARY:
  31. case cmState::MODULE_LIBRARY:
  32. case cmState::OBJECT_LIBRARY:
  33. return new cmNinjaNormalTargetGenerator(target);
  34. case cmState::UTILITY:
  35. return new cmNinjaUtilityTargetGenerator(target);
  36. ;
  37. case cmState::GLOBAL_TARGET: {
  38. // We only want to process global targets that live in the home
  39. // (i.e. top-level) directory. CMake creates copies of these targets
  40. // in every directory, which we don't need.
  41. if (strcmp(target->GetLocalGenerator()->GetCurrentSourceDirectory(),
  42. target->GetLocalGenerator()->GetSourceDirectory()) == 0)
  43. return new cmNinjaUtilityTargetGenerator(target);
  44. // else fallthrough
  45. }
  46. default:
  47. return 0;
  48. }
  49. }
  50. cmNinjaTargetGenerator::cmNinjaTargetGenerator(cmGeneratorTarget* target)
  51. : cmCommonTargetGenerator(cmOutputConverter::HOME_OUTPUT, target)
  52. , MacOSXContentGenerator(0)
  53. , OSXBundleGenerator(0)
  54. , MacContentFolders()
  55. , LocalGenerator(
  56. static_cast<cmLocalNinjaGenerator*>(target->GetLocalGenerator()))
  57. , Objects()
  58. {
  59. MacOSXContentGenerator = new MacOSXContentGeneratorType(this);
  60. }
  61. cmNinjaTargetGenerator::~cmNinjaTargetGenerator()
  62. {
  63. delete this->MacOSXContentGenerator;
  64. }
  65. cmGeneratedFileStream& cmNinjaTargetGenerator::GetBuildFileStream() const
  66. {
  67. return *this->GetGlobalGenerator()->GetBuildFileStream();
  68. }
  69. cmGeneratedFileStream& cmNinjaTargetGenerator::GetRulesFileStream() const
  70. {
  71. return *this->GetGlobalGenerator()->GetRulesFileStream();
  72. }
  73. cmGlobalNinjaGenerator* cmNinjaTargetGenerator::GetGlobalGenerator() const
  74. {
  75. return this->LocalGenerator->GetGlobalNinjaGenerator();
  76. }
  77. std::string cmNinjaTargetGenerator::LanguageCompilerRule(
  78. const std::string& lang) const
  79. {
  80. return lang + "_COMPILER__" +
  81. cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName());
  82. }
  83. std::string cmNinjaTargetGenerator::OrderDependsTargetForTarget()
  84. {
  85. return "cmake_order_depends_target_" + this->GetTargetName();
  86. }
  87. // TODO: Most of the code is picked up from
  88. // void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink),
  89. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  90. // Refactor it.
  91. std::string cmNinjaTargetGenerator::ComputeFlagsForObject(
  92. cmSourceFile const* source, const std::string& language)
  93. {
  94. std::string flags = this->GetFlags(language);
  95. // Add Fortran format flags.
  96. if (language == "Fortran") {
  97. this->AppendFortranFormatFlags(flags, *source);
  98. }
  99. // Add source file specific flags.
  100. this->LocalGenerator->AppendFlags(flags,
  101. source->GetProperty("COMPILE_FLAGS"));
  102. return flags;
  103. }
  104. void cmNinjaTargetGenerator::AddIncludeFlags(std::string& languageFlags,
  105. std::string const& language)
  106. {
  107. std::vector<std::string> includes;
  108. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  109. language, this->GetConfigName());
  110. // Add include directory flags.
  111. std::string includeFlags = this->LocalGenerator->GetIncludeFlags(
  112. includes, this->GeneratorTarget, language,
  113. language == "RC", // full include paths for RC needed by cmcldeps
  114. false, this->GetConfigName());
  115. if (this->GetGlobalGenerator()->IsGCCOnWindows())
  116. std::replace(includeFlags.begin(), includeFlags.end(), '\\', '/');
  117. this->LocalGenerator->AppendFlags(languageFlags, includeFlags);
  118. }
  119. bool cmNinjaTargetGenerator::NeedDepTypeMSVC(const std::string& lang) const
  120. {
  121. return strcmp(this->GetMakefile()->GetSafeDefinition("CMAKE_NINJA_DEPTYPE_" +
  122. lang),
  123. "msvc") == 0;
  124. }
  125. // TODO: Refactor with
  126. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
  127. std::string cmNinjaTargetGenerator::ComputeDefines(cmSourceFile const* source,
  128. const std::string& language)
  129. {
  130. std::set<std::string> defines;
  131. this->LocalGenerator->AppendDefines(
  132. defines, source->GetProperty("COMPILE_DEFINITIONS"));
  133. {
  134. std::string defPropName = "COMPILE_DEFINITIONS_";
  135. defPropName += cmSystemTools::UpperCase(this->GetConfigName());
  136. this->LocalGenerator->AppendDefines(defines,
  137. source->GetProperty(defPropName));
  138. }
  139. std::string definesString = this->GetDefines(language);
  140. this->LocalGenerator->JoinDefines(defines, definesString, language);
  141. return definesString;
  142. }
  143. cmNinjaDeps cmNinjaTargetGenerator::ComputeLinkDeps() const
  144. {
  145. // Static libraries never depend on other targets for linking.
  146. if (this->GeneratorTarget->GetType() == cmState::STATIC_LIBRARY ||
  147. this->GeneratorTarget->GetType() == cmState::OBJECT_LIBRARY)
  148. return cmNinjaDeps();
  149. cmComputeLinkInformation* cli =
  150. this->GeneratorTarget->GetLinkInformation(this->GetConfigName());
  151. if (!cli)
  152. return cmNinjaDeps();
  153. const std::vector<std::string>& deps = cli->GetDepends();
  154. cmNinjaDeps result(deps.size());
  155. std::transform(deps.begin(), deps.end(), result.begin(), MapToNinjaPath());
  156. // Add a dependency on the link definitions file, if any.
  157. if (this->ModuleDefinitionFile) {
  158. result.push_back(
  159. this->ConvertToNinjaPath(this->ModuleDefinitionFile->GetFullPath()));
  160. }
  161. // Add a dependency on user-specified manifest files, if any.
  162. std::vector<cmSourceFile const*> manifest_srcs;
  163. this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName);
  164. for (std::vector<cmSourceFile const*>::iterator mi = manifest_srcs.begin();
  165. mi != manifest_srcs.end(); ++mi) {
  166. result.push_back(this->ConvertToNinjaPath((*mi)->GetFullPath()));
  167. }
  168. // Add user-specified dependencies.
  169. if (const char* linkDepends =
  170. this->GeneratorTarget->GetProperty("LINK_DEPENDS")) {
  171. std::vector<std::string> linkDeps;
  172. cmSystemTools::ExpandListArgument(linkDepends, linkDeps);
  173. std::transform(linkDeps.begin(), linkDeps.end(),
  174. std::back_inserter(result), MapToNinjaPath());
  175. }
  176. return result;
  177. }
  178. std::string cmNinjaTargetGenerator::GetSourceFilePath(
  179. cmSourceFile const* source) const
  180. {
  181. return ConvertToNinjaPath(source->GetFullPath());
  182. }
  183. std::string cmNinjaTargetGenerator::GetObjectFilePath(
  184. cmSourceFile const* source) const
  185. {
  186. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  187. if (!path.empty())
  188. path += "/";
  189. std::string const& objectName = this->GeneratorTarget->GetObjectName(source);
  190. path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  191. path += "/";
  192. path += objectName;
  193. return path;
  194. }
  195. std::string cmNinjaTargetGenerator::GetTargetOutputDir() const
  196. {
  197. std::string dir = this->GeneratorTarget->GetDirectory(this->GetConfigName());
  198. return ConvertToNinjaPath(dir);
  199. }
  200. std::string cmNinjaTargetGenerator::GetTargetFilePath(
  201. const std::string& name) const
  202. {
  203. std::string path = this->GetTargetOutputDir();
  204. if (path.empty() || path == ".")
  205. return name;
  206. path += "/";
  207. path += name;
  208. return path;
  209. }
  210. std::string cmNinjaTargetGenerator::GetTargetName() const
  211. {
  212. return this->GeneratorTarget->GetName();
  213. }
  214. bool cmNinjaTargetGenerator::SetMsvcTargetPdbVariable(cmNinjaVars& vars) const
  215. {
  216. cmMakefile* mf = this->GetMakefile();
  217. if (mf->GetDefinition("MSVC_C_ARCHITECTURE_ID") ||
  218. mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID")) {
  219. std::string pdbPath;
  220. std::string compilePdbPath;
  221. if (this->GeneratorTarget->GetType() == cmState::EXECUTABLE ||
  222. this->GeneratorTarget->GetType() == cmState::STATIC_LIBRARY ||
  223. this->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY ||
  224. this->GeneratorTarget->GetType() == cmState::MODULE_LIBRARY) {
  225. pdbPath = this->GeneratorTarget->GetPDBDirectory(this->GetConfigName());
  226. pdbPath += "/";
  227. pdbPath += this->GeneratorTarget->GetPDBName(this->GetConfigName());
  228. }
  229. if (this->GeneratorTarget->GetType() <= cmState::OBJECT_LIBRARY) {
  230. compilePdbPath =
  231. this->GeneratorTarget->GetCompilePDBPath(this->GetConfigName());
  232. if (compilePdbPath.empty()) {
  233. compilePdbPath = this->GeneratorTarget->GetSupportDirectory() + "/";
  234. }
  235. }
  236. vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  237. ConvertToNinjaPath(pdbPath), cmOutputConverter::SHELL);
  238. vars["TARGET_COMPILE_PDB"] =
  239. this->GetLocalGenerator()->ConvertToOutputFormat(
  240. ConvertToNinjaPath(compilePdbPath), cmOutputConverter::SHELL);
  241. EnsureParentDirectoryExists(pdbPath);
  242. EnsureParentDirectoryExists(compilePdbPath);
  243. return true;
  244. }
  245. return false;
  246. }
  247. void cmNinjaTargetGenerator::WriteLanguageRules(const std::string& language)
  248. {
  249. #ifdef NINJA_GEN_VERBOSE_FILES
  250. this->GetRulesFileStream() << "# Rules for language " << language << "\n\n";
  251. #endif
  252. this->WriteCompileRule(language);
  253. }
  254. void cmNinjaTargetGenerator::WriteCompileRule(const std::string& lang)
  255. {
  256. cmLocalGenerator::RuleVariables vars;
  257. vars.RuleLauncher = "RULE_LAUNCH_COMPILE";
  258. vars.CMTarget = this->GetGeneratorTarget();
  259. vars.Language = lang.c_str();
  260. vars.Source = "$in";
  261. vars.Object = "$out";
  262. vars.Defines = "$DEFINES";
  263. vars.Includes = "$INCLUDES";
  264. vars.TargetPDB = "$TARGET_PDB";
  265. vars.TargetCompilePDB = "$TARGET_COMPILE_PDB";
  266. vars.ObjectDir = "$OBJECT_DIR";
  267. vars.ObjectFileDir = "$OBJECT_FILE_DIR";
  268. cmMakefile* mf = this->GetMakefile();
  269. std::string flags = "$FLAGS";
  270. std::string rspfile;
  271. std::string rspcontent;
  272. std::string responseFlag;
  273. if (this->ForceResponseFile()) {
  274. rspfile = "$RSP_FILE";
  275. responseFlag = "@" + rspfile;
  276. rspcontent = " $DEFINES $INCLUDES $FLAGS";
  277. flags = responseFlag;
  278. vars.Defines = "";
  279. vars.Includes = "";
  280. }
  281. // Tell ninja dependency format so all deps can be loaded into a database
  282. std::string deptype;
  283. std::string depfile;
  284. std::string cldeps;
  285. if (this->NeedDepTypeMSVC(lang)) {
  286. deptype = "msvc";
  287. depfile = "";
  288. flags += " /showIncludes";
  289. } else if (mf->IsOn("CMAKE_NINJA_CMCLDEPS_" + lang)) {
  290. // For the MS resource compiler we need cmcldeps, but skip dependencies
  291. // for source-file try_compile cases because they are always fresh.
  292. if (!mf->GetIsSourceFileTryCompile()) {
  293. deptype = "gcc";
  294. depfile = "$DEP_FILE";
  295. const std::string cl = mf->GetDefinition("CMAKE_C_COMPILER")
  296. ? mf->GetSafeDefinition("CMAKE_C_COMPILER")
  297. : mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
  298. cldeps = "\"";
  299. cldeps += cmSystemTools::GetCMClDepsCommand();
  300. cldeps += "\" " + lang + " $in \"$DEP_FILE\" $out \"";
  301. cldeps += mf->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX");
  302. cldeps += "\" \"" + cl + "\" ";
  303. }
  304. } else {
  305. deptype = "gcc";
  306. const char* langdeptype = mf->GetDefinition("CMAKE_NINJA_DEPTYPE_" + lang);
  307. if (langdeptype) {
  308. deptype = langdeptype;
  309. }
  310. depfile = "$DEP_FILE";
  311. const std::string flagsName = "CMAKE_DEPFILE_FLAGS_" + lang;
  312. std::string depfileFlags = mf->GetSafeDefinition(flagsName);
  313. if (!depfileFlags.empty()) {
  314. cmSystemTools::ReplaceString(depfileFlags, "<DEPFILE>", "$DEP_FILE");
  315. cmSystemTools::ReplaceString(depfileFlags, "<OBJECT>", "$out");
  316. cmSystemTools::ReplaceString(depfileFlags, "<CMAKE_C_COMPILER>",
  317. mf->GetDefinition("CMAKE_C_COMPILER"));
  318. flags += " " + depfileFlags;
  319. }
  320. }
  321. vars.Flags = flags.c_str();
  322. vars.DependencyFile = depfile.c_str();
  323. // Rule for compiling object file.
  324. const std::string cmdVar = std::string("CMAKE_") + lang + "_COMPILE_OBJECT";
  325. std::string compileCmd = mf->GetRequiredDefinition(cmdVar);
  326. std::vector<std::string> compileCmds;
  327. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  328. // Maybe insert an include-what-you-use runner.
  329. if (!compileCmds.empty() && (lang == "C" || lang == "CXX")) {
  330. std::string const iwyu_prop = lang + "_INCLUDE_WHAT_YOU_USE";
  331. const char* iwyu = this->GeneratorTarget->GetProperty(iwyu_prop);
  332. std::string const tidy_prop = lang + "_CLANG_TIDY";
  333. const char* tidy = this->GeneratorTarget->GetProperty(tidy_prop);
  334. if ((iwyu && *iwyu) || (tidy && *tidy)) {
  335. std::string run_iwyu = this->GetLocalGenerator()->ConvertToOutputFormat(
  336. cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL);
  337. run_iwyu += " -E __run_iwyu";
  338. if (iwyu && *iwyu) {
  339. run_iwyu += " --iwyu=";
  340. run_iwyu += this->GetLocalGenerator()->EscapeForShell(iwyu);
  341. }
  342. if (tidy && *tidy) {
  343. run_iwyu += " --tidy=";
  344. run_iwyu += this->GetLocalGenerator()->EscapeForShell(tidy);
  345. run_iwyu += " --source=$in";
  346. }
  347. run_iwyu += " -- ";
  348. compileCmds.front().insert(0, run_iwyu);
  349. }
  350. }
  351. // Maybe insert a compiler launcher like ccache or distcc
  352. if (!compileCmds.empty() && (lang == "C" || lang == "CXX")) {
  353. std::string const clauncher_prop = lang + "_COMPILER_LAUNCHER";
  354. const char* clauncher = this->GeneratorTarget->GetProperty(clauncher_prop);
  355. if (clauncher && *clauncher) {
  356. std::vector<std::string> launcher_cmd;
  357. cmSystemTools::ExpandListArgument(clauncher, launcher_cmd, true);
  358. for (std::vector<std::string>::iterator i = launcher_cmd.begin(),
  359. e = launcher_cmd.end();
  360. i != e; ++i) {
  361. *i = this->LocalGenerator->EscapeForShell(*i);
  362. }
  363. std::string const& run_launcher = cmJoin(launcher_cmd, " ") + " ";
  364. compileCmds.front().insert(0, run_launcher);
  365. }
  366. }
  367. if (!compileCmds.empty()) {
  368. compileCmds.front().insert(0, cldeps);
  369. }
  370. for (std::vector<std::string>::iterator i = compileCmds.begin();
  371. i != compileCmds.end(); ++i)
  372. this->GetLocalGenerator()->ExpandRuleVariables(*i, vars);
  373. std::string cmdLine =
  374. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  375. // Write the rule for compiling file of the given language.
  376. std::ostringstream comment;
  377. comment << "Rule for compiling " << lang << " files.";
  378. std::ostringstream description;
  379. description << "Building " << lang << " object $out";
  380. this->GetGlobalGenerator()->AddRule(
  381. this->LanguageCompilerRule(lang), cmdLine, description.str(),
  382. comment.str(), depfile, deptype, rspfile, rspcontent,
  383. /*restat*/ "",
  384. /*generator*/ false);
  385. }
  386. void cmNinjaTargetGenerator::WriteObjectBuildStatements()
  387. {
  388. // Write comments.
  389. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  390. this->GetBuildFileStream()
  391. << "# Object build statements for "
  392. << cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType())
  393. << " target " << this->GetTargetName() << "\n\n";
  394. std::string config = this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  395. std::vector<cmSourceFile const*> customCommands;
  396. this->GeneratorTarget->GetCustomCommands(customCommands, config);
  397. for (std::vector<cmSourceFile const*>::const_iterator si =
  398. customCommands.begin();
  399. si != customCommands.end(); ++si) {
  400. cmCustomCommand const* cc = (*si)->GetCustomCommand();
  401. this->GetLocalGenerator()->AddCustomCommandTarget(
  402. cc, this->GetGeneratorTarget());
  403. // Record the custom commands for this target. The container is used
  404. // in WriteObjectBuildStatement when called in a loop below.
  405. this->CustomCommands.push_back(cc);
  406. }
  407. std::vector<cmSourceFile const*> headerSources;
  408. this->GeneratorTarget->GetHeaderSources(headerSources, config);
  409. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  410. headerSources, this->MacOSXContentGenerator);
  411. std::vector<cmSourceFile const*> extraSources;
  412. this->GeneratorTarget->GetExtraSources(extraSources, config);
  413. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  414. extraSources, this->MacOSXContentGenerator);
  415. std::vector<cmSourceFile const*> externalObjects;
  416. this->GeneratorTarget->GetExternalObjects(externalObjects, config);
  417. for (std::vector<cmSourceFile const*>::const_iterator si =
  418. externalObjects.begin();
  419. si != externalObjects.end(); ++si) {
  420. this->Objects.push_back(this->GetSourceFilePath(*si));
  421. }
  422. cmNinjaDeps orderOnlyDeps;
  423. this->GetLocalGenerator()->AppendTargetDepends(this->GeneratorTarget,
  424. orderOnlyDeps);
  425. // Add order-only dependencies on custom command outputs.
  426. for (std::vector<cmCustomCommand const*>::const_iterator cci =
  427. this->CustomCommands.begin();
  428. cci != this->CustomCommands.end(); ++cci) {
  429. cmCustomCommand const* cc = *cci;
  430. cmCustomCommandGenerator ccg(*cc, this->GetConfigName(),
  431. this->GetLocalGenerator());
  432. const std::vector<std::string>& ccoutputs = ccg.GetOutputs();
  433. const std::vector<std::string>& ccbyproducts = ccg.GetByproducts();
  434. std::transform(ccoutputs.begin(), ccoutputs.end(),
  435. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  436. std::transform(ccbyproducts.begin(), ccbyproducts.end(),
  437. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  438. }
  439. if (!orderOnlyDeps.empty()) {
  440. cmNinjaDeps orderOnlyTarget;
  441. orderOnlyTarget.push_back(this->OrderDependsTargetForTarget());
  442. this->GetGlobalGenerator()->WritePhonyBuild(
  443. this->GetBuildFileStream(),
  444. "Order-only phony target for " + this->GetTargetName(), orderOnlyTarget,
  445. cmNinjaDeps(), cmNinjaDeps(), orderOnlyDeps);
  446. }
  447. std::vector<cmSourceFile const*> objectSources;
  448. this->GeneratorTarget->GetObjectSources(objectSources, config);
  449. for (std::vector<cmSourceFile const*>::const_iterator si =
  450. objectSources.begin();
  451. si != objectSources.end(); ++si) {
  452. this->WriteObjectBuildStatement(*si, !orderOnlyDeps.empty());
  453. }
  454. this->GetBuildFileStream() << "\n";
  455. }
  456. void cmNinjaTargetGenerator::WriteObjectBuildStatement(
  457. cmSourceFile const* source, bool writeOrderDependsTargetForTarget)
  458. {
  459. std::string const language = source->GetLanguage();
  460. std::string const sourceFileName =
  461. language == "RC" ? source->GetFullPath() : this->GetSourceFilePath(source);
  462. std::string const objectDir =
  463. this->ConvertToNinjaPath(this->GeneratorTarget->GetSupportDirectory());
  464. std::string const objectFileName =
  465. this->ConvertToNinjaPath(this->GetObjectFilePath(source));
  466. std::string const objectFileDir =
  467. cmSystemTools::GetFilenamePath(objectFileName);
  468. cmNinjaVars vars;
  469. vars["FLAGS"] = this->ComputeFlagsForObject(source, language);
  470. vars["DEFINES"] = this->ComputeDefines(source, language);
  471. vars["INCLUDES"] = this->GetIncludes(language);
  472. if (!this->NeedDepTypeMSVC(language)) {
  473. vars["DEP_FILE"] =
  474. cmGlobalNinjaGenerator::EncodeDepfileSpace(objectFileName + ".d");
  475. }
  476. this->ExportObjectCompileCommand(
  477. language, sourceFileName, objectDir, objectFileName, objectFileDir,
  478. vars["FLAGS"], vars["DEFINES"], vars["INCLUDES"]);
  479. std::string comment;
  480. std::string rule = this->LanguageCompilerRule(language);
  481. cmNinjaDeps outputs;
  482. outputs.push_back(objectFileName);
  483. // Add this object to the list of object files.
  484. this->Objects.push_back(objectFileName);
  485. cmNinjaDeps explicitDeps;
  486. explicitDeps.push_back(sourceFileName);
  487. cmNinjaDeps implicitDeps;
  488. if (const char* objectDeps = source->GetProperty("OBJECT_DEPENDS")) {
  489. std::vector<std::string> depList;
  490. cmSystemTools::ExpandListArgument(objectDeps, depList);
  491. for (std::vector<std::string>::iterator odi = depList.begin();
  492. odi != depList.end(); ++odi) {
  493. if (cmSystemTools::FileIsFullPath(*odi)) {
  494. *odi = cmSystemTools::CollapseFullPath(*odi);
  495. }
  496. }
  497. std::transform(depList.begin(), depList.end(),
  498. std::back_inserter(implicitDeps), MapToNinjaPath());
  499. }
  500. cmNinjaDeps orderOnlyDeps;
  501. if (writeOrderDependsTargetForTarget) {
  502. orderOnlyDeps.push_back(this->OrderDependsTargetForTarget());
  503. }
  504. // If the source file is GENERATED and does not have a custom command
  505. // (either attached to this source file or another one), assume that one of
  506. // the target dependencies, OBJECT_DEPENDS or header file custom commands
  507. // will rebuild the file.
  508. if (source->GetPropertyAsBool("GENERATED") && !source->GetCustomCommand() &&
  509. !this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFileName)) {
  510. this->GetGlobalGenerator()->AddAssumedSourceDependencies(sourceFileName,
  511. orderOnlyDeps);
  512. }
  513. EnsureParentDirectoryExists(objectFileName);
  514. vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  515. objectDir, cmOutputConverter::SHELL);
  516. vars["OBJECT_FILE_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  517. objectFileDir, cmOutputConverter::SHELL);
  518. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
  519. vars);
  520. this->SetMsvcTargetPdbVariable(vars);
  521. int const commandLineLengthLimit = this->ForceResponseFile() ? -1 : 0;
  522. std::string const rspfile = objectFileName + ".rsp";
  523. this->GetGlobalGenerator()->WriteBuild(
  524. this->GetBuildFileStream(), comment, rule, outputs, explicitDeps,
  525. implicitDeps, orderOnlyDeps, vars, rspfile, commandLineLengthLimit);
  526. if (const char* objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
  527. std::vector<std::string> outputList;
  528. cmSystemTools::ExpandListArgument(objectOutputs, outputList);
  529. std::transform(outputList.begin(), outputList.end(), outputList.begin(),
  530. MapToNinjaPath());
  531. this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
  532. "Additional output files.",
  533. outputList, outputs);
  534. }
  535. }
  536. void cmNinjaTargetGenerator::ExportObjectCompileCommand(
  537. std::string const& language, std::string const& sourceFileName,
  538. std::string const& objectDir, std::string const& objectFileName,
  539. std::string const& objectFileDir, std::string const& flags,
  540. std::string const& defines, std::string const& includes)
  541. {
  542. if (!this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS")) {
  543. return;
  544. }
  545. cmLocalGenerator::RuleVariables compileObjectVars;
  546. compileObjectVars.Language = language.c_str();
  547. std::string escapedSourceFileName = sourceFileName;
  548. if (!cmSystemTools::FileIsFullPath(sourceFileName.c_str())) {
  549. escapedSourceFileName = cmSystemTools::CollapseFullPath(
  550. escapedSourceFileName, this->GetGlobalGenerator()
  551. ->GetCMakeInstance()
  552. ->GetHomeOutputDirectory());
  553. }
  554. escapedSourceFileName = this->LocalGenerator->ConvertToOutputFormat(
  555. escapedSourceFileName, cmOutputConverter::SHELL);
  556. compileObjectVars.Source = escapedSourceFileName.c_str();
  557. compileObjectVars.Object = objectFileName.c_str();
  558. compileObjectVars.ObjectDir = objectDir.c_str();
  559. compileObjectVars.ObjectFileDir = objectFileDir.c_str();
  560. compileObjectVars.Flags = flags.c_str();
  561. compileObjectVars.Defines = defines.c_str();
  562. compileObjectVars.Includes = includes.c_str();
  563. // Rule for compiling object file.
  564. std::string compileCmdVar = "CMAKE_";
  565. compileCmdVar += language;
  566. compileCmdVar += "_COMPILE_OBJECT";
  567. std::string compileCmd =
  568. this->GetMakefile()->GetRequiredDefinition(compileCmdVar);
  569. std::vector<std::string> compileCmds;
  570. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  571. for (std::vector<std::string>::iterator i = compileCmds.begin();
  572. i != compileCmds.end(); ++i)
  573. this->GetLocalGenerator()->ExpandRuleVariables(*i, compileObjectVars);
  574. std::string cmdLine =
  575. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  576. this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine, sourceFileName);
  577. }
  578. void cmNinjaTargetGenerator::EnsureDirectoryExists(
  579. const std::string& path) const
  580. {
  581. if (cmSystemTools::FileIsFullPath(path.c_str())) {
  582. cmSystemTools::MakeDirectory(path.c_str());
  583. } else {
  584. cmGlobalNinjaGenerator* gg = this->GetGlobalGenerator();
  585. std::string fullPath =
  586. std::string(gg->GetCMakeInstance()->GetHomeOutputDirectory());
  587. // Also ensures their is a trailing slash.
  588. gg->StripNinjaOutputPathPrefixAsSuffix(fullPath);
  589. fullPath += path;
  590. cmSystemTools::MakeDirectory(fullPath.c_str());
  591. }
  592. }
  593. void cmNinjaTargetGenerator::EnsureParentDirectoryExists(
  594. const std::string& path) const
  595. {
  596. EnsureDirectoryExists(cmSystemTools::GetParentDirectory(path));
  597. }
  598. void cmNinjaTargetGenerator::MacOSXContentGeneratorType::operator()(
  599. cmSourceFile const& source, const char* pkgloc)
  600. {
  601. // Skip OS X content when not building a Framework or Bundle.
  602. if (!this->Generator->GetGeneratorTarget()->IsBundleOnApple()) {
  603. return;
  604. }
  605. std::string macdir =
  606. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc);
  607. // Get the input file location.
  608. std::string input = source.GetFullPath();
  609. input = this->Generator->GetGlobalGenerator()->ConvertToNinjaPath(input);
  610. // Get the output file location.
  611. std::string output = macdir;
  612. output += "/";
  613. output += cmSystemTools::GetFilenameName(input);
  614. output = this->Generator->GetGlobalGenerator()->ConvertToNinjaPath(output);
  615. // Write a build statement to copy the content into the bundle.
  616. this->Generator->GetGlobalGenerator()->WriteMacOSXContentBuild(input,
  617. output);
  618. // Add as a dependency of all target so that it gets called.
  619. this->Generator->GetGlobalGenerator()->AddDependencyToAll(output);
  620. }
  621. void cmNinjaTargetGenerator::addPoolNinjaVariable(
  622. const std::string& pool_property, cmGeneratorTarget* target,
  623. cmNinjaVars& vars)
  624. {
  625. const char* pool = target->GetProperty(pool_property);
  626. if (pool) {
  627. vars["pool"] = pool;
  628. }
  629. }
  630. bool cmNinjaTargetGenerator::ForceResponseFile()
  631. {
  632. static std::string const forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
  633. return (this->GetMakefile()->IsDefinitionSet(forceRspFile) ||
  634. cmSystemTools::GetEnv(forceRspFile) != 0);
  635. }