cmLocalXCodeGenerator.cxx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmLocalXCodeGenerator.h"
  4. #include <memory>
  5. #include <ostream>
  6. #include <utility>
  7. #include "cmGeneratorExpression.h"
  8. #include "cmGeneratorTarget.h"
  9. #include "cmGlobalXCodeGenerator.h"
  10. #include "cmMakefile.h"
  11. #include "cmSourceFile.h"
  12. #include "cmStringAlgorithms.h"
  13. #include "cmSystemTools.h"
  14. class cmGlobalGenerator;
  15. cmLocalXCodeGenerator::cmLocalXCodeGenerator(cmGlobalGenerator* gg,
  16. cmMakefile* mf)
  17. : cmLocalGenerator(gg, mf)
  18. {
  19. // the global generator does this, so do not
  20. // put these flags into the language flags
  21. this->EmitUniversalBinaryFlags = false;
  22. }
  23. cmLocalXCodeGenerator::~cmLocalXCodeGenerator() = default;
  24. std::string cmLocalXCodeGenerator::GetTargetDirectory(
  25. cmGeneratorTarget const* target) const
  26. {
  27. return cmStrCat(target->GetName(), ".dir");
  28. }
  29. void cmLocalXCodeGenerator::AppendFlagEscape(std::string& flags,
  30. std::string const& rawFlag) const
  31. {
  32. cmGlobalXCodeGenerator const* gg =
  33. static_cast<cmGlobalXCodeGenerator const*>(this->GlobalGenerator);
  34. gg->AppendFlag(flags, rawFlag);
  35. }
  36. void cmLocalXCodeGenerator::Generate()
  37. {
  38. cmLocalGenerator::Generate();
  39. for (auto const& target : this->GetGeneratorTargets()) {
  40. target->HasMacOSXRpathInstallNameDir("");
  41. }
  42. }
  43. void cmLocalXCodeGenerator::AddGeneratorSpecificInstallSetup(std::ostream& os)
  44. {
  45. // First check if we need to warn about incompatible settings
  46. for (auto const& target : this->GetGeneratorTargets()) {
  47. target->HasMacOSXRpathInstallNameDir("");
  48. }
  49. // CMakeIOSInstallCombined.cmake needs to know the location of the top of
  50. // the build directory
  51. os << "set(CMAKE_BINARY_DIR \"" << this->GetBinaryDirectory() << "\")\n\n";
  52. if (this->Makefile->PlatformIsAppleEmbedded()) {
  53. std::string platformName;
  54. switch (this->Makefile->GetAppleSDKType()) {
  55. case cmMakefile::AppleSDK::IPhoneOS:
  56. platformName = "iphoneos";
  57. break;
  58. case cmMakefile::AppleSDK::IPhoneSimulator:
  59. platformName = "iphonesimulator";
  60. break;
  61. case cmMakefile::AppleSDK::AppleTVOS:
  62. platformName = "appletvos";
  63. break;
  64. case cmMakefile::AppleSDK::AppleTVSimulator:
  65. platformName = "appletvsimulator";
  66. break;
  67. case cmMakefile::AppleSDK::WatchOS:
  68. platformName = "watchos";
  69. break;
  70. case cmMakefile::AppleSDK::WatchSimulator:
  71. platformName = "watchsimulator";
  72. break;
  73. case cmMakefile::AppleSDK::XROS:
  74. platformName = "xros";
  75. break;
  76. case cmMakefile::AppleSDK::XRSimulator:
  77. platformName = "xrsimulator";
  78. break;
  79. case cmMakefile::AppleSDK::MacOS:
  80. break;
  81. }
  82. if (!platformName.empty()) {
  83. // The effective platform name is just the platform name with a hyphen
  84. // prepended. We can get the SUPPORTED_PLATFORMS from the project file
  85. // at runtime, so we don't need to compute that here.
  86. /* clang-format off */
  87. os <<
  88. "if(NOT PLATFORM_NAME)\n"
  89. " if(NOT \"$ENV{PLATFORM_NAME}\" STREQUAL \"\")\n"
  90. " set(PLATFORM_NAME \"$ENV{PLATFORM_NAME}\")\n"
  91. " endif()\n"
  92. " if(NOT PLATFORM_NAME)\n"
  93. " set(PLATFORM_NAME " << platformName << ")\n"
  94. " endif()\n"
  95. "endif()\n\n"
  96. "if(NOT EFFECTIVE_PLATFORM_NAME)\n"
  97. " if(NOT \"$ENV{EFFECTIVE_PLATFORM_NAME}\" STREQUAL \"\")\n"
  98. " set(EFFECTIVE_PLATFORM_NAME \"$ENV{EFFECTIVE_PLATFORM_NAME}\")\n"
  99. " endif()\n"
  100. " if(NOT EFFECTIVE_PLATFORM_NAME)\n"
  101. " set(EFFECTIVE_PLATFORM_NAME -" << platformName << ")\n"
  102. " endif()\n"
  103. "endif()\n\n";
  104. /* clang-format off */
  105. }
  106. }
  107. }
  108. void cmLocalXCodeGenerator::ComputeObjectFilenames(
  109. std::map<cmSourceFile const*, cmObjectLocations>& mapping,
  110. cmGeneratorTarget const*)
  111. {
  112. // Count the number of object files with each name. Warn about duplicate
  113. // names since Xcode names them uniquely automatically with a numeric suffix
  114. // to avoid exact duplicate file names. Note that Mac file names are not
  115. // typically case sensitive, hence the LowerCase.
  116. std::map<std::string, int> counts;
  117. for (auto& si : mapping) {
  118. cmSourceFile const* sf = si.first;
  119. std::string shortObjectName = this->GetShortObjectFileName(*sf);
  120. std::string longObjectName = cmStrCat(
  121. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath()), ".o");
  122. std::string longObjectNameLower = cmSystemTools::LowerCase(longObjectName);
  123. counts[longObjectNameLower] += 1;
  124. if (2 == counts[longObjectNameLower]) {
  125. // TODO: emit warning about duplicate name?
  126. }
  127. si.second.ShortLoc.emplace(shortObjectName);
  128. si.second.LongLoc.Update(longObjectName);
  129. }
  130. }
  131. void cmLocalXCodeGenerator::AddXCConfigSources(cmGeneratorTarget* target)
  132. {
  133. auto xcconfig = target->GetProperty("XCODE_XCCONFIG");
  134. if (!xcconfig) {
  135. return;
  136. }
  137. auto configs = target->Makefile->GetGeneratorConfigs(
  138. cmMakefile::IncludeEmptyConfig);
  139. for (auto& config : configs) {
  140. auto file = cmGeneratorExpression::Evaluate(
  141. *xcconfig,
  142. this, config);
  143. if (!file.empty()) {
  144. target->AddSource(file);
  145. }
  146. }
  147. }