# 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 $@
# 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"