cmNinjaTargetGenerator.cxx 27 KB

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