class LlvmAT14 < Formula desc "Next-gen compiler infrastructure" homepage "https://llvm.org/" url "https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.6/llvm-project-14.0.6.src.tar.xz" sha256 "8b3cfd7bc695bd6cea0f37f53f0981f34f87496e79e2529874fd03a2f9dd3a8a" # The LLVM Project is under the Apache License v2.0 with LLVM Exceptions license "Apache-2.0" => { with: "LLVM-exception" } livecheck do url :stable regex(/^llvmorg[._-]v?(14(?:\.\d+)+)$/i) end bottle do rebuild 1 sha256 cellar: :any, arm64_ventura: "d1e73c4acd999c1aced08909135b69a08810ebdd6576dde2a9c08aedf1b750f8" sha256 cellar: :any, arm64_monterey: "ba9b5f19cf6ed7846e6b5e9cbeb07263d2d2fb8a6c6523df05f5d3571cc1e84a" sha256 cellar: :any, arm64_big_sur: "6604d21b9f3cd98ca5648fb351b7267c71b874c3009d2077c31ace7435772e2e" sha256 cellar: :any, monterey: "fe171bc50c83ef517768c493bd32ba19d0900cb4d40ae32a24ba832391605229" sha256 cellar: :any, big_sur: "938d244c32373ccef1ab8398ed1c60990b044f567a83e9926ce82dd797864d09" sha256 cellar: :any, catalina: "5cc0bd34950758c9a0ac1060f91ab166a2c8067094ab07a4e3bd9d5e2c1098e4" sha256 cellar: :any_skip_relocation, x86_64_linux: "3fdca95c6df877e34cc8b000f56b72631e7624f0a3704b0171ecb0aece59fe1d" 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.10" => :build uses_from_macos "python" => :test 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 end # Fails at building LLDB fails_with gcc: "5" def install python3 = "python3.10" # The clang bindings need a little help finding our libclang. inreplace "clang/bindings/python/clang/cindex.py", /^(\s*library_path\s*=\s*)None$/, "\\1'#{lib}'" projects = %w[ clang clang-tools-extra lld lldb mlir polly ] runtimes = %w[ compiler-rt libcxx libcxxabi libunwind ] if OS.mac? runtimes << "openmp" else projects << "openmp" end python_versions = Formula.names .select { |name| name.start_with? "python@" } .map { |py| py.delete_prefix("python@") } 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 # we install the lldb Python module into libexec to prevent users from # accidentally importing it with a non-Homebrew Python or a Homebrew Python # in a non-default prefix. See https://lldb.llvm.org/resources/caveats.html 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=#{python_versions.join(";")} -DLLVM_CREATE_XCODE_TOOLCHAIN=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 runtimes_cmake_args = [] builtins_cmake_args = [] if OS.mac? args << "-DLLVM_BUILD_LLVM_C_DYLIB=ON" args << "-DLLVM_ENABLE_LIBCXX=ON" args << "-DLIBCXX_INSTALL_LIBRARY_DIR=#{lib}/c++" args << "-DLIBCXXABI_INSTALL_LIBRARY_DIR=#{lib}/c++" args << "-DDEFAULT_SYSROOT=#{macos_sdk}" if macos_sdk runtimes_cmake_args << "-DCMAKE_INSTALL_RPATH=#{loader_path}" # Prevent CMake from defaulting to `lld` when it's found next to `clang`. # This can be removed after CMake 3.25. See: # https://gitlab.kitware.com/cmake/cmake/-/merge_requests/7671 args << "-DLLVM_USE_LINKER=ld" [args, runtimes_cmake_args, builtins_cmake_args].each do |arg_array| arg_array << "-DCMAKE_LINKER=ld" end 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" runtimes_cmake_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 -DCOMPILER_RT_USE_BUILTINS_LIBRARY=ON -DCOMPILER_RT_USE_LLVM_UNWINDER=ON -DSANITIZER_CXX_ABI=libc++ -DSANITIZER_TEST_CXX=libc++ ] # Prevent compiler-rt from building i386 targets, as this is not portable. builtins_cmake_args << "-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON" end if ENV.cflags.present? args << "-DCMAKE_C_FLAGS=#{ENV.cflags}" runtimes_cmake_args << "-DCMAKE_C_FLAGS=#{ENV.cflags}" builtins_cmake_args << "-DCMAKE_C_FLAGS=#{ENV.cflags}" end if ENV.cxxflags.present? args << "-DCMAKE_CXX_FLAGS=#{ENV.cxxflags}" runtimes_cmake_args << "-DCMAKE_CXX_FLAGS=#{ENV.cxxflags}" builtins_cmake_args << "-DCMAKE_CXX_FLAGS=#{ENV.cxxflags}" end args << "-DRUNTIMES_CMAKE_ARGS=#{runtimes_cmake_args.join(";")}" if runtimes_cmake_args.present? args << "-DBUILTINS_CMAKE_ARGS=#{builtins_cmake_args.join(";")}" if builtins_cmake_args.present? llvmpath = buildpath/"llvm" mkdir llvmpath/"build" do system "cmake", "-G", "Unix Makefiles", "..", *(std_cmake_args + args) system "cmake", "--build", "." system "cmake", "--build", ".", "--target", "install" end if OS.mac? llvm_version = Version.new(Utils.safe_popen_read(bin/"llvm-config", "--version").strip) soversion = llvm_version.major.to_s # Install versioned symlink, or else `llvm-config` doesn't work properly lib.install_symlink "libLLVM.dylib" => "libLLVM-#{soversion}.dylib" # Install Xcode toolchain. See: # https://github.com/llvm/llvm-project/blob/main/llvm/tools/xcode-toolchain/CMakeLists.txt # We do this manually in order to avoid: # 1. installing duplicates of files in the prefix # 2. requiring an existing Xcode installation xctoolchain = prefix/"Toolchains/LLVM#{llvm_version}.xctoolchain" system "/usr/libexec/PlistBuddy", "-c", "Add:CFBundleIdentifier string org.llvm.#{llvm_version}", "Info.plist" system "/usr/libexec/PlistBuddy", "-c", "Add:CompatibilityVersion integer 2", "Info.plist" xctoolchain.install "Info.plist" (xctoolchain/"usr").install_symlink [bin, include, lib, libexec, share] end # Install LLVM Python bindings # Clang Python bindings are installed by CMake (lib/site_packages).install llvmpath/"bindings/python/llvm" # Create symlinks so that the Python bindings can be used with alternative Python versions python_versions.each do |py_ver| next if py_ver == Language::Python.major_minor_version(python3).to_s (lib/"python#{py_ver}/site-packages").install_symlink (lib/site_packages).children end # Install Vim plugins %w[ftdetect ftplugin indent syntax].each do |dir| (share/"vim/vimfiles"/dir).install Pathname.glob("*/utils/vim/#{dir}/*.vim") end # Install Emacs modes elisp.install llvmpath.glob("utils/emacs/*.el") + share.glob("clang/*.el") end def caveats on_macos do <<~EOS To use the bundled libc++ please add the following LDFLAGS: LDFLAGS="-L#{opt_lib}/c++ -Wl,-rpath,#{opt_lib}/c++" EOS end end test do llvm_version = Version.new(Utils.safe_popen_read(bin/"llvm-config", "--version").strip) soversion = llvm_version.major.to_s assert_equal version, llvm_version assert_equal prefix.to_s, shell_output("#{bin}/llvm-config --prefix").chomp assert_equal "-lLLVM-#{soversion}", shell_output("#{bin}/llvm-config --libs").chomp assert_equal (lib/shared_library("libLLVM-#{soversion}")).to_s, shell_output("#{bin}/llvm-config --libfiles").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 system "#{bin}/clang", "-L#{lib}", "-fopenmp", "-nobuiltininc", "-I#{lib}/clang/#{llvm_version.major_minor_patch}/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 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 cxx_libdir = OS.mac? ? opt_lib/"c++" : opt_lib system "#{bin}/clang++", "-v", "-isystem", "#{opt_include}/c++/v1", "-std=c++11", "-stdlib=libc++", "test.cpp", "-o", "testlibc++", "-rtlib=compiler-rt", "-L#{cxx_libdir}", "-Wl,-rpath,#{cxx_libdir}" assert_includes (testpath/"testlibc++").dynamically_linked_libraries, (cxx_libdir/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 __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 else # FIXME: scan-build test appears to be broken on Linux. (testpath/"scanbuildtest.cpp").write <<~EOS #include 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 clang++ scanbuildtest.cpp 2>&1"), "warning: Use of memory after it is freed" 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/"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") # This will fail if the clang bindings cannot find `libclang`. # We explicitly call `"python3"` instead of the method to be able to do # `uses_from_macos "python" => :test`. with_env(PYTHONPATH: prefix/Language::Python.site_packages("python3")) do system "python3", "-c", <<~EOS from clang import cindex cindex.Config().get_cindex_library() EOS end # 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