From f9695327d3c7c98bc75e8ee444de7fa573e1a041 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera Date: Sun, 8 Dec 2024 21:37:57 +0800 Subject: [PATCH] llvm: fix targeting older macOS versions than the host If you try to target a macOS version that is older than the host machine, our config files will point to an SDK that is likely not available on the host.[^1] We can fix this by defaulting to the unversioned SDK in these cases. [^1]: See, for example, https://github.com/Homebrew/homebrew-core/issues/196344#issuecomment-2473088486 --- Formula/l/llvm.rb | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/Formula/l/llvm.rb b/Formula/l/llvm.rb index 5fa8ca9e552ef..3ed311390a117 100644 --- a/Formula/l/llvm.rb +++ b/Formula/l/llvm.rb @@ -490,7 +490,10 @@ def write_config_files(macos_version, kernel_version, arch) arches = Set.new([:arm64, :x86_64, :aarch64]) arches << arch - sysroot = if macos_version >= "10.14" || (macos_version.blank? && kernel_version.blank?) + + sysroot = if macos_version.blank? || (MacOS.version > macos_version && MacOS::CLT.separate_header_package?) + "#{MacOS::CLT::PKG_PATH}/SDKs/MacOSX.sdk" + elsif macos_version >= "10.14" "#{MacOS::CLT::PKG_PATH}/SDKs/MacOSX#{macos_version}.sdk" else "/" @@ -627,15 +630,22 @@ def caveats system bin/"clang", "-v", "test.c", "-o", "testCLT" assert_equal "Hello World!", shell_output("./testCLT").chomp - target = "#{Hardware::CPU.arch}-apple-macosx#{MacOS.full_version}" - system bin/"clang-cpp", "-v", "--target=#{target}", "test.c" - system bin/"clang-cpp", "-v", "--target=#{target}", "test.cpp" - - system bin/"clang", "-v", "--target=#{target}", "test.c", "-o", "test-macosx" - assert_equal "Hello World!", shell_output("./test-macosx").chomp - - system bin/"clang++", "-v", "--target=#{target}", "-std=c++11", "test.cpp", "-o", "test++-macosx" - assert_equal "Hello World!", shell_output("./test++-macosx").chomp + [ + "#{Hardware::CPU.arch}-apple-macosx#{MacOS.full_version}", + "#{Hardware::CPU.arch}-apple-macosx#{HOMEBREW_MACOS_OLDEST_SUPPORTED.to_i - 1}", + "#{Hardware::CPU.arch}-apple-macosx", + "#{Hardware::CPU.arch}-apple-macosx", + "#{Hardware::CPU.arch}-apple-darwin", + ].each do |target| + system bin/"clang-cpp", "-v", "--target=#{target}", "test.c" + system bin/"clang-cpp", "-v", "--target=#{target}", "test.cpp" + + system bin/"clang", "-v", "--target=#{target}", "test.c", "-o", "test-macosx" + assert_equal "Hello World!", shell_output("./test-macosx").chomp + + system bin/"clang++", "-v", "--target=#{target}", "-std=c++11", "test.cpp", "-o", "test++-macosx" + assert_equal "Hello World!", shell_output("./test++-macosx").chomp + end end # Testing Xcode