ARTIFACT = RCOCI

#Build architecture/variant string, possible values: x86, armv7le, etc...
PLATFORM ?= aarch64le

#Build profile, possible values: release, debug, profile, coverage
BUILD_PROFILE ?= debug

CONFIG_NAME ?= $(PLATFORM)-$(BUILD_PROFILE)
OUTPUT_DIR = build/$(CONFIG_NAME)
TARGET = $(OUTPUT_DIR)/$(ARTIFACT)

#Compiler definitions

CC = qcc -Vgcc_nto$(PLATFORM) -D _TSCP_
CXX = qcc -lang-c++ -Vgcc_nto$(PLATFORM) -D _TSCP_
LD = $(CC)

#User defined include/preprocessor flags and libraries

#INCLUDES += -I/path/to/my/lib/include
#INCLUDES += -I../mylib/public
INCLUDES += -I ./src/commoncode/common \
            -I ./src/commoncode/commoncode_config_vxworks \
            -I ./src/dqu_common/dqu_idmap_common \
            -I ./src/dqu_common/dqu_emap_common \
            -I ./src/dqu_common/dqu_dfs_common \
            -I ./src/dqu_common/dqu_cbtc_common \
            -I .\src\protocol\protocol_cbtc\common_protocol_config \
            -I .\src\protocol\protocol_cbtc\common_dqu_protocol \
            -I .\src\protocol\protocol_cbtc\common_protocol \
			-I .\src\protocol\protocol_cbtc\common_protocol_sfp_rsspi-config \
            -I .\src\protocol\redun_record_cbtc\common_dqu_rp \
            -I .\src\protocol\redun_record_cbtc\common_redun \
            -I .\src\protocol\redun_record_cbtc\common_redun_record_config \
            -I .\src\protocol\rsr_record_cbtc\common_dqu_rsr \
            -I .\src\protocol\rsr_record_cbtc\common_rsr\rssp \
            -I .\src\protocol\rsr_record_cbtc\common_rsr\sig \
            -I .\src\protocol\rsr_record_cbtc\common_rsr \
            -I .\src\protocol\rsr_record_cbtc\common_rsr_record_config \
            -I .\src\protocol\rssp_record_cbtc1\common_dqu_rssp \
            -I .\src\protocol\rssp_record_cbtc1\common_rssp \
            -I .\src\protocol\rssp_record_cbtc1\common_rssp_record_config \
            -I .\src\protocol\rssp2_record_cbtc\dqu_rsspii_cbtc \
            -I .\src\protocol\rssp2_record_cbtc\rsspii_cbtc \
            -I .\src\protocol\rssp2_record_cbtc\rsspii_record_config_cbtc \
            -I .\src\protocol\sfp_record_cbtc1\common_dqu_sfp \
            -I .\src\protocol\sfp_record_cbtc1\common_sfp \
            -I .\src\protocol\sfp_record_cbtc1\common_sfp_nr_config \
            -I .\src\protocol\fsfb\common_dqu_fsfb \
			-I .\src\protocol\fsfb\common_fsfb \
            -I ./src/DRV \
            -I ./src/MSCP \
            -I ./src/MSCP/CM \
            -I ./src/MSCP/DFM \
            -I ./src/MSCP/CFM \
            -I ./src/MSCP/SFM \
            -I ./src/MSCP/PFM \
            -I ./src/MSCP_CFG \
            -I ./src/APP \
            -I ./src/APP/ciapp/2oo2 \
            -I ./src/APP/ciapp/AllAutoReturn \
            -I ./src/APP/ciapp/AutoReturn \
            -I ./src/APP/ciapp/AutoRoute \
            -I ./src/APP/ciapp/DataManage \
            -I ./src/APP/ciapp/DebugPrint \
            -I ./src/APP/ciapp/DiffLineDev \
            -I ./src/APP/ciapp/ERROR \
            -I ./src/APP/ciapp/ESB \
            -I ./src/APP/ciapp/FloodGate \
            -I ./src/APP/ciapp/Garagedoor \
            -I ./src/APP/ciapp/HLHTMsgHead \
            -I ./src/APP/ciapp/Indicator \
            -I ./src/APP/ciapp/Initial \
            -I ./src/APP/ciapp/InvadeSection \
            -I ./src/APP/ciapp/IO \
            -I ./src/APP/ciapp/LightTestButtonManage \
            -I ./src/APP/ciapp/LogicManage \
            -I ./src/APP/ciapp/LogicProcess \
            -I ./src/APP/ciapp/LogRecord \
            -I ./src/APP/ciapp/Main \
            -I ./src/APP/ciapp/Mask \
            -I ./src/APP/ciapp/NonRoute \
            -I ./src/APP/ciapp/Overlap \
            -I ./src/APP/ciapp/ParsePack \
            -I ./src/APP/ciapp/PSC \
            -I ./src/APP/ciapp/PSD \
            -I ./src/APP/ciapp/QueryFiles \
            -I ./src/APP/ciapp/Relay \
            -I ./src/APP/ciapp/ReverseRoute \
            -I ./src/APP/ciapp/Route \
            -I ./src/APP/ciapp/Section \
            -I ./src/APP/ciapp/Signal \
            -I ./src/APP/ciapp/SPKS \
            -I ./src/APP/ciapp/Switch \
            -I ./src/APP/ciapp/System \
            -I ./src/APP/ciapp/Transmit \
            -I ./src/APP/ciapp/VirtualDevice \
            -I ./src/APP/ciapp/WashMachine \
            -I ./src/APP/PU13_integration_config \
            -I ./src/APP/ciapp/AxisSort \
            -I ./src/APP/ciapp/Train \
            -I ./src/APP/ciapp/EnvelopeManage \
            -I ./src/APP/ciapp/CutoffPoint \
            -I ./src/APP/ciapp/SectionStateProcess \
            -I ./src/APP/ciapp/AxisAssert \
            -I ./src/APP/ciapp/ReverseCut \
            -I ./src/APP/ciapp/WLScreen \
            -I ./src/APP/ciapp/WLBrake \
            -I ./src/APP/ciapp/RemoteRM \
            -I ./src/APP/ciapp/NonAutoReturn \
            -include stdarg.h

#LIBS += -L/path/to/my/lib/$(PLATFORM)/usr/lib -lmylib
#LIBS += -L../mylib/$(OUTPUT_DIR) -lmylib

#Compiler flags for build profiles
CCFLAGS_release += -O0
CCFLAGS_debug += -g -O0 -fno-builtin
CCFLAGS_coverage += -g -O0 -ftest-coverage -fprofile-arcs -nopipe -Wc,-auxbase-strip,$@
LDFLAGS_coverage += -ftest-coverage -fprofile-arcs
CCFLAGS_profile += -g -O0 -finstrument-functions
LIBS_profile += -lprofilingS

#Generic compiler flags (which include build type flags)
CCFLAGS_all += -w -fmessage-length=0
CCFLAGS_all += $(CCFLAGS_$(BUILD_PROFILE))
#Shared library has to be compiled with -fPIC
#CCFLAGS_all += -fPIC
LDFLAGS_all += -l socket -l m 
LDFLAGS_all += $(LDFLAGS_$(BUILD_PROFILE))
LIBS_all += $(LIBS_$(BUILD_PROFILE))
DEPS = -Wp,-MMD,$(@:%.o=%.d),-MT,$@

#Macro to expand files recursively: parameters $1 -  directory, $2 - extension, i.e. cpp
rwildcard = $(wildcard $(addprefix $1/*.,$2)) $(foreach d,$(wildcard $1/*),$(call rwildcard,$d,$2))

#Source list
SRCS = $(call rwildcard, src, c)

#Object files list
OBJS = $(addprefix $(OUTPUT_DIR)/,$(addsuffix .o, $(basename $(SRCS))))

#Compiling rule
$(OUTPUT_DIR)/%.o: %.c
	@mkdir -p $(dir $@)
	$(CC) -c $(DEPS) -o $@ $(INCLUDES) $(CCFLAGS_all) $(CCFLAGS) $<

#Linking rule
$(TARGET):$(OBJS)
	$(LD) -o $(TARGET) $(LDFLAGS_all) $(LDFLAGS) $(OBJS) $(LIBS_all) $(LIBS)

#Rules section for default compilation and linking
all: $(TARGET)

clean:
	rm -fr $(OUTPUT_DIR)

rebuild: clean all

#Inclusion of dependencies (object files to source and includes)
-include $(OBJS:%.o=%.d)