homebrew-core/Formula/llvm@4.rb
2018-09-13 14:00:16 +02:00

301 lines
11 KiB
Ruby

class CodesignRequirement < Requirement
fatal true
satisfy(:build_env => false) do
mktemp do
FileUtils.cp "/usr/bin/false", "llvm_check"
quiet_system "/usr/bin/codesign", "-f", "-s", "lldb_codesign", "--dryrun", "llvm_check"
end
end
def message
<<~EOS
lldb_codesign identity must be available to build with LLDB.
See: https://llvm.org/svn/llvm-project/lldb/trunk/docs/code-signing.txt
EOS
end
end
class LlvmAT4 < Formula
desc "Next-gen compiler infrastructure"
homepage "https://llvm.org/"
url "https://releases.llvm.org/4.0.1/llvm-4.0.1.src.tar.xz"
sha256 "da783db1f82d516791179fe103c71706046561f7972b18f0049242dee6712b51"
bottle do
cellar :any
sha256 "f50bac523d466168e2223e182c4aa63d88b7a0467df87ed350043889c4a46cb0" => :mojave
sha256 "d0bfdb67e52a97994cef179c3b8659f5c9fd6783f792524452f19ec234474ecd" => :high_sierra
sha256 "0c97a3cd61602de11f49bdff478a3eb43fd2b72c47b58bcd607a7a4b8652fdb2" => :sierra
sha256 "cfe3899f563c1dd1f5f6db15d5aeee6163a990344d29892453fe6f0bc6b3299c" => :el_capitan
sha256 "1bbffa119d25d27b4b2596c0277882d0a4de3c327bfecffcce98529cd4275486" => :yosemite
end
keg_only :versioned_formula
option "with-toolchain", "Build with Toolchain to facilitate overriding system compiler"
option "with-lldb", "Build LLDB debugger"
option "with-python@2", "Build bindings against Homebrew's Python 2"
deprecated_option "with-python" => "with-python@2"
depends_on "cmake" => :build
depends_on "libffi"
if MacOS.version <= :snow_leopard
depends_on "python@2"
else
depends_on "python@2" => :optional
end
if build.with? "lldb"
depends_on "swig" if MacOS.version >= :lion
depends_on CodesignRequirement
end
# According to the official llvm readme, GCC 4.7+ is required
fails_with :gcc_4_0
fails_with :gcc
("4.3".."4.6").each do |n|
fails_with :gcc => n
end
resource "clang" do
url "https://releases.llvm.org/4.0.1/cfe-4.0.1.src.tar.xz"
sha256 "61738a735852c23c3bdbe52d035488cdb2083013f384d67c1ba36fabebd8769b"
end
resource "clang-extra-tools" do
url "https://releases.llvm.org/4.0.1/clang-tools-extra-4.0.1.src.tar.xz"
sha256 "35d1e64efc108076acbe7392566a52c35df9ec19778eb9eb12245fc7d8b915b6"
end
resource "compiler-rt" do
url "https://releases.llvm.org/4.0.1/compiler-rt-4.0.1.src.tar.xz"
sha256 "a3c87794334887b93b7a766c507244a7cdcce1d48b2e9249fc9a94f2c3beb440"
end
resource "libcxx" do
url "https://releases.llvm.org/4.0.1/libcxx-4.0.1.src.tar.xz"
sha256 "520a1171f272c9ff82f324d5d89accadcec9bc9f3c78de11f5575cdb99accc4c"
end
resource "libunwind" do
url "https://releases.llvm.org/4.0.1/libunwind-4.0.1.src.tar.xz"
sha256 "3b072e33b764b4f9b5172698e080886d1f4d606531ab227772a7fc08d6a92555"
end
resource "lld" do
url "https://releases.llvm.org/4.0.1/lld-4.0.1.src.tar.xz"
sha256 "63ce10e533276ca353941ce5ab5cc8e8dcd99dbdd9c4fa49f344a212f29d36ed"
end
resource "lldb" do
url "https://releases.llvm.org/4.0.1/lldb-4.0.1.src.tar.xz"
sha256 "8432d2dfd86044a0fc21713e0b5c1d98e1d8aad863cf67562879f47f841ac47b"
end
resource "openmp" do
url "https://releases.llvm.org/4.0.1/openmp-4.0.1.src.tar.xz"
sha256 "ec693b170e0600daa7b372240a06e66341ace790d89eaf4a843e8d56d5f4ada4"
end
resource "polly" do
url "https://releases.llvm.org/4.0.1/polly-4.0.1.src.tar.xz"
sha256 "b443bb9617d776a7d05970e5818aa49aa2adfb2670047be8e9f242f58e84f01a"
end
def install
# Apple's libstdc++ is too old to build LLVM
ENV.libcxx if ENV.compiler == :clang
if build.with? "python@2"
ENV.prepend_path "PATH", Formula["python@2"].opt_libexec/"bin"
end
(buildpath/"tools/clang").install resource("clang")
(buildpath/"tools/clang/tools/extra").install resource("clang-extra-tools")
(buildpath/"projects/openmp").install resource("openmp")
(buildpath/"projects/libcxx").install resource("libcxx")
(buildpath/"projects/libunwind").install resource("libunwind")
(buildpath/"tools/lld").install resource("lld")
(buildpath/"tools/polly").install resource("polly")
if build.with? "lldb"
if build.with? "python@2"
pyhome = `python-config --prefix`.chomp
ENV["PYTHONHOME"] = pyhome
pylib = "#{pyhome}/lib/libpython2.7.dylib"
pyinclude = "#{pyhome}/include/python2.7"
end
(buildpath/"tools/lldb").install resource("lldb")
# Building lldb requires a code signing certificate.
# The instructions provided by llvm creates this certificate in the
# user's login keychain. Unfortunately, the login keychain is not in
# the search path in a superenv build. The following three lines add
# the login keychain to ~/Library/Preferences/com.apple.security.plist,
# which adds it to the superenv keychain search path.
mkdir_p "#{ENV["HOME"]}/Library/Preferences"
username = ENV["USER"]
system "security", "list-keychains", "-d", "user", "-s", "/Users/#{username}/Library/Keychains/login.keychain"
end
(buildpath/"projects/compiler-rt").install resource("compiler-rt")
# 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[
-DLIBOMP_ARCH=x86_64
-DLINK_POLLY_INTO_TOOLS=ON
-DLLVM_BUILD_EXTERNAL_COMPILER_RT=ON
-DLLVM_BUILD_LLVM_DYLIB=ON
-DLLVM_ENABLE_EH=ON
-DLLVM_ENABLE_FFI=ON
-DLLVM_ENABLE_LIBCXX=ON
-DLLVM_ENABLE_RTTI=ON
-DLLVM_INCLUDE_DOCS=OFF
-DLLVM_INSTALL_UTILS=ON
-DLLVM_OPTIMIZED_TABLEGEN=ON
-DLLVM_TARGETS_TO_BUILD=all
-DWITH_POLLY=ON
-DFFI_INCLUDE_DIR=#{Formula["libffi"].opt_lib}/libffi-#{Formula["libffi"].version}/include
-DFFI_LIBRARY_DIR=#{Formula["libffi"].opt_lib}
]
args << "-DLLVM_CREATE_XCODE_TOOLCHAIN=ON" if build.with? "toolchain"
if build.with?("lldb") && build.with?("python@2")
args << "-DLLDB_RELOCATABLE_PYTHON=ON"
args << "-DPYTHON_LIBRARY=#{pylib}"
args << "-DPYTHON_INCLUDE_DIR=#{pyinclude}"
end
mkdir "build" do
system "cmake", "-G", "Unix Makefiles", "..", *(std_cmake_args + args)
system "make"
system "make", "install"
system "make", "install-xcode-toolchain" if build.with? "toolchain"
end
(share/"clang/tools").install Dir["tools/clang/tools/scan-{build,view}"]
(share/"cmake").install "cmake/modules"
inreplace "#{share}/clang/tools/scan-build/bin/scan-build", "$RealBin/bin/clang", "#{bin}/clang"
bin.install_symlink share/"clang/tools/scan-build/bin/scan-build", share/"clang/tools/scan-view/bin/scan-view"
man1.install_symlink share/"clang/tools/scan-build/man/scan-build.1"
# install llvm python bindings
(lib/"python2.7/site-packages").install buildpath/"bindings/python/llvm"
(lib/"python2.7/site-packages").install buildpath/"tools/clang/bindings/python/clang"
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
(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
system "#{bin}/clang", "-L#{lib}", "-fopenmp", "-nobuiltininc",
"-I#{lib}/clang/#{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 Command Line Tools
if MacOS::CLT.installed?
libclangclt = Dir["/Library/Developer/CommandLineTools/usr/lib/clang/#{MacOS::CLT.version.to_i}*"].last { |f| File.directory? f }
system "#{bin}/clang++", "-v", "-nostdinc",
"-I/Library/Developer/CommandLineTools/usr/include/c++/v1",
"-I#{libclangclt}/include",
"-I/usr/include", # need it because /Library/.../usr/include/c++/v1/iosfwd refers to <wchar.h>, which CLT installs to /usr/include
"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", "-nostdinc",
"-I/usr/include", # this is where CLT installs stdio.h
"test.c", "-o", "testCLT"
assert_equal "Hello World!", shell_output("./testCLT").chomp
end
# Testing Xcode
if MacOS::Xcode.installed?
libclangxc = Dir["#{MacOS::Xcode.toolchain_path}/usr/lib/clang/#{DevelopmentTools.clang_version}*"].last { |f| File.directory? f }
system "#{bin}/clang++", "-v", "-nostdinc",
"-I#{MacOS::Xcode.toolchain_path}/usr/include/c++/v1",
"-I#{libclangxc}/include",
"-I#{MacOS.sdk_path}/usr/include",
"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", "-nostdinc",
"-I#{MacOS.sdk_path}/usr/include",
"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", "-nostdinc",
"-std=c++11", "-stdlib=libc++",
"-I#{MacOS::Xcode.toolchain_path}/usr/include/c++/v1",
"-I#{libclangxc}/include",
"-I#{MacOS.sdk_path}/usr/include",
"-L#{lib}",
"-Wl,-rpath,#{lib}", "test.cpp", "-o", "test"
assert_includes MachO::Tools.dylibs("test"), "#{opt_lib}/libc++.1.dylib"
assert_equal "Hello World!", shell_output("./test").chomp
end
end