cmNinjaTargetGenerator.cxx 29 KB

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