Skip to content

Commit

Permalink
add llvm-12
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcdonell committed Jul 6, 2021
1 parent ac8a975 commit 83b3f84
Showing 1 changed file with 158 additions and 0 deletions.
158 changes: 158 additions & 0 deletions Formula/llvm-12.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
class Llvm12 < Formula
desc "Next-gen compiler infrastructure"
homepage "http://llvm.org/"
# The LLVM Project is under the Apache License v2.0 with LLVM Exceptions
license "Apache-2.0" => { with: "LLVM-exception" }
revision 1

stable do
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-12.0.0/llvm-project-12.0.0.src.tar.xz"
sha256 "9ed1688943a4402d7c904cc4515798cdb20080066efa010fe7e1f2551b423628"
end

# bottle do
# root_url "https://github.com/llvm-hs/homebrew-llvm/releases/download/v12.0.0"
# sha256 cellar: :any, big_sur: ""
# end

# Clang cannot find system headers if Xcode CLT is not installed
pour_bottle? do
on_macos do
reason "The bottle needs the Xcode CLT to be installed."
satisfy { MacOS::CLT.installed? }
end
end

# http://releases.llvm.org/12.0.0/docs/GettingStarted.html#requirements
depends_on "cmake" => :build

uses_from_macos "libedit"
uses_from_macos "libffi", since: :catalina
uses_from_macos "libxml2"
uses_from_macos "ncurses"
uses_from_macos "zlib"

# version suffix
def version
"12"
end

# http://releases.llvm.org/12.0.0/docs/CMake.html
def install
projects = %w[
clang
clang-tools-extra
compiler-rt
libcxx
libcxxabi
libunwind
lld
lldb
mlir
openmp
polly
]
runtimes = %w[
]

# 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 also needs support 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

install_prefix = lib/"llvm-#{version}"

args = %W[
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_INSTALL_PREFIX=#{install_prefix}
-DLLVM_ENABLE_PROJECTS=#{projects.join(";")}
-DLLVM_ENABLE_RUNTIMES=#{runtimes.join(";")}
-DLLVM_TARGETS_TO_BUILD=all
-DLLVM_ENABLE_ASSERTIONS=ON
-DLLVM_OPTIMIZED_TABLEGEN=ON
-DLLVM_INCLUDE_DOCS=OFF
-DLLVM_INCLUDE_TESTS=OFF
-DLLVM_ENABLE_RTTI=ON
-DLLVM_ENABLE_EH=ON
-DLLVM_INSTALL_UTILS=ON
-DLLVM_ENABLE_Z3_SOLVER=OFF
-DLLVM_BUILD_EXTERNAL_COMPILER_RT=ON
-DLLVM_BUILD_LLVM_C_DYLIB=ON
-DLLVM_LINK_LLVM_DYLIB=ON
-DLLVM_ENABLE_LIBCXX=ON
-DLLVM_ENABLE_FFI=ON
-DLLVM_CREATE_XCODE_TOOLCHAIN=ON
-DLLVM_CREATE_XCODE_TOOLCHAIN=#{MacOS::Xcode.installed? ? "ON" : "OFF"}
-DLLDB_USE_SYSTEM_DEBUGSERVER=ON
]

if MacOS.version >= :catalina
args << "-DFFI_INCLUDE_DIR=#{MacOS.sdk_path}/usr/include/ffi"
args << "-DFFI_LIBRARY_DIR=#{MacOS.sdk_path}/usr/lib"
else
args << "-DFFI_INCLUDE_DIR=#{Formula["libffi"].opt_include}"
args << "-DFFI_LIBRARY_DIR=#{Formula["libffi"].opt_lib}"
end

sdk = MacOS.sdk_path_if_needed
args << "-DDEFAULT_SYSROOT=#{sdk}" if sdk

if MacOS.version == :mojave && MacOS::CLT.installed?
# Mojave CLT linker via software update is older than Xcode.
# Use it to retain compatibility.
args << "-DCMAKE_LINKER=/Library/Developer/CommandLineTools/usr/bin/ld"
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

# replace the existing "clang -> clang-12" symlink
rm install_prefix/"bin/clang"
mv install_prefix/"bin/clang-#{version}", install_prefix/"bin/clang"

# These versioned .dylib symlinks are missing for some reason
# Note that we use relative symlinks
ln_s "libLLVM.dylib", install_prefix/"lib/libLLVM-#{version}.dylib"

Dir.glob(install_prefix/"bin/*") do |exec_path|
basename = File.basename(exec_path)
bin.install_symlink exec_path => "#{basename}-#{version}"
end

Dir.glob(install_prefix/"share/man/man1/*") do |manpage|
basename = File.basename(manpage, ".1")
man1.install_symlink manpage => "#{basename}-#{version}.1"
end
end

def caveats
<<~EOS
Extra tools are installed in #{opt_share}/clang-#{version}
To link to libc++, something like the following is required:
CXX="clang++-#{version} -stdlib=libc++"
CXXFLAGS="$CXXFLAGS -nostdinc++ -I#{opt_lib}/llvm-#{version}/include/c++/v1"
LDFLAGS="$LDFLAGS -L#{opt_lib}/llvm-#{version}/lib"
EOS
end

test do
assert_equal prefix.to_s, shell_output("#{bin}/llvm-config-#{version} --prefix").chomp

# test for sed errors since some llvm makefiles assume that sed
# understands '\n' which is true for gnu sed and not for bsd sed.
assert_no_match(/PATH\)n/, (lib/"llvm-#{version}/share/llvm/cmake/LLVMConfig.cmake").read)
system "#{bin}/llvm-config-#{version}", "--version"
end
end

0 comments on commit 83b3f84

Please sign in to comment.