commit 43fdf3986ea9f6d43d510f2de766f140ad5fe768
parent 2cfc673dde0c06e7e2d818486b461ba5ac4487ed
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Mon, 4 Sep 2023 13:15:51 +0200
Write a POSIX Makefile to replace CMake
The build procedure is written in POSIX make, which the user can
configure via the config.mk file. The make.sh script contains commands
that could be found directly in POSIX make, but which are placed here to
simplify writing the Makefile. Finally, a pkg-config file is provided to
link the library as an external dependency.
In addition to the features already provided in its CMake alternative,
this Makefile supports the construction of static libraries and provides
an uninstall target. In any case, the main motivation behind its writing
is to use a good old well-established standard with simple features,
available on all UNIX systems, thus simplifying its portability and
support while being much lighter.
Diffstat:
| M | .gitignore | | | 19 | +++++++++++-------- |
| A | Makefile | | | 174 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | config.mk | | | 67 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | htcp.pc.in | | | 10 | ++++++++++ |
| A | make.sh | | | 71 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 files changed, 333 insertions(+), 8 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,13 +1,16 @@
.gitignore
-CMakeCache.txt
-CMakeFiles
-Makefile
-tmp
+*.nc
+*.htcp
[Bb]uild*
*.sw[po]
-*.[ao]
-*.orig
+*.[aod]
+*.so
*~
+test*
+!test*.[ch]
+.config
+.test
tags
-*.nc
-*.htcp
+*.pc
+src/les2htcp.h
+les2htcp
diff --git a/Makefile b/Makefile
@@ -0,0 +1,174 @@
+# 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/>.
+
+.POSIX:
+.SUFFIXES: # Clean up default inference rules
+
+include config.mk
+
+LIBNAME_STATIC = libhtcp.a
+LIBNAME_SHARED = libhtcp.so
+LIBNAME = $(LIBNAME_$(LIB_TYPE))
+
+################################################################################
+# Library/program building
+################################################################################
+SRC = src/htcp.c
+OBJ = $(SRC:.c=.o)
+DEP = $(SRC:.c=.d)
+
+default: build_library build_program
+
+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)
+
+build_program: .config src/les2htcp.d build_library
+ @$(MAKE) -fMakefile -fsrc/les2htcp.d les2htcp
+
+$(DEP) $(OBJ): config.mk
+
+$(LIBNAME_SHARED): $(OBJ)
+ $(CC) $(CFLAGS) $(RSYS_CFLAGS) -o $@ $(OBJ) $(LDFLAGS) $(SOFLAGS) $(RSYS_LIBS)
+
+$(LIBNAME_STATIC): $(OBJ)
+ $(AR) -rc $@ $?
+ $(RANLIB) $@
+
+src/les2htcp.d src/les2htcp.o: config.mk
+
+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) -o $@ src/les2htcp.o $(LDFLAGS) $(RSYS_LIBS) $(NETCDF_LIBS) -lm
+
+.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" > $@
+
+.SUFFIXES: .c .d .o
+.c.d:
+ @$(CC) $(CFLAGS) $(RSYS_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@
+
+.c.o:
+ $(CC) $(CFLAGS) $(RSYS_CFLAGS) -DHTCP_SHARED_BUILD -c $< -o $@
+
+src/les2htcp.d: src/les2htcp.c
+ @$(CC) $(CFLAGS) $(RSYS_CFLAGS) $(NETCDF_CFLAGS) -MM -MT "$(@:.d=.o) $@" \
+ src/les2htcp.c -MF $@
+
+src/les2htcp.o: src/les2htcp.c
+ $(CC) $(CFLAGS) $(RSYS_CFLAGS) $(NETCDF_CFLAGS) -c src/les2htcp.c -o $@
+
+################################################################################
+# Installation
+################################################################################
+pkg:
+ sed -e 's#@PREFIX@#$(PREFIX)#g'\
+ -e 's#@VERSION@#$(VERSION)#g'\
+ -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g'\
+ htcp.pc.in > htcp.pc
+
+htcp-local.pc: htcp.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'\
+ 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
+
+uninstall:
+ rm -f "$(DESTDIR)$(PREFIX)/bin/les2htcp"
+ rm -f "$(DESTDIR)$(PREFIX)/lib/$(LIBNAME)"
+ rm -f "$(DESTDIR)$(PREFIX)/lib/pkgconfig/htcp.pc"
+ 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"
+
+################################################################################
+# Miscellaneous targets
+################################################################################
+all: build_library build_program build_tests
+
+clean: clean_test
+ rm -f $(OBJ) src/les2htcp.o $(TEST_OBJ) $(LIBNAME) les2htcp .config .test htcp.pc htcp-local.pc
+
+distclean: clean
+ rm -f $(DEP) src/les2htcp.d src/les2htcp.h $(TEST_DEP)
+
+lint:
+ shellcheck -o all make.sh
+ shellcheck -o all src/dump_netcdf_data.sh
+ shellcheck -o all src/dump_netcdf_desc.sh
+
+################################################################################
+# Tests
+################################################################################
+TEST_SRC =\
+ src/test_htcp.c\
+ src/test_htcp_load.c\
+ src/test_htcp_load_from_file.c
+TEST_OBJ = $(TEST_SRC:.c=.o)
+TEST_DEP = $(TEST_SRC:.c=.d)
+
+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)
+
+test: build_tests build_program
+ @$(SHELL) make.sh run_test src/test_htcp.c src/test_htcp_load.c
+
+build_tests: build_library $(TEST_DEP) .test
+ @$(MAKE) -fMakefile -f.test $$(for i in $(TEST_DEP); do echo -f"$${i}"; done) test_bin
+
+.test: Makefile make.sh
+ @$(SHELL) make.sh config_test $(TEST_SRC) > $@
+
+clean_test:
+ $(SHELL) make.sh clean_test $(TEST_SRC)
+
+$(TEST_DEP): config.mk htcp-local.pc
+ @$(CC) $(CFLAGS) $(RSYS_CFLAGS) $(HTCP_CFLAGS) \
+ -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@
+
+$(TEST_OBJ): config.mk htcp-local.pc
+ $(CC) $(CFLAGS) $(RSYS_CFLAGS) $(HTCP_CFLAGS) -c $(@:.o=.c) -o $@
+
+test_htcp \
+test_htcp_load \
+test_htcp_load_from_file \
+: config.mk htcp-local.pc
+ $(CC) -o $@ src/$@.o $(RSYS_LIBS) $(HTCP_LIBS) -lm
diff --git a/config.mk b/config.mk
@@ -0,0 +1,67 @@
+VERSION_MAJOR = 0
+VERSION_MINOR = 0
+VERSION_PATCH = 5
+VERSION = $(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH)
+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_SHARED =
+PCFLAGS_STATIC = --static
+PCFLAGS = $(PCFLAGS_$(LIB_TYPE))
+
+RSYS_VERSION = 0.6
+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
+################################################################################
+WFLAGS =\
+ -Wall\
+ -Wcast-align\
+ -Wconversion\
+ -Wextra\
+ -Wmissing-declarations\
+ -Wmissing-prototypes\
+ -Wshadow
+
+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_DEBUG =
+LDFLAGS_RELEASE = -s
+LDFLAGS = $(LDFLAGS_$(BUILD_TYPE))
diff --git a/htcp.pc.in b/htcp.pc.in
@@ -0,0 +1,10 @@
+prefix=@PREFIX@
+includedir=${prefix}/include
+libdir=${prefix}/lib
+
+Requires: rsys >= @RSYS_VERSION@
+Name: htcp
+Description: High-Tune Cloud Properties
+Version: @VERSION@
+Libs: -L${libdir} -lhtcp
+CFlags: -I${includedir}
diff --git a/make.sh b/make.sh
@@ -0,0 +1,71 @@
+#!/bin/sh -e
+
+# 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/>.
+
+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 "\e[1;32mOK\e[m\n"
+ else
+ printf "\e[1;31mError\e[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
+}
+
+"$@"