commit 20a80e42d441348e50cf38a2cb7ebe4b7f5fab62
parent 48a7ec5cbcf8cc7d8e67efa88f2a82e65c9fdce9
Author: vaplv <vaplv@free.fr>
Date: Sat, 6 May 2023 15:34:31 +0200
Rework the POSIX Makefile
The linker is no longer invoked with the list of useless CFLAGS. In
addition, the linker flags are now separated into 3 different macros:
- The LIBS macro lists the libraries with which RSys is linked.
- The SOFLAGS macro defines the flags when creating a shared object,
i.e. a dynamic library.
- Finally, the LDFLAGS macro defines the linker options for all linking
operations, i.e. when creating a shared object or an executable
Add a static library target. The LIBSUFFIX macro controls the type of
library to build: "so" for a dynamic library, "a" for a static library.
Add the BUILD_TYPE macro which controls if the compilation is done in
RELEASE or in DEBUG.
Move the VERSION macro from the config.mk file to the Makefile because
it is not a variable that the user should update for his needs.
Finally, we no longer hide the build commands (except for the file
dependency check) and we have slightly updated the make.sh script
Diffstat:
| M | Makefile | | | 103 | +++++++++++++++++++++++++++++++++---------------------------------------------- |
| M | config.mk | | | 46 | ++++++++++++++++++++++++++++++++++++++++------ |
| M | make.sh | | | 32 | ++++++++++---------------------- |
3 files changed, 93 insertions(+), 88 deletions(-)
diff --git a/Makefile b/Makefile
@@ -16,6 +16,8 @@
.POSIX:
.SUFFIXES: # Clean up default inference rules
+VERSION = 0.13.0
+
include config.mk
################################################################################
@@ -36,26 +38,27 @@ SRC =\
src/quaternion.c\
src/str.c\
src/text_reader.c
-
OBJ = $(SRC:.c=.o)
DEP = $(SRC:.c=.d)
build_library: $(DEP)
- @$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f $${i}; done) librsys.so
+ @$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f $${i}; done) librsys.$(LIBSUFFIX)
$(OBJ): config.mk
librsys.so: $(OBJ)
- @echo "LD $@"
- @$(CC) $(CFLAGS) -o $@ $(OBJ) $(LDFLAGS)
+ $(CC) -o $@ $(OBJ) $(LDFLAGS) $(SOFLAGS) $(LIBS)
+
+librsys.a: $(OBJ)
+ $(AR) -rc $@ $?
+ $(RANLIB) $@
.SUFFIXES: .c .d .o
.c.d:
- @$(CC) $(CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@
+ @$(CC) $(CFLAGS_$(BUILD_TYPE)) -MM -MT "$(@:.d=.o) $@" $< -MF $@
.c.o:
- @echo "CC $@"
- @$(CC) $(CFLAGS) -DRSYS_SHARED_BUILD -c $< -o $@
+ $(CC) $(CFLAGS_$(BUILD_TYPE)) -DRSYS_SHARED_BUILD -c $< -o $@
################################################################################
# Installation
@@ -125,19 +128,34 @@ pkg:
@sed -e 's#@PREFIX@#$(PREFIX)#g' -e 's#@VERSION@#$(VERSION)#g' rsys.pc.in > rsys.pc
install: build_library pkg
- @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib" librsys.so
+ @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib" librsys.$(LIBSUFFIX)
@$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/doc/rsys" COPYING README.md
@$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib/pkgconfig" rsys.pc
@$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/include/rsys" $(API)
uninstall:
- rm -f $(DESTDIR)$(PREFIX)/lib/librsys.so
+ rm -f $(DESTDIR)$(PREFIX)/lib/librsys.$(LIBSUFFIX)
rm -f $(DESTDIR)$(PREFIX)/lib/pkgconfig/rsys.pc
rm -f $(DESTDIR)$(PREFIX)/share/doc/rsys/COPYING
rm -f $(DESTDIR)$(PREFIX)/share/doc/rsys/README.md
rm -f $$(echo $(API) | sed 's,src\/,$(DESTDIR)$(PREFIX)\/include\/rsys\/,g')
################################################################################
+# Miscellaneous targets
+################################################################################
+all: build_library build_tests
+
+clean: clean_test
+ @rm -f $(OBJ) $(TEST_OBJ) librsys.so librsys.a libtest_lib.so test_lib.o\
+ .test rsys.pc .test.ppm test_text_reader.txt test.ppm
+
+distclean: clean
+ @rm -f $(DEP) $(TEST_DEP)
+
+lint:
+ @shellcheck -o all make.sh
+
+################################################################################
# Tests
################################################################################
TEST_SRC =\
@@ -181,7 +199,6 @@ TEST_SRC =\
src/test_text_reader.c\
src/test_time.c\
src/test_vmacros.c
-
TEST_OBJ = $(TEST_SRC:.c=.o)
TEST_DEP = $(TEST_SRC:.c=.d)
@@ -191,15 +208,12 @@ build_tests: build_library $(TEST_DEP) .test
$(TEST_OBJ): config.mk
test: build_tests
- @$(MAKE) -fMakefile -f.test $$(for i in $(TEST_DEP); do echo -f"$${i}"; done) test_run
+ @$(SHELL) make.sh run_test $(TEST_SRC)
.test: Makefile
@echo "Setup tests"
@$(SHELL) make.sh config_test $(TEST_SRC) > .test
-test_run: test_bin
- @$(SHELL) make.sh run_test $(TEST_SRC)
-
clean_test:
@rm -f libtest_lib.so test_lib.o .test .test.ppm test_text_reader.txt test.ppm
@$(SHELL) make.sh clean_test $(TEST_SRC)
@@ -242,19 +256,13 @@ src/test_text_reader.o \
src/test_time.o \
src/test_vmacros.o \
:
- @echo "CC $@"
- @$(CC) $(CFLAGS) -c $(@:.o=.c) -o $@
+ $(CC) $(CFLAGS_$(BUILD_TYPE)) -c $(@:.o=.c) -o $@
-src/test_cstr.o \
-:
- @echo "CC $@"
- @$(CC) $(CFLAGS) -Wno-long-long -c $(@:.o=.c) -o $@
+src/test_cstr.o:
+ $(CC) $(CFLAGS_$(BUILD_TYPE)) -Wno-long-long -c $(@:.o=.c) -o $@
-src/test_condition.o \
-src/test_mutex.o \
-:
- @echo "CC $@"
- @$(CC) $(CFLAGS) -fopenmp -c $(@:.o=.c) -o $@
+src/test_condition.o src/test_mutex.o:
+ $(CC) $(CFLAGS_$(BUILD_TYPE)) -fopenmp -c $(@:.o=.c) -o $@
test_algorithm \
test_atomic \
@@ -265,8 +273,7 @@ test_morton \
test_ref \
test_vmacros \
:
- @echo "LD $@"
- @$(CC) $(CFLAGS) -o $@ src/$@.o
+ $(CC) -o $@ src/$@.o $(LDFLAGS)
test_double22 \
test_double2 \
@@ -282,8 +289,7 @@ test_float44 \
test_float4 \
test_math \
:
- @echo "LD $@"
- @$(CC) $(CFLAGS) -o $@ src/$@.o -lm
+ $(CC) -o $@ src/$@.o $(LDFLAGS) -lm
test_binary_heap\
test_cstr \
@@ -301,42 +307,19 @@ test_str \
test_stretchy_array \
test_text_reader \
test_time \
-: librsys.so
- @echo "LD $@"
- @$(CC) $(CFLAGS) -o $@ src/$@.o -L$$(pwd) -lrsys
+:
+ $(CC) -o $@ src/$@.o $(LDFLAGS) -L$$(pwd) -lrsys
-test_quaternion \
-: librsys.so
- @echo "LD $@"
- @$(CC) $(CFLAGS) -o $@ src/$@.o -L$$(pwd) -lrsys -lm
+test_quaternion:
+ $(CC) -o $@ src/$@.o $(LDFLAGS) -L$$(pwd) -lrsys -lm
-test_condition \
-test_mutex \
-: librsys.so
- @echo "LD $@"
- @$(CC) $(CFLAGS) -o $@ src/$@.o -fopenmp -L$$(pwd) -lrsys -lm
+test_condition test_mutex:
+ $(CC) -o $@ src/$@.o $(LDFLAGS) -fopenmp -L$$(pwd) -lrsys -lm
test_lib.o: src/test_library.c src/rsys.h
- @echo "CC $@"
- @$(CC) $(CFLAGS) -c src/test_library.c -DTEST_LIBRARY_BUILD_LIB -o $@
+ $(CC) $(CFLAGS_$(BUILD_TYPE)) -c src/test_library.c -DTEST_LIBRARY_BUILD_LIB -o $@
libtest_lib.so: test_lib.o
- @echo "LD $@"
- @$(CC) $(CFLAGS) -shared -o $@ test_lib.o
+ $(CC) -o $@ test_lib.o $(LDFLAGS) $(SOFLAGS)
test_library: libtest_lib.so
-
-################################################################################
-# Miscellaneous targets
-################################################################################
-all: build_library build_tests
-
-clean: clean_test
- @rm -f $(OBJ) $(TEST_OBJ) librsys.so libtest_lib.so test_lib.o .test \
- rsys.pc .test.ppm test_text_reader.txt test.ppm
-
-distclean: clean
- @rm -f $(DEP) $(TEST_DEP)
-
-lint:
- @shellcheck -o all make.sh
diff --git a/config.mk b/config.mk
@@ -1,10 +1,44 @@
-VERSION = 0.13.0
-
PREFIX = /usr/local
-CPPFLAGS = -DNDEBUG
-WFLAGS = -Wall -Wextra -Wmissing-declarations -Wmissing-prototypes -Wconversion -Wshadow
-CFLAGS = -O3 -std=c89 -pedantic -fPIC -fvisibility=hidden -fstrict-aliasing -Wl,--no-undefined $(WFLAGS) $(CPPFLAGS)
-LDFLAGS = -shared -ldl -lpthread -lm
+LIBSUFFIX = so # Shared library
+#LIBSUFFIX = a # Static library
+
+BUILD_TYPE= RELEASE
+#BUILD_TYPE= DEBUG
+################################################################################
+# Tools
+################################################################################
CC = cc
+AR = ar
+RANLIB = ranlib
+PKG_CONFIG = pkg-config
+
+################################################################################
+# Compilation options
+################################################################################
+WFLAGS =\
+ -Wall\
+ -Wextra\
+ -Wmissing-declarations\
+ -Wmissing-prototypes\
+ -Wconversion\
+ -Wshadow
+
+CFLAGS_COMMON =\
+ -std=c89\
+ -pedantic\
+ -fPIC\
+ -fvisibility=hidden\
+ -fstrict-aliasing\
+ $(WFLAGS)
+
+CFLAGS_RELEASE = -O2 -DNDEBUG $(CFLAGS_COMMON)
+CFLAGS_DEBUG = -g $(CFLAGS_COMMON)
+
+################################################################################
+# Linker options
+################################################################################
+LIBS = -ldl -lpthread -lm
+SOFLAGS = -shared -Wl,--no-undefined
+LDFLAGS = -s
diff --git a/make.sh b/make.sh
@@ -17,13 +17,8 @@
config_test()
{
- if [ $# -lt 1 ]; then
- echo "usage: config_test [src ...]" >&2
- exit 1
- fi
-
for i in "$@"; do
- test=$(echo "${i}" | sed 's/src\/\(.\{1,\}\).c$/\1/')
+ test=$(basename "${i}" ".c")
test_list="${test_list} ${test}"
printf "%s: %s\n" "${test}" "src/${test}.o"
done
@@ -32,24 +27,23 @@ config_test()
run_test()
{
- n=0
-
for i in "$@"; do
- test=$(echo "${i}" | sed 's/src\/\(.\{1,\}\).c$/\1/')
+ test=$(basename "${i}" ".c")
printf "%s " "${test}"
if ./"${test}" > /dev/null 2>&1; then
printf "\e[1;32mOK\e[m\n"
else
printf "\e[1;31mErreur\e[m\n"
- n=$((n+1))
fi
done
+}
- if [ "${n}" -ne 0 ]; then
- printf "%d errors\n" "${n}"
- exit 1
- fi
+clean_test()
+{
+ for i in "$@"; do
+ rm -f "$(basename "${i}" ".c")"
+ done
}
install()
@@ -58,8 +52,10 @@ install()
shift 1
mkdir -p "${prefix}"
+
for i in "$@"; do
dst="${prefix}/${i##*/}"
+
if cmp -s "${i}" "${dst}"; then
printf "Up to date %s\n" "${dst}"
else
@@ -69,12 +65,4 @@ install()
done
}
-clean_test()
-{
- for i in "$@"; do
- test=$(echo "${i}" | sed 's/src\/\(.\{1,\}\).c$/\1/')
- rm -f "${test}"
- done
-}
-
"$@"