#!/usr/bin/env bash # Copyright (c) 2016-present, Facebook, Inc. All rights reserved. # rebuild is a reasonbuild wrapper that builds reason files # it calls into ocamlbuild, telling it to call a special # command 'reopt' and 'rec' which links custom reasson build # rules. setglobal DIR = $[ cd $[ dirname $(BASH_SOURCE[0])] && pwd] setglobal REOPT = ''"" setglobal REC = ''"" if [[ -f $DIR/reopt.sh ]] { setglobal REOPT = ""$DIR/reopt.sh"" } if [[ -f $DIR/reopt ]] { setglobal REOPT = ""$DIR/reopt"" } if test -z $REOPT { echo "Couldn't find reopt" exit 1 } if [[ -f $DIR/rec.sh ]] { setglobal REC = ""$DIR/rec.sh"" } if [[ -f $DIR/rec ]] { setglobal REC = ""$DIR/rec"" } if test -z $REC { echo "Couldn't find rec" exit 1 } # Since we need to override -ocamlopt and -ocaml, we parse the user passed-in # -ocamlopt and -ocamlc here and rebuild it in reopt and rec setglobal OCAMLOPTIDX = '-1' setglobal OCAMLCIDX = '-1' setglobal USEOCAMLFIND = '-1' # find ocamlopt in argument list setglobal i = '1' for var in [@Argv] { if [[ $var = "-ocamlopt" ]] { setglobal OCAMLOPTIDX = $i } if [[ $var = "-ocamlc" ]] { setglobal OCAMLCIDX = $i } if [[ $var = "-use-ocamlfind" ]] { setglobal USEOCAMLFIND = '1' } setglobal i = "$i+1" } # found ocamlopt, parsing setglobal OCAMLOPT = '"ocamlopt.opt'" if [[ $OCAMLOPTIDX -ne -1 ]] { # The argument after "-ocamlopt" will be parsed into reopt as ocamlopt to be used setglobal VALUEIDX = $shExpr('OCAMLOPTIDX+1') setglobal OCAMLOPT = $(!VALUEIDX) # Remove the parsed argument out of argument list set -- $(@:1:OCAMLOPTIDX-1) $(@: VALUEIDX+1) } # found ocamlc, parsing setglobal OCAMLC = '"ocamlc.opt'" if [[ $OCAMLCIDX -ne -1 ]] { # The argument after "-ocamlc" will be parsed into rec as ocamlc to be used setglobal VALUEIDX = $shExpr('OCAMLCIDX+1') setglobal OCAMLC = $(!VALUEIDX) # Remove the parsed argument out of argument list set -- $(@:1:OCAMLCIDX-1) $(@: VALUEIDX+1) } if [[ $USEOCAMLFIND -ne -1 ]] { env OCAMLFIND_COMMANDS="ocamlopt=$REOPT ocamlc=$REC" reasonbuild @Argv } else { # pass OCAMLOPT as an environment variable reasonbuild -ocamlopt "env OCAMLOPT=\"$OCAMLOPT\" $REOPT" -ocamlc "env OCAMLC=\"$OCAMLC\" $REC" @Argv }