commit 1618f34ca86c71aced49897d84b242fa591116ec
parent c332bc8a945b01ebf24cf1baf8cf144268b39252
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Fri, 23 May 2025 14:55:21 +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.
Diffstat:
| M | Makefile | | | 99 | +++++++++++++++++++++++++++++++++++++++++++++++-------------------------------- |
| M | config.mk | | | 9 | ++++++--- |
| D | make.sh | | | 71 | ----------------------------------------------------------------------- |
3 files changed, 65 insertions(+), 114 deletions(-)
diff --git a/Makefile b/Makefile
@@ -23,6 +23,9 @@ LIBNAME_STATIC = libsvx.a
LIBNAME_SHARED = libsvx.so
LIBNAME = $(LIBNAME_$(LIB_TYPE))
+default: library
+all: default tests
+
################################################################################
# Library building
################################################################################
@@ -37,7 +40,10 @@ SRC =\
OBJ = $(SRC:.c=.o)
DEP = $(SRC:.c=.d)
-build_library: .config $(DEP)
+CFLAGS_LIB = $(CFLAGS_SO) $(INCS) -DSVX_SHARED_BUILD
+LDFLAGS_LIB = $(LDFLAGS_SO) $(LIBS)
+
+library: .config $(DEP)
@$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f $${i}; done) \
$$(if [ -n "$(LIBNAME)" ]; then \
echo "$(LIBNAME)"; \
@@ -48,7 +54,7 @@ build_library: .config $(DEP)
$(DEP) $(OBJ): config.mk
$(LIBNAME_SHARED): $(OBJ)
- $(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -o $@ $(OBJ) $(LDFLAGS_SO) $(RSYS_LIBS) -lm
+ $(CC) $(CFLAGS_LIB) -o $@ $(OBJ) $(LDFLAGS_LIB)
$(LIBNAME_STATIC): libsvx.o
$(AR) -rc $@ $?
@@ -59,19 +65,18 @@ libsvx.o: $(OBJ)
$(OBJCOPY) $(OCPFLAGS) $@
.config: config.mk
- @if ! $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys; then \
- echo "rsys $(RSYS_VERSION) not found" >&2; exit 1; fi
+ $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys
@echo "config done" > $@
.SUFFIXES: .c .d .o
.c.d:
- @$(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@
+ @$(CC) $(CFLAGS_LIB) -MM -MT "$(@:.d=.o) $@" $< -MF $@
.c.o:
- $(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -DSVX_SHARED_BUILD -c $< -o $@
+ $(CC) $(CFLAGS_LIB) -c $< -o $@
################################################################################
-# Installation
+# Miscellaneous
################################################################################
pkg:
sed -e 's#@PREFIX@#$(PREFIX)#g'\
@@ -87,11 +92,16 @@ svx-local.pc: svx.pc.in
-e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g'\
svx.pc.in > $@
-install: build_library pkg
- @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib" $(LIBNAME)
- @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib/pkgconfig" svx.pc
- @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/include/star" src/svx.h
- @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/doc/star-vx" COPYING README.md
+install: library pkg
+ install() { mode="$$1"; prefix="$$2"; shift 2; \
+ mkdir -p "$${prefix}"; \
+ cp "$$@" "$${prefix}"; \
+ chmod "$${mode}" "$$@"; \
+ }; \
+ install 755 "$(DESTDIR)$(LIBPREFIX)" $(LIBNAME); \
+ install 644 "$(DESTDIR)$(LIBPREFIX)/pkgconfig" svx.pc; \
+ install 644 "$(DESTDIR)$(INCPREFIX)/star" src/svx.h; \
+ install 644 "$(DESTDIR)$(PREFIX)/share/doc/star-vx" COPYING README.md
uninstall:
rm -f "$(DESTDIR)$(PREFIX)/lib/$(LIBNAME)"
@@ -100,20 +110,9 @@ uninstall:
rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-vx/README.md"
rm -f "$(DESTDIR)$(PREFIX)/include/star/svx.h"
-################################################################################
-# Miscellaneous targets
-################################################################################
-all: build_library build_tests
-
clean: clean_test
- rm -f $(OBJ) $(TEST_OBJ) $(LIBNAME)
- rm -f .config .test libsvx.o svx.pc svx-local.pc
-
-distclean: clean
- rm -f $(DEP) $(TEST_DEP)
-
-lint:
- shellcheck -o all make.sh
+ rm -f $(DEP) $(OBJ) $(LIBNAME)
+ rm -f .config libsvx.o svx.pc svx-local.pc
################################################################################
# Tests
@@ -126,29 +125,33 @@ TEST_SRC =\
src/test_svx_octree_trace_ray.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)
-SVX_CFLAGS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags svx-local.pc)
-SVX_LIBS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs svx-local.pc)
+INCS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags rsys svx-local.pc)
+LIBS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs rsys svx-local.pc) -lm
-build_tests: build_library $(TEST_DEP) .test
- @$(MAKE) -fMakefile -f.test $$(for i in $(TEST_DEP); do echo -f"$${i}"; done) test_bin
+CFLAGS_TEST = $(CFLAGS_EXE) $(INCS_TEST)
+LDFLAGS_TEST = $(LDFLAGS_EXE) $(LIBS_TEST)
-test: build_tests
- @$(SHELL) make.sh run_test $(TEST_SRC)
+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_list
-.test: Makefile
- @$(SHELL) make.sh config_test $(TEST_SRC) > $@
-
-clean_test:
- @$(SHELL) make.sh clean_test $(TEST_SRC)
+$(TEST_TGT):
+ @{ \
+ exe="$$(basename "$@" ".t")"; \
+ printf '%s: %s\n' "$${exe}" $(@:.t=.o); \
+ printf 'test_list: %s\n' "$${exe}"; \
+ } > $@
$(TEST_DEP): config.mk svx-local.pc
- @$(CC) $(CFLAGS_EXE) $(RSYS_CFLAGS) $(SVX_CFLAGS) \
- -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@
+ @$(CC) $(CFLAGS_TEST) -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@
$(TEST_OBJ): config.mk svx-local.pc
- $(CC) $(CFLAGS_EXE) $(RSYS_CFLAGS) $(SVX_CFLAGS) -c $(@:.o=.c) -o $@
+ $(CC) $(CFLAGS_TEST) -c $(@:.o=.c) -o $@
test_svx_bintree \
test_svx_bintree_trace_ray \
@@ -156,4 +159,20 @@ test_svx_device \
test_svx_octree \
test_svx_octree_trace_ray \
: config.mk svx-local.pc $(LIBNAME)
- $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE) $(SVX_LIBS) $(RSYS_LIBS) -lm
+ $(CC) $(CFLAGS_TEST) -o $@ src/$@.o $(LDFLAGS_TEST)
+clean_test:
+ rm -f $(TEST_DEP) $(TEST_OBJ) $(TEST_TGT)
+ for i in $(TEST_SRC); do rm -f "$$(basename "$${i}" ".c")"; done
+
+test: tests
+ @err=0; \
+ for i in $(TEST_SRC); do \
+ test="$$(basename "$${i}" ".c")"; \
+ if "./$${test}" > /dev/null 2>&1; then \
+ printf '%s\n' "$${test}"; \
+ else \
+ >&2 printf '%s: error %s\n' "$${test}" "$$?"; \
+ 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)/libs
+INCPREFIX = $(PREFIX)/include
+
################################################################################
# Tools
################################################################################
@@ -24,9 +27,9 @@ PCFLAGS_SHARED =
PCFLAGS_STATIC = --static
PCFLAGS = $(PCFLAGS_$(LIB_TYPE))
-RSYS_VERSION=0.14
-RSYS_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsys)
-RSYS_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs rsys)
+RSYS_VERSION = 0.14
+INCS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsys)
+LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs rsys) -lm
################################################################################
# Compilation options
diff --git a/make.sh b/make.sh
@@ -1,71 +0,0 @@
-#!/bin/sh
-
-# Copyright (C) 2018, 2020-2024 |Méso|Star> (contact@meso-star.com)
-# Copyright (C) 2018 Université Paul Sabatier
-#
-# 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/>.
-
-set -e
-
-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
-}
-
-"$@"