cmCommonTargetGenerator.cxx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2015 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCommonTargetGenerator.h"
  11. #include "cmComputeLinkInformation.h"
  12. #include "cmGeneratorTarget.h"
  13. #include "cmGlobalCommonGenerator.h"
  14. #include "cmLocalCommonGenerator.h"
  15. #include "cmMakefile.h"
  16. #include "cmSourceFile.h"
  17. #include "cmSystemTools.h"
  18. cmCommonTargetGenerator::cmCommonTargetGenerator(cmGeneratorTarget* gt)
  19. : GeneratorTarget(gt)
  20. , Makefile(gt->Makefile)
  21. , LocalGenerator(static_cast<cmLocalCommonGenerator*>(gt->LocalGenerator))
  22. , GlobalGenerator(static_cast<cmGlobalCommonGenerator*>(
  23. gt->LocalGenerator->GetGlobalGenerator()))
  24. , ConfigName(LocalGenerator->GetConfigName())
  25. , ModuleDefinitionFile(GeneratorTarget->GetModuleDefinitionFile(ConfigName))
  26. {
  27. }
  28. cmCommonTargetGenerator::~cmCommonTargetGenerator()
  29. {
  30. }
  31. std::string const& cmCommonTargetGenerator::GetConfigName() const
  32. {
  33. return this->ConfigName;
  34. }
  35. std::string cmCommonTargetGenerator::Convert(
  36. std::string const& source, cmOutputConverter::RelativeRoot relative,
  37. cmOutputConverter::OutputFormat output)
  38. {
  39. return this->LocalGenerator->Convert(source, relative, output);
  40. }
  41. const char* cmCommonTargetGenerator::GetFeature(const std::string& feature)
  42. {
  43. return this->GeneratorTarget->GetFeature(feature, this->ConfigName);
  44. }
  45. bool cmCommonTargetGenerator::GetFeatureAsBool(const std::string& feature)
  46. {
  47. return this->GeneratorTarget->GetFeatureAsBool(feature, this->ConfigName);
  48. }
  49. void cmCommonTargetGenerator::AddFeatureFlags(std::string& flags,
  50. const std::string& lang)
  51. {
  52. // Add language-specific flags.
  53. this->LocalGenerator->AddLanguageFlags(flags, lang, this->ConfigName);
  54. if (this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION")) {
  55. this->LocalGenerator->AppendFeatureOptions(flags, lang, "IPO");
  56. }
  57. }
  58. void cmCommonTargetGenerator::AddModuleDefinitionFlag(std::string& flags)
  59. {
  60. if (!this->ModuleDefinitionFile) {
  61. return;
  62. }
  63. // TODO: Create a per-language flag variable.
  64. const char* defFileFlag =
  65. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  66. if (!defFileFlag) {
  67. return;
  68. }
  69. // Append the flag and value. Use ConvertToLinkReference to help
  70. // vs6's "cl -link" pass it to the linker.
  71. std::string flag = defFileFlag;
  72. flag += (this->LocalGenerator->ConvertToLinkReference(
  73. this->ModuleDefinitionFile->GetFullPath()));
  74. this->LocalGenerator->AppendFlags(flags, flag);
  75. }
  76. void cmCommonTargetGenerator::AppendFortranFormatFlags(
  77. std::string& flags, cmSourceFile const& source)
  78. {
  79. const char* srcfmt = source.GetProperty("Fortran_FORMAT");
  80. cmOutputConverter::FortranFormat format =
  81. cmOutputConverter::GetFortranFormat(srcfmt);
  82. if (format == cmOutputConverter::FortranFormatNone) {
  83. const char* tgtfmt = this->GeneratorTarget->GetProperty("Fortran_FORMAT");
  84. format = cmOutputConverter::GetFortranFormat(tgtfmt);
  85. }
  86. const char* var = 0;
  87. switch (format) {
  88. case cmOutputConverter::FortranFormatFixed:
  89. var = "CMAKE_Fortran_FORMAT_FIXED_FLAG";
  90. break;
  91. case cmOutputConverter::FortranFormatFree:
  92. var = "CMAKE_Fortran_FORMAT_FREE_FLAG";
  93. break;
  94. default:
  95. break;
  96. }
  97. if (var) {
  98. this->LocalGenerator->AppendFlags(flags,
  99. this->Makefile->GetDefinition(var));
  100. }
  101. }
  102. std::string cmCommonTargetGenerator::GetFlags(const std::string& l)
  103. {
  104. ByLanguageMap::iterator i = this->FlagsByLanguage.find(l);
  105. if (i == this->FlagsByLanguage.end()) {
  106. std::string flags;
  107. const char* lang = l.c_str();
  108. // Add language feature flags.
  109. this->AddFeatureFlags(flags, lang);
  110. this->LocalGenerator->AddArchitectureFlags(flags, this->GeneratorTarget,
  111. lang, this->ConfigName);
  112. // Fortran-specific flags computed for this target.
  113. if (l == "Fortran") {
  114. this->LocalGenerator->AppendFlags(
  115. flags, this->LocalGenerator->GetTargetFortranFlags(
  116. this->GeneratorTarget, this->ConfigName));
  117. }
  118. this->LocalGenerator->AddCMP0018Flags(flags, this->GeneratorTarget, lang,
  119. this->ConfigName);
  120. this->LocalGenerator->AddVisibilityPresetFlags(
  121. flags, this->GeneratorTarget, lang);
  122. // Append old-style preprocessor definition flags.
  123. this->LocalGenerator->AppendFlags(flags, this->Makefile->GetDefineFlags());
  124. // Add framework directory flags.
  125. this->LocalGenerator->AppendFlags(
  126. flags, this->LocalGenerator->GetFrameworkFlags(l, this->ConfigName,
  127. this->GeneratorTarget));
  128. // Add target-specific flags.
  129. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget, lang,
  130. this->ConfigName);
  131. ByLanguageMap::value_type entry(l, flags);
  132. i = this->FlagsByLanguage.insert(entry).first;
  133. }
  134. return i->second;
  135. }
  136. std::string cmCommonTargetGenerator::GetDefines(const std::string& l)
  137. {
  138. ByLanguageMap::iterator i = this->DefinesByLanguage.find(l);
  139. if (i == this->DefinesByLanguage.end()) {
  140. std::set<std::string> defines;
  141. this->LocalGenerator->GetTargetDefines(this->GeneratorTarget,
  142. this->ConfigName, l, defines);
  143. std::string definesString;
  144. this->LocalGenerator->JoinDefines(defines, definesString, l);
  145. ByLanguageMap::value_type entry(l, definesString);
  146. i = this->DefinesByLanguage.insert(entry).first;
  147. }
  148. return i->second;
  149. }
  150. std::string cmCommonTargetGenerator::GetIncludes(std::string const& l)
  151. {
  152. ByLanguageMap::iterator i = this->IncludesByLanguage.find(l);
  153. if (i == this->IncludesByLanguage.end()) {
  154. std::string includes;
  155. this->AddIncludeFlags(includes, l);
  156. ByLanguageMap::value_type entry(l, includes);
  157. i = this->IncludesByLanguage.insert(entry).first;
  158. }
  159. return i->second;
  160. }
  161. std::vector<std::string> cmCommonTargetGenerator::GetLinkedTargetDirectories()
  162. const
  163. {
  164. std::vector<std::string> dirs;
  165. std::set<cmGeneratorTarget const*> emitted;
  166. if (cmComputeLinkInformation* cli =
  167. this->GeneratorTarget->GetLinkInformation(this->ConfigName)) {
  168. cmComputeLinkInformation::ItemVector const& items = cli->GetItems();
  169. for (cmComputeLinkInformation::ItemVector::const_iterator i =
  170. items.begin();
  171. i != items.end(); ++i) {
  172. cmGeneratorTarget const* linkee = i->Target;
  173. if (linkee && !linkee->IsImported()
  174. // We can ignore the INTERFACE_LIBRARY items because
  175. // Target->GetLinkInformation already processed their
  176. // link interface and they don't have any output themselves.
  177. && linkee->GetType() != cmState::INTERFACE_LIBRARY &&
  178. emitted.insert(linkee).second) {
  179. cmLocalGenerator* lg = linkee->GetLocalGenerator();
  180. std::string di = lg->GetCurrentBinaryDirectory();
  181. di += "/";
  182. di += lg->GetTargetDirectory(linkee);
  183. dirs.push_back(di);
  184. }
  185. }
  186. }
  187. return dirs;
  188. }
  189. std::string cmCommonTargetGenerator::GetManifests()
  190. {
  191. std::vector<cmSourceFile const*> manifest_srcs;
  192. this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName);
  193. std::vector<std::string> manifests;
  194. for (std::vector<cmSourceFile const*>::iterator mi = manifest_srcs.begin();
  195. mi != manifest_srcs.end(); ++mi) {
  196. manifests.push_back(this->Convert(
  197. (*mi)->GetFullPath(), this->LocalGenerator->GetWorkingDirectory(),
  198. cmOutputConverter::SHELL));
  199. }
  200. return cmJoin(manifests, " ");
  201. }
  202. void cmCommonTargetGenerator::AppendOSXVerFlag(std::string& flags,
  203. const std::string& lang,
  204. const char* name, bool so)
  205. {
  206. // Lookup the flag to specify the version.
  207. std::string fvar = "CMAKE_";
  208. fvar += lang;
  209. fvar += "_OSX_";
  210. fvar += name;
  211. fvar += "_VERSION_FLAG";
  212. const char* flag = this->Makefile->GetDefinition(fvar);
  213. // Skip if no such flag.
  214. if (!flag) {
  215. return;
  216. }
  217. // Lookup the target version information.
  218. int major;
  219. int minor;
  220. int patch;
  221. this->GeneratorTarget->GetTargetVersion(so, major, minor, patch);
  222. if (major > 0 || minor > 0 || patch > 0) {
  223. // Append the flag since a non-zero version is specified.
  224. std::ostringstream vflag;
  225. vflag << flag << major << "." << minor << "." << patch;
  226. this->LocalGenerator->AppendFlags(flags, vflag.str());
  227. }
  228. }