star-sp

Random number generators and distributions
git clone git://git.meso-star.fr/star-sp.git
Log | Files | Refs | README | LICENSE

commit 5d75d31083c86bb7ab5f7cd624b076337baece55
parent c2c86d47fedbdca6a7fd95b99db6e137e578ceb2
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Sun, 14 May 2023 20:32:03 +0200

Extensive rework of the POSIX Makefile

Split the linker flags in 2 different macros:
- the SOFLAGS macro defines the flags when creating a shared object,
  i.e. a dynamic library,
- and the LDFLAGS macro defines the linker options for all linking
  operations, i.e. when creating a shared object or an executable.

Add the BUILD_TYPE macro which controls if the compilation is done in
RELEASE or in DEBUG: the CFLAGS, CXXFLAGS and LDFLAGS macros are
defined according to BUILD_TYPE.

Add building the library as a static library. The new LIB_TYPE macro
controls whether the generated library is a shared object or an archive,
depending on whether its value is SHARED or STATIC respectively. Note
that the new macro PCFLAGS, whose value depends on LIB_TYPE, controls
whether pkg-config fetches dependencies for static linking or not.

Do not hide the compiler commands anymore (except for the file
dependency check). There is no particular advantage in doing this and it
is sane to see the compiler options used. In the same spirit, the
commands executed by the clean, distclean and lint targets are also
displayed to show what they do.

Replace the BUILD_R123_AES macro with the AES_CFLAGS macro, which lists
the compilation options when building the library with support for the
AES random number generator. If the macro is not defined or if it is
empty, its support is disabled.

Diffstat:
M.gitignore | 2+-
MMakefile | 79++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------
Mconfig.mk | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------
Mmake.sh | 23+++++++++++++++++++++--
4 files changed, 127 insertions(+), 58 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -10,4 +10,4 @@ test* .test tags star-sp.pc - +star-sp-local.pc diff --git a/Makefile b/Makefile @@ -34,6 +34,10 @@ include config.mk +LIBNAME_STATIC = libstar-sp.a +LIBNAME_SHARED = libstar-sp.so +LIBNAME = $(LIBNAME_$(LIB_TYPE)) + ################################################################################ # Star-SP building ################################################################################ @@ -49,15 +53,23 @@ OBJ = $(SRC:.c=.o) DEP = $(SRC:.c=.d) build_library: .config $(DEP) - @$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f $${i}; done) libstar-sp.so + @$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f $${i}; done)\ + $$(if [ -n "$(LIBNAME)" ]; then\ + echo "$(LIBNAME)";\ + else\ + echo "$(LIBNAME_SHARED)";\ + fi) $(OBJ): config.mk -libstar-sp.so: $(OBJ) - @echo "LD $@" - @$(CXX) $(CXXFLAGS) $(LDFLAGS) $(LIBS) -o $@ $(OBJ) +$(LIBNAME_SHARED): $(OBJ) + $(CXX) $(CXXFLAGS) -o $@ $(OBJ) $(LDFLAGS) $(SOFLAGS) $(DPDC_LIBS) + +$(LIBNAME_STATIC): $(OBJ) + $(AR) -rc $@ $? + $(RANLIB) $@ -.config: Makefile +.config: Makefile config.mk @echo "Find packages" @if ! $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys; then \ echo "rsys $(RSYS_VERSION) not found" >&2; exit 1; fi @@ -67,11 +79,10 @@ libstar-sp.so: $(OBJ) .SUFFIXES: .c .d .o .c.d: - @$(CXX) $(CXXFLAGS) $(INCS) -MM -MT "$(@:.d=.o) $@" $< -MF $@ + @$(CXX) $(CXXFLAGS) $(DPDC_CFLAGS) $(AES_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@ .c.o: - @echo "CXX $@" - @$(CXX) $(CXXFLAGS) $(INCS) -DSSP_SHARED_BUILD -c $< -o $@ + $(CXX) $(CXXFLAGS) $(DPDC_CFLAGS) $(AES_CFLAGS) -DSSP_SHARED_BUILD -c $< -o $@ ################################################################################ # Installation @@ -84,15 +95,22 @@ pkg: -e 's#@RANDOM123_VERSION@#$(RANDOM123_VERSION)#g' \ star-sp.pc.in > star-sp.pc +star-sp-local.pc: star-sp.pc.in + @sed -e '1d'\ + -e 's#^includedir=.*#includedir=./src/#'\ + -e 's#^libdir=.*#libdir=./#'\ + -e 's#@PREFIX@#$(PREFIX)#g'\ + -e 's#@VERSION@#$(VERSION)#g'\ + -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g'\ + -e 's#@RANDOM123_VERSION@#$(RANDOM123_VERSION)#g'\ + star-sp.pc.in > $@ + install: build_library pkg - mkdir -p $(DESTDIR)$(PREFIX)/lib - mkdir -p $(DESTDIR)$(PREFIX)/lib/pkgconfig - mkdir -p $(DESTDIR)$(PREFIX)/include/star - mkdir -p $(DESTDIR)$(PREFIX)/share/doc/star-sp - cp libstar-sp.so $(DESTDIR)$(PREFIX)/lib - cp star-sp.pc $(DESTDIR)$(PREFIX)/lib/pkgconfig - cp src/ssp.h $(DESTDIR)$(PREFIX)/include/star - cp COPYING.en COPYING.fr README.md $(DESTDIR)$(PREFIX)/share/doc/star-sp + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib" $(LIBNAME) + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib/pkgconfig" star-sp.pc + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/include/star" src/ssp.h + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/doc/star-sp"\ + COPYING.en COPYING.fr README.md uninstall: rm -f $(DESTDIR)$(PREFIX)/lib/libstar-sp.so @@ -108,10 +126,13 @@ uninstall: all: build_library build_tests clean: clean_test - @rm -f $(OBJ) $(TEST_OBJ) libstar-sp.so .test star-sp.pc .config + rm -f $(OBJ) $(TEST_OBJ) $(LIBNAME) .config .test star-sp.pc star-sp-local.pc distclean: clean - @rm -f $(DEP) $(TEST_DEP) + rm -f $(DEP) $(TEST_DEP) + +lint: + shellcheck -o all make.sh ################################################################################ # Tests @@ -131,23 +152,27 @@ TEST_SRC =\ src/test_ssp_ran_uniform_disk.c\ src/test_ssp_rng.c\ src/test_ssp_rng_proxy.c - TEST_OBJ = $(TEST_SRC:.c=.o) TEST_DEP = $(TEST_SRC:.c=.d) -test: build_tests - @$(SHELL) make.sh run_test "$(BUILD_R123_AES)" $(TEST_SRC) +SSP_CFLAGS = $$($(PKG_CONFIG) --with-path=./ $(PCFLAGS) --cflags star-sp-local.pc) +SSP_LIBS = $$($(PKG_CONFIG) --with-path=./ $(PCFLAGS) --libs star-sp-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 +test: build_tests + @$(SHELL) make.sh run_test "$(AES_CFLAGS)" $(TEST_SRC) + .test: Makefile - @echo "Setup tests" @$(SHELL) make.sh config_test $(TEST_SRC) > .test clean_test: @$(SHELL) make.sh clean_test $(TEST_SRC) +$(TEST_OBJ): config.mk star-sp-local.pc + $(CC) $(CFLAGS) $(S3D_CFLAGS) $(RSYS_CFLAGS) -c $(@:.o=.c) -o $@ + test_ssp_ran_circle \ test_ssp_ran_discrete \ test_ssp_ran_gaussian \ @@ -161,10 +186,6 @@ test_ssp_ran_tetrahedron \ test_ssp_ran_triangle \ test_ssp_ran_uniform_disk \ test_ssp_rng \ -test_ssp_rng_proxy : libstar-sp.so - @echo "LD $@" - @$(CC) $(CFLAGS) -o $@ src/$@.o -L$$(pwd) -lstar-sp $(RSYS_LIB) - -$(TEST_OBJ): config.mk - @echo "CC $@" - @$(CC) $(CFLAGS) $(RSYS_INC) -c $(@:.o=.c) -o $@ +test_ssp_rng_proxy \ +: star-sp-local.pc + $(CC) -o $@ src/$@.o $(LDFLAGS) $(SSP_LIBS) $(RSYS_LIBS) -lm diff --git a/config.mk b/config.mk @@ -1,48 +1,77 @@ -VERSION = 0.13.0 # Library version - +VERSION = 0.13.0 PREFIX = /usr/local -PKG_CONFIG = pkg-config -BUILD_R123_AES = 1 +LIB_TYPE = SHARED +#LIB_TYPE = STATIC + +BUILD_TYPE = RELEASE +#BUILD_TYPE = DEBUG + +################################################################################ +# Tools +################################################################################ +AR = ar +CC = cc +CXX = c++ +PKG_CONFIG = pkg-config +RANLIB = ranlib ################################################################################ # Dependencies ################################################################################ +PCFLAGS_STATIC = --static +PCFLAGS = $(PCFLAGS_$(LIB_TYPE)) + RSYS_VERSION = 0.6 -RSYS_INC = $$($(PKG_CONFIG) --cflags rsys) -RSYS_LIB = $$($(PKG_CONFIG) --libs rsys) +RSYS_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsys) +RSYS_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs rsys) RANDOM123_VERSION = 1.09 -RANDOM123_INC = $$($(PKG_CONFIG) --cflags random123) -RANDOM123_LIB = $$($(PKG_CONFIG) --libs random123) +RANDOM123_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags random123) +RANDOM123_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs random123) -INCS=$(RSYS_INC) $(RANDOM123_INC) -LIBS=$(RSYS_LIB) $(RANDOM123_LIB) +DPDC_CFLAGS = $(RSYS_CFLAGS) $(RANDOM123_CFLAGS) +DPDC_LIBS = $(RSYS_LIBS) $(RANDOM123_LIBS) ################################################################################ # Compilation options ################################################################################ +# Comment to disable the Random123 AES RNG +AES_CFLAGS = -DWITH_R123_AES -maes -# Compilers -CC = cc -CXX = c++ - -CPPFLAGS = -DNDEBUG $$([ ! "$(BUILD_R123_AES)" = 0 ] && echo -DWITH_R123_AES) - -WFLAGS =\ +FLAGS =\ + -pedantic\ + -fPIC\ + -fvisibility=hidden\ + -fstrict-aliasing\ -Wall\ -Wconversion\ -Wextra\ -Wmissing-declarations\ - -Wshadow + -Wshadow\ + +CFLAGS_COMMON =\ + -std=c89\ + -Wmissing-prototypes\ + $(FLAGS) -# Compiler options -FLAGS = -O3 -pedantic -fPIC -fvisibility=hidden -fstrict-aliasing \ - -Wl,--no-undefined $(WFLAGS) $(CPPFLAGS) -CFLAGS = $(FLAGS) -std=c89 -Wmissing-prototypes +CXXFLAGS_COMMON =\ + -std=c++11\ + $(FLAGS) -# Disable warnings emitted by Random123 -CXXFLAGS =$(FLAGS) -std=c++11 -Wno-deprecated-copy -Wno-expansion-to-defined\ - $$([ ! "$(BUILD_R123_AES)" = 0 ] && echo -maes) +CFLAGS_RELEASE = -O2 -DNDEBUG $(CFLAGS_COMMON) +CFLAGS_DEBUG = -g $(CFLAGS_COMMON) +CFLAGS = $(CFLAGS_$(BUILD_TYPE)) + +CXXFLAGS_RELEASE = -O2 -DNDEBUG $(CXXFLAGS_COMMON) +CXXFLAGS_DEBUG = -g $(CXXFLAGS_COMMON) +CXXFLAGS = $(CXXFLAGS_$(BUILD_TYPE)) + +################################################################################ +# Linker options +################################################################################ +SOFLAGS = -shared -Wl,--no-undefined -LDFLAGS = -shared # Linker options +LDFLAGS_DEBUG = +LDFLAGS_RELEASE = -s +LDFLAGS = $(LDFLAGS_$(BUILD_TYPE)) diff --git a/make.sh b/make.sh @@ -57,7 +57,7 @@ check() run_test() { - build_r123_aes=$1 + aes_flags=$1 shift 1 for i in "$@"; do @@ -65,7 +65,7 @@ run_test() if ! [ "${test}" = "test_ssp_rng" ]; then check "${test}" "${test}" else - if [ ! "${build_r123_aes}" = 0 ]; then + if [ -n "${aes_flags}" ]; then check "${test}_aes" "${test}" aes fi check "${test}_kiss" "${test}" kiss @@ -84,4 +84,23 @@ clean_test() 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 +} + "$@"