From a17815f36c78c20a7b8782f65d38bd947894d679 Mon Sep 17 00:00:00 2001 From: halosghost Date: Fri, 9 Nov 2018 11:19:49 -0600 Subject: [PATCH 1/3] Dramatically update Makefile - Out-of-Tree builds (defaults to ./build, configurable via DIST) - Install and uninstall rules (which respect DESTDIR and PREFIX) - Production of static and dynamic libraries - Testing of all three methods of inclusion - direct file inclusion - static library - dynamic library (using LD_LIBRARY_PATH) - Better declaration of PHONY targets - Expose ability to override CC and CFLAGS --- Makefile | 61 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 9a45c77..c6be65f 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,9 @@ - -CC = gcc -STND=-ansi -CFLAGS = $(STND) -pedantic -O3 -g -Wall -Werror -Wextra -Wformat=2 -Wshadow \ +PROJ = mpc +CC ?= gcc +STND = -ansi +DIST = build +PREFIX ?= /usr/local +CFLAGS ?= $(STND) -pedantic -O3 -g -Wall -Werror -Wextra -Wformat=2 -Wshadow \ -Wno-long-long -Wno-overlength-strings -Wno-format-nonliteral -Wcast-align \ -Wwrite-strings -Wstrict-prototypes -Wold-style-definition -Wredundant-decls \ -Wnested-externs -Wmissing-include-dirs -Wswitch-default @@ -10,15 +12,50 @@ TESTS = $(wildcard tests/*.c) EXAMPLES = $(wildcard examples/*.c) EXAMPLESEXE = $(EXAMPLES:.c=) -all: $(EXAMPLESEXE) check +.PHONY: all check clean libs -check: $(TESTS) mpc.c - $(CC) $(filter-out -Werror, $(CFLAGS)) $^ -lm -o test - ./test +all: $(EXAMPLESEXE) check -examples/%: examples/%.c mpc.c - $(CC) $(CFLAGS) $^ -lm -o $@ +$(DIST): + mkdir -p $(DIST)/examples + +check: test-file test-static test-dynamic + ./$(DIST)/test-file + ./$(DIST)/test-static + LD_LIBRARY_PATH=$(DIST) ./$(DIST)/test-dynamic + +test-file: $(DIST) $(TESTS) $(PROJ).c + $(CC) $(filter-out -Werror, $(CFLAGS)) $(TESTS) $(PROJ).c -lm -o $(DIST)/test-file + +test-dynamic: $(DIST) $(TESTS) lib$(PROJ).so + $(CC) $(filter-out -Werror, $(CFLAGS)) $(TESTS) -lm -L$(DIST) -l$(PROJ) -o $(DIST)/test-dynamic + +test-static: $(DIST) $(TESTS) lib$(PROJ).a + $(CC) $(filter-out -Werror, $(CFLAGS)) $(TESTS) -lm -L$(DIST) -l$(PROJ) -static -o $(DIST)/test-static + +examples/%: $(DIST) examples/%.c $(PROJ).c + $(CC) $(CFLAGS) $(filter-out $(DIST), $^) -lm -o $(DIST)/$@ + +lib$(PROJ).so: $(DIST) $(PROJ).c + $(CC) $(CFLAGS) -fPIC -shared $(PROJ).c -o $(DIST)/lib$(PROJ).so + +lib$(PROJ).a: $(DIST) $(PROJ).c + $(CC) $(CFLAGS) -c $(PROJ).c -o $(DIST)/$(PROJ).o + ar rcs $(DIST)/lib$(PROJ).a $(DIST)/$(PROJ).o + +libs: lib$(PROJ).so lib$(PROJ).a clean: - rm -rf test examples/doge examples/lispy examples/maths examples/smallc \ - examples/foobar examples/tree_traversal + rm -rf -- $(DIST) + +install: all + install -d -m644 $(DESTDIR)$(PREFIX)/{include,lib,share/$(PROJ)} + install -m755 -t $(DESTDIR)$(PREFIX)/lib $(DIST)/lib* + install -m644 -t $(DESTDIR)$(PREFIX)/share/$(PROJ) $(PROJ).{c,h} + install -m644 $(PROJ).h $(DESTDIR)$(PREFIX)/include/$(PROJ).h + +uninstall: + rm -rf -- \ + $(DESTDIR)$(PREFIX)/include/$(PROJ).h \ + $(DESTDIR)$(PREFIX)/share/$(PROJ)/$(PROJ).{c,h} \ + $(DESTDIR)$(PREFIX)/lib/lib$(PROJ).{so,a} From 41f3a6de7cf1f7346d2fc872179a65860c5d71b8 Mon Sep 17 00:00:00 2001 From: halosghost Date: Thu, 23 May 2019 21:23:05 -0500 Subject: [PATCH 2/3] leverage $(AR) and allow mkdir-invocation to be configurable --- Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index c6be65f..aa890bb 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,7 @@ PROJ = mpc CC ?= gcc STND = -ansi DIST = build +MKDIR ?= mkdir -p PREFIX ?= /usr/local CFLAGS ?= $(STND) -pedantic -O3 -g -Wall -Werror -Wextra -Wformat=2 -Wshadow \ -Wno-long-long -Wno-overlength-strings -Wno-format-nonliteral -Wcast-align \ @@ -17,7 +18,7 @@ EXAMPLESEXE = $(EXAMPLES:.c=) all: $(EXAMPLESEXE) check $(DIST): - mkdir -p $(DIST)/examples + $(MKDIR) $(DIST)/examples check: test-file test-static test-dynamic ./$(DIST)/test-file @@ -41,7 +42,7 @@ lib$(PROJ).so: $(DIST) $(PROJ).c lib$(PROJ).a: $(DIST) $(PROJ).c $(CC) $(CFLAGS) -c $(PROJ).c -o $(DIST)/$(PROJ).o - ar rcs $(DIST)/lib$(PROJ).a $(DIST)/$(PROJ).o + $(AR) rcs $(DIST)/lib$(PROJ).a $(DIST)/$(PROJ).o libs: lib$(PROJ).so lib$(PROJ).a From 147545a488dbc353be3c5f9a4484c967e167bbdf Mon Sep 17 00:00:00 2001 From: halosghost Date: Wed, 29 May 2019 19:39:33 -0500 Subject: [PATCH 3/3] preliminarily detect windows platforms and call -fPIC accordingly --- Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Makefile b/Makefile index aa890bb..fd3460b 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,11 @@ examples/%: $(DIST) examples/%.c $(PROJ).c $(CC) $(CFLAGS) $(filter-out $(DIST), $^) -lm -o $(DIST)/$@ lib$(PROJ).so: $(DIST) $(PROJ).c +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 lib$(PROJ).a: $(DIST) $(PROJ).c $(CC) $(CFLAGS) -c $(PROJ).c -o $(DIST)/$(PROJ).o