star-3dut

Generate meshes of simple geometric shapes
git clone git://git.meso-star.fr/star-3dut.git
Log | Files | Refs | README | LICENSE

commit 85381ebb55da0d988b6bb4c1e6eb069f3176d300
parent 370851afae62315ddfab9d941e978095011eef12
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Mon, 17 Jul 2023 15:34:36 +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.  Add
to the pkg-config file the math library as a private library, i.e.  as a
required library when linking with the Star-3DUT static library.

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. Redirect error
messages from the for loop into the run_test function to show only the
defined error message.

Diffstat:
M.gitignore | 4++--
MMakefile | 97+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
Mconfig.mk | 52+++++++++++++++++++++++++++++++++++++++-------------
Mmake.sh | 31+++++++++++++++++++++++++------
Ms3dut.pc.in | 1+
5 files changed, 122 insertions(+), 63 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -6,7 +6,7 @@ *~ test* !test*.[ch] -.pkg +.config .test tags -s3dut.pc +*.pc diff --git a/Makefile b/Makefile @@ -18,8 +18,12 @@ include config.mk +LIBNAME_STATIC = libs3dut.a +LIBNAME_SHARED = libs3dut.so +LIBNAME = $(LIBNAME_$(LIB_TYPE)) + ################################################################################ -# Star-3D building +# Library building ################################################################################ SRC =\ src/s3dut_cuboid.c\ @@ -27,53 +31,61 @@ SRC =\ src/s3dut_mesh.c\ src/s3dut_sphere.c\ src/s3dut_super_shape.c - OBJ = $(SRC:.c=.o) DEP = $(SRC:.c=.d) -build_library: .pkg $(DEP) - @$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f $${i}; done) libs3dut.so +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) + +$(DEP) $(OBJ): config.mk -$(OBJ): config.mk +$(LIBNAME_SHARED): $(OBJ) + $(CC) $(CFLAGS) $(RSYS_CFLAGS) -o $@ $(OBJ) $(LDFLAGS) $(SOFLAGS) $(RSYS_LIBS) -lm -libs3dut.so: $(OBJ) - @echo "LD $@" - @$(CC) $(CFLAGS) $(LDFLAGS) $(LIBS) -o $@ $(OBJ) +$(LIBNAME_STATIC): $(OBJ) + $(AR) -rc $@ $? + $(RANLIB) $@ -.pkg: config.mk make.sh +.config: config.mk @if ! $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys; then\ - >&2 printf "\e[1;31merror\e[0m: RSys $(RSYS_VERSION): dependency is missing\n";\ - exit 1;\ - fi - @echo "config done" > $@ + echo "rsys $(RSYS_VERSION) not found" >&2; exit 1; fi + @echo "config done" > $@ .SUFFIXES: .c .d .o .c.d: - @$(CC) $(CFLAGS) $(INCS) -MM -MT "$(@:.d=.o) $@" $< -MF $@ + @$(CC) $(CFLAGS) $(RSYS_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@ .c.o: - @echo "CC $@" - @$(CC) $(CFLAGS) $(INCS) -DS3DUT_SHARED_BUILD -c $< -o $@ + $(CC) $(CFLAGS) $(RSYS_CFLAGS) -DS3DUT_SHARED_BUILD -c $< -o $@ ################################################################################ # Installation ################################################################################ pkg: @echo "Setup s3dut.pc" - @sed -e 's#@PREFIX@#$(PREFIX)#g' \ - -e 's#@VERSION@#$(VERSION)#g' \ - -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g' \ + @sed -e 's#@PREFIX@#$(PREFIX)#g'\ + -e 's#@VERSION@#$(VERSION)#g'\ + -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g'\ s3dut.pc.in > s3dut.pc +s3dut-local.pc: s3dut.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'\ + s3dut.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-3dut - cp libs3dut.so $(DESTDIR)$(PREFIX)/lib - cp s3dut.pc $(DESTDIR)$(PREFIX)/lib/pkgconfig - cp src/s3dut.h $(DESTDIR)$(PREFIX)/include/star - cp COPYING README.md $(DESTDIR)$(PREFIX)/share/doc/star-3dut + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib" $(LIBNAME) + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib/pkgconfig" s3dut.pc + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/include/star" src/s3dut.h + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/doc/star-3dut" COPYING README.md uninstall: rm -f $(DESTDIR)$(PREFIX)/lib/libs3dut.so @@ -88,13 +100,13 @@ uninstall: all: build_library build_tests clean: clean_test - @rm -f $(OBJ) $(TEST_OBJ) libs3dut.so .test .pkg s3dut.pc + rm -f $(OBJ) $(TEST_OBJ) $(LIBNAME) .config .test s3dut.pc s3dut-local.pc distclean: clean - @rm -f $(DEP) $(TEST_DEP) + rm -f $(DEP) $(TEST_DEP) lint: - @shellcheck -o all make.sh + shellcheck -o all make.sh ################################################################################ # Tests @@ -110,24 +122,30 @@ TEST_SRC =\ src/test_s3dut_thick_truncated_super_shape.c\ src/test_s3dut_thin_cylinder.c\ src/test_s3dut_truncated_sphere.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) +S3DUT_CFLAGS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags s3dut-local.pc) +S3DUT_LIBS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs s3dut-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 +test: build_tests + @$(SHELL) make.sh run_test $(TEST_SRC) + .test: Makefile @echo "Setup tests" - @$(SHELL) make.sh config_test $(TEST_SRC) > .test + @$(SHELL) make.sh config_test $(TEST_SRC) > $@ clean_test: @$(SHELL) make.sh clean_test $(TEST_SRC) -$(TEST_OBJ): config.mk +$(TEST_DEP) $(TEST_OBJ): config.mk + +$(TEST_OBJ): s3dut-local.pc + $(CC) $(CFLAGS) $(RSYS_CFLAGS) $(S3DUT_CFLAGS) -c $(@:.o=.c) -o $@ test_s3dut_cuboid \ test_s3dut_cylinder \ @@ -139,10 +157,5 @@ test_s3dut_thick_truncated_sphere \ test_s3dut_thick_truncated_super_shape \ test_s3dut_thin_cylinder \ test_s3dut_truncated_sphere \ -: libs3dut.so config.mk - @echo "LD $@" - @$(CC) $(CFLAGS) -o $@ src/$@.o -L$$(pwd) -ls3dut $(RSYS_LIB) - -$(TEST_OBJ): config.mk - @echo "CC $@" - @$(CC) $(CFLAGS) $(RSYS_INC) -c $(@:.o=.c) -o $@ +: config.mk s3dut-local.pc + $(CC) -o $@ src/$@.o $(S3DUT_LIBS) $(RSYS_LIBS) -lm diff --git a/config.mk b/config.mk @@ -1,24 +1,34 @@ -VERSION = 0.3.3 # Library version - +VERSION = 0.3.3 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)) -INCS=$(RSYS_INC) -LIBS=$(RSYS_LIB) +RSYS_VERSION=0.6 +RSYS_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsys) +RSYS_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs rsys) ################################################################################ # Compilation options ################################################################################ -CC = cc # Compiler - -CPPFLAGS = -DNDEBUG WFLAGS =\ -Wall\ -Wcast-align\ @@ -28,7 +38,23 @@ WFLAGS =\ -Wmissing-prototypes\ -Wshadow -CFLAGS = -O3 -std=c99 -pedantic -fPIC -fvisibility=hidden -fstrict-aliasing\ - -Wl,--no-undefined $(WFLAGS) $(CPPFLAGS) # Compiler options +CFLAGS_COMMON =\ + -std=c89\ + -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 @@ -18,7 +18,7 @@ config_test() { for i in "$@"; do - test=$(echo "${i}" | sed 's/src\/\(.\{1,\}\).c$/\1/') + test=$(basename "${i}" ".c") test_list="${test_list} ${test}" printf "%s: %s\n" "${test}" "src/${test}.o" done @@ -28,21 +28,40 @@ config_test() run_test() { for i in "$@"; do - test=$(echo "${i}" | sed 's/src\/\(.\{1,\}\).c$/\1/') + test=$(basename "${i}" ".c") + printf "%s " "${test}" - if ./"${test}" > /dev/null 2>&1; then + if "./${test}" > /dev/null 2>&1; then printf "\e[1;32mOK\e[m\n" else printf "\e[1;31mErreur\e[m\n" fi - done + done 2> /dev/null } clean_test() { for i in "$@"; do - test=$(echo "${i}" | sed 's/src\/\(.\{1,\}\).c$/\1/') - rm -f "${test}" + 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/s3dut.pc.in b/s3dut.pc.in @@ -7,4 +7,5 @@ Name: Star-3DUT Description: Star 3D Utility Toolkit library Version: @VERSION@ Libs: -L${libdir} -ls3dut +Libs.private: -lm CFlags: -I${includedir}