cmLocalXCodeGenerator.cxx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt 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*) const
  26. {
  27. // No per-target directory for this generator (yet).
  28. return "";
  29. }
  30. void cmLocalXCodeGenerator::AppendFlagEscape(std::string& flags,
  31. const std::string& rawFlag) const
  32. {
  33. const cmGlobalXCodeGenerator* gg =
  34. static_cast<const cmGlobalXCodeGenerator*>(this->GlobalGenerator);
  35. gg->AppendFlag(flags, rawFlag);
  36. }
  37. void cmLocalXCodeGenerator::Generate()
  38. {
  39. cmLocalGenerator::Generate();
  40. for (const auto& target : this->GetGeneratorTargets()) {
  41. target->HasMacOSXRpathInstallNameDir("");
  42. }
  43. }
  44. void cmLocalXCodeGenerator::AddGeneratorSpecificInstallSetup(std::ostream& os)
  45. {
  46. // First check if we need to warn about incompatible settings
  47. for (const auto& target : this->GetGeneratorTargets()) {
  48. target->HasMacOSXRpathInstallNameDir("");
  49. }
  50. // CMakeIOSInstallCombined.cmake needs to know the location of the top of
  51. // the build directory
  52. os << "set(CMAKE_BINARY_DIR \"" << this->GetBinaryDirectory() << "\")\n\n";
  53. if (this->Makefile->PlatformIsAppleEmbedded()) {
  54. std::string platformName;
  55. switch (this->Makefile->GetAppleSDKType()) {
  56. case cmMakefile::AppleSDK::IPhoneOS:
  57. platformName = "iphoneos";
  58. break;
  59. case cmMakefile::AppleSDK::IPhoneSimulator:
  60. platformName = "iphonesimulator";
  61. break;
  62. case cmMakefile::AppleSDK::AppleTVOS:
  63. platformName = "appletvos";
  64. break;
  65. case cmMakefile::AppleSDK::AppleTVSimulator:
  66. platformName = "appletvsimulator";
  67. break;
  68. case cmMakefile::AppleSDK::WatchOS:
  69. platformName = "watchos";
  70. break;
  71. case cmMakefile::AppleSDK::WatchSimulator:
  72. platformName = "watchsimulator";
  73. break;
  74. case cmMakefile::AppleSDK::MacOS:
  75. break;
  76. }
  77. if (!platformName.empty()) {
  78. // The effective platform name is just the platform name with a hyphen
  79. // prepended. We can get the SUPPORTED_PLATFORMS from the project file
  80. // at runtime, so we don't need to compute that here.
  81. /* clang-format off */
  82. os <<
  83. "if(NOT PLATFORM_NAME)\n"
  84. " if(NOT \"$ENV{PLATFORM_NAME}\" STREQUAL \"\")\n"
  85. " set(PLATFORM_NAME \"$ENV{PLATFORM_NAME}\")\n"
  86. " endif()\n"
  87. " if(NOT PLATFORM_NAME)\n"
  88. " set(PLATFORM_NAME " << platformName << ")\n"
  89. " endif()\n"
  90. "endif()\n\n"
  91. "if(NOT EFFECTIVE_PLATFORM_NAME)\n"
  92. " if(NOT \"$ENV{EFFECTIVE_PLATFORM_NAME}\" STREQUAL \"\")\n"
  93. " set(EFFECTIVE_PLATFORM_NAME \"$ENV{EFFECTIVE_PLATFORM_NAME}\")\n"
  94. " endif()\n"
  95. " if(NOT EFFECTIVE_PLATFORM_NAME)\n"
  96. " set(EFFECTIVE_PLATFORM_NAME -" << platformName << ")\n"
  97. " endif()\n"
  98. "endif()\n\n";
  99. /* clang-format off */
  100. }
  101. }
  102. }
  103. void cmLocalXCodeGenerator::ComputeObjectFilenames(
  104. std::map<cmSourceFile const*, std::string>& mapping,
  105. cmGeneratorTarget const*)
  106. {
  107. // Count the number of object files with each name. Warn about duplicate
  108. // names since Xcode names them uniquely automatically with a numeric suffix
  109. // to avoid exact duplicate file names. Note that Mac file names are not
  110. // typically case sensitive, hence the LowerCase.
  111. std::map<std::string, int> counts;
  112. for (auto& si : mapping) {
  113. cmSourceFile const* sf = si.first;
  114. std::string objectName = cmStrCat(
  115. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath()), ".o");
  116. std::string objectNameLower = cmSystemTools::LowerCase(objectName);
  117. counts[objectNameLower] += 1;
  118. if (2 == counts[objectNameLower]) {
  119. // TODO: emit warning about duplicate name?
  120. }
  121. si.second = objectName;
  122. }
  123. }
  124. void cmLocalXCodeGenerator::AddXCConfigSources(cmGeneratorTarget* target)
  125. {
  126. auto xcconfig = target->GetProperty("XCODE_XCCONFIG");
  127. if (!xcconfig) {
  128. return;
  129. }
  130. auto configs = target->Makefile->GetGeneratorConfigs(
  131. cmMakefile::IncludeEmptyConfig);
  132. for (auto& config : configs) {
  133. auto file = cmGeneratorExpression::Evaluate(
  134. xcconfig,
  135. this, config);
  136. if (!file.empty()) {
  137. target->AddSource(file);
  138. }
  139. }
  140. }