Fix META file and goaljobs script to work with installed package.
[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,package:,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         --package)
51             passthru[i++]="-package"
52             passthru[i++]="$2"
53             shift 2;;
54         --pkgdir)
55             pkgdir="$2"
56             shift 2;;
57
58         --help)
59             usage
60             exit 0;;
61         --)
62             shift
63             break;;
64         *)
65             echo "Internal error!"
66             exit 1;;
67     esac
68 done
69
70 if [ $# -lt 1 ]; then
71     usage
72     exit 1
73 fi
74
75 # Bytecode or native code?
76 if ocamlopt --help >/dev/null 2>&1; then
77     best=opt
78     libext=cmxa
79 else
80     best=c
81     libext=cma
82 fi
83
84 # Get name of final source file.
85 for final; do :; done
86
87 # Get module names of all source files, and check them.
88 declare -a modules
89 i=0
90 for src in "$@"; do
91     # Module name of source file.
92     module=`basename "$src" .ml`
93     module="$(tr '[:lower:]' '[:upper:]' <<< ${module:0:1})${module:1}"
94
95     # Check module name is a valid OCaml name.
96     if [[ ! ( "$module" =~ ^[A-Z][a-zA-Z0-9_]*$ ) ]]; then
97         echo "$0: module name '$module' is not a valid OCaml module name."
98         echo "You have to use a file that contains only letters, numbers and"
99         echo "underscore, and starts with a letter."
100         exit 1
101     fi
102
103     modules[i++]="$module"
104 done
105
106 # Choose an output filename if the user didn't select one.
107 if [ "$output" = "" ]; then
108     output=`basename "$final" .ml`
109 fi
110
111 # Create a temporary 'main' file to handle command line args.
112 main=$(mktemp --suffix=.ml /tmp/goaljobsmainXXXXXX)
113 echo "let modules = [" > $main
114 for module in "${modules[@]}"; do
115     echo "    \"$module\";" >> $main
116 done
117 echo "] ;;" >> $main
118 echo "Goaljobs.init ()" >> $main
119
120 # Either use installed package or if user selected --pkgdir then
121 # use goaljobs from that directory.
122 declare -a pkg
123 if [ "$pkgdir" = "" ]; then
124     pkg[0]="-package"
125     pkg[1]="goaljobs"
126     pkg[2]="-syntax"
127     pkg[3]="goaljobs"
128 else
129     # Get the dependencies manually.  Note that calendar requires
130     # unix & str.
131     pkgdir="$(cd $pkgdir; pwd)"
132     pkg[0]="-I"
133     pkg[1]="$pkgdir"
134     pkg[2]="unix.$libext"
135     pkg[3]="str.$libext"
136     pkg[4]="-I"
137     pkg[5]="+calendar"
138     pkg[6]="calendarLib.$libext"
139     pkg[7]="goaljobs.$libext"
140     pkg[8]="-pp"
141     pkg[9]="camlp4o $pkgdir/pa_goal.cmo"
142 fi
143
144 # Compile the input file(s).
145 echo \
146 ocamlfind $best "${passthru[@]}" "${pkg[@]}" "$@" $main -linkpkg -o "$output"
147 ocamlfind $best "${passthru[@]}" "${pkg[@]}" "$@" $main -linkpkg -o "$output"
148
149 mainbase="$(echo $main | sed s,\.ml$,,)"
150 rm -f "$mainbase"*