Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ CFLAGS ?= $(STD) -pedantic -O3 -g -Wall -Werror -Wextra -Wformat=2 -Wshadow \
TESTS = $(wildcard tests/*.c)
EXAMPLES = $(wildcard examples/*.c)
EXAMPLESEXE = $(EXAMPLES:.c=)
DEPFILES = $(DIST)/$(PROJ).d

.PHONY: all check clean libs $(DIST)/$(PROJ).pc

all: $(EXAMPLESEXE) check libs $(DIST)/$(PROJ).pc

$(DIST)/.dirstamp:
-include $(DEPFILES)

$(DIST)/.dirstamp:
$(MKDIR) $(DIST)
$(MKDIR) $(DIST)/examples
touch $@
touch $@

check: $(DIST)/.dirstamp $(DIST)/test-file $(DIST)/test-static $(DIST)/test-dynamic
check: $(DIST)/.dirstamp $(DIST)/test-file $(DIST)/test-static $(DIST)/test-dynamic
./$(DIST)/test-file
./$(DIST)/test-static
LD_LIBRARY_PATH=$(DIST) ./$(DIST)/test-dynamic
Expand All @@ -36,33 +39,35 @@ $(DIST)/test-dynamic: $(TESTS) $(DIST)/lib$(PROJ).so $(PROJ).h tests/ptest.h
$(DIST)/test-static: $(TESTS) $(DIST)/lib$(PROJ).a $(PROJ).h tests/ptest.h
$(CC) $(filter-out -Werror, $(CFLAGS)) $(TESTS) -lm -L$(DIST) -l$(PROJ) -static -o $(DIST)/test-static

examples/%: $(DIST)/.dirstamp examples/%.c $(PROJ).c $(PROJ).h
$(CC) $(CFLAGS) $(filter-out $(DIST)/.dirstamp $(PROJ).h, $^) -lm -o $(DIST)/$@
examples/%: $(DIST)/.dirstamp examples/%.c $(PROJ).c $(PROJ).h
$(CC) $(CFLAGS) $(filter-out $(DIST)/.dirstamp $(PROJ).h, $^) -lm -o $(DIST)/$@

$(DIST)/lib$(PROJ).so: $(DIST)/.dirstamp $(PROJ).c $(PROJ).h
$(DIST)/lib$(PROJ).so: $(DIST)/.dirstamp $(PROJ).c $(PROJ).h
ifneq ($(OS),Windows_NT)
$(CC) $(CFLAGS) -fPIC -shared $(PROJ).c -o $(DIST)/lib$(PROJ).so
else
$(CC) $(CFLAGS) -shared $(PROJ).c -o $(DIST)/lib$(PROJ).so
endif

$(DIST)/lib$(PROJ).a: $(DIST)/.dirstamp $(PROJ).c $(PROJ).h
$(CC) $(CFLAGS) -c $(PROJ).c -o $(DIST)/$(PROJ).o
$(AR) rcs $(DIST)/lib$(PROJ).a $(DIST)/$(PROJ).o
$(DIST)/$(PROJ).o: $(PROJ).c $(PROJ).h $(DIST)/.dirstamp
$(CC) $(CFLAGS) -MMD -MT $@ -MF $(DIST)/$(PROJ).d -c $< -o $@

$(DIST)/lib$(PROJ).a: $(DIST)/$(PROJ).o
$(AR) rcs $@ $^

libs: $(DIST)/lib$(PROJ).so $(DIST)/lib$(PROJ).a

$(DIST)/$(PROJ).pc: $(DIST)/.dirstamp $(PROJ).pc
$(DIST)/$(PROJ).pc: $(DIST)/.dirstamp $(PROJ).pc
cp $(PROJ).pc $(DIST)/$(PROJ).pc
sed -i '1i\prefix=$(PREFIX)/' $(DIST)/$(PROJ).pc

clean:
rm -rf -- $(DIST)

install: all
install -d -m645 $(DESTDIR)$(PREFIX)/include
install -d -m645 $(DESTDIR)$(PREFIX)/lib/pkgconfig
install -d -m645 $(DESTDIR)$(PREFIX)/share/$(PROJ)
install -d -m645 $(DESTDIR)$(PREFIX)/include
install -d -m645 $(DESTDIR)$(PREFIX)/lib/pkgconfig
install -d -m645 $(DESTDIR)$(PREFIX)/share/$(PROJ)
install -m755 -t $(DESTDIR)$(PREFIX)/lib $(DIST)/lib*
install -m644 -t $(DESTDIR)$(PREFIX)/share/$(PROJ) $(PROJ).c $(PROJ).h
install -m644 $(PROJ).h $(DESTDIR)$(PREFIX)/include/$(PROJ).h
Expand Down