cmNinjaTargetGenerator.cxx 28 KB

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