From 1af6acb23d4b00f02a6b520562258e8bb7e38d2b Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 19 Feb 2022 21:05:57 +0100 Subject: Improve makefile MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 60 ++++++++++++++++++++++++++++++++++++++--------------------- config.mk | 4 ++-- mk/linux.mk | 7 +++++++ mk/macos.mk | 7 +++++++ mk/windows.mk | 7 +++++++ 5 files changed, 62 insertions(+), 23 deletions(-) create mode 100644 mk/linux.mk create mode 100644 mk/macos.mk create mode 100644 mk/windows.mk diff --git a/Makefile b/Makefile index ac15b3a..ffa9da4 100644 --- a/Makefile +++ b/Makefile @@ -3,51 +3,69 @@ CONFIGFILE = config.mk include $(CONFIGFILE) -VERSION_MAJOR = 1 -VERSION = 1.2 +OS = linux +# Linux: linux +# Mac OS: macos +# Windows: windows +include mk/$(OS).mk -all: libclut.a libclut.so test +LIB_MAJOR = 1 +LIB_MINOR = 0 +LIB_VERSION = $(LIB_MAJOR).$(LIB_MINOR) -libclut.o: libclut.c libclut.h - $(CC) -c -o $@ libclut.c $(CPPFLAGS) $(CFLAGS) -libclut.lo: libclut.c libclut.h - $(CC) -fPIC -c -o $@ libclut.c $(CPPFLAGS) $(CFLAGS) +HDR = libclut.h +OBJ = libclut.o -libclut.a: libclut.o - $(AR) rc $@ libclut.o - $(AR) s $@ +LOBJ = $(OBJ:.o=.lo) -libclut.so: libclut.lo - $(CC) -shared -Wl,-soname,libclut.so.$(VERSION_MAJOR) -o $@ $(LDFLAGS) libclut.lo -test.o: test.c libclut.h - $(CC) -c -o $@ test.c $(CPPFLAGS) $(CFLAGS) +all: libclut.a libclut.$(LIBEXT) test +$(OBJ): $(HDR) +$(LOBJ): $(HDR) + +.c.o: + $(CC) -c -o $@ $< $(CFLAGS) $(CPPFLAGS) + +.c.lo: + $(CC) -fPIC -c -o $@ $< $(CFLAGS) $(CPPFLAGS) test: test.o libclut.a $(CC) -o $@ test.o libclut.a $(LDFLAGS) +libclut.$(LIBEXT): $(LOBJ) + $(CC) $(LIBFLAGS) -o $@ $(LOBJ) $(LDFLAGS) + +libclut.a: $(OBJ) + -rm -f -- $@ + $(AR) rc $@ $(OBJ) + $(AR) -s $@ + check: test ./test -install: libclut.a libclut.so +install: libclut.a libclut.$(LIBEXT) mkdir -p -- "$(DESTDIR)$(PREFIX)/lib" mkdir -p -- "$(DESTDIR)$(PREFIX)/include" cp -- libclut.a "$(DESTDIR)$(PREFIX)/lib" - cp -- libclut.so "$(DESTDIR)$(PREFIX)/lib/libclut.so.$(VERSION_MAJOR)" - ln -sf -- libclut.so.$(VERSION_MAJOR) "$(DESTDIR)$(PREFIX)/lib/libclut.so.$(VERSION)" - ln -sf -- libclut.so.$(VERSION_MAJOR) "$(DESTDIR)$(PREFIX)/lib/libclut.so" + cp -- libclut.so "$(DESTDIR)$(PREFIX)/lib/libclut.$(LIBMINOREXT)" + $(FIX_INSTALL_NAME) -- "$(DESTDIR)$(PREFIX)/lib/libclut.$(LIBMINOREXT)" + ln -sf -- "libclut.$(LIBMINOREXT)" "$(DESTDIR)$(PREFIX)/lib/libclut.$(LIBMAJOREXT)" + ln -sf -- "libclut.$(LIBMINOREXT)" "$(DESTDIR)$(PREFIX)/lib/libclut.$(LIBEXT)" cp -- libclut.h "$(DESTDIR)$(PREFIX)/include" uninstall: -rm -f -- "$(DESTDIR)$(PREFIX)/lib/libclut.a" - -rm -f -- "$(DESTDIR)$(PREFIX)/lib/libclut.so.$(VERSION_MAJOR)" - -rm -f -- "$(DESTDIR)$(PREFIX)/lib/libclut.so.$(VERSION)" - -rm -f -- "$(DESTDIR)$(PREFIX)/lib/libclut.so" + -rm -f -- "$(DESTDIR)$(PREFIX)/lib/libclut.so.$(LIBEXT)" + -rm -f -- "$(DESTDIR)$(PREFIX)/lib/libclut.so.$(LIBMAJOREXT)" + -rm -f -- "$(DESTDIR)$(PREFIX)/lib/libclut.so.$(LIBMINOREXT)" -rm -f -- "$(DESTDIR)$(PREFIX)/include/libclut.h" clean: -rm -f -- *.o *.a *.so *.lo *.su test +.SUFFIXES: +.SUFFIXES: .lo .o .c + .PHONY: all check install uninstall clean diff --git a/config.mk b/config.mk index 9b1bc39..9aa4de7 100644 --- a/config.mk +++ b/config.mk @@ -1,8 +1,8 @@ PREFIX = /usr MANPREFIX = $(PREFIX)/share/man -CC = cc +CC = c99 CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_GNU_SOURCE -CFLAGS = -std=c99 -Wall -O2 +CFLAGS = -Wall -O2 LDFLAGS = -lm -s diff --git a/mk/linux.mk b/mk/linux.mk new file mode 100644 index 0000000..37ce5a6 --- /dev/null +++ b/mk/linux.mk @@ -0,0 +1,7 @@ +LIBEXT = so +LIBFLAGS = -shared -Wl,-soname,libclut.$(LIBEXT).$(LIB_MAJOR) + +LIBMAJOREXT = $(LIBEXT).$(LIB_MAJOR) +LIBMINOREXT = $(LIBEXT).$(LIB_MAJOR).$(LIB_MINOR) + +FIX_INSTALL_NAME = : diff --git a/mk/macos.mk b/mk/macos.mk new file mode 100644 index 0000000..c5636bf --- /dev/null +++ b/mk/macos.mk @@ -0,0 +1,7 @@ +LIBEXT = dylib +LIBFLAGS = -dynamiclib -Wl,-compatibility_version,$(LIB_MAJOR) -Wl,-current_version,$(LIB_VERSION) + +LIBMAJOREXT = $(LIB_MAJOR).$(LIBEXT) +LIBMINOREXT = $(LIB_MAJOR).$(LIB_MINOR).$(LIBEXT) + +FIX_INSTALL_NAME = install_name_tool -id "$(PREFIX)/lib/libclut.$(LIBMAJOREXT)" diff --git a/mk/windows.mk b/mk/windows.mk new file mode 100644 index 0000000..304b6be --- /dev/null +++ b/mk/windows.mk @@ -0,0 +1,7 @@ +LIBEXT = dll +LIBFLAGS = -shared + +LIBMAJOREXT = $(LIB_MAJOR).$(LIBEXT) +LIBMINOREXT = $(LIB_MAJOR).$(LIB_MINOR).$(LIBEXT) + +FIX_INSTALL_NAME = : -- cgit v1.2.3-70-g09d2