commit 8a86b1ec4abb67aa76134a6efebf4c5e86d9f128
parent 1de3c933256485d5145e4b8680afc0acbd80cc00
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Thu, 7 Sep 2023 18:15:38 +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 | | | 15 | ++++++++------- |
| A | Makefile | | | 159 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | config.mk | | | 67 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | htmie.pc.in | | | 11 | +++++++++++ |
| A | make.sh | | | 69 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | src/dump_netcdf_data.sh | | | 2 | +- |
6 files changed, 315 insertions(+), 8 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,12 +1,13 @@
.gitignore
-CMakeCache.txt
-CMakeFiles
-Makefile
-tmp
[Bb]uild*
*.sw[po]
-*.[ao]
-*.orig
+*.[aod]
+*.so
*~
+test*
+!test*.[ch]
+.config
+.test
tags
-*.htcp
+*.pc
+Mie_LUT_Cloud_*
diff --git a/Makefile b/Makefile
@@ -0,0 +1,159 @@
+# 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 = libhtmie.a
+LIBNAME_SHARED = libhtmie.so
+LIBNAME = $(LIBNAME_$(LIB_TYPE))
+
+################################################################################
+# Library building
+################################################################################
+SRC = src/htmie.c
+OBJ = $(SRC:.c=.o)
+DEP = $(SRC:.c=.d)
+
+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)
+
+$(DEP) $(OBJ): config.mk
+
+$(LIBNAME_SHARED): $(OBJ)
+ $(CC) $(CFLAGS) $(DPDC_CFLAGS) -o $@ $(OBJ) $(LDFLAGS) $(SOFLAGS) $(DPDC_LIBS)
+
+$(LIBNAME_STATIC): $(OBJ)
+ $(AR) -rc $@ $?
+ $(RANLIB) $@
+
+.config: config.mk
+ @if ! $(PKG_CONFIG) --atleast-version $(NETCDF_VERSION) netcdf; then \
+ echo "netcdf $(NETCDF_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
+ @echo "config done" > $@
+
+.SUFFIXES: .c .d .o
+.c.d:
+ @$(CC) $(CFLAGS) $(DPDC_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@
+
+.c.o:
+ $(CC) $(CFLAGS) $(DPDC_CFLAGS) -DHTMIE_SHARED_BUILD -c $< -o $@
+
+################################################################################
+# Installation
+################################################################################
+pkg:
+ sed -e 's#@PREFIX@#$(PREFIX)#g'\
+ -e 's#@VERSION@#$(VERSION)#g'\
+ -e 's#@RNETCDF_VERSION@#$(NETCDF_VERSION)#g'\
+ -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g'\
+ htmie.pc.in > htmie.pc
+
+htmie-local.pc: htmie.pc.in
+ sed -e '1d'\
+ -e 's#^includedir=.*#includedir=./src/#'\
+ -e 's#^libdir=.*#libdir=./#'\
+ -e 's#@VERSION@#$(VERSION)#g'\
+ -e 's#@RNETCDF_VERSION@#$(NETCDF_VERSION)#g'\
+ -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g'\
+ htmie.pc.in > $@
+
+install: build_library pkg
+ @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib" $(LIBNAME)
+ @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib/pkgconfig" htmie.pc
+ @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/include/high_tune" src/htmie.h
+ @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/doc/htmie" COPYING README.md
+ @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/man/man5" htmie.5
+
+uninstall:
+ rm -f "$(DESTDIR)$(PREFIX)/lib/$(LIBNAME)"
+ rm -f "$(DESTDIR)$(PREFIX)/lib/pkgconfig/htmie.pc"
+ rm -f "$(DESTDIR)$(PREFIX)/share/doc/htmie/COPYING"
+ rm -f "$(DESTDIR)$(PREFIX)/share/doc/htmie/README.md"
+ rm -f "$(DESTDIR)$(PREFIX)/include/high_tune/htmie.h"
+ rm -f "$(DESTDIR)$(PREFIX)/share/man/man5/htmie.5"
+
+################################################################################
+# Miscellaneous targets
+################################################################################
+all: build_library build_tests
+
+clean: clean_test
+ rm -f $(OBJ) $(TEST_OBJ) $(LIBNAME) .config .test htmie.pc htmie-local.pc
+
+distclean: clean
+ rm -f $(DEP) $(TEST_DEP)
+
+lint:
+ shellcheck -o all make.sh
+ shellcheck -o all src/dump_netcdf_data.sh
+# mandoc -Tlint -Wall htmie.5
+
+################################################################################
+# Tests
+################################################################################
+TEST_SRC = src/test_htmie.c
+TEST_OBJ = $(TEST_SRC:.c=.o)
+TEST_DEP = $(TEST_SRC:.c=.d)
+TEST_MIE_LUT = Mie_LUT_Cloud
+TEST_MIE_LUT_FILE =$(TEST_MIE_LUT).nc
+TEST_MIE_LUT_VARS =\
+ $(TEST_MIE_LUT)_lambda\
+ $(TEST_MIE_LUT)_macs\
+ $(TEST_MIE_LUT)_mscs\
+ $(TEST_MIE_LUT)_rmod\
+ $(TEST_MIE_LUT)_smod
+
+PKG_CONFIG_LOCAL = PKG_CONFIG_PATH="./:$${PKG_CONFIG_PATH}" $(PKG_CONFIG)
+HTMIE_CFLAGS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags htmie-local.pc)
+HTMIE_LIBS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs htmie-local.pc)
+
+test: build_tests $(TEST_MIE_LUT_VARS)
+ @$(SHELL) make.sh check test_htmie $(TEST_MIE_LUT_FILE)
+
+$(TEST_MIE_LUT_VARS): $(TEST_MIE_LUT_FILE) src/dump_netcdf_data.sh
+ $(SHELL) src/dump_netcdf_data.sh \
+ $$(echo $@ | sed 's/$(TEST_MIE_LUT)_\(.\{1,\}\)$$/\1/g') $(TEST_MIE_LUT_FILE) > $@
+
+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)
+ rm -rf $(TEST_MIE_LUT_VARS)
+
+$(TEST_DEP): config.mk htmie-local.pc
+ @$(CC) $(CFLAGS) $(RSYS_CFLAGS) $(HTMIE_CFLAGS) \
+ -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@
+
+$(TEST_OBJ): config.mk htmie-local.pc
+ $(CC) $(CFLAGS) $(RSYS_CFLAGS) $(HTMIE_CFLAGS) -c $(@:.o=.c) -o $@
+
+test_htmie: config.mk htmie-local.pc
+ $(CC) -o $@ src/$@.o $(RSYS_LIBS) $(HTMIE_LIBS) -lm
diff --git a/config.mk b/config.mk
@@ -0,0 +1,67 @@
+VERSION = 0.0.4
+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))
+
+NETCDF_VERSION = 4
+NETCDF_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags netcdf)
+NETCDF_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs netcdf)
+
+RSYS_VERSION = 0.6
+RSYS_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsys)
+RSYS_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs rsys)
+
+DPDC_CFLAGS = $(NETCDF_CFLAGS) $(RSYS_CFLAGS)
+DPDC_LIBS = $(NETCDF_LIBS) $(RSYS_LIBS)
+
+################################################################################
+# 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/htmie.pc.in b/htmie.pc.in
@@ -0,0 +1,11 @@
+prefix=@PREFIX@
+includedir=${prefix}/include
+libdir=${prefix}/lib
+
+Requires: rsys >= @RSYS_VERSION@
+Requires.private: netcdf >= @NETCDF_VERSION@
+Name: htmie
+Description: High-Tune Mie
+Version: @VERSION@
+Libs: -L${libdir} -lhtmie
+CFlags: -I${includedir}
diff --git a/make.sh b/make.sh
@@ -0,0 +1,69 @@
+#!/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}"
+}
+
+check()
+{
+ prog="$1"
+ shift 1
+
+ printf "%s " "${prog}"
+ if ./"${prog}" "$@" > /dev/null 2>&1; then
+ printf "\e[1;32mOK\e[m\n"
+ else
+ printf "\e[1;31mError\e[m\n"
+ fi
+}
+
+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
+}
+
+"$@"
diff --git a/src/dump_netcdf_data.sh b/src/dump_netcdf_data.sh
@@ -37,5 +37,5 @@ ncdump -v "$1" "$2" \
| sed "s/^${blanks}$1${blanks}=${blanks}//g" \
| sed "s/[;} ]//g" \
| sed "s/,/\n/g" \
- | sed "/^${blanks}$/d" > "${name}_${1}"
+ | sed "/^${blanks}$/d"