class Llvm < Formula desc "Next-gen compiler infrastructure" homepage "https://llvm.org/" stable do url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/llvm-8.0.1.src.tar.xz" sha256 "44787a6d02f7140f145e2250d56c9f849334e11f9ae379827510ed72f12b75e7" resource "clang" do url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/cfe-8.0.1.src.tar.xz" sha256 "70effd69f7a8ab249f66b0a68aba8b08af52aa2ab710dfb8a0fba102685b1646" end resource "clang-extra-tools" do url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/clang-tools-extra-8.0.1.src.tar.xz" sha256 "187179b617e4f07bb605cc215da0527e64990b4a7dd5cbcc452a16b64e02c3e1" end resource "compiler-rt" do url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/compiler-rt-8.0.1.src.tar.xz" sha256 "11828fb4823387d820c6715b25f6b2405e60837d12a7469e7a8882911c721837" end resource "libcxx" do url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/libcxx-8.0.1.src.tar.xz" sha256 "7f0652c86a0307a250b5741ab6e82bb10766fb6f2b5a5602a63f30337e629b78" end resource "libunwind" do url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/libunwind-8.0.1.src.tar.xz" sha256 "1870161dda3172c63e632c1f60624564e1eb0f9233cfa8f040748ca5ff630f6e" end resource "lld" do url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/lld-8.0.1.src.tar.xz" sha256 "9fba1e94249bd7913e8a6c3aadcb308b76c8c3d83c5ce36c99c3f34d73873d88" end resource "lldb" do url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/lldb-8.0.1.src.tar.xz" sha256 "e8a79baa6d11dd0650ab4a1b479f699dfad82af627cbbcd49fa6f2dc14e131d7" end resource "openmp" do url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/openmp-8.0.1.src.tar.xz" sha256 "3e85dd3cad41117b7c89a41de72f2e6aa756ea7b4ef63bb10dcddf8561a7722c" end resource "polly" do url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/polly-8.0.1.src.tar.xz" sha256 "e8a1f7e8af238b32ce39ab5de1f3317a2e3f7d71a8b1b8bbacbd481ac76fd2d1" end end bottle do cellar :any sha256 "8017a53698fb2daf1ee34842baf27fe205ccf24292bd5a3afe4af9654aab29f2" => :mojave sha256 "ebe23525722afbd8b06744e9ad021cb9cd41ad3167c5fd3c4ea18e2c69767481" => :high_sierra sha256 "f1046344d20c85c40554e9378a4f3aaab2c2c6ceb707e0201462c2fd56fe05be" => :sierra end # Clang cannot find system headers if Xcode CLT is not installed pour_bottle? do reason "The bottle needs the Xcode CLT to be installed." satisfy { MacOS::CLT.installed? } end head do url "https://git.llvm.org/git/llvm.git" resource "clang" do url "https://git.llvm.org/git/clang.git" end resource "clang-extra-tools" do url "https://git.llvm.org/git/clang-tools-extra.git" end resource "compiler-rt" do url "https://git.llvm.org/git/compiler-rt.git" end resource "libcxx" do url "https://git.llvm.org/git/libcxx.git" end resource "libunwind" do url "https://git.llvm.org/git/libunwind.git" end resource "lld" do url "https://git.llvm.org/git/lld.git" end resource "lldb" do url "https://git.llvm.org/git/lldb.git" end resource "openmp" do url "https://git.llvm.org/git/openmp.git" end resource "polly" do url "https://git.llvm.org/git/polly.git" end end keg_only :provided_by_macos # https://llvm.org/docs/GettingStarted.html#requirement depends_on "cmake" => :build depends_on :xcode => :build depends_on "libffi" depends_on "swig" if MacOS.version >= :lion def install # Apple's libstdc++ is too old to build LLVM ENV.libcxx if ENV.compiler == :clang (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/lldb").install resource("lldb") (buildpath/"tools/polly").install resource("polly") (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} -DLLVM_CREATE_XCODE_TOOLCHAIN=ON -DLLDB_USE_SYSTEM_DEBUGSERVER=ON -DLLDB_DISABLE_PYTHON=1 -DLIBOMP_INSTALL_ALIASES=OFF ] mkdir "build" do system "cmake", "-G", "Unix Makefiles", "..", *(std_cmake_args + args) system "make" system "make", "install" system "make", "install-xcode-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 #include #include 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 int main() { printf("Hello World!\\n"); return 0; } EOS (testpath/"test.cpp").write <<~EOS #include 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 , 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