# Makefile pour illustrer la notion de surcharge d'opérateur en c++
# incluant une nouvelle façon de créer un binaire exécutable
# + une nouvelle façon de sécuriser ce binaire.

# Auteur : Eric Bachard
# Ce document est sous Licence GPL v2  23 Septembre 2024
# voir : http://www.gnu.org/licenses/gpl-2.0.html

UNAME_S = `uname -s`
BUILD_DIR = build
INCLUDE_DIR = inc
APPLICATION_NAME = Modulations_Numeriques
BINARY_NAME = ${BUILD_DIR}/${APPLICATION_NAME}

# please adapt the value to the gcc version
# available on your machine
#G++_VERSION = ...

CXX = g++${G++_VERSION}

SOURCES_DIR = src
DEBUG_EXT = _debug
CXX_STD=-std=c++20

INC_BACKEND = ${INCLUDE_DIR}/backend
INC_FRONTEND = ${INCLUDE_DIR}/frontend
LOCALIZATION_INC = localization

CXX_FLAGS += -I ${INCLUDE_DIR} -I ${LOCALIZATION_INC} -I ${INC_BACKEND} -I ${INC_FRONTEND} -I ${SOURCES_DIR}/gl3w/

CXX_FLAGS += -Wall -ansi -pedantic ${CXX_STD}

CXX_FLAGS += `pkg-config --cflags --libs sdl3`

LDD_CXX_FLAGS += `pkg-config --libs sdl3`

CXX_FLAGS_DEBUG = -g -DDEBUG -Wall -Wformat


LIBS += -lm -lGL -dl ${LDD_CXX_FLAGS}

include secu.mk

# à modifier en fonction de vos besoins !!
SRC_BACKEND  = ${SOURCES_DIR}/backend
SRC_FRONTEND = ${SOURCES_DIR}/frontend

SOURCES = \
    ${SOURCES_DIR}/gl_helpers.cxx \
    ${SOURCES_DIR}/modulations.cxx \
    ${SOURCES_DIR}/imgui_utils.cxx \
    ${SOURCES_DIR}/application.cxx \
    ${SOURCES_DIR}/engine.cxx \
    ${SOURCES_DIR}/main.cxx

SOURCES += \
    ${SRC_FRONTEND}/imgui.cxx \
    ${SRC_FRONTEND}/imgui_themes.cxx \
    ${SRC_FRONTEND}/imgui_demo.cxx \
    ${SRC_FRONTEND}/imgui_draw.cxx \
    ${SRC_FRONTEND}/imgui_tables.cxx \
    ${SRC_FRONTEND}/imgui_widgets.cxx

SOURCES += \
    ${SRC_BACKEND}/imgui_impl_sdl3.cxx \
    ${SRC_BACKEND}/imgui_impl_opengl3.cxx


# todo : embed the DroidSans font
#    ${SOURCES_DIR}/${SRC_FRONTEND}/droidsans.cxx


# works but looks buggy (only the first item of the list contains the right prefix)
# that's the reason why the rule is slightly modified in the final collect
OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))
D_OBJS = $(addsuffix .do, $(basename $(notdir $(SOURCES))))

##---------------------------------------------------------------------
## BUILD RULES
##---------------------------------------------------------------------

#%.o:${SOURCES_DIR}/%.cxx
#	$(CXX) $(CXX_STD) ${EXTRA_CXX_FLAGS} ${CXX_FLAGS} -c -o ${BUILD_DIR}/$@ $<

#%.do:${SOURCES_DIR}/%.cxx
#	$(CXX) $(CXX_STD) ${CXX_FLAGS} ${CXX_FLAGS_DEBUG} ${EXTRA_CXX_FLAGS} ${CXX_FLAGS} ${CXX_FLAGS_DEBUG} -c -o ${BUILD_DIR}/$@ $<

%.o:${SOURCES_DIR}/%.cxx
	$(CXX) $(CXX_STD) ${CXX_FLAGS} ${EXTRA_CXX_FLAGS} $(CXXFLAGS) -c -o ${BUILD_DIR}/$@ $<

%.o:${SRC_BACKEND}/%.cxx
	$(CXX) $(CXX_STD) ${CXX_FLAGS} ${EXTRA_CXX_FLAGS} $(CXXFLAGS) -c -o ${BUILD_DIR}/$@ $<

%.o:${SRC_FRONTEND}/%.cxx
	$(CXX) $(CXX_STD) ${CXX_FLAGS} ${EXTRA_CXX_FLAGS} $(CXXFLAGS) -c -o ${BUILD_DIR}/$@ $<

%.do:${SOURCES_DIR}/%.cxx
	$(CXX) $(CXX_STD) ${CXX_FLAGS} ${CXX_FLAGS_DEBUG} ${EXTRA_CXX_FLAGS} $(CXXFLAGS) -c -o ${BUILD_DIR}/$@ $<

%.do:${SRC_BACKEND}/%.cxx
	$(CXX) $(CXX_STD) ${CXX_FLAGS} ${CXX_FLAGS_DEBUG} ${EXTRA_CXX_FLAGS} $(CXXFLAGS) -c -o ${BUILD_DIR}/$@ $<

%.do:${SRC_FRONTEND}/%.cxx
	$(CXX) $(CXX_STD) ${CXX_FLAGS} ${CXX_FLAGS_DEBUG} ${EXTRA_CXX_FLAGS} $(CXXFLAGS) -c -o ${BUILD_DIR}/$@ $<

%.do:${SOURCES_DIR}/gl3w/GL/%.c
	$(CC) $(CFLAGS) -c -o ${BUILD_DIR}/$@ $<

all: ${BINARY_NAME} ${BINARY_NAME}${DEBUG_EXT}
	@echo Build complete for ${UNAME_S}

${BINARY_NAME}: ${OBJS}
	$(CXX) $(CXX_STD) ${CXX_FLAGS} ${EXTRA_CXX_FLAGS} -DNDEBUG -o $@ ${BUILD_DIR}/*.o ${LIBS}
	strip $@
	rm -f ${BUILD_DIR}/*.o

${BINARY_NAME}_debug: ${D_OBJS}
	$(CXX) $(CXX_STD) ${CXX_FLAGS} ${CXX_FLAGS_DEBUG} ${EXTRA_CXX_FLAGS} -o $@ ${BUILD_DIR}/*.do  ${LIBS}
	rm -f ${BUILD_DIR}/*.do

clean:
	echo  Effacement de : ${BINARY_NAME}*
	rm -f ${BINARY_NAME}* ${BUILD_DIR}/*.o ${BUILD_DIR}/*.do


