cmNinjaTargetGenerator.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2011 Peter Collingbourne <[email protected]>
  4. Copyright 2011 Nicolas Despres <[email protected]>
  5. Distributed under the OSI-approved BSD License (the "License");
  6. see accompanying file Copyright.txt for details.
  7. This software is distributed WITHOUT ANY WARRANTY; without even the
  8. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. See the License for more information.
  10. ============================================================================*/
  11. #include "cmNinjaTargetGenerator.h"
  12. #include "cmGlobalNinjaGenerator.h"
  13. #include "cmLocalNinjaGenerator.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGeneratorTarget.h"
  16. #include "cmNinjaNormalTargetGenerator.h"
  17. #include "cmNinjaUtilityTargetGenerator.h"
  18. #include "cmSystemTools.h"
  19. #include "cmMakefile.h"
  20. #include "cmComputeLinkInformation.h"
  21. #include "cmSourceFile.h"
  22. #include "cmCustomCommandGenerator.h"
  23. #include <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. if(cmGlobalNinjaGenerator::IsMinGW())
  152. cmSystemTools::ReplaceString(includeFlags, "\\", "/");
  153. this->LocalGenerator->AppendFlags(languageFlags, includeFlags);
  154. // Append old-style preprocessor definition flags.
  155. this->LocalGenerator->AppendFlags(languageFlags,
  156. this->Makefile->GetDefineFlags());
  157. // Add target-specific flags.
  158. this->LocalGenerator->AddCompileOptions(languageFlags, this->Target,
  159. language,
  160. this->GetConfigName());
  161. }
  162. std::string flags = languageFlags;
  163. // Add source file specific flags.
  164. this->LocalGenerator->AppendFlags(flags,
  165. source->GetProperty("COMPILE_FLAGS"));
  166. // TODO: Handle Apple frameworks.
  167. return flags;
  168. }
  169. bool cmNinjaTargetGenerator::needsDepFile(const std::string& lang)
  170. {
  171. cmMakefile* mf = this->GetMakefile();
  172. const bool usingMSVC = std::string("MSVC") ==
  173. (mf->GetDefinition("CMAKE_C_COMPILER_ID") ?
  174. mf->GetSafeDefinition("CMAKE_C_COMPILER_ID") :
  175. mf->GetSafeDefinition("CMAKE_CXX_COMPILER_ID"));
  176. return !usingMSVC || lang == "RC";
  177. }
  178. // TODO: Refactor with
  179. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
  180. std::string
  181. cmNinjaTargetGenerator::
  182. ComputeDefines(cmSourceFile const* source, const std::string& language)
  183. {
  184. std::set<std::string> defines;
  185. // Add the export symbol definition for shared library objects.
  186. if(const char* exportMacro = this->Target->GetExportMacro())
  187. {
  188. this->LocalGenerator->AppendDefines(defines, exportMacro);
  189. }
  190. // Add preprocessor definitions for this target and configuration.
  191. this->LocalGenerator->AddCompileDefinitions(defines, this->Target,
  192. this->GetConfigName());
  193. this->LocalGenerator->AppendDefines
  194. (defines,
  195. source->GetProperty("COMPILE_DEFINITIONS"));
  196. {
  197. std::string defPropName = "COMPILE_DEFINITIONS_";
  198. defPropName += cmSystemTools::UpperCase(this->GetConfigName());
  199. this->LocalGenerator->AppendDefines
  200. (defines,
  201. source->GetProperty(defPropName));
  202. }
  203. std::string definesString;
  204. this->LocalGenerator->JoinDefines(defines, definesString,
  205. language);
  206. return definesString;
  207. }
  208. cmNinjaDeps cmNinjaTargetGenerator::ComputeLinkDeps() const
  209. {
  210. // Static libraries never depend on other targets for linking.
  211. if (this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  212. this->Target->GetType() == cmTarget::OBJECT_LIBRARY)
  213. return cmNinjaDeps();
  214. cmComputeLinkInformation* cli =
  215. this->Target->GetLinkInformation(this->GetConfigName());
  216. if(!cli)
  217. return cmNinjaDeps();
  218. const std::vector<std::string> &deps = cli->GetDepends();
  219. cmNinjaDeps result(deps.size());
  220. std::transform(deps.begin(), deps.end(), result.begin(), MapToNinjaPath());
  221. // Add a dependency on the link definitions file, if any.
  222. if(!this->ModuleDefinitionFile.empty())
  223. {
  224. result.push_back(this->ModuleDefinitionFile);
  225. }
  226. return result;
  227. }
  228. std::string
  229. cmNinjaTargetGenerator
  230. ::GetSourceFilePath(cmSourceFile const* source) const
  231. {
  232. return ConvertToNinjaPath(source->GetFullPath().c_str());
  233. }
  234. std::string
  235. cmNinjaTargetGenerator
  236. ::GetObjectFilePath(cmSourceFile const* source) const
  237. {
  238. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  239. if(!path.empty())
  240. path += "/";
  241. std::string const& objectName = this->GeneratorTarget
  242. ->GetObjectName(source);
  243. path += this->LocalGenerator->GetTargetDirectory(*this->Target);
  244. path += "/";
  245. path += objectName;
  246. return path;
  247. }
  248. std::string cmNinjaTargetGenerator::GetTargetOutputDir() const
  249. {
  250. std::string dir = this->Target->GetDirectory(this->GetConfigName());
  251. return ConvertToNinjaPath(dir.c_str());
  252. }
  253. std::string
  254. cmNinjaTargetGenerator
  255. ::GetTargetFilePath(const std::string& name) const
  256. {
  257. std::string path = this->GetTargetOutputDir();
  258. if (path.empty() || path == ".")
  259. return name;
  260. path += "/";
  261. path += name;
  262. return path;
  263. }
  264. std::string cmNinjaTargetGenerator::GetTargetName() const
  265. {
  266. return this->Target->GetName();
  267. }
  268. bool cmNinjaTargetGenerator::SetMsvcTargetPdbVariable(cmNinjaVars& vars) const
  269. {
  270. cmMakefile* mf = this->GetMakefile();
  271. if (mf->GetDefinition("MSVC_C_ARCHITECTURE_ID") ||
  272. mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID"))
  273. {
  274. std::string pdbPath;
  275. std::string compilePdbPath;
  276. if(this->Target->GetType() == cmTarget::EXECUTABLE ||
  277. this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  278. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  279. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  280. {
  281. pdbPath = this->Target->GetPDBDirectory(this->GetConfigName());
  282. pdbPath += "/";
  283. pdbPath += this->Target->GetPDBName(this->GetConfigName());
  284. }
  285. if(this->Target->GetType() <= cmTarget::OBJECT_LIBRARY)
  286. {
  287. compilePdbPath = this->Target->GetCompilePDBPath(this->GetConfigName());
  288. if(compilePdbPath.empty())
  289. {
  290. compilePdbPath = this->Target->GetSupportDirectory() + "/";
  291. }
  292. }
  293. vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  294. ConvertToNinjaPath(pdbPath.c_str()),
  295. cmLocalGenerator::SHELL);
  296. vars["TARGET_COMPILE_PDB"] =
  297. this->GetLocalGenerator()->ConvertToOutputFormat(
  298. ConvertToNinjaPath(compilePdbPath.c_str()),
  299. cmLocalGenerator::SHELL);
  300. EnsureParentDirectoryExists(pdbPath);
  301. EnsureParentDirectoryExists(compilePdbPath);
  302. return true;
  303. }
  304. return false;
  305. }
  306. void
  307. cmNinjaTargetGenerator
  308. ::WriteLanguageRules(const std::string& language)
  309. {
  310. #ifdef NINJA_GEN_VERBOSE_FILES
  311. this->GetRulesFileStream()
  312. << "# Rules for language " << language << "\n\n";
  313. #endif
  314. this->WriteCompileRule(language);
  315. }
  316. void
  317. cmNinjaTargetGenerator
  318. ::WriteCompileRule(const std::string& lang)
  319. {
  320. cmLocalGenerator::RuleVariables vars;
  321. vars.RuleLauncher = "RULE_LAUNCH_COMPILE";
  322. vars.CMTarget = this->GetTarget();
  323. vars.Language = lang.c_str();
  324. vars.Source = "$in";
  325. vars.Object = "$out";
  326. vars.Defines = "$DEFINES";
  327. vars.TargetPDB = "$TARGET_PDB";
  328. vars.TargetCompilePDB = "$TARGET_COMPILE_PDB";
  329. vars.ObjectDir = "$OBJECT_DIR";
  330. cmMakefile* mf = this->GetMakefile();
  331. const std::string cId = mf->GetDefinition("CMAKE_C_COMPILER_ID")
  332. ? mf->GetSafeDefinition("CMAKE_C_COMPILER_ID")
  333. : mf->GetSafeDefinition("CMAKE_CXX_COMPILER_ID");
  334. const std::string sId = mf->GetDefinition("CMAKE_C_SIMULATE_ID")
  335. ? mf->GetSafeDefinition("CMAKE_C_SIMULATE_ID")
  336. : mf->GetSafeDefinition("CMAKE_CXX_SIMULATE_ID");
  337. const bool usingMSVC = (cId == "MSVC" || sId == "MSVC");
  338. // Tell ninja dependency format so all deps can be loaded into a database
  339. std::string deptype;
  340. std::string depfile;
  341. std::string cldeps;
  342. std::string flags = "$FLAGS";
  343. if (usingMSVC)
  344. {
  345. if (!mf->GetIsSourceFileTryCompile() && lang == "RC")
  346. {
  347. deptype = "gcc";
  348. depfile = "$DEP_FILE";
  349. const std::string cl = mf->GetDefinition("CMAKE_C_COMPILER") ?
  350. mf->GetSafeDefinition("CMAKE_C_COMPILER") :
  351. mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
  352. cldeps = "\"";
  353. cldeps += mf->GetSafeDefinition("CMAKE_CMCLDEPS_EXECUTABLE");
  354. cldeps += "\" " + lang + " $in \"$DEP_FILE\" $out \"";
  355. cldeps += mf->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX");
  356. cldeps += "\" \"" + cl + "\" ";
  357. }
  358. else
  359. {
  360. deptype = "msvc";
  361. depfile = "";
  362. flags += " /showIncludes";
  363. }
  364. }
  365. else
  366. {
  367. deptype = "gcc";
  368. const char* langdeptype = mf->GetDefinition("CMAKE_NINJA_DEPTYPE_" + lang);
  369. if (langdeptype)
  370. {
  371. deptype = langdeptype;
  372. }
  373. depfile = "$DEP_FILE";
  374. const std::string flagsName = "CMAKE_DEPFILE_FLAGS_" + lang;
  375. std::string depfileFlags = mf->GetSafeDefinition(flagsName);
  376. if (!depfileFlags.empty())
  377. {
  378. cmSystemTools::ReplaceString(depfileFlags, "<DEPFILE>", "$DEP_FILE");
  379. cmSystemTools::ReplaceString(depfileFlags, "<OBJECT>", "$out");
  380. cmSystemTools::ReplaceString(depfileFlags, "<CMAKE_C_COMPILER>",
  381. mf->GetDefinition("CMAKE_C_COMPILER"));
  382. flags += " " + depfileFlags;
  383. }
  384. }
  385. vars.Flags = flags.c_str();
  386. vars.DependencyFile = depfile.c_str();
  387. // Rule for compiling object file.
  388. const std::string cmdVar = std::string("CMAKE_") + lang + "_COMPILE_OBJECT";
  389. std::string compileCmd = mf->GetRequiredDefinition(cmdVar);
  390. std::vector<std::string> compileCmds;
  391. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  392. compileCmds.front().insert(0, cldeps);
  393. for (std::vector<std::string>::iterator i = compileCmds.begin();
  394. i != compileCmds.end(); ++i)
  395. this->GetLocalGenerator()->ExpandRuleVariables(*i, vars);
  396. std::string cmdLine =
  397. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  398. // Write the rule for compiling file of the given language.
  399. cmOStringStream comment;
  400. comment << "Rule for compiling " << lang << " files.";
  401. cmOStringStream description;
  402. description << "Building " << lang << " object $out";
  403. this->GetGlobalGenerator()->AddRule(this->LanguageCompilerRule(lang),
  404. cmdLine,
  405. description.str(),
  406. comment.str(),
  407. depfile,
  408. deptype,
  409. /*rspfile*/ "",
  410. /*rspcontent*/ "",
  411. /*restat*/ false,
  412. /*generator*/ false);
  413. }
  414. void
  415. cmNinjaTargetGenerator
  416. ::WriteObjectBuildStatements()
  417. {
  418. // Write comments.
  419. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  420. this->GetBuildFileStream()
  421. << "# Object build statements for "
  422. << cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
  423. << " target "
  424. << this->GetTargetName()
  425. << "\n\n";
  426. std::string config = this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  427. std::vector<cmSourceFile const*> customCommands;
  428. this->GeneratorTarget->GetCustomCommands(customCommands, config);
  429. for(std::vector<cmSourceFile const*>::const_iterator
  430. si = customCommands.begin();
  431. si != customCommands.end(); ++si)
  432. {
  433. cmCustomCommand const* cc = (*si)->GetCustomCommand();
  434. this->GetLocalGenerator()->AddCustomCommandTarget(cc, this->GetTarget());
  435. // Record the custom commands for this target. The container is used
  436. // in WriteObjectBuildStatement when called in a loop below.
  437. this->CustomCommands.push_back(cc);
  438. }
  439. std::vector<cmSourceFile const*> headerSources;
  440. this->GeneratorTarget->GetHeaderSources(headerSources, config);
  441. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  442. headerSources,
  443. this->MacOSXContentGenerator);
  444. std::vector<cmSourceFile const*> extraSources;
  445. this->GeneratorTarget->GetExtraSources(extraSources, config);
  446. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  447. extraSources,
  448. this->MacOSXContentGenerator);
  449. std::vector<cmSourceFile const*> externalObjects;
  450. this->GeneratorTarget->GetExternalObjects(externalObjects, config);
  451. for(std::vector<cmSourceFile const*>::const_iterator
  452. si = externalObjects.begin();
  453. si != externalObjects.end(); ++si)
  454. {
  455. this->Objects.push_back(this->GetSourceFilePath(*si));
  456. }
  457. cmNinjaDeps orderOnlyDeps;
  458. this->GetLocalGenerator()->AppendTargetDepends(this->Target, orderOnlyDeps);
  459. // Add order-only dependencies on custom command outputs.
  460. for(std::vector<cmCustomCommand const*>::const_iterator
  461. cci = this->CustomCommands.begin();
  462. cci != this->CustomCommands.end(); ++cci)
  463. {
  464. cmCustomCommand const* cc = *cci;
  465. cmCustomCommandGenerator ccg(*cc, this->GetConfigName(),
  466. this->GetMakefile());
  467. const std::vector<std::string>& ccoutputs = ccg.GetOutputs();
  468. std::transform(ccoutputs.begin(), ccoutputs.end(),
  469. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  470. }
  471. cmNinjaDeps orderOnlyTarget;
  472. orderOnlyTarget.push_back(this->OrderDependsTargetForTarget());
  473. this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
  474. "Order-only phony target for "
  475. + this->GetTargetName(),
  476. orderOnlyTarget,
  477. cmNinjaDeps(),
  478. cmNinjaDeps(),
  479. orderOnlyDeps);
  480. std::vector<cmSourceFile const*> objectSources;
  481. this->GeneratorTarget->GetObjectSources(objectSources, config);
  482. for(std::vector<cmSourceFile const*>::const_iterator
  483. si = objectSources.begin(); si != objectSources.end(); ++si)
  484. {
  485. this->WriteObjectBuildStatement(*si);
  486. }
  487. std::string def = this->GeneratorTarget->GetModuleDefinitionFile(config);
  488. if(!def.empty())
  489. {
  490. this->ModuleDefinitionFile = this->ConvertToNinjaPath(def.c_str());
  491. }
  492. this->GetBuildFileStream() << "\n";
  493. }
  494. void
  495. cmNinjaTargetGenerator
  496. ::WriteObjectBuildStatement(cmSourceFile const* source)
  497. {
  498. std::string comment;
  499. const std::string language = source->GetLanguage();
  500. std::string rule = this->LanguageCompilerRule(language);
  501. cmNinjaDeps outputs;
  502. std::string objectFileName = this->GetObjectFilePath(source);
  503. outputs.push_back(objectFileName);
  504. // Add this object to the list of object files.
  505. this->Objects.push_back(objectFileName);
  506. cmNinjaDeps explicitDeps;
  507. std::string sourceFileName;
  508. if (language == "RC")
  509. sourceFileName = source->GetFullPath();
  510. else
  511. sourceFileName = this->GetSourceFilePath(source);
  512. explicitDeps.push_back(sourceFileName);
  513. cmNinjaDeps implicitDeps;
  514. if(const char* objectDeps = source->GetProperty("OBJECT_DEPENDS")) {
  515. std::vector<std::string> depList;
  516. cmSystemTools::ExpandListArgument(objectDeps, depList);
  517. std::transform(depList.begin(), depList.end(),
  518. std::back_inserter(implicitDeps), MapToNinjaPath());
  519. }
  520. cmNinjaDeps orderOnlyDeps;
  521. orderOnlyDeps.push_back(this->OrderDependsTargetForTarget());
  522. // If the source file is GENERATED and does not have a custom command
  523. // (either attached to this source file or another one), assume that one of
  524. // the target dependencies, OBJECT_DEPENDS or header file custom commands
  525. // will rebuild the file.
  526. if (source->GetPropertyAsBool("GENERATED") && !source->GetCustomCommand() &&
  527. !this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFileName)) {
  528. this->GetGlobalGenerator()->AddAssumedSourceDependencies(sourceFileName,
  529. orderOnlyDeps);
  530. }
  531. cmNinjaVars vars;
  532. vars["FLAGS"] = this->ComputeFlagsForObject(source, language);
  533. vars["DEFINES"] = this->ComputeDefines(source, language);
  534. if (needsDepFile(language)) {
  535. vars["DEP_FILE"] =
  536. cmGlobalNinjaGenerator::EncodeDepfileSpace(objectFileName + ".d");
  537. }
  538. EnsureParentDirectoryExists(objectFileName);
  539. std::string objectDir = this->Target->GetSupportDirectory();
  540. vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  541. ConvertToNinjaPath(objectDir.c_str()),
  542. cmLocalGenerator::SHELL);
  543. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetTarget(), vars);
  544. this->SetMsvcTargetPdbVariable(vars);
  545. if(this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS"))
  546. {
  547. cmLocalGenerator::RuleVariables compileObjectVars;
  548. std::string lang = language;
  549. compileObjectVars.Language = lang.c_str();
  550. std::string escapedSourceFileName = sourceFileName;
  551. if (!cmSystemTools::FileIsFullPath(sourceFileName.c_str()))
  552. {
  553. escapedSourceFileName = cmSystemTools::CollapseFullPath(
  554. escapedSourceFileName.c_str(),
  555. this->GetGlobalGenerator()->GetCMakeInstance()->
  556. GetHomeOutputDirectory());
  557. }
  558. escapedSourceFileName =
  559. this->LocalGenerator->ConvertToOutputFormat(
  560. escapedSourceFileName, cmLocalGenerator::SHELL);
  561. compileObjectVars.Source = escapedSourceFileName.c_str();
  562. compileObjectVars.Object = objectFileName.c_str();
  563. compileObjectVars.ObjectDir = objectDir.c_str();
  564. compileObjectVars.Flags = vars["FLAGS"].c_str();
  565. compileObjectVars.Defines = vars["DEFINES"].c_str();
  566. // Rule for compiling object file.
  567. std::string compileCmdVar = "CMAKE_";
  568. compileCmdVar += language;
  569. compileCmdVar += "_COMPILE_OBJECT";
  570. std::string compileCmd =
  571. this->GetMakefile()->GetRequiredDefinition(compileCmdVar);
  572. std::vector<std::string> compileCmds;
  573. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  574. for (std::vector<std::string>::iterator i = compileCmds.begin();
  575. i != compileCmds.end(); ++i)
  576. this->GetLocalGenerator()->ExpandRuleVariables(*i, compileObjectVars);
  577. std::string cmdLine =
  578. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  579. this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine,
  580. sourceFileName);
  581. }
  582. this->GetGlobalGenerator()->WriteBuild(this->GetBuildFileStream(),
  583. comment,
  584. rule,
  585. outputs,
  586. explicitDeps,
  587. implicitDeps,
  588. orderOnlyDeps,
  589. vars);
  590. if(const char* objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
  591. std::vector<std::string> outputList;
  592. cmSystemTools::ExpandListArgument(objectOutputs, outputList);
  593. std::transform(outputList.begin(), outputList.end(), outputList.begin(),
  594. MapToNinjaPath());
  595. this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
  596. "Additional output files.",
  597. outputList,
  598. outputs);
  599. }
  600. }
  601. //----------------------------------------------------------------------------
  602. void
  603. cmNinjaTargetGenerator
  604. ::AddModuleDefinitionFlag(std::string& flags)
  605. {
  606. if(this->ModuleDefinitionFile.empty())
  607. {
  608. return;
  609. }
  610. // TODO: Create a per-language flag variable.
  611. const char* defFileFlag =
  612. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  613. if(!defFileFlag)
  614. {
  615. return;
  616. }
  617. // Append the flag and value. Use ConvertToLinkReference to help
  618. // vs6's "cl -link" pass it to the linker.
  619. std::string flag = defFileFlag;
  620. flag += (this->LocalGenerator->ConvertToLinkReference(
  621. this->ModuleDefinitionFile));
  622. this->LocalGenerator->AppendFlags(flags, flag);
  623. }
  624. void
  625. cmNinjaTargetGenerator
  626. ::EnsureDirectoryExists(const std::string& path) const
  627. {
  628. if (cmSystemTools::FileIsFullPath(path.c_str()))
  629. {
  630. cmSystemTools::MakeDirectory(path.c_str());
  631. }
  632. else
  633. {
  634. const std::string fullPath = std::string(this->GetGlobalGenerator()->
  635. GetCMakeInstance()->GetHomeOutputDirectory())
  636. + "/" + path;
  637. cmSystemTools::MakeDirectory(fullPath.c_str());
  638. }
  639. }
  640. void
  641. cmNinjaTargetGenerator
  642. ::EnsureParentDirectoryExists(const std::string& path) const
  643. {
  644. EnsureDirectoryExists(cmSystemTools::GetParentDirectory(path.c_str()));
  645. }
  646. //----------------------------------------------------------------------------
  647. void
  648. cmNinjaTargetGenerator::MacOSXContentGeneratorType::operator()(
  649. cmSourceFile const& source, const char* pkgloc)
  650. {
  651. // Skip OS X content when not building a Framework or Bundle.
  652. if(!this->Generator->GetTarget()->IsBundleOnApple())
  653. {
  654. return;
  655. }
  656. std::string macdir =
  657. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc);
  658. // Get the input file location.
  659. std::string input = source.GetFullPath();
  660. input =
  661. this->Generator->GetLocalGenerator()->ConvertToNinjaPath(input.c_str());
  662. // Get the output file location.
  663. std::string output = macdir;
  664. output += "/";
  665. output += cmSystemTools::GetFilenameName(input);
  666. output =
  667. this->Generator->GetLocalGenerator()->ConvertToNinjaPath(output.c_str());
  668. // Write a build statement to copy the content into the bundle.
  669. this->Generator->GetGlobalGenerator()->WriteMacOSXContentBuild(input,
  670. output);
  671. // Add as a dependency of all target so that it gets called.
  672. this->Generator->GetGlobalGenerator()->AddDependencyToAll(output);
  673. }
  674. void cmNinjaTargetGenerator::addPoolNinjaVariable(
  675. const std::string& pool_property,
  676. cmTarget* target,
  677. cmNinjaVars& vars)
  678. {
  679. const char* pool = target->GetProperty(pool_property);
  680. if (pool)
  681. {
  682. vars["pool"] = pool;
  683. }
  684. }