cmNinjaTargetGenerator.cxx 27 KB

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