Source

sulu-ocaml-core / base / core / lib / discover.sh

#!/usr/bin/env bash
set -e -u -o pipefail

ML_OUTFILE="${1:?Usage: discover.sh ML_OUTFILE C_OUTFILE}"
C_OUTFILE="${2:?Usage: discover.sh ML_OUTFILE C_OUTFILE}"
shift 2

function cpp_test () {
    local name="$1"
    local cond="$2"
    if [[ "$#" == 3 ]]; then
        local warning="$3"
    else
        local warning="Feature $1 will not be availlable"
    fi
    cat <<EOF
#if ${cond}
printf ("DEFINE $name\n");
EOF
if [[ "$warning" != "" ]]; then
cat <<EOF
#else
#  warning "cpp test --${cond}-- was false"
#  warning "$warning"
EOF
fi
echo "#endif"
}

WORD_SIZE="$(ocaml <( echo "print_int Sys.word_size;;"))"
SRC="$(mktemp "./discover_src.XXXXXXX.c")"
PGM="$(mktemp "./discover.XXXXXXX")"
OUT="$(mktemp "./discover.out.XXXXXXX")"

trap "rm -f $SRC $PGM $OUT" EXIT
cat > "$SRC" <<EOF
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
# $LINENO "$(basename "${BASH_SOURCE[0]}")"
int main () {
  $(cpp_test LINUX_EXT    "defined(LINUX_EXT)")
  $(cpp_test POSIX_TIMERS "defined(POSIX_TIMERS)")
  $(if [[ ${WORD_SIZE} = 64 ]]; then
       echo 'printf ("DEFINE ARCH_SIXTYFOUR\n");';
    fi)
  $(if [[ $(uname -p) = "x86_64" ]]; then 
       echo 'printf ("DEFINE ARCH_x86_64\n");';
    fi)
  $(if [[ $(uname -p) = "i386" ]]; then 
       echo 'printf ("DEFINE ARCH_i386\n");';
    fi)
  $(cpp_test MSG_NOSIGNAL "defined MSG_NOSIGNAL" \
     "Bigstring.(unsafe_|really_)?send(to)?(_noblocking)?_no_sigpipe\
 will not be availlable")
  $(cpp_test MUTEX_TIMED_LOCK \
     "defined(_POSIX_TIMEOUTS) && (_POSIX_TIMEOUTS > 0)")

  $(cpp_test FDATASYNC \
     "defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0")

  return 0;
}
EOF
# Maybe we should leverage the ocaml compiler in here instead of hardcoding
# gcc
gcc "$SRC" -o "$PGM" "$@"
rm "$SRC"
"$PGM" > "$OUT"
rm "$PGM"
mv "$OUT" "$ML_OUTFILE"
cat "$ML_OUTFILE" \
    | sed -e 's|^DEFINE *|#define JSC_|' \
    | sed -e 's|\(#define JSC_[^ ]*\) *=|\1 |' > "$C_OUTFILE"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.