homebrew-core/Formula/libgccjit.rb

181 lines
7.2 KiB
Ruby

class Libgccjit < Formula
desc "JIT library for the GNU compiler collection"
if Hardware::CPU.arm?
# Branch from the Darwin maintainer of GCC with Apple Silicon support,
# located at https://github.com/iains/gcc-darwin-arm64 and
# backported with his help to gcc-11 branch. Too big for a patch.
url "https://github.com/fxcoudert/gcc/archive/refs/tags/gcc-11.2.0-arm-20211124.tar.gz"
sha256 "d7f8af7a0d9159db2ee3c59ffb335025a3d42547784bee321d58f2b4712ca5fd"
version "11.3.0"
else
url "https://ftp.gnu.org/gnu/gcc/gcc-11.3.0/gcc-11.3.0.tar.xz"
mirror "https://ftpmirror.gnu.org/gcc/gcc-11.3.0/gcc-11.3.0.tar.xz"
sha256 "b47cf2818691f5b1e21df2bb38c795fac2cfbd640ede2d0a5e1c89e338a3ac39"
end
homepage "https://gcc.gnu.org/"
license "GPL-3.0-or-later" => { with: "GCC-exception-3.1" }
head "https://gcc.gnu.org/git/gcc.git", branch: "master"
livecheck do
formula "gcc"
end
bottle do
sha256 arm64_monterey: "cb67919738ceaf2dc52ad6eda4b981cf3ef09034a506980de526a0a72f78b380"
sha256 arm64_big_sur: "ac5e717fe292f83c5a3c49ed76c0f47d1f968d6b24089b2ee5021682908ad935"
sha256 monterey: "9f0d50538e40657c82c891846d77c3193e4b39bcf4504429b11738fe0155d075"
sha256 big_sur: "4cce8e0cf231b7d52cb17cdd45af65d340bac1a8b6b2d76c15bab4544f1c778f"
sha256 catalina: "da0b032249866a6a74d6f0a7dd44cc2465261fd3becf847fcaafdf4a2750509c"
sha256 x86_64_linux: "ccd88ffd12b336f866a4ea6b85bac542d78d6dd887ccc28847b81a9fef690e0f"
end
# The bottles are built on systems with the CLT installed, and do not work
# out of the box on Xcode-only systems due to an incorrect sysroot.
pour_bottle? only_if: :clt_installed
depends_on "gcc" => :test
depends_on "gmp"
depends_on "isl"
depends_on "libmpc"
depends_on "mpfr"
depends_on "zstd"
uses_from_macos "zlib"
# GCC bootstraps itself, so it is OK to have an incompatible C++ stdlib
cxxstdlib_check :skip
# Fix for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102992
# Working around a macOS Monterey bug
if MacOS.version >= :monterey && Hardware::CPU.arm?
patch do
url "https://gcc.gnu.org/git/?p=gcc.git;a=patch;h=fabe8cc41e9b01913e2016861237d1d99d7567bf"
sha256 "9d3c2c91917cdc37d11385bdeba005cd7fa89efdbdf7ca38f7de3f6fa8a8e51b"
end
end
def install
# GCC will suffer build errors if forced to use a particular linker.
ENV.delete "LD"
pkgversion = "Homebrew GCC #{pkg_version} #{build.used_options*" "}".strip
cpu = Hardware::CPU.arm? ? "aarch64" : "x86_64"
args = %W[
--prefix=#{prefix}
--libdir=#{lib}/gcc/#{version.major}
--disable-nls
--enable-checking=release
--with-gcc-major-version-only
--with-gmp=#{Formula["gmp"].opt_prefix}
--with-mpfr=#{Formula["mpfr"].opt_prefix}
--with-mpc=#{Formula["libmpc"].opt_prefix}
--with-isl=#{Formula["isl"].opt_prefix}
--with-zstd=#{Formula["zstd"].opt_prefix}
--with-pkgversion=#{pkgversion}
--with-bugurl=#{tap.issues_url}
]
if OS.mac?
args << "--build=#{cpu}-apple-darwin#{OS.kernel_version.major}"
# Xcode 10 dropped 32-bit support
args << "--disable-multilib" if DevelopmentTools.clang_build_version >= 1000
# System headers may not be in /usr/include
sdk = MacOS.sdk_path_if_needed
if sdk
args << "--with-native-system-header-dir=/usr/include"
args << "--with-sysroot=#{sdk}"
end
# Use -headerpad_max_install_names in the build,
# otherwise updated load commands won't fit in the Mach-O header.
# This is needed because `gcc` avoids the superenv shim.
make_args = ["BOOT_LDFLAGS=-Wl,-headerpad_max_install_names"]
else
# Fix cc1: error while loading shared libraries: libisl.so.15
args << "--with-boot-ldflags=-static-libstdc++ -static-libgcc #{ENV["LDFLAGS"]}"
# Fix Linux error: gnu/stubs-32.h: No such file or directory.
args << "--disable-multilib"
# Change the default directory name for 64-bit libraries to `lib`
# https://stackoverflow.com/a/54038769
inreplace "gcc/config/i386/t-linux64", "m64=../lib64", "m64="
end
# Building jit needs --enable-host-shared, which slows down the compiler.
mkdir "build-jit" do
system "../configure", *args, "--enable-languages=jit", "--enable-host-shared"
system "make", *make_args
system "make", "install"
end
# We only install the relevant libgccjit files from libexec and delete the rest.
Dir["#{prefix}/**/*"].each do |f|
rm_rf f if !File.directory?(f) && !File.basename(f).to_s.start_with?("libgccjit")
end
end
test do
(testpath/"test-libgccjit.c").write <<~EOS
#include <libgccjit.h>
#include <stdlib.h>
#include <stdio.h>
static void create_code (gcc_jit_context *ctxt) {
gcc_jit_type *void_type = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
gcc_jit_type *const_char_ptr_type = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CONST_CHAR_PTR);
gcc_jit_param *param_name = gcc_jit_context_new_param (ctxt, NULL, const_char_ptr_type, "name");
gcc_jit_function *func = gcc_jit_context_new_function (ctxt, NULL, GCC_JIT_FUNCTION_EXPORTED,
void_type, "greet", 1, &param_name, 0);
gcc_jit_param *param_format = gcc_jit_context_new_param (ctxt, NULL, const_char_ptr_type, "format");
gcc_jit_function *printf_func = gcc_jit_context_new_function (ctxt, NULL, GCC_JIT_FUNCTION_IMPORTED,
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT), "printf", 1, &param_format, 1);
gcc_jit_rvalue *args[2];
args[0] = gcc_jit_context_new_string_literal (ctxt, "hello %s");
args[1] = gcc_jit_param_as_rvalue (param_name);
gcc_jit_block *block = gcc_jit_function_new_block (func, NULL);
gcc_jit_block_add_eval (block, NULL, gcc_jit_context_new_call (ctxt, NULL, printf_func, 2, args));
gcc_jit_block_end_with_void_return (block, NULL);
}
int main (int argc, char **argv) {
gcc_jit_context *ctxt;
gcc_jit_result *result;
ctxt = gcc_jit_context_acquire ();
if (!ctxt) {
fprintf (stderr, "NULL ctxt");
exit (1);
}
gcc_jit_context_set_bool_option (ctxt, GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE, 0);
create_code (ctxt);
result = gcc_jit_context_compile (ctxt);
if (!result) {
fprintf (stderr, "NULL result");
exit (1);
}
typedef void (*fn_type) (const char *);
fn_type greet = (fn_type)gcc_jit_result_get_code (result, "greet");
if (!greet) {
fprintf (stderr, "NULL greet");
exit (1);
}
greet ("world");
fflush (stdout);
gcc_jit_context_release (ctxt);
gcc_jit_result_release (result);
return 0;
}
EOS
gcc_major_ver = Formula["gcc"].any_installed_version.major
gcc = Formula["gcc"].opt_bin/"gcc-#{gcc_major_ver}"
libs = "#{HOMEBREW_PREFIX}/lib/gcc/#{gcc_major_ver}"
system gcc.to_s, "-I#{include}", "test-libgccjit.c", "-o", "test", "-L#{libs}", "-lgccjit"
assert_equal "hello world", shell_output("./test")
end
end