SRCDIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
JULIAHOME := $(abspath $(SRCDIR)/../..)
BUILDDIR := .
include $(JULIAHOME)/Make.inc

JCFLAGS += $(CFLAGS)
JCXXFLAGS += $(CXXFLAGS)
JCPPFLAGS += $(CPPFLAGS)
JLDFLAGS += $(LDFLAGS)

NAME := flisp
EXENAME := $(NAME)
LIBTARGET := lib$(NAME)

SRCS := flisp.c builtins.c string.c equalhash.c table.c iostream.c \
        julia_extensions.c

LLTDIR := ../support
LLTSRCDIR := $(SRCDIR)/$(LLTDIR)
NATIVE_BUILDDIR :=
ifeq ($(BUILDING_HOST_TOOLS), 1)
NATIVE_BUILDDIR := $(BUILDDIR)/..
LLT_BUILDDIR := $(BUILDDIR)/../$(LLTDIR)/host
else
NATIVE_BUILDDIR := $(BUILDDIR)
LLT_BUILDDIR := $(BUILDDIR)/$(LLTDIR)
endif

HEADERS := $(wildcard *.h) $(LIBUV_INC)/uv.h $(wildcard $(LLTDIR)/*.h)

OBJS := $(SRCS:%.c=$(BUILDDIR)/%.o)
DOBJS := $(SRCS:%.c=$(BUILDDIR)/%.dbg.obj)
LLT_release := $(LLT_BUILDDIR)/libsupport.a
LLT_debug := $(LLT_BUILDDIR)/libsupport-debug.a
LIBFILES_release := $(LLT_release) $(LIBUV)
LIBFILES_debug := $(LLT_debug) $(LIBUV)
LIBS :=
ifneq ($(OS),WINNT)
LIBS += -lpthread
endif

ifeq ($(USE_SYSTEM_UTF8PROC),0)
LIBFILES_release += $(LIBUTF8PROC)
LIBFILES_debug += $(LIBUTF8PROC)
else
LIBS += $(LIBUTF8PROC)
endif


FLAGS := -I$(LLTSRCDIR) $(JCFLAGS) $(HFILEDIRS:%=-I%) \
        -I$(LIBUV_INC) -I$(UTF8PROC_INC) -I$(build_includedir) $(LIBDIRS:%=-L%) \
        -DJL_LIBRARY_EXPORTS_INTERNAL -DUTF8PROC_EXPORTS
ifneq ($(OS), emscripten)
FLAGS += -DUSE_COMPUTED_GOTO
endif
FLAGS += -Wall -Wno-strict-aliasing -fvisibility=hidden -Wpointer-arith -Wundef
FLAGS += -Wold-style-definition -Wstrict-prototypes -Wc++-compat

DEBUGFLAGS += $(FLAGS)
SHIPFLAGS += $(FLAGS)

default: release

release: $(BUILDDIR)/$(EXENAME)$(EXE)

debug: $(BUILDDIR)/$(EXENAME)-debug$(EXE)

$(BUILDDIR):
	mkdir -p $(BUILDDIR)

$(BUILDDIR)/%.o: $(SRCDIR)/%.c $(HEADERS) | $(BUILDDIR)
	@$(call PRINT_CC, $(CC) $(JCPPFLAGS) $(SHIPFLAGS) $(DISABLE_ASSERTIONS) -c $< -o $@)
$(BUILDDIR)/%.dbg.obj: $(SRCDIR)/%.c $(HEADERS) | $(BUILDDIR)
	@$(call PRINT_CC, $(CC) $(JCPPFLAGS) $(DEBUGFLAGS) -c $< -o $@)

FLISP_SRCS := $(addprefix $(SRCDIR)/,flisp.c cvalues.c types.c flisp.h print.c read.c equal.c)
FLMAIN_SRCS := $(addprefix $(SRCDIR)/,flmain.c flisp.h)
$(BUILDDIR)/flisp.o: $(FLISP_SRCS)
$(BUILDDIR)/flisp.dbg.obj: $(FLISP_SRCS)
$(BUILDDIR)/flmain.o: $(FLMAIN_SRCS)
$(BUILDDIR)/flmain.dbg.obj: $(FLMAIN_SRCS)

$(LLT_release): $(LLTSRCDIR)/*.h $(LLTSRCDIR)/*.c
	$(MAKE) -C $(NATIVE_BUILDDIR)/$(LLTDIR) $(subst $(abspath $(NATIVE_BUILDDIR)/$(LLTDIR))/,,$(abspath $(LLT_release)))
$(LLT_debug): $(LLTSRCDIR)/*.h $(LLTSRCDIR)/*.c
	$(MAKE) -C $(NATIVE_BUILDDIR)/$(LLTDIR) $(subst $(abspath $(NATIVE_BUILDDIR)/$(LLTDIR))/,,$(abspath $(LLT_debug)))

$(BUILDDIR)/$(LIBTARGET)-debug.a: $(DOBJS) | $(BUILDDIR)
	rm -rf $@
	@$(call PRINT_LINK, $(AR) -rcs $@ $(DOBJS))

$(BUILDDIR)/$(LIBTARGET).a: $(OBJS) | $(BUILDDIR)
	rm -rf $@
	@$(call PRINT_LINK, $(AR) -rcs $@ $(OBJS))

CCLD := $(CC)

# Override `-shared-libasan` from root Make.inc
ifeq ($(SANITIZE),1)
ifeq ($(SANITIZE_ADDRESS),1)
JLDFLAGS += -static-libsan
endif
endif

$(BUILDDIR)/$(EXENAME)-debug$(EXE): $(DOBJS) $(LIBFILES_debug) $(BUILDDIR)/$(LIBTARGET)-debug.a $(BUILDDIR)/flmain.dbg.obj | $(BUILDDIR)/flisp.boot
	@$(call PRINT_LINK, $(CCLD) $(DEBUGFLAGS) $(JLDFLAGS) $(DOBJS) $(BUILDDIR)/flmain.dbg.obj -o $@ $(BUILDDIR)/$(LIBTARGET)-debug.a $(LIBFILES_debug) $(LIBS) $(OSLIBS))

$(BUILDDIR)/$(EXENAME)$(EXE): $(OBJS) $(LIBFILES_release) $(BUILDDIR)/$(LIBTARGET).a $(BUILDDIR)/flmain.o | $(BUILDDIR)/flisp.boot
	@$(call PRINT_LINK, $(CCLD) $(SHIPFLAGS) $(JLDFLAGS) $(OBJS) $(BUILDDIR)/flmain.o -o $@ $(BUILDDIR)/$(LIBTARGET).a $(LIBFILES_release) $(LIBS) $(OSLIBS))

$(BUILDDIR)/host/Makefile:
	mkdir -p $(BUILDDIR)/host
	@# add Makefiles to the build directories for convenience (pointing back to the source location of each)
	@echo '# -- This file is automatically generated in julia/src/flisp/Makefile -- #' > $@
	@echo 'BUILDDIR=$(BUILDDIR)/host' >> $@
	@echo 'BUILDING_HOST_TOOLS=1' >> $@
	@echo 'include $(SRCDIR)/Makefile' >> $@

$(BUILDDIR)/host/$(EXENAME): $(BUILDDIR)/host/Makefile | ${BUILDDIR}/host/flisp.boot
	make -C $(BUILDDIR)/host $(EXENAME)


$(BUILDDIR)/host/flisp.boot: $(SRCDIR)/flisp.boot | $(BUILDDIR)/host/Makefile
	cp $< $@

ifneq ($(BUILDDIR),.)
ifneq ($(BUILDDIR),$(SRCDIR))
$(BUILDDIR)/flisp.boot: $(SRCDIR)/flisp.boot | $(BUILDDIR)
	cp $< $@
endif
endif

test:
	$(call spawn,./$(EXENAME)$(EXE)) unittest.lsp

clean:
	rm -f $(BUILDDIR)/*.o
	rm -f $(BUILDDIR)/*.dbg.obj
	rm -f $(BUILDDIR)/*.a
	rm -f $(BUILDDIR)/$(EXENAME)$(EXE)
	rm -f $(BUILDDIR)/$(EXENAME)-debug$(EXE)
	rm -f $(BUILDDIR)/host/*

.PHONY: flisp-deps
