# "make test" Compiles everything and runs the regression tests

.PHONY : test
test : all testall.sh
	./testall.sh

# "make all" builds the executable as well as the "printbig" library designed
# to test linking external code

.PHONY : all
all : lucifer.native initsdl initsdl.o player.o entity.o

# "make sdk" runs a bash script that installs sdl on the current computer
# to prepare Lucifer and SDL for linking
.PHONY : sdl
sdl : installsdl.sh
	./installsdl.sh 
 
# "make microc.native" compiles the compiler
#
# The _tags file controls the operation of ocamlbuild, e.g., by including
# packages, enabling warnings
#
# See https://github.com/ocaml/ocamlbuild/blob/master/manual/manual.adoc

lucifer.native :
	opam config exec -- \
	ocamlbuild -use-ocamlfind lucifer.native

# "make initsdl" compiles sdl code for a simple init and blue screen. without xserver running blue executable fails.
# taken from https://www.parallelrealities.co.uk/tutorials/shooter/shooter1.php
PROG = initsdl
CC = gcc

# the "make initsdl" is included in common.mk as well as definitions for OBJS
include common.mk

CXXFLAGS += `sdl2-config --cflags`
CXXFLAGS += -g -lefence

LDFLAGS += `sdl2-config --libs` -lSDL2_mixer -lSDL2_image -lSDL2_ttf -lm

$(PROG):$(OBJS)
	$(CC) -o $@ $(OBJS) $(LDFLAGS)

initsdl.o:
	gcc -c initsdl.c

# "make clean" removes all generated files

.PHONY : clean
clean :
	ocamlbuild -clean
	rm -rf testall.log ocamlllvm *.diff player.o entity.o initsdl.o \
	bin/main.o bin/input.o bin/init.o bin/draw.o \
	$(VISUALS:%=visual-%.exe)
       	

player : player.c
	cc -o player -DBUILD_TEST player.c

entity : entity.c
	cc -o entity -DBUILD_TEST entity.c

# Building the tarball

TESTS = \
  addEntityHitBox addPlayerControl addPlayerControl2 addPlayerHitBox \
  arith1 arith2 arith3 changeEntityXY changePlayerXY \
  comment entity entityExprArg fib for1 for2 \
  fun1 fun2 fun3 fun4 fun5 fun6 fun7 fun8 fun9 \
  gcd hello-world-nums hello-world-variable hello-world \
  if1 if2 if3 if4 if5 if6 objectFunction1 \
  objectFunction2 ops1 ops2 player playerExprArg \
  playerExtend singleComment while1 while2 modulo

FAILS = \
  after-return arith1 arith2 arith3 assign1 assign2 \
  assign3 comment controlPlayer-rungame \
  declEntity-changeXY entity-uninitialized entityInvalidName1 \
  entityInvalidName2 entityInvalidTexture entityInvalidX \
  entityInvalidY expr1 for1 for2 fun1 fun10 fun2 fun3 \
  fun4 fun5 fun6 fun7 fun8 fun9 hello-world-nofun \
  hello-world-nosemi hello-world-openstring \
  hello-world1 hello-world2 if1 if2 if3 no-main \
  objectFunctionArgOrder playerInvalidSize1 \
  playerInvalidSize2 playerInvalidSize3 playerInvalidTexture \
  playerInvalidX playerInvalidY rungame-entity-decl \
  rungame-undecl-entities sdl-init singleComment \
  structNotCap while1 while2

VISUALS = \
  big-program controlPlayer-rungame iskeypressed-moveentity \
  iskeypressed moving-entities render-player rungame-first \
  rungame-multiple-entities rungame-render-origin rungame-render \
  sdl-init


TESTFILES = $(TESTS:%=test-%.luc) $(TESTS:%=test-%.out) \
	    $(FAILS:%=fail-%.luc) $(FAILS:%=fail-%.err) \
	    $(VISUALS:%=visual-%.luc)

TARFILES = ast.ml sast.ml codegen.ml Makefile _tags lucifer.ml Luciferparse.mly installsdl.sh \
	README scanner.mll semant.ml testall.sh player.c entity.c initsdl.c common.mk \
        src/common.h  src/draw.c  src/entity.h  src/init.h     src/input.c  src/main.c  src/player.h \
	src/defs.h    src/draw.h  src/init.c    src/initsdl.h  src/input.h  src/main.h  src/structs.h \
	gfx/bat.jpg  gfx/bat.png  gfx/bat2.png  gfx/knight.png  gfx/playerBullet.png  gfx/youwin.png \
	Dockerfile \
	$(TESTFILES:%=tests/%) 

lucifer.tar.gz : $(TARFILES)
	cd .. && tar czf lucifer/lucifer.tar.gz \
		$(TARFILES:%=lucifer/%)


