CC = cc

ifeq "$(OSTYPE)" "qnx423"
	AR 	    = wlib
	AR_CMD = $(AR) -n
	QNX_ENUMS_AS_INTS = -Wc,-ei
else
	AR = ar
	AR_CMD = $(AR) -r -v
endif

INCLUDES = \
	-Isrc \
	-Iinclude 

CFLAGS = -Wpedantic -std=c90 $(QNX_ENUMS_AS_INTS)
DEBUG = -g
LDFLAGS = -g
CFLAGS += -c $(DEBUG)
BINDIR = build
OBJDIR = build/obj


# SOURCES = $(wildcard src/*.c) $(wildcard src/*.c)

# Only a subset of the sources are needed (and can be built on QNX), so 
# rather than building everything, take only what we need.
SOURCES = \
	src/vnamath.c \
	src/vnenum.c \
	src/vnerrdet.c \
	src/vnhsi2d.c \
	src/vnhsi3d.c \
	src/vnhsi.c \
	src/vnhsidipole.c \
	src/vnhsimath.c \
	src/vnhsipatches.c \
	src/vnmatrix.c \
	src/vnstring.c \
	src/vntime.c \
	src/vnvector.c

# Set the object file names, with the source directory stripped
# from the path, and the build path prepended in its place			
OBJECTS = $(SOURCES:%.c=$(OBJDIR)/%.o)

all: dirs lib

lib: libvnhsic.a

libvnhsic.a: $(OBJECTS)
	$(AR_CMD) $(BINDIR)/libvnhsic.a $(OBJECTS)

# Create the directories used in the build
.PHONY: dirs
dirs:
	@mkdir -p $(OBJDIR)
	@mkdir -p $(BINDIR)
	@mkdir -p $(dir $(OBJECTS))

$(OBJDIR)/%.o: %.c
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

clean:
	rm -f $(BINDIR)/libvnhsic.a
	rm -f $(BINDIR)/*.bak
	rm -f $(OBJECTS)
