#########################################################################
#                                                                       #
#                            Objective Caml                             #
#                                                                       #
#            Xavier Leroy, projet Cristal, INRIA Rocquencourt           #
#                                                                       #
#   Copyright 1999 Institut National de Recherche en Informatique et    #
#   en Automatique.  All rights reserved.  This file is distributed     #
#   under the terms of the GNU Library General Public License, with     #
#   the special exception on linking described in file ../LICENSE.      #
#                                                                       #
#########################################################################

# $Id: Makefile,v 1.81.4.5 2005/08/02 14:11:12 frisch Exp $

include ../config/Makefile

RUNTIME=../boot/ocamlrun
COMPILER=../ocamlc
CAMLC=$(RUNTIME) $(COMPILER)
COMPFLAGS=-g -warn-error As -nostdlib -I cduce
OPTCOMPILER=../ocamlopt
CAMLOPT=$(RUNTIME) $(OPTCOMPILER)
OPTCOMPFLAGS=-warn-error As -nostdlib -I cduce
CAMLDEP=../boot/ocamlrun ../tools/ocamldep
CC=$(BYTECC)

OBJS=pervasives.cmo $(OTHERS)
OTHERS=array.cmo list.cmo char.cmo string.cmo sys.cmo \
  hashtbl.cmo sort.cmo marshal.cmo obj.cmo \
  int32.cmo int64.cmo nativeint.cmo \
  lexing.cmo parsing.cmo \
  set.cmo map.cmo stack.cmo queue.cmo stream.cmo buffer.cmo \
  printf.cmo format.cmo scanf.cmo \
  arg.cmo printexc.cmo gc.cmo \
  digest.cmo random.cmo callback.cmo camlinternalOO.cmo oo.cmo \
  genlex.cmo weak.cmo \
  lazy.cmo filename.cmo complex.cmo \
  arrayLabels.cmo listLabels.cmo stringLabels.cmo moreLabels.cmo stdLabels.cmo \
  int_misc.cmo string_misc.cmo nat.cmo big_int.cmo arith_flags.cmo \
  ratio.cmo num.cmo arith_status.cmo \
  cduce_types.cmo \
  $(EXTRA)

EXTRA=ocamlduce.cmo

CDUCE=\
  cduce/custom.cmo cduce/encodings.cmo \
  cduce/imap.cmo cduce/upool.cmo \
  cduce/ns.cmo cduce/sortedList.cmo cduce/atoms.cmo \
  cduce/bool.cmo cduce/chars.cmo cduce/ident.cmo \
  cduce/intervals.cmo cduce/inttbl.cmo cduce/normal.cmo \
  cduce/pretty.cmo cduce/stats.cmo \
  cduce/compunit.cmo \
  cduce/types.cmo cduce/sequence.cmo cduce/sample.cmo \
  cduce/auto_pat.cmo cduce/patterns.cmo \
  cduce/value.cmo cduce/run_dispatch.cmo cduce/explain.cmo \
  cduce/typepat.cmo \
  cduce/serial.cmo

all: stdlib.cma std_exit.cmo camlheader camlheader_ur

allopt: stdlib.cmxa std_exit.cmx allopt-$(PROFILING)

allopt-noprof:

allopt-prof: stdlib.p.cmxa std_exit.p.cmx
	rm -f std_exit.p.cmi

install:
	cp stdlib.cma std_exit.cmo *.cmi *.mli *.ml camlheader camlheader_ur \
           $(LIBDIR)

installopt: installopt-default installopt-$(PROFILING)

installopt-default:
	cp stdlib.cmxa stdlib.a std_exit.o *.cmx $(LIBDIR)
	cd $(LIBDIR); $(RANLIB) stdlib.a

installopt-noprof:
	rm -f $(LIBDIR)/stdlib.p.cmxa; ln -s stdlib.cmxa $(LIBDIR)/stdlib.p.cmxa
	rm -f $(LIBDIR)/stdlib.p.a; ln -s stdlib.a $(LIBDIR)/stdlib.p.a
	rm -f $(LIBDIR)/std_exit.p.cmx; \
          ln -s std_exit.cmx $(LIBDIR)/std_exit.p.cmx
	rm -f $(LIBDIR)/std_exit.p.o; ln -s std_exit.o $(LIBDIR)/std_exit.p.o

installopt-prof:
	cp stdlib.p.cmxa stdlib.p.a std_exit.p.cmx std_exit.p.o $(LIBDIR)
	cd $(LIBDIR); $(RANLIB) stdlib.p.a

stdlib.cma: $(OBJS)
	$(CAMLC) -a -o stdlib.cma $(OBJS)

stdlib.cmxa: $(OBJS:.cmo=.cmx)
	$(CAMLOPT) -a -o stdlib.cmxa $(OBJS:.cmo=.cmx)

stdlib.p.cmxa: $(OBJS:.cmo=.p.cmx)
	$(CAMLOPT) -a -o stdlib.p.cmxa $(OBJS:.cmo=.p.cmx)

camlheader camlheader_ur: header.c ../config/Makefile
	if $(SHARPBANGSCRIPTS); then \
          echo '#!$(BINDIR)/ocamlrun' > camlheader && \
          echo '#!' | tr -d '\012' > camlheader_ur; \
        else \
	  $(BYTECC) $(BYTECCCOMPOPTS) $(BYTECCLINKOPTS) \
                    -DRUNTIME_NAME='"$(BINDIR)/ocamlrun"' \
                    header.c -o tmpheader$(EXE) && \
          strip tmpheader$(EXE) && \
	  mv tmpheader$(EXE) camlheader && \
          cp camlheader camlheader_ur; \
        fi

clean::
	rm -f camlheader camlheader_ur

.SUFFIXES: .mli .ml .cmi .cmo .cmx .p.cmx

.mli.cmi:
	$(CAMLC) $(COMPFLAGS) `./Compflags $@` -c $<

.ml.cmo:
	$(CAMLC) $(COMPFLAGS) `./Compflags $@` -c $<

.ml.cmx:
	$(CAMLOPT) $(OPTCOMPFLAGS) `./Compflags $@` -c $<

.ml.p.cmx:
	$(CAMLOPT) $(OPTCOMPFLAGS) `./Compflags $@` -p -c -o $*.p.cmx $<

cduce_types.cmo: $(CDUCE)
	$(CAMLC) $(COMPFLAGS) -pack -o cduce_types.cmo $(CDUCE)

# To make ocamlopt -pack works, under Mac OS X, we need to install
# this objcopy emulator before the real installation

cduce_types.cmx: $(CDUCE:.cmo=.cmx)
	$(BINUTILS_INSTALL_OBJCOPY) ../tools/ocaml-objcopy-macosx $(BINUTILS_OBJCOPY)
	$(CAMLOPT) $(OPTCOMPFLAGS) -pack -o cduce_types.cmx $(CDUCE:.cmo=.cmx)

#cduce_types.p.cmx: $(CDUCE:.cmo=.p.cmx)
#	for i in $(CDUCE:.cmo=.cmi); do \
#	  cp $$i `echo $$i | sed s/\.cmi/.p.cmi/`; \
#	done
#	$(CAMLOPT) $(OPTCOMPFLAGS) -pack -p -o cduce_types.cmx $(CDUCE:.cmo=.p.cmx)
cduce_types.p.cmx: cduce_types.cmx
	cp cduce_types.cmx cduce_types.p.cmx
	cp cduce_types.o cduce_types.p.o

# Dependencies on the compiler
$(OBJS) $(CDUCE) std_exit.cmo: $(COMPILER)
$(OBJS:.cmo=.cmi) $(CDUCE:.cmo=.cmi) std_exit.cmi: $(COMPILER)
$(OBJS:.cmo=.cmx) $(CDUCE:.cmo=.cmx) std_exit.cmx: $(OPTCOMPILER)
$(OBJS:.cmo=.p.cmx) $(CDUCE:.cmo=.p.cmx) std_exit.p.cmx: $(OPTCOMPILER)

# Dependencies on Pervasives (not tracked by ocamldep)
$(OBJS) $(CDUCE) std_exit.cmo: pervasives.cmi
$(OTHERS:.cmo=.cmi) $(CDUCE:.cmo=.cmi) std_exit.cmi: pervasives.cmi
$(OBJS:.cmo=.cmx) $(CDUCE:.cmo=.cmx) std_exit.cmx: pervasives.cmi
$(OBJS:.cmo=.p.cmx) $(CDUCE:.cmo=.p.cmx) std_exit.p.cmx: pervasives.cmi
$(OTHERS:.cmo=.cmx) std_exit.cmx: pervasives.cmx
$(OTHERS:.cmo=.p.cmx) std_exit.p.cmx: pervasives.cmx

clean::
	rm -f *.cm* *.o *.a
	rm -f *~
	(cd cduce; rm -f *.cm* *.o *.a)
	(cd cduce; rm -f *~)

include .depend

depend:
	$(CAMLDEP) -I cduce *.mli *.ml cduce/*.mli cduce/*.ml > .depend


link:
	ln -f -s ../otherlibs/num/*.ml ../otherlibs/num/*.mli .

link_clean:
	rm -f arith_flags.mli arith_status.mli big_int.mli int_misc.mli nat.mli num.mli ratio.mli string_misc.mli	
	rm -f arith_flags.ml arith_status.ml big_int.ml int_misc.ml nat.ml num.ml ratio.ml string_misc.ml
