cmNinjaTargetGenerator.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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(cmTarget* 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->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. : Target(target),
  52. Makefile(target->GetMakefile()),
  53. LocalGenerator(
  54. static_cast<cmLocalNinjaGenerator*>(Makefile->GetLocalGenerator())),
  55. Objects()
  56. {
  57. this->GeneratorTarget =
  58. this->GetGlobalGenerator()->GetGeneratorTarget(target);
  59. }
  60. cmNinjaTargetGenerator::~cmNinjaTargetGenerator()
  61. {
  62. }
  63. cmGeneratedFileStream& cmNinjaTargetGenerator::GetBuildFileStream() const
  64. {
  65. return *this->GetGlobalGenerator()->GetBuildFileStream();
  66. }
  67. cmGeneratedFileStream& cmNinjaTargetGenerator::GetRulesFileStream() const
  68. {
  69. return *this->GetGlobalGenerator()->GetRulesFileStream();
  70. }
  71. cmGlobalNinjaGenerator* cmNinjaTargetGenerator::GetGlobalGenerator() const
  72. {
  73. return this->LocalGenerator->GetGlobalNinjaGenerator();
  74. }
  75. const char* cmNinjaTargetGenerator::GetConfigName() const
  76. {
  77. return this->LocalGenerator->ConfigName.c_str();
  78. }
  79. // TODO: Picked up from cmMakefileTargetGenerator. Refactor it.
  80. const char* cmNinjaTargetGenerator::GetFeature(const char* feature)
  81. {
  82. return this->Target->GetFeature(feature, this->GetConfigName());
  83. }
  84. // TODO: Picked up from cmMakefileTargetGenerator. Refactor it.
  85. bool cmNinjaTargetGenerator::GetFeatureAsBool(const char* feature)
  86. {
  87. return cmSystemTools::IsOn(this->GetFeature(feature));
  88. }
  89. // TODO: Picked up from cmMakefileTargetGenerator. Refactor it.
  90. void cmNinjaTargetGenerator::AddFeatureFlags(std::string& flags,
  91. const char* lang)
  92. {
  93. // Add language-specific flags.
  94. this->LocalGenerator->AddLanguageFlags(flags, lang, this->GetConfigName());
  95. if(this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION"))
  96. {
  97. this->LocalGenerator->AppendFeatureOptions(flags, lang, "IPO");
  98. }
  99. }
  100. // TODO: Most of the code is picked up from
  101. // void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink),
  102. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  103. // Refactor it.
  104. std::string
  105. cmNinjaTargetGenerator::ComputeFlagsForObject(cmSourceFile *source,
  106. const std::string& language)
  107. {
  108. std::string flags;
  109. this->AddFeatureFlags(flags, language.c_str());
  110. this->GetLocalGenerator()->AddArchitectureFlags(flags,
  111. this->GetTarget(),
  112. language.c_str(),
  113. this->GetConfigName());
  114. // TODO: Fortran support.
  115. // // Fortran-specific flags computed for this target.
  116. // if(*l == "Fortran")
  117. // {
  118. // this->AddFortranFlags(flags);
  119. // }
  120. // Add shared-library flags if needed.
  121. {
  122. bool shared = ((this->Target->GetType() == cmTarget::SHARED_LIBRARY) ||
  123. (this->Target->GetType() == cmTarget::MODULE_LIBRARY));
  124. this->GetLocalGenerator()->AddSharedFlags(flags, language.c_str(), shared);
  125. }
  126. // TODO: Handle response file.
  127. // Add include directory flags.
  128. {
  129. std::vector<std::string> includes;
  130. this->LocalGenerator->GetIncludeDirectories(includes, this->Target,
  131. language.c_str());
  132. std::string includeFlags =
  133. this->LocalGenerator->GetIncludeFlags(includes, language.c_str(), false);
  134. this->LocalGenerator->AppendFlags(flags, includeFlags.c_str());
  135. }
  136. // Append old-style preprocessor definition flags.
  137. this->LocalGenerator->AppendFlags(flags, this->Makefile->GetDefineFlags());
  138. // Add target-specific and source-specific flags.
  139. this->LocalGenerator->AppendFlags(flags,
  140. this->Target->GetProperty("COMPILE_FLAGS"));
  141. this->LocalGenerator->AppendFlags(flags,
  142. source->GetProperty("COMPILE_FLAGS"));
  143. // TODO: Handle Apple frameworks.
  144. return flags;
  145. }
  146. // TODO: Refactor with
  147. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
  148. std::string
  149. cmNinjaTargetGenerator::
  150. ComputeDefines(cmSourceFile *source, const std::string& language)
  151. {
  152. std::string defines;
  153. // Add the export symbol definition for shared library objects.
  154. if(const char* exportMacro = this->Target->GetExportMacro())
  155. {
  156. this->LocalGenerator->AppendDefines(defines, exportMacro,
  157. language.c_str());
  158. }
  159. // Add preprocessor definitions for this target and configuration.
  160. this->LocalGenerator->AppendDefines
  161. (defines,
  162. this->Makefile->GetProperty("COMPILE_DEFINITIONS"),
  163. language.c_str());
  164. this->LocalGenerator->AppendDefines
  165. (defines,
  166. this->Target->GetProperty("COMPILE_DEFINITIONS"),
  167. language.c_str());
  168. this->LocalGenerator->AppendDefines
  169. (defines,
  170. source->GetProperty("COMPILE_DEFINITIONS"),
  171. language.c_str());
  172. {
  173. std::string defPropName = "COMPILE_DEFINITIONS_";
  174. defPropName += cmSystemTools::UpperCase(this->GetConfigName());
  175. this->LocalGenerator->AppendDefines
  176. (defines,
  177. this->Makefile->GetProperty(defPropName.c_str()),
  178. language.c_str());
  179. this->LocalGenerator->AppendDefines
  180. (defines,
  181. this->Target->GetProperty(defPropName.c_str()),
  182. language.c_str());
  183. this->LocalGenerator->AppendDefines
  184. (defines,
  185. source->GetProperty(defPropName.c_str()),
  186. language.c_str());
  187. }
  188. return defines;
  189. }
  190. cmNinjaDeps cmNinjaTargetGenerator::ComputeLinkDeps() const
  191. {
  192. // Static libraries never depend on other targets for linking.
  193. if (this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  194. this->Target->GetType() == cmTarget::OBJECT_LIBRARY)
  195. return cmNinjaDeps();
  196. cmComputeLinkInformation* cli =
  197. this->Target->GetLinkInformation(this->GetConfigName());
  198. if(!cli)
  199. return cmNinjaDeps();
  200. const std::vector<std::string> &deps = cli->GetDepends();
  201. cmNinjaDeps result(deps.size());
  202. std::transform(deps.begin(), deps.end(), result.begin(), MapToNinjaPath());
  203. // Add a dependency on the link definitions file, if any.
  204. if(!this->ModuleDefinitionFile.empty())
  205. {
  206. result.push_back(this->ModuleDefinitionFile);
  207. }
  208. return result;
  209. }
  210. std::string
  211. cmNinjaTargetGenerator
  212. ::GetSourceFilePath(cmSourceFile* source) const
  213. {
  214. return ConvertToNinjaPath(source->GetFullPath().c_str());
  215. }
  216. std::string
  217. cmNinjaTargetGenerator
  218. ::GetObjectFilePath(cmSourceFile* source) const
  219. {
  220. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  221. if(!path.empty())
  222. path += "/";
  223. std::string const& objectName = this->GeneratorTarget->Objects[source];
  224. path += this->LocalGenerator->GetTargetDirectory(*this->Target);
  225. path += "/";
  226. path += objectName;
  227. return path;
  228. }
  229. std::string cmNinjaTargetGenerator::GetTargetOutputDir() const
  230. {
  231. std::string dir = this->Target->GetDirectory(this->GetConfigName());
  232. return ConvertToNinjaPath(dir.c_str());
  233. }
  234. std::string
  235. cmNinjaTargetGenerator
  236. ::GetTargetFilePath(const std::string& name) const
  237. {
  238. std::string path = this->GetTargetOutputDir();
  239. if (path.empty() || path == ".")
  240. return name;
  241. path += "/";
  242. path += name;
  243. return path;
  244. }
  245. std::string cmNinjaTargetGenerator::GetTargetName() const
  246. {
  247. return this->Target->GetName();
  248. }
  249. std::string cmNinjaTargetGenerator::GetTargetPDB() const
  250. {
  251. std::string targetFullPathPDB;
  252. if(this->Target->GetType() == cmTarget::EXECUTABLE ||
  253. this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  254. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  255. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  256. {
  257. targetFullPathPDB = this->Target->GetDirectory(this->GetConfigName());
  258. targetFullPathPDB += "/";
  259. targetFullPathPDB += this->Target->GetPDBName(this->GetConfigName());
  260. }
  261. return ConvertToNinjaPath(targetFullPathPDB.c_str());
  262. }
  263. void
  264. cmNinjaTargetGenerator
  265. ::WriteLanguageRules(const std::string& language)
  266. {
  267. this->GetRulesFileStream()
  268. << "# Rules for language " << language << "\n\n";
  269. this->WriteCompileRule(language);
  270. this->GetRulesFileStream() << "\n";
  271. }
  272. void
  273. cmNinjaTargetGenerator
  274. ::WriteCompileRule(const std::string& language)
  275. {
  276. cmLocalGenerator::RuleVariables vars;
  277. vars.RuleLauncher = "RULE_LAUNCH_COMPILE";
  278. vars.CMTarget = this->GetTarget();
  279. std::string lang = language;
  280. vars.Language = lang.c_str();
  281. vars.Source = "$in";
  282. vars.Object = "$out";
  283. std::string flags = "$FLAGS";
  284. vars.Defines = "$DEFINES";
  285. vars.TargetPDB = "$TARGET_PDB";
  286. std::string depfile;
  287. std::string depfileFlagsName = "CMAKE_DEPFILE_FLAGS_" + language;
  288. const char *depfileFlags =
  289. this->GetMakefile()->GetDefinition(depfileFlagsName.c_str());
  290. if (depfileFlags) {
  291. std::string depfileFlagsStr = depfileFlags;
  292. depfile = "$out.d";
  293. cmSystemTools::ReplaceString(depfileFlagsStr, "<DEPFILE>",
  294. depfile.c_str());
  295. cmSystemTools::ReplaceString(depfileFlagsStr, "<OBJECT>",
  296. "$out");
  297. cmSystemTools::ReplaceString(depfileFlagsStr, "<CMAKE_C_COMPILER>",
  298. this->GetMakefile()->GetDefinition("CMAKE_C_COMPILER"));
  299. flags += " " + depfileFlagsStr;
  300. }
  301. vars.Flags = flags.c_str();
  302. // Rule for compiling object file.
  303. std::string compileCmdVar = "CMAKE_";
  304. compileCmdVar += language;
  305. compileCmdVar += "_COMPILE_OBJECT";
  306. std::string compileCmd =
  307. this->GetMakefile()->GetRequiredDefinition(compileCmdVar.c_str());
  308. std::vector<std::string> compileCmds;
  309. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  310. for (std::vector<std::string>::iterator i = compileCmds.begin();
  311. i != compileCmds.end(); ++i)
  312. this->GetLocalGenerator()->ExpandRuleVariables(*i, vars);
  313. std::string cmdLine =
  314. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  315. // Write the rule for compiling file of the given language.
  316. std::ostringstream comment;
  317. comment << "Rule for compiling " << language << " files.";
  318. std::ostringstream description;
  319. description << "Building " << language << " object $out";
  320. this->GetGlobalGenerator()->AddRule(this->LanguageCompilerRule(language),
  321. cmdLine,
  322. description.str(),
  323. comment.str(),
  324. depfile);
  325. }
  326. void
  327. cmNinjaTargetGenerator
  328. ::WriteObjectBuildStatements()
  329. {
  330. // Write comments.
  331. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  332. this->GetBuildFileStream()
  333. << "# Object build statements for "
  334. << cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
  335. << " target "
  336. << this->GetTargetName()
  337. << "\n\n";
  338. for(std::vector<cmSourceFile*>::const_iterator
  339. si = this->GeneratorTarget->CustomCommands.begin();
  340. si != this->GeneratorTarget->CustomCommands.end(); ++si)
  341. {
  342. cmCustomCommand const* cc = (*si)->GetCustomCommand();
  343. this->GetLocalGenerator()->AddCustomCommandTarget(cc, this->GetTarget());
  344. }
  345. for(std::vector<cmSourceFile*>::const_iterator
  346. si = this->GeneratorTarget->ExternalObjects.begin();
  347. si != this->GeneratorTarget->ExternalObjects.end(); ++si)
  348. {
  349. this->Objects.push_back(this->GetSourceFilePath(*si));
  350. }
  351. for(std::vector<cmSourceFile*>::const_iterator
  352. si = this->GeneratorTarget->ObjectSources.begin();
  353. si != this->GeneratorTarget->ObjectSources.end(); ++si)
  354. {
  355. this->WriteObjectBuildStatement(*si);
  356. }
  357. if(!this->GeneratorTarget->ModuleDefinitionFile.empty())
  358. {
  359. this->ModuleDefinitionFile = this->ConvertToNinjaPath(
  360. this->GeneratorTarget->ModuleDefinitionFile.c_str());
  361. }
  362. {
  363. // Add object library contents as external objects.
  364. std::vector<std::string> objs;
  365. this->GeneratorTarget->UseObjectLibraries(objs);
  366. for(std::vector<std::string>::iterator oi = objs.begin();
  367. oi != objs.end(); ++oi)
  368. {
  369. this->Objects.push_back(ConvertToNinjaPath(oi->c_str()));
  370. }
  371. }
  372. this->GetBuildFileStream() << "\n";
  373. }
  374. void
  375. cmNinjaTargetGenerator
  376. ::WriteObjectBuildStatement(cmSourceFile* source)
  377. {
  378. cmNinjaDeps emptyDeps;
  379. std::string comment;
  380. const char* language = source->GetLanguage();
  381. std::string rule = this->LanguageCompilerRule(language);
  382. cmNinjaDeps outputs;
  383. std::string objectFileName = this->GetObjectFilePath(source);
  384. outputs.push_back(objectFileName);
  385. // Add this object to the list of object files.
  386. this->Objects.push_back(objectFileName);
  387. cmNinjaDeps explicitDeps;
  388. std::string sourceFileName = this->GetSourceFilePath(source);
  389. explicitDeps.push_back(sourceFileName);
  390. // Ensure that the target dependencies are built before any source file in
  391. // the target, using order-only dependencies.
  392. cmNinjaDeps orderOnlyDeps;
  393. this->GetLocalGenerator()->AppendTargetDepends(this->Target, orderOnlyDeps);
  394. if(const char* objectDeps = source->GetProperty("OBJECT_DEPENDS")) {
  395. std::vector<std::string> depList;
  396. cmSystemTools::ExpandListArgument(objectDeps, depList);
  397. std::transform(depList.begin(), depList.end(),
  398. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  399. }
  400. // Add order-only dependencies on custom command outputs.
  401. for(std::vector<cmSourceFile*>::const_iterator
  402. si = this->GeneratorTarget->CustomCommands.begin();
  403. si != this->GeneratorTarget->CustomCommands.end(); ++si)
  404. {
  405. cmCustomCommand const* cc = (*si)->GetCustomCommand();
  406. const std::vector<std::string>& ccoutputs = cc->GetOutputs();
  407. std::transform(ccoutputs.begin(), ccoutputs.end(),
  408. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  409. }
  410. // If the source file is GENERATED and does not have a custom command
  411. // (either attached to this source file or another one), assume that one of
  412. // the target dependencies, OBJECT_DEPENDS or header file custom commands
  413. // will rebuild the file.
  414. if (source->GetPropertyAsBool("GENERATED") && !source->GetCustomCommand() &&
  415. !this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFileName)) {
  416. this->GetGlobalGenerator()->AddAssumedSourceDependencies(sourceFileName,
  417. orderOnlyDeps);
  418. }
  419. cmNinjaVars vars;
  420. vars["FLAGS"] = this->ComputeFlagsForObject(source, language);
  421. vars["DEFINES"] = this->ComputeDefines(source, language);
  422. vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  423. this->GetTargetPDB().c_str(), cmLocalGenerator::SHELL);
  424. if(this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS"))
  425. {
  426. cmLocalGenerator::RuleVariables compileObjectVars;
  427. std::string lang = language;
  428. compileObjectVars.Language = lang.c_str();
  429. std::string escapedSourceFileName =
  430. this->LocalGenerator->ConvertToOutputFormat(
  431. sourceFileName.c_str(), cmLocalGenerator::SHELL);
  432. compileObjectVars.Source = escapedSourceFileName.c_str();
  433. compileObjectVars.Object = objectFileName.c_str();
  434. compileObjectVars.Flags = vars["FLAGS"].c_str();
  435. compileObjectVars.Defines = vars["DEFINES"].c_str();
  436. // Rule for compiling object file.
  437. std::string compileCmdVar = "CMAKE_";
  438. compileCmdVar += language;
  439. compileCmdVar += "_COMPILE_OBJECT";
  440. std::string compileCmd =
  441. this->GetMakefile()->GetRequiredDefinition(compileCmdVar.c_str());
  442. std::vector<std::string> compileCmds;
  443. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  444. for (std::vector<std::string>::iterator i = compileCmds.begin();
  445. i != compileCmds.end(); ++i)
  446. this->GetLocalGenerator()->ExpandRuleVariables(*i, compileObjectVars);
  447. std::string cmdLine =
  448. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  449. this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine,
  450. sourceFileName);
  451. }
  452. cmGlobalNinjaGenerator::WriteBuild(this->GetBuildFileStream(),
  453. comment,
  454. rule,
  455. outputs,
  456. explicitDeps,
  457. emptyDeps,
  458. orderOnlyDeps,
  459. vars);
  460. if(const char* objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
  461. std::vector<std::string> outputList;
  462. cmSystemTools::ExpandListArgument(objectOutputs, outputList);
  463. std::transform(outputList.begin(), outputList.end(), outputList.begin(),
  464. MapToNinjaPath());
  465. cmGlobalNinjaGenerator::WritePhonyBuild(this->GetBuildFileStream(),
  466. "Additional output files.",
  467. outputList,
  468. outputs);
  469. }
  470. }
  471. //----------------------------------------------------------------------------
  472. void
  473. cmNinjaTargetGenerator
  474. ::AddModuleDefinitionFlag(std::string& flags)
  475. {
  476. if(this->ModuleDefinitionFile.empty())
  477. {
  478. return;
  479. }
  480. // TODO: Create a per-language flag variable.
  481. const char* defFileFlag =
  482. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  483. if(!defFileFlag)
  484. {
  485. return;
  486. }
  487. // Append the flag and value. Use ConvertToLinkReference to help
  488. // vs6's "cl -link" pass it to the linker.
  489. std::string flag = defFileFlag;
  490. flag += (this->LocalGenerator->ConvertToLinkReference(
  491. this->ModuleDefinitionFile.c_str()));
  492. this->LocalGenerator->AppendFlags(flags, flag.c_str());
  493. }