cmNinjaTargetGenerator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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->GeneratorTarget->ModuleDefinitionFile.empty())
  205. {
  206. result.push_back(this->GeneratorTarget->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. flags += " " + depfileFlagsStr;
  296. }
  297. vars.Flags = flags.c_str();
  298. // Rule for compiling object file.
  299. std::string compileCmdVar = "CMAKE_";
  300. compileCmdVar += language;
  301. compileCmdVar += "_COMPILE_OBJECT";
  302. std::string compileCmd =
  303. this->GetMakefile()->GetRequiredDefinition(compileCmdVar.c_str());
  304. std::vector<std::string> compileCmds;
  305. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  306. for (std::vector<std::string>::iterator i = compileCmds.begin();
  307. i != compileCmds.end(); ++i)
  308. this->GetLocalGenerator()->ExpandRuleVariables(*i, vars);
  309. std::string cmdLine =
  310. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  311. // Write the rule for compiling file of the given language.
  312. std::ostringstream comment;
  313. comment << "Rule for compiling " << language << " files.";
  314. std::ostringstream description;
  315. description << "Building " << language << " object $out";
  316. this->GetGlobalGenerator()->AddRule(this->LanguageCompilerRule(language),
  317. cmdLine,
  318. description.str(),
  319. comment.str(),
  320. depfile);
  321. }
  322. void
  323. cmNinjaTargetGenerator
  324. ::WriteObjectBuildStatements()
  325. {
  326. // Write comments.
  327. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  328. this->GetBuildFileStream()
  329. << "# Object build statements for "
  330. << cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
  331. << " target "
  332. << this->GetTargetName()
  333. << "\n\n";
  334. for(std::vector<cmSourceFile*>::const_iterator
  335. si = this->GeneratorTarget->CustomCommands.begin();
  336. si != this->GeneratorTarget->CustomCommands.end(); ++si)
  337. {
  338. cmCustomCommand const* cc = (*si)->GetCustomCommand();
  339. this->GetLocalGenerator()->AddCustomCommandTarget(cc, this->GetTarget());
  340. }
  341. // TODO: this->GeneratorTarget->OSXContent
  342. for(std::vector<cmSourceFile*>::const_iterator
  343. si = this->GeneratorTarget->ExternalObjects.begin();
  344. si != this->GeneratorTarget->ExternalObjects.end(); ++si)
  345. {
  346. this->Objects.push_back(this->GetSourceFilePath(*si));
  347. }
  348. for(std::vector<cmSourceFile*>::const_iterator
  349. si = this->GeneratorTarget->ObjectSources.begin();
  350. si != this->GeneratorTarget->ObjectSources.end(); ++si)
  351. {
  352. this->WriteObjectBuildStatement(*si);
  353. }
  354. {
  355. // Add object library contents as external objects.
  356. std::vector<std::string> objs;
  357. this->GeneratorTarget->UseObjectLibraries(objs);
  358. for(std::vector<std::string>::iterator oi = objs.begin();
  359. oi != objs.end(); ++oi)
  360. {
  361. this->Objects.push_back(ConvertToNinjaPath(oi->c_str()));
  362. }
  363. }
  364. this->GetBuildFileStream() << "\n";
  365. }
  366. void
  367. cmNinjaTargetGenerator
  368. ::WriteObjectBuildStatement(cmSourceFile* source)
  369. {
  370. cmNinjaDeps emptyDeps;
  371. std::string comment;
  372. const char* language = source->GetLanguage();
  373. std::string rule = this->LanguageCompilerRule(language);
  374. cmNinjaDeps outputs;
  375. std::string objectFileName = this->GetObjectFilePath(source);
  376. outputs.push_back(objectFileName);
  377. // Add this object to the list of object files.
  378. this->Objects.push_back(objectFileName);
  379. cmNinjaDeps explicitDeps;
  380. std::string sourceFileName = this->GetSourceFilePath(source);
  381. explicitDeps.push_back(sourceFileName);
  382. // Ensure that the target dependencies are built before any source file in
  383. // the target, using order-only dependencies.
  384. cmNinjaDeps orderOnlyDeps;
  385. this->GetLocalGenerator()->AppendTargetDepends(this->Target, orderOnlyDeps);
  386. if(const char* objectDeps = source->GetProperty("OBJECT_DEPENDS")) {
  387. std::vector<std::string> depList;
  388. cmSystemTools::ExpandListArgument(objectDeps, depList);
  389. std::transform(depList.begin(), depList.end(),
  390. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  391. }
  392. // Add order-only dependencies on custom command outputs.
  393. for(std::vector<cmSourceFile*>::const_iterator
  394. si = this->GeneratorTarget->CustomCommands.begin();
  395. si != this->GeneratorTarget->CustomCommands.end(); ++si)
  396. {
  397. cmCustomCommand const* cc = (*si)->GetCustomCommand();
  398. const std::vector<std::string>& ccoutputs = cc->GetOutputs();
  399. std::transform(ccoutputs.begin(), ccoutputs.end(),
  400. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  401. }
  402. // If the source file is GENERATED and does not have a custom command
  403. // (either attached to this source file or another one), assume that one of
  404. // the target dependencies, OBJECT_DEPENDS or header file custom commands
  405. // will rebuild the file.
  406. if (source->GetPropertyAsBool("GENERATED") && !source->GetCustomCommand() &&
  407. !this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFileName)) {
  408. this->GetGlobalGenerator()->AddAssumedSourceDependencies(sourceFileName,
  409. orderOnlyDeps);
  410. }
  411. cmNinjaVars vars;
  412. vars["FLAGS"] = this->ComputeFlagsForObject(source, language);
  413. vars["DEFINES"] = this->ComputeDefines(source, language);
  414. vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  415. this->GetTargetPDB().c_str(), cmLocalGenerator::SHELL);
  416. cmGlobalNinjaGenerator::WriteBuild(this->GetBuildFileStream(),
  417. comment,
  418. rule,
  419. outputs,
  420. explicitDeps,
  421. emptyDeps,
  422. orderOnlyDeps,
  423. vars);
  424. if(const char* objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
  425. std::vector<std::string> outputList;
  426. cmSystemTools::ExpandListArgument(objectOutputs, outputList);
  427. std::transform(outputList.begin(), outputList.end(), outputList.begin(),
  428. MapToNinjaPath());
  429. cmGlobalNinjaGenerator::WritePhonyBuild(this->GetBuildFileStream(),
  430. "Additional output files.",
  431. outputList,
  432. outputs);
  433. }
  434. }
  435. //----------------------------------------------------------------------------
  436. void
  437. cmNinjaTargetGenerator
  438. ::AddModuleDefinitionFlag(std::string& flags)
  439. {
  440. if(this->GeneratorTarget->ModuleDefinitionFile.empty())
  441. {
  442. return;
  443. }
  444. // TODO: Create a per-language flag variable.
  445. const char* defFileFlag =
  446. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  447. if(!defFileFlag)
  448. {
  449. return;
  450. }
  451. // Append the flag and value. Use ConvertToLinkReference to help
  452. // vs6's "cl -link" pass it to the linker.
  453. std::string flag = defFileFlag;
  454. flag += (this->LocalGenerator->ConvertToLinkReference(
  455. this->GeneratorTarget->ModuleDefinitionFile.c_str()));
  456. this->LocalGenerator->AppendFlags(flags, flag.c_str());
  457. }