rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

commit 6df58655b22396886581eee417a1a83480dda23b
parent 7b4dfc36e5e2519c1353682987ce9938e8a107f6
Author: vaplv <vaplv@free.fr>
Date:   Fri, 25 Apr 2025 14:46:53 +0200

Rewriting the Makefile

Delete the make.sh script. It implemented functions which, while
contributing to the clarity of the Makefile, made the build system less
compact. All these functions can be implemented with just a few lines of
shell code, without any major impact on the clarity of the Makefile.
Perhaps on the contrary, since it's all in one place.

Add the LIBPREFIX and INCPREFIX macros to control the subdirectories
into which libraries and header files are copied. Systems may use
locations other than those proposed by default.

Remove the "distclean" Makefile target. In fact, it does the wrong
thing. distclean, in addition to a normal cleanup target, should also
delete files created by a dist target, i.e.  a target generating files
for distribution, such as a compressed archive. But it was used to
delete automatically generated dependency files. These files can simply
be removed by normal cleanup. What's more, as the project is designed to
be installed by compiling its sources directly, no distribution target
is implemented, so no distclean is required.

Reduce the number of ways to compile tests. All tests are now linked to
RSys and the math library, even those that don't strictly need them.
This makes the Makefile clearer by grouping almost all tests under the
same compilation command. Only the condition and mutex tests are
compiled separately, as they rely on OpenMP.

Diffstat:
MMakefile | 190++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
Mconfig.mk | 3+++
Dmake.sh | 68--------------------------------------------------------------------
Msrc/test_cstr.c | 6+++---
4 files changed, 112 insertions(+), 155 deletions(-)

diff --git a/Makefile b/Makefile @@ -22,6 +22,9 @@ LIBNAME_STATIC = librsys.a LIBNAME_SHARED = librsys.so LIBNAME = $(LIBNAME_$(LIB_TYPE)) +default: library +all: library tests + ################################################################################ # RSys building ################################################################################ @@ -43,7 +46,10 @@ SRC =\ OBJ = $(SRC:.c=.o) DEP = $(SRC:.c=.d) -build_library: $(DEP) +CFLAGS_LIB = $(CFLAGS_SO) -DRSYS_SHARED_BUILD +LDFLAGS_LIB = $(LDFLAGS_SO) $(LIBS) + +library: $(DEP) @$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f $${i}; done) \ $$(if [ -n "$(LIBNAME)" ]; then\ echo "$(LIBNAME)";\ @@ -54,7 +60,7 @@ build_library: $(DEP) $(DEP) $(OBJ): config.mk $(LIBNAME_SHARED): $(OBJ) - $(CC) $(CFLAGS_SO) -o $@ $(OBJ) $(LDFLAGS_SO) $(LIBS) + $(CC) $(CFLAGS_LIB) -o $@ $(OBJ) $(LDFLAGS_LIB) $(LIBNAME_STATIC): librsys.o $(AR) -rc $@ $? @@ -66,10 +72,10 @@ librsys.o: $(OBJ) .SUFFIXES: .c .d .o .c.d: - @$(CC) $(CFLAGS_SO) -MM -MT "$(@:.d=.o) $@" $< -MF $@ + @$(CC) $(CFLAGS_LIB) -MM -MT "$(@:.d=.o) $@" $< -MF $@ .c.o: - $(CC) $(CFLAGS_SO) -DRSYS_SHARED_BUILD -c $< -o $@ + $(CC) $(CFLAGS_LIB) -c $< -o $@ ################################################################################ # Installation @@ -135,8 +141,8 @@ API =\ src/text_reader.h pkg: - sed -e 's#@PREFIX@#$(PREFIX)#g'\ - -e 's#@VERSION@#$(VERSION)#g'\ + sed -e 's,@PREFIX@,$(PREFIX),g'\ + -e 's,@VERSION@,$(VERSION),g'\ rsys.pc.in > rsys.pc # Remove the include directive rather than setting it to "./src". to prevent @@ -149,42 +155,40 @@ pkg: # library rsys-local.pc: rsys.pc.in sed -e '1,2d'\ - -e 's#^libdir=.*#libdir=./#'\ - -e 's#@PREFIX@#$(PREFIX)#g'\ - -e 's#@VERSION@#$(VERSION)#g'\ - -e 's#-I$${includedir}##g'\ + -e 's,^libdir=.*,libdir=./,'\ + -e 's,@PREFIX@,$(PREFIX),g'\ + -e 's,@VERSION@,$(VERSION),g'\ + -e 's,-I$${includedir},,g'\ rsys.pc.in > $@ -install: build_library pkg - @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib/" $(LIBNAME) - @$(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) +install: library pkg + install() { mode="$$1"; prefix="$$2"; shift 2; \ + mkdir -p "$${prefix}"; \ + for i in "$$@"; do \ + if ! cmp -s "$${i}" "$${prefix}/$${i}"; then \ + cp "$$@" "$${prefix}"; \ + chmod "$${mode}" "$$@"; \ + fi; \ + done; \ + }; \ + install 755 "$(DESTDIR)$(LIBPREFIX)" $(LIBNAME); \ + install 644 "$(DESTDIR)$(LIBPREFIX)/pkgconfig" rsys.pc; \ + install 644 "$(DESTDIR)$(INCPREFIX)/rsys" $(API); \ + install 644 "$(DESTDIR)$(PREFIX)/share/doc/rsys" COPYING README.md uninstall: - rm -f "$(DESTDIR)$(PREFIX)/lib/$(LIBNAME)" - rm -f "$(DESTDIR)$(PREFIX)/lib/pkgconfig/rsys.pc" + rm -f "$(DESTDIR)$(LIBPREFIX)/$(LIBNAME)" + rm -f "$(DESTDIR)$(LIBPREFIX)/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 + rm -f $$(echo $(API) | sed 's,src/,$(DESTDIR)$(INCPREFIX)/rsys/,g') clean: clean_test - rm -f $(OBJ) $(TEST_OBJ) $(LIBNAME) librsys.o + rm -f $(OBJ) $(DEP) $(LIBNAME) librsys.o rm -f rsys.pc rsys-local.pc rm -f libtest_lib.so test_lib.o rm -f .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 ################################################################################ @@ -231,30 +235,48 @@ TEST_SRC =\ src/test_vmacros.c TEST_OBJ = $(TEST_SRC:.c=.o) TEST_DEP = $(TEST_SRC:.c=.d) +TEST_TGT = $(TEST_SRC:.c=.t) PKG_CONFIG_LOCAL = PKG_CONFIG_PATH="./:$${PKG_CONFIG_PATH}" $(PKG_CONFIG) -RSYS_CFLAGS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags rsys-local.pc) -RSYS_LIBS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs rsys-local.pc) -build_tests: build_library $(TEST_DEP) .test - @$(MAKE) -fMakefile -f.test $$(for i in $(TEST_DEP); do echo -f"$${i}"; done) test_bin +INCS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags rsys-local.pc) +LIBS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs rsys-local.pc) -lm + +CFLAGS_TEST = $(CFLAGS_EXE) $(INCS_TEST) +LDFLAGS_TEST = $(LDFLAGS_EXE) $(LIBS_TEST) -$(TEST_DEP) $(TEST_OBJ): config.mk +tests: library $(TEST_DEP) $(TEST_TGT) + @$(MAKE) -fMakefile \ + $$(for i in $(TEST_DEP); do echo -f"$${i}"; done) \ + $$(for i in $(TEST_TGT); do echo -f"$${i}"; done) \ + test_all -test: build_tests - @$(SHELL) make.sh run_test $(TEST_SRC) +$(TEST_TGT): + @{ \ + exe="$$(basename "$@" ".t")"; \ + printf '%s: %s\n' "$${exe}" $(@:.t=.o); \ + printf 'test_all: %s\n' "$${exe}"; \ + } > $@ -.test: Makefile - @echo "Setup tests" - @$(SHELL) make.sh config_test $(TEST_SRC) > .test +$(TEST_DEP) $(TEST_OBJ): config.mk rsys-local.pc 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) + rm -f $(TEST_DEP) $(TEST_OBJ) $(TEST_TGT) + rm -f libtest_lib.so test_lib.o .test .test.ppm test_text_reader.txt test.ppm + for i in $(TEST_SRC); do rm -f "$$(basename "$${i}" ".c")"; done + +test_lib.o: src/test_library.c src/rsys.h config.mk + $(CC) $(CFLAGS_SO) -c src/test_library.c -DTEST_LIBRARY_BUILD_LIB -o $@ + +libtest_lib.so: test_lib.o config.mk + $(CC) $(CFLAGS_SO) -o $@ test_lib.o $(LDFLAGS_SO) + +test_library: libtest_lib.so src/test_algorithm.o \ src/test_atomic.o \ src/test_binary_heap.o \ +src/test_cstr.o \ src/test_double22.o \ src/test_double2.o \ src/test_double33.o \ @@ -289,71 +311,71 @@ src/test_stretchy_array.o \ src/test_text_reader.o \ src/test_time.o \ src/test_vmacros.o \ -: config.mk rsys-local.pc $(LIBNAME) - $(CC) $(CFLAGS_EXE) $(RSYS_CFLAGS) -c $(@:.o=.c) -o $@ +: + $(CC) $(CFLAGS_TEST) -c $(@:.o=.c) -o $@ -src/test_cstr.o: config.mk rsys-local.pc $(LIBNAME) - $(CC) $(CFLAGS_EXE) $(RSYS_CFLAGS) -Wno-long-long -c $(@:.o=.c) -o $@ +src/test_condition.o \ +src/test_mutex.o \ +: + $(CC) $(CFLAGS_TEST) -fopenmp -c $(@:.o=.c) -o $@ -src/test_condition.o src/test_mutex.o: config.mk rsys-local.pc $(LIBNAME) - $(CC) $(CFLAGS_EXE) $(RSYS_CFLAGS) -fopenmp -c $(@:.o=.c) -o $@ test_algorithm \ test_atomic \ -test_endianness \ -test_func_name \ -test_misc \ -test_morton \ -test_ref \ -test_vmacros \ -: config.mk - $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE) - -test_double22 \ +test_binary_heap\ +test_cstr \ test_double2 \ -test_double33 \ +test_double22 \ test_double3 \ -test_double44 \ +test_double33 \ test_double4 \ -test_float22 \ +test_double44 \ +test_dynamic_array \ +test_endianness \ test_float2 \ -test_float33 \ +test_float22 \ test_float3 \ -test_float44 \ +test_float33 \ test_float4 \ -test_math \ -: config.mk - $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE) -lm - -test_binary_heap\ -test_cstr \ -test_dynamic_array \ +test_float44 \ test_free_list \ +test_func_name \ test_hash_sha256 \ test_hash_table \ test_image \ test_library \ test_list \ test_logger \ +test_math \ test_mem_allocator \ +test_misc \ +test_morton \ +test_quaternion \ +test_ref \ test_signal \ test_str \ test_stretchy_array \ test_text_reader \ test_time \ +test_vmacros \ : config.mk rsys-local.pc $(LIBNAME) - $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS) $(RSYS_LIBS) - -test_quaternion: config.mk rsys-local.pc $(LIBNAME) - $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS) $(RSYS_LIBS) -lm + $(CC) $(CFLAGS_TEST) -o $@ src/$@.o $(LDFLAGS_TEST) -test_condition test_mutex: config.mk rsys-local.pc $(LIBNAME) - $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS) $(RSYS_LIBS) -lm -fopenmp - -test_lib.o: src/test_library.c src/rsys.h config.mk - $(CC) $(CFLAGS_SO) -c src/test_library.c -DTEST_LIBRARY_BUILD_LIB -o $@ - -libtest_lib.so: test_lib.o config.mk - $(CC) $(CFLAGS_SO) -o $@ test_lib.o $(LDFLAGS_SO) - -test_library: libtest_lib.so +test_condition \ +test_mutex \ +: config.mk rsys-local.pc $(LIBNAME) + $(CC) $(CFLAGS_TEST) -o $@ src/$@.o $(LDFLAGS_TEST) -fopenmp + +test: tests + @err=0; \ + for i in $(TEST_SRC); do \ + test="$$(basename "$${i}" ".c")"; \ + printf '%s' "$${test}"; \ + if "./$${test}" > /dev/null 2>&1; then \ + printf '\n'; \ + else \ + printf ': error %s\n' "$$?"; \ + err=$$((err+1)); \ + fi \ + done; \ + [ "$${err}" -eq 0 ] diff --git a/config.mk b/config.mk @@ -7,6 +7,9 @@ LIB_TYPE = SHARED BUILD_TYPE = RELEASE #BUILD_TYPE = DEBUG +LIBPREFIX = $(PREFIX)/lib +INCPREFIX = $(PREFIX)/include + ################################################################################ # Tools ################################################################################ diff --git a/make.sh b/make.sh @@ -1,68 +0,0 @@ -#!/bin/sh -e - -# Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -config_test() -{ - for i in "$@"; do - test=$(basename "${i}" ".c") - test_list="${test_list} ${test}" - printf "%s: %s\n" "${test}" "src/${test}.o" - done - printf "test_bin: %s\n" "${test_list}" -} - -run_test() -{ - for i in "$@"; do - test=$(basename "${i}" ".c") - - printf "%s " "${test}" - if ./"${test}" > /dev/null 2>&1; then - printf "\033[1;32mOK\033[m\n" - else - printf "\033[1;31mError\033[m\n" - fi - done 2> /dev/null -} - -clean_test() -{ - for i in "$@"; do - rm -f "$(basename "${i}" ".c")" - done -} - -install() -{ - prefix=$1 - 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 - printf "Installing %s\n" "${dst}" - cp "${i}" "${prefix}" - fi - done -} - -"$@" diff --git a/src/test_cstr.c b/src/test_cstr.c @@ -102,9 +102,9 @@ test_int(void) sprintf(buf, "%d", INT_MIN); CHK(cstr_to_int(buf, &i) == RES_OK); CHK(i == INT_MIN); - sprintf(buf, "%lld", (long long)INT_MAX+1); + sprintf(buf, "%ld", (int64_t)INT_MAX+1); CHK(cstr_to_int(buf, &i) == RES_BAD_ARG); - sprintf(buf, "%lld", (long long)INT_MIN-1); + sprintf(buf, "%ld", (int64_t)INT_MIN-1); CHK(cstr_to_int(buf, &i) == RES_BAD_ARG); } @@ -128,7 +128,7 @@ test_uint(void) sprintf(buf, "%u", UINT_MAX); CHK(cstr_to_uint(buf, &u) == RES_OK); CHK(u == UINT_MAX); - sprintf(buf, "%llu", (unsigned long long)UINT_MAX+1); + sprintf(buf, "%lu", (uint64_t)UINT_MAX+1); CHK(cstr_to_uint(buf, &u) == RES_BAD_ARG); }