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 cmState::EXECUTABLE:
  31. case cmState::SHARED_LIBRARY:
  32. case cmState::STATIC_LIBRARY:
  33. case cmState::MODULE_LIBRARY:
  34. case cmState::OBJECT_LIBRARY:
  35. return new cmNinjaNormalTargetGenerator(target);
  36. case cmState::UTILITY:
  37. return new cmNinjaUtilityTargetGenerator(target);;
  38. case cmState::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->GeneratorTarget->GetType() == cmState::STATIC_LIBRARY ||
  168. this->GeneratorTarget->GetType() == cmState::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->GeneratorTarget->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->GeneratorTarget->GetType() == cmState::EXECUTABLE ||
  249. this->GeneratorTarget->GetType() == cmState::STATIC_LIBRARY ||
  250. this->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY ||
  251. this->GeneratorTarget->GetType() == cmState::MODULE_LIBRARY)
  252. {
  253. pdbPath = this->GeneratorTarget->GetPDBDirectory(this->GetConfigName());
  254. pdbPath += "/";
  255. pdbPath += this->GeneratorTarget->GetPDBName(this->GetConfigName());
  256. }
  257. if(this->GeneratorTarget->GetType() <= cmState::OBJECT_LIBRARY)
  258. {
  259. compilePdbPath =
  260. this->GeneratorTarget->GetCompilePDBPath(this->GetConfigName());
  261. if(compilePdbPath.empty())
  262. {
  263. compilePdbPath = this->GeneratorTarget->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(
  430. this->GetGeneratorTarget()->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->GeneratorTarget->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. }