cmNinjaTargetGenerator.cxx 26 KB

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