#
# OpenComal -- a free Comal implementation
#
# This file is part of the OpenComal package.
# (c) Copyright 1992-2024 Jos Visser <josvisser66@gmail.com>
#
# The OpenComal package is covered by the GNU General Public
# License. See doc/LICENSE for more information.
#

SOURCES=lex.yy.c pdccloop.c pdccmd.c pdcenv.c pdcexec.c pdcexp.c pdcext.c \
	pdcfree.c pdcid.c pdclexs.c pdclinux.c pdclist.c pdcmain.c pdcmem.c \
	pdcmisc.c pdcpars.tab.c pdcparss.c pdcprog.c pdcrun.c pdcscan.c \
	pdcseg.c pdcsqash.c pdcstr.c pdcsym.c pdcval.c pdcmod.c pdccomp.c \
	pdccexp.c

TARG1=../bin/opencomal
TARG1D=../bin/opencomal_defsys
OBJ1=pdcpars.tab.o lex.yy.o pdcmain.o pdcmisc.o pdccmd.o  pdclexs.o  \
	pdcid.o  pdcscan.o pdcparss.o pdcenv.o pdcsym.o pdcexec.o pdclist.o \
	pdcfree.o pdcexp.o pdcmem.o pdcsqash.o pdcstr.o pdcprog.o pdcext.o \
	pdcseg.o pdcval.o pdccloop.o pdcmod.o pdccomp.o pdccexp.o

TARG2=../bin/opencomalrun
TARG2D=../bin/opencomalrun_defsys
OBJ2=pdcmain.o pdcmisc.o \
	pdcid.o  pdcscan.o pdcenv.o pdcsym.o pdcexec.o pdcfree.o \
	pdcexp.o pdcmem.o pdcsqash.o pdcstr.o pdcprog.o pdcext.o pdcseg.o \
	pdcval.o pdcrun.o pdcmod.o

DSYS=pdcdsys.o pdcposix.o

TARG3=../bin/opencomal_tests
OBJ3=pdcpars.tab.o lex.yy.o pdctests.o pdcmisc.o pdccmd.o  pdclexs.o  \
	pdcid.o  pdcscan.o pdcparss.o pdcenv.o pdcsym.o pdcexec.o pdclist.o \
	pdcfree.o pdcexp.o pdcmem.o pdcsqash.o pdcstr.o pdcprog.o pdcext.o \
	pdcseg.o pdcval.o pdccloop.o pdcmod.o pdccomp.o pdccexp.o

# Choose OS below
ASAN=-fsanitize=address -fno-omit-frame-pointer

# MacOS
OS=pdcmac.o pdcposix.o
CC=clang
COPTS=-c -Wall -ggdb -DOS_MAC $(PROFILE) -I/opt/homebrew/opt/readline/include -pg $(ASAN)
OSLDOPTS=-lncurses  -L/opt/homebrew/opt/readline/lib -lreadline $(ASAN)

# Linux
# OS=pdclinux.o pdcposix.o
# CC=gcc
# COPTS=-c -ggdb -pedantic -Wall -DOS_LINUX $(PROFILE) $(ASAN)
# COPTS=-c -pedantic -Wall -DOS_LINUX $(PROFILE)
# OSLDOPTS=$(ASAN) -lncurses -lreadline

# Webasm
# OS=pdcwasm.c
# CC=emcc
# COPTS=-c
# OSLDOPTS=-sEXPORTED_RUNTIME_METHODS=stringToNewUTF8 -sASYNCIFY --preload-file ../samples@/ -sSTACK_SIZE=131072

# C compiler
LDOPTS=-lm $(OSLDOPTS)

%.o: %.c
		$(CC) $(COPTS) -o $@ $*.c

%.d: %.c
		set -e; $(CC) -M $(CPPFLAGS) $< \
			| sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' > $@; \
			[ -s $@ ] || rm -f $@

all:		buildinfo $(TARG1) $(TARG2) $(TARG1D) $(TARG2D)

tests:		test_build
		../bin/$(TARG3) ../samples/tests

test_build:		$(TARG3)

.depend:	
		makedepend -f .depend *.c *.y *.l

$(TARG1):	 $(OBJ1) $(OS)
		$(CC) $(PROFILE) -o $(TARG1) $(OBJ1) $(OS) $(LDOPTS)

$(TARG1D):	 $(OBJ1) $(DSYS)
		$(CC) $(PROFILE) -o $(TARG1D) $(OBJ1) $(DSYS) $(LDOPTS)


$(TARG2):	 $(OBJ2) $(OS)
		$(CC) $(PROFILE) -o $(TARG2) $(OBJ2) $(OS) $(LDOPTS)

$(TARG2D):	 $(OBJ2) $(DSYS)
		$(CC) $(PROFILE) -o $(TARG2D) $(OBJ2) $(DSYS) $(LDOPTS)

$(TARG3):	 $(OBJ3) $(DSYS)
		$(CC) $(PROFILE) -o $(TARG3) $(OBJ3) $(DSYS) $(LDOPTS)


buildinfo:		
		../tools/genversion

pdcpars.tab.c:	pdcpars.y
		bison -vd pdcpars.y

lex.yy.c:	pdclex.l
		flex pdclex.l

# Run this target before compiling the DOS binary in a dosbox.
dosprep:	pdcpars.tab.c lex.yy.c
		cp pdcpars.tab.c pdcpars.c
		cp pdcpars.tab.h pdcpars.h
		cp lex.yy.c pdclex.c

almostclean:	
		-rm *.o *.obj *.map *.OBJ *~ *.d
		-rm lex.yy.c pdcpars.tab.* pdcpars.output
		-rm pdcpars.c pdclex.c pdcpars.h
		
clean:		almostclean
		-rm $(TARG1) $(TARG2) $(TARG1D) $(TARG2D) $(TARG3)
		-rm ../bin/*.exe ../bin/*.EXE ../bin/*.wasm ../bin/*.data

.c.o:
		gcc $(COPTS) $*.c 

include $(SOURCES:.c=.d)

# DO NOT DELETE
