
WARNFLAGS = \
        -Wformat -Wall -Wundef -Wpointer-arith -Wcast-qual \
        -Wcast-align -Wwrite-strings -Wsign-compare \
        -Wmissing-noreturn \
        -Wextra -Wstrict-aliasing=2 \
        -Wunsafe-loop-optimizations \
		-Wuninitialized

# Compiler flags for generating dependencies
DEPFLAGS = -MMD -MP
CFLAGS ?= -O3
# CFLAGS ?= -O0 -g
# LDFLAGS ?= -g
ALLCFLAGS = -std=gnu99 -fPIC $(WARNFLAGS) $(DEPFLAGS) $(CFLAGS)

# declarations

RAINBOW_OBJS = nss-rainbow.o buf.o nat.o slist.o
UIDS_OBJS = uids.o
GIDS_OBJS = gids.o
TEST_SLIST_OBJS = test_slist.o slist.o
TEST_ENDGRENT_OBJS = test_endgrent.o
TEST_NAT_OBJS = test_nat.o nat.o
TEST_FORMAT_BUF_OBJS = test_format_buf.o buf.o
ALL_OBJS = $(RAINBOW_OBJS) $(UIDS_OBJS) $(GIDS_OBJS) $(TEST_SLIST_OBJS) $(TEST_NAT_OBJS)
BINARIES = libnss_rainbow.so.2 uids gids test_slist test_nat test_endgrent test_format_buf

# if you invoke make as 'make V=1' it will verbosely report on what it
# is doing, otherwise it defaults to quiet/pretty mode, which makes
# errors _much_ easier to see
ifneq ($V, 1)
MAKEFLAGS = -s
endif


# targets

all: $(BINARIES)

clean:
	rm -f $(BINARIES) *.d *.o

install:
	install -D -m 0755 libnss_rainbow.so.2 $(LIBDIR)/libnss_rainbow.so.2

# objects

%.o: %.c
	@echo "  CC    $@"
	$(CC) $(ALLCFLAGS) -c -o $@ $<


# linked binaries

test_endgrent: $(TEST_ENDGRENT_OBJS)
	@echo "  LD    $@"
	$(CC) $(ALLCFLAGS) -o $@ $^

test_slist: $(TEST_SLIST_OBJS)
	@echo "  LD    $@"
	$(CC) $(ALLCFLAGS) -o $@ $^

test_nat: $(TEST_NAT_OBJS)
	@echo "  LD    $@"
	$(CC) $(ALLCFLAGS) -o $@ $^

test_format_buf: $(TEST_FORMAT_BUF_OBJS)
	@echo "  LD    $@"
	$(CC) $(ALLCFLAGS) -o $@ $^

uids: $(UIDS_OBJS)
	@echo "  LD    $@"
	$(CC) $(ALLCFLAGS) -o $@ $^

gids: $(GIDS_OBJS)
	@echo "  LD    $@"
	$(CC) $(ALLCFLAGS) -o $@ $^

libnss_rainbow.so.2: $(RAINBOW_OBJS)
	@echo "  LD    $@"
	$(CC) -shared $(LDFLAGS) -o $@ -Wl,-soname,$@ $^


.PHONY: clean install

-include $(ALL_OBJS:%.o=%.d)

# vim: noet sts=4 ts=4 sw=4 :
