homebrew-core/Formula/libgccjit.rb

169 lines
6.7 KiB
Ruby

class Libgccjit < Formula
desc "JIT library for the GNU compiler collection"
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"
stable do
url "https://ftp.gnu.org/gnu/gcc/gcc-12.2.0/gcc-12.2.0.tar.xz"
mirror "https://ftpmirror.gnu.org/gcc/gcc-12.2.0/gcc-12.2.0.tar.xz"
sha256 "e549cf9cf3594a00e27b6589d4322d70e0720cdd213f39beb4181e06926230ff"
# Branch from the Darwin maintainer of GCC, with a few generic fixes and
# Apple Silicon support, located at https://github.com/iains/gcc-12-branch
patch do
url "https://raw.githubusercontent.com/Homebrew/formula-patches/1d184289/gcc/gcc-12.2.0-arm.diff"
sha256 "a7843b5c6bf1401e40c20c72af69c8f6fc9754ae980bb4a5f0540220b3dcb62d"
end
end
livecheck do
formula "gcc"
end
bottle do
sha256 arm64_ventura: "96e4b528984f59182ee7aed7861a740233140d032502841ebb66c4b94410796a"
sha256 arm64_monterey: "b9dfa10c1e056616bc9d637c03617e13b4620b613678e0abc45af4d3869328d5"
sha256 arm64_big_sur: "40984147835921a54fc6474ccb118b8c8ecf0886d8df0b9237cebc636abe4fdb"
sha256 ventura: "c61ebdda50c654fec1fbf5661fbdd90f17b2ead5df10255b6a150370c6b25fbd"
sha256 monterey: "f60548bf308d057615d804c8c57fe76bf9368e8ca73cf94f0c8cdf98d017340a"
sha256 big_sur: "43289b749acef40ffc8a3e23d4dedc8573be1203f62dd4f481bb1ce01c271ecf"
sha256 catalina: "dc1090d2da6c7c4ae491b361107103451314233e472d5bc868c05c4feb842076"
sha256 x86_64_linux: "adefc39946df2174d113c47455430067180d639d88a2b97457f79c921791d827"
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
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
# Use `lib/gcc/current` to align with the GCC formula.
args = %W[
--prefix=#{prefix}
--libdir=#{lib}/gcc/current
--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}
--with-system-zlib
]
if OS.mac?
cpu = Hardware::CPU.arm? ? "aarch64" : "x86_64"
args << "--build=#{cpu}-apple-darwin#{OS.kernel_version.major}"
# System headers may not be in /usr/include
sdk = MacOS.sdk_path_if_needed
args << "--with-sysroot=#{sdk}" if sdk
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"
system "make", "install"
end
# We only install the relevant libgccjit files from libexec and delete the rest.
prefix.find do |f|
rm_rf f if !f.directory? && !f.basename.to_s.start_with?("libgccjit")
end
# Provide a `lib/gcc/xy` directory to align with the versioned GCC formulae.
(lib/"gcc"/version.major).install_symlink (lib/"gcc/current").children
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