|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +#************************************************************************** |
| 4 | +#* * |
| 5 | +#* OCaml * |
| 6 | +#* * |
| 7 | +#* Xavier Leroy, Collège de France and Inria * |
| 8 | +#* * |
| 9 | +#* Copyright 2023 Institut National de Recherche en Informatique et * |
| 10 | +#* en Automatique. * |
| 11 | +#* * |
| 12 | +#* All rights reserved. This file is distributed under the terms of * |
| 13 | +#* the GNU Lesser General Public License version 2.1, with the * |
| 14 | +#* special exception on linking described in the file LICENSE. * |
| 15 | +#* * |
| 16 | +#************************************************************************** |
| 17 | + |
| 18 | +# Build the runtime/prims.c file, with proper C declarations of the primitives |
| 19 | + |
| 20 | +export LC_ALL=C |
| 21 | + |
| 22 | +case $# in |
| 23 | + 0) echo "Usage: gen_primsc.sh <primitives file> <.c files>" 1>&2 |
| 24 | + exit 2;; |
| 25 | + *) primitives="$1"; shift;; |
| 26 | +esac |
| 27 | + |
| 28 | +cat <<'EOF' |
| 29 | +/* Generated file, do not edit */ |
| 30 | +
|
| 31 | +#define CAML_INTERNALS |
| 32 | +#include "caml/mlvalues.h" |
| 33 | +#include "caml/prims.h" |
| 34 | +
|
| 35 | +EOF |
| 36 | + |
| 37 | +# Extract the beginning of primitive definitions: |
| 38 | +# from 'CAMLprim' at beginning of line to the first closing parenthesis. |
| 39 | +# The first pattern below matches single-line definitions such as |
| 40 | +# CAMLprim value foo(value x) { |
| 41 | +# The second pattern matches multi-line definitions such as |
| 42 | +# CAMLprim value foo(value x, |
| 43 | +# value y) |
| 44 | +sed -n \ |
| 45 | + -e '/^CAMLprim value .*)/p' \ |
| 46 | + -e '/^CAMLprim value [^)]*$/,/)/p' \ |
| 47 | + "$@" | |
| 48 | +# Transform these definitions into "CAMLextern" declarations |
| 49 | +# TODO: consider getting rid of the DEFINE_NAN_CMP macro in runtime/floats.c |
| 50 | +sed \ |
| 51 | + -e 's/DEFINE_NAN_CMP(.*)/(value, value)/' \ |
| 52 | + -e 's/^CAMLprim /CAMLextern /' \ |
| 53 | + -e 's/).*$/);/' |
| 54 | + |
| 55 | +# Generate the table of primitives |
| 56 | +echo |
| 57 | +echo 'const c_primitive caml_builtin_cprim[] = {' |
| 58 | +sed -e 's/.*/ (c_primitive) &,/' "$primitives" |
| 59 | +echo ' 0 };' |
| 60 | + |
| 61 | +# Generate the table of primitive names |
| 62 | +echo |
| 63 | +echo 'const char * const caml_names_of_builtin_cprim[] = {' |
| 64 | +sed -e 's/.*/ "&",/' "$primitives" |
| 65 | +echo ' 0 };' |
| 66 | + |
0 commit comments