Write 'goaljobs' script (wrapper around the compiler).
[goaljobs.git] / goaljobs
1 #!/bin/bash -
2 # goaljobs
3 # Copyright (C) 2013 Red Hat Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 unset CDPATH
20
21 TEMP=`getopt -o 'gI:o:' --long 'help,pkgdir:' -n goaljobs -- "$@"`
22 if [ $? -ne 0 ]; then
23     echo "goaljobs: could not parse command line arguments"
24     exit 1
25 fi
26 eval set -- "$TEMP"
27
28 set -e
29
30 usage ()
31 {
32     echo "Usage: goaljobs [-o output] source.ml"
33     echo "See goaljobs(1) man page for more information."
34 }
35
36 declare -a passthru
37 i=0
38
39 while true; do
40     case "$1" in
41         -g)
42             passthru[i++]="$1"
43             shift;;
44         -I)
45             passthru[i++]="$2"
46             shift 2;;
47         -o)
48             output="$2"
49             shift 2;;
50         --pkgdir)
51             pkgdir="$2"
52             shift 2;;
53
54         --help)
55             usage
56             exit 0;;
57         --)
58             shift
59             break;;
60         *)
61             echo "Internal error!"
62             exit 1;;
63     esac
64 done
65
66 if [ $# -lt 1 ]; then
67     usage
68     exit 1
69 fi
70
71 # Bytecode or native code?
72 if ocamlopt --help >/dev/null 2>&1; then
73     best=opt
74     libext=cmxa
75 else
76     best=c
77     libext=cma
78 fi
79
80 # Get name of final source file.
81 for final; do :; done
82
83 # Get module names of all source files, and check them.
84 declare -a modules
85 i=0
86 for src in "$@"; do
87     # Module name of source file.
88     module=`basename "$src" .ml`
89     module="$(tr '[:lower:]' '[:upper:]' <<< ${module:0:1})${module:1}"
90
91     # Check module name is a valid OCaml name.
92     if [[ ! ( "$module" =~ ^[A-Z][a-zA-Z0-9_]*$ ) ]]; then
93         echo "$0: module name '$module' is not a valid OCaml module name."
94         echo "You have to use a file that contains only letters, numbers and"
95         echo "underscore, and starts with a letter."
96         exit 1
97     fi
98
99     modules[i++]="$module"
100 done
101
102 # Choose an output filename if the user didn't select one.
103 if [ "$output" = "" ]; then
104     output=`basename "$final" .ml`
105 fi
106
107 # Either use installed package or if user selected --pkgdir then
108 # use goaljobs from that directory.
109 declare -a pkg
110 if [ "$pkgdir" = "" ]; then
111     pkg[0]="-package"
112     pkg[1]="goaljobs,goaljobs.syntax"
113 else
114     pkgdir="$(cd $pkgdir; pwd)"
115     pkg[0]="-I"
116     pkg[1]="$pkgdir"
117     pkg[2]="unix.$libext"
118     pkg[3]="goaljobs.$libext"
119     pkg[4]="-pp"
120     pkg[5]="camlp4o $pkgdir/pa_goal.cmo"
121 fi
122
123 # Compile the input file(s).
124 echo \
125 ocamlfind $best "${passthru[@]}" "${pkg[@]}" "$@" -o "$output"
126 ocamlfind $best "${passthru[@]}" "${pkg[@]}" "$@" -o "$output"