From 14dab3da0548cb4134d79ad91a67f854e564f5ef Mon Sep 17 00:00:00 2001 From: Matthew Thompson Date: Wed, 3 May 2023 09:53:46 -0400 Subject: [PATCH 1/3] Update CMake to allow for prebuild dependencies --- CMakeLists.txt | 1 + cmake/Findfunctional.cmake | 30 +++++++++++++ cmake/functional.cmake | 36 +++++++++------- cmake/h5fortran.cmake | 38 ++++++++++++----- cmake/json.cmake | 86 ++++++++++++++++++++++---------------- 5 files changed, 132 insertions(+), 59 deletions(-) create mode 100644 cmake/Findfunctional.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index bd48e993..c658d4d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ include(FetchContent) include(cmake/options.cmake) include(cmake/compilers.cmake) +list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) include(cmake/functional.cmake) include(cmake/h5fortran.cmake) include(cmake/json.cmake) diff --git a/cmake/Findfunctional.cmake b/cmake/Findfunctional.cmake new file mode 100644 index 00000000..3663a9b9 --- /dev/null +++ b/cmake/Findfunctional.cmake @@ -0,0 +1,30 @@ +# This is a CMake file to detect the installation of the functional-fortran library +# An install of this library provides: +# +# 1. A library named "libfunctional.a" +# 2. A variety of Fortran .mod files + +include(FindPackageHandleStandardArgs) + +find_library(functional_LIBRARY + NAMES functional) +message(STATUS "functional_LIBRARY: ${functional_LIBRARY}") + +find_path(functional_INCLUDE_DIR + NAMES functional.mod) +message(STATUS "functional_INCLUDE_DIR: ${functional_INCLUDE_DIR}") + +find_package_handle_standard_args(functional DEFAULT_MSG + functional_LIBRARY functional_INCLUDE_DIR) +message(STATUS "functional_FOUND: ${functional_FOUND}") + +if (functional_FOUND) + mark_as_advanced(functional_INCLUDE_DIR) + mark_as_advanced(functional_LIBRARY) +endif() + +if (functional_FOUND AND NOT functional::functional) + add_library(functional::functional IMPORTED STATIC) + set_property(TARGET functional::functional PROPERTY IMPORTED_LOCATION ${functional_LIBRARY}) + target_include_directories(functional::functional INTERFACE ${functional_INCLUDE_DIR}) +endif() diff --git a/cmake/functional.cmake b/cmake/functional.cmake index d8f21200..96e86368 100644 --- a/cmake/functional.cmake +++ b/cmake/functional.cmake @@ -1,18 +1,26 @@ -FetchContent_Declare(functional - GIT_REPOSITORY https://github.com/wavebitscientific/functional-fortran - GIT_TAG 0.6.1 - GIT_SHALLOW true -) +find_package(functional QUIET) -FetchContent_Populate(functional) +if (NOT functional_FOUND) + message(STATUS "functional not found, fetching from GitHub") -add_library(functional ${functional_SOURCE_DIR}/src/functional.f90) -target_include_directories(functional PUBLIC -$ -$ -) + FetchContent_Declare(functional + GIT_REPOSITORY https://github.com/wavebitscientific/functional-fortran + GIT_TAG 0.6.1 + GIT_SHALLOW true + ) -add_library(functional::functional INTERFACE IMPORTED GLOBAL) -target_link_libraries(functional::functional INTERFACE functional) + FetchContent_Populate(functional) -install(TARGETS functional) + add_library(functional ${functional_SOURCE_DIR}/src/functional.f90) + target_include_directories(functional PUBLIC + $ + $ + ) + + add_library(functional::functional INTERFACE IMPORTED GLOBAL) + target_link_libraries(functional::functional INTERFACE functional) + + install(TARGETS functional) +else () + message(STATUS "functional found, using system version") +endif () diff --git a/cmake/h5fortran.cmake b/cmake/h5fortran.cmake index 6351e63a..b2c4baaa 100644 --- a/cmake/h5fortran.cmake +++ b/cmake/h5fortran.cmake @@ -1,15 +1,33 @@ -set(h5fortran_BUILD_TESTING false) +find_package(HDF5 COMPONENTS Fortran REQUIRED + OPTIONAL_COMPONENTS C HL) +if (HDF5_HL_FOUND) + message(STATUS "HDF5 HL is available") + target_link_libraries(HDF5::HDF5 INTERFACE hdf5::hdf5_hl hdf5::hdf5_hl_fortran) +endif() +if (HDF5_IS_PARALLEL) + message(STATUS "HDF5 is parallel") + target_link_libraries(HDF5::HDF5 INTERFACE MPI::MPI_Fortran) +endif() -FetchContent_Declare(h5fortran - GIT_REPOSITORY https://github.com/geospace-code/h5fortran - GIT_TAG v4.6.3 - GIT_SHALLOW true -) +find_package(h5fortran 4.6.3 QUIET) -FetchContent_MakeAvailable(h5fortran) +if (NOT h5fortran_FOUND) + message(STATUS "h5fortran not found, fetching from GitHub") -file(MAKE_DIRECTORY ${h5fortran_BINARY_DIR}/include) + set(h5fortran_BUILD_TESTING false) + FetchContent_Declare(h5fortran + GIT_REPOSITORY https://github.com/geospace-code/h5fortran + GIT_TAG v4.6.3 + GIT_SHALLOW true + ) -list(APPEND CMAKE_MODULE_PATH ${h5fortran_SOURCE_DIR}/cmake/Modules) -find_package(HDF5 COMPONENTS Fortran REQUIRED) + FetchContent_MakeAvailable(h5fortran) + + file(MAKE_DIRECTORY ${h5fortran_BINARY_DIR}/include) + + + list(APPEND CMAKE_MODULE_PATH ${h5fortran_SOURCE_DIR}/cmake/Modules) +else() + message(STATUS "h5fortran found: ${h5fortran_DIR}") +endif() diff --git a/cmake/json.cmake b/cmake/json.cmake index 5713382e..1bc3a811 100644 --- a/cmake/json.cmake +++ b/cmake/json.cmake @@ -1,35 +1,51 @@ -# use our own CMake script to build jsonfortran instead of jsonfortran/CMakelists.txt - -FetchContent_Declare(jsonfortran - GIT_REPOSITORY https://github.com/jacobwilliams/json-fortran - GIT_TAG 8.3.0 - GIT_SHALLOW true -) - -FetchContent_Populate(jsonfortran) - -SET(JSON_REAL_KIND "REAL64") -SET(JSON_INT_KIND "INT32") - -set(_src ${jsonfortran_SOURCE_DIR}/src) - -set (JF_LIB_SRCS -${_src}/json_kinds.F90 -${_src}/json_parameters.F90 -${_src}/json_string_utilities.F90 -${_src}/json_value_module.F90 -${_src}/json_file_module.F90 -${_src}/json_module.F90 -) - -add_library(jsonfortran ${JF_LIB_SRCS}) -target_compile_definitions(jsonfortran PRIVATE ${JSON_REAL_KIND} ${JSON_INT_KIND}) -target_include_directories(jsonfortran PUBLIC -$ -$ -) - -add_library(jsonfortran::jsonfortran INTERFACE IMPORTED GLOBAL) -target_link_libraries(jsonfortran::jsonfortran INTERFACE jsonfortran) - -install(TARGETS jsonfortran) +# Per the jsonfortran README, if you build with their CMakeLists.txt, you will +# make a jsonfortran-${CMAKE_Fortran_COMPILER_ID} package, so you have to find_package +# on that. +find_package(jsonfortran-${CMAKE_Fortran_COMPILER_ID} 8.3.0 QUIET) + +if (NOT jsonfortran-${CMAKE_Fortran_COMPILER_ID}_FOUND) + message(STATUS "jsonfortran not found, fetching from github") + + # use our own CMake script to build jsonfortran instead of jsonfortran/CMakelists.txt + + FetchContent_Declare(jsonfortran + GIT_REPOSITORY https://github.com/jacobwilliams/json-fortran + GIT_TAG 8.3.0 + GIT_SHALLOW true + ) + + FetchContent_Populate(jsonfortran) + + SET(JSON_REAL_KIND "REAL64") + SET(JSON_INT_KIND "INT32") + + set(_src ${jsonfortran_SOURCE_DIR}/src) + + set (JF_LIB_SRCS + ${_src}/json_kinds.F90 + ${_src}/json_parameters.F90 + ${_src}/json_string_utilities.F90 + ${_src}/json_value_module.F90 + ${_src}/json_file_module.F90 + ${_src}/json_module.F90 + ) + + add_library(jsonfortran ${JF_LIB_SRCS}) + target_compile_definitions(jsonfortran PRIVATE ${JSON_REAL_KIND} ${JSON_INT_KIND}) + target_include_directories(jsonfortran PUBLIC + $ + $ + ) + + add_library(jsonfortran::jsonfortran INTERFACE IMPORTED GLOBAL) + target_link_libraries(jsonfortran::jsonfortran INTERFACE jsonfortran) + + install(TARGETS jsonfortran) +else () + message(STATUS "jsonfortran found: ${jsonfortran-${CMAKE_Fortran_COMPILER_ID}_DIR}") + # We need to make a jsonfortran::jsonfortran target for the jsonfortran we found because + # neural-fortran is expecting it. It looks like it is a lower-case version of the compiler + # name, so we'll just do that. + string(TOLOWER ${CMAKE_Fortran_COMPILER_ID} lower_compiler_id) + add_library(jsonfortran::jsonfortran ALIAS jsonfortran-${lower_compiler_id}::jsonfortran) +endif () From f209a3d3cb8afa816b758c69157ca4558a440eac Mon Sep 17 00:00:00 2001 From: Matthew Thompson Date: Wed, 3 May 2023 10:03:02 -0400 Subject: [PATCH 2/3] Remove some messages --- cmake/Findfunctional.cmake | 3 --- 1 file changed, 3 deletions(-) diff --git a/cmake/Findfunctional.cmake b/cmake/Findfunctional.cmake index 3663a9b9..583a5a5f 100644 --- a/cmake/Findfunctional.cmake +++ b/cmake/Findfunctional.cmake @@ -8,15 +8,12 @@ include(FindPackageHandleStandardArgs) find_library(functional_LIBRARY NAMES functional) -message(STATUS "functional_LIBRARY: ${functional_LIBRARY}") find_path(functional_INCLUDE_DIR NAMES functional.mod) -message(STATUS "functional_INCLUDE_DIR: ${functional_INCLUDE_DIR}") find_package_handle_standard_args(functional DEFAULT_MSG functional_LIBRARY functional_INCLUDE_DIR) -message(STATUS "functional_FOUND: ${functional_FOUND}") if (functional_FOUND) mark_as_advanced(functional_INCLUDE_DIR) From df45f7c9cce6bde87f7fa12e24e4858dddc72624 Mon Sep 17 00:00:00 2001 From: Matthew Thompson Date: Wed, 3 May 2023 10:39:11 -0400 Subject: [PATCH 3/3] Add find_package for MPI --- cmake/h5fortran.cmake | 1 + 1 file changed, 1 insertion(+) diff --git a/cmake/h5fortran.cmake b/cmake/h5fortran.cmake index b2c4baaa..df93655d 100644 --- a/cmake/h5fortran.cmake +++ b/cmake/h5fortran.cmake @@ -6,6 +6,7 @@ if (HDF5_HL_FOUND) endif() if (HDF5_IS_PARALLEL) message(STATUS "HDF5 is parallel") + find_package(MPI REQUIRED COMPONENTS Fortran) target_link_libraries(HDF5::HDF5 INTERFACE MPI::MPI_Fortran) endif()