# Makefile template for shared library
TEST_DIR = .
C_API_DIR = ../c_src

CC = gcc # C compiler
LD = gcc # C linker
CFLAGS = -g -ggdb -fPIC -Wall -O2 -DDPI_COMPATIBILITY_VERSION_1800v2005 \
        -fstack-usage -fdata-sections -ffunction-sections -DADI_FILESYSTEM_AVAILABLE -DADI_PLATFORM_API_DLL -DADRV9001_ZCU102_OFFSET  -DADI_ADRV9001_SI_REV_C0\
        -UADI_VALIDATE_PARAMS -UADI_COMMON_VERBOSE -UADI_DYNAMIC_PROFILE_LOAD -UADI_INITIALIZE_FROM_PROFILE \
        -UADI_ADRV9001_VERBOSE -UADI_ADRV9001_ARM_VERBOSE -UADI_FPGA9001_VERBOSE \
        -U_FPGA9001_BITFIELD_VALUE_CHECK -UFPGA9001_BITFIELD_ADDR_CHECK -UFPGA9001_BITFIELD_NULL_CHECK -UFPGA9001_CHANNELID_CHECK \
        -U_ADRV9001_BITFIELD_VALUE_CHECK -UADRV9001_BITFIELD_ADDR_CHECK -UADRV9001_BITFIELD_NULL_CHECK -UADRV9001_CHANNELID_CHECK \
        -I$(TEST_DIR)

#Recursive Wildcard
rwildcard = $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))

#Do not compile or include these files and directories
IGNORE_SRC = $(C_API_DIR)/third_party/fru_tools/operating-system.c \
        $(wildcard $(C_API_DIR)/third_party/jsmn/example/*.c) \
        $(wildcard $(C_API_DIR)/third_party/jsmn/test/*.c) \
        $(wildcard $(C_API_DIR)/third_party/cliopts/*.c) \
        $(wildcard $(C_API_DIR)/devices/adrv9001/private/src/a0/*.c)

IGNORE_INCL = $(wildcard $(C_API_DIR)/third_party/jsmn/example/*.h) \
        $(wildcard $(C_API_DIR)/third_party/jsmn/test/*.h) \
        $(wildcard $(C_API_DIR)/devices/adrv9001/private/include/a0/*.h)

#Finds all header files and added the directories to CFLAGS with -I option, ignoring specified h files
DEPS = $(filter-out $(IGNORE_INCL), $(call rwildcard, $(C_API_DIR)/, *.h))
INCLUDE_DIRS = $(sort $(dir $(DEPS)))
override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(INCLUDE_DIRS)))

LDFLAGS = -Wl,--gc-sections -g # linking flags
RM = rm -f  # rm command
TARGET = $(TEST_DIR)/example # target lib

#List of all c files in C_SRC and generated c file, ignoring specified c files
SRCS = $(filter-out $(IGNORE_SRC), $(call rwildcard, $(C_API_DIR)/, *.c)) $(wildcard $(TEST_DIR)/*.c)

#Create object files of all c files
OBJS = $(SRCS:.c=.o)

#Recipes
all: ${TARGET}

$(TARGET): $(OBJS)
	$(LD) ${LDFLAGS} -o $@ $^ -lm

.$(EXT).o:
	$(CC) $(CFLAGS) -o $@ -c $<

.PHONY : clean          #prevents make from getting confused by an actual file called clean

clean:
	rm -rf ${TARGET} ${OBJS} $(OBJS:.o=.su) $(TEST_DIR)/example

