JULIAHOME = $(abspath ..)
include $(JULIAHOME)/Make.inc

SRCS = \
	jltypes gf ast builtins module codegen interpreter \
	alloc dlload sys init task array dump
FLAGS = \
    -Wall -Wno-strict-aliasing -fno-omit-frame-pointer \
	-Iflisp -Isupport -fvisibility=hidden -fno-common \
	-I$(call exec,$(LLVM_CONFIG) --includedir) \
	-I$(EXTROOT)/include

OBJS = $(SRCS:%=%.o)
DOBJS = $(SRCS:%=%.do)
DEBUGFLAGS += $(FLAGS)
SHIPFLAGS += $(FLAGS)

ifeq ($(JULIAGC),MARKSWEEP)
SRCS += gc
endif

ifeq ($(USE_COPY_STACKS),1)
CFLAGS += -DCOPY_STACKS
endif

default: release

release debug: %: libjulia-%

%.o: %.c julia.h
	@$(call, PRINT_CC, $(CC) $(CFLAGS) $(SHIPFLAGS) -c $< -o $@)
%.do: %.c julia.h
	@$(call PRINT_CC, $(CC) $(CFLAGS) $(DEBUGFLAGS) -c $< -o $@)
%.o: %.cpp julia.h
	@$(call PRINT_CC, $(CXX) $(CXXFLAGS) $(SHIPFLAGS) $(call exec,$(LLVM_CONFIG) --cppflags) -c $< -o $@)
%.do: %.cpp julia.h
	@$(call PRINT_CC, $(CXX) $(CXXFLAGS) $(DEBUGFLAGS) $(call exec,$(LLVM_CONFIG) --cppflags) -c $< -o $@)

ast.o ast.do: julia_flisp.boot.inc

julia_flisp.boot.inc: julia_flisp.boot flisp/libflisp-debug.a
	@$(call PRINT_FLISP, flisp/flisp-debug ./bin2hex.scm < $< > $@)

julia_flisp.boot: julia-parser.scm julia-syntax.scm \
	match.scm utils.scm jlfrontend.scm mk_julia_flisp_boot.scm flisp/libflisp-debug.a
	@$(call PRINT_FLISP, flisp/flisp-debug ./mk_julia_flisp_boot.scm)

codegen.o codegen.do: intrinsics.cpp debuginfo.cpp cgutils.cpp ccall.cpp
builtins.o builtins.do: table.c

support/libsupport-debug.a: support/*.h support/*.c
	$(MAKE) -C support debug

support/libsupport-release.a: support/*.h support/*.c
	$(MAKE) -C support release

flisp/libflisp-debug.a: flisp/*.h flisp/*.c support/libsupport-debug.a
	$(MAKE) -C flisp debug

flisp/libflisp-release.a: flisp/*.h flisp/*.c support/libsupport-release.a
	$(MAKE) -C flisp release

../libjulia-debug.$(SHLIB_EXT): $(DOBJS) flisp/libflisp-debug.a support/libsupport-debug.a
	@$(call PRINT_LINK, $(CXX) $(DEBUGFLAGS) $(DOBJS) -shared -o ../libjulia-debug.$(SHLIB_EXT) $(LIBS))
libjulia-debug.a: $(DOBJS) flisp/libflisp-debug.a support/libsupport-debug.a
	rm -f $@
	@$(call PRINT_LINK, ar -rcs $@ $(DOBJS))
libjulia-debug: libjulia-debug.a ../libjulia-debug.$(SHLIB_EXT)

../libjulia-release.$(SHLIB_EXT): $(OBJS) flisp/libflisp-release.a support/libsupport-release.a
	@$(call PRINT_LINK, $(CXX) $(SHIPFLAGS) $(OBJS) -shared -o ../libjulia-release.$(SHLIB_EXT) $(LIBS))
libjulia-release.a: $(OBJS) flisp/libflisp-release.a support/libsupport-release.a
	rm -f $@
	@$(call PRINT_LINK, ar -rcs $@ $(OBJS))
libjulia-release: libjulia-release.a ../libjulia-release.$(SHLIB_EXT)

clean:
	rm -f libjulia*.$(SHLIB_EXT)
	rm -f libjulia*.a
	rm -f ../libjulia*.$(SHLIB_EXT)
	rm -f julia_flisp.boot
	rm -f julia_flisp.boot.inc
	rm -f *.do
	rm -f *.o
	rm -f *~ *#

clean-flisp:
	$(MAKE) -C flisp clean

clean-support:
	$(MAKE) -C support clean

cleanall: clean clean-flisp clean-support

.PHONY: debug release clean cleanall clean-*
