star-stl

Load STereo Lithography (StL) file format
git clone git://git.meso-star.fr/star-stl.git
Log | Files | Refs | README | LICENSE

commit 2ed56a13917f76ecff75b5af5eb92f274698baeb
parent 8b9e653f32ebb2e14701186cc3ad2c0d358b6ed7
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Thu, 13 Jul 2023 17:41:01 +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. We also removed
an unnecessary input argument check in the config_test function since it
is only called internally and should be called correctly, except for a
bug. We redirect error messages from the for loop into the run_test
function to show only the defined error message and no longer show the
number of failed tests as it seems unnecessary.

Diffstat:
M.gitignore | 4++--
MMakefile | 95+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
Mconfig.mk | 50+++++++++++++++++++++++++++++++++++++-------------
Mmake.sh | 35+++++++++++++++++++++++++++--------
Msstl.pc.in | 1+
5 files changed, 121 insertions(+), 64 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -6,8 +6,8 @@ *~ test* !test*.[ch] -.pkg +.config .test tags -sstl.pc +*.pc *.stl diff --git a/Makefile b/Makefile @@ -1,4 +1,4 @@ -# Copyright (C) 2015, 2016, 2019, 2021 |Meso|Star> (contact@meso-star.com) +# Copyright (C) 2015, 2016, 2019, 2021, 2023 |Méso|Star> (contact@meso-star.com) # # This software is governed by the CeCILL license under French law and # abiding by the rules of distribution of free software. You can use, @@ -31,37 +31,45 @@ include config.mk +LIBNAME_STATIC = libsstl.a +LIBNAME_SHARED = libsstl.so +LIBNAME = $(LIBNAME_$(LIB_TYPE)) + ################################################################################ -# Star-3D building +# Library building ################################################################################ SRC = src/sstl.c - OBJ = $(SRC:.c=.o) DEP = $(SRC:.c=.d) -build_library: .pkg $(DEP) - @$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f $${i}; done) libsstl.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 -$(OBJ): config.mk +$(LIBNAME_SHARED): $(OBJ) + $(CC) -std=c99 $(CFLAGS) $(RSYS_CFLAGS) -o $@ $(OBJ) $(LDFLAGS) $(SOFLAGS) $(RSYS_LIBS) -lm -libsstl.so: $(OBJ) - @echo "LD $@" - @$(CC) $(CFLAGS) $(LDFLAGS) $(LIBS) -o $@ $(OBJ) +$(LIBNAME_STATIC): $(OBJ) + $(AR) -rc $@ $? + $(RANLIB) $@ -.pkg: config.mk make.sh - @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 +.config: config.mk + @if ! $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys; then \ + echo "rsys $(RSYS_VERSION) not found"; exit 1; fi @echo "config done" > $@ .SUFFIXES: .c .d .o .c.d: - @$(CC) $(CFLAGS) $(INCS) -MM -MT "$(@:.d=.o) $@" $< -MF $@ + @$(CC) -std=c99 $(CFLAGS) $(RSYS_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@ .c.o: - @echo "CC $@" - @$(CC) $(CFLAGS) $(INCS) -DSSTL_SHARED_BUILD -c $< -o $@ + $(CC) -std=c99 $(CFLAGS) $(RSYS_CFLAGS) -DSSTL_SHARED_BUILD -c $< -o $@ ################################################################################ # Installation @@ -73,18 +81,23 @@ pkg: -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g' \ sstl.pc.in > sstl.pc +sstl-local.pc: sstl.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' \ + sstl.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-stl - cp libsstl.so $(DESTDIR)$(PREFIX)/lib - cp sstl.pc $(DESTDIR)$(PREFIX)/lib/pkgconfig - cp src/sstl.h $(DESTDIR)$(PREFIX)/include/star - cp COPYING.en COPYING.fr README.md $(DESTDIR)$(PREFIX)/share/doc/star-stl + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib" $(LIBNAME) + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib/pkgconfig/" sstl.pc + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/include/star" src/sstl.h + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/doc/star-stl/"\ + COPYING.en COPYING.fr README.md uninstall: - rm -f $(DESTDIR)$(PREFIX)/lib/libsstl.so + rm -f $(DESTDIR)$(PREFIX)/lib/$(LIBNAME) rm -f $(DESTDIR)$(PREFIX)/lib/pkgconfig/sstl.pc rm -f $(DESTDIR)$(PREFIX)/share/doc/star-stl/COPYING.en rm -f $(DESTDIR)$(PREFIX)/share/doc/star-stl/COPYING.fr @@ -97,13 +110,13 @@ uninstall: all: build_library build_tests clean: clean_test - @rm -f $(OBJ) $(TEST_OBJ) libsstl.so .test sstl.pc .pkg + rm -f $(OBJ) $(TEST_OBJ) $(LIBNAME) .test sstl.pc .config distclean: clean - @rm -f $(DEP) $(TEST_DEP) + rm -f $(DEP) $(TEST_DEP) lint: - @shellcheck -o all make.sh + shellcheck -o all make.sh ################################################################################ # Tests @@ -111,28 +124,28 @@ lint: TEST_SRC =\ src/test_sstl.c\ src/test_sstl_load.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) +SSTL_CFLAGS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags sstl-local.pc) +SSTL_LIBS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs sstl-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: - @rm -f corner.stl corner_bin.stl test_basic.stl test_basic2.stl - @$(SHELL) make.sh clean_test $(TEST_SRC) + rm -f corner.stl corner_bin.stl test_basic.stl test_basic2.stl + $(SHELL) make.sh clean_test $(TEST_SRC) test_sstl test_sstl_load: libsstl.so config.mk - @echo "LD $@" - @$(CC) $(CFLAGS) -o $@ src/$@.o -L$$(pwd) -lsstl $(RSYS_LIB) + $(CC) -o $@ src/$@.o $(RSYS_LIBS) $(SSTL_LIBS) -lm -$(TEST_OBJ): config.mk - @echo "CC $@" - @$(CC) $(CFLAGS) $(RSYS_INC) -c $(@:.o=.c) -o $@ +$(TEST_OBJ): config.mk sstl-local.pc + $(CC) -std=c89 $(CFLAGS) $(RSYS_CFLAGS) $(SSTL_CFLAGS) -c $(@:.o=.c) -o $@ diff --git a/config.mk b/config.mk @@ -1,24 +1,33 @@ -VERSION = 0.4.0 # Library version - +VERSION = 0.4.0 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_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 +37,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 @@ -1,6 +1,6 @@ #!/bin/sh -e -# Copyright (C) 2015, 2016, 2019, 2021 |Meso|Star> (contact@meso-star.com) +# Copyright (C) 2015, 2016, 2019, 2021, 2023 |Méso|Star> (contact@meso-star.com) # # This software is governed by the CeCILL license under French law and # abiding by the rules of distribution of free software. You can use, @@ -31,9 +31,9 @@ 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" + printf "%s: src/%s.o\n" "${test}" "${test}" done printf "test_bin: %s\n" "${test_list}" } @@ -41,21 +41,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 printf "\e[1;32mOK\e[m\n" else - printf "\e[1;31mErreur\e[m\n" + printf "\e[1;31mError\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/sstl.pc.in b/sstl.pc.in @@ -7,4 +7,5 @@ Name: Star-STL Description: Star STereo Lithography library Version: @VERSION@ Libs: -L${libdir} -lsstl +Libs.private: -lm CFlags: -I${includedir}