Skip to content

Commit fd28746

Browse files
carlocabBo98
andcommitted
llvm: also write *-apple-macosx* config files
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]>
1 parent 2f6a1fb commit fd28746

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

Formula/l/llvm.rb

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ class Llvm < Formula
3232
sha256 cellar: :any_skip_relocation, x86_64_linux: "479d06278bca8d5a7b8863f003ca127641ffef9c734976eee34fe8c0cc01d763"
3333
end
3434

35-
# Clang cannot find system headers if Xcode CLT is not installed
36-
pour_bottle? only_if: :clt_installed
37-
3835
keg_only :provided_by_macos
3936

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

451448
# Write config files for each macOS major version so that this works across OS upgrades.
452-
# TODO: replace this with a call to `MacOSVersion.kernel_major_version` once this is in a release tag:
453-
# https://github.com/Homebrew/brew/pull/18674
454-
{
455-
11 => 20,
456-
12 => 21,
457-
13 => 22,
458-
14 => 23,
459-
15 => 24,
460-
}.each do |macos_version, kernel_version|
461-
write_config_files(macos_version, kernel_version, Hardware::CPU.arch)
449+
MacOSVersion::SYMBOLS.each_value do |v|
450+
macos_version = MacOSVersion.new(v)
451+
write_config_files(macos_version, MacOSVersion.kernel_major_version(macos_version), Hardware::CPU.arch)
462452
end
463453

464454
# Also write an unversioned config file as fallback
@@ -503,20 +493,37 @@ def write_config_files(macos_version, kernel_version, arch)
503493

504494
arches = Set.new([:arm64, :x86_64])
505495
arches << arch
496+
sysroot = if macos_version >= "10.14" || (macos_version.blank? && kernel_version.blank?)
497+
"#{MacOS::CLT::PKG_PATH}/SDKs/MacOSX#{macos_version}.sdk"
498+
else
499+
"/"
500+
end
506501

507-
arches.each do |target_arch|
508-
target_triple = "#{target_arch}-apple-darwin#{kernel_version}"
509-
(clang_config_file_dir/"#{target_triple}.cfg").atomic_write <<~CONFIG
510-
--sysroot=#{MacOS::CLT::PKG_PATH}/SDKs/MacOSX#{macos_version}.sdk
511-
CONFIG
502+
{
503+
darwin: kernel_version,
504+
macosx: macos_version,
505+
}.each do |system, version|
506+
arches.each do |target_arch|
507+
config_file = "#{target_arch}-apple-#{system}#{version}.cfg"
508+
(clang_config_file_dir/config_file).atomic_write <<~CONFIG
509+
--sysroot=#{sysroot}
510+
CONFIG
511+
end
512512
end
513513
end
514514

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

519-
write_config_files(MacOS.version.major, OS.kernel_version.major, Hardware::CPU.arch)
518+
config_files = {
519+
darwin: OS.kernel_version.major,
520+
macosx: MacOS.version,
521+
}.map do |system, version|
522+
clang_config_file_dir/"#{Hardware::CPU.arch}-apple-#{system}#{version}.cfg"
523+
end
524+
return if config_files.all?(&:exist?)
525+
526+
write_config_files(MacOS.version, OS.kernel_version.major, Hardware::CPU.arch)
520527
end
521528

522529
def caveats
@@ -534,6 +541,10 @@ def caveats
534541
on_macos do
535542
s += <<~EOS
536543
544+
Using `clang`, `clang++`, etc., requires a CLT installation at `/Library/Developer/CommandLineTools`.
545+
If you don't want to install the CLT, you can write appropriate configuration files pointing to your
546+
SDK at ~/.config/clang.
547+
537548
To use the bundled libunwind please use the following LDFLAGS:
538549
LDFLAGS="-L#{opt_lib}/unwind -lunwind"
539550
@@ -618,6 +629,16 @@ def caveats
618629
assert_equal "Hello World!", shell_output("./testCLT++").chomp
619630
system bin/"clang", "-v", "test.c", "-o", "testCLT"
620631
assert_equal "Hello World!", shell_output("./testCLT").chomp
632+
633+
target = "#{Hardware::CPU.arch}-apple-macosx#{MacOS.full_version}"
634+
system bin/"clang-cpp", "-v", "--target=#{target}", "test.c"
635+
system bin/"clang-cpp", "-v", "--target=#{target}", "test.cpp"
636+
637+
system bin/"clang", "-v", "--target=#{target}", "test.c", "-o", "test-macosx"
638+
assert_equal "Hello World!", shell_output("./test-macosx").chomp
639+
640+
system bin/"clang++", "-v", "--target=#{target}", "-std=c++11", "test.cpp", "-o", "test++-macosx"
641+
assert_equal "Hello World!", shell_output("./test++-macosx").chomp
621642
end
622643

623644
# Testing Xcode

0 commit comments

Comments
 (0)