# UDPS C client library — standalone, no MARTe2, no external dependencies.
#
#   make           build libudpsclient.a and the example
#   make example   build only the example
#   make cxxcheck  verify the header is usable from C++
#   make clean

CC      ?= cc
CXX     ?= c++
AR      ?= ar
CFLAGS  ?= -O2 -g
WARN     = -Wall -Wextra -Wpedantic
STD      = -std=c99
CPPFLAGS += -I.

# Old glibc (< 2.17) keeps clock_gettime in librt; harmless to add there.
LDLIBS ?=

LIB     = libudpsclient.a
OBJ     = udps_client.o
EXAMPLE = udps_dump

.PHONY: all example cxxcheck clean

all: $(LIB) $(EXAMPLE)

$(LIB): $(OBJ)
	$(AR) rcs $@ $^

udps_client.o: udps_client.c udps_client.h
	$(CC) $(STD) $(WARN) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

example: $(EXAMPLE)

$(EXAMPLE): example/udps_dump.c $(LIB)
	$(CC) $(STD) $(WARN) $(CFLAGS) $(CPPFLAGS) -o $@ $< $(LIB) $(LDLIBS)

# The header is C++-safe; this target keeps it that way.
cxxcheck: udps_client.h
	echo '#include "udps_client.h"' > .cxxcheck.cpp
	echo 'int main() { udps_client_config_t c; udps_client_config_init(&c); return 0; }' >> .cxxcheck.cpp
	$(CXX) -std=c++11 -Wall -Wextra $(CPPFLAGS) -o .cxxcheck .cxxcheck.cpp $(LIB) $(LDLIBS)
	./.cxxcheck && rm -f .cxxcheck .cxxcheck.cpp

clean:
	rm -f $(LIB) $(OBJ) $(EXAMPLE) .cxxcheck .cxxcheck.cpp
