commit 12f7fcdac1cb3c8897c364307ef964edf2220c31
parent bf1f946b125fdee45b7c0569f6b0c195d34d66d6
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Thu, 16 Oct 2025 16:50:13 +0200
Improve Makefile readability and portability
Group all pc dependencies for each executable into a single call to the
pkg-config utility. This not only reduces the (small) workload
associated with multiple calls to pkg-config, but also makes the way
CFLAGS and LIBS dependencies are defined more concise.
Simplify prerequisite checking by displaying only the pkg-config command
line used to check the version rather than displaying a handwritten
error message if it fails. If one of the commands fails, the
configuration step stops at the given command, which displays the
corresponding error message. There is therefore no need to do anything
further.
Addition of macros enabling the user to define the subpaths in which
binaries, libraries, documentation and manuals are installed, in
relation to the PREFIX macro. Depend ending on the system, the expected
file hiearchy can be different.
Remove distclean targets. These were not only misused, as they deleted
files that should have been cleaned up by the normal "clean" target. But
they were also useless because no dist target is provided. And there are
no plans to provide one, since htrdr is designed to be installed from
source.
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.
Diffstat:
| M | Makefile | | | 178 | ++++++++++++++++++++++++++++++++++++++++++++++--------------------------------- |
| M | config.mk | | | 11 | ++++++----- |
| D | make.sh | | | 73 | ------------------------------------------------------------------------- |
3 files changed, 110 insertions(+), 152 deletions(-)
diff --git a/Makefile b/Makefile
@@ -24,16 +24,20 @@ LIBNAME_STATIC = libhtcp.a
LIBNAME_SHARED = libhtcp.so
LIBNAME = $(LIBNAME_$(LIB_TYPE))
+default: library util
+all: default tests
+
################################################################################
-# Library/program building
+# Library building
################################################################################
SRC = src/htcp.c
OBJ = $(SRC:.c=.o)
DEP = $(SRC:.c=.d)
-default: build_library build_program
+CFLAGS_LIB = $(CFLAGS_SO) $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsys) -DHTCP_SHARED_BUILD
+LDFLAGS_LIB = $(LDFLAGS_SO) $$($(PKG_CONFIG) $(PCFLAGS) --libs rsys)
-build_library: .config $(DEP)
+library: .config $(DEP)
@$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f $${i}; done) \
$$(if [ -n "$(LIBNAME)" ]; then \
echo "$(LIBNAME)"; \
@@ -41,13 +45,10 @@ build_library: .config $(DEP)
echo "$(LIBNAME_SHARED)"; \
fi)
-build_program: .config src/les2htcp.d build_library
- @$(MAKE) -fMakefile -fsrc/les2htcp.d les2htcp
-
$(DEP) $(OBJ): config.mk
$(LIBNAME_SHARED): $(OBJ)
- $(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -o $@ $(OBJ) $(LDFLAGS_SO) $(RSYS_LIBS)
+ $(CC) $(CFLAGS_LIB) -o $@ $(OBJ) $(LDFLAGS_LIB)
$(LIBNAME_STATIC): libhtcp.o
$(AR) -rc $@ $?
@@ -57,42 +58,49 @@ libhtcp.o: $(OBJ)
$(LD) -r $(OBJ) -o $@
$(OBJCOPY) $(OCPFLAGS) $@
-src/les2htcp.d src/les2htcp.o: config.mk
+.config: config.mk
+ $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys
+ $(PKG_CONFIG) --atleast-version $(NETCDF_VERSION) netcdf
+ echo "config done" > $@
+
+.SUFFIXES: .c .d .o
+.c.d:
+ @$(CC) $(CFLAGS_LIB) -MM -MT "$(@:.d=.o) $@" $< -MF $@
+
+.c.o:
+ $(CC) $(CFLAGS_LIB) -c $< -o $@
+
+################################################################################
+# Utils
+################################################################################
+UTIL_SRC = src/les2htcp.c
+UTIL_OBJ = $(UTIL_SRC:.c=.o)
+UTIL_DEP = $(UTIL_SRC:.c=.d)
+
+CFLAGS_UTIL = $(CFLAGS_EXE) $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsys netcdf)
+LDFLAGS_UTIL = $(LDFLAGS_EXE) $$($(PKG_CONFIG) $(PCFLAGS) --libs rsys netcdf) -lm
+
+util: library .config $(UTIL_DEP)
+ @$(MAKE) -fMakefile -f"$(UTIL_DEP)" les2htcp
+
-src/les2htcp.c: src/les2htcp.h
src/les2htcp.h: src/les2htcp.h.in
sed -e 's#@VERSION_MAJOR@#$(VERSION_MAJOR)#g' \
-e 's#@VERSION_MINOR@#$(VERSION_MINOR)#g' \
-e 's#@VERSION_PATCH@#$(VERSION_PATCH)#g' \
src/les2htcp.h.in > $@
-les2htcp: src/les2htcp.o
- $(CC) $(CFLAGS_EXE) $(RSYS_CFLAGS) $(NETCDF_CFLAGS) \
- -o $@ src/les2htcp.o $(LDFLAGS_EXE) $(RSYS_LIBS) $(NETCDF_LIBS) -lm
+les2htcp: config.mk $(UTIL_OBJ)
+ $(CC) $(CFLAGS_UTIL) -o $@ $(UTIL_OBJ) $(LDFLAGS_UTIL)
-.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 $(NETCDF_VERSION) netcdf; then \
- echo "netcdf $(NETCDF_VERSION) not found" >&2; exit 1; fi
- @echo "config done" > $@
+$(UTIL_DEP): config.mk
+ @$(CC) $(CFLAGS_UTIL) -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@
-.SUFFIXES: .c .d .o
-.c.d:
- @$(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@
-
-.c.o:
- $(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -DHTCP_SHARED_BUILD -c $< -o $@
-
-src/les2htcp.d: src/les2htcp.c
- @$(CC) $(CFLAGS_EXE) $(RSYS_CFLAGS) $(NETCDF_CFLAGS) -MM -MT "$(@:.d=.o) $@" \
- src/les2htcp.c -MF $@
-
-src/les2htcp.o: src/les2htcp.c
- $(CC) $(CFLAGS_EXE) $(RSYS_CFLAGS) $(NETCDF_CFLAGS) -c src/les2htcp.c -o $@
+$(UTIL_OBJ): config.mk
+ $(CC) $(CFLAGS_UTIL) -c $(@:.o=.c) -o $@
################################################################################
-# Installation
+# Miscellaneous
################################################################################
pkg:
sed -e 's#@PREFIX@#$(PREFIX)#g'\
@@ -108,39 +116,38 @@ htcp-local.pc: htcp.pc.in
-e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g'\
htcp.pc.in > $@
-install: build_library build_program pkg
- @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/bin" les2htcp
- @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib" $(LIBNAME)
- @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib/pkgconfig" htcp.pc
- @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/include/high_tune" src/htcp.h
- @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/doc/htcp" COPYING README.md
- @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/man/man1" les2htcp.1
- @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/man/man5" htcp.5
+install: library util pkg
+ install() { mode="$$1"; prefix="$$2"; shift 2; \
+ mkdir -p "$${prefix}"; \
+ cp "$$@" "$${prefix}"; \
+ printf '%s\n' "$${@}" | while read -r i; do \
+ chmod "$${mode}" "$${prefix}/$${i##*/}"; \
+ done; \
+ }; \
+ install 755 "$(DESTDIR)$(LIBPREFIX)" $(LIBNAME); \
+ install 755 "$(DESTDIR)$(BINPREFIX)" les2htcp; \
+ install 644 "$(DESTDIR)$(LIBPREFIX)/pkgconfig" htcp.pc; \
+ install 644 "$(DESTDIR)$(INCPREFIX)/high_tune" src/htcp.h; \
+ install 644 "$(DESTDIR)$(MANPREFIX)/man1" les2htcp.1; \
+ install 644 "$(DESTDIR)$(MANPREFIX)/man5" htcp.5; \
+ install 644 "$(DESTDIR)$(PREFIX)/share/doc/htcp" COPYING README.md
uninstall:
- rm -f "$(DESTDIR)$(PREFIX)/bin/les2htcp"
- rm -f "$(DESTDIR)$(PREFIX)/lib/$(LIBNAME)"
- rm -f "$(DESTDIR)$(PREFIX)/lib/pkgconfig/htcp.pc"
+ rm -f "$(DESTDIR)$(LIBPREFIX)/$(LIBNAME)"
+ rm -f "$(DESTDIR)$(BINPREFIX)/les2htcp"
+ rm -f "$(DESTDIR)$(LIBPREFIX)/pkgconfig/htcp.pc"
+ rm -f "$(DESTDIR)$(INCPREFIX)/high_tune/htcp.h"
+ rm -f "$(DESTDIR)$(MANPREFIX)/man1/les2htcp.1"
+ rm -f "$(DESTDIR)$(MANPREFIX)/man5/htcp.5"
rm -f "$(DESTDIR)$(PREFIX)/share/doc/htcp/COPYING"
rm -f "$(DESTDIR)$(PREFIX)/share/doc/htcp/README.md"
- rm -f "$(DESTDIR)$(PREFIX)/include/high_tune/htcp.h"
- rm -f "$(DESTDIR)$(PREFIX)/share/man/man1/les2htcp.1"
- rm -f "$(DESTDIR)$(PREFIX)/share/man/man5/htcp.5"
-
-################################################################################
-# Miscellaneous targets
-################################################################################
-all: build_library build_program build_tests
clean: clean_test
- rm -f $(OBJ) src/les2htcp.o $(TEST_OBJ) $(LIBNAME) les2htcp
- rm -f .config .test libhtcp.o htcp.pc htcp-local.pc
-
-distclean: clean
- rm -f $(DEP) src/les2htcp.d src/les2htcp.h $(TEST_DEP)
+ rm -f $(DEP) $(OBJ) $(LIBNAME)
+ rm -f $(UTIL_DEP) $(UTIL_OBJ) les2htcp
+ rm -f .config libhtcp.o htcp.pc htcp-local.pc
lint:
- shellcheck -o all make.sh
shellcheck -o all src/dump_netcdf_data.sh
shellcheck -o all src/dump_netcdf_desc.sh
shellcheck -o all src/test_htcp_load_from_file.sh
@@ -150,38 +157,61 @@ lint:
################################################################################
# Tests
################################################################################
-TEST_SRC =\
+TEST_SRC_REGULAR =\
src/test_htcp.c\
- src/test_htcp_load.c\
+ src/test_htcp_load.c
+TEST_SRC =\
+ $(TEST_SRC_REGULAR)\
src/test_htcp_load_from_file.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)
-HTCP_CFLAGS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags htcp-local.pc)
-HTCP_LIBS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs htcp-local.pc)
+INCS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags htcp-local rsys)
+LIBS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs htcp-local rsys)
-test: build_tests build_program
- @$(SHELL) make.sh run_test src/test_htcp.c src/test_htcp_load.c
+CFLAGS_TEST = $(CFLAGS_EXE) $(INCS_TEST)
+LDFLAGS_TEST = $(LDFLAGS_EXE) $(LIBS_TEST) -lm
-build_tests: build_library $(TEST_DEP) .test
- @$(MAKE) -fMakefile -f.test $$(for i in $(TEST_DEP); do echo -f"$${i}"; done) test_bin
+tests: library $(TEST_DEP) $(TEST_TGT)
+ @$(MAKE) -fMakefile \
+ $$(for i in $(TEST_DEP); do echo -f"$${i}"; done) \
+ $$(for i in $(TEST_TGT); do echo -f"$${i}"; done) \
+ test_list
-.test: Makefile make.sh
- @$(SHELL) make.sh config_test $(TEST_SRC) > $@
-
-clean_test:
- $(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 htcp-local.pc
- @$(CC) $(CFLAGS_EXE) $(HTCP_CFLAGS) $(RSYS_CFLAGS) \
- -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@
+ @$(CC) $(CFLAGS_TEST) -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@
$(TEST_OBJ): config.mk htcp-local.pc
- $(CC) $(CFLAGS_EXE) $(HTCP_CFLAGS) $(RSYS_CFLAGS) -c $(@:.o=.c) -o $@
+ $(CC) $(CFLAGS_TEST) -c $(@:.o=.c) -o $@
test_htcp \
test_htcp_load \
test_htcp_load_from_file \
: config.mk htcp-local.pc $(LIBNAME)
- $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE) $(HTCP_LIBS) $(RSYS_LIBS) -lm
+ $(CC) $(CFLAGS_TEST) -o $@ src/$@.o $(LDFLAGS_TEST)
+
+clean_test:
+ rm -f $(TEST_DEP) $(TEST_OBJ) $(TEST_TGT)
+ for i in $(TEST_SRC); do rm -f "$$(basename "$${i}" ".c")"; done
+
+test: tests
+ @err=0; \
+ for i in $(TEST_SRC_REGULAR); do \
+ test="$$(basename "$${i}" ".c")"; \
+ if "./$${test}" > /dev/null 2>&1; then \
+ printf '%s\n' "$${test}"; \
+ else \
+ >&2 printf '%s: error %s\n' "$${test}" "$$?"; \
+ err=$$((err+1)); \
+ fi \
+ done; \
+ [ "$${err}" -eq 0 ]
diff --git a/config.mk b/config.mk
@@ -2,6 +2,7 @@ VERSION_MAJOR = 0
VERSION_MINOR = 1
VERSION_PATCH = 0
VERSION = $(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH)
+
PREFIX = /usr/local
LIB_TYPE = SHARED
@@ -10,6 +11,11 @@ LIB_TYPE = SHARED
BUILD_TYPE = RELEASE
#BUILD_TYPE = DEBUG
+BINPREFIX = $(PREFIX)/bin
+LIBPREFIX = $(PREFIX)/lib
+INCPREFIX = $(PREFIX)/include
+MANPREFIX = $(PREFIX)/share/man
+
################################################################################
# Tools
################################################################################
@@ -28,12 +34,7 @@ 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)
-
NETCDF_VERSION = 4
-NETCDF_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags netcdf)
-NETCDF_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs netcdf)
################################################################################
# Compilation options
diff --git a/make.sh b/make.sh
@@ -1,73 +0,0 @@
-#!/bin/sh
-
-# Copyright (C) 2018, 2020-2023 |Méso|Star> (contact@meso-star.com)
-# Copyright (C) 2018 Centre National de la Recherche Scientifique
-# Copyright (C) 2018 Université Paul Sabatier
-#
-# 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: %s\n" "${test}" "src/${test}.o"
- 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
-}
-
-"$@"