ARCH_FLAGS =
STANDARD_FLAGS = -std=c++17 -Wno-sized-deallocation -fno-sized-deallocation
WARNING_FLAGS = -Werror -Wextra -Wall -Wno-nonnull-compare -Wno-deprecated-copy -Wno-address-of-packed-member -Wundef -Wcast-qual -Wwrite-strings -Wimplicit-fallthrough -Wno-expansion-to-defined
FLAVOR_FLAGS = -fno-exceptions -fno-rtti -fstack-protector
OPTIMIZATION_FLAGS = -Os

MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
SERENITY_BASE_DIR := $(patsubst %/,%,$(dir $(MAKEFILE_PATH)))

INCLUDE_FLAGS += \
    -I. \
    -I$(SERENITY_BASE_DIR)/DevTools \
    -I$(SERENITY_BASE_DIR) \
    -I$(SERENITY_BASE_DIR)/Libraries \
    -I$(SERENITY_BASE_DIR)/Servers

LDFLAGS += \
    -L$(SERENITY_BASE_DIR)/Libraries/LibC \
    -L$(SERENITY_BASE_DIR)/Libraries/LibPthread \
    -L$(SERENITY_BASE_DIR)/Libraries/LibCore \
    -L$(SERENITY_BASE_DIR)/Libraries/LibIPC \
    -L$(SERENITY_BASE_DIR)/Libraries/LibM \
    -L$(SERENITY_BASE_DIR)/Libraries/LibDraw \
    -L$(SERENITY_BASE_DIR)/Libraries/LibGUI \
    -L$(SERENITY_BASE_DIR)/Libraries/LibHTML \
    -L$(SERENITY_BASE_DIR)/Libraries/LibMarkdown \
    -L$(SERENITY_BASE_DIR)/Libraries/LibThread \
    -L$(SERENITY_BASE_DIR)/Libraries/LibVT \
    -L$(SERENITY_BASE_DIR)/Libraries/LibPCIDB \
    -L$(SERENITY_BASE_DIR)/Libraries/LibProtocol \
    -L$(SERENITY_BASE_DIR)/Libraries/LibAudio

VERBOSE = 0

ifneq ($(HOST_CXX),)
    CXX = $(PRE_CXX) g++
    AS = as
    LINK = ld
    OBJ_SUFFIX ?= .host
else
    TOOLCHAIN_PATH = $(SERENITY_BASE_DIR)/Toolchain/Local/bin
    CXX = $(PRE_CXX) $(TOOLCHAIN_PATH)/i686-pc-serenity-g++
    AS = $(TOOLCHAIN_PATH)/i686-pc-serenity-as
    LINK = $(TOOLCHAIN_PATH)/i686-pc-serenity-ld
    DEFINES += -DDEBUG

    INCLUDE_FLAGS += \
    	-I$(SERENITY_BASE_DIR)/Libraries/LibC \
    	-I$(SERENITY_BASE_DIR)/Libraries/LibM \
    	-I$(SERENITY_BASE_DIR)/Libraries/LibPthread

    LDFLAGS += \
        -L$(SERENITY_BASE_DIR)/Libraries/LibC

    ifdef KERNEL
        DEFINES += -DKERNEL
    endif

    OBJ_SUFFIX ?=
endif

#CXX = clang $(CLANG_FLAGS)
#CLANG_FLAGS = -Wconsumed -m32 -ffreestanding -march=i686

#SUGGEST_FLAGS = -Wsuggest-final-types -Wsuggest-final-methods -Wsuggest-override #-Wsuggest-attribute=noreturn 
CXXFLAGS = -MMD -MP $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(FLAVOR_FLAGS) $(ARCH_FLAGS) $(STANDARD_FLAGS) $(SUGGEST_FLAGS) $(INCLUDE_FLAGS) $(DEFINES) $(SUBPROJECT_CXXFLAGS)

DEFINES += -DSANITIZE_PTRS

IPCCOMPILER = $(SERENITY_BASE_DIR)/DevTools/IPCCompiler/IPCCompiler

SUFFIXED_OBJS = $(patsubst %.o,%$(OBJ_SUFFIX).o,$(OBJS))

ifeq ($(VERBOSE),1)
    QUIET =
else
    QUIET = @
endif

-include $(SUFFIXED_OBJS:%.o=%.d)

%$(OBJ_SUFFIX).o: %.cpp $(EXTRA_SOURCES)
	@echo "C++ $@"
	$(QUIET) $(CXX) $(CXXFLAGS) -o $@ -c $<

%.ao: %.S
	@echo "AS $@"
	$(QUIET) $(AS) -o $@ $<

$(PROGRAM): $(SUFFIXED_OBJS) $(EXTRA_OBJS)
	@echo "LINK $(PROGRAM)"
	$(QUIET) $(CXX) -o $(PROGRAM) $(EXTRA_OBJS) $(SUFFIXED_OBJS) $(LDFLAGS)

$(LIBRARY): $(SUFFIXED_OBJS) $(EXTRA_OBJS)
	@echo "LIB $@"
	$(QUIET) $(AR) rcs $@ $(OBJS) $(EXTRA_OBJS) $(LIBS)

.DEFAULT_GOAL := all

all: $(PROGRAM) $(LIBRARY)

EXTRA_CLEAN ?=

clean:
	@echo "CLEAN"
	$(QUIET) rm -f $(PROGRAM) $(LIBRARY) $(SUFFIXED_OBJS) $(EXTRA_OBJS) *.d $(EXTRA_CLEAN)

install:

.PHONY: all clean install
