cmNinjaTargetGenerator.cxx 28 KB

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