# By default, recipe steps will be quieted.
# But a user can supply VERBOSE=1 as an environment variable or command line argument
# to re-enable build output.
VERBOSE ?= 0
ifeq ($(VERBOSE),1)
Q =
VERBOSE = 1
else
Q = @
VERBOSE = 0
endif

CFLAGS = -Wall -Wextra -I include
STATIC_LIB_FLAGS = rcs
DEPFLAGS = -MT $@ -MMD -MP -MF $*.d

.PHONY: all
all: multifile-program-distributed

APP_SOURCES := src/multifile_main.c
LIB_SOURCES := src/lib/multifile_func.c
LIB_OBJECTS := $(LIB_SOURCES:.c=.o)
APP_OBJECTS := $(APP_SOURCES:.c=.o)
DEPFILES := $(LIB_SOURCES:.c=.d) $(APP_SOURCES:.c=.d)

%.o: %.c
%.o: %.c %.d
	$(Q)$(CC) $(CFLAGS) $(DEPFLAGS) -c $< -o $@

libmultifile_func.a: $(LIB_OBJECTS)
	$(Q)$(AR) $(STATIC_LIB_FLAGS) $@ $^

multifile-program-distributed: libmultifile_func.a $(APP_OBJECTS)
	$(Q)$(CC) $(CFLAGS) $(APP_OBJECTS) -L. -lmultifile_func -o $@

clean:
	$(Q)$(RM) multifile-program-distributed
	$(Q)$(RM) *.a
	$(Q)$(RM) *.o src/*.o src/lib/*.o
	$(Q)$(RM) *.d src/*.d src/lib/*.d

$(DEPFILES):

include $(wildcard $(DEPFILES))
