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
	CFLAGS = -Wpedantic -std=c90
endif

INCLUDES = -Iinclude
CFLAGS += $(QNX_ENUMS_AS_INTS)
DEBUG = -g
LDFLAGS = -g
CFLAGS += -c $(DEBUG)
BINDIR = build
OBJDIR = build/obj

# SOURCES = \
#     src/vn/conv.c \
#     src/vn/error.c \
#     src/vn/error_detection.c \
#     src/vn/sensors.c \
#     src/vn/util.c \
#     src/vn/math/matrix.c \
#     src/vn/math/vector.c \
#     src/vn/protocol/spi.c \
#     src/vn/protocol/upack.c \
#     src/vn/protocol/upackf.c \
#     src/vn/sensors/compositedata.c \
#     src/vn/sensors/ezasyncdata.c \
#     src/vn/sensors/searcher.c \
#     src/vn/xplat/criticalsection.c \
#     src/vn/xplat/event.c \
#     src/vn/xplat/serialport.c \
#     src/vn/xplat/thread.c \
#     src/vn/xplat/time.c

# Contents from the VN Programming Library we use for comms with the VN100
SOURCES = \
    src/vn/conv.c \
    src/vn/error.c \
    src/vn/error_detection.c \
    src/vn/util.c \
    src/vn/math/matrix.c \
    src/vn/math/vector.c \
    src/vn/protocol/spi.c \
    src/vn/protocol/upack.c \
    src/vn/protocol/upackf.c \
    src/vn/xplat/time.c

# Contents of the VN HSI Calibration Library we use for Hard/Soft Iron Cal
SOURCES += src/vn/hsi/vnamath.c \
    src/vn/hsi/vnhsi2d.c \
    src/vn/hsi/vnhsi3d.c \
    src/vn/hsi/vnhsi.c \
    src/vn/hsi/vnhsidipole.c \
    src/vn/hsi/vnhsimath.c \
    src/vn/hsi/vnhsipatches.c
				
# Set the object file names, with the source directory stripped
# from the path, and the build path prepended in its place			
OBJECTS = $(SOURCES:src/%.c=$(OBJDIR)/%.o)

all: dirs lib

lib: libvnc.a

libvnc.a: $(OBJECTS)
	$(AR_CMD) $(BINDIR)/libvnc.a $(OBJECTS)
	# ar -r -v $(BINDIR)/libvnc.a $(OBJECTS)

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

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

clean:
	rm -f $(BINDIR)/libvnc.*
	rm -f $(OBJECTS)
