star-3dstl

Create star-3d geometries from StL files
git clone git://git.meso-star.fr/star-3dstl.git
Log | Files | Refs | README | LICENSE

commit 9f4fa8a5a1a584e597cb4102391b1044a2bf10fa
parent 9b8679f110071960a892c70078801ddeedfbafd9
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Mon, 17 Jul 2023 11:03:46 +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.

Complete rewrite of the make.sh script. It's easier to look for
dependencies directly in the Makefile and not in the script. Also, since
there is only one test, it seems overzealous to check it in the make.sh:
so it runs directly in the Makefile. After this cleanup, the make.sh
script contains only the new install function used to install the files.
This function creates the target directory if necessary and copies files
only if they are updated, thus avoiding forcing a rebuild for projects
relying on the library after copying its unchanged header files.

Diffstat:
M.gitignore | 2+-
MMakefile | 119+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
Mconfig.mk | 64++++++++++++++++++++++++++++++++++++++++++++++------------------
Mmake.sh | 61+++++++++++--------------------------------------------------
Ms3dstl.pc.in | 3++-
5 files changed, 134 insertions(+), 115 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -8,5 +8,5 @@ test* !test*.[ch] .pkg tags -s3dstl.pc +*.pc *.stl diff --git a/Makefile b/Makefile @@ -31,63 +31,86 @@ include config.mk +LIBNAME_STATIC = libs3dstl.a +LIBNAME_SHARED = libs3dstl.so +LIBNAME = $(LIBNAME_$(LIB_TYPE)) + ################################################################################ -# Star-3DAW building +# Library building ################################################################################ SRC = src/s3dstl.c - OBJ = $(SRC:.c=.o) DEP = $(SRC:.c=.d) -build_library: .pkg $(DEP) - @$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f $${i}; done) libs3dstl.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) $(OBJ) $(DEP): config.mk -libs3dstl.so: $(OBJ) - @echo "LD $@" - @$(CC) $(CFLAGS) $(LDFLAGS) $(LIBS) -o $@ $(OBJ) +$(LIBNAME_SHARED): $(OBJ) + $(CC) $(CFLAGS) $(DPDC_CFLAGS) -o $@ $(OBJ) $(LDFLAGS) $(SOFLAGS) $(DPDC_LIBS) -.pkg: make.sh config.mk - @$(SHELL) make.sh config_pkg && echo "pkg done" > $@ || exit 1 +$(LIBNAME_STATIC): $(OBJ) + $(AR) -rc $@ $? + $(RANLIB) $@ + +.config: 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 $(S3D_VERSION) s3d; then\ + echo "s3d $(S3D_VERSION) not found" >&2; exit 1; fi + @if ! $(PKG_CONFIG) --atleast-version $(SSTL_VERSION) sstl; then\ + echo "sstl $(SSTL_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) $(DPDC_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@ .c.o: - @echo "CC $@" - @$(CC) $(CFLAGS) $(INCS) -DS3DSTL_SHARED_BUILD -c $< -o $@ + $(CC) $(CFLAGS) $(DPDC_CFLAGS) -DS3DSTL_SHARED_BUILD -c $< -o $@ ################################################################################ # Installation ################################################################################ pkg: @echo "Setup s3dstl.pc" - @sed -e 's#@PREFIX@#$(PREFIX)#g' \ - -e 's#@VERSION@#$(VERSION)#g' \ - -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g' \ - -e 's#@STAR-3D_VERSION@#$(STAR-3D_VERSION)#g' \ - -e 's#@STAR-STL_VERSION@#$(STAR-STL_VERSION)#g' \ + @sed -e 's#@PREFIX@#$(PREFIX)#g'\ + -e 's#@VERSION@#$(VERSION)#g'\ + -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g'\ + -e 's#@S3D_VERSION@#$(S3D_VERSION)#g'\ + -e 's#@SSTL_VERSION@#$(SSTL_VERSION)#g'\ s3dstl.pc.in > s3dstl.pc +s3dstl-local.pc: s3dstl.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#@S3D_VERSION@#$(S3D_VERSION)#g'\ + -e 's#@SSTL_VERSION@#$(SSTL_VERSION)#g'\ + s3dstl.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-3dstl - cp libs3dstl.so $(DESTDIR)$(PREFIX)/lib - cp s3dstl.pc $(DESTDIR)$(PREFIX)/lib/pkgconfig - cp src/s3dstl.h $(DESTDIR)$(PREFIX)/include/star - cp COPYING.en COPYING.fr README.md $(DESTDIR)$(PREFIX)/share/doc/star-3dstl + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib" $(LIBNAME) + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib/pkgconfig/" s3dstl.pc + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/include/star" src/s3dstl.h + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/doc/star-3dstl"\ + COPYING.en COPYING.fr README.md uninstall: - rm -f $(DESTDIR)$(PREFIX)/lib/libs3dstl.so - rm -f $(DESTDIR)$(PREFIX)/lib/pkgconfig/s3dstl.pc - rm -f $(DESTDIR)$(PREFIX)/share/doc/star-3dstl/COPYING.en - rm -f $(DESTDIR)$(PREFIX)/share/doc/star-3dstl/COPYING.fr - rm -f $(DESTDIR)$(PREFIX)/share/doc/star-3dstl/README.md - rm -f $(DESTDIR)$(PREFIX)/include/star/s3dstl.h + rm -f "$(DESTDIR)$(PREFIX)/lib/libs3dstl.so" + rm -f "$(DESTDIR)$(PREFIX)/lib/pkgconfig/s3dstl.pc" + rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-3dstl/COPYING.en" + rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-3dstl/COPYING.fr" + rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-3dstl/README.md" + rm -f "$(DESTDIR)$(PREFIX)/include/star/s3dstl.h" ################################################################################ # Miscellaneous targets @@ -95,13 +118,13 @@ uninstall: all: build_library build_tests clean: clean_test - @rm -f $(OBJ) $(TEST_OBJ) libs3d.so .test s3d.pc .pkg + rm -f $(OBJ) $(TEST_OBJ) $(LIBNAME) .config .test s3dstl.pc s3dstl-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 ################################################################################ # Test @@ -109,22 +132,28 @@ lint: TEST_OBJ = src/test_s3dstl.o TEST_DEP = src/test_s3dstl.d -test: build_tests - @$(SHELL) make.sh run_test test_s3dstl +PKG_CONFIG_LOCAL = PKG_CONFIG_PATH="./:$${PKG_CONFIG_PATH}" $(PKG_CONFIG) +S3DSTL_CFLAGS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags s3dstl-local.pc) +S3DSTL_LIBS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs s3dstl-local.pc) -build_tests: src/test_s3dstl.d +build_tests: build_library src/test_s3dstl.d @$(MAKE) -fMakefile -fsrc/test_s3dstl.d test_s3dstl +test: build_tests + @printf "%s " "test_s3dstl" + @if ./test_s3dstl > /dev/null 2>&1; then \ + printf "\e[1;32mOK\e[m\n"; \ + else \ + printf "\e[1;31mError\e[m\n"; \ + fi + clean_test: - @rm -f test_s3dstl test_empty.stl + rm -f test_s3dstl test_empty.stl $(TEST_OBJ) $(TEST_DEP): config.mk -src/test_s3dstl.o: src/test_s3dstl.c - @echo "CC $@" - @$(CC) $(CFLAGS) $(RSYS_INC) $(STAR-3D_INC) $(STAR-STL_INC) -c $< -o $@ +src/test_s3dstl.o: src/test_s3dstl.c s3dstl-local.pc + $(CC) $(CFLAGS) $(RSYS_CFLAGS) $(S3D_CFLAGS) $(S3DSTL_CFLAGS) $(SSTL_CFLAGS) -c $< -o $@ -test_s3dstl: src/test_s3dstl.o libs3dstl.so config.mk - @echo "LD $@" - @$(CC) $(CFLAGS) -o $@ src/$@.o -L$$(pwd) -ls3dstl\ - $(RSYS_LIB) $(STAR-3D_LIB) $(STAR-STL_LIB) +test_s3dstl: src/test_s3dstl.o s3dstl-local.pc + $(CC) -o $@ src/$@.o $(RSYS_LIBS) $(S3D_LIBS) $(S3DSTL_LIBS) $(SSTL_LIBS) diff --git a/config.mk b/config.mk @@ -1,32 +1,44 @@ -VERSION = 0.3.2 # Library version - +VERSION = 0.3.2 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 ################################################################################ +PCFLAGS_STATIC = --static +PCFLAGS = $(PCFLAGS_$(LIB_TYPE)) + RSYS_VERSION=0.3 -RSYS_INC = $$($(PKG_CONFIG) --cflags rsys) -RSYS_LIB = $$($(PKG_CONFIG) --libs rsys) +RSYS_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsys) +RSYS_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs rsys) -STAR-3D_VERSION=0.3 -STAR-3D_INC = $$($(PKG_CONFIG) --cflags s3d) -STAR-3D_LIB = $$($(PKG_CONFIG) --libs s3d) +S3D_VERSION=0.3 +S3D_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags s3d) +S3D_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs s3d) -STAR-STL_VERSION=0.3 -STAR-STL_INC = $$($(PKG_CONFIG) --cflags sstl) -STAR-STL_LIB = $$($(PKG_CONFIG) --libs sstl) +SSTL_VERSION=0.3 +SSTL_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags sstl) +SSTL_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs sstl) -INCS=$(RSYS_INC) $(STAR-3D_INC) $(STAR-STL_INC) -LIBS=$(RSYS_LIB) $(STAR-3D_LIB) $(STAR-STL_LIB) +DPDC_CFLAGS=$(RSYS_CFLAGS) $(S3D_CFLAGS) $(SSTL_CFLAGS) +DPDC_LIBS=$(RSYS_LIBS) $(S3D_LIBS) $(SSTL_LIBS) ################################################################################ # Compilation options ################################################################################ -CC = cc # Compiler - -CPPFLAGS = -DNDEBUG WFLAGS =\ -Wall\ -Wcast-align\ @@ -36,7 +48,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 @@ -28,62 +28,23 @@ # The fact that you are presently reading this means that you have had # knowledge of the CeCILL license and that you accept its terms. -################################################################################ -# Helper functions -################################################################################ -# Print the value of a variable in config.mk -showvar() +install() { - # 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 -<< EOF 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 -} + prefix=$1 + shift 1 + + mkdir -p "${prefix}" -################################################################################ -# Main functions -################################################################################ -run_test() -{ for i in "$@"; do - printf "%s " "${i}" - if ./"${i}" > /dev/null 2>&1; then - printf "\e[1;32mOK\e[m\n" + dst="${prefix}/${i##*/}" + + if cmp -s "${i}" "${dst}"; then + printf "Up to date %s\n" "${dst}" else - printf "\e[1;31mErreur\e[m\n" + printf "Installing %s\n" "${dst}" + cp "${i}" "${prefix}" fi done } -config_pkg() -{ - pkg_config=$(showvar PKG_CONFIG) - - rsys_version=$(showvar RSYS_VERSION) - s3d_version=$(showvar STAR-3D_VERSION) - sstl_version=$(showvar STAR-STL_VERSION) - dependencies="\ - RSys rsys ${rsys_version} - Star-3D s3d ${s3d_version} - Star-STL sstl ${sstl_version}" - - printf "%s\n" "${dependencies}" | while read -r i; do - name=$(printf "%s" "${i}" | cut -d' ' -f1) - pc=$(printf "%s" "${i}" | cut -d' ' -f2) - version=$(printf "%s" "${i}" | cut -d' ' -f3) - - if ! "${pkg_config}" --atleast-version "${version}" "${pc}"; then - >&2 printf "\e[1;31merror\e[0m:%s %s: dependency is missing\n" \ - "${name}" "${version}" - exit 1 - fi - done || exit $? -} - "$@" diff --git a/s3dstl.pc.in b/s3dstl.pc.in @@ -3,9 +3,10 @@ includedir=${prefix}/include libdir=${prefix}/lib Requires: rsys >= @RSYS_VERSION@ -Requires.private: s3d >= @STAR-3D_VERSION@, sstl >= @STAR-STL_VERSION@ +Requires.private: s3d >= @S3D_VERSION@, sstl >= @SSTL_VERSION@ Name: Star-3DSTL Description: Star-3DSTL library Version: @VERSION@ Libs: -L${libdir} -ls3dstl +Libs.private: -ls3d -lsstl CFlags: -I${includedir}