Skip to content

Commit

Permalink
llvm: also write *-apple-macosx* config files
Browse files Browse the repository at this point in the history
Fixes #197278.

Also, remove the CLT requirement for pouring bottles. Many users use
this for the libraries, which don't need the CLT. We can add it back if
users report getting tripped up by this.

Co-authored-by: Bo Anderson <[email protected]>
  • Loading branch information
carlocab and Bo98 committed Nov 12, 2024
1 parent 2f6a1fb commit d853ae9
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions Formula/l/llvm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ class Llvm < Formula
sha256 cellar: :any_skip_relocation, x86_64_linux: "479d06278bca8d5a7b8863f003ca127641ffef9c734976eee34fe8c0cc01d763"
end

# Clang cannot find system headers if Xcode CLT is not installed
pour_bottle? only_if: :clt_installed

keg_only :provided_by_macos

# https://llvm.org/docs/GettingStarted.html#requirement
Expand Down Expand Up @@ -449,16 +446,11 @@ def install
xctoolchain.parent.install_symlink xctoolchain.basename.to_s => "LLVM#{soversion}.xctoolchain"

# Write config files for each macOS major version so that this works across OS upgrades.
# TODO: replace this with a call to `MacOSVersion.kernel_major_version` once this is in a release tag:
# https://github.com/Homebrew/brew/pull/18674
{
11 => 20,
12 => 21,
13 => 22,
14 => 23,
15 => 24,
}.each do |macos_version, kernel_version|
write_config_files(macos_version, kernel_version, Hardware::CPU.arch)
MacOSVersion::SYMBOLS.each_key do |v|
macos_version = MacOSVersion.new(v)
next if macos_version < "10.14" # system headers are in `/usr` before this.

write_config_files(macos_version, MacOSVersion.kernel_major_version(macos_version), Hardware::CPU.arch)
end

# Also write an unversioned config file as fallback
Expand Down Expand Up @@ -504,19 +496,29 @@ def write_config_files(macos_version, kernel_version, arch)
arches = Set.new([:arm64, :x86_64])
arches << arch

systems = {
darwin: kernel_version,
macosx: macos_version,
}
arches.each do |target_arch|
target_triple = "#{target_arch}-apple-darwin#{kernel_version}"
(clang_config_file_dir/"#{target_triple}.cfg").atomic_write <<~CONFIG
--sysroot=#{MacOS::CLT::PKG_PATH}/SDKs/MacOSX#{macos_version}.sdk
CONFIG
systems.each do |system, version|
config_file = "#{target_arch}-apple-#{system}#{version}.cfg"
(clang_config_file_dir/config_file).atomic_write <<~CONFIG
--sysroot=#{MacOS::CLT::PKG_PATH}/SDKs/MacOSX#{macos_version}.sdk
CONFIG
end
end
end

def post_install
return unless OS.mac?
return if (clang_config_file_dir/"#{Hardware::CPU.arch}-apple-darwin#{OS.kernel_version.major}.cfg").exist?

write_config_files(MacOS.version.major, OS.kernel_version.major, Hardware::CPU.arch)
config_files = %w[darwin macosx].map do |system|
clang_config_file_dir/"#{Hardware::CPU.arch}-apple-#{system}#{OS.kernel_version.major}.cfg"
end
return if config_files.all?(&:exist?)

write_config_files(MacOS.version, OS.kernel_version.major, Hardware::CPU.arch)
end

def caveats
Expand All @@ -534,6 +536,10 @@ def caveats
on_macos do
s += <<~EOS
Using `clang`, `clang++`, etc., requires a CLT installation at `/Library/Developer/CommandLineTools`.
If you don't want to install the CLT, you can write appropriate configuration files pointing to your
SDK at ~/.config/clang.
To use the bundled libunwind please use the following LDFLAGS:
LDFLAGS="-L#{opt_lib}/unwind -lunwind"
Expand Down Expand Up @@ -618,6 +624,16 @@ def caveats
assert_equal "Hello World!", shell_output("./testCLT++").chomp
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
end

# Testing Xcode
Expand Down

0 comments on commit d853ae9

Please sign in to comment.