|
| 1 | +require File.expand_path("../Abstract/portable-formula", __dir__) |
| 2 | + |
| 3 | +class PortableLibffi < PortableFormula |
| 4 | + desc "Portable Foreign Function Interface library" |
| 5 | + homepage "https://sourceware.org/libffi/" |
| 6 | + url "https://github.com/libffi/libffi/releases/download/v3.4.4/libffi-3.4.4.tar.gz" |
| 7 | + sha256 "d66c56ad259a82cf2a9dfc408b32bf5da52371500b84745f7fb8b645712df676" |
| 8 | + license "MIT" |
| 9 | + |
| 10 | + def install |
| 11 | + system "./configure", *portable_configure_args, |
| 12 | + *std_configure_args, |
| 13 | + "--enable-static", |
| 14 | + "--disable-shared", |
| 15 | + "--disable-docs" |
| 16 | + system "make", "install" |
| 17 | + end |
| 18 | + |
| 19 | + test do |
| 20 | + (testpath/"closure.c").write <<~EOS |
| 21 | + #include <stdio.h> |
| 22 | + #include <ffi.h> |
| 23 | + /* Acts like puts with the file given at time of enclosure. */ |
| 24 | + void puts_binding(ffi_cif *cif, unsigned int *ret, void* args[], |
| 25 | + FILE *stream) |
| 26 | + { |
| 27 | + *ret = fputs(*(char **)args[0], stream); |
| 28 | + } |
| 29 | + int main() |
| 30 | + { |
| 31 | + ffi_cif cif; |
| 32 | + ffi_type *args[1]; |
| 33 | + ffi_closure *closure; |
| 34 | + int (*bound_puts)(char *); |
| 35 | + int rc; |
| 36 | + /* Allocate closure and bound_puts */ |
| 37 | + closure = ffi_closure_alloc(sizeof(ffi_closure), &bound_puts); |
| 38 | + if (closure) |
| 39 | + { |
| 40 | + /* Initialize the argument info vectors */ |
| 41 | + args[0] = &ffi_type_pointer; |
| 42 | + /* Initialize the cif */ |
| 43 | + if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1, |
| 44 | + &ffi_type_uint, args) == FFI_OK) |
| 45 | + { |
| 46 | + /* Initialize the closure, setting stream to stdout */ |
| 47 | + if (ffi_prep_closure_loc(closure, &cif, puts_binding, |
| 48 | + stdout, bound_puts) == FFI_OK) |
| 49 | + { |
| 50 | + rc = bound_puts("Hello World!"); |
| 51 | + /* rc now holds the result of the call to fputs */ |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + /* Deallocate both closure, and bound_puts */ |
| 56 | + ffi_closure_free(closure); |
| 57 | + return 0; |
| 58 | + } |
| 59 | + EOS |
| 60 | + |
| 61 | + flags = ["-L#{lib}", "-lffi", "-I#{include}"] |
| 62 | + system ENV.cc, "-o", "closure", "closure.c", *(flags + ENV.cflags.to_s.split) |
| 63 | + system "./closure" |
| 64 | + end |
| 65 | +end |
0 commit comments