### Configuration section

# Which C compiler to use. 
# If you're compiling on a Sparc, 680x0, or 80386, performance will be much
# improved if you use gcc instead of cc.
CC=gcc

# Additional options to $(CC).
# Add -DANSI if your compiler understands function prototypes.
OPTS=-DANSI

# Where public executables reside
BINDIR=/usr/local/bin

# Where the Caml Light standard library resides
LIBDIR=/usr/local/lib/caml-light

# The manual section where the manual pages should be installed
MANEXT=1

# The directory where the manual pages should be installed
MANDIR=/usr/man/man$(MANEXT)

### End of configuration section

SUBDIRS=runtime launch lib compiler linker librar toplevel lex yacc tools

world:
	cd runtime; make CC="$(CC)" OPTS="$(OPTS)" all
	cp runtime/camlrun .
	cd yacc; make CC="$(CC)" OPTS="$(OPTS)" all
	cp yacc/camlyacc .
	cd lib; make all
	cd compiler; make all
	cd linker; make all
	cd librar; make all
	cd lex; make all
	cd toplevel; make all
	cd launch; make LIBDIR=$(LIBDIR) BINDIR=$(BINDIR) \
                           CC="$(CC)" OPTS="$(OPTS)" all
	@ echo "Let's test quickly the toplevel system..."
	(echo "1+2;;";                                                        \
         echo "let rec fib n = if n < 2 then 1 else fib(n-1)+fib(n-2);;";     \
         echo "fib 20;;") | ./camlrun toplevel/camltop -stdlib lib
	@ echo "Is that 10946 on the line above? Good."
	@ echo "The Caml Light system is up and running."
	
bootstrap: backup promote again compare

backup:
	sh tools/backup camlrun camlcomp camllink camllibr camllex 

promote:
	cp compiler/camlcomp linker/camllink librar/camllibr lex/camllex .
	- cp toplevel/camltop .

again:
	for d in lib compiler linker librar lex toplevel; \
        do (cd $$d; make scratch all); \
	done

compare:
	@sh -c ' \
        if cmp camlcomp compiler/camlcomp \
        && cmp camllink linker/camllink \
        && cmp camllibr librar/camllibr \
        && cmp camltop toplevel/camltop \
        && cmp camllex lex/camllex; \
        then echo "The Caml Light system has successfully recompiled itself.";\
        else echo "Hmph. Better do one more bootstrapping cycle."; \
        fi'

install:
	- mkdir $(LIBDIR) $(BINDIR)
	cd runtime; make BINDIR=$(BINDIR) LIBDIR=$(LIBDIR) install
	cd launch; make BINDIR=$(BINDIR) LIBDIR=$(LIBDIR) install
	cd lib; make BINDIR=$(BINDIR) LIBDIR=$(LIBDIR) install
	cd compiler; make BINDIR=$(BINDIR) LIBDIR=$(LIBDIR) install
	cd linker; make BINDIR=$(BINDIR) LIBDIR=$(LIBDIR) install
	cd librar; make BINDIR=$(BINDIR) LIBDIR=$(LIBDIR) install
	cd toplevel; make BINDIR=$(BINDIR) LIBDIR=$(LIBDIR) install
	cd lex; make BINDIR=$(BINDIR) LIBDIR=$(LIBDIR) install
	cd yacc; make BINDIR=$(BINDIR) LIBDIR=$(LIBDIR) install
	cd man; make MANDIR=$(MANDIR) MANEXT=$(MANEXT) install

rminstall:
	rm -rf $(LIBDIR)

clean:
	for d in $(SUBDIRS); \
	do (cd $$d; make clean); \
        done

scratch:
	for d in $(SUBDIRS); \
	do (cd $$d; make scratch); \
        done
