1# SPDX-License-Identifier: GPL-2.0
2PROG=	aicasm
3
4OUTDIR ?= ./
5
6.SUFFIXES= .l .y .c .h
7
8CSRCS=	aicasm.c aicasm_symbol.c
9YSRCS=	aicasm_gram.y aicasm_macro_gram.y
10LSRCS=	aicasm_scan.l aicasm_macro_scan.l
11
12GENHDRS=	$(addprefix ${OUTDIR}/,aicdb.h $(YSRCS:.y=.h))
13GENSRCS=	$(addprefix ${OUTDIR}/,$(YSRCS:.y=.c) $(LSRCS:.l=.c))
14
15SRCS=	${CSRCS} ${GENSRCS}
16LIBS=	-ldb
17clean-files:= ${GENSRCS} ${GENHDRS} $(YSRCS:.y=.output) $(PROG)
18# Override default kernel CFLAGS.  This is a userland app.
19AICASM_CFLAGS:= -I/usr/include -I. -I$(OUTDIR)
20LEX= flex
21YACC= bison
22YFLAGS= -d
23
24NOMAN=	noman
25
26ifneq ($(HOSTCC),)
27AICASM_CC= $(HOSTCC)
28else
29AICASM_CC= $(CC)
30endif
31
32ifdef DEBUG
33CFLAGS+= -DDEBUG -g
34YFLAGS+= -t -v
35LFLAGS= -d
36endif
37
38$(PROG):  $(OUTDIR) ${GENHDRS} $(SRCS)
39	$(AICASM_CC) $(AICASM_CFLAGS) $(SRCS) -o $(OUTDIR)/$(PROG) $(LIBS)
40
41$(OUTDIR):
42	mkdir -p $(OUTDIR)
43
44$(OUTDIR)/aicdb.h:
45	@if [ -e "/usr/include/db4/db_185.h" ]; then		\
46		echo "#include <db4/db_185.h>" > $@;	\
47	 elif [ -e "/usr/include/db3/db_185.h" ]; then		\
48		echo "#include <db3/db_185.h>" > $@;	\
49	 elif [ -e "/usr/include/db2/db_185.h" ]; then		\
50		echo "#include <db2/db_185.h>" > $@;	\
51	 elif [ -e "/usr/include/db1/db_185.h" ]; then		\
52		echo "#include <db1/db_185.h>" > $@;	\
53	 elif [ -e "/usr/include/db/db_185.h" ]; then		\
54		echo "#include <db/db_185.h>" > $@;	\
55	 elif [ -e "/usr/include/db_185.h" ]; then		\
56		echo "#include <db_185.h>" > $@;		\
57	 else							\
58		echo "*** Install db development libraries";	\
59	 fi
60
61clean:
62	rm -f $(clean-files)
63
64$(OUTDIR)/aicasm_gram.c $(OUTDIR)/aicasm_gram.h: aicasm_gram.y
65	$(YACC) $(YFLAGS) -b $(<:.y=) $< -o $(OUTDIR)/$(<:.y=.c)
66
67$(OUTDIR)/aicasm_macro_gram.c $(OUTDIR)/aicasm_macro_gram.h: aicasm_macro_gram.y
68	$(YACC) $(YFLAGS) -b $(<:.y=) -p mm $< -o $(OUTDIR)/$(<:.y=.c)
69
70$(OUTDIR)/aicasm_scan.c: aicasm_scan.l
71	$(LEX) $(LFLAGS) -o $@ $<
72
73$(OUTDIR)/aicasm_macro_scan.c: aicasm_macro_scan.l
74	$(LEX) $(LFLAGS) -Pmm -o $@ $<
75