star-3daw

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

commit c4d49b8b16b567e96cd5af9c770ac79108684b22
parent e592a3c60f0e036350f77b5451feea8e686b5122
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Thu, 13 Jul 2023 15:05:57 +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 | 4++--
MMakefile | 121++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------
Mconfig.mk | 71++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
Mmake.sh | 63+++++++++++----------------------------------------------------
Ms3daw.pc.in | 5+++--
5 files changed, 143 insertions(+), 121 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -6,8 +6,8 @@ *~ test* !test*.[ch] -.pkg +.config tags -s3daw.pc +*.pc *.mtl *.obj diff --git a/Makefile b/Makefile @@ -31,64 +31,90 @@ include config.mk +LIBNAME_STATIC = libs3daw.a +LIBNAME_SHARED = libs3daw.so +LIBNAME = $(LIBNAME_$(LIB_TYPE)) + ################################################################################ # Star-3DAW building ################################################################################ SRC = src/s3daw.c - OBJ = $(SRC:.c=.o) DEP = $(SRC:.c=.d) -build_library: .pkg $(DEP) - @$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f $${i}; done) libs3daw.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 -libs3daw.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: make.sh config.mk + @if ! $(PKG_CONFIG) --atleast-version $(AW_VERSION) aw; then\ + echo "aw $(AW_VERSION) not found" >&2; exit 1; fi + @if ! $(PKG_CONFIG) --atleast-version $(POLYGON_VERSION) polygon; then\ + echo "polygon $(POLYGON_VERSION) not found" >&2; exit 1; fi + @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 + @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) -DS3DAW_SHARED_BUILD -c $< -o $@ + $(CC) $(CFLAGS) $(DPDC_CFLAGS) -DS3DAW_SHARED_BUILD -c $< -o $@ ################################################################################ # Installation ################################################################################ pkg: @echo "Setup s3daw.pc" - @sed -e 's#@PREFIX@#$(PREFIX)#g' \ - -e 's#@VERSION@#$(VERSION)#g' \ - -e 's#@AW_VERSION@#$(AW_VERSION)#g' \ - -e 's#@POLYGON_VERSION@#$(POLYGON_VERSION)#g' \ - -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g' \ - -e 's#@STAR-3D_VERSION@#$(STAR-3D_VERSION)#g' \ + @sed -e 's#@PREFIX@#$(PREFIX)#g'\ + -e 's#@VERSION@#$(VERSION)#g'\ + -e 's#@AW_VERSION@#$(AW_VERSION)#g'\ + -e 's#@POLYGON_VERSION@#$(POLYGON_VERSION)#g'\ + -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g'\ + -e 's#@S3D_VERSION@#$(S3D_VERSION)#g'\ s3daw.pc.in > s3daw.pc +s3daw-local.pc: s3daw.pc.in + @sed -e '1d'\ + -e 's#^includedir=.*#includedir=./src/#'\ + -e 's#^libdir=.*#libdir=./#'\ + -e 's#@VERSION@#$(VERSION)#g'\ + -e 's#@AW_VERSION@#$(AW_VERSION)#g'\ + -e 's#@POLYGON_VERSION@#$(POLYGON_VERSION)#g'\ + -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g'\ + -e 's#@S3D_VERSION@#$(S3D_VERSION)#g'\ + s3daw.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-3daw - cp libs3daw.so $(DESTDIR)$(PREFIX)/lib - cp s3daw.pc $(DESTDIR)$(PREFIX)/lib/pkgconfig - cp src/s3daw.h $(DESTDIR)$(PREFIX)/include/star - cp COPYING.en COPYING.fr README.md $(DESTDIR)$(PREFIX)/share/doc/star-3daw + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib" $(LIBNAME) + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib/pkgconfig/" s3daw.pc + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/include/star" src/s3daw.h + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/doc/star-3daw"\ + COPYING.en COPYING.fr uninstall: - rm -f $(DESTDIR)$(PREFIX)/lib/libs3daw.so - rm -f $(DESTDIR)$(PREFIX)/lib/pkgconfig/s3daw.pc - rm -f $(DESTDIR)$(PREFIX)/share/doc/star-3daw/COPYING.en - rm -f $(DESTDIR)$(PREFIX)/share/doc/star-3daw/COPYING.fr - rm -f $(DESTDIR)$(PREFIX)/share/doc/star-3daw/README.md - rm -f $(DESTDIR)$(PREFIX)/include/star/s3daw.h + rm -f "$(DESTDIR)$(PREFIX)/lib/$(LIBNAME)" + rm -f "$(DESTDIR)$(PREFIX)/lib/pkgconfig/s3daw.pc" + rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-3daw/COPYING.en" + rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-3daw/COPYING.fr" + rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-3daw/README.md" + rm -f "$(DESTDIR)$(PREFIX)/include/star/s3daw.h" ################################################################################ # Miscellaneous targets @@ -96,13 +122,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 s3daw.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 ################################################################################ # Test @@ -110,21 +136,28 @@ lint: TEST_OBJ = src/test_s3daw.o TEST_DEP = src/test_s3daw.d -test: build_tests - @$(SHELL) make.sh run_test test_s3daw +PKG_CONFIG_LOCAL = PKG_CONFIG_PATH="./:$${PKG_CONFIG_PATH}" $(PKG_CONFIG) +S3DAW_CFLAGS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags s3daw-local.pc) +S3DAW_LIBS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs s3daw-local.pc) -build_tests: src/test_s3daw.d +build_tests: build_library src/test_s3daw.d @$(MAKE) -fMakefile -fsrc/test_s3daw.d test_s3daw +test: build_tests + @printf "%s " "test_s3daw" + @if ./test_s3daw > /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_s3daw cbox.mtl cbox2.mtl cbox.obj + rm -f test_s3daw cbox.mtl cbox2.mtl cbox.obj $(TEST_OBJ) $(TEST_DEP): config.mk -src/test_s3daw.o: src/test_s3daw.c - @echo "CC $@" - @$(CC) $(CFLAGS) $(RSYS_INC) $(STAR-3D_INC) -c $< -o $@ +src/test_s3daw.o: src/test_s3daw.c s3daw-local.pc + $(CC) $(CFLAGS) $(S3D_CFLAGS) $(S3DAW_CFLAGS) $(RSYS_CFLAGS) -c $< -o $@ -test_s3daw: src/test_s3daw.o libs3daw.so config.mk - @echo "LD $@" - @$(CC) $(CFLAGS) -o $@ src/$@.o -L$$(pwd) -ls3daw $(RSYS_LIB) $(STAR-3D_LIB) +test_s3daw: src/test_s3daw.o s3daw-local.pc + $(CC) -o $@ src/$@.o $(S3D_LIBS) $(S3DAW_LIBS) $(RSYS_LIBS) diff --git a/config.mk b/config.mk @@ -1,36 +1,49 @@ VERSION = 0.4.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 ################################################################################ -AW_VERSION=2.0 -AW_INC = $$($(PKG_CONFIG) --cflags aw) -AW_LIB = $$($(PKG_CONFIG) --libs aw) +PCFLAGS_SHARED = +PCFLAGS_STATIC = --static +PCFLAGS = $(PCFLAGS_$(LIB_TYPE)) -POLYGON_VERSION=0.1 -POLYGON_INC = $$($(PKG_CONFIG) --cflags polygon) -POLYGON_LIB = $$($(PKG_CONFIG) --libs polygon) +AW_VERSION = 2.0 +AW_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags aw) +AW_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs aw) -RSYS_VERSION=0.6 -RSYS_INC = $$($(PKG_CONFIG) --cflags rsys) -RSYS_LIB = $$($(PKG_CONFIG) --libs rsys) +POLYGON_VERSION = 0.1 +POLYGON_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags polygon) +POLYGON_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs polygon) -STAR-3D_VERSION=0.3 -STAR-3D_INC = $$($(PKG_CONFIG) --cflags s3d) -STAR-3D_LIB = $$($(PKG_CONFIG) --libs s3d) +RSYS_VERSION = 0.6 +RSYS_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsys) +RSYS_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs rsys) -INCS=$(AW_INC) $(POLYGON_INC) $(RSYS_INC) $(STAR-3D_INC) -LIBS=$(AW_LIB) $(POLYGON_LIB) $(RSYS_LIB) $(STAR-3D_LIB) +S3D_VERSION = 0.3 +S3D_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags s3d) +S3D_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs s3d) + +DPDC_CFLAGS = $(AW_CFLAGS) $(POLYGON_CFLAGS) $(RSYS_CFLAGS) $(S3D_CFLAGS) +DPDC_LIBS = $(AW_LIBS) $(POLYGON_LIBS) $(RSYS_LIBS) $(S3D_LIBS) ################################################################################ # Compilation options ################################################################################ -CC = cc # Compiler - -CPPFLAGS = -DNDEBUG WFLAGS =\ -Wall\ -Wcast-align\ @@ -40,7 +53,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,64 +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) - - aw_version=$(showvar AW_VERSION) - polygon_version=$(showvar POLYGON_VERSION) - rsys_version=$(showvar RSYS_VERSION) - s3d_version=$(showvar STAR-3D_VERSION) - dependencies="\ - AW aw ${aw_version} - Polygon polygon ${polygon_version} - RSys rsys ${rsys_version} - Star-3D s3d ${s3d_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/s3daw.pc.in b/s3daw.pc.in @@ -3,9 +3,10 @@ includedir=${prefix}/include libdir=${prefix}/lib Requires: rsys >= @RSYS_VERSION@ -Requires.private: aw >= @AW_VERSION@, polygon >= @POLYGON_VERSION@, s3d >= @STAR-3D_VERSION@ -Name: Star-3D +Requires.private: aw >= @AW_VERSION@, polygon >= @POLYGON_VERSION@, s3d >= @S3D_VERSION@ +Name: Star-3DAW Description: Star-3DAW library Version: @VERSION@ Libs: -L${libdir} -ls3daw +Libs.private: -law -lpolygon -ls3d CFlags: -I${includedir}