Version 0.2.
[goaljobs.git] / goaljobs
index 6628d3e..89e15e3 100755 (executable)
--- a/goaljobs
+++ b/goaljobs
 # 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,package:,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;;
+        --package)
+            passthru[i++]="-package"
+            passthru[i++]="$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
+
+# Create a temporary 'main' file to handle command line args.
+main=$(mktemp --suffix=.ml /tmp/goaljobsmainXXXXXX)
+echo "let modules = [" > $main
+for module in "${modules[@]}"; do
+    echo "    \"$module\";" >> $main
+done
+echo "] ;;" >> $main
+echo "Goaljobs.init ()" >> $main
+
+# 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
+    # Get the dependencies manually.  Note that calendar requires
+    # unix & str.
+    pkgdir="$(cd $pkgdir; pwd)"
+    pkg[0]="-I"
+    pkg[1]="$pkgdir"
+    pkg[2]="unix.$libext"
+    pkg[3]="str.$libext"
+    pkg[4]="-I"
+    pkg[5]="+calendar"
+    pkg[6]="calendarLib.$libext"
+    pkg[7]="goaljobs.$libext"
+    pkg[8]="-pp"
+    pkg[9]="camlp4o $pkgdir/pa_goal.cmo"
+fi
+
+# Compile the input file(s).
+echo \
+ocamlfind $best "${passthru[@]}" "${pkg[@]}" "$@" $main -linkpkg -o "$output"
+ocamlfind $best "${passthru[@]}" "${pkg[@]}" "$@" $main -linkpkg -o "$output"
+
+mainbase="$(echo $main | sed s,\.ml$,,)"
+rm -f "$mainbase"*