star-sf

Set of surface and volume scattering functions
git clone git://git.meso-star.fr/star-sf.git
Log | Files | Refs | README | LICENSE

commit dbd6f0191c08727628ca4dc555dda7519bbd4148
parent 723cf19930cdb53da70cccaf100944f3a2754279
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed,  7 May 2025 09:38:24 +0200

Rewriting the Makefile

Delete the make.sh script. It implemented functions that, while
contributing to the clarity of the Makefile, made the construction
system less compact. Almost all the same features can be implemented
with a few lines of shell code, without any major impact on the clarity
of Makefile. Perhaps on the contrary, since everything is in one place.
In addition, the overall build system is therefore simpler. For example,
querying Makefile macros no longer requires tricks.

However, the features of the build system are not strictly the
same. Automatic detection of the SIMD instruction set to be used has
been removed. It is now always configured in the config.mk file (by
default, SIMD is deactivated). This automatic detection made the
Makefile more complex, requiring intermediate targets in order to add
the automatically-defined SIMD_WIDTH macro to the Makefile. Removing
this automatic detection makes the Makefile simpler, clearer and faster.
In other words, it sucks far less less than its predecessor, whose
complexity was driven by a dispensable feature.

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.

Diffstat:
MMakefile | 156++++++++++++++++++++++++++++++++++++++++++++++----------------------------------
Mconfig.mk | 29++++++++---------------------
Dmake.sh | 148-------------------------------------------------------------------------------
3 files changed, 98 insertions(+), 235 deletions(-)

diff --git a/Makefile b/Makefile @@ -22,8 +22,11 @@ LIBNAME_STATIC = libssf.a LIBNAME_SHARED = libssf.so LIBNAME = $(LIBNAME_$(LIB_TYPE)) +default: library +all: library tests + ################################################################################ -# Star-3D building +# Star-SF building ################################################################################ SRC =\ src/ssf.c\ @@ -50,20 +53,21 @@ SRC =\ OBJ = $(SRC:.c=.o) DEP = $(SRC:.c=.d) -build_library: +CFLAGS_LIB = $(CFLAGS_SO) $(CFLAGS_SIMD) $(INCS) -DSSF_SHARED_BUILD +LDFLAGS_LIB = $(LDFLAGS_SO) $(LIBS) -build_library__: .config $(DEP) - @$(MAKE) -f.simd -fMakefile $$(for i in $(DEP); do echo -f $${i}; done) \ +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 .simd +$(DEP) $(OBJ): config.mk $(LIBNAME_SHARED): $(OBJ) - $(CC) $(CFLAGS_SO) $(CFLAGS_SIMD) $(DPDC_CFLAGS) -o $@ $(OBJ) $(LDFLAGS_SO) $(DPDC_LIBS) + $(CC) $(CFLAGS_LIB) -o $@ $(OBJ) $(LDFLAGS_LIB) $(LIBNAME_STATIC): libssf.o $(AR) -rc $@ $? @@ -73,43 +77,20 @@ libssf.o: $(OBJ) $(LD) -r $(OBJ) -o $@ $(OBJCOPY) $(OCPFLAGS) $@ -.config: config.mk .simd - @if [ "$(SIMD_WIDTH)" = "128" ] || [ "$(SIMD_WIDTH)" = "256" ]; then \ - if ! $(PKG_CONFIG) --atleast-version $(RSIMD_VERSION) rsimd; then \ - echo "rsimd $(RSIMD_VERSION) not found"; exit 1; fi; fi - @if ! $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys; then \ - echo "rsys $(RSYS_VERSION) not found"; exit 1; fi - @if ! $(PKG_CONFIG) --atleast-version $(SSP_VERSION) star-sp; then \ - echo "star-sp $(SSP_VERSION) not found"; exit 1; fi - @echo "config done" > $@ - -.simd: make.sh config.mk - @$(SHELL) make.sh config_simd > $@ +.config: config.mk + if [ "$(SIMD_WIDTH)" = "128" ] || [ "$(SIMD_WIDTH)" = "256" ]; then \ + $(PKG_CONFIG) --atleast-version $(RSIMD_VERSION) rsimd; \ + fi + $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys + $(PKG_CONFIG) --atleast-version $(SSP_VERSION) star-sp + echo "config done" > $@ .SUFFIXES: .c .d .o .c.d: - @$(CC) $(CFLAGS_SO) $(CFLAGS_SIMD) $(DPDC_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@ + @$(CC) $(CFLAGS_LIB) -MM -MT "$(@:.d=.o) $@" $< -MF $@ .c.o: - $(CC) $(CFLAGS_SO) $(CFLAGS_SIMD) $(DPDC_CFLAGS) -DSSF_SHARED_BUILD -c $< -o $@ - -################################################################################ -# Miscellaneous targets -################################################################################ -all: build_library build_tests - -clean: clean_test - rm -f $(OBJ) $(TEST_OBJ) $(LIBNAME) - rm -f .config .simd .test libssf.o ssf.pc ssf-local.pc - -distclean: clean - rm -f $(DEP) $(TEST_DEP) - -lint: - shellcheck -o all make.sh - -build_library build_tests pkg: .simd - @$(MAKE) -f.simd -fMakefile $@__ + $(CC) $(CFLAGS_LIB) -c $< -o $@ ################################################################################ # Installation @@ -119,7 +100,7 @@ PKG_SIMD_128 = $(PKG_RSIMD) PKG_SIMD_256 = $(PKG_RSIMD) PKG_SIMD = $(PKG_SIMD_$(SIMD_WIDTH)) -pkg__: +pkg: sed -e 's#@PREFIX@#$(PREFIX)#g'\ -e 's#@VERSION@#$(VERSION)#g'\ -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g'\ @@ -127,7 +108,7 @@ pkg__: -e 's#@SIMD@#$(PKG_SIMD)#g'\ ssf.pc.in > ssf.pc -ssf-local.pc: ssf.pc.in .simd +ssf-local.pc: ssf.pc.in sed -e '1d'\ -e 's#^includedir=.*#includedir=./src/#'\ -e 's#^libdir=.*#libdir=./#'\ @@ -137,18 +118,27 @@ ssf-local.pc: ssf.pc.in .simd -e 's#@SIMD@#$(PKG_SIMD)#g'\ ssf.pc.in > $@ -install: build_library pkg - @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib" $(LIBNAME) - @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib/pkgconfig" ssf.pc - @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/include/star" src/ssf.h - @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/doc/star-sf" 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" ssf.pc; \ + install 644 "$(DESTDIR)$(INCPREFIX)/star" src/ssf.h; \ + install 644 "$(DESTDIR)$(PREFIX)/share/doc/star-sf" COPYING README.md uninstall: - rm -f "$(DESTDIR)$(PREFIX)/lib/$(LIBNAME)" - rm -f "$(DESTDIR)$(PREFIX)/lib/pkgconfig/ssf.pc" + rm -f "$(DESTDIR)$(LIBPREFIX)/$(LIBNAME)" + rm -f "$(DESTDIR)$(LIBPREFIX)/pkgconfig/ssf.pc" + rm -f "$(DESTDIR)$(INCPREFIX)/star/ssf.h" rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-sf/COPYING" rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-sf/README.md" - rm -f "$(DESTDIR)$(PREFIX)/include/star/ssf.h" + +clean: clean_test + rm -f $(OBJ) $(DEP) $(LIBNAME) + rm -f .config .test libssf.o ssf.pc ssf-local.pc ################################################################################ # Tests @@ -176,29 +166,34 @@ TEST_SRC =\ src/test_ssf_thin_specular_dielectric.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) -SSF_CFLAGS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags ssf-local.pc) -SSF_LIBS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs ssf-local.pc) - -build_tests__: build_library $(TEST_DEP) .test ssf-local.pc - @$(MAKE) -fMakefile -f.test $$(for i in $(TEST_DEP); do echo -f"$${i}"; done) test_bin - -test: build_tests .simd - @$(SHELL) make.sh run_test $(TEST_SRC) - -.test: Makefile - @$(SHELL) make.sh config_test $(TEST_SRC) > .test - -clean_test: - @$(SHELL) make.sh clean_test $(TEST_SRC) +PC_TEST = rsys star-sp ssf-local $(RSIMD_PC_$(SIMD_WIDTH)) +INCS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags $(PC_TEST)) +LIBS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs $(PC_TEST)) -lm + +CFLAGS_TEST = $(CFLAGS_EXE) $(INCS_TEST) +LDFLAGS_TEST = $(LDFLAGS_EXE) $(LIBS_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}"; \ + } > $@ $(TEST_DEP): config.mk ssf-local.pc - @$(CC) $(CFLAGS_EXE) $(SSF_CFLAGS) $(RSYS_CFLAGS) $(SSP_CFLAGS) -MM -MT \ - "$(@:.d=.o) $@" $(@:.d=.c) -MF $@ + @$(CC) $(CFLAGS_TEST) -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@ $(TEST_OBJ): config.mk ssf-local.pc - $(CC) $(CFLAGS_EXE) $(SSF_CFLAGS) $(RSYS_CFLAGS) $(SSP_CFLAGS) -c $(@:.o=.c) -o $@ + $(CC) $(CFLAGS_TEST) -c $(@:.o=.c) -o $@ test_ssf_beckmann_distribution \ test_ssf_blinn_distribution \ @@ -221,4 +216,33 @@ test_ssf_specular_dielectric_dielectric_reflection \ test_ssf_specular_reflection \ test_ssf_thin_specular_dielectric \ : config.mk ssf-local.pc $(LIBNAME) - $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE) $(SSF_LIBS) $(RSYS_LIBS) $(SSP_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; \ + check() { name="$$1"; exe="$$2"; shift 2; \ + printf '%s' "$${name}"; \ + if "./$${exe}" "$$@"> /dev/null 2>&1; then \ + printf '\n'; \ + else \ + printf ': error %s\n' "$$?"; \ + err=$$((err+1)); \ + fi \ + }; \ + for i in $(TEST_SRC); do \ + test="$$(basename "$${i}" ".c")"; \ + if [ "$${test}" != "test_ssf_phase_rdgfa" ]; then \ + check "$${test}" "$${test}"; \ + elif [ "$(SIMD_WIDTH)" = "128" ]; then \ + check "$${test}_simd_128" "$${test}" simd_128; \ + elif [ "$(SIMD_WIDTH)" = "256" ]; then \ + check "$${test}_simd_256" "$${test}" simd_256; \ + else \ + check "$${test}_simd_none" "$${test}" simd_none; \ + fi; \ + done; \ + [ "$${err}" -eq 0 ] diff --git a/config.mk b/config.mk @@ -7,11 +7,13 @@ LIB_TYPE = SHARED BUILD_TYPE = RELEASE #BUILD_TYPE = DEBUG -# If not set, SIMD WIDTH is retrieved from host CPU -#SIMD_WIDTH = NONE +SIMD_WIDTH = NONE #SIMD_WIDTH = 128 #SIMD_WIDTH = 256 +LIBPREFIX = $(PREFIX)/lib +INCPREFIX = $(PREFIX)/include + ################################################################################ # Tools ################################################################################ @@ -29,28 +31,13 @@ PCFLAGS_STATIC = --static PCFLAGS = $(PCFLAGS_$(LIB_TYPE)) RSIMD_VERSION = 0.5 -RSIMD_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsimd) -RSIMD_CFLAGS_128 = $(RSIMD_CFLAGS) -RSIMD_CFLAGS_256 = $(RSIMD_CFLAGS) -RSIMD_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs rsimd) -RSIMD_LIBS_128 = $(RSIMD_LIBS) -RSIMD_LIBS_256 = $(RSIMD_LIBS) - RSYS_VERSION = 0.14 -RSYS_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsys) -RSYS_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs rsys) - SSP_VERSION = 0.14 -SSP_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags star-sp) -SSP_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs star-sp) - -DPDC_CFLAGS_SIMD_128 = $(RSIMD_CFLAGS) -DPDC_CFLAGS_SIMD_256 = $(RSIMD_CFLAGS) -DPDC_LIBS_SIMD_128 = $(RSIMD_LIBS) -DPDC_LIBS_SIMD_256 = $(RSIMD_LIBS) +RSIMD_PC_128 = rsimd +RSIMD_PC_256 = rsimd -DPDC_CFLAGS = $(RSIMD_CFLAGS_$(SIMD_WIDTH)) $(RSYS_CFLAGS) $(SSP_CFLAGS) -DPDC_LIBS = $(RSIMD_LIBS_$(SIMD_WIDTH)) $(RSYS_LIBS) $(SSP_LIBS) -lm +INCS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsys star-sp $(RSIMD_PC_$(SIMD_WIDTH))) +LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs rsys star-sp $(RSIMD_PC_$(SIMD_WIDTH))) -lm ################################################################################ # Compilation options diff --git a/make.sh b/make.sh @@ -1,148 +0,0 @@ -#!/bin/sh - -# Copyright (C) 2016-2018, 2021-2024 |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 - -################################################################################ -# Helper functions -################################################################################ -# Print the value of a variable in config.mk -showvar() -{ - # To avoid messages from Make being displayed instead of the value of - # the queried variable, we redirect its output to /dev/null and open a - # new file descriptor to stdout to print the variable. - # - # The MAKEFLAGS environment variable is cleared to prevent make - # defining options which reserve the file descriptor used to print the - # variable, which would result in a ‘wrong file descriptor’ error. - # This can be the case with certain versions of GNU make which, when - # run in parallel, use pipe file descriptors to synchronise with the - # jobserver. -<< EOF MAKEFLAGS="" ${MAKE:-make} -f 3>&1 1>/dev/null 2>&1 - || kill -HUP $$ -.POSIX: -include config.mk -showvar: - @1>&3 echo \$($1) -EOF - exec 3<&- # Close file descriptor 3 -} - -check_cpuflag() -{ - sed -n "/^flags[[:blank:]]\{1,\}:/{p;q}" /proc/cpuinfo \ -| sed "s/.*[[:blank:]]\{1,\}\($1\)[[:blank:]]\{1,\}.*/\1/" -} - -################################################################################ -# Main functions -################################################################################ -config_simd() -{ - simd_width="$(showvar SIMD_WIDTH)" - pkg_config="$(showvar PKG_CONFIG)" - avx="$(check_cpuflag avx)" - if [ -z "${simd_width}" ]; then - if ! ${pkg_config} --exists rsimd; then - simd_width="NONE" - elif [ -n "${avx}" ]; then - simd_width="256" - else - simd_width="128" - fi - fi - printf "SIMD_WIDTH = %s\n" "${simd_width}" -} - -config_test() -{ - for i in "$@"; do - test=$(basename "${i}" ".c") - test_list="${test_list} ${test}" - printf "%s: src/%s.o\n" "${test}" "${test}" - 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 2> /dev/null -} - -run_test() -{ - for i in "$@"; do - test=$(basename "${i}" ".c") - if [ "${test}" != "test_ssf_phase_rdgfa" ]; then - check "${test}" "${test}" - else - macro="SIMD_WIDTH" - value="[^[:blank:]]\{0,\}" - spaces="[[:space:]]\{0,\}" - simd_width=$(sed "s/${macro}${spaces}=${spaces}\(${value}\)${spaces}$/\1/" .simd) - if [ "${simd_width}" = "256" ]; then - check "${test}_simd_256" "${test}" simd_256 - fi - if [ "${simd_width}" = "256" ] || [ "${simd_width}" = 128 ]; then - check "${test}_simd_128" "${test}" simd_128 - fi - check "${test}_simd_none" "${test}" simd_none - fi - done 2> /dev/null -} - -clean_test() -{ - for i in "$@"; do - rm -f "$(basename "${i}" ".c")" - done -} - -install() -{ - prefix=$1 - shift 1 - - for i in "$@"; do - # Remove the "src" directory and append the "prefix" - dst="${prefix}/${i#*/}" - - # Create the Install directory if required - dir="${dst%/*}" - if [ ! -d "${dir}" ]; then - mkdir -p "${dir}" - fi - - if cmp -s "${i}" "${dst}"; then - printf "Up to date %s\n" "${dst}" - else - printf "Installing %s\n" "${dst}" - cp "${i}" "${dst}" - fi - done -} - -"$@"