EM = ../../../..
CEG = $(EM)/lib/ceg

Em   = $(CEG)/EM_parser/obj_EM_pars
ASMAIN = $(CEG)/assemble/obj_assemble
BACK = $(CEG)/ce_back
AS   = $(CEG)/as_parser
EVAL = $(CEG)/as_parser/eval
DEF = $(CEG)/defaults

AR = ar
CC = cc
LEXLIB = -ll

IFILES	= -I. -I.. -I$(AS) -I$(Em) -I$(EM)/h -I$(EM)/modules/h -I../back

CEG_LIBS = $(EM)/modules/lib/liballoc.a\
	   $(EM)/modules/lib/libprint.a\
	   $(EM)/modules/lib/libstring.a\
	   $(EM)/modules/lib/libsystem.a

CFLAGS = -O $(IFILES)

.c.o:	mach.h back.a
	cd ce ; $(CC) -c $(CFLAGS) ../$*.c ; cd ..

all:	back.a ce.a

install:	all
	../../install ce.a
	../../install back.a

cmp:	all
	-../../compare ce.a
	-../../compare back.a

clean:
	rm -rf ce back ceg ce.a back.a Out

ce.a : ce/dummy
	$(AR) r ce.a ce/*.o
	-sh -c 'ranlib ce.a'

ce/dummy : ce/dummy1 mach.h back.a
	cd ce;$(CC) -c $(CFLAGS) *.c; touch dummy ; cd ..

ce/dummy1:	ceg/ceg EM_table mach.h back.a
	-mkdir ce
	-ln mach.c ce/mach.c
	cd ce; ../ceg/ceg -l < $(DEF)/not_impl/not_impl_table ; cp $(DEF)/*.c . ; cp $(DEF)/*/*.c . ; cd ..
	cd ce; $(EM)/lib/cpp -P ../EM_table | ../ceg/ceg -l ; cd ..
	touch ce/dummy1

back.a: mach.h
	-mkdir back
	cd back;cp $(BACK)/obj_back/*h . ; cp $(BACK)/obj_back/Makefile . ; make; cd ..
	$(AR) r back.a back/*o;
	-sh -c 'ranlib back.a'

ceg/ceg : ceg/as_lib.a ceg/as_instr.o  ceg/eval_as.o # $(Em)/em_parser
	cd ceg; $(CC) -o ceg $(Em)/*.o as_instr.o eval_as.o as_lib.a $(CEG_LIBS) $(LEXLIB) ; cd ..

ceg/as_lib.a:
	-mkdir ceg
	cd ceg; cp $(ASMAIN)/* . ; $(CC) -c $(IFILES) *.c; $(AR) r as_lib.a *.o ; touch dummy ; cd ..
	-sh -c 'ranlib ceg/as_lib.a'

ceg/eval_as.o : ceg/eval_as.c as.h # $(AS)/as_parser.h $(Em)/arg_type.h
	cd ceg;$(CC) -c $(IFILES) eval_as.c ; cd ..

ceg/eval_as.c : as.c # $(EVAL)/eval
	cd ceg; $(EVAL)/eval < ../as.c > eval_as.c ; cd ..

ceg/as_instr.o : as.h ceg/dummy ceg/as_instr.c
	cd ceg;$(CC) -c $(IFILES) as_instr.c ; cd ..

ceg/as_instr.c : as_table # $(AS)/as_parser
	cd ceg; $(EM)/lib/cpp -P ../as_table| $(AS)/as_parser > as_instr.c ; cd ..
