commit 137ec1aa680d5d15817160ac199c930fdf3a88d8
parent af3a3cdba9e503c6e664ce8826a4be8a836ba383
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Mon, 30 Oct 2023 12:36:56 +0100
Merge branch 'feature_posix_make' into develop
Diffstat:
| M | .gitignore | | | 15 | ++++++++------- |
| A | Makefile | | | 162 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | README.md | | | 37 | +++++++++++++++++++------------------ |
| D | cmake/CMakeLists.txt | | | 133 | ------------------------------------------------------------------------------- |
| A | config.mk | | | 77 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | htgop.pc.in | | | 11 | +++++++++++ |
| A | make.sh | | | 69 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
7 files changed, 346 insertions(+), 158 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
-
+*.pc
+etc/
diff --git a/Makefile b/Makefile
@@ -0,0 +1,162 @@
+# Copyright (C) 2018-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/>.
+
+.POSIX:
+.SUFFIXES: # Clean up default inference rules
+
+include config.mk
+
+LIBNAME_STATIC = libhtgop.a
+LIBNAME_SHARED = libhtgop.so
+LIBNAME = $(LIBNAME_$(LIB_TYPE))
+
+################################################################################
+# Library building
+################################################################################
+SRC = src/htgop.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_SO) $(RSYS_CFLAGS) -o $@ $(OBJ) $(LDFLAGS_SO) $(RSYS_LIBS) -lm
+
+$(LIBNAME_STATIC): libhtgop.o
+ $(AR) -rc $@ $?
+ $(RANLIB) $@
+
+libhtgop.o: $(OBJ)
+ $(LD) -r $(OBJ) -o $@
+ $(OBJCOPY) $(OCPFLAGS) $@
+
+.config: config.mk
+ @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_SO) $(RSYS_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@
+
+.c.o:
+ $(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -DHTGOP_SHARED_BUILD -c $< -o $@
+
+################################################################################
+# Installation
+################################################################################
+pkg:
+ sed -e 's#@PREFIX@#$(PREFIX)#g'\
+ -e 's#@VERSION@#$(VERSION)#g'\
+ -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g'\
+ htgop.pc.in > htgop.pc
+
+htgop-local.pc: htgop.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'\
+ htgop.pc.in > $@
+
+install: build_library pkg
+ @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib" $(LIBNAME)
+ @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib/pkgconfig" htgop.pc
+ @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/include/high_tune/" src/htgop.h
+ @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/doc/htgop" COPYING README.md
+
+uninstall:
+ rm -f "$(DESTDIR)$(PREFIX)/lib/$(LIBNAME)"
+ rm -f "$(DESTDIR)$(PREFIX)/lib/pkgconfig/htgop.pc"
+ rm -f "$(DESTDIR)$(PREFIX)/share/doc/htgop/COPYING"
+ rm -f "$(DESTDIR)$(PREFIX)/share/doc/htgop/README.md"
+ rm -f "$(DESTDIR)$(PREFIX)/include/high_tune/htgop.h"
+
+################################################################################
+# Miscellaneous targets
+################################################################################
+all: build_library build_tests
+
+clean: clean_test
+ rm -f $(OBJ) $(TEST_OBJ) $(LIBNAME)
+ rm -f .config .test libhtgop.o htgop.pc htgop-local.pc
+
+distclean: clean
+ rm -f $(DEP) $(TEST_DEP)
+ rm -f etc/ecrad_opt_prop.txt
+
+lint:
+ shellcheck -o all make.sh
+
+################################################################################
+# Tests
+################################################################################
+TEST_SRC =\
+ src/test_htgop.c\
+ src/test_htgop_fetch_radiative_properties.c\
+ src/test_htgop_get_radiative_properties_bounds.c\
+ src/test_htgop_load.c\
+ src/test_htgop_sample.c
+TEST_OBJ = $(TEST_SRC:.c=.o)
+TEST_DEP = $(TEST_SRC:.c=.d)
+TEST_FILE=etc/ecrad_opt_prop.txt
+
+PKG_CONFIG_LOCAL = PKG_CONFIG_PATH="./:$${PKG_CONFIG_PATH}" $(PKG_CONFIG)
+HTGOP_CFLAGS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags htgop-local.pc)
+HTGOP_LIBS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs htgop-local.pc)
+
+test: build_tests etc/ecrad_opt_prop.txt
+ @$(SHELL) make.sh check test_htgop
+ @$(SHELL) make.sh check test_htgop_fetch_radiative_properties "$(TEST_FILE)"
+ @$(SHELL) make.sh check test_htgop_get_radiative_properties_bounds "$(TEST_FILE)"
+ @$(SHELL) make.sh check test_htgop_load "$(TEST_FILE)"
+ @$(SHELL) make.sh check test_htgop_sample "$(TEST_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)
+
+$(TEST_DEP): config.mk htgop-local.pc
+ @$(CC) $(CFLAGS_EXE) $(HTGOP_CFLAGS) $(RSYS_CFLAGS) \
+ -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@
+
+$(TEST_OBJ): config.mk htgop-local.pc
+ $(CC) $(CFLAGS_EXE) $(HTGOP_CFLAGS) $(RSYS_CFLAGS) -c $(@:.o=.c) -o $@
+
+test_htgop \
+test_htgop_fetch_radiative_properties \
+test_htgop_get_radiative_properties_bounds \
+test_htgop_load \
+test_htgop_sample \
+: config.mk htgop-local.pc $(LIBNAME)
+ $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE) $(HTGOP_LIBS) $(RSYS_LIBS) -lm
+
+# Touch the target to update its timestamp. Otherwise, the extracted file will
+# have a timestamp lower than its prerequisite, forcing its re-extraction.
+etc/ecrad_opt_prop.txt: etc.tgz
+ tar xzvf etc.tgz && touch $@
diff --git a/README.md b/README.md
@@ -1,22 +1,22 @@
# High-Tune: Gas Optical Properties
-This library loads the optical properties of a gas.
+This C library loads the optical properties of a gas mixture stored in htgop
+format. See
+[gas_opt_prop_en.pdf](https://www.meso-star.com/projects/htrdr/downloads/gas_opt_prop_en.pdf)
+for format specification.
-## How to build
+## Requirements
-This program relies on the [CMake](http://www.cmake.org) and the
-[RCMake](https://gitlab.com/vaplv/rcmake/) packages to build.
-It also depends on the
-[RSys](https://gitlab.com/vaplv/rsys/),
-library.
+- C compiler
+- POSIX make
+- pkg-config
+- [RSys](https://gitlab.com/vaplv/rsys)
-First ensure that CMake is installed on your system. Then install the RCMake
-package as well as the aforementioned prerequisites. Finally generate the
-project from the `cmake/CMakeLists.txt` file by appending to the
-`CMAKE_PREFIX_PATH` variable the install directories of its dependencies. The
-resulting project can be edited, built, tested and installed as any CMake
-project. Refer to the [CMake](https://cmake.org/documentation) for further
-informations on CMake.
+## Installation
+
+Edit config.mk as needed, then run:
+
+ make clean install
## Release notes
@@ -48,8 +48,9 @@ radiation is whether external or internal to the medium, respectively.
## Licenses
-Copyright (C) 2018-2021, 2023 |Méso|Star> (contact@meso-star.com). htgop is
-free software released under the GPL v3+ license: GNU GPL version 3 or later.
-You are welcome to redistribute it under certain conditions; refer to the
-COPYING file for details.
+Copyright (C) 2018-2021, 2023 |Méso|Star> (contact@meso-star.com)
+
+htgop is free software released under the GPL v3+ license: GNU GPL version 3 or
+later. You are welcome to redistribute it under certain conditions; refer to
+the COPYING file for details.
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -1,133 +0,0 @@
-# Copyright (C) 2018-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/>.
-
-cmake_minimum_required(VERSION 3.1)
-project(htgop C)
-enable_testing()
-
-set(HTGOP_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src)
-option(NO_TEST "Do not build tests" OFF)
-
-################################################################################
-# Check dependencies
-################################################################################
-find_package(RCMake 0.3 REQUIRED)
-find_package(RSys 0.7 REQUIRED)
-
-set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RCMAKE_SOURCE_DIR})
-include(rcmake)
-include(rcmake_runtime)
-
-include_directories(${RSys_INCLUDE_DIR})
-
-################################################################################
-# Configure and define targets
-################################################################################
-set(VERSION_MAJOR 0)
-set(VERSION_MINOR 1)
-set(VERSION_PATCH 2)
-set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
-
-set(HTGOP_FILES_SRC
- htgop.c)
-set(HTGOP_FILES_INC
- htgop_c.h
- htgop_fetch_radiative_properties.h
- htgop_get_radiative_properties_bounds.h
- htgop_layer.h
- htgop_parse_layers_spectral_intervals_data.h
- htgop_reader.h
- htgop_spectral_intervals.h)
-set(HTGOP_FILES_INC_API
- htgop.h)
-
-set(HTGOP_FILES_DOC COPYING README.md)
-
-# Prepend each file in the `HTGOP_FILES_<SRC|INC>' list by `HTGOP_SOURCE_DIR'
-rcmake_prepend_path(HTGOP_FILES_SRC ${HTGOP_SOURCE_DIR})
-rcmake_prepend_path(HTGOP_FILES_INC ${HTGOP_SOURCE_DIR})
-rcmake_prepend_path(HTGOP_FILES_INC_API ${HTGOP_SOURCE_DIR})
-rcmake_prepend_path(HTGOP_FILES_DOC ${PROJECT_SOURCE_DIR}/../)
-
-add_library(htgop SHARED ${HTGOP_FILES_SRC} ${HTGOP_FILES_INC} ${HTGOP_FILES_INC_API})
-target_link_libraries(htgop RSys)
-
-if(CMAKE_COMPILER_IS_GNUCC)
- target_link_libraries(htgop m)
-endif()
-
-set_target_properties(htgop PROPERTIES
- DEFINE_SYMBOL HTGOP_SHARED_BUILD
- VERSION ${VERSION}
- SOVERSION ${VERSION_MAJOR})
-
-rcmake_setup_devel(htgop HTGOP ${VERSION} high_tune/htgop_version.h)
-
-################################################################################
-# Add tests
-################################################################################
-if(NOT NO_TEST)
- set(HTGOP_ETC_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/etc/)
- get_filename_component(_etc_src "${PROJECT_SOURCE_DIR}/../etc.tgz" ABSOLUTE)
- add_custom_command(
- OUTPUT etc.stamp
- COMMAND ${CMAKE_COMMAND} -E tar xzf "${_etc_src}"
- COMMAND ${CMAKE_COMMAND} -E touch etc.stamp
- DEPENDS "${_etc_src}"
- COMMENT "Extract ${_etc_src}"
- VERBATIM)
- add_custom_target(extract-etc ALL DEPENDS etc.stamp)
-
- function(build_test _name)
- add_executable(${_name}
- ${HTGOP_SOURCE_DIR}/${_name}.c
- ${HTGOP_SOURCE_DIR}/test_htgop_utils.h)
- target_link_libraries(${_name} htgop RSys)
- endfunction()
-
- function(new_test _name)
- build_test(${_name})
- add_test(${_name} ${_name})
- endfunction()
-
- new_test(test_htgop)
- build_test(test_htgop_fetch_radiative_properties)
- build_test(test_htgop_get_radiative_properties_bounds)
- build_test(test_htgop_load)
- build_test(test_htgop_sample)
-
- add_test(test_htgop_fetch_radiative_properties
- test_htgop_fetch_radiative_properties
- ${HTGOP_ETC_DIRECTORY}/ecrad_opt_prop.txt)
- add_test(test_htgop_get_radiative_properties_bounds
- test_htgop_get_radiative_properties_bounds
- ${HTGOP_ETC_DIRECTORY}/ecrad_opt_prop.txt)
- add_test(test_htgop_load
- test_htgop_load ${HTGOP_ETC_DIRECTORY}/ecrad_opt_prop.txt)
- add_test(test_htgop_sample
- test_htgop_sample ${HTGOP_ETC_DIRECTORY}/ecrad_opt_prop.txt)
-
-endif()
-
-################################################################################
-# Define output & install directories
-################################################################################
-install(TARGETS htgop
- ARCHIVE DESTINATION bin
- LIBRARY DESTINATION lib
- RUNTIME DESTINATION bin)
-install(FILES ${HTGOP_FILES_INC_API} DESTINATION include/high_tune)
-install(FILES ${HTGOP_FILES_DOC} DESTINATION share/doc/htgop)
-
diff --git a/config.mk b/config.mk
@@ -0,0 +1,77 @@
+VERSION = 0.1.2
+PREFIX = /usr/local
+
+LIB_TYPE = SHARED
+#LIB_TYPE = STATIC
+
+BUILD_TYPE = RELEASE
+#BUILD_TYPE = DEBUG
+
+################################################################################
+# Tools
+################################################################################
+AR = ar
+CC = cc
+LD = ld
+OBJCOPY = objcopy
+PKG_CONFIG = pkg-config
+RANLIB = ranlib
+
+################################################################################
+# Dependencies
+################################################################################
+PCFLAGS_SHARED =
+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)
+
+################################################################################
+# Compilation options
+################################################################################
+WFLAGS =\
+ -Wall\
+ -Wcast-align\
+ -Wconversion\
+ -Wextra\
+ -Wmissing-declarations\
+ -Wmissing-prototypes\
+ -Wshadow
+
+CFLAGS_HARDENED =\
+ -D_FORTIFY_SOURCES=2\
+ -fcf-protection=full\
+ -fstack-clash-protection\
+ -fstack-protector-strong
+
+CFLAGS_COMMON =\
+ -std=c89\
+ -pedantic\
+ -fvisibility=hidden\
+ -fstrict-aliasing\
+ $(CFLAGS_HARDENED)\
+ $(WFLAGS)
+
+CFLAGS_DEBUG = -g $(CFLAGS_COMMON)
+CFLAGS_RELEASE = -O2 -DNDEBUG $(CFLAGS_COMMON)
+CFLAGS = $(CFLAGS_$(BUILD_TYPE))
+
+CFLAGS_SO = $(CFLAGS) -fPIC
+CFLAGS_EXE = $(CFLAGS) -fPIE
+
+################################################################################
+# Linker options
+################################################################################
+LDFLAGS_HARDENED = -Wl,-z,relro,-z,now
+LDFLAGS_DEBUG = $(LDFLAGS_HARDENED)
+LDFLAGS_RELEASE = -s $(LDFLAGS_HARDENED)
+LDFLAGS = $(LDFLAGS_$(BUILD_TYPE))
+
+LDFLAGS_SO = $(LDFLAGS) -shared -Wl,--no-undefined
+LDFLAGS_EXE = $(LDFLAGS) -pie
+
+OCPFLAGS_DEBUG = --localize-hidden
+OCPFLAGS_RELEASE = --localize-hidden --strip-unneeded
+OCPFLAGS = $(OCPFLAGS_$(BUILD_TYPE))
diff --git a/htgop.pc.in b/htgop.pc.in
@@ -0,0 +1,11 @@
+prefix=@PREFIX@
+includedir=${prefix}/include
+libdir=${prefix}/lib
+
+Requires: rsys >= @RSYS_VERSION@
+Name: htgop
+Description: High-Tune Gas Optical Properties
+Version: @VERSION@
+Libs: -L${libdir} -lhtgop
+Libs.private: -lm
+CFlags: -I${includedir}
diff --git a/make.sh b/make.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+# Copyright (C) 2018-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: %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 "\033[1;32mOK\033[m\n"
+ else
+ printf "\033[1;31mError\033[m\n"
+ fi 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
+}
+
+"$@"