
.SUFFIXES:

TOP = @TOP@
DIR = source
LIB = .
BIN = ../bin
INCDIRS = $(TOP)/OSLib $(TOP)/Modules $(TOP) @INCDIRS@ 
INCS = $(foreach i, $(strip $(INCDIRS)), -I$(i))
#VPATH= $(INCDIRS) $(TOP) $(TOP)/Modules
LIBS = @LIBS@

CC = @CC@
LD = @LD@
CFLAGS = $(INCS) @DEFS@ @CFLAGS@
LDFLAGS = @LDFLAGS@ 
ARFLAGS = @ARFLAGS@

SRCS = xmalloc.c 9900.c 9901.c cru.c debugger.c dsr.c emulate.c \
	log.c grom.c keyboard.c memory.c mix_server.c fiad.c demo.c \
	roms.c sound.c speech.c sysdeps.c timer.c v9t9.c v9t9_module.c \
	vdp.c vdpsprites.c config.c command.c command_rl.c help.c moduledb.c \
	@SRCS@

GENSRCS = command.l.c command.y.c vdpdrawrow.c vdpdrawrowtext.c

DEPS = $(SRCS:%.c=%.d)
GENOBJS = $(GENSRCS:%.c=%.o)
OBJS = $(SRCS:%.c=%.o)

all:  link $(foreach i, $(HOSTOS) $(CONFIG), $(HOSTLINK_$(i)))

OUTDIRS = $(BIN)

$(OUTDIRS):
	mkdir -p $@

DIRS = Generate
LIBDIRS = OSLib Modules 
ALLLIBS = $(foreach i,$(LIBDIRS),$(i)/lib$(i).a) \
		Modules/@HOSTOS@/lib@HOSTOS@.a

vdpdrawrow.c vdpdrawrowtext.c: Generate

command.y.c: command.y
	@YACC@ command.y -v -d -o command.y.c

command.l.c: command.l command.y
	@LEX@ -ocommand.l.c command.l 

sub:	$(DIRS) $(LIBDIRS)
	for i in $(DIRS) $(LIBDIRS) ; do \
	if ! $(MAKE) -C $$i $(WHICH)  ; then exit 1; fi ;\
	done

# $(LIBDIRS:%=$(LIB)/lib%.a): sub-compile

sub-compile:
	if ! $(MAKE) sub WHICH=compile; then false; fi

sub-clean:
	if ! $(MAKE) sub WHICH=clean; then false; fi

sub-dep:
	if ! $(MAKE) sub WHICH=dep; then false; fi

-include $(DEPS)

FORCE:

gtkcallbacks.c: FORCE
	@PERL@ -w $(TOP)/fixgtkcallbacks.pl gtkcallbacks.c

%.o: %.c $(TOP)/conf.h
	$(CC) $(CFLAGS) @DEPFLAGS@ $(CFLAGS_$<) -o $@ -c $<

clean: sub-clean 
	rm -f $(BIN)/v9t9 $(DEPS) \
	$(GENOBJS) $(OBJS) \
	$(GENSRCS)

#dep: 	sub-dep
#	rm .depend
#	$(MAKE) makedep

compile: $(OUTDIRS) sub-compile $(OBJS) $(GENOBJS)

pp: 
	$(CC) $(CFLAGS) -E $(FILE)

$(BIN)/v9t9: compile $(ALLLIBS)
	$(LD) $(LDFLAGS) -o $(BIN)/v9t9 @MAPFLAGS@ \
		$(OBJS) $(GENOBJS) \
		$(ALLLIBS) $(LIBS)

link: $(BIN)/v9t9

link_unix_sudo: $(BIN)/v9t9
	sudo chown root $(BIN)/v9t9
	sudo chmod +s $(BIN)/v9t9





