commit 121b413758f57dfa131a39d8a690c28c45c3edc9
parent 70c0ba0d536609b99f383eb3287e1d485a536c57
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Fri, 11 Apr 2025 09:54:54 +0200
Rewriting the Makefile
Delete the make.sh script. It implemented functions which, while
contributing to the clarity of the Makefile, made the build system less
compact. All these functions can be implemented with just a few lines of
shell code, without any major impact on the clarity of the Makefile.
Perhaps on the contrary, since it's all in one place.
Add the BINPREFIX, LIBPREFIX, INCPREFIX and MANPREFIX macros to control
the subdirectories into which binaries, libraries, header files and
documentation are copied. Systems may use locations other than those
proposed by default.
Remove the "distclean" Makefile target. In fact, it does the wrong
thing. distclean, in addition to a normal cleanup target, should also
delete files created by a dist target, i.e. a target generating files
for distribution, such as a compressed archive. But it was used to
delete automatically generated dependency files. These files can simply
be removed by normal cleanup. What's more, as the project is designed to
be installed by compiling its sources directly, no distribution target
is implemented, so no distclean is required.
Diffstat:
| M | Makefile | | | 117 | +++++++++++++++++++++++++++++++++++++++++++++++--------------------------------- |
| M | config.mk | | | 12 | +++++++++--- |
| D | make.sh | | | 70 | ---------------------------------------------------------------------- |
3 files changed, 78 insertions(+), 121 deletions(-)
diff --git a/Makefile b/Makefile
@@ -22,6 +22,9 @@ LIBNAME_STATIC = libsstl.a
LIBNAME_SHARED = libsstl.so
LIBNAME = $(LIBNAME_$(LIB_TYPE))
+default: library
+all: library tests
+
################################################################################
# Library building
################################################################################
@@ -29,7 +32,10 @@ SRC = src/sstl.c src/sstl_ascii.c src/sstl_binary.c
OBJ = $(SRC:.c=.o)
DEP = $(SRC:.c=.d)
-build_library: .config $(DEP)
+CFLAGS_LIB = -std=c99 $(CFLAGS_SO) $(INCS) -DSSTL_SHARED_BUILD
+LDFLAGS_LIB = $(LDFLAGS_SO) $(LIBS) -lm
+
+library: .config $(DEP)
@$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f $${i}; done) \
$$(if [ -n "$(LIBNAME)" ]; then\
echo "$(LIBNAME)";\
@@ -40,7 +46,7 @@ build_library: .config $(DEP)
$(DEP) $(OBJ): config.mk
$(LIBNAME_SHARED): $(OBJ)
- $(CC) -std=c99 $(CFLAGS_SO) $(RSYS_CFLAGS) -o $@ $(OBJ) $(LDFLAGS_SO) $(RSYS_LIBS) -lm
+ $(CC) $(CFLAGS_LIB) -o $@ $(OBJ) $(LDFLAGS_LIB)
$(LIBNAME_STATIC): libsstl.o
$(AR) -rc $@ $?
@@ -51,16 +57,15 @@ libsstl.o: $(OBJ)
$(OBJCOPY) $(OCPFLAGS) $@
.config: config.mk
- @if ! $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys; then \
- echo "rsys $(RSYS_VERSION) not found"; exit 1; fi
- @echo "config done" > $@
+ $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys
+ echo "config done" > $@
.SUFFIXES: .c .d .o
.c.d:
- @$(CC) -std=c99 $(CFLAGS_SO) $(RSYS_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@
+ @$(CC) $(CFLAGS_LIB) -MM -MT "$(@:.d=.o) $@" $< -MF $@
.c.o:
- $(CC) -std=c99 $(CFLAGS_SO) $(RSYS_CFLAGS) -DSSTL_SHARED_BUILD -c $< -o $@
+ $(CC) $(CFLAGS_LIB) -c $< -o $@
################################################################################
# Installation
@@ -73,39 +78,33 @@ pkg:
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#^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
- @$(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 README.md
+install: library pkg
+ install() { mode="$$1"; prefix="$$2"; shift 2; \
+ mkdir -p "$${prefix}"; \
+ cp "$$@" "$${prefix}"; \
+ chmod "$${mode}" "$$@"; \
+ }; \
+ install 755 "$(DESTDIR)$(LIBPREFIX)/" $(LIBNAME); \
+ install 644 "$(DESTDIR)$(LIBPREFIX)/pkgconfig" sstl.pc; \
+ install 644 "$(DESTDIR)$(INCPREFIX)/star" src/sstl.h; \
+ install 644 "$(DESTDIR)$(PREFIX)/share/doc/star-stl" COPYING README.md
uninstall:
- rm -f "$(DESTDIR)$(PREFIX)/lib/$(LIBNAME)"
- rm -f "$(DESTDIR)$(PREFIX)/lib/pkgconfig/sstl.pc"
+ rm -f "$(DESTDIR)$(LIBPREFIX)/$(LIBNAME)"
+ rm -f "$(DESTDIR)$(LIBPREFIX)/pkgconfig/sstl.pc"
+ rm -f "$(DESTDIR)$(INCPREFIX)/star/sstl.h"
rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-stl/COPYING"
rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-stl/README.md"
- rm -f "$(DESTDIR)$(PREFIX)/include/star/sstl.h"
-
-################################################################################
-# Miscellaneous targets
-################################################################################
-all: build_library build_tests
clean: clean_test
- rm -f $(OBJ) $(TEST_OBJ) $(LIBNAME)
- rm -f .config .test libsstl.o sstl.pc sstl-local.pc
-
-distclean: clean
- rm -f $(DEP) $(TEST_DEP)
-
-lint:
- shellcheck -o all make.sh
+ rm -f $(OBJ) $(DEP) $(LIBNAME)
+ rm -f .config libsstl.o sstl.pc sstl-local.pc
################################################################################
# Tests
@@ -116,34 +115,56 @@ TEST_SRC =\
src/test_sstl_load_binary.c
TEST_OBJ = $(TEST_SRC:.c=.o)
TEST_DEP = $(TEST_SRC:.c=.d)
+TEST_TGT = $(TEST_SRC:.c=.t)
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
+INCS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags rsys sstl-local)
+LIBS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs rsys sstl-local)
-test: build_tests
- @$(SHELL) make.sh run_test $(TEST_SRC)
+CFLAGS_TEST = -std=c89 $(CFLAGS_EXE) $(INCS_TEST)
+LDFLAGS_TEST = $(LDFLAGS_EXE) $(LIBS_TEST) -lm
-.test: Makefile
- @$(SHELL) make.sh config_test $(TEST_SRC) > $@
+tests: library $(TEST_DEP)
+ @$(MAKE) -fMakefile \
+ $$(for i in $(TEST_DEP); do echo -f"$${i}"; done) \
+ $$(for i in $(TEST_TGT); do echo -f"$${i}"; done) \
+ test_list
-clean_test:
- rm -f 1_triangle.stl 1_triangle_no_normal.stl 1_triangle_with_noise.stl
- rm -f empty.stl test.stl
- $(SHELL) make.sh clean_test $(TEST_SRC)
+$(TEST_TGT):
+ @{ \
+ exe="$$(basename "$@" ".t")"; \
+ printf '%s: %s\n' "$${exe}" $(@:.t=.o); \
+ printf 'test_list: %s\n' "$${exe}"; \
+ } > $@
$(TEST_DEP): config.mk sstl-local.pc
- @$(CC) -std=c89 $(CFLAGS_EXE) $(RSYS_CFLAGS) $(SSTL_CFLAGS) \
- -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@
+ @$(CC) $(CFLAGS_TEST) -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@
$(TEST_OBJ): config.mk sstl-local.pc
- $(CC) -std=c89 $(CFLAGS_EXE) $(RSYS_CFLAGS) $(SSTL_CFLAGS) -c $(@:.o=.c) -o $@
+ $(CC) $(CFLAGS_TEST) -c $(@:.o=.c) -o $@
test_sstl \
test_sstl_load_ascii \
test_sstl_load_binary \
: config.mk sstl-local.pc $(LIBNAME)
- $(CC) -std=c89 $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE) $(SSTL_LIBS) $(RSYS_LIBS) -lm
+ $(CC) $(CFLAGS_TEST) -o $@ src/$@.o $(LDFLAGS_TEST)
+
+clean_test:
+ rm -f $(TEST_DEP) $(TEST_OBJ) $(TEST_TGT)
+ rm -f 1_triangle.stl 1_triangle_no_normal.stl
+ rm -f 1_triangle_with_noise.stl empty.stl test.stl
+ for i in $(TEST_SRC); do rm -f "$$(basename "$${i}" ".c")"; done
+
+test: tests
+ @err=0; \
+ for i in $(TEST_SRC); do \
+ test="$$(basename "$${i}" ".c")"; \
+ printf '%s' "$${test}"; \
+ if "./$${test}" > /dev/null 2>&1; then \
+ printf '\n'; \
+ else \
+ printf ': error %s\n' "$$?"; \
+ err=$$((err+1)); \
+ fi \
+ done; \
+ [ "$${err}" -eq 0 ]
diff --git a/config.mk b/config.mk
@@ -7,6 +7,11 @@ LIB_TYPE = SHARED
BUILD_TYPE = RELEASE
#BUILD_TYPE = DEBUG
+BINPREFIX = $(PREFIX)/bin
+LIBPREFIX = $(PREFIX)/lib
+INCPREFIX = $(PREFIX)/include
+MANPREFIX = $(PREFIX)/share/man
+
################################################################################
# Tools
################################################################################
@@ -24,8 +29,9 @@ PCFLAGS_STATIC = --static
PCFLAGS = $(PCFLAGS_$(LIB_TYPE))
RSYS_VERSION = 0.14
-RSYS_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsys)
-RSYS_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs rsys)
+
+INCS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsys)
+LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs rsys)
################################################################################
# Compilation options
@@ -72,4 +78,4 @@ LDFLAGS_EXE = $(LDFLAGS) -pie
OCPFLAGS_DEBUG = --localize-hidden
OCPFLAGS_RELEASE = --localize-hidden --strip-unneeded
-OCPFLAGS = $(OCPFLAGS_$(BUILD_TYPE))
+BOCPFLAGS = $(OCPFLAGS_$(BUILD_TYPE))
diff --git a/make.sh b/make.sh
@@ -1,70 +0,0 @@
-#!/bin/sh
-
-# Copyright (C) 2015, 2016, 2019, 2021, 2023 |Méso|Star> (contact@meso-star.com)
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-set -e
-
-config_test()
-{
- for i in "$@"; do
- test=$(basename "${i}" ".c")
- test_list="${test_list} ${test}"
- printf "%s: src/%s.o\n" "${test}" "${test}"
- done
- printf "test_bin: %s\n" "${test_list}"
-}
-
-run_test()
-{
- for i in "$@"; do
- test=$(basename "${i}" ".c")
-
- printf "%s " "${test}"
- if ./"${test}" > /dev/null 2>&1; then
- printf "\033[1;32mOK\033[m\n"
- else
- printf "\033[1;31mError\033[m\n"
- fi
- done 2> /dev/null
-}
-
-clean_test()
-{
- for i in "$@"; do
- 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
-}
-
-"$@"