star-2d

Contour structuring for efficient 2D geometric queries
git clone git://git.meso-star.fr/star-2d.git
Log | Files | Refs | README | LICENSE

commit d2c2b3e2860c4f4f6be380ec009bb6657a006d6f
parent 3816118ed65fb4f899fc72be54f25f1052724da6
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed, 12 Jul 2023 14:37:35 +0200

Extensive rework of the POSIX Makefile

Split the linker flags in 2 different macros: the SOFLAGS macro defines
the flags when creating a shared object, i.e. a dynamic library, and the
LDFLAGS macro defines the linker options for all linking operations,
i.e. when creating a shared object or an executable.

Add the BUILD_TYPE macro which controls if the compilation is done in
RELEASE or in DEBUG: the CFLAGS and LDFLAGS macros are defined according
to BUILD_TYPE.

Add building the library as a static library. The new LIB_TYPE macro
controls whether the generated library is a shared object or an archive,
depending on whether its value is SHARED or STATIC respectively. Note
that the new macro PCFLAGS, whose value depends on LIB_TYPE, controls
whether pkg-config fetches dependencies for static linking or not.

Do not hide the compiler commands anymore (except for the file
dependency check). There is no particular advantage in doing this and it
is sane to see the compiler options used. In the same spirit, the
commands executed by the clean, distclean and lint targets are also
displayed to show what they do.

Use the new install function in the make.sh script to install the files.
It creates the target directory if necessary and copies the files only
if they are updated, thus avoiding forcing the reconstruction for
projects relying on the library after copying its unchanged header
files.

Clean up the make.sh script by replacing the sed directives with
basename, which is what these seds were actually doing.

Let the Makefile define the test targets rather than generating them in
the config_test function of the make.sh script. It's actually much more
natural for a developer to write/find the build directives in the
Makefile.

Diffstat:
MMakefile | 102+++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
Mconfig.mk | 58+++++++++++++++++++++++++++++++++++++++++++---------------
Mmake.sh | 57++++++++++++++++++++++++++++++++++-----------------------
3 files changed, 145 insertions(+), 72 deletions(-)

diff --git a/Makefile b/Makefile @@ -31,6 +31,10 @@ include config.mk +LIBNAME_STATIC = libs2d.a +LIBNAME_SHARED = libs2d.so +LIBNAME = $(LIBNAME_$(LIB_TYPE)) + ################################################################################ # Star-2D building ################################################################################ @@ -43,21 +47,27 @@ SRC =\ src/s2d_scene_view.c\ src/s2d_scene_view_closest_point.c\ src/s2d_shape.c - OBJ = $(SRC:.c=.o) DEP = $(SRC:.c=.d) build_library: .config $(DEP) - @$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f $${i}; done) libs2d.so + @$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f $${i}; done)\ + $$(if [ -n "$(LIBNAME)" ]; then\ + echo "$(LIBNAME)";\ + else\ + echo "$(LIBNAME_SHARED)";\ + fi) $(OBJ): config.mk -libs2d.so: $(OBJ) - @echo "LD $@" - @$(CC) $(CFLAGS) $(LDFLAGS) $(LIBS) -o $@ $(OBJ) +$(LIBNAME_SHARED): $(OBJ) + $(CC) -std=c99 $(CFLAGS) $(DPDC_CFLAGS) -o $@ $(OBJ) $(LDFLAGS) $(SOFLAGS) $(DPDC_LIBS) + +$(LIBNAME_STATIC): $(OBJ) + $(AR) -rc $@ $? + $(RANLIB) $@ -.config: Makefile - @echo "Find packages" +.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 \ @@ -66,11 +76,10 @@ libs2d.so: $(OBJ) .SUFFIXES: .c .d .o .c.d: - @$(CC) $(CFLAGS) $(INCS) -MM -MT "$(@:.d=.o) $@" $< -MF $@ + @$(CC) -std=c99 $(CFLAGS) $(DPDC_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@ .c.o: - @echo "CC $@" - @$(CC) $(CFLAGS) $(INCS) -DS2D_SHARED_BUILD -c $< -o $@ + $(CC) -std=c99 $(CFLAGS) $(DPDC_CFLAGS) -DS2D_SHARED_BUILD -c $< -o $@ ################################################################################ # Installation @@ -83,23 +92,29 @@ pkg: -e 's#@EMBREE_VERSION@#$(EMBREE_VERSION)#g' \ s2d.pc.in > s2d.pc +s2d-local.pc: s2d.pc.in + @sed -e '1d'\ + -e 's#^includedir=.*#includedir=./src/#'\ + -e 's#^libdir=.*#libdir=./#'\ + -e 's#@VERSION@#$(VERSION)#g'\ + -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g'\ + -e 's#@EMBREE_VERSION@#$(EMBREE_VERSION)#g'\ + s2d.pc.in > $@ + install: build_library pkg - mkdir -p $(DESTDIR)$(PREFIX)/lib - mkdir -p $(DESTDIR)$(PREFIX)/lib/pkgconfig - mkdir -p $(DESTDIR)$(PREFIX)/include/star - mkdir -p $(DESTDIR)$(PREFIX)/share/doc/star-2d - cp libs2d.so $(DESTDIR)$(PREFIX)/lib - cp s2d.pc $(DESTDIR)$(PREFIX)/lib/pkgconfig - cp src/s2d.h $(DESTDIR)$(PREFIX)/include/star - cp COPYING.en COPYING.fr README.md $(DESTDIR)$(PREFIX)/share/doc/star-2d + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib" $(LIBNAME) + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib/pkgconfig" s2d.pc + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/include/star" src/s2d.h + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/doc/star-2d"\ + COPYING.en COPYING.fr README.md uninstall: - rm -f $(DESTDIR)$(PREFIX)/lib/libs2d.so - rm -f $(DESTDIR)$(PREFIX)/lib/pkgconfig/s2d.pc - rm -f $(DESTDIR)$(PREFIX)/share/doc/star-2d/COPYING.en - rm -f $(DESTDIR)$(PREFIX)/share/doc/star-2d/COPYING.fr - rm -f $(DESTDIR)$(PREFIX)/share/doc/star-2d/README.md - rm -f $(DESTDIR)$(PREFIX)/include/star/s2d.h + rm -f "$(DESTDIR)$(PREFIX)/lib/$(LIBNAME)" + rm -f "$(DESTDIR)$(PREFIX)/lib/pkgconfig/s2d.pc" + rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-2d/COPYING.en" + rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-2d/COPYING.fr" + rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-2d/README.md" + rm -f "$(DESTDIR)$(PREFIX)/include/star/s2d.h" ################################################################################ # Miscellaneous targets @@ -107,10 +122,13 @@ uninstall: all: build_library build_tests clean: clean_test - @rm -f $(OBJ) $(TEST_OBJ) libs2d.so .test s2d.pc .config + rm -f $(OBJ) $(TEST_OBJ) $(LIBNAME) .config .test s2d.pc s2d-local.pc distclean: clean - @rm -f $(DEP) $(TEST_DEP) + rm -f $(DEP) $(TEST_DEP) + +lint: + shellcheck -o all make.sh ################################################################################ # Tests @@ -127,23 +145,39 @@ TEST_SRC =\ src/test_s2d_scene_view2.c\ src/test_s2d_trace_ray.c\ src/test_s2d_trace_ray_3d.c - TEST_OBJ = $(TEST_SRC:.c=.o) TEST_DEP = $(TEST_SRC:.c=.d) -test: build_tests - @$(SHELL) make.sh run_test $(TEST_SRC) +PKG_CONFIG_LOCAL = PKG_CONFIG_PATH="./:$${PKG_CONFIG_PATH}" $(PKG_CONFIG) +S2D_CFLAGS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags s2d-local.pc) +S2D_LIBS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs s2d-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 + @$(MAKE) -fMakefile -f.test \ + $$(for i in $(TEST_DEP); do echo -f"$${i}"; done) test_bin + +test: build_tests + @$(SHELL) make.sh run_test $(TEST_SRC) .test: Makefile make.sh - @echo "Setup tests" @$(SHELL) make.sh config_test $(TEST_SRC) > .test clean_test: @$(SHELL) make.sh clean_test $(TEST_SRC) -$(TEST_OBJ): config.mk - @echo "CC $@" - @$(CC) $(CFLAGS) $(RSYS_INC) -c $(@:.o=.c) -o $@ +$(TEST_OBJ): config.mk s2d-local.pc + $(CC) -std=c89 $(CFLAGS) $(S2D_CFLAGS) -c $(@:.o=.c) -o $@ + +test_s2d_closest_point \ +test_s2d_device \ +test_s2d_primitive \ +test_s2d_raytrace \ +test_s2d_sample \ +test_s2d_shape \ +test_s2d_scene \ +test_s2d_scene_view \ +test_s2d_scene_view2 \ +test_s2d_trace_ray \ +test_s2d_trace_ray_3d \ +: s2d-local.pc + $(CC) -o $@ src/$@.o $(LDFLAGS) $(S2D_LIBS) -lm diff --git a/config.mk b/config.mk @@ -1,28 +1,41 @@ VERSION = 0.5.1 # Library version - PREFIX = /usr/local + +LIB_TYPE = SHARED +#LIB_TYPE = STATIC + +BUILD_TYPE = RELEASE +#BUILD_TYPE = DEBUG + +################################################################################ +# Tools +################################################################################ +AR = ar +CC = cc PKG_CONFIG = pkg-config +RANLIB = ranlib ################################################################################ # Dependencies ################################################################################ -RSYS_VERSION=0.6 -RSYS_INC = $$($(PKG_CONFIG) --cflags rsys) -RSYS_LIB = $$($(PKG_CONFIG) --libs rsys) +PCFLAGS_SHARED = +PCFLAGS_STATIC = --static +PCFLAGS = $(PCFLAGS_$(LIB_TYPE)) -EMBREE_VERSION=4.0 -EMBREE_INC = $$($(PKG_CONFIG) --cflags embree4) -EMBREE_LIB = $$($(PKG_CONFIG) --libs embree4) +RSYS_VERSION = 0.6 +RSYS_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsys) +RSYS_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs rsys) -INCS=$(RSYS_INC) $(EMBREE_INC) -LIBS=$(RSYS_LIB) $(EMBREE_LIB) +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 ################################################################################ # Compilation options ################################################################################ -CC = cc # Compiler - -CPPFLAGS = -DNDEBUG WFLAGS =\ -Wall\ -Wcast-align\ @@ -32,7 +45,22 @@ WFLAGS =\ -Wmissing-prototypes\ -Wshadow -CFLAGS = -O3 -std=c99 -pedantic -fPIC -fvisibility=hidden -fstrict-aliasing\ - -Wl,--no-undefined $(WFLAGS) $(CPPFLAGS) # Compiler options +CFLAGS_COMMON =\ + -pedantic\ + -fPIC\ + -fvisibility=hidden\ + -fstrict-aliasing\ + $(WFLAGS) + +CFLAGS_RELEASE = -O2 -DNDEBUG $(CFLAGS_COMMON) +CFLAGS_DEBUG = -g $(CFLAGS_COMMON) +CFLAGS = $(CFLAGS_$(BUILD_TYPE)) + +################################################################################ +# Linker options +################################################################################ +SOFLAGS = -shared -Wl,--no-undefined -LDFLAGS = -shared # Linker options +LDFLAGS_DEBUG = +LDFLAGS_RELEASE = -s +LDFLAGS = $(LDFLAGS_$(BUILD_TYPE)) diff --git a/make.sh b/make.sh @@ -34,43 +34,35 @@ config_test() { for i in "$@"; do - test=$(echo "${i}" | sed 's/src\/\(.\{1,\}\).c$/\1/') - test_list="${test} ${test_list}" + test=$(basename "${i}" ".c") + test_list="${test_list} ${test}" printf "%s: %s\n" "${test}" "src/${test}.o" done - printf "%s: libs2d.so\n" "${test_list}" - printf "\t@echo \"LD \$@\"\n" - printf "\t@\$(CC) \$(CFLAGS) -o \$@ src/\$@.o -L\$\$(pwd) -ls2d \$(RSYS_LIB)\n" - printf "test_bin: %s\n" "${test_list}" } -check() +run_test() { - if [ $# -lt 1 ]; then - echo "usage: check <name> <prog>" >&2 - exit 1 - fi + for i in "$@"; do + test=$(basename "${i}" ".c") - name="$1" - prog="$2" - shift 2 - printf "%s " "${name}" - if ./"${prog}" "$@" > /dev/null 2>&1; then - printf "\e[1;32mOK\e[m\n" - else - printf "\e[1;31mError\e[m\n" - fi + 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" + fi + done 2> /dev/null } -run_test() +clean_test() { for i in "$@"; do - test=$(echo "${i}" | sed 's/src\/\(.\{1,\}\).c$/\1/') - check "${test}" "${test}" + rm -f "$(basename "${i}" ".c")" done } + clean_test() { for i in "$@"; do @@ -79,4 +71,23 @@ clean_test() 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 +} + "$@"