SHELL		= /bin/sh
MAKE		= make

CC		= cc
CFLAGS		= -DNDEBUG
LDFLAGS		= $(CFLAGS)

PRGNAM		= gleipnir_lineread
OBJ_A		= lib$(PRGNAM).a
OBJ_SO		= lib$(PRGNAM).so
OBJS		= $(PRGNAM).o

VERSION		= 140617
MANCAT		= 7
MANTITLE	= Gleipnir Lineread
MANS		= $(PRGNAM)_eng.$(MANCAT)\
		$(PRGNAM)_swe.$(MANCAT)

CLANG		= scan-build
CPPCHECK	= cppcheck --enable=all
GCC		= gcc -g -std=c89\
		-pedantic-errors -Waggregate-return -Wall\
		-Wbad-function-cast -Wcast-align -Wcast-qual -Wcomment\
		-Wconversion -Wdeclaration-after-statement\
		-Wdisabled-optimization -Wdouble-promotion -Werror\
		-Wextra -Wfloat-equal -Wformat=2\
		-Wframe-larger-than=128 -Winline -Winit-self\
		-Winvalid-pch -Wjump-misses-init -Wlarger-than=128\
		-Wlogical-op -Wlong-long -Wmissing-declarations\
		-Wmissing-format-attribute -Wmissing-include-dirs\
		-Wmissing-prototypes -Wnested-externs\
		-Wold-style-declaration -Wold-style-definition -Wpacked\
		-Wredundant-decls -Wshadow -Wstack-protector\
		-Wstack-usage=128 -Wstrict-aliasing -Wstrict-overflow=5\
		-Wstrict-prototypes -Wswitch-default -Wswitch-enum\
		-Wsync-nand -Wtrampolines -Wundef -Wuninitialized\
		-Wunreachable-code -Wunsafe-loop-optimizations -Wunused\
		-Wvector-operation-performance -Wvla -Wwrite-strings
LINT		= splint -nof -strict -strict-lib -ansi89limits\
		-exportany -systemdirerrors -bounds +likelybounds

LINE		= gleipnir_line
LINE_A		= lib$(LINE).a
LINE_SO		= lib$(LINE).so
LINE_DIR	= ../$(LINE)

LINES		= gleipnir_lines
LINES_A		= lib$(LINES).a
LINES_SO	= lib$(LINES).so
LINES_DIR	= ../$(LINES)

INC		= -I.\
		-I$(LINE_DIR)\
		-I$(LINES_DIR)
LIB		= -L$(LINE_DIR)\
		-L$(LINES_DIR)
LDLIBS		=

all: static

dynamic: $(OBJ_SO)

static: $(OBJ_A)

$(OBJ_SO): $(OBJS)
	$(CC) $(LDFLAGS) $(LDLIBS) $(LIB) $(INC)\
		-shared -Wl,-soname,$(OBJ_SO) -o $(OBJ_SO) $(OBJS)

$(OBJ_A): $(OBJS)
	ar rcs $(OBJ_A) $(OBJS)

$(PRGNAM).o: $(PRGNAM).c $(PRGNAM).h
	$(CC) $(CFLAGS) $(INC) -fpic -c -o $(PRGNAM).o $(PRGNAM).c

$(LINE_DIR)/$(LINE_SO): FORCE
	cd $(LINE_DIR)		&& $(MAKE) dynamic

$(LINE_DIR)/$(LINE_A): FORCE
	cd $(LINE_DIR)		&& $(MAKE) static

$(LINES_DIR)/$(LINES_SO): FORCE
	cd $(LINES_DIR)		&& $(MAKE) dynamic

$(LINES_DIR)/$(LINES_A): FORCE
	cd $(LINES_DIR)		&& $(MAKE) static

clean:
	rm -f $(OBJS)

mrproper: clean
	rm -f $(OBJ_A) $(OBJ_SO)

clean-roff:
	rm -f $(MANS)

clang:
	$(CLANG) $(CC) -g -std=c99 -O0 $(INC) -c *.c

cppcheck:
	$(CPPCHECK) $(INC) *.c

gcc:
	$(GCC) $(INC) -fpic -c -o $(PRGNAM).o $(PRGNAM).c

lint:
	$(LINT) $(INC) -systemdirs\
		"/usr/include:$(LINE_DIR):$(LINES_DIR)"\
		*.c

roff: $(MANS)

$(PRGNAM)_eng.$(MANCAT): $(PRGNAM)_eng.txt
	cat $(PRGNAM)_eng.txt\
		| ../.cman.sh	-c "$(MANCAT)" -t "$(MANTITLE)"\
				-p "$(PRGNAM)" -v "$(VERSION)"\
		> $(PRGNAM)_eng.$(MANCAT)

$(PRGNAM)_swe.$(MANCAT): $(PRGNAM)_swe.txt
	cat $(PRGNAM)_swe.txt\
		| ../.cman.sh	-c "$(MANCAT)" -t "$(MANTITLE)"\
				-p "$(PRGNAM)" -v "$(VERSION)"\
		> $(PRGNAM)_swe.$(MANCAT)

.PHONY: all clean mrproper\
	clean-roff roff\
	dynamic static\
	clang cppcheck gcc lint

FORCE:

