homebrew-core/Formula/llvm@12.rb

416 lines
16 KiB
Ruby

class LlvmAT12 < Formula
desc "Next-gen compiler infrastructure"
homepage "https://llvm.org/"
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-12.0.1/llvm-project-12.0.1.src.tar.xz"
sha256 "129cb25cd13677aad951ce5c2deb0fe4afc1e9d98950f53b51bdcfb5a73afa0e"
# The LLVM Project is under the Apache License v2.0 with LLVM Exceptions
license "Apache-2.0" => { with: "LLVM-exception" }
revision 1
bottle do
rebuild 1
sha256 cellar: :any, arm64_ventura: "a450c4527222e41c437ea4986d12d808b26af73bb72cf90ac0e4aab91a72061c"
sha256 cellar: :any, arm64_monterey: "ff433ce6b78be82fae375dc3735c19272a14d7f62f0724e31d5e6fb438716700"
sha256 cellar: :any, arm64_big_sur: "4424885d3b2d3dbfef7a9ceb64e4d6f325f6aa88b66a334f89409c27a6054787"
sha256 cellar: :any, ventura: "e59faa5fc23a597bfc3d9649bfe968bdaa3f753ab125ef4fad40484ebb0a16ca"
sha256 cellar: :any, monterey: "ea1bd6a3e69f76a43f7acb9ca9e2fb4ef14c444cfc854ccba4188e88b6a28c8b"
sha256 cellar: :any, big_sur: "4635d7fab2baae3b65c031c7d9afb107554a9e529347160164c44ae960726699"
sha256 cellar: :any, catalina: "ac5f61a1bc47001ebb52dec1a3278b4fce04a1a579b855245a3aa533cff63ad3"
sha256 cellar: :any_skip_relocation, x86_64_linux: "8786b4ef7bd5d287feda6aa51aedd29a1856116895f1893066b44040e2a81c87"
end
# Clang cannot find system headers if Xcode CLT is not installed
pour_bottle? only_if: :clt_installed
keg_only :versioned_formula
# https://llvm.org/docs/GettingStarted.html#requirement
# We intentionally use Make instead of Ninja.
# See: Homebrew/homebrew-core/issues/35513
depends_on "cmake" => :build
depends_on "python@3.11" => :build
depends_on "swig" => :build
uses_from_macos "libedit"
uses_from_macos "libffi", since: :catalina
uses_from_macos "ncurses"
uses_from_macos "zlib"
on_linux do
depends_on "pkg-config" => :build
depends_on "binutils" # needed for gold
depends_on "elfutils" # openmp requires <gelf.h>
depends_on "glibc" if Formula["glibc"].any_version_installed?
# Apply patches slated for the 12.0.x release stream
# to allow building with GCC 5 and 6. Upstream bug:
# https://bugs.llvm.org/show_bug.cgi?id=50732
patch do
url "https://raw.githubusercontent.com/Homebrew/formula-patches/f0b8ff8b7ad4c2e1d474b214cd615a98e0caa796/llvm/llvm.patch"
sha256 "084adce7711b07d94197a75fb2162b253186b38d612996eeb6e2bc9ce5b1e6e2"
end
end
# Fix crash in clangd when built with GCC <6. Remove in LLVM 13
# https://github.com/clangd/clangd/issues/800
# https://github.com/Homebrew/homebrew-core/issues/84365
patch do
url "https://github.com/llvm/llvm-project/commit/ec1fb9533305e9bd69294ede7e5e7d9befbb2225.patch?full_index=1"
sha256 "b80a5718420c789588f3392366ac15485e43bea8e81adb14424c3cad4afa7315"
end
# Fix parallel builds. Remove in LLVM 13.
# https://reviews.llvm.org/D106305
# https://lists.llvm.org/pipermail/llvm-dev/2021-July/151665.html
patch do
url "https://github.com/llvm/llvm-project/commit/b31080c596246bc26d2493cfd5e07f053cf9541c.patch?full_index=1"
sha256 "b4576303404e68100dc396d2414d6740c5bfd0162979d22152a688d1e7307379"
end
def install
python3 = "python3.11"
projects = %w[
clang
clang-tools-extra
lld
mlir
polly
openmp
]
# LLDB fails to build on arm64 linux:
# NativeRegisterContextLinux_arm.cpp:60:64: error: no matching function for call to
# 'lldb_private::process_linux::NativeRegisterContextLinux::NativeRegisterContextLinux()'
projects << "lldb" if !OS.linux? || !Hardware::CPU.arm?
runtimes = %w[
compiler-rt
libcxx
libcxxabi
libunwind
]
py_ver = Language::Python.major_minor_version(python3)
site_packages = Language::Python.site_packages(python3).delete_prefix("lib/")
# Apple's libstdc++ is too old to build LLVM
ENV.libcxx if ENV.compiler == :clang
# compiler-rt has some iOS simulator features that require i386 symbols
# I'm assuming the rest of clang needs support too for 32-bit compilation
# to work correctly, but if not, perhaps universal binaries could be
# limited to compiler-rt. llvm makes this somewhat easier because compiler-rt
# can almost be treated as an entirely different build from llvm.
ENV.permit_arch_flags
args = %W[
-DLLVM_ENABLE_PROJECTS=#{projects.join(";")}
-DLLVM_ENABLE_RUNTIMES=#{runtimes.join(";")}
-DLLVM_POLLY_LINK_INTO_TOOLS=ON
-DLLVM_BUILD_EXTERNAL_COMPILER_RT=ON
-DLLVM_LINK_LLVM_DYLIB=ON
-DLLVM_ENABLE_EH=ON
-DLLVM_ENABLE_FFI=ON
-DLLVM_ENABLE_RTTI=ON
-DLLVM_INCLUDE_DOCS=OFF
-DLLVM_INCLUDE_TESTS=OFF
-DLLVM_INSTALL_UTILS=ON
-DLLVM_ENABLE_Z3_SOLVER=OFF
-DLLVM_OPTIMIZED_TABLEGEN=ON
-DLLVM_TARGETS_TO_BUILD=all
-DLLDB_USE_SYSTEM_DEBUGSERVER=ON
-DLLDB_ENABLE_PYTHON=OFF
-DLLDB_ENABLE_LUA=OFF
-DLLDB_ENABLE_LZMA=OFF
-DLIBOMP_INSTALL_ALIASES=OFF
-DCLANG_PYTHON_BINDINGS_VERSIONS=#{py_ver}
-DLLVM_CREATE_XCODE_TOOLCHAIN=#{MacOS::Xcode.installed? ? "ON" : "OFF"}
-DPACKAGE_VENDOR=#{tap.user}
-DBUG_REPORT_URL=#{tap.issues_url}
-DCLANG_VENDOR_UTI=org.#{tap.user.downcase}.clang
]
macos_sdk = MacOS.sdk_path_if_needed
if MacOS.version >= :catalina
args << "-DFFI_INCLUDE_DIR=#{macos_sdk}/usr/include/ffi"
args << "-DFFI_LIBRARY_DIR=#{macos_sdk}/usr/lib"
else
args << "-DFFI_INCLUDE_DIR=#{Formula["libffi"].opt_include}"
args << "-DFFI_LIBRARY_DIR=#{Formula["libffi"].opt_lib}"
end
# gcc-5 fails at building compiler-rt. Enable PGO
# build on Linux when we switch to Ubuntu 18.04.
if OS.mac?
args << "-DLLVM_BUILD_LLVM_C_DYLIB=ON"
args << "-DLLVM_ENABLE_LIBCXX=ON"
args << "-DRUNTIMES_CMAKE_ARGS=-DCMAKE_INSTALL_RPATH=#{rpath}"
args << "-DDEFAULT_SYSROOT=#{macos_sdk}" if macos_sdk
else
ENV.append_to_cflags "-fpermissive -Wno-free-nonheap-object"
# Disable `libxml2`, which isn't very useful.
args << "-DLLVM_ENABLE_LIBXML2=OFF"
args << "-DLLVM_ENABLE_LIBCXX=OFF"
args << "-DCLANG_DEFAULT_CXX_STDLIB=libstdc++"
# Enable llvm gold plugin for LTO
args << "-DLLVM_BINUTILS_INCDIR=#{Formula["binutils"].opt_include}"
# Parts of Polly fail to correctly build with PIC when being used for DSOs.
args << "-DCMAKE_POSITION_INDEPENDENT_CODE=ON"
runtime_args = %w[
-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
-DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON
-DLIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY=OFF
-DLIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY=ON
-DLIBCXX_USE_COMPILER_RT=ON
-DLIBCXX_HAS_ATOMIC_LIB=OFF
-DLIBCXXABI_ENABLE_STATIC_UNWINDER=ON
-DLIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY=OFF
-DLIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY=ON
-DLIBCXXABI_USE_COMPILER_RT=ON
-DLIBCXXABI_USE_LLVM_UNWINDER=ON
-DLIBUNWIND_USE_COMPILER_RT=ON
]
args << "-DRUNTIMES_CMAKE_ARGS=#{runtime_args.join(";")}"
end
llvmpath = buildpath/"llvm"
mkdir llvmpath/"build" do
system "cmake", "-G", "Unix Makefiles", "..", *(std_cmake_args + args)
system "cmake", "--build", "."
system "cmake", "--build", ".", "--target", "install"
system "cmake", "--build", ".", "--target", "install-xcode-toolchain" if MacOS::Xcode.installed?
end
# Install versioned symlink, or else `llvm-config` doesn't work properly
lib.install_symlink "libLLVM.dylib" => "libLLVM-#{version.major}.dylib" if OS.mac?
# Install LLVM Python bindings
# Clang Python bindings are installed by CMake
(lib/site_packages).install llvmpath/"bindings/python/llvm"
# Install Vim plugins
%w[ftdetect ftplugin indent syntax].each do |dir|
(share/"vim/vimfiles"/dir).install Dir["*/utils/vim/#{dir}/*.vim"]
end
# Install Emacs modes
elisp.install Dir[llvmpath/"utils/emacs/*.el"] + Dir[share/"clang/*.el"]
end
def caveats
<<~EOS
To use the bundled libc++ please add the following LDFLAGS:
LDFLAGS="-L#{opt_lib} -Wl,-rpath,#{opt_lib}"
EOS
end
test do
assert_equal prefix.to_s, shell_output("#{bin}/llvm-config --prefix").chomp
assert_equal "-lLLVM-#{version.major}", shell_output("#{bin}/llvm-config --libs").chomp
assert_equal (lib/shared_library("libLLVM-#{version.major}")).to_s,
shell_output("#{bin}/llvm-config --libfiles").chomp
(testpath/"omptest.c").write <<~EOS
#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
int main() {
#pragma omp parallel num_threads(4)
{
printf("Hello from thread %d, nthreads %d\\n", omp_get_thread_num(), omp_get_num_threads());
}
return EXIT_SUCCESS;
}
EOS
clean_version = version.to_s[/(\d+\.?)+/]
system "#{bin}/clang", "-L#{lib}", "-fopenmp", "-nobuiltininc",
"-I#{lib}/clang/#{clean_version}/include",
"omptest.c", "-o", "omptest"
testresult = shell_output("./omptest")
sorted_testresult = testresult.split("\n").sort.join("\n")
expected_result = <<~EOS
Hello from thread 0, nthreads 4
Hello from thread 1, nthreads 4
Hello from thread 2, nthreads 4
Hello from thread 3, nthreads 4
EOS
assert_equal expected_result.strip, sorted_testresult.strip
(testpath/"test.c").write <<~EOS
#include <stdio.h>
int main()
{
printf("Hello World!\\n");
return 0;
}
EOS
(testpath/"test.cpp").write <<~EOS
#include <iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
return 0;
}
EOS
# Testing default toolchain and SDK location.
system "#{bin}/clang++", "-v",
"-std=c++11", "test.cpp", "-o", "test++"
assert_includes MachO::Tools.dylibs("test++"), "/usr/lib/libc++.1.dylib" if OS.mac?
assert_equal "Hello World!", shell_output("./test++").chomp
system "#{bin}/clang", "-v", "test.c", "-o", "test"
assert_equal "Hello World!", shell_output("./test").chomp
# Testing Command Line Tools
if MacOS::CLT.installed?
toolchain_path = "/Library/Developer/CommandLineTools"
system "#{bin}/clang++", "-v",
"-isysroot", MacOS::CLT.sdk_path,
"-isystem", "#{toolchain_path}/usr/include/c++/v1",
"-isystem", "#{toolchain_path}/usr/include",
"-isystem", "#{MacOS::CLT.sdk_path}/usr/include",
"-std=c++11", "test.cpp", "-o", "testCLT++"
assert_includes MachO::Tools.dylibs("testCLT++"), "/usr/lib/libc++.1.dylib"
assert_equal "Hello World!", shell_output("./testCLT++").chomp
system "#{bin}/clang", "-v", "test.c", "-o", "testCLT"
assert_equal "Hello World!", shell_output("./testCLT").chomp
end
# Testing Xcode
if MacOS::Xcode.installed?
system "#{bin}/clang++", "-v",
"-isysroot", MacOS::Xcode.sdk_path,
"-isystem", "#{MacOS::Xcode.toolchain_path}/usr/include/c++/v1",
"-isystem", "#{MacOS::Xcode.toolchain_path}/usr/include",
"-isystem", "#{MacOS::Xcode.sdk_path}/usr/include",
"-std=c++11", "test.cpp", "-o", "testXC++"
assert_includes MachO::Tools.dylibs("testXC++"), "/usr/lib/libc++.1.dylib"
assert_equal "Hello World!", shell_output("./testXC++").chomp
system "#{bin}/clang", "-v",
"-isysroot", MacOS.sdk_path,
"test.c", "-o", "testXC"
assert_equal "Hello World!", shell_output("./testXC").chomp
end
# link against installed libc++
# related to https://github.com/Homebrew/legacy-homebrew/issues/47149
system "#{bin}/clang++", "-v",
"-isystem", "#{opt_include}/c++/v1",
"-std=c++11", "-stdlib=libc++", "test.cpp", "-o", "testlibc++",
"-rtlib=compiler-rt", "-L#{opt_lib}", "-Wl,-rpath,#{opt_lib}"
assert_includes (testpath/"testlibc++").dynamically_linked_libraries,
(opt_lib/shared_library("libc++", "1")).to_s
(testpath/"testlibc++").dynamically_linked_libraries.each do |lib|
refute_match(/libstdc\+\+/, lib)
refute_match(/libgcc/, lib)
refute_match(/libatomic/, lib)
end
assert_equal "Hello World!", shell_output("./testlibc++").chomp
if OS.linux?
# Link installed libc++, libc++abi, and libunwind archives both into
# a position independent executable (PIE), as well as into a fully
# position independent (PIC) DSO for things like plugins that export
# a C-only API but internally use C++.
#
# FIXME: It'd be nice to be able to use flags like `-static-libstdc++`
# together with `-stdlib=libc++` (the latter one we need anyways for
# headers) to achieve this but those flags don't set up the correct
# search paths or handle all of the libraries needed by `libc++` when
# linking statically.
system "#{bin}/clang++", "-v", "-o", "test_pie_runtimes",
"-pie", "-fPIC", "test.cpp", "-L#{opt_lib}",
"-stdlib=libc++", "-rtlib=compiler-rt",
"-static-libstdc++", "-lpthread", "-ldl"
assert_equal "Hello World!", shell_output("./test_pie_runtimes").chomp
(testpath/"test_pie_runtimes").dynamically_linked_libraries.each do |lib|
refute_match(/lib(std)?c\+\+/, lib)
refute_match(/libgcc/, lib)
refute_match(/libatomic/, lib)
refute_match(/libunwind/, lib)
end
(testpath/"test_plugin.cpp").write <<~EOS
#include <iostream>
__attribute__((visibility("default")))
extern "C" void run_plugin() {
std::cout << "Hello Plugin World!" << std::endl;
}
EOS
(testpath/"test_plugin_main.c").write <<~EOS
extern void run_plugin();
int main() {
run_plugin();
}
EOS
system "#{bin}/clang++", "-v", "-o", "test_plugin.so",
"-shared", "-fPIC", "test_plugin.cpp", "-L#{opt_lib}",
"-stdlib=libc++", "-rtlib=compiler-rt",
"-static-libstdc++", "-lpthread", "-ldl"
system "#{bin}/clang", "-v",
"test_plugin_main.c", "-o", "test_plugin_libc++",
"test_plugin.so", "-Wl,-rpath=#{testpath}", "-rtlib=compiler-rt"
assert_equal "Hello Plugin World!", shell_output("./test_plugin_libc++").chomp
(testpath/"test_plugin.so").dynamically_linked_libraries.each do |lib|
refute_match(/lib(std)?c\+\+/, lib)
refute_match(/libgcc/, lib)
refute_match(/libatomic/, lib)
refute_match(/libunwind/, lib)
end
end
# Testing mlir
(testpath/"test.mlir").write <<~EOS
func @bad_branch() {
br ^missing // expected-error {{reference to an undefined block}}
}
EOS
system "#{bin}/mlir-opt", "--verify-diagnostics", "test.mlir"
(testpath/"scanbuildtest.cpp").write <<~EOS
#include <iostream>
int main() {
int *i = new int;
*i = 1;
delete i;
std::cout << *i << std::endl;
return 0;
}
EOS
assert_includes shell_output("#{bin}/scan-build #{bin}/clang++ scanbuildtest.cpp 2>&1"),
"warning: Use of memory after it is freed"
(testpath/"clangformattest.c").write <<~EOS
int main() {
printf("Hello world!"); }
EOS
assert_equal "int main() { printf(\"Hello world!\"); }\n",
shell_output("#{bin}/clang-format -style=google clangformattest.c")
# Ensure LLVM did not regress output of `llvm-config --system-libs` which for a time
# was known to output incorrect linker flags; e.g., `-llibxml2.tbd` instead of `-lxml2`.
# On the other hand, note that a fully qualified path to `dylib` or `tbd` is OK, e.g.,
# `/usr/local/lib/libxml2.tbd` or `/usr/local/lib/libxml2.dylib`.
shell_output("#{bin}/llvm-config --system-libs").chomp.strip.split.each do |lib|
if lib.start_with?("-l")
assert !lib.end_with?(".tbd"), "expected abs path when lib reported as .tbd"
assert !lib.end_with?(".dylib"), "expected abs path when lib reported as .dylib"
else
p = Pathname.new(lib)
if p.extname == ".tbd" || p.extname == ".dylib"
assert p.absolute?, "expected abs path when lib reported as .tbd or .dylib"
end
end
end
end
end