From 320697ee8d5d5353414476057cef7f6ae07564c0 Mon Sep 17 00:00:00 2001 From: q-posev Date: Tue, 9 Nov 2021 16:52:43 +0100 Subject: [PATCH 01/17] [WIP] FindTREXIO.cmake file --- tools/FindTREXIO.cmake | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 tools/FindTREXIO.cmake diff --git a/tools/FindTREXIO.cmake b/tools/FindTREXIO.cmake new file mode 100644 index 0000000..87fc57a --- /dev/null +++ b/tools/FindTREXIO.cmake @@ -0,0 +1,45 @@ + + +#=========================================== + +# This file should be located WITHIN your project source tree. +# (e.g. in cmake/FindTREXIO.cmake file) + +# usage in CMakeLists.txt +#list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") +#find_package(TREXIO) +#include_directories("${TREXIO_INCLUDE_DIR}") +#target_link_libraries(your_target ${TREXIO_LIBRARY}) + +#=========================================== + +# If already in cache, be silent +if (TREXIO_INCLUDE_DIR AND TREXIO_LIBRARY) + set(TREXIO_FIND_QUIETLY TRUE) +endif() + +set(TREXIO_FOUND OFF) +set(TREXIO_LIBRARY) + +set(TREXIO_NAMES "libtrexio.so") +#set(TREXIO_NAMES "libtrexio.so" "libtrexio.dylib" "libtrexio.la") + +find_path(TREXIO_INCLUDE_DIR + NAMES trexio.h + HINTS /usr/include + /usr/local/include + $ENV{HOME}/include) + +find_library(TREXIO_LIBRARY + NAMES ${TREXIO_NAMES} + PATHS /usr/lib + /usr/local/lib + $ENV{HOME}/lib + NO_DEFAULT_PATH) + + +# Handle the QUIETLY and REQUIRED arguments and set TREXIO_FOUND to TRUE if +# all listed variables are TRUE. +INCLUDE(FindPackageHandleStandardArgs) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(TREXIO DEFAULT_MSG TREXIO_LIBRARY TREXIO_INCLUDE_DIR ) +MARK_AS_ADVANCED(TREXIO_INCLUDE_DIR TREXIO_LIBRARY) From 7fa07bf7e7719437c20cb77de59640d3b3aa0cec Mon Sep 17 00:00:00 2001 From: q-posev Date: Fri, 12 Nov 2021 17:00:04 +0100 Subject: [PATCH 02/17] [WIP] working Guix project config file TODO: implement possibility to build from the GitHub repo OR without hdf5 --- tools/trexio.scm | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tools/trexio.scm diff --git a/tools/trexio.scm b/tools/trexio.scm new file mode 100644 index 0000000..5f8deb9 --- /dev/null +++ b/tools/trexio.scm @@ -0,0 +1,34 @@ +(define-module (gnu packages trexio) + #:use-module (guix packages) + #:use-module (gnu packages pkg-config) + #:use-module (gnu packages glib) + #:use-module (gnu packages gcc) + #:use-module (gnu packages autotools) + #:use-module (gnu packages maths) + #:use-module (guix download) + #:use-module (guix build-system gnu) + #:use-module (guix licenses)) + +(define-public trexio + (package + (name "trexio") + (version "2.0") + (source (origin + (method url-fetch) + (uri (string-append "https://github.com/TREX-CoE/trexio/releases/download/v" version + "/trexio-" version ".0" + ".tar.gz")) + (sha256 + (base32 + "1d2cn4w2r9gfid5b9wrq9q290kqdnbjdmvli76s1i5r58kdg5vkf" + )))) + (build-system gnu-build-system) + (arguments '(#:configure-flags '("--enable-silent-rules"))) + (inputs `(("hdf5" ,hdf5-1.12) ("gfortran", gfortran))) + (synopsis "TREX I/O lbrary: trexio package") + (description "APIs in C and Fortran to exchange wavefunction data. + Supports HDF5 and TEXT back ends.") + (home-page "https://trex-coe.github.io/trexio") + (license bsd-3))) + +trexio From 297552418b73580bd5700331169246b386ff002b Mon Sep 17 00:00:00 2001 From: q-posev Date: Sat, 13 Nov 2021 18:04:52 +0100 Subject: [PATCH 03/17] add AC_CONFIG_AUX_DIR --- configure.ac | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/configure.ac b/configure.ac index 9bbc467..8824597 100644 --- a/configure.ac +++ b/configure.ac @@ -3,6 +3,12 @@ AC_PREREQ([2.69]) AC_INIT([trexio], [2.0.0], [https://github.com/TREX-CoE/trexio/issues]) + +AC_CONFIG_SRCDIR([Makefile.in]) +AC_CONFIG_HEADERS([include/config.h]) +AC_CONFIG_MACRO_DIR([m4]) +AC_CONFIG_AUX_DIR([build-config]) + AM_INIT_AUTOMAKE([subdir-objects color-tests parallel-tests silent-rules 1.11]) AM_MAINTAINER_MODE() @@ -13,12 +19,8 @@ AC_CHECK_FILE([$TEST_IFEXISTS], [enable_maintainer_mode="yes"], ) -AM_MAINTAINER_MODE() LT_PREREQ([2.2]) LT_INIT -AC_CONFIG_SRCDIR([Makefile.in]) -AC_CONFIG_HEADERS([include/config.h]) -AC_CONFIG_MACRO_DIR([m4]) VERSION_MAJOR=`echo ${PACKAGE_VERSION} | cut -d. -f1` VERSION_MINOR=`echo ${PACKAGE_VERSION} | cut -d. -f2` From 5b93f55c656e57e6e6a27152b41a5a69bcfa2315 Mon Sep 17 00:00:00 2001 From: q-posev Date: Sat, 13 Nov 2021 19:01:21 +0100 Subject: [PATCH 04/17] [WIP] first working CMakeLists.txt --- CMakeLists.txt | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..c4f589c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,58 @@ +cmake_minimum_required(VERSION 3.16) + +project(Trexio + VERSION 2.0.0 + DESCRIPTION "TREX I/O library" + LANGUAGES C Fortran + ) + +set(CMAKE_C_STANDARD 99) +set(CMAKE_C_STANDARD_REQUIRED ON) + +option(ENABLE_HDF5 "Enable HDF5 support" ON) + +if(ENABLE_HDF5) + find_package(HDF5 REQUIRED) + if(HDF5_FOUND) + message(STATUS "HDF5 version :: ${HDF5_VERSION}") + message(STATUS "HDF5 include dir :: ${HDF5_INCLUDE_DIRS}") + message(STATUS "HDF5 libraries :: ${HDF5_C_LIBRARIES}") + else() + # Download and install HDF5 library using FetchContent + # ... + endif() +endif() + +#IF DO_SHARED +add_library(trexio SHARED + src/trexio.c + src/trexio_hdf5.c + src/trexio_text.c + ) +#ELIF DO_STATIC + +#ENDIF + +set_target_properties(trexio PROPERTIES + VERSION ${PROJECT_VERSION} + PUBLIC_HEADER include/trexio.h + ) + +target_include_directories(trexio PRIVATE + include + src + ) + +if(ENABLE_HDF5) + target_include_directories(trexio PRIVATE ${HDF5_C_INCLUDE_DIRS}) + target_link_libraries(trexio PRIVATE ${HDF5_C_LIBRARIES}) +endif() + + +include(GNUInstallDirs) +install(TARGETS trexio + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + ) + + From f997265be3b30c52436271215056a8a5d67d8aee Mon Sep 17 00:00:00 2001 From: q-posev Date: Sat, 13 Nov 2021 19:49:02 +0100 Subject: [PATCH 05/17] [WIP] add working test --- CMakeLists.txt | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c4f589c..e35fe4c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.16) +# =========== SETUP THE PROJECT ============= + project(Trexio VERSION 2.0.0 DESCRIPTION "TREX I/O library" @@ -9,10 +11,12 @@ project(Trexio set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED ON) +# ============= CONFIGURE HDF5 ============== + option(ENABLE_HDF5 "Enable HDF5 support" ON) if(ENABLE_HDF5) - find_package(HDF5 REQUIRED) + find_package(HDF5 REQUIRED COMPONENTS C HL) if(HDF5_FOUND) message(STATUS "HDF5 version :: ${HDF5_VERSION}") message(STATUS "HDF5 include dir :: ${HDF5_INCLUDE_DIRS}") @@ -23,6 +27,8 @@ if(ENABLE_HDF5) endif() endif() +# ========= DEFINE TREXIO C LIBRARY ========= + #IF DO_SHARED add_library(trexio SHARED src/trexio.c @@ -38,16 +44,27 @@ set_target_properties(trexio PROPERTIES PUBLIC_HEADER include/trexio.h ) -target_include_directories(trexio PRIVATE - include - src - ) +target_include_directories(trexio PRIVATE src) + +include_directories(include) if(ENABLE_HDF5) - target_include_directories(trexio PRIVATE ${HDF5_C_INCLUDE_DIRS}) - target_link_libraries(trexio PRIVATE ${HDF5_C_LIBRARIES}) + target_include_directories(trexio PRIVATE ${HDF5_C_INCLUDE_DIRS}) + target_link_libraries(trexio PRIVATE ${HDF5_C_HL_LIBRARIES} ${HDF5_C_LIBRARIES}) endif() +# ================= TESTING ================= + +enable_testing() + +add_executable(io_all tests/io_all.c) +target_link_libraries(io_all PRIVATE trexio) +add_test( + NAME io_all + COMMAND $ +) + +# ============= INSTALL TREXIO ============== include(GNUInstallDirs) install(TARGETS trexio @@ -55,4 +72,5 @@ install(TARGETS trexio PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) +# =========================================== From 11142e07238ca93d9730b40b0d147c27eddf0795 Mon Sep 17 00:00:00 2001 From: q-posev Date: Mon, 15 Nov 2021 10:29:50 +0100 Subject: [PATCH 06/17] working CTest and optional HDF5 configuration --- CMakeLists.txt | 83 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 57 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e35fe4c..706db44 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,28 +11,11 @@ project(Trexio set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED ON) -# ============= CONFIGURE HDF5 ============== - -option(ENABLE_HDF5 "Enable HDF5 support" ON) - -if(ENABLE_HDF5) - find_package(HDF5 REQUIRED COMPONENTS C HL) - if(HDF5_FOUND) - message(STATUS "HDF5 version :: ${HDF5_VERSION}") - message(STATUS "HDF5 include dir :: ${HDF5_INCLUDE_DIRS}") - message(STATUS "HDF5 libraries :: ${HDF5_C_LIBRARIES}") - else() - # Download and install HDF5 library using FetchContent - # ... - endif() -endif() - # ========= DEFINE TREXIO C LIBRARY ========= #IF DO_SHARED add_library(trexio SHARED src/trexio.c - src/trexio_hdf5.c src/trexio_text.c ) #ELIF DO_STATIC @@ -45,24 +28,72 @@ set_target_properties(trexio PROPERTIES ) target_include_directories(trexio PRIVATE src) - include_directories(include) +# ============= CONFIGURE HDF5 ============== + +option(ENABLE_HDF5 "Enable HDF5 support" ON) + if(ENABLE_HDF5) - target_include_directories(trexio PRIVATE ${HDF5_C_INCLUDE_DIRS}) - target_link_libraries(trexio PRIVATE ${HDF5_C_HL_LIBRARIES} ${HDF5_C_LIBRARIES}) + find_package(HDF5 REQUIRED COMPONENTS C HL) + + if(HDF5_FOUND) + message(STATUS "HDF5 version :: ${HDF5_VERSION}") + message(STATUS "HDF5 include dir :: ${HDF5_INCLUDE_DIRS}") + message(STATUS "HDF5 libraries :: ${HDF5_C_LIBRARIES}") + else() + # Download and install HDF5 library using FetchContent + # ... + # For now - raise a FatalError + message(FATAL_ERROR "HDF5 is enabled not found.") + endif() + + target_sources(trexio PRIVATE src/trexio_hdf5.c) + target_compile_definitions(trexio PUBLIC HAVE_HDF5=1) + target_include_directories(trexio PRIVATE ${HDF5_C_INCLUDE_DIRS}) + target_link_libraries(trexio PRIVATE + ${HDF5_C_HL_LIBRARIES} + ${HDF5_C_LIBRARIES}) endif() # ================= TESTING ================= enable_testing() -add_executable(io_all tests/io_all.c) -target_link_libraries(io_all PRIVATE trexio) -add_test( - NAME io_all - COMMAND $ -) +# create the lists of tests +set(Tests_text + open_text + io_dset_float_text + io_dset_str_text + io_safe_dset_float_text + io_dset_int_text + io_num_text + io_str_text + overwrite_all_text + ) + +if(ENABLE_HDF5) + set(Tests_hdf5 + open_hdf5 + io_dset_float_hdf5 + io_dset_str_hdf5 + io_safe_dset_float_hdf5 + io_dset_int_hdf5 + io_num_hdf5 + io_str_hdf5 + overwrite_all_hdf5 + ) + set(Tests io_all ${Tests_text} ${Tests_hdf5}) +else() + set(Tests ${Tests_text}) +endif() + +# Add all the ADD_TEST for each test +foreach(test ${Tests}) + add_executable(${test} tests/${test}.c) + target_link_libraries(${test} PRIVATE trexio) + add_test(NAME ${test} COMMAND $) +endforeach () # ============= INSTALL TREXIO ============== From 0659dc1ca0dc2e8479f88a8b1ad8cac790dd1b71 Mon Sep 17 00:00:00 2001 From: q-posev Date: Mon, 15 Nov 2021 11:16:28 +0100 Subject: [PATCH 07/17] added BUILD__LIBS options and comments --- CMakeLists.txt | 54 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 706db44..03414be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,39 +2,67 @@ cmake_minimum_required(VERSION 3.16) # =========== SETUP THE PROJECT ============= +# Initialize the CMake project. project(Trexio VERSION 2.0.0 DESCRIPTION "TREX I/O library" LANGUAGES C Fortran ) +# Request the C99 standard. set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED ON) +# By default, the shared TREXIO library is built (even without -DBUILD_SHARED_LIBS=ON). +# To change this behaviour, append -DBUILD_STATIC_LIBS=ON to the cmake call. +option(BUILD_SHARED_LIBS "Build the shared library" ON) +option(BUILD_STATIC_LIBS "Build the static library" OFF) + +if(BUILD_SHARED_LIBS AND BUILD_STATIC_LIBS) + set(BUILD_SHARED_LIBS OFF) +endif() + # ========= DEFINE TREXIO C LIBRARY ========= -#IF DO_SHARED -add_library(trexio SHARED +# Set a list of TREXIO source files that are always compiled. +set(TREXIO_SOURCES src/trexio.c src/trexio_text.c ) -#ELIF DO_STATIC -#ENDIF +# Conditional SHARED/STATIC build for TREXIO library. +if(BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS) + message(STATUS "TREXIO :: shared C library will be built") + add_library(trexio SHARED ${TREXIO_SOURCES}) + +elseif(NOT BUILD_SHARED_LIBS AND BUILD_STATIC_LIBS) + + message(STATUS "TREXIO :: static C library will be built") + add_library(trexio STATIC ${TREXIO_SOURCES}) + +else() + message(FATAL_ERROR "Building both static and shared TREXIO simultaneously is not supported.") +endif() + +# Set TREXIO version and include files. set_target_properties(trexio PROPERTIES VERSION ${PROJECT_VERSION} PUBLIC_HEADER include/trexio.h ) +# Set directories to be included at build time. target_include_directories(trexio PRIVATE src) include_directories(include) # ============= CONFIGURE HDF5 ============== +# By default TREXIO is configured with HDF5 support. +# To change this, append -DENABLE-HDF5=OFF to the cmake call. option(ENABLE_HDF5 "Enable HDF5 support" ON) if(ENABLE_HDF5) + # Try to detect HDF5 installation using built-in FindHDF5.cmake macro. find_package(HDF5 REQUIRED COMPONENTS C HL) if(HDF5_FOUND) @@ -44,13 +72,18 @@ if(ENABLE_HDF5) else() # Download and install HDF5 library using FetchContent # ... - # For now - raise a FatalError - message(FATAL_ERROR "HDF5 is enabled not found.") + # For now - raise a FATAL_ERROR + message(FATAL_ERROR "HDF5 is enabled but not found.") endif() + # If HDF5 found: + # - append the trexio_hdf5.c source file with the HDF5 back end target_sources(trexio PRIVATE src/trexio_hdf5.c) + # - define symbol HAVE_HDF5=1 (use to activate HDF5 back end in the preprocessor conditionals) target_compile_definitions(trexio PUBLIC HAVE_HDF5=1) + # - include dirs with HDF5 header files target_include_directories(trexio PRIVATE ${HDF5_C_INCLUDE_DIRS}) + # - link to HDF5 C libraries target_link_libraries(trexio PRIVATE ${HDF5_C_HL_LIBRARIES} ${HDF5_C_LIBRARIES}) @@ -60,7 +93,7 @@ endif() enable_testing() -# create the lists of tests +# Create a list of tests for TEXT back end. set(Tests_text open_text io_dset_float_text @@ -73,6 +106,7 @@ set(Tests_text ) if(ENABLE_HDF5) +# Create a list of tests for HDF5 back end. set(Tests_hdf5 open_hdf5 io_dset_float_hdf5 @@ -83,12 +117,14 @@ if(ENABLE_HDF5) io_str_hdf5 overwrite_all_hdf5 ) + +# Set ${Tests} variable to the complete list of tests. set(Tests io_all ${Tests_text} ${Tests_hdf5}) else() set(Tests ${Tests_text}) endif() -# Add all the ADD_TEST for each test +# Compile each TREXIO test as an executable and add them to CTest using add_test foreach(test ${Tests}) add_executable(${test} tests/${test}.c) target_link_libraries(${test} PRIVATE trexio) @@ -98,6 +134,8 @@ endforeach () # ============= INSTALL TREXIO ============== include(GNUInstallDirs) +# Use standard GNU directories for installation of TREXIO (e.g. /usr/local/lib|include). +# The installation prefix can be modified using -DCMAKE_INSTALL_PREFIX= option of cmake. install(TARGETS trexio LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} From 6f499ea58d325892299ed311b1f0d056f68f6a4c Mon Sep 17 00:00:00 2001 From: q-posev Date: Mon, 15 Nov 2021 12:58:30 +0100 Subject: [PATCH 08/17] add Fortran support (module and test) --- CMakeLists.txt | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 03414be..355351e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,8 +57,8 @@ include_directories(include) # ============= CONFIGURE HDF5 ============== -# By default TREXIO is configured with HDF5 support. -# To change this, append -DENABLE-HDF5=OFF to the cmake call. +# By defaulti, TREXIO is configured with the HDF5 support. +# To change this, append -DENABLE_HDF5=OFF to the cmake call. option(ENABLE_HDF5 "Enable HDF5 support" ON) if(ENABLE_HDF5) @@ -68,7 +68,6 @@ if(ENABLE_HDF5) if(HDF5_FOUND) message(STATUS "HDF5 version :: ${HDF5_VERSION}") message(STATUS "HDF5 include dir :: ${HDF5_INCLUDE_DIRS}") - message(STATUS "HDF5 libraries :: ${HDF5_C_LIBRARIES}") else() # Download and install HDF5 library using FetchContent # ... @@ -77,11 +76,11 @@ if(ENABLE_HDF5) endif() # If HDF5 found: - # - append the trexio_hdf5.c source file with the HDF5 back end + # - append the trexio_hdf5.c source file with the HDF5 back end to target_sources target_sources(trexio PRIVATE src/trexio_hdf5.c) - # - define symbol HAVE_HDF5=1 (use to activate HDF5 back end in the preprocessor conditionals) + # - define symbol HAVE_HDF5=1 (used to activate HDF5 back end in the preprocessor conditionals) target_compile_definitions(trexio PUBLIC HAVE_HDF5=1) - # - include dirs with HDF5 header files + # - include directories with HDF5 header files target_include_directories(trexio PRIVATE ${HDF5_C_INCLUDE_DIRS}) # - link to HDF5 C libraries target_link_libraries(trexio PRIVATE @@ -124,13 +123,31 @@ else() set(Tests ${Tests_text}) endif() -# Compile each TREXIO test as an executable and add them to CTest using add_test +# Compile each TREXIO test as an executable and add them to CTest using add_test. foreach(test ${Tests}) add_executable(${test} tests/${test}.c) target_link_libraries(${test} PRIVATE trexio) add_test(NAME ${test} COMMAND $) endforeach () +# ============= FORTRAN SUPPORT ============== + +include(FortranCInterface) +# Check that C and Fortran compilers can talk to each other. +FortranCInterface_VERIFY() + +set(TREXIO_MOD_FILE ${CMAKE_SOURCE_DIR}/include/trexio_f.f90) + +# Add TREXIO Fortran module as a library. +add_library(trexio_f SHARED) +target_sources(trexio_f PUBLIC ${TREXIO_MOD_FILE}) +target_link_libraries(trexio_f PUBLIC trexio) + +# Add Fortran test and link it with the aforementioned library. +add_executable(test_f tests/test_f.f90) +target_link_libraries(test_f PRIVATE trexio_f) +add_test(NAME test_f COMMAND $) + # ============= INSTALL TREXIO ============== include(GNUInstallDirs) @@ -140,6 +157,8 @@ install(TARGETS trexio LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) +# Also install trexio_f.f90 file with TREXIO Fortran module. +install(FILES ${TREXIO_MOD_FILE} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) # =========================================== From f389ba29525bd19ab2f5709f5cab6916a8a7c1d2 Mon Sep 17 00:00:00 2001 From: q-posev Date: Tue, 16 Nov 2021 11:37:11 +0100 Subject: [PATCH 09/17] [WIP] better FindTREXIO module (following FindSDL2 example) --- tools/FindTREXIO.cmake | 80 +++++++++++++++++++++++++++++------------- 1 file changed, 56 insertions(+), 24 deletions(-) diff --git a/tools/FindTREXIO.cmake b/tools/FindTREXIO.cmake index 87fc57a..9c847a3 100644 --- a/tools/FindTREXIO.cmake +++ b/tools/FindTREXIO.cmake @@ -1,45 +1,77 @@ - - #=========================================== +# Try to find TREXIO library; +# If found, it will define the following variables (note the plural form): +# TREXIO_FOUND - System has libtrexio; +# TREXIO_INCLUDE_DIRS - The TREXIO include directories; +# TREXIO_LIBRARIES - The libraries needed to use TREXIO; + +# If TREXIO has been installed in a non-standard location, one can set an +# environment variable $TREXIO_DIR in the current shell: +# $ export TREXIO_DIR= +# to indicate the prefix used during the TREXIO installation +# (typically `./configure prefix= ..` or `cmake -DCMAKE_INSTALL_DIR= ..`) + # This file should be located WITHIN your project source tree. -# (e.g. in cmake/FindTREXIO.cmake file) +# (e.g. in cmake/FindTREXIO.cmake) +# How to use it in your project CMakeLists.txt: -# usage in CMakeLists.txt -#list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") -#find_package(TREXIO) -#include_directories("${TREXIO_INCLUDE_DIR}") -#target_link_libraries(your_target ${TREXIO_LIBRARY}) +# This is needed to locate FindTREXIO.cmake file, modify it according to your source tree. +# list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/") + +# find_package(TREXIO) +# if (TREXIO_FOUND) +# include_directories(${TREXIO_INCLUDE_DIRS}) +# target_link_libraries(your_target ${TREXIO_LIBRARIES}) +# endif() #=========================================== -# If already in cache, be silent -if (TREXIO_INCLUDE_DIR AND TREXIO_LIBRARY) - set(TREXIO_FIND_QUIETLY TRUE) -endif() +# This file is distirbuted under the BSD 3-Clause License. +# Copyright (c) 2021, TREX Center of Excellence -set(TREXIO_FOUND OFF) -set(TREXIO_LIBRARY) +#=========================================== -set(TREXIO_NAMES "libtrexio.so") -#set(TREXIO_NAMES "libtrexio.so" "libtrexio.dylib" "libtrexio.la") +message("") + +set(TREXIO_SEARCH_PATHS + ~/Library/Frameworks + /Library/Frameworks + /usr/local + /usr + /sw # Fink + /opt/local # DarwinPorts + /opt/csw # Blastwave + /opt +) find_path(TREXIO_INCLUDE_DIR NAMES trexio.h - HINTS /usr/include - /usr/local/include - $ENV{HOME}/include) + HINTS $ENV{TREXIO_DIR} + PATH_SUFFIXES include/trexio include + PATHS ${TREXIO_SEARCH_PATHS} + ) + +# No need to specify platform-specific prefix (e.g. libtrexio on Unix) or +# suffix (e.g. .so on Unix or .dylib on MacOS) in NAMES. CMake takes care of that. find_library(TREXIO_LIBRARY - NAMES ${TREXIO_NAMES} - PATHS /usr/lib - /usr/local/lib - $ENV{HOME}/lib - NO_DEFAULT_PATH) + NAMES trexio + HINTS $ENV{TREXIO_DIR} + PATH_SUFFIXES lib64 lib + PATHS ${TREXIO_SEARCH_PATHS} + ) +message("") # Handle the QUIETLY and REQUIRED arguments and set TREXIO_FOUND to TRUE if # all listed variables are TRUE. INCLUDE(FindPackageHandleStandardArgs) FIND_PACKAGE_HANDLE_STANDARD_ARGS(TREXIO DEFAULT_MSG TREXIO_LIBRARY TREXIO_INCLUDE_DIR ) MARK_AS_ADVANCED(TREXIO_INCLUDE_DIR TREXIO_LIBRARY) + +# Mot setting _INCLUDE_DIR and _LIBRARIES is considered a bug, +# see https://gitlab.kitware.com/cmake/community/-/wikis/doc/tutorials/How-To-Find-Libraries +set(TREXIO_LIBRARIES ${TREXIO_LIBRARY}) +set(TREXIO_INCLUDE_DIRS ${TREXIO_INCLUDE_DIR}) + From 5f4994fc3106dc2d2253e67c28dfd38f7b589fb2 Mon Sep 17 00:00:00 2001 From: q-posev Date: Tue, 16 Nov 2021 17:52:05 +0100 Subject: [PATCH 10/17] add developer mode (with code auto-generation) to CMake --- CMakeLists.txt | 53 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 355351e..3bc44e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,17 @@ project(Trexio set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED ON) +# Optional configure for developer mode to generate source code from org-mode files. +option(TREXIO_DEVEL "TREXIO developer mode (for code generation)." OFF) + +if(EXISTS ".git/config") + set(TREXIO_DEVEL ON) + find_package(Python3) + if(Python3_FOUND) + message(STATUS "Python3 version :: ${Python3_VERSION}") + endif() +endif() + # By default, the shared TREXIO library is built (even without -DBUILD_SHARED_LIBS=ON). # To change this behaviour, append -DBUILD_STATIC_LIBS=ON to the cmake call. option(BUILD_SHARED_LIBS "Build the shared library" ON) @@ -26,20 +37,21 @@ endif() # Set a list of TREXIO source files that are always compiled. set(TREXIO_SOURCES - src/trexio.c - src/trexio_text.c + ${CMAKE_SOURCE_DIR}/src/trexio.c + ${CMAKE_SOURCE_DIR}/src/trexio_text.c ) +set(TREXIO_HEADER ${CMAKE_SOURCE_DIR}/include/trexio.h) # Conditional SHARED/STATIC build for TREXIO library. if(BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS) message(STATUS "TREXIO :: shared C library will be built") - add_library(trexio SHARED ${TREXIO_SOURCES}) + add_library(trexio SHARED) elseif(NOT BUILD_SHARED_LIBS AND BUILD_STATIC_LIBS) message(STATUS "TREXIO :: static C library will be built") - add_library(trexio STATIC ${TREXIO_SOURCES}) + add_library(trexio STATIC) else() message(FATAL_ERROR "Building both static and shared TREXIO simultaneously is not supported.") @@ -48,7 +60,7 @@ endif() # Set TREXIO version and include files. set_target_properties(trexio PROPERTIES VERSION ${PROJECT_VERSION} - PUBLIC_HEADER include/trexio.h + PUBLIC_HEADER ${TREXIO_HEADER} ) # Set directories to be included at build time. @@ -76,10 +88,11 @@ if(ENABLE_HDF5) endif() # If HDF5 found: - # - append the trexio_hdf5.c source file with the HDF5 back end to target_sources - target_sources(trexio PRIVATE src/trexio_hdf5.c) + # - append the trexio_hdf5.c source file with the HDF5 back end to the list TREXIO_SOURCES + set(TREXIO_HDF5_SOURCE ${CMAKE_SOURCE_DIR}/src/trexio_hdf5.c) + list(APPEND TREXIO_SOURCES ${TREXIO_HDF5_SOURCE}) # - define symbol HAVE_HDF5=1 (used to activate HDF5 back end in the preprocessor conditionals) - target_compile_definitions(trexio PUBLIC HAVE_HDF5=1) + target_compile_definitions(trexio PUBLIC "HAVE_HDF5") # - include directories with HDF5 header files target_include_directories(trexio PRIVATE ${HDF5_C_INCLUDE_DIRS}) # - link to HDF5 C libraries @@ -88,6 +101,8 @@ if(ENABLE_HDF5) ${HDF5_C_LIBRARIES}) endif() +target_sources(trexio PRIVATE ${TREXIO_SOURCES}) + # ================= TESTING ================= enable_testing() @@ -148,6 +163,28 @@ add_executable(test_f tests/test_f.f90) target_link_libraries(test_f PRIVATE trexio_f) add_test(NAME test_f COMMAND $) +# ====== CODE GENERATION FOR DEVEL MODE ===== + +if(TREXIO_DEVEL) + set(ORG_FILES + src/templates_front/templator_front.org + src/templates_text/templator_text.org + trex.org + ) + if(ENABLE_HDF5) + list(APPEND ORG_FILES src/templates_hdf5/templator_hdf5.org) + endif() + + add_custom_command(OUTPUT ${TREXIO_SOURCES} + ${TREXIO_HEADER} + ${TREXIO_MOD_FILE} + COMMAND ./build_trexio.sh + DEPENDS ${ORG_FILES} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tools + COMMENT "Generating TREXIO source code from org-mode files." + VERBATIM) +endif() + # ============= INSTALL TREXIO ============== include(GNUInstallDirs) From 6feef1cd5f847ac09b33b3cc83af4fc7aab651b8 Mon Sep 17 00:00:00 2001 From: q-posev Date: Tue, 16 Nov 2021 18:10:36 +0100 Subject: [PATCH 11/17] add custom uninstall target to CMake --- CMakeLists.txt | 12 ++++++++++++ tools/cmake_uninstall.cmake.in | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 tools/cmake_uninstall.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 3bc44e1..b688d92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -197,5 +197,17 @@ install(TARGETS trexio # Also install trexio_f.f90 file with TREXIO Fortran module. install(FILES ${TREXIO_MOD_FILE} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) +# Uninstall target, copied from CMake FAQ: +# https://gitlab.kitware.com/cmake/community/wikis/FAQ#can-i-do-make-uninstall-with-cmake +if(NOT TARGET uninstall) + configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake_uninstall.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" + IMMEDIATE @ONLY) + + add_custom_target(uninstall + COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake) +endif() + # =========================================== diff --git a/tools/cmake_uninstall.cmake.in b/tools/cmake_uninstall.cmake.in new file mode 100644 index 0000000..f779906 --- /dev/null +++ b/tools/cmake_uninstall.cmake.in @@ -0,0 +1,24 @@ +# Needed to make uninstall target in CMake, see the CMake FAQ: +# https://gitlab.kitware.com/cmake/community/wikis/FAQ#can-i-do-make-uninstall-with-cmake + +if(NOT EXISTS "@CMAKE_BINARY_DIR@/install_manifest.txt") + message(FATAL_ERROR "Cannot find install manifest: @CMAKE_BINARY_DIR@/install_manifest.txt") +endif() + +file(READ "@CMAKE_BINARY_DIR@/install_manifest.txt" files) +string(REGEX REPLACE "\n" ";" files "${files}") +foreach(file ${files}) + message(STATUS "Uninstalling $ENV{DESTDIR}${file}") + if(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}") + exec_program( + "@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\"" + OUTPUT_VARIABLE rm_out + RETURN_VALUE rm_retval + ) + if(NOT "${rm_retval}" STREQUAL 0) + message(FATAL_ERROR "Problem when removing $ENV{DESTDIR}${file}") + endif() + else(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}") + message(STATUS "File $ENV{DESTDIR}${file} does not exist.") + endif() +endforeach() From 21131ed352b709ec382e6221fc7b6234a4e998b8 Mon Sep 17 00:00:00 2001 From: q-posev Date: Tue, 16 Nov 2021 18:11:42 +0100 Subject: [PATCH 12/17] include CMake-specific files into Autotools distribution --- Makefile.am | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Makefile.am b/Makefile.am index 3935e1f..7faee3c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -76,6 +76,11 @@ ORG_FILES = \ src_libtrexio_la_SOURCES = $(SOURCES) +# Include CMake-related files in the distribution. +dist_data_DATA = CMakeLists.txt \ + tools/cmake_uninstall.cmake.in \ + tools/FindTREXIO.cmake + # =============== TESTS =============== # TESTS_C = \ From a1e6fcbfc5493a4559c2581ef077bd4666dfcd39 Mon Sep 17 00:00:00 2001 From: q-posev Date: Wed, 17 Nov 2021 10:07:19 +0100 Subject: [PATCH 13/17] add detection of emacs and min required version of Python3 --- CMakeLists.txt | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b688d92..073d0f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,9 +18,19 @@ option(TREXIO_DEVEL "TREXIO developer mode (for code generation)." OFF) if(EXISTS ".git/config") set(TREXIO_DEVEL ON) - find_package(Python3) + find_package(Python3 REQUIRED) if(Python3_FOUND) - message(STATUS "Python3 version :: ${Python3_VERSION}") + if(Python3_VERSION_MINOR LESS 6) + message(FATAL_ERROR "Required Python3 version :: >= 3.6; Found :: ${Python3_VERSION}") + else() + message(STATUS "Python3 version :: ${Python3_VERSION}") + endif() + endif() + find_program(EMACS NAMES emacs REQUIRED) + if(NOT EMACS-NOTFOUND) + message(STATUS "EMACS detected :: ${EMACS}") + else() + message(FATAL_ERROR "EMACS not found. It is required to produce TREXIO source code from org-mode files.") endif() endif() From 3dbe2f66ddc29242f58d70dbee477c25ec87b243 Mon Sep 17 00:00:00 2001 From: q-posev Date: Wed, 17 Nov 2021 10:57:03 +0100 Subject: [PATCH 14/17] add missing dependency on private headers --- CMakeLists.txt | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 073d0f4..07ab5a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,12 +45,17 @@ endif() # ========= DEFINE TREXIO C LIBRARY ========= -# Set a list of TREXIO source files that are always compiled. +# Set a list of TREXIO source and header files that are always compiled. set(TREXIO_SOURCES ${CMAKE_SOURCE_DIR}/src/trexio.c ${CMAKE_SOURCE_DIR}/src/trexio_text.c ) -set(TREXIO_HEADER ${CMAKE_SOURCE_DIR}/include/trexio.h) +set(TREXIO_PUBLIC_HEADERS ${CMAKE_SOURCE_DIR}/include/trexio.h) +set(TREXIO_PRIVATE_HEADERS + ${CMAKE_SOURCE_DIR}/src/trexio_s.h + ${CMAKE_SOURCE_DIR}/src/trexio_private.h + ${CMAKE_SOURCE_DIR}/src/trexio_text.h + ) # Conditional SHARED/STATIC build for TREXIO library. if(BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS) @@ -70,11 +75,10 @@ endif() # Set TREXIO version and include files. set_target_properties(trexio PROPERTIES VERSION ${PROJECT_VERSION} - PUBLIC_HEADER ${TREXIO_HEADER} + PUBLIC_HEADER ${TREXIO_PUBLIC_HEADERS} ) # Set directories to be included at build time. -target_include_directories(trexio PRIVATE src) include_directories(include) # ============= CONFIGURE HDF5 ============== @@ -99,8 +103,8 @@ if(ENABLE_HDF5) # If HDF5 found: # - append the trexio_hdf5.c source file with the HDF5 back end to the list TREXIO_SOURCES - set(TREXIO_HDF5_SOURCE ${CMAKE_SOURCE_DIR}/src/trexio_hdf5.c) - list(APPEND TREXIO_SOURCES ${TREXIO_HDF5_SOURCE}) + list(APPEND TREXIO_SOURCES ${CMAKE_SOURCE_DIR}/src/trexio_hdf5.c) + list(APPEND TREXIO_PRIVATE_HEADERS ${CMAKE_SOURCE_DIR}/src/trexio_hdf5.h) # - define symbol HAVE_HDF5=1 (used to activate HDF5 back end in the preprocessor conditionals) target_compile_definitions(trexio PUBLIC "HAVE_HDF5") # - include directories with HDF5 header files @@ -111,7 +115,9 @@ if(ENABLE_HDF5) ${HDF5_C_LIBRARIES}) endif() -target_sources(trexio PRIVATE ${TREXIO_SOURCES}) +# Private headers have to be listed as sources, otherwise they are installed +# with public headers upon make install (when using PRIVATE_HEADER property). +target_sources(trexio PRIVATE ${TREXIO_SOURCES} ${TREXIO_PRIVATE_HEADERS}) # ================= TESTING ================= @@ -186,7 +192,8 @@ if(TREXIO_DEVEL) endif() add_custom_command(OUTPUT ${TREXIO_SOURCES} - ${TREXIO_HEADER} + ${TREXIO_PUBLIC_HEADERS} + ${TREXIO_PRIVATE_HEADERS} ${TREXIO_MOD_FILE} COMMAND ./build_trexio.sh DEPENDS ${ORG_FILES} @@ -201,9 +208,9 @@ include(GNUInstallDirs) # Use standard GNU directories for installation of TREXIO (e.g. /usr/local/lib|include). # The installation prefix can be modified using -DCMAKE_INSTALL_PREFIX= option of cmake. install(TARGETS trexio - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} - ) + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + ) # Also install trexio_f.f90 file with TREXIO Fortran module. install(FILES ${TREXIO_MOD_FILE} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) From ae753108ffd794c34e1b1db800788bd8202b981f Mon Sep 17 00:00:00 2001 From: q-posev Date: Wed, 17 Nov 2021 11:02:35 +0100 Subject: [PATCH 15/17] move FindTREXIO and cmake_uninstall modules in cmake directory --- CMakeLists.txt | 2 +- Makefile.am | 4 ++-- {tools => cmake}/FindTREXIO.cmake | 0 {tools => cmake}/cmake_uninstall.cmake.in | 0 4 files changed, 3 insertions(+), 3 deletions(-) rename {tools => cmake}/FindTREXIO.cmake (100%) rename {tools => cmake}/cmake_uninstall.cmake.in (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 07ab5a8..554676d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -218,7 +218,7 @@ install(FILES ${TREXIO_MOD_FILE} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) # https://gitlab.kitware.com/cmake/community/wikis/FAQ#can-i-do-make-uninstall-with-cmake if(NOT TARGET uninstall) configure_file( - "${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake_uninstall.cmake.in" + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" IMMEDIATE @ONLY) diff --git a/Makefile.am b/Makefile.am index 7faee3c..6338065 100644 --- a/Makefile.am +++ b/Makefile.am @@ -78,8 +78,8 @@ src_libtrexio_la_SOURCES = $(SOURCES) # Include CMake-related files in the distribution. dist_data_DATA = CMakeLists.txt \ - tools/cmake_uninstall.cmake.in \ - tools/FindTREXIO.cmake + cmake/cmake_uninstall.cmake.in \ + cmake/FindTREXIO.cmake # =============== TESTS =============== # diff --git a/tools/FindTREXIO.cmake b/cmake/FindTREXIO.cmake similarity index 100% rename from tools/FindTREXIO.cmake rename to cmake/FindTREXIO.cmake diff --git a/tools/cmake_uninstall.cmake.in b/cmake/cmake_uninstall.cmake.in similarity index 100% rename from tools/cmake_uninstall.cmake.in rename to cmake/cmake_uninstall.cmake.in From 72c355c7ccb333e38c08d6111d54ff1bd3d0b2fd Mon Sep 17 00:00:00 2001 From: q-posev Date: Wed, 17 Nov 2021 14:00:35 +0100 Subject: [PATCH 16/17] Top-level CMakeLists with subdirectories --- CMakeLists.txt | 192 +------------------------------------------ Makefile.am | 2 + src/CMakeLists.txt | 147 +++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 46 +++++++++++ 4 files changed, 199 insertions(+), 188 deletions(-) create mode 100644 src/CMakeLists.txt create mode 100644 tests/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 554676d..062a117 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,197 +34,13 @@ if(EXISTS ".git/config") endif() endif() -# By default, the shared TREXIO library is built (even without -DBUILD_SHARED_LIBS=ON). -# To change this behaviour, append -DBUILD_STATIC_LIBS=ON to the cmake call. -option(BUILD_SHARED_LIBS "Build the shared library" ON) -option(BUILD_STATIC_LIBS "Build the static library" OFF) - -if(BUILD_SHARED_LIBS AND BUILD_STATIC_LIBS) - set(BUILD_SHARED_LIBS OFF) -endif() - -# ========= DEFINE TREXIO C LIBRARY ========= - -# Set a list of TREXIO source and header files that are always compiled. -set(TREXIO_SOURCES - ${CMAKE_SOURCE_DIR}/src/trexio.c - ${CMAKE_SOURCE_DIR}/src/trexio_text.c - ) -set(TREXIO_PUBLIC_HEADERS ${CMAKE_SOURCE_DIR}/include/trexio.h) -set(TREXIO_PRIVATE_HEADERS - ${CMAKE_SOURCE_DIR}/src/trexio_s.h - ${CMAKE_SOURCE_DIR}/src/trexio_private.h - ${CMAKE_SOURCE_DIR}/src/trexio_text.h - ) - -# Conditional SHARED/STATIC build for TREXIO library. -if(BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS) - - message(STATUS "TREXIO :: shared C library will be built") - add_library(trexio SHARED) - -elseif(NOT BUILD_SHARED_LIBS AND BUILD_STATIC_LIBS) - - message(STATUS "TREXIO :: static C library will be built") - add_library(trexio STATIC) - -else() - message(FATAL_ERROR "Building both static and shared TREXIO simultaneously is not supported.") -endif() - -# Set TREXIO version and include files. -set_target_properties(trexio PROPERTIES - VERSION ${PROJECT_VERSION} - PUBLIC_HEADER ${TREXIO_PUBLIC_HEADERS} - ) - # Set directories to be included at build time. include_directories(include) -# ============= CONFIGURE HDF5 ============== - -# By defaulti, TREXIO is configured with the HDF5 support. -# To change this, append -DENABLE_HDF5=OFF to the cmake call. -option(ENABLE_HDF5 "Enable HDF5 support" ON) - -if(ENABLE_HDF5) - # Try to detect HDF5 installation using built-in FindHDF5.cmake macro. - find_package(HDF5 REQUIRED COMPONENTS C HL) - - if(HDF5_FOUND) - message(STATUS "HDF5 version :: ${HDF5_VERSION}") - message(STATUS "HDF5 include dir :: ${HDF5_INCLUDE_DIRS}") - else() - # Download and install HDF5 library using FetchContent - # ... - # For now - raise a FATAL_ERROR - message(FATAL_ERROR "HDF5 is enabled but not found.") - endif() - - # If HDF5 found: - # - append the trexio_hdf5.c source file with the HDF5 back end to the list TREXIO_SOURCES - list(APPEND TREXIO_SOURCES ${CMAKE_SOURCE_DIR}/src/trexio_hdf5.c) - list(APPEND TREXIO_PRIVATE_HEADERS ${CMAKE_SOURCE_DIR}/src/trexio_hdf5.h) - # - define symbol HAVE_HDF5=1 (used to activate HDF5 back end in the preprocessor conditionals) - target_compile_definitions(trexio PUBLIC "HAVE_HDF5") - # - include directories with HDF5 header files - target_include_directories(trexio PRIVATE ${HDF5_C_INCLUDE_DIRS}) - # - link to HDF5 C libraries - target_link_libraries(trexio PRIVATE - ${HDF5_C_HL_LIBRARIES} - ${HDF5_C_LIBRARIES}) -endif() - -# Private headers have to be listed as sources, otherwise they are installed -# with public headers upon make install (when using PRIVATE_HEADER property). -target_sources(trexio PRIVATE ${TREXIO_SOURCES} ${TREXIO_PRIVATE_HEADERS}) - -# ================= TESTING ================= +# Add subdirectory with add_library(trexio) specifications. +add_subdirectory(src) +# Add subdirectory with unit tests. enable_testing() - -# Create a list of tests for TEXT back end. -set(Tests_text - open_text - io_dset_float_text - io_dset_str_text - io_safe_dset_float_text - io_dset_int_text - io_num_text - io_str_text - overwrite_all_text - ) - -if(ENABLE_HDF5) -# Create a list of tests for HDF5 back end. - set(Tests_hdf5 - open_hdf5 - io_dset_float_hdf5 - io_dset_str_hdf5 - io_safe_dset_float_hdf5 - io_dset_int_hdf5 - io_num_hdf5 - io_str_hdf5 - overwrite_all_hdf5 - ) - -# Set ${Tests} variable to the complete list of tests. - set(Tests io_all ${Tests_text} ${Tests_hdf5}) -else() - set(Tests ${Tests_text}) -endif() - -# Compile each TREXIO test as an executable and add them to CTest using add_test. -foreach(test ${Tests}) - add_executable(${test} tests/${test}.c) - target_link_libraries(${test} PRIVATE trexio) - add_test(NAME ${test} COMMAND $) -endforeach () - -# ============= FORTRAN SUPPORT ============== - -include(FortranCInterface) -# Check that C and Fortran compilers can talk to each other. -FortranCInterface_VERIFY() - -set(TREXIO_MOD_FILE ${CMAKE_SOURCE_DIR}/include/trexio_f.f90) - -# Add TREXIO Fortran module as a library. -add_library(trexio_f SHARED) -target_sources(trexio_f PUBLIC ${TREXIO_MOD_FILE}) -target_link_libraries(trexio_f PUBLIC trexio) - -# Add Fortran test and link it with the aforementioned library. -add_executable(test_f tests/test_f.f90) -target_link_libraries(test_f PRIVATE trexio_f) -add_test(NAME test_f COMMAND $) - -# ====== CODE GENERATION FOR DEVEL MODE ===== - -if(TREXIO_DEVEL) - set(ORG_FILES - src/templates_front/templator_front.org - src/templates_text/templator_text.org - trex.org - ) - if(ENABLE_HDF5) - list(APPEND ORG_FILES src/templates_hdf5/templator_hdf5.org) - endif() - - add_custom_command(OUTPUT ${TREXIO_SOURCES} - ${TREXIO_PUBLIC_HEADERS} - ${TREXIO_PRIVATE_HEADERS} - ${TREXIO_MOD_FILE} - COMMAND ./build_trexio.sh - DEPENDS ${ORG_FILES} - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tools - COMMENT "Generating TREXIO source code from org-mode files." - VERBATIM) -endif() - -# ============= INSTALL TREXIO ============== - -include(GNUInstallDirs) -# Use standard GNU directories for installation of TREXIO (e.g. /usr/local/lib|include). -# The installation prefix can be modified using -DCMAKE_INSTALL_PREFIX= option of cmake. -install(TARGETS trexio - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} - ) -# Also install trexio_f.f90 file with TREXIO Fortran module. -install(FILES ${TREXIO_MOD_FILE} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) - -# Uninstall target, copied from CMake FAQ: -# https://gitlab.kitware.com/cmake/community/wikis/FAQ#can-i-do-make-uninstall-with-cmake -if(NOT TARGET uninstall) - configure_file( - "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in" - "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" - IMMEDIATE @ONLY) - - add_custom_target(uninstall - COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake) -endif() - -# =========================================== +add_subdirectory(tests) diff --git a/Makefile.am b/Makefile.am index 6338065..6e71d7c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -78,6 +78,8 @@ src_libtrexio_la_SOURCES = $(SOURCES) # Include CMake-related files in the distribution. dist_data_DATA = CMakeLists.txt \ + src/CMakeLists.txt \ + tests/CMakeLists.txt \ cmake/cmake_uninstall.cmake.in \ cmake/FindTREXIO.cmake diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..9e87cd9 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,147 @@ + +# ========= DEFINE TREXIO C LIBRARY ========= + +# Set a list of TREXIO source and header files that are always compiled. +set(TREXIO_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/trexio.c + ${CMAKE_CURRENT_SOURCE_DIR}/trexio_text.c + ) +set(TREXIO_PUBLIC_HEADERS ${CMAKE_SOURCE_DIR}/include/trexio.h) +set(TREXIO_PRIVATE_HEADERS + ${CMAKE_CURRENT_SOURCE_DIR}/trexio_s.h + ${CMAKE_CURRENT_SOURCE_DIR}/trexio_private.h + ${CMAKE_CURRENT_SOURCE_DIR}/trexio_text.h + ) + +# By default, the shared TREXIO library is built (even without -DBUILD_SHARED_LIBS=ON). +# To change this behaviour, append -DBUILD_STATIC_LIBS=ON to the cmake call. +option(BUILD_SHARED_LIBS "Build the shared library" ON) +option(BUILD_STATIC_LIBS "Build the static library" OFF) + +if(BUILD_SHARED_LIBS AND BUILD_STATIC_LIBS) + set(BUILD_SHARED_LIBS OFF) +endif() + +# Conditional SHARED/STATIC build for TREXIO library. +if(BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS) + + message(STATUS "TREXIO :: shared C library will be built") + add_library(trexio SHARED) + +elseif(NOT BUILD_SHARED_LIBS AND BUILD_STATIC_LIBS) + + message(STATUS "TREXIO :: static C library will be built") + add_library(trexio STATIC) + # Static TREXIO has to be compiled with -fPIC flag. For Shared it is done by default. + set_property(TARGET trexio PROPERTY POSITION_INDEPENDENT_CODE ON) + +else() + message(FATAL_ERROR "Building both static and shared TREXIO simultaneously is not supported.") +endif() + +# Set TREXIO version and include files. +set_target_properties(trexio PROPERTIES + VERSION ${PROJECT_VERSION} + PUBLIC_HEADER ${TREXIO_PUBLIC_HEADERS} + ) + +# ============= CONFIGURE HDF5 ============== + +# By defaulti, TREXIO is configured with the HDF5 support. +# To change this, append -DENABLE_HDF5=OFF to the cmake call. +option(ENABLE_HDF5 "Enable HDF5 support" ON) + +if(ENABLE_HDF5) + # Try to detect HDF5 installation using built-in FindHDF5.cmake macro. + find_package(HDF5 REQUIRED COMPONENTS C HL) + + if(HDF5_FOUND) + message(STATUS "HDF5 version :: ${HDF5_VERSION}") + message(STATUS "HDF5 include dir :: ${HDF5_INCLUDE_DIRS}") + else() + # Download and install HDF5 library using FetchContent + # ... + # For now - raise a FATAL_ERROR + message(FATAL_ERROR "HDF5 is enabled but not found.") + endif() + + # If HDF5 found: + # - append the trexio_hdf5.c source file with the HDF5 back end to the list TREXIO_SOURCES + list(APPEND TREXIO_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/trexio_hdf5.c) + list(APPEND TREXIO_PRIVATE_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/trexio_hdf5.h) + # - define symbol HAVE_HDF5=1 (used to activate HDF5 back end in the preprocessor conditionals) + target_compile_definitions(trexio PUBLIC "HAVE_HDF5") + # - include directories with HDF5 header files + target_include_directories(trexio PRIVATE ${HDF5_C_INCLUDE_DIRS}) + # - link to HDF5 C libraries + target_link_libraries(trexio PRIVATE + ${HDF5_C_HL_LIBRARIES} + ${HDF5_C_LIBRARIES}) +endif() + +# Private headers have to be listed as sources, otherwise they are installed +# with public headers upon make install (when using PRIVATE_HEADER property). +target_sources(trexio PRIVATE ${TREXIO_SOURCES} ${TREXIO_PRIVATE_HEADERS}) + +# ============= FORTRAN SUPPORT ============== + +include(FortranCInterface) +# Check that C and Fortran compilers can talk to each other. +FortranCInterface_VERIFY() + +# Fortran module +set(TREXIO_MOD_FILE ${CMAKE_SOURCE_DIR}/include/trexio_f.f90) +# Add TREXIO Fortran module as a library. +add_library(trexio_f SHARED) +target_sources(trexio_f PUBLIC ${TREXIO_MOD_FILE}) +target_link_libraries(trexio_f PUBLIC trexio) + +# ====== CODE GENERATION FOR DEVEL MODE ===== + +if(TREXIO_DEVEL) + set(ORG_FILES + templates_front/templator_front.org + templates_text/templator_text.org + ${CMAKE_SOURCE_DIR}/trex.org + ) + if(ENABLE_HDF5) + list(APPEND ORG_FILES templates_hdf5/templator_hdf5.org) + endif() + + add_custom_command(OUTPUT + ${TREXIO_SOURCES} + ${TREXIO_PUBLIC_HEADERS} + ${TREXIO_PRIVATE_HEADERS} + ${TREXIO_MOD_FILE} + COMMAND ./build_trexio.sh + DEPENDS ${ORG_FILES} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tools + COMMENT "Generating TREXIO source code from org-mode files." + VERBATIM) +endif() + +# ============= INSTALL TREXIO ============== + +include(GNUInstallDirs) +# Use standard GNU directories for installation of TREXIO (e.g. /usr/local/lib|include). +# The installation prefix can be modified using -DCMAKE_INSTALL_PREFIX= option of cmake. +install(TARGETS trexio + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + ) +# Also install trexio_f.f90 file with TREXIO Fortran module. +install(FILES ${TREXIO_MOD_FILE} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) + +# Uninstall target, copied from CMake FAQ: +# https://gitlab.kitware.com/cmake/community/wikis/FAQ#can-i-do-make-uninstall-with-cmake +if(NOT TARGET uninstall) + configure_file( + "${CMAKE_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" + IMMEDIATE @ONLY) + + add_custom_target(uninstall + COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake) +endif() + +# =========================================== diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..cf2d896 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,46 @@ + +# ================= TESTING ================= + +# Create a list of tests for TEXT back end. +set(Tests_text + open_text + io_dset_float_text + io_dset_str_text + io_safe_dset_float_text + io_dset_int_text + io_num_text + io_str_text + overwrite_all_text + ) + +if(ENABLE_HDF5) +# Create a list of tests for HDF5 back end. + set(Tests_hdf5 + open_hdf5 + io_dset_float_hdf5 + io_dset_str_hdf5 + io_safe_dset_float_hdf5 + io_dset_int_hdf5 + io_num_hdf5 + io_str_hdf5 + overwrite_all_hdf5 + ) + +# Set ${Tests} variable to the complete list of tests. + set(Tests io_all ${Tests_text} ${Tests_hdf5}) +else() + set(Tests ${Tests_text}) +endif() + +# Compile each TREXIO test as an executable and add them to CTest using add_test. +foreach(Test ${Tests}) + add_executable(${Test} ${Test}.c) + target_link_libraries(${Test} PRIVATE trexio) + add_test(NAME ${Test} COMMAND $) +endforeach() + +# Add Fortran test and link it with trexio_f (Fortran module) library. +add_executable(test_f test_f.f90) +target_link_libraries(test_f PRIVATE trexio_f) +add_test(NAME test_f COMMAND $) + From ac2e070956a0c715ce98dc09d32b66a6ac855f7c Mon Sep 17 00:00:00 2001 From: q-posev Date: Thu, 18 Nov 2021 13:59:34 +0100 Subject: [PATCH 17/17] rename html target to html-local folowing autoconf warning --- Makefile.am | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile.am b/Makefile.am index 6e71d7c..98486a8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -181,8 +181,8 @@ dist_html_DATA = $(HTML_FILES) $(HTML_TANGLED): $(htmlizer) -html: $(dist_html_DATA) -doc: html +html-local: $(dist_html_DATA) +doc: html-local # =============== DEVELOPER MODE =============== #