cmLocalXCodeGenerator.cxx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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,
  26. cmStateEnums::IntermediateDirKind /*kind*/) const
  27. {
  28. return cmStrCat(target->GetName(), ".dir");
  29. }
  30. void cmLocalXCodeGenerator::AppendFlagEscape(std::string& flags,
  31. std::string const& rawFlag) const
  32. {
  33. cmGlobalXCodeGenerator const* gg =
  34. static_cast<cmGlobalXCodeGenerator const*>(this->GlobalGenerator);
  35. gg->AppendFlag(flags, rawFlag);
  36. }
  37. void cmLocalXCodeGenerator::Generate()
  38. {
  39. cmLocalGenerator::Generate();
  40. for (auto const& 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 (auto const& 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::XROS:
  75. platformName = "xros";
  76. break;
  77. case cmMakefile::AppleSDK::XRSimulator:
  78. platformName = "xrsimulator";
  79. break;
  80. case cmMakefile::AppleSDK::MacOS:
  81. break;
  82. }
  83. if (!platformName.empty()) {
  84. // The effective platform name is just the platform name with a hyphen
  85. // prepended. We can get the SUPPORTED_PLATFORMS from the project file
  86. // at runtime, so we don't need to compute that here.
  87. /* clang-format off */
  88. os <<
  89. "if(NOT PLATFORM_NAME)\n"
  90. " if(NOT \"$ENV{PLATFORM_NAME}\" STREQUAL \"\")\n"
  91. " set(PLATFORM_NAME \"$ENV{PLATFORM_NAME}\")\n"
  92. " endif()\n"
  93. " if(NOT PLATFORM_NAME)\n"
  94. " set(PLATFORM_NAME " << platformName << ")\n"
  95. " endif()\n"
  96. "endif()\n\n"
  97. "if(NOT EFFECTIVE_PLATFORM_NAME)\n"
  98. " if(NOT \"$ENV{EFFECTIVE_PLATFORM_NAME}\" STREQUAL \"\")\n"
  99. " set(EFFECTIVE_PLATFORM_NAME \"$ENV{EFFECTIVE_PLATFORM_NAME}\")\n"
  100. " endif()\n"
  101. " if(NOT EFFECTIVE_PLATFORM_NAME)\n"
  102. " set(EFFECTIVE_PLATFORM_NAME -" << platformName << ")\n"
  103. " endif()\n"
  104. "endif()\n\n";
  105. /* clang-format off */
  106. }
  107. }
  108. }
  109. void cmLocalXCodeGenerator::ComputeObjectFilenames(
  110. std::map<cmSourceFile const*, cmObjectLocations>& mapping,
  111. std::string const& config,
  112. cmGeneratorTarget const*)
  113. {
  114. // Count the number of object files with each name. Warn about duplicate
  115. // names since Xcode names them uniquely automatically with a numeric suffix
  116. // to avoid exact duplicate file names. Note that Mac file names are not
  117. // typically case sensitive, hence the LowerCase.
  118. std::map<std::string, int> counts;
  119. for (auto& si : mapping) {
  120. cmSourceFile const* sf = si.first;
  121. std::string shortObjectName = this->GetShortObjectFileName(*sf);
  122. std::string longObjectName = cmStrCat(
  123. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath()), ".o");
  124. std::string longObjectNameLower = cmSystemTools::LowerCase(longObjectName);
  125. counts[longObjectNameLower] += 1;
  126. if (2 == counts[longObjectNameLower]) {
  127. // TODO: emit warning about duplicate name?
  128. }
  129. si.second.ShortLoc.emplace(shortObjectName);
  130. si.second.LongLoc.Update(longObjectName);
  131. this->FillCustomInstallObjectLocations(*sf, config, ".o", si.second.InstallLongLoc);
  132. }
  133. }
  134. void cmLocalXCodeGenerator::AddXCConfigSources(cmGeneratorTarget* target)
  135. {
  136. auto xcconfig = target->GetProperty("XCODE_XCCONFIG");
  137. if (!xcconfig) {
  138. return;
  139. }
  140. auto configs = target->Makefile->GetGeneratorConfigs(
  141. cmMakefile::IncludeEmptyConfig);
  142. for (auto& config : configs) {
  143. auto file = cmGeneratorExpression::Evaluate(
  144. *xcconfig,
  145. this, config);
  146. if (!file.empty()) {
  147. auto* xcconfig_sf = target->AddSource(file);
  148. xcconfig_sf->SetSpecialSourceType(cmSourceFile::SpecialSourceType::XcodeXCConfigFile);
  149. }
  150. }
  151. }