From 818d66d4d38c1c84a6843f77dad7b86eac424f94 Mon Sep 17 00:00:00 2001 From: Evgeny Posenitskiy <45995097+q-posev@users.noreply.github.com> Date: Wed, 28 Apr 2021 15:30:18 +0200 Subject: [PATCH] Autoconfig with optional HDF5 compilation (#42) Makefile.in: * recursive build; * advanced make targets; * major support for autoconf and minor for package managers. configure.ac: * optional HDF5 compilation [default: --with-hdf5]; * add -fPIC to CFLAGS if missing. --- .gitignore | 13 ++ Makefile.in | 83 +++++++++++++ configure.ac | 152 ++++++++++++++++++++++++ src/Makefile | 95 --------------- src/Makefile.in | 133 +++++++++++++++++++++ src/templates_front/templator_front.org | 4 +- 6 files changed, 383 insertions(+), 97 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile.in create mode 100644 configure.ac delete mode 100644 src/Makefile create mode 100644 src/Makefile.in diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..baf4b19 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ + +Makefile +src/Makefile +auto-bugs +autogen.sh +autom4te.cache/ +clean.sh +config.* +configure +install-sh + +trexio-*.tar.gz +trexio-*/ diff --git a/Makefile.in b/Makefile.in new file mode 100644 index 0000000..9597a1d --- /dev/null +++ b/Makefile.in @@ -0,0 +1,83 @@ +# @configure_input@ + +# Package-specific substitution variables +package = @PACKAGE_NAME@ +version = @PACKAGE_VERSION@ +tarname = @PACKAGE_TARNAME@ +distdir = $(tarname)-$(version) + +# Prefix-specific substitution variables +prefix = @prefix@ +includedir = @includedir@ +docdir = @docdir@ +htmldir = @htmldir@ +libdir = @libdir@ +# Supplementary +exec_prefix = @exec_prefix@ +libexecdir = @libexecdir@ +lispdir = @lispdir@ +# VPATH-specific substitution variables +srcdir = @srcdir@ +VPATH = @srcdir@ +# Tool-specific substitution variables +CC = @CC@ +CFLAGS = @CFLAGS@ +CPPFLAGS = @CPPFLAGS@ +FC = @FC@ +FCFLAGS = @FCFLAGS@ +LIBS = @LIBS@ +LDFLAGS = @LDFLAGS@ +INSTALL = @INSTALL@ + +.PHONY: FORCE all install clean check dist distcheck +.POSIX: +.SUFFIXES: + +all install clean check: + cd src && $(MAKE) $@ + + +dist: $(distdir).tar.gz + + +$(distdir).tar.gz: $(distdir) + tar chof - $(distdir) | gzip -9 -c > $@ + rm -rf $(distdir) + +# for now copy entire src/ directory into $(distdir) in order for distcheck rule to work +# later on can be changed to ship only files like *.c *.h *.f90 *.so *.mod +$(distdir): FORCE + mkdir -p $(distdir) + cp $(srcdir)/configure.ac $(distdir) + cp $(srcdir)/configure $(distdir) + cp $(srcdir)/config.h.in $(distdir) + cp $(srcdir)/install-sh $(distdir) + cp -r $(srcdir)/src $(distdir) + cp $(srcdir)/Makefile.in $(distdir) + cp $(srcdir)/LICENSE $(distdir) + + +FORCE: + -rm $(distdir).tar.gz >/dev/null 2>&1 + -rm -rf $(distdir) >/dev/null 2>&1 + + +distcheck: $(distdir).tar.gz + gzip -cd $(distdir).tar.gz | tar xvf - + cd $(distdir) && \ + ./configure CC="$(CC)" CFLAGS="$(CFLAGS)" && \ + $(MAKE) all && \ + $(MAKE) check && \ + $(MAKE) DESTDIR=$${PWD}/_inst install && \ + $(MAKE) clean + rm -rf $(distdir) + @echo "*** Package $(distdir).tar.gz is ready for distribution." + + +Makefile: Makefile.in config.status + ./config.status $@ + + +config.status: configure + ./config.status --recheck + diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..481480a --- /dev/null +++ b/configure.ac @@ -0,0 +1,152 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_PREREQ([2.69]) +AC_INIT([TREXIO], [0.2], [https://github.com/TREX-CoE/trexio/issues]) +AC_CONFIG_SRCDIR([src/trexio_text.h]) +AC_CONFIG_HEADERS([config.h]) +PKG_PROG_PKG_CONFIG() +# Checks for programs. +AC_PROG_CC +AC_PROG_FC +AC_PROG_INSTALL +AC_PROG_LN_S +AC_PROG_CC_C99 + +# Configure with HDF5 (default: --with-hdf5 using pkg-config) [optional]: +# ./configure [--with-hdf5 or --with-hdf5=yes] +# Configure with user-provided path to HDF5: +# ./configure --with-hdf5=/usr/lib/x86_64-linux-gnu/hdf5/serial +# Configure without HDF5: +# ./configure --without-hdf5 [or --with-hdf5=no] + +# Checks for basic libraries. +AC_CHECK_LIB([m], [main]) +AC_CHECK_LIB([z], [main]) +# Checks for basic header files. +AC_CHECK_HEADERS([fcntl.h stdint.h stdlib.h string.h unistd.h]) + +# Search for pthread +have_pthreads=no +AC_SEARCH_LIBS([pthread_create], [pthread], [have_pthreads=yes]) + +if test "x${have_pthreads}" = xyes; then + AC_CHECK_HEADERS([pthread.h], [], [have_pthreads=no]) +fi + +if test "x${have_pthreads}" = xno; then +AC_MSG_ERROR([ + ------------------------------------------ + The pthread library and header file + required to build TREXIO. Stopping... + Check 'config.log' for more information. + ------------------------------------------]) +fi + +AC_ARG_WITH([hdf5], + AS_HELP_STRING([--with-hdf5], + [Include HDF5 functionality @<:@default: yes@:>@])], + [hdf5=${withval}], + [hdf5=yes]) + +have_hdf5=no +have_hdf5_hl=no +if test "x${hdf5}" = xno; then + AC_MSG_WARN([ + ------------------------------------------ + Configuring with the HDF5 library is + recommended to build efficient TREXIO. + ------------------------------------------]) +elif test "x${hdf5}" = xyes; then + + # Check if HDF5 is already configured (e.g. after calling `module load hdf5-`) + AC_CHECK_HEADERS([hdf5_hl.h]) + AC_CHECK_LIB([hdf5_hl], [H5LTfind_dataset], [], [have_hdf5_hl=no]) + + AC_CHECK_HEADERS([hdf5.h]) + AC_CHECK_LIB([hdf5], [H5open], [], [have_hdf5=no]) + + # If HDF5 is not configured, try using pkg-config + if test "x${have_hdf5}" = xno; then + PKG_CHECK_MODULES([HDF5], [hdf5 >= 1.8]) + CFLAGS="${HDF5_CFLAGS} ${CFLAGS}" + LIBS="${HDF5_LIBS} -lhdf5_hl ${LIBS}" + fi + +else + + if test "x${hdf5}" = x; then + AC_MSG_ERROR([ + ------------------------------------------ + The path to HDF5 library is required. + Maybe you forgot to pass --with-hdf5="/your/path/" to configure script. + Stopping... + ------------------------------------------]) + fi + + CPPFLAGS="-I${hdf5}/include ${CPPFLAGS}" + LDFLAGS="-L${hdf5} ${LDFLAGS}" + + AC_CHECK_HEADERS([hdf5_hl.h], [], [have_hdf5_hl=no]) + AC_CHECK_LIB([hdf5_hl], [H5LTfind_dataset], [], [have_hdf5_hl=no]) + + AC_CHECK_HEADERS([hdf5.h], [], [have_hdf5=no]) + AC_CHECK_LIB([hdf5], [H5open], [], [have_hdf5=no]) + +fi + +# add -fPIC if not present in CFLAGS, necessary to compile TREXIO +# TODO: replace with AM_CFLAGS for appropriate targer once Automake is introduced +case "${CFLAGS}" in + *-fPIC*) + ;; + *) + AC_MSG_WARN([ + ------------------------------------------ + Adding -fPIC option to CFLAGS. + This is necessary to build TREXIO. + ------------------------------------------]) + CFLAGS="${CFLAGS} -fPIC" + ;; +esac + +# Checks for typedefs, structures, and compiler characteristics. +AC_TYPE_INT32_T +AC_TYPE_INT64_T +AC_TYPE_SIZE_T +AC_TYPE_UINT32_T +AC_TYPE_UINT64_T + +# Checks for library functions. +AC_FUNC_MALLOC +AC_CHECK_FUNCS([memset mkdir strerror]) + +AC_CONFIG_FILES([Makefile + src/Makefile]) +AC_OUTPUT + +echo \ +"------------------------------------------------- + +${PACKAGE_NAME} Version ${PACKAGE_VERSION} + +Prefix: '${prefix}'. + +CC: ${CC} +CPPFLAGS: ${CPPFLAGS} +CFLAGS: ${CFLAGS} +FC: ${FC} +FCLAGS: ${FCFLAGS} +LDFLAGS: ${LDFLAGS} +LIBS: ${LIBS} + +Package features: + Compilation with HDF5: ${hdf5} + +Now type 'make @<:@@:>@' +where the optional is: + all - build C and Fortran APIs + check - check C and Fortran APIs + clean - clean the produced files +--------------------------------------------------" + diff --git a/src/Makefile b/src/Makefile deleted file mode 100644 index f2c90ba..0000000 --- a/src/Makefile +++ /dev/null @@ -1,95 +0,0 @@ -COMPILER=GNU -#COMPILER=INTEL -#COMPILER=LLVM - -ifeq ($(COMPILER),GNU) -CC=gcc -g -CFLAGS=-fPIC -fexceptions -Wall -Werror -Wpedantic -Wextra - -FC=gfortran -g -FFLAGS=-fPIC -fcheck=all -Waliasing -Wampersand -Wconversion -Wsurprising -Wintrinsics-std -Wno-tabs -Wintrinsic-shadow -Wline-truncation -Wreal-q-constant -Wuninitialized -fbacktrace -ffpe-trap=zero,overflow,underflow -finit-real=nan - -LIBS= $(shell pkg-config --libs hdf5) -lhdf5_hl -lgfortran -INCLUDE = $(shell pkg-config --cflags hdf5) -endif - -ifeq ($(COMPILER),INTEL) -CC=icc -xHost -CFLAGS=-fPIC -g -O2 - -FC=ifort -xHost -FFLAGS=-fPIC -g -O2 - -LIBS= #-lm -endif - -#TODO -ifeq ($(COMPILER),LLVM) -CC=clang -CFLAGS=-fPIC -g -O2 - -FC=flang -FFLAGS=fPIC -g -O2 - -LIBS=-lm -endif - -OBJECT_FILES= trexio.o trexio_text.o trexio_hdf5.o -SOURCE_FILES= test.c trexio.c trexio_hdf5.c trexio_text.c -HEADER_FILES= trexio.h trexio_text.h trexio_hdf5.h trexio_s.h -ORG_FILES= templates_front/templator_front.org templates_text/templator_text.org \ - templates_hdf5/templator_hdf5.org - - -export CC CFLAGS FC FFLAGS LIBS - -.PHONY: clean - - -libtrexio.so: $(OBJECT_FILES) $(HEADER_FILES) - $(CC) -shared $(OBJECT_FILES) -o libtrexio.so - - -fortran: trexio_f.o - - -trexio_f.o: libtrexio.so trexio_f.f90 - $(FC) $(FFLAGS) -c trexio_f.f90 -o trexio_f.o - - -check: cppcheck.out - cat cppcheck.out - -cppcheck.out: $(HEADER_FILES) $(SOURCE_FILES) - cppcheck --addon=cert -q --error-exitcode=0 \ - --enable=warning,performance,portability,missingInclude,information \ - --language=c --std=c99 -rp --platform=unix64 \ - $(INCLUDE) $(SOURCE_FILES) 2>$@ - - -trexio.c trexio_f.f90 trexio.h: $(ORG_FILES) - ./build_trexio.sh - - -test_c: libtrexio.so test.c - $(CC) $(CFLAGS) $(INCLUDE) -Wl,-rpath,$(PWD) -L. test.c -ltrexio $(LIBS) -o test_c - - -test_f: libtrexio.so test.f90 trexio_f.o - $(FC) $(FFLAGS) $(INCLUDE) -Wl,-rpath,$(PWD) -L. test.f90 trexio_f.o -ltrexio $(LIBS) -o test_f - - -test: test_c test_f - $(RM) -r trexio_test - $(RM) -r trexio_test_fort - ./test_c - ./test_f - -clean: - $(RM) *.o libtrexio.so test_*.h5 test_f test_c - $(RM) -r trexio_test/ trexio_test_fort/ - - -%.o: %.c $(HEADER_FILES) - $(CC) $(CFLAGS) $(INCLUDE) -c $*.c -o $*.o - diff --git a/src/Makefile.in b/src/Makefile.in new file mode 100644 index 0000000..a54c4be --- /dev/null +++ b/src/Makefile.in @@ -0,0 +1,133 @@ +# @configure_input@ + +# Package-specific substitution variables +package = @PACKAGE_NAME@ +version = @PACKAGE_VERSION@ +tarname = @PACKAGE_TARNAME@ +distdir = $(tarname)-$(version) + +# Prefix-specific substitution variables +prefix = @prefix@ +includedir = @includedir@ +docdir = @docdir@ +htmldir = @htmldir@ +libdir = @libdir@ +# Supplementary +exec_prefix = @exec_prefix@ +libexecdir = @libexecdir@ +lispdir = @lispdir@ +# VPATH-specific substitution variables +srcdir = @srcdir@ +VPATH = @srcdir@ + +# Tool-specific substitution variables +CC = @CC@ +CFLAGS = @CFLAGS@ +CPPFLAGS = @CPPFLAGS@ +FC = @FC@ +FCFLAGS = @FCFLAGS@ +LIBS = @LIBS@ +LDFLAGS = @LDFLAGS@ +INSTALL = @INSTALL@ + + +ifeq ($(CC),gcc) +#CFLAGS = -g -fPIC -O2 -fexceptions -Wall -Werror -Wpedantic -Wextra +endif +ifeq ($(FC),gfortran) +#FCFLAGS = -g -fPIC -O2 -fcheck=all -Waliasing -Wampersand -Wconversion -Wsurprising -Wintrinsics-std -Wno-tabs -Wintrinsic-shadow -Wline-truncation -Wreal-q-constant -Wuninitialized -fbacktrace -ffpe-trap=zero,overflow,underflow -finit-real=nan +endif + +ifeq ($(CC),icc) +#CFLAGS = -xHost -fPIC -g -O2 -fexceptions -Wall -Werror -Wpedantic -Wextra +endif +ifeq ($(FC),ifort) +#FCFLAGS = -xHost -fPIC -g -O2 +endif + +ifeq ($(CC),clang) +#CFLAGS = -fPIC -g -O2 +endif +ifeq ($(FC),flang) +#FCFLAGS = -fPIC -g -O2 +endif + +OBJECT_FILES= trexio.o trexio_text.o trexio_hdf5.o +SOURCE_FILES= test.c trexio.c trexio_hdf5.c trexio_text.c +HEADER_FILES= trexio.h trexio_text.h trexio_hdf5.h trexio_s.h +ORG_FILES= templates_front/templator_front.org templates_text/templator_text.org \ + templates_hdf5/templator_hdf5.org + + +.PHONY: all fortran clean install +.POSIX: +.SUFFIXES: + + +all: libtrexio.so fortran + + +libtrexio.so: $(OBJECT_FILES) $(HEADER_FILES) + $(CC) -shared $(OBJECT_FILES) -o libtrexio.so + + +fortran: trexio_f.o + + +trexio_f.o: libtrexio.so trexio_f.f90 + $(FC) $(FCFLAGS) -c trexio_f.f90 -o trexio_f.o + + +cppcheck: cppcheck.out + cat cppcheck.out + + +cppcheck.out: $(HEADER_FILES) $(SOURCE_FILES) + cppcheck --addon=cert -q --error-exitcode=0 \ + --enable=warning,performance,portability,missingInclude,information \ + --language=c --std=c99 -rp --platform=unix64 \ + $(CPPFLAGS) $(SOURCE_FILES) 2>$@ + + +trexio.c trexio_f.f90 trexio.h: $(ORG_FILES) + ./build_trexio.sh + + +test_c: libtrexio.so test.c + $(CC) $(CPPFLAGS) $(CFLAGS) -Wl,-rpath,$(PWD) -L. test.c -ltrexio $(LDFLAGS) $(LIBS) -o test_c + + +test_f: libtrexio.so trexio_f.o test.f90 + $(FC) $(CPPFLAGS) $(FCFLAGS) -Wl,-rpath,$(PWD) -L. test.f90 trexio_f.o -ltrexio $(LDFLAGS) $(LIBS) -o test_f + + +check: test_c test_f + $(RM) -r trexio_test + $(RM) -r trexio_test_fort + ./test_c + ./test_f + +# $(DESTDIR) is needed for future support of package managers +# install should avoid build target due to possible conflict when prefix overriden +install: + $(INSTALL) -d $(DESTDIR)$(libdir) $(DESTDIR)$(includedir) + cp libtrexio.so $(DESTDIR)$(libdir) + cp trexio*.h $(DESTDIR)$(includedir) + + +clean: + $(RM) *.o libtrexio.so test_*.h5 test_f test_c + $(RM) -r trexio_test/ trexio_test_fort/ + + +%.o: %.c $(HEADER_FILES) + $(CC) $(CPPFLAGS) $(CFLAGS) -c $*.c -o $*.o + + +Makefile: Makefile.in ../config.status + cd .. && ./config.status src/$@ + + +../config.status: ../configure + cd .. && ./config.status --recheck + diff --git a/src/templates_front/templator_front.org b/src/templates_front/templator_front.org index 6ffce9a..1b0d911 100644 --- a/src/templates_front/templator_front.org +++ b/src/templates_front/templator_front.org @@ -402,7 +402,7 @@ struct trexio_back_end_s { These names are populated by the generator.py (i.e. they are hard-coded), which is why the user should tend to avoid renaming the ~.txt~ data files. - #+begin_src c :tangle prefix_front.h + #+begin_src c :tangle prefix_front.h :exports none trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t back_end); #+end_src @@ -530,7 +530,7 @@ end interface output: ~trexio_exit_code~ exit code. - #+begin_src c :tangle prefix_front.h + #+begin_src c :tangle prefix_front.h :exports none trexio_exit_code trexio_close(trexio_t* file); #+end_src