From 45fa9b32caae2a45adf4e7eec8e69fc0a149a4ff Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 8 Apr 2020 11:28:30 -0400 Subject: Apple: Improve handling of missing SDKs in CMAKE_APPLE_ARCH_SYSROOTS Use `-SDK-NOTFOUND` instead of an empty string as a placeholder in `CMAKE_APPLE_ARCH_SYSROOTS` for architectures whose SDK is not found. This ensures the length of `CMAKE_APPLE_ARCH_SYSROOTS` matches the length of `CMAKE_OSX_ARCHITECTURES`. It also makes the missing SDKs more visible in the value. Issue: #20534 --- Modules/Platform/Darwin-Initialize.cmake | 4 ++-- Source/cmLocalGenerator.cxx | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Modules/Platform/Darwin-Initialize.cmake b/Modules/Platform/Darwin-Initialize.cmake index 729217c..80e668e 100644 --- a/Modules/Platform/Darwin-Initialize.cmake +++ b/Modules/Platform/Darwin-Initialize.cmake @@ -136,7 +136,7 @@ endfunction() # Handle multi-arch sysroots. Do this before CMAKE_OSX_SYSROOT is # transformed into a path, so that we know the sysroot name. function(_apple_resolve_multi_arch_sysroots) - if(CMAKE_APPLE_ARCH_SYSROOTS) + if(DEFINED CMAKE_APPLE_ARCH_SYSROOTS) return() # Already cached endif() @@ -202,7 +202,7 @@ function(_apple_resolve_multi_arch_sysroots) list(APPEND _arch_sysroots ${_arch_sysroot}) else() message(WARNING "No SDK found for architecture '${arch}'") - list(APPEND _arch_sysroots "") # Placeholder + list(APPEND _arch_sysroots "${arch}-SDK-NOTFOUND") endif() endforeach() diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index 00ad62e..62a5e0c 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -1800,7 +1800,7 @@ void cmLocalGenerator::AddArchitectureFlags(std::string& flags, if (!arch_sysroots.empty()) { assert(arch_sysroots.size() == archs.size()); for (size_t i = 0; i < archs.size(); ++i) { - if (arch_sysroots[i].empty()) { + if (cmIsOff(arch_sysroots[i])) { continue; } flags += " -Xarch_" + archs[i] + " "; -- cgit v0.12 From 84a1e67380207bafaceefe1c19b47513c0fb63b2 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 8 Apr 2020 11:35:32 -0400 Subject: Apple: Fix mapping CMAKE_APPLE_ARCH_SYSROOTS to custom OSX_ARCHITECTURES The `CMAKE_OSX_ARCHITECTURES` value is not used directly by generators. It is used to initialize a per-target `OSX_ARCHITECTURES` property, but that property can also be set explicitly by project code to a subset of the full list of architectures. In order to handle this case, construct a mapping from each `CMAKE_OSX_ARCHITECTURES` entry to the corresponding `CMAKE_APPLE_ARCH_SYSROOTS` entry by name. Use the mapping to find the sysroot for each entry in `OSX_ARCHITECTURES` for a given target. If `CMAKE_APPLE_ARCH_SYSROOTS` does not have the same length as `CMAKE_OSX_ARCHITECTURES`, error out early rather than risking a crash or assertion failure. Fixes: #20534 --- Source/cmLocalGenerator.cxx | 39 ++++++++++++++++++++++++++++----------- Source/cmLocalGenerator.h | 1 + 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index 62a5e0c..2c923dd 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -131,6 +131,28 @@ cmLocalGenerator::cmLocalGenerator(cmGlobalGenerator* gg, cmMakefile* makefile) this->LinkerSysroot = this->Makefile->GetSafeDefinition("CMAKE_SYSROOT"); } + if (std::string const* appleArchSysroots = + this->Makefile->GetDef("CMAKE_APPLE_ARCH_SYSROOTS")) { + std::string const& appleArchs = + this->Makefile->GetSafeDefinition("CMAKE_OSX_ARCHITECTURES"); + std::vector archs; + std::vector sysroots; + cmExpandList(appleArchs, archs); + cmExpandList(*appleArchSysroots, sysroots, true); + if (archs.size() == sysroots.size()) { + for (size_t i = 0; i < archs.size(); ++i) { + this->AppleArchSysroots[archs[i]] = sysroots[i]; + } + } else { + std::string const e = + cmStrCat("CMAKE_APPLE_ARCH_SYSROOTS:\n ", *appleArchSysroots, + "\n" + "is not the same length as CMAKE_OSX_ARCHITECTURES:\n ", + appleArchs); + this->IssueMessage(MessageType::FATAL_ERROR, e); + } + } + for (std::string const& lang : enabledLanguages) { if (lang == "NONE") { continue; @@ -1792,20 +1814,15 @@ void cmLocalGenerator::AddArchitectureFlags(std::string& flags, std::string("CMAKE_") + lang + "_SYSROOT_FLAG"; const char* sysrootFlag = this->Makefile->GetDefinition(sysrootFlagVar); if (sysrootFlag && *sysrootFlag) { - std::vector arch_sysroots; - if (const char* arch_sysroots_str = - this->Makefile->GetDefinition("CMAKE_APPLE_ARCH_SYSROOTS")) { - cmExpandList(std::string(arch_sysroots_str), arch_sysroots, true); - } - if (!arch_sysroots.empty()) { - assert(arch_sysroots.size() == archs.size()); - for (size_t i = 0; i < archs.size(); ++i) { - if (cmIsOff(arch_sysroots[i])) { + if (!this->AppleArchSysroots.empty()) { + for (std::string const& arch : archs) { + std::string const& archSysroot = this->AppleArchSysroots[arch]; + if (cmIsOff(archSysroot)) { continue; } - flags += " -Xarch_" + archs[i] + " "; + flags += " -Xarch_" + arch + " "; // Combine sysroot flag and path to work with -Xarch - std::string arch_sysroot = sysrootFlag + arch_sysroots[i]; + std::string arch_sysroot = sysrootFlag + archSysroot; flags += this->ConvertToOutputFormat(arch_sysroot, SHELL); } } else if (sysroot && *sysroot) { diff --git a/Source/cmLocalGenerator.h b/Source/cmLocalGenerator.h index 88194b7..5377be9 100644 --- a/Source/cmLocalGenerator.h +++ b/Source/cmLocalGenerator.h @@ -513,6 +513,7 @@ protected: std::map VariableMappings; std::string CompilerSysroot; std::string LinkerSysroot; + std::unordered_map AppleArchSysroots; bool EmitUniversalBinaryFlags; -- cgit v0.12