# some commands and settings
#SLASH		:=	\\
#RM		:=	del /Q
#CP		:=	copy

SLASH		:=	/
RM		:=	rm -f
CP		:=	cp


MODS		:=	gui file net

SRC_gui		:=	gui.c window.c

SRC_file	:=	file.c stat.c

SRC_net		:=	net.c socks.c


# in case the user did not define it
ifndef PROJECT
PROJECT		:=	stupid
endif

CC		:=	gcc
LD		:=	gcc


all: final.elf

final.elf: $(OBJ) $(DEP)
	$(LD) -o $@ $(OBJ)

# don't do any automatic rules
.SUFFIXES:

# the targets
.PHONY: dep all clean mrproper obj


# to let "make" find all the sources
VPATH =	$(patsubst %,src/%/SRC,$(MODS))

SRC =	$(foreach mod,$(MODS),$(foreach src,$(SRC_$(mod)),$(mod)/SRC/$(src)))

DEP =	$(patsubst %.c,dep/%.d, $(foreach src,$(SRC),$(notdir $(src))))
OBJ =	$(patsubst %.c,obj/%.o, $(foreach src,$(SRC),$(notdir $(src))))

MODPATH = $(patsubst %,src/%/SRC,$(MODS))
IMODPATH = $(patsubst %,-i %,$(MODPATH))

dep: $(DEP)

obj: $(OBJ)

# the rule to create the dependencies
$(DEP): dep/%.d : %.c
	perl perl/autodep.pl	\
		-s $< \
		-o $@ \
		$(IMODPATH) \
		-objdir obj \
		-depdir dep \
		-eo o \
		-ed d

# include the dependencies.  This has to be written AFTER the rule to
# generate them, why ???
include $(DEP)

$(OBJ): obj/%.o : %.c
	$(CC) -o $@ -c $<

clean:
	-$(RM) $(subst /,$(SLASH),$(DEP))
	-$(RM) $(subst /,$(SLASH),$(OBJ))

show:
	@echo "SRC"
	@echo $(SRC)
	@echo "DEP"
	@echo $(DEP)
	@echo "VPATH"
	@echo $(VPATH)
	@echo "MODPATH"
	@echo $(MODPATH)
	@echo "OS"
	@echo $(OS)
	@echo "HOST"
	@echo $(HOST)
	@echo "OBJ"
	@echo $(OBJ)

