From: Richard W.M. Jones Date: Mon, 16 Sep 2013 15:20:52 +0000 (+0100) Subject: Write 'goaljobs' script (wrapper around the compiler). X-Git-Tag: 0.2~39 X-Git-Url: http://git.annexia.org/?p=goaljobs.git;a=commitdiff_plain;h=4a78b6d657697537bfd18488cd2177ba833b94af Write 'goaljobs' script (wrapper around the compiler). --- diff --git a/examples/compile-c/.depend b/examples/compile-c/.depend deleted file mode 100644 index a95aa04..0000000 --- a/examples/compile-c/.depend +++ /dev/null @@ -1,2 +0,0 @@ -compile.cmo : ../../goaljobs.cmi -compile.cmx : ../../goaljobs.cmx diff --git a/examples/compile-c/Makefile.am b/examples/compile-c/Makefile.am index 89906ad..9d01536 100644 --- a/examples/compile-c/Makefile.am +++ b/examples/compile-c/Makefile.am @@ -15,36 +15,13 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -include ../../common-rules.mk +include $(top_srcdir)/common-rules.mk -EXTRA_DIST = main.c utils.c $(sources) +EXTRA_DIST = main.c utils.c compile.ml CLEANFILES += compile program -sources = compile.ml - noinst_SCRIPTS = compile -compile: ../../goaljobs.cmxa compile.cmx - $(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) \ - $(OCAMLOPTPACKAGES) -linkpkg $^ -o $@ - -compile.cmx: compile.ml ../../pa_goal.cmo - OCAMLPATH=$(abs_top_builddir) \ - $(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) $(OCAMLOPTPACKAGES) \ - -pp "$(CAMLP4O) ../../pa_goal.cmo" -c $< -o $@ - -# Dependencies. - -depend: .depend - -.depend: $(sources) - rm -f $@ $@-t - $(OCAMLFIND) ocamldep -I $(abs_srcdir) -I $(top_builddir) $^ | \ - $(SED) 's/ *$$//' | \ - $(SED) -e :a -e '/ *\\$$/N; s/ *\\\n */ /; ta' | \ - $(SED) -e 's,$(abs_srcdir)/,$(builddir)/,g' | \ - sort > $@-t - mv $@-t $@ - --include .depend +compile: compile.ml $(top_builddir)/goaljobs.cmxa $(top_builddir)/pa_goal.cmo $(top_builddir)/goaljobs + $(top_builddir)/goaljobs --pkgdir $(top_builddir) $< -o $@ diff --git a/examples/compile-c/compile.ml b/examples/compile-c/compile.ml index 6cd2e88..043bc9a 100644 --- a/examples/compile-c/compile.ml +++ b/examples/compile-c/compile.ml @@ -1,4 +1,4 @@ -open Goaljobs (* will be implicit *) +open Goaljobs let rec goal all () = require (built "program" ["main.c"; "utils.c"]) diff --git a/goaljobs b/goaljobs index 6628d3e..93dd844 100755 --- a/goaljobs +++ b/goaljobs @@ -15,3 +15,112 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +unset CDPATH + +TEMP=`getopt -o 'gI:o:' --long 'help,pkgdir:' -n goaljobs -- "$@"` +if [ $? -ne 0 ]; then + echo "goaljobs: could not parse command line arguments" + exit 1 +fi +eval set -- "$TEMP" + +set -e + +usage () +{ + echo "Usage: goaljobs [-o output] source.ml" + echo "See goaljobs(1) man page for more information." +} + +declare -a passthru +i=0 + +while true; do + case "$1" in + -g) + passthru[i++]="$1" + shift;; + -I) + passthru[i++]="$2" + shift 2;; + -o) + output="$2" + shift 2;; + --pkgdir) + pkgdir="$2" + shift 2;; + + --help) + usage + exit 0;; + --) + shift + break;; + *) + echo "Internal error!" + exit 1;; + esac +done + +if [ $# -lt 1 ]; then + usage + exit 1 +fi + +# Bytecode or native code? +if ocamlopt --help >/dev/null 2>&1; then + best=opt + libext=cmxa +else + best=c + libext=cma +fi + +# Get name of final source file. +for final; do :; done + +# Get module names of all source files, and check them. +declare -a modules +i=0 +for src in "$@"; do + # Module name of source file. + module=`basename "$src" .ml` + module="$(tr '[:lower:]' '[:upper:]' <<< ${module:0:1})${module:1}" + + # Check module name is a valid OCaml name. + if [[ ! ( "$module" =~ ^[A-Z][a-zA-Z0-9_]*$ ) ]]; then + echo "$0: module name '$module' is not a valid OCaml module name." + echo "You have to use a file that contains only letters, numbers and" + echo "underscore, and starts with a letter." + exit 1 + fi + + modules[i++]="$module" +done + +# Choose an output filename if the user didn't select one. +if [ "$output" = "" ]; then + output=`basename "$final" .ml` +fi + +# Either use installed package or if user selected --pkgdir then +# use goaljobs from that directory. +declare -a pkg +if [ "$pkgdir" = "" ]; then + pkg[0]="-package" + pkg[1]="goaljobs,goaljobs.syntax" +else + pkgdir="$(cd $pkgdir; pwd)" + pkg[0]="-I" + pkg[1]="$pkgdir" + pkg[2]="unix.$libext" + pkg[3]="goaljobs.$libext" + pkg[4]="-pp" + pkg[5]="camlp4o $pkgdir/pa_goal.cmo" +fi + +# Compile the input file(s). +echo \ +ocamlfind $best "${passthru[@]}" "${pkg[@]}" "$@" -o "$output" +ocamlfind $best "${passthru[@]}" "${pkg[@]}" "$@" -o "$output"