cmNinjaTargetGenerator.cxx 27 KB

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