Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ include(GNUInstallDirs)
# General configuration information
add_subdirectory("config")

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/config/cmake")

option(HT_HDF5_ENABLE "Enabling HDF5 support" OFF)

# Find h5fortran
if(HT_HDF5_ENABLE AND NOT TARGET "h5fortran::h5fortran")
find_package("h5fortran" REQUIRED)
endif()

# --- find preprocessor
find_program(FYPP fypp)
if(NOT FYPP)
Expand All @@ -49,6 +58,10 @@ list(
set(srcs)
add_subdirectory("src")

target_link_libraries("${PROJECT_NAME}-lib" PUBLIC
$<$<BOOL:${HT_HDF5_ENABLE}>:h5fortran::h5fortran>
)

# We need the module directory before we finish the configure stage
if(NOT EXISTS "${PROJECT_BINARY_DIR}/include")
make_directory("${PROJECT_BINARY_DIR}/include")
Expand All @@ -72,11 +85,16 @@ target_include_directories(
target_compile_definitions(
"${PROJECT_NAME}-lib"
PRIVATE
"$<$<BOOL:${HT_HDF5_ENABLE}>:_HDF5>"
)

# Export targets for other projects
add_library("${PROJECT_NAME}" INTERFACE)
target_link_libraries("${PROJECT_NAME}" INTERFACE "${PROJECT_NAME}-lib")

target_link_libraries("${PROJECT_NAME}" INTERFACE "${PROJECT_NAME}-lib"
# $<$<BOOL:${HT_HDF5_ENABLE}>:h5fortran::h5fortran>
)

install(
TARGETS
"${PROJECT_NAME}"
Expand Down
183 changes: 183 additions & 0 deletions config/cmake/Findh5fortran.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
# SPDX-Identifier: MIT

#[[.rst:
Find h5fortran
-------------------

Makes the Fortran standard library (stdlib) project available.

Imported Targets
^^^^^^^^^^^^^^^^

This module provides the following imported target, if found:

``h5fortran::h5fortran``
The Fortran standard library


Result Variables
^^^^^^^^^^^^^^^^

This module will define the following variables:

``H5FORTRAN_FOUND``
True if the Fortran standard library is available

``H5FORTRAN_SOURCE_DIR``
Path to the source directory of the Fortran standard library project,
only set if the project is included as source.

``H5FORTRAN_BINARY_DIR``
Path to the binary directory of the Fortran standard library project,
only set if the project is included as source.

Cache variables
^^^^^^^^^^^^^^^

The following cache variables may be set to influence the library detection:

``H5FORTRAN_FIND_METHOD``
Methods to find or make the project available. Available methods are
- ``cmake``: Try to find via CMake config file
- ``pkgconf``: Try to find via pkg-config file
- ``subproject``: Use source in subprojects directory
- ``fetch``: Fetch the source from upstream

``H5FORTRAN_DIR``
Used for searching the CMake config file

``H5FORTRAN_SUBPROJECT``
Directory to find the Fortran standard library subproject, relative to the project root

#]]

set(_lib "h5fortran")
set(_pkg "H5FORTRAN")
set(_url "https://github.com/geospace-code/h5fortran.git")

set(_tag "HEAD")

if(NOT DEFINED "${_pkg}_FIND_METHOD")
if(DEFINED "${PROJECT_NAME}-dependency-method")
set("${_pkg}_FIND_METHOD" "${${PROJECT_NAME}-dependency-method}")
else()
set("${_pkg}_FIND_METHOD" "cmake" "pkgconf" "subproject" "fetch")
endif()
set("_${_pkg}_FIND_METHOD")
endif()

foreach(method ${${_pkg}_FIND_METHOD})
if(TARGET "${_lib}::${_lib}")
break()
endif()

if("${method}" STREQUAL "cmake")
message(STATUS "${_lib}: Find installed package")
if(DEFINED "${_pkg}_DIR")
set("_${_pkg}_DIR")
set("${_lib}_DIR" "${_pkg}_DIR")
endif()
find_package("${_lib}" CONFIG)
if("${_lib}_FOUND")
message(STATUS "${_lib}: Found installed package")
break()
endif()
endif()

if("${method}" STREQUAL "pkgconf")
find_package(PkgConfig QUIET)
pkg_check_modules("${_pkg}" QUIET "${_lib}")
if("${_pkg}_FOUND")
message(STATUS "Found ${_lib} via pkg-config")

add_library("${_lib}::${_lib}" INTERFACE IMPORTED)
target_link_libraries(
"${_lib}::${_lib}"
INTERFACE
"${${_pkg}_LINK_LIBRARIES}"
)
target_include_directories(
"${_lib}::${_lib}"
INTERFACE
"${${_pkg}_INCLUDE_DIRS}"
)

break()
endif()
endif()

if("${method}" STREQUAL "subproject")
if(NOT DEFINED "${_pkg}_SUBPROJECT")
set("_${_pkg}_SUBPROJECT")
set("${_pkg}_SUBPROJECT" "subprojects/${_lib}")
endif()
set("${_pkg}_SOURCE_DIR" "${PROJECT_SOURCE_DIR}/${${_pkg}_SUBPROJECT}")
set("${_pkg}_BINARY_DIR" "${PROJECT_BINARY_DIR}/${${_pkg}_SUBPROJECT}")
if(EXISTS "${${_pkg}_SOURCE_DIR}/CMakeLists.txt")
message(STATUS "Include ${_lib} from ${${_pkg}_SUBPROJECT}")
add_subdirectory(
"${${_pkg}_SOURCE_DIR}"
"${${_pkg}_BINARY_DIR}"
)

add_library("${_lib}::${_lib}" INTERFACE IMPORTED)
target_link_libraries("${_lib}::${_lib}" INTERFACE "${_lib}")

# We need the module directory in the subproject before we finish the configure stage
if(NOT EXISTS "${${_pkg}_BINARY_DIR}/mod_files")
make_directory("${${_pkg}_BINARY_DIR}/mod_files")
endif()

break()
endif()
endif()

if("${method}" STREQUAL "fetch")
message(STATUS "Retrieving ${_lib} from ${_url}")
include(FetchContent)
FetchContent_Declare(
"${_lib}"
GIT_REPOSITORY "${_url}"
GIT_TAG "${_tag}"
# TLS_VERIFY true
# FIND_PACKAGE_ARGS
)
FetchContent_MakeAvailable("${_lib}")

# add_library("${_lib}::${_lib}" INTERFACE IMPORTED)
# target_link_libraries("${_lib}::${_lib}" INTERFACE "${_lib}")

# We need the module directory in the subproject before we finish the configure stage
FetchContent_GetProperties("${_lib}" SOURCE_DIR "${_pkg}_SOURCE_DIR")
FetchContent_GetProperties("${_lib}" BINARY_DIR "${_pkg}_BINARY_DIR")
if(NOT EXISTS "${${_pkg}_BINARY_DIR}/mod_files")
make_directory("${${_pkg}_BINARY_DIR}/mod_files")
endif()

break()
endif()

endforeach()

if(TARGET "${_lib}::${_lib}")
set("${_pkg}_FOUND" TRUE)
else()
set("${_pkg}_FOUND" FALSE)
endif()

if(DEFINED "_${_pkg}_SUBPROJECT")
unset("${_pkg}_SUBPROJECT")
unset("_${_pkg}_SUBPROJECT")
endif()
if(DEFINED "_${_pkg}_DIR")
unset("${_lib}_DIR")
unset("_${_pkg}_DIR")
endif()
if(DEFINED "_${_pkg}_FIND_METHOD")
unset("${_pkg}_FIND_METHOD")
unset("_${_pkg}_FIND_METHOD")
endif()
unset(_lib)
unset(_pkg)
unset(_url)
unset(_tag)
Loading