star-3d

Surface structuring for efficient 3D geometric queries
git clone git://git.meso-star.com/star-3d.git
Log | Files | Refs | README | LICENSE

commit 16e5cfe75f4c42a0815163a82eeb2061cf94a688
parent c4a2f03f9b99d7ca7811096a60df706579ecc156
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Fri, 10 Jul 2026 15:38:31 +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.

Dependencies are grouped in the INCS and LIBS macros using a single call
to the pkg-config program. In the Makefile, the CFLAGS and LDFLAGS
variables for the library and tests have been written into their own
macro to make the command lines more compact.

Diffstat:
MMakefile | 129++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
Mconfig.mk | 14+++++++-------
Dmake.sh | 84-------------------------------------------------------------------------------
3 files changed, 83 insertions(+), 144 deletions(-)

diff --git a/Makefile b/Makefile @@ -22,6 +22,9 @@ LIBNAME_STATIC = libs3d.a LIBNAME_SHARED = libs3d.so LIBNAME = $(LIBNAME_$(LIB_TYPE)) +default: library +all: library tests + ################################################################################ # Star-3D building ################################################################################ @@ -40,18 +43,21 @@ SRC =\ OBJ = $(SRC:.c=.o) DEP = $(SRC:.c=.d) -build_library: .config $(DEP) - @$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f "$${i}"; done)\ - $$(if [ -n "$(LIBNAME)" ]; then\ - echo "$(LIBNAME)";\ - else\ - echo "$(LIBNAME_SHARED)";\ - fi) +CFLAGS_LIB = -std=c99 $(CFLAGS_SO) $(INCS) -DS3D_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)"; \ + else \ + echo "$(LIBNAME_SHARED)"; \ + fi) $(DEP) $(OBJ): config.mk $(LIBNAME_SHARED): $(OBJ) - $(CC) -std=c99 $(CFLAGS_SO) $(DPDC_CFLAGS) -o $@ $(OBJ) $(LDFLAGS_SO) $(DPDC_LIBS) + $(CC) $(CFLAGS_LIB) -o $@ $(OBJ) $(LDFLAGS_LIB) $(LIBNAME_STATIC): libs3d.o $(AR) -rc $@ $? @@ -62,18 +68,16 @@ libs3d.o: $(OBJ) $(OBJCOPY) $(OCPFLAGS) $@ .config: Makefile config.mk - @if ! $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys; then\ - echo "rsys $(RSYS_VERSION) not found" >&2; exit 1; fi - @if ! $(PKG_CONFIG) --atleast-version $(EMBREE_VERSION) embree4; then\ - echo "embree $(EMBREE_VERSION) not found" >&2; exit 1; fi - @echo "config done" > $@ + $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys + $(PKG_CONFIG) --atleast-version $(EMBREE_VERSION) embree4 + echo "config done" > $@ .SUFFIXES: .c .d .o .c.d: - @$(CC) -std=c99 $(CFLAGS_SO) $(DPDC_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@ + @$(CC) $(CFLAGS_LIB) -MM -MT "$(@:.d=.o) $@" $< -MF $@ .c.o: - $(CC) -std=c99 $(CFLAGS_SO) $(DPDC_CFLAGS) -DS3D_SHARED_BUILD -c $< -o $@ + $(CC) $(CFLAGS_LIB) -c $< -o $@ ################################################################################ # Installation @@ -94,35 +98,31 @@ s3d-local.pc: s3d.pc.in -e 's#@EMBREE_VERSION@#$(EMBREE_VERSION)#g'\ s3d.pc.in > $@ -install: build_library pkg - @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib" $(LIBNAME) - @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib/pkgconfig" s3d.pc - @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/include/star" src/s3d.h - @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/doc/star-3d"\ - COPYING README.md - +install: library pkg + install() { mode="$$1"; prefix="$$2"; shift 2; \ + mkdir -p "$${prefix}"; \ + cp "$$@" "$${prefix}"; \ + printf '%s\n' "$${@}" | while read -r i; do \ + chmod "$${mode}" "$${prefix}/$${i##*/}"; \ + done; \ + }; \ + if [ "$(LIB_TYPE)" = "STATIC" ]; then mode=644; else mode=755; fi; \ + install "$${mode}" "$(DESTDIR)$(LIBPREFIX)" $(LIBNAME); \ + install 644 "$(DESTDIR)$(LIBPREFIX)/pkgconfig" s3d.pc; \ + install 644 "$(DESTDIR)$(INCPREFIX)/star" src/s3d.h; \ + install 644 "$(DESTDIR)$(PREFIX)/share/doc/star-3d" COPYING README.md + uninstall: - rm -f "$(DESTDIR)$(PREFIX)/lib/$(LIBNAME)" - rm -f "$(DESTDIR)$(PREFIX)/lib/pkgconfig/s3d.pc" + rm -f "$(DESTDIR)$(LIBPREFIX)/$(LIBNAME)" + rm -f "$(DESTDIR)$(LIBPREFIX)/pkgconfig/s3d.pc" + rm -f "$(DESTDIR)$(INCPREFIX)/star/s3d.h" rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-3d/COPYING" rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-3d/README.md" - rm -f "$(DESTDIR)$(PREFIX)/include/star/s3d.h" - -################################################################################ -# Miscellaneous targets -################################################################################ -all: build_library build_tests clean: clean_test - rm -f $(OBJ) $(TEST_OBJ) $(LIBNAME) + rm -f $(OBJ) $(DEP) $(LIBNAME) rm -f .config .test libs3d.o s3d.pc s3d-local.pc -distclean: clean - rm -f $(DEP) $(TEST_DEP) - -lint: - shellcheck -o all make.sh - ################################################################################ # Tests ################################################################################ @@ -147,30 +147,35 @@ TEST_SRC =\ src/test_s3d_trace_ray_sphere.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) -S3D_CFLAGS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags s3d-local.pc) -S3D_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs s3d-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 s3d) +LIBS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs s3d) -test: build_tests - @$(SHELL) make.sh run_test $(TEST_SRC) +CFLAGS_TEST = -std=c89 $(CFLAGS_EXE) $(INCS_TEST) +LDFLAGS_TEST = $(LDFLAGS_EXE) $(LIBS_TEST) -.test: Makefile - @$(SHELL) make.sh config_test $(TEST_SRC) > .test +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_TGT): + @{ \ + exe="$$(basename "$@" ".t")"; \ + printf '%s: %s\n' "$${exe}" $(@:.t=.o); \ + printf 'test_list: %s\n' "$${exe}"; \ + } > $@ -clean_test: - @$(SHELL) make.sh clean_test $(TEST_SRC) $(TEST_DEP): config.mk s3d-local.pc - @$(CC) -std=c89 $(CFLAGS_EXE) $(S3D_CFLAGS) \ - -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@ + @$(CC) $(CFLAGS_TEST) -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@ $(TEST_OBJ): config.mk s3d-local.pc - $(CC) -std=c89 $(CFLAGS_EXE) $(S3D_CFLAGS) -c $(@:.o=.c) -o $@ + $(CC) $(CFLAGS_TEST) -c $(@:.o=.c) -o $@ test_s3d_device \ test_s3d_primitive \ @@ -181,7 +186,7 @@ test_s3d_scene_view_aabb \ test_s3d_scene_view_delayed_cache_update \ test_s3d_shape \ : config.mk s3d-local.pc $(LIBNAME) - $(CC) -std=c89 $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE) $(S3D_LIBS) + $(CC) $(CFLAGS_TEST) -o $@ src/$@.o $(LDFLAGS_TEST) test_s3d_accel_struct_conf \ test_s3d_closest_point \ @@ -194,4 +199,22 @@ test_s3d_trace_ray \ test_s3d_trace_ray_instance \ test_s3d_trace_ray_sphere \ : config.mk s3d-local.pc $(LIBNAME) - $(CC) -std=c89 $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE) $(S3D_LIBS) -lm + $(CC) -std=c89 $(CFLAGS_TEST) -o $@ src/$@.o $(LDFLAGS_TEST) -lm + +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")"; \ + 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 @@ -1,5 +1,10 @@ VERSION = 0.10.0 + +# Default install paths PREFIX = /tmp/local +LIBPREFIX = $(PREFIX)/lib +INCPREFIX = $(PREFIX)/include + LIB_TYPE = SHARED #LIB_TYPE = STATIC @@ -25,15 +30,10 @@ 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) - EMBREE_VERSION=4.0 -EMBREE_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags embree4) -EMBREE_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs embree4) -DPDC_CFLAGS=$(RSYS_CFLAGS) $(EMBREE_CFLAGS) -DPDC_LIBS=$(RSYS_LIBS) $(EMBREE_LIBS) -lm +INCS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsys embree4) +LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs rsys embree4) -lm ################################################################################ # Compilation options diff --git a/make.sh b/make.sh @@ -1,84 +0,0 @@ -#!/bin/sh - -# Copyright (C) 2015-2023 |Méso|Star> (contact@meso-star.com) -# -# 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}" -} - -check() -{ - name="$1" - prog="$2" - shift 2 - - printf "%s " "${name}" - if ./"${prog}" "$@" > /dev/null 2>&1; then - printf "\033[1;32mOK\033[m\n" - else - printf "\033[1;31mError\033[m\n" - fi -} - -run_test() -{ - for i in "$@"; do - test=$(basename "${i}" ".c") - if [ "${test}" = "test_s3d_sphere_instance" ]\ - || [ "${test}" = "test_s3d_trace_ray" ]; then - check "${test}_legacy" "${test}" - check "${test}_filter" "${test}" filter - else - check "${test}" "${test}" - 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 -} - -"$@"