CC=gcc -fPIC -g
CFLAGS= -c -Wall 
LDFLAGS=

#defined global variables and programs to build
include Makefile.conf

#Globals Directories

# necessary to access Paolo includes to build interface to its library (contains includes and libs)
#
# used if no rpm
# PCIE40_INC_DIR=$(pcie40DaqDir)
# PCIE40_DYN_LIB=$(pcie40DaqDir)/pcie40_ecs/libpcie40_ecs.so
# PCIE40_STAT_LIB=$(pcie40DaqDir)/pcie40_ecs/libpcie40_ecs.a
# used if rpm (normally useless because default 
# but need to be defined later or build crashes because include in -I are missing
#PCIE40_INC_DIR=/usr/include/
#PCIE40_DYN_LIB=/usr/lib64
#PCIE40_STAT_LIB=/usr/lib64

PCIE40_INC_DIR=../DriverInterface

OBJ_DIR=obj/
SRC_DIR=src/
INC_DIR=inc/
LIB_DIR=lib/

#SubDirectories
ECS_DIR=ecs/
MINIPODS_DIR=minipods/
USER_DIR=user/
SYSTEM_DIR=system/
LTC2990_DIR=ltc2990/

#Files ------------------------------------------------------------------------

#ECS
ECS_SRC= $(wildcard $(SRC_DIR)$(ECS_DIR)*.c)
ECS_OBJ=$(ECS_SRC:$(SRC_DIR)$(ECS_DIR)%.c=$(OBJ_DIR)%.o)

#Minipods
MINIPODS_SRC= $(wildcard $(SRC_DIR)$(MINIPODS_DIR)*.c)
MINIPODS_OBJ=$(MINIPODS_SRC:$(SRC_DIR)$(MINIPODS_DIR)%.c=$(OBJ_DIR)%.o)

#User
USER_SRC= $(wildcard $(SRC_DIR)$(USER_DIR)*.c)
USER_OBJ=$(USER_SRC:$(SRC_DIR)$(USER_DIR)%.c=$(OBJ_DIR)%.o)

#LTC2990
LTC2990_SRC= $(wildcard $(SRC_DIR)$(LTC2990_DIR)*.c)
LTC2990_OBJ=$(LTC2990_SRC:$(SRC_DIR)$(LTC2990_DIR)%.c=$(OBJ_DIR)%.o)

# static libraries
ECS_LIB= libecs.a
MINIPODS_LIB= libminipods.a
USER_LIB= libuser.a
LTC2990_LIB= libltc2990.a
# dynamic libraries
USER_DYNLIB= libuser-dyn.so
LLI_DYNLIB= lli-dyn.so


all: $(ECS_OBJ) $(ECS_LIB) $(MINIPODS_OBJ) $(MINIPODS_LIB) $(LTC2990_OBJ) $(LTC2990_LIB) $(LLI_DYNLIB) $(USER_LIB) $(USER_DYNLIB)

$(ECS_OBJ): $(OBJ_DIR)%.o : $(SRC_DIR)$(ECS_DIR)%.c
	@echo "Construction of ecs objects $@ from $<"
	mkdir -p obj
	$(CC) $(CFLAGS)  -I $(INC_DIR)$(ECS_DIR) -I $(INC_DIR)$(SYSTEM_DIR) -I $(PCIE40_INC_DIR)  $< -o $@	

$(ECS_LIB) : $(ECS_OBJ)
	@echo "Construction of ECS Library"
	mkdir -p lib
	ar -q $(LIB_DIR)$(ECS_LIB) $(ECS_OBJ)
	@echo ""

$(MINIPODS_OBJ): $(OBJ_DIR)%.o : $(SRC_DIR)$(MINIPODS_DIR)%.c
	@echo "Construction of $@ from $<"
	$(CC) $(CFLAGS) -I $(INC_DIR)$(MINIPODS_DIR) -I $(INC_DIR)$(SYSTEM_DIR) -I $(INC_DIR)$(ECS_DIR)  -I $(PCIE40_INC_DIR) $< -o $@
	@echo ""

$(MINIPODS_LIB) : $(MINIPODS_OBJ)
	@echo "Construction of MINIPODS Library"
	ar -q $(LIB_DIR)$(MINIPODS_LIB) $(MINIPODS_OBJ)
	@echo ""

$(LTC2990_OBJ): $(OBJ_DIR)%.o : $(SRC_DIR)$(LTC2990_DIR)%.c
	@echo "Construction of $@ from $<"
	$(CC) $(CFLAGS) -I $(INC_DIR)$(PLL_DIR) -I $(INC_DIR)$(LTC2990_DIR) -I $(INC_DIR)$(SYSTEM_DIR) -I $(INC_DIR)$(MINIPODS_DIR)  -I $(INC_DIR)$(LTC2990_DIR)  -I $(PCIE40_INC_DIR) $< -o $@
	@echo ""

$(LTC2990_LIB) : $(LTC2990_OBJ)
	@echo "Construction of LTC2990 Library"
	ar -q $(LIB_DIR)$(LTC2990_LIB) $(LTC2990_OBJ)
	@echo ""

$(USER_OBJ): $(OBJ_DIR)%.o : $(SRC_DIR)$(USER_DIR)%.c
	@echo "Construction of $@ from $<"
	$(CC) $(CFLAGS) -I $(INC_DIR)$(USER_DIR) -I $(INC_DIR)$(SYSTEM_DIR) -I $(INC_DIR)$(ECS_DIR)  -I $(PCIE40_INC_DIR) $< -o $@
	@echo ""

$(USER_LIB) : $(USER_OBJ)
	@echo "Construction of USER Library"
	ar -q $(LIB_DIR)$(USER_LIB) $(USER_OBJ) $(ECS_OBJ)
	@echo ""

$(USER_DYNLIB) : $(USER_OBJ)
	@echo "Construction of dynamic USER Library"
	$(CC) -o $(LIB_DIR)$(USER_DYNLIB) -shared $(USER_OBJ) $(ECS_OBJ)
	@echo 

$(LLI_DYNLIB) : $(ECS_OBJ) $(MINIPODS_OBJ)
	@echo "Construction of dynamic LLI technical Library for V2"
# with locally built ecs library
#	$(CC) -o $(LIB_DIR)$(LLI_DYNLIB) -shared $(ECS_OBJ) $(PLL_OBJ) $(MINIPODS_OBJ) $(PCIE40_DYN_LIB)
# with rpm installed ecs library
	$(CC) -o $(LIB_DIR)$(LLI_DYNLIB) -shared $(ECS_OBJ) $(MINIPODS_OBJ) -lpcie40_ecs
	@echo 

clean: mrproper
	@echo "removing lli objects"
	-rm $(OBJ_DIR)*.o
	@echo ""

	@echo "removing $(ECS_LIB)"
	-rm $(LIB_DIR)$(ECS_LIB)
	@echo ""

	@echo "removing $(MINIPODS_LIB)"
	-rm $(LIB_DIR)$(MINIPODS_LIB)
	@echo ""

	@echo "removing $(LTC2990_LIB)"
	-rm $(LIB_DIR)$(LTC2990_LIB)
	@echo ""

	@echo "removing $(USER_LIB)"
	-rm $(LIB_DIR)$(USER_LIB)
	-rm $(LIB_DIR)$(USER_DYNLIB)
	-rm $(LIB_DIR)$(LLI_DYNLIB)

	@echo ""

mrproper:
	@echo "Removing all *~ files"
	-find . -name "*~" -exec rm {} \;
	@echo ""