cmCommonTargetGenerator.cxx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 = CM_NULLPTR;
  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. this->LocalGenerator->GetTargetCompileFlags(this->GeneratorTarget,
  108. this->ConfigName, l, flags);
  109. ByLanguageMap::value_type entry(l, flags);
  110. i = this->FlagsByLanguage.insert(entry).first;
  111. }
  112. return i->second;
  113. }
  114. std::string cmCommonTargetGenerator::GetDefines(const std::string& l)
  115. {
  116. ByLanguageMap::iterator i = this->DefinesByLanguage.find(l);
  117. if (i == this->DefinesByLanguage.end()) {
  118. std::set<std::string> defines;
  119. this->LocalGenerator->GetTargetDefines(this->GeneratorTarget,
  120. this->ConfigName, l, defines);
  121. std::string definesString;
  122. this->LocalGenerator->JoinDefines(defines, definesString, l);
  123. ByLanguageMap::value_type entry(l, definesString);
  124. i = this->DefinesByLanguage.insert(entry).first;
  125. }
  126. return i->second;
  127. }
  128. std::string cmCommonTargetGenerator::GetIncludes(std::string const& l)
  129. {
  130. ByLanguageMap::iterator i = this->IncludesByLanguage.find(l);
  131. if (i == this->IncludesByLanguage.end()) {
  132. std::string includes;
  133. this->AddIncludeFlags(includes, l);
  134. ByLanguageMap::value_type entry(l, includes);
  135. i = this->IncludesByLanguage.insert(entry).first;
  136. }
  137. return i->second;
  138. }
  139. std::vector<std::string> cmCommonTargetGenerator::GetLinkedTargetDirectories()
  140. const
  141. {
  142. std::vector<std::string> dirs;
  143. std::set<cmGeneratorTarget const*> emitted;
  144. if (cmComputeLinkInformation* cli =
  145. this->GeneratorTarget->GetLinkInformation(this->ConfigName)) {
  146. cmComputeLinkInformation::ItemVector const& items = cli->GetItems();
  147. for (cmComputeLinkInformation::ItemVector::const_iterator i =
  148. items.begin();
  149. i != items.end(); ++i) {
  150. cmGeneratorTarget const* linkee = i->Target;
  151. if (linkee && !linkee->IsImported()
  152. // We can ignore the INTERFACE_LIBRARY items because
  153. // Target->GetLinkInformation already processed their
  154. // link interface and they don't have any output themselves.
  155. && linkee->GetType() != cmState::INTERFACE_LIBRARY &&
  156. emitted.insert(linkee).second) {
  157. cmLocalGenerator* lg = linkee->GetLocalGenerator();
  158. std::string di = lg->GetCurrentBinaryDirectory();
  159. di += "/";
  160. di += lg->GetTargetDirectory(linkee);
  161. dirs.push_back(di);
  162. }
  163. }
  164. }
  165. return dirs;
  166. }
  167. std::string cmCommonTargetGenerator::GetManifests()
  168. {
  169. std::vector<cmSourceFile const*> manifest_srcs;
  170. this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName);
  171. std::vector<std::string> manifests;
  172. for (std::vector<cmSourceFile const*>::iterator mi = manifest_srcs.begin();
  173. mi != manifest_srcs.end(); ++mi) {
  174. manifests.push_back(this->Convert(
  175. (*mi)->GetFullPath(), this->LocalGenerator->GetWorkingDirectory(),
  176. cmOutputConverter::SHELL));
  177. }
  178. return cmJoin(manifests, " ");
  179. }
  180. void cmCommonTargetGenerator::AppendOSXVerFlag(std::string& flags,
  181. const std::string& lang,
  182. const char* name, bool so)
  183. {
  184. // Lookup the flag to specify the version.
  185. std::string fvar = "CMAKE_";
  186. fvar += lang;
  187. fvar += "_OSX_";
  188. fvar += name;
  189. fvar += "_VERSION_FLAG";
  190. const char* flag = this->Makefile->GetDefinition(fvar);
  191. // Skip if no such flag.
  192. if (!flag) {
  193. return;
  194. }
  195. // Lookup the target version information.
  196. int major;
  197. int minor;
  198. int patch;
  199. this->GeneratorTarget->GetTargetVersion(so, major, minor, patch);
  200. if (major > 0 || minor > 0 || patch > 0) {
  201. // Append the flag since a non-zero version is specified.
  202. std::ostringstream vflag;
  203. vflag << flag << major << "." << minor << "." << patch;
  204. this->LocalGenerator->AppendFlags(flags, vflag.str());
  205. }
  206. }