Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

llvm lld flang wasi-runtimes 19.1.3 #196094

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Formula/f/flang.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Flang < Formula
desc "LLVM Fortran Frontend"
homepage "https://flang.llvm.org/"
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-19.1.2/llvm-project-19.1.2.src.tar.xz"
sha256 "3666f01fc52d8a0b0da83e107d74f208f001717824be0b80007f529453aa1e19"
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-19.1.3/llvm-project-19.1.3.src.tar.xz"
sha256 "324d483ff0b714c8ce7819a1b679dd9e4706cf91c6caf7336dc4ac0c1d3bf636"
# The LLVM Project is under the Apache License v2.0 with LLVM Exceptions
license "Apache-2.0" => { with: "LLVM-exception" }
head "https://github.com/llvm/llvm-project.git", branch: "main"
Expand Down
4 changes: 2 additions & 2 deletions Formula/l/lld.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Lld < Formula
desc "LLVM Project Linker"
homepage "https://lld.llvm.org/"
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-19.1.2/llvm-project-19.1.2.src.tar.xz"
sha256 "3666f01fc52d8a0b0da83e107d74f208f001717824be0b80007f529453aa1e19"
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-19.1.3/llvm-project-19.1.3.src.tar.xz"
sha256 "324d483ff0b714c8ce7819a1b679dd9e4706cf91c6caf7336dc4ac0c1d3bf636"
# The LLVM Project is under the Apache License v2.0 with LLVM Exceptions
license "Apache-2.0" => { with: "LLVM-exception" }
head "https://github.com/llvm/llvm-project.git", branch: "main"
Expand Down
88 changes: 85 additions & 3 deletions Formula/l/llvm.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
class Llvm < Formula
desc "Next-gen compiler infrastructure"
homepage "https://llvm.org/"
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-19.1.2/llvm-project-19.1.2.src.tar.xz"
sha256 "3666f01fc52d8a0b0da83e107d74f208f001717824be0b80007f529453aa1e19"
# The LLVM Project is under the Apache License v2.0 with LLVM Exceptions
license "Apache-2.0" => { with: "LLVM-exception" }
head "https://github.com/llvm/llvm-project.git", branch: "main"

stable do
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-19.1.3/llvm-project-19.1.3.src.tar.xz"
sha256 "324d483ff0b714c8ce7819a1b679dd9e4706cf91c6caf7336dc4ac0c1d3bf636"

# Backport relative `CLANG_CONFIG_FILE_SYSTEM_DIR` patch.
# Remove in LLVM 20.
# https://github.com/llvm/llvm-project/pull/110962
patch do
url "https://github.com/llvm/llvm-project/commit/1682c99a8877364f1d847395cef501e813804caa.patch?full_index=1"
sha256 "2d0a185e27ff2bc46531fc2c18c61ffab521ae8ece2db5b5bed498a15f3f3758"
end
end

livecheck do
url :stable
regex(/^llvmorg[._-]v?(\d+(?:\.\d+)+)$/i)
Expand Down Expand Up @@ -49,10 +60,28 @@ class Llvm < Formula
# Fails at building LLDB
fails_with gcc: "5"

# Support simplified triples in version config files.
# https://github.com/llvm/llvm-project/pull/111387
patch do
url "https://github.com/llvm/llvm-project/commit/88dd0d33147a7f46a3c9df4aed28ad4e47ef597c.patch?full_index=1"
sha256 "0acaa80042055ad194306abb9843a94da24f53ee2bb819583d624391a6329b90"
end

# Fix triple config loading for clang-cl
# https://github.com/llvm/llvm-project/pull/111397
patch do
url "https://github.com/llvm/llvm-project/commit/a3e8b860788934d7cc1489f850f00dcfd9d8b595.patch?full_index=1"
sha256 "6d8403fec7be55004e94de90b074c2c166811903ad4921fd76274498c5a60a23"
end

def python3
"python3.13"
end

def clang_config_file_dir
etc/"clang"
end

def install
# The clang bindings need a little help finding our libclang.
inreplace "clang/bindings/python/clang/cindex.py",
Expand Down Expand Up @@ -116,6 +145,8 @@ def install
-DLLVM_ENABLE_Z3_SOLVER=#{versioned_formula? ? "OFF" : "ON"}
-DLLVM_OPTIMIZED_TABLEGEN=ON
-DLLVM_TARGETS_TO_BUILD=all
-DLLVM_USE_RELATIVE_PATHS_IN_FILES=ON
-DLLVM_SOURCE_PREFIX=.
-DLLDB_USE_SYSTEM_DEBUGSERVER=ON
-DLLDB_ENABLE_PYTHON=ON
-DLLDB_ENABLE_LUA=OFF
Expand All @@ -126,6 +157,8 @@ def install
-DCLANG_PYTHON_BINDINGS_VERSIONS=#{python_versions.join(";")}
-DLLVM_CREATE_XCODE_TOOLCHAIN=OFF
-DCLANG_FORCE_MATCHING_LIBCLANG_SOVERSION=OFF
-DCLANG_CONFIG_FILE_SYSTEM_DIR=#{clang_config_file_dir.relative_path_from(bin)}
-DCLANG_CONFIG_FILE_USER_DIR=~/.config/clang
]

if tap.present?
Expand Down Expand Up @@ -156,7 +189,6 @@ def install
args << "-DLIBCXX_INSTALL_LIBRARY_DIR=#{libcxx_install_libdir}"
args << "-DLIBUNWIND_INSTALL_LIBRARY_DIR=#{libunwind_install_libdir}"
args << "-DLIBCXXABI_INSTALL_LIBRARY_DIR=#{libcxx_install_libdir}"
args << "-DDEFAULT_SYSROOT=#{macos_sdk}" if macos_sdk
runtimes_cmake_args << "-DCMAKE_INSTALL_RPATH=#{libcxx_rpaths.join("|")}"

# Disable builds for OSes not supported by the CLT SDK.
Expand Down Expand Up @@ -412,6 +444,25 @@ def install
system "/usr/libexec/PlistBuddy", "-c", "Add:CompatibilityVersion integer 2", "Info.plist"
xctoolchain.install "Info.plist"
(xctoolchain/"usr").install_symlink [bin, include, lib, libexec, share]

# Install a major-versioned symlink that can be used across minor/patch version upgrades.
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)
end
Comment on lines +454 to +462
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we already know this formula doesn't work on 10.14 and 10.15? Also would be good to iterate over https://github.com/Homebrew/brew/blob/master/Library/Homebrew/macos_version.rb#L21-L32 automatically.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we already know this formula doesn't work on 10.14 and 10.15?

No, but 10.14 doesn't need a config file IIRC.

Also would be good to iterate over Homebrew/brew@master/Library/Homebrew/macos_version.rb#L21-L32 automatically.

Yes, but that needs Homebrew/brew@42dd0ac which isn't in a release tag yet.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, but 10.14 doesn't need a config file IIRC.

10.14 is the first version without /usr/include (unless you install the header pkg but we do not support/require that). https://github.com/Homebrew/brew/blob/f18e1ea86ab32a8d6119f2d1ae0c6dfcdc77395c/Library/Homebrew/os/mac/xcode.rb#L284


# Also write an unversioned config file as fallback
write_config_files("", "", Hardware::CPU.arch)
end

# Install Vim plugins
Expand Down Expand Up @@ -445,8 +496,34 @@ def install
end
end

# We use the extra layer of indirection in `arch` because the FormulaAudit/OnSystemConditionals
# doesn't want to let us use `Hardware::CPU.arch` outside of `install` or `post_install` blocks.
def write_config_files(macos_version, kernel_version, arch)
clang_config_file_dir.mkpath

arches = Set.new([:arm64, :x86_64])
arches << arch

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
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)
end

def caveats
s = <<~EOS
CLANG_CONFIG_FILE_SYSTEM_DIR: #{clang_config_file_dir}
CLANG_CONFIG_FILE_USER_DIR: ~/.config/clang

LLD is now provided in a separate formula:
brew install lld

Expand Down Expand Up @@ -513,6 +590,9 @@ def caveats
}
CPP

system bin/"clang-cpp", "-v", "test.c"
system bin/"clang-cpp", "-v", "test.cpp"

# Testing default toolchain and SDK location.
system bin/"clang++", "-v",
"-std=c++11", "test.cpp", "-o", "test++"
Expand All @@ -528,6 +608,7 @@ def caveats
toolchain_path = "/Library/Developer/CommandLineTools"
cpp_base = (MacOS.version >= :big_sur) ? MacOS::CLT.sdk_path : toolchain_path
system bin/"clang++", "-v",
"--no-default-config",
"-isysroot", MacOS::CLT.sdk_path,
"-isystem", "#{cpp_base}/usr/include/c++/v1",
"-isystem", "#{MacOS::CLT.sdk_path}/usr/include",
Expand All @@ -543,6 +624,7 @@ def caveats
if OS.mac? && MacOS::Xcode.installed?
cpp_base = (MacOS::Xcode.version >= "12.5") ? MacOS::Xcode.sdk_path : MacOS::Xcode.toolchain_path
system bin/"clang++", "-v",
"--no-default-config",
"-isysroot", MacOS::Xcode.sdk_path,
"-isystem", "#{cpp_base}/usr/include/c++/v1",
"-isystem", "#{MacOS::Xcode.sdk_path}/usr/include",
Expand Down
42 changes: 30 additions & 12 deletions Formula/w/wasi-runtimes.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class WasiRuntimes < Formula
desc "Compiler-RT and libc++ runtimes for WASI"
homepage "https://wasi.dev"
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-19.1.2/llvm-project-19.1.2.src.tar.xz"
sha256 "3666f01fc52d8a0b0da83e107d74f208f001717824be0b80007f529453aa1e19"
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-19.1.3/llvm-project-19.1.3.src.tar.xz"
sha256 "324d483ff0b714c8ce7819a1b679dd9e4706cf91c6caf7336dc4ac0c1d3bf636"
license "Apache-2.0" => { with: "LLVM-exception" }
head "https://github.com/llvm/llvm-project.git", branch: "main"

Expand Down Expand Up @@ -53,6 +53,9 @@ def install
-DCMAKE_C_COMPILER_WORKS=ON
-DCMAKE_CXX_COMPILER_WORKS=ON
-DCMAKE_SYSROOT=#{wasi_libc.opt_share}/wasi-sysroot
-DCMAKE_FIND_FRAMEWORK=NEVER
-DCMAKE_VERBOSE_MAKEFILE=ON
-DCMAKE_PROJECT_TOP_LEVEL_INCLUDES=#{HOMEBREW_LIBRARY_PATH}/cmake/trap_fetchcontent_provider.cmake
]
# Compiler flags taken from:
# https://github.com/WebAssembly/wasi-sdk/blob/5e04cd81eb749edb5642537d150ab1ab7aedabe9/cmake/wasi-sdk-sysroot.cmake#L65-L75
Expand All @@ -75,7 +78,10 @@ def install
(pkgshare/"lib").install_symlink "wasi" => "wasip1"
(pkgshare/"lib").install_symlink "wasi" => "wasip2"

clang_resource_dir = Pathname.new(Utils.safe_popen_read(llvm.opt_bin/"clang", "-print-resource-dir").chomp)
clang_resource_dir = Utils.safe_popen_read(llvm.opt_bin/"clang", "-print-resource-dir").chomp
clang_resource_dir.sub! llvm.prefix.realpath, llvm.opt_prefix
clang_resource_dir = Pathname.new(clang_resource_dir)

clang_resource_include_dir = clang_resource_dir/"include"
clang_resource_include_dir.find do |pn|
next unless pn.file?
Expand All @@ -84,16 +90,22 @@ def install
target = pkgshare/relative_path
next if target.exist?

target.parent.install_symlink pn
target.parent.mkpath
ln_s pn, target
end

# FIXME: the build mistakenly concludes our toolchain doesn't support `-fno-exceptions`
# because we have no `wasm-component-ld`. Remove the line below when
# `wasm-component-ld` is merged.
ENV.append_to_cflags "-fno-exceptions"
target_configuration = Hash.new { |h, k| h[k] = {} }

targets.each do |target|
# Configuration taken from:
# https://github.com/WebAssembly/wasi-sdk/blob/5e04cd81eb749edb5642537d150ab1ab7aedabe9/cmake/wasi-sdk-sysroot.cmake#L227-L271
configuration = target_configuration[target]
configuration[:threads] = configuration[:pic] = target.end_with?("-threads") ? "ON" : "OFF"
configuration[:threads] = target.end_with?("-threads") ? "ON" : "OFF"
configuration[:pic] = target.end_with?("-threads") ? "OFF" : "ON"
configuration[:flags] = target.end_with?("-threads") ? ["-pthread"] : []

cflags = ENV.cflags&.split || []
Expand Down Expand Up @@ -130,7 +142,7 @@ def install
-DLIBCXX_ENABLE_FILESYSTEM:BOOL=ON
-DLIBCXX_ENABLE_ABI_LINKER_SCRIPT:BOOL=OFF
-DLIBCXX_CXX_ABI=libcxxabi
-DLIBCXX_CXX_ABI_INCLUDE_PATHS=#{testpath}/libcxxabi/include
-DLIBCXX_CXX_ABI_INCLUDE_PATHS=#{buildpath}/libcxxabi/include
-DLIBCXX_HAS_MUSL_LIBC:BOOL=ON
-DLIBCXX_ABI_VERSION=2
-DLIBCXXABI_ENABLE_EXCEPTIONS:BOOL=OFF
Expand All @@ -157,6 +169,16 @@ def install
system "cmake", "-S", "runtimes", "-B", "runtimes-#{target}", *target_cmake_args, *common_cmake_args
system "cmake", "--build", "runtimes-#{target}"
system "cmake", "--install", "runtimes-#{target}"

triple = Utils.safe_popen_read(llvm.opt_bin/"clang", "--target=#{target}", "--print-target-triple").chomp
config_file = "#{triple}.cfg"

(buildpath/config_file).write <<~CONFIG
--sysroot=#{HOMEBREW_PREFIX}/share/wasi-sysroot
-resource-dir=#{HOMEBREW_PREFIX}/share/wasi-runtimes
CONFIG

(etc/"clang").install config_file
end
(share/"wasi-sysroot/include/c++/v1").mkpath
touch share/"wasi-sysroot/include/c++/v1/.keepme"
Expand Down Expand Up @@ -184,18 +206,14 @@ def install
CPP

clang = Formula["llvm"].opt_bin/"clang"
wasm_args = %W[
--sysroot=#{HOMEBREW_PREFIX}/share/wasi-sysroot
-resource-dir=#{HOMEBREW_PREFIX}/share/wasi-runtimes
]
targets.each do |target|
# FIXME: Needs a working `wasm-component-ld`.
next if target.include?("wasip2")

system clang, "--target=#{target}", *wasm_args, "-v", "test.c", "-o", "test-#{target}"
system clang, "--target=#{target}", "-v", "test.c", "-o", "test-#{target}"
assert_equal "the answer is 42", shell_output("wasmtime #{testpath}/test-#{target}")

system "#{clang}++", "--target=#{target}", *wasm_args, "-v", "test.cc", "-o", "test-cxx-#{target}"
system "#{clang}++", "--target=#{target}", "-v", "test.cc", "-o", "test-cxx-#{target}"
assert_equal "hello from C++ main with cout!", shell_output("wasmtime #{testpath}/test-cxx-#{target}").chomp
end
end
Expand Down
Loading