cmNinjaTargetGenerator.cxx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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::NeedDepTypeMSVC(const std::string& lang) const
  172. {
  173. if (lang == "C" || lang == "CXX")
  174. {
  175. cmMakefile* mf = this->GetMakefile();
  176. return (
  177. strcmp(mf->GetSafeDefinition("CMAKE_C_COMPILER_ID"), "MSVC") == 0 ||
  178. strcmp(mf->GetSafeDefinition("CMAKE_CXX_COMPILER_ID"), "MSVC") == 0 ||
  179. strcmp(mf->GetSafeDefinition("CMAKE_C_SIMULATE_ID"), "MSVC") == 0 ||
  180. strcmp(mf->GetSafeDefinition("CMAKE_CXX_SIMULATE_ID"), "MSVC") == 0
  181. );
  182. }
  183. return false;
  184. }
  185. // TODO: Refactor with
  186. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
  187. std::string
  188. cmNinjaTargetGenerator::
  189. ComputeDefines(cmSourceFile const* source, const std::string& language)
  190. {
  191. std::set<std::string> defines;
  192. // Add the export symbol definition for shared library objects.
  193. if(const char* exportMacro = this->Target->GetExportMacro())
  194. {
  195. this->LocalGenerator->AppendDefines(defines, exportMacro);
  196. }
  197. // Add preprocessor definitions for this target and configuration.
  198. this->LocalGenerator->AddCompileDefinitions(defines, this->Target,
  199. this->GetConfigName(), language);
  200. this->LocalGenerator->AppendDefines
  201. (defines,
  202. source->GetProperty("COMPILE_DEFINITIONS"));
  203. {
  204. std::string defPropName = "COMPILE_DEFINITIONS_";
  205. defPropName += cmSystemTools::UpperCase(this->GetConfigName());
  206. this->LocalGenerator->AppendDefines
  207. (defines,
  208. source->GetProperty(defPropName));
  209. }
  210. std::string definesString;
  211. this->LocalGenerator->JoinDefines(defines, definesString,
  212. language);
  213. return definesString;
  214. }
  215. cmNinjaDeps cmNinjaTargetGenerator::ComputeLinkDeps() const
  216. {
  217. // Static libraries never depend on other targets for linking.
  218. if (this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  219. this->Target->GetType() == cmTarget::OBJECT_LIBRARY)
  220. return cmNinjaDeps();
  221. cmComputeLinkInformation* cli =
  222. this->Target->GetLinkInformation(this->GetConfigName());
  223. if(!cli)
  224. return cmNinjaDeps();
  225. const std::vector<std::string> &deps = cli->GetDepends();
  226. cmNinjaDeps result(deps.size());
  227. std::transform(deps.begin(), deps.end(), result.begin(), MapToNinjaPath());
  228. // Add a dependency on the link definitions file, if any.
  229. if(!this->ModuleDefinitionFile.empty())
  230. {
  231. result.push_back(this->ModuleDefinitionFile);
  232. }
  233. return result;
  234. }
  235. std::string
  236. cmNinjaTargetGenerator
  237. ::GetSourceFilePath(cmSourceFile const* source) const
  238. {
  239. return ConvertToNinjaPath(source->GetFullPath());
  240. }
  241. std::string
  242. cmNinjaTargetGenerator
  243. ::GetObjectFilePath(cmSourceFile const* source) const
  244. {
  245. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  246. if(!path.empty())
  247. path += "/";
  248. std::string const& objectName = this->GeneratorTarget
  249. ->GetObjectName(source);
  250. path += this->LocalGenerator->GetTargetDirectory(*this->Target);
  251. path += "/";
  252. path += objectName;
  253. return path;
  254. }
  255. std::string cmNinjaTargetGenerator::GetTargetOutputDir() const
  256. {
  257. std::string dir = this->Target->GetDirectory(this->GetConfigName());
  258. return ConvertToNinjaPath(dir);
  259. }
  260. std::string
  261. cmNinjaTargetGenerator
  262. ::GetTargetFilePath(const std::string& name) const
  263. {
  264. std::string path = this->GetTargetOutputDir();
  265. if (path.empty() || path == ".")
  266. return name;
  267. path += "/";
  268. path += name;
  269. return path;
  270. }
  271. std::string cmNinjaTargetGenerator::GetTargetName() const
  272. {
  273. return this->Target->GetName();
  274. }
  275. bool cmNinjaTargetGenerator::SetMsvcTargetPdbVariable(cmNinjaVars& vars) const
  276. {
  277. cmMakefile* mf = this->GetMakefile();
  278. if (mf->GetDefinition("MSVC_C_ARCHITECTURE_ID") ||
  279. mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID"))
  280. {
  281. std::string pdbPath;
  282. std::string compilePdbPath;
  283. if(this->Target->GetType() == cmTarget::EXECUTABLE ||
  284. this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  285. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  286. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  287. {
  288. pdbPath = this->Target->GetPDBDirectory(this->GetConfigName());
  289. pdbPath += "/";
  290. pdbPath += this->Target->GetPDBName(this->GetConfigName());
  291. }
  292. if(this->Target->GetType() <= cmTarget::OBJECT_LIBRARY)
  293. {
  294. compilePdbPath = this->Target->GetCompilePDBPath(this->GetConfigName());
  295. if(compilePdbPath.empty())
  296. {
  297. compilePdbPath = this->Target->GetSupportDirectory() + "/";
  298. }
  299. }
  300. vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  301. ConvertToNinjaPath(pdbPath),
  302. cmLocalGenerator::SHELL);
  303. vars["TARGET_COMPILE_PDB"] =
  304. this->GetLocalGenerator()->ConvertToOutputFormat(
  305. ConvertToNinjaPath(compilePdbPath),
  306. cmLocalGenerator::SHELL);
  307. EnsureParentDirectoryExists(pdbPath);
  308. EnsureParentDirectoryExists(compilePdbPath);
  309. return true;
  310. }
  311. return false;
  312. }
  313. void
  314. cmNinjaTargetGenerator
  315. ::WriteLanguageRules(const std::string& language)
  316. {
  317. #ifdef NINJA_GEN_VERBOSE_FILES
  318. this->GetRulesFileStream()
  319. << "# Rules for language " << language << "\n\n";
  320. #endif
  321. this->WriteCompileRule(language);
  322. }
  323. void
  324. cmNinjaTargetGenerator
  325. ::WriteCompileRule(const std::string& lang)
  326. {
  327. cmLocalGenerator::RuleVariables vars;
  328. vars.RuleLauncher = "RULE_LAUNCH_COMPILE";
  329. vars.CMTarget = this->GetTarget();
  330. vars.Language = lang.c_str();
  331. vars.Source = "$in";
  332. vars.Object = "$out";
  333. vars.Defines = "$DEFINES";
  334. vars.TargetPDB = "$TARGET_PDB";
  335. vars.TargetCompilePDB = "$TARGET_COMPILE_PDB";
  336. vars.ObjectDir = "$OBJECT_DIR";
  337. vars.ObjectFileDir = "$OBJECT_FILE_DIR";
  338. cmMakefile* mf = this->GetMakefile();
  339. // Tell ninja dependency format so all deps can be loaded into a database
  340. std::string deptype;
  341. std::string depfile;
  342. std::string cldeps;
  343. std::string flags = "$FLAGS";
  344. if (this->NeedDepTypeMSVC(lang))
  345. {
  346. deptype = "msvc";
  347. depfile = "";
  348. flags += " /showIncludes";
  349. }
  350. else if (lang == "RC" && this->NeedDepTypeMSVC("C"))
  351. {
  352. // For the MS resource compiler we need cmcldeps, but skip dependencies
  353. // for source-file try_compile cases because they are always fresh.
  354. if (!mf->GetIsSourceFileTryCompile())
  355. {
  356. deptype = "gcc";
  357. depfile = "$DEP_FILE";
  358. const std::string cl = mf->GetDefinition("CMAKE_C_COMPILER") ?
  359. mf->GetSafeDefinition("CMAKE_C_COMPILER") :
  360. mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
  361. cldeps = "\"";
  362. cldeps += mf->GetSafeDefinition("CMAKE_CMCLDEPS_EXECUTABLE");
  363. cldeps += "\" " + lang + " $in \"$DEP_FILE\" $out \"";
  364. cldeps += mf->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX");
  365. cldeps += "\" \"" + cl + "\" ";
  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. for(std::vector<std::string>::iterator odi = depList.begin();
  531. odi != depList.end(); ++odi)
  532. {
  533. if (cmSystemTools::FileIsFullPath(*odi))
  534. {
  535. *odi = cmSystemTools::CollapseFullPath(*odi);
  536. }
  537. }
  538. std::transform(depList.begin(), depList.end(),
  539. std::back_inserter(implicitDeps), MapToNinjaPath());
  540. }
  541. cmNinjaDeps orderOnlyDeps;
  542. if (writeOrderDependsTargetForTarget)
  543. {
  544. orderOnlyDeps.push_back(this->OrderDependsTargetForTarget());
  545. }
  546. // If the source file is GENERATED and does not have a custom command
  547. // (either attached to this source file or another one), assume that one of
  548. // the target dependencies, OBJECT_DEPENDS or header file custom commands
  549. // will rebuild the file.
  550. if (source->GetPropertyAsBool("GENERATED") && !source->GetCustomCommand() &&
  551. !this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFileName)) {
  552. this->GetGlobalGenerator()->AddAssumedSourceDependencies(sourceFileName,
  553. orderOnlyDeps);
  554. }
  555. cmNinjaVars vars;
  556. vars["FLAGS"] = this->ComputeFlagsForObject(source, language);
  557. vars["DEFINES"] = this->ComputeDefines(source, language);
  558. if (!this->NeedDepTypeMSVC(language)) {
  559. vars["DEP_FILE"] =
  560. cmGlobalNinjaGenerator::EncodeDepfileSpace(objectFileName + ".d");
  561. }
  562. EnsureParentDirectoryExists(objectFileName);
  563. std::string objectDir = this->Target->GetSupportDirectory();
  564. vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  565. ConvertToNinjaPath(objectDir),
  566. cmLocalGenerator::SHELL);
  567. std::string objectFileDir = cmSystemTools::GetFilenamePath(objectFileName);
  568. vars["OBJECT_FILE_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  569. ConvertToNinjaPath(objectFileDir),
  570. cmLocalGenerator::SHELL);
  571. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetTarget(), vars);
  572. this->SetMsvcTargetPdbVariable(vars);
  573. if(this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS"))
  574. {
  575. cmLocalGenerator::RuleVariables compileObjectVars;
  576. std::string lang = language;
  577. compileObjectVars.Language = lang.c_str();
  578. std::string escapedSourceFileName = sourceFileName;
  579. if (!cmSystemTools::FileIsFullPath(sourceFileName.c_str()))
  580. {
  581. escapedSourceFileName = cmSystemTools::CollapseFullPath(
  582. escapedSourceFileName,
  583. this->GetGlobalGenerator()->GetCMakeInstance()->
  584. GetHomeOutputDirectory());
  585. }
  586. escapedSourceFileName =
  587. this->LocalGenerator->ConvertToOutputFormat(
  588. escapedSourceFileName, cmLocalGenerator::SHELL);
  589. compileObjectVars.Source = escapedSourceFileName.c_str();
  590. compileObjectVars.Object = objectFileName.c_str();
  591. compileObjectVars.ObjectDir = objectDir.c_str();
  592. compileObjectVars.ObjectFileDir = objectFileDir.c_str();
  593. compileObjectVars.Flags = vars["FLAGS"].c_str();
  594. compileObjectVars.Defines = vars["DEFINES"].c_str();
  595. // Rule for compiling object file.
  596. std::string compileCmdVar = "CMAKE_";
  597. compileCmdVar += language;
  598. compileCmdVar += "_COMPILE_OBJECT";
  599. std::string compileCmd =
  600. this->GetMakefile()->GetRequiredDefinition(compileCmdVar);
  601. std::vector<std::string> compileCmds;
  602. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  603. for (std::vector<std::string>::iterator i = compileCmds.begin();
  604. i != compileCmds.end(); ++i)
  605. this->GetLocalGenerator()->ExpandRuleVariables(*i, compileObjectVars);
  606. std::string cmdLine =
  607. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  608. this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine,
  609. sourceFileName);
  610. }
  611. this->GetGlobalGenerator()->WriteBuild(this->GetBuildFileStream(),
  612. comment,
  613. rule,
  614. outputs,
  615. explicitDeps,
  616. implicitDeps,
  617. orderOnlyDeps,
  618. vars);
  619. if(const char* objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
  620. std::vector<std::string> outputList;
  621. cmSystemTools::ExpandListArgument(objectOutputs, outputList);
  622. std::transform(outputList.begin(), outputList.end(), outputList.begin(),
  623. MapToNinjaPath());
  624. this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
  625. "Additional output files.",
  626. outputList,
  627. outputs);
  628. }
  629. }
  630. //----------------------------------------------------------------------------
  631. void
  632. cmNinjaTargetGenerator
  633. ::AddModuleDefinitionFlag(std::string& flags)
  634. {
  635. if(this->ModuleDefinitionFile.empty())
  636. {
  637. return;
  638. }
  639. // TODO: Create a per-language flag variable.
  640. const char* defFileFlag =
  641. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  642. if(!defFileFlag)
  643. {
  644. return;
  645. }
  646. // Append the flag and value. Use ConvertToLinkReference to help
  647. // vs6's "cl -link" pass it to the linker.
  648. std::string flag = defFileFlag;
  649. flag += (this->LocalGenerator->ConvertToLinkReference(
  650. this->ModuleDefinitionFile));
  651. this->LocalGenerator->AppendFlags(flags, flag);
  652. }
  653. void
  654. cmNinjaTargetGenerator
  655. ::EnsureDirectoryExists(const std::string& path) const
  656. {
  657. if (cmSystemTools::FileIsFullPath(path.c_str()))
  658. {
  659. cmSystemTools::MakeDirectory(path.c_str());
  660. }
  661. else
  662. {
  663. const std::string fullPath = std::string(this->GetGlobalGenerator()->
  664. GetCMakeInstance()->GetHomeOutputDirectory())
  665. + "/" + path;
  666. cmSystemTools::MakeDirectory(fullPath.c_str());
  667. }
  668. }
  669. void
  670. cmNinjaTargetGenerator
  671. ::EnsureParentDirectoryExists(const std::string& path) const
  672. {
  673. EnsureDirectoryExists(cmSystemTools::GetParentDirectory(path));
  674. }
  675. //----------------------------------------------------------------------------
  676. void
  677. cmNinjaTargetGenerator::MacOSXContentGeneratorType::operator()(
  678. cmSourceFile const& source, const char* pkgloc)
  679. {
  680. // Skip OS X content when not building a Framework or Bundle.
  681. if(!this->Generator->GetTarget()->IsBundleOnApple())
  682. {
  683. return;
  684. }
  685. std::string macdir =
  686. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc);
  687. // Get the input file location.
  688. std::string input = source.GetFullPath();
  689. input =
  690. this->Generator->GetLocalGenerator()->ConvertToNinjaPath(input);
  691. // Get the output file location.
  692. std::string output = macdir;
  693. output += "/";
  694. output += cmSystemTools::GetFilenameName(input);
  695. output =
  696. this->Generator->GetLocalGenerator()->ConvertToNinjaPath(output);
  697. // Write a build statement to copy the content into the bundle.
  698. this->Generator->GetGlobalGenerator()->WriteMacOSXContentBuild(input,
  699. output);
  700. // Add as a dependency of all target so that it gets called.
  701. this->Generator->GetGlobalGenerator()->AddDependencyToAll(output);
  702. }
  703. void cmNinjaTargetGenerator::addPoolNinjaVariable(
  704. const std::string& pool_property,
  705. cmTarget* target,
  706. cmNinjaVars& vars)
  707. {
  708. const char* pool = target->GetProperty(pool_property);
  709. if (pool)
  710. {
  711. vars["pool"] = pool;
  712. }
  713. }