Skip to content

Commit

Permalink
autobuild HDF5 if needed
Browse files Browse the repository at this point in the history
add cmake -Dfind option to disable finding HDF5

particularly useful for testing HDF5 autobuild
  • Loading branch information
scivision committed Oct 23, 2023
1 parent 61d11fd commit b07c51d
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 26 deletions.
23 changes: 6 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,16 @@ include(options.cmake)
include(cmake/compilers.cmake)
include(cmake/CheckHDF5.cmake)

find_package(HDF5 COMPONENTS Fortran)
if(NOT HDF5_FOUND)
message(FATAL_ERROR "HDF5 not found or working on the system. First build HDF5 by:
cmake -S ${CMAKE_CURRENT_SOURCE_DIR}/scripts -B /tmp/build_hdf5 -DCMAKE_INSTALL_PREFIX=~/hdf5
cmake --build /tmp/build_hdf5
The '~/hdf5' is an arbitrary directory.
Then build h5fortran, referring to this HDF5 installation:
cmake -S ${CMAKE_CURRENT_SOURCE_DIR} -B ${PROJECT_BINARY_DIR} -DCMAKE_PREFIX_PATH=~/hdf5
cmake --build ${PROJECT_BINARY_DIR}
")
if(find)
find_package(HDF5 COMPONENTS Fortran)
endif()

if(HDF5_VERSION VERSION_LESS 1.8.7)
message(WARNING "HDF5 >= 1.8.7 required for ${PROJECT_NAME}")
if(HDF5_FOUND)
check_hdf5()
else()
include(cmake/hdf5.cmake)
endif()

check_hdf5()

if(hdf5_parallel OR HDF5_HAVE_PARALLEL)
target_link_libraries(HDF5::HDF5 INTERFACE MPI::MPI_Fortran)
endif()
Expand Down
1 change: 0 additions & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"configurePresets": [
{
"name": "default",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"CMAKE_COMPILE_WARNING_AS_ERROR": true
Expand Down
9 changes: 9 additions & 0 deletions cmake/hdf5.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ endforeach()

set(HDF5_INCLUDE_DIRS ${CMAKE_INSTALL_FULL_INCLUDEDIR})

file(READ ${CMAKE_CURRENT_LIST_DIR}/libraries.json json)

# --- Zlib
if(NOT TARGET ZLIB::ZLIB)
include(${CMAKE_CURRENT_LIST_DIR}/zlib.cmake)
Expand Down Expand Up @@ -79,6 +81,13 @@ BUILD_BYPRODUCTS ${HDF5_LIBRARIES}
DEPENDS ZLIB
CONFIGURE_HANDLED_BY_BUILD ON
INACTIVITY_TIMEOUT 60
USES_TERMINAL_DOWNLOAD true
USES_TERMINAL_UPDATE true
USES_TERMINAL_PATCH true
USES_TERMINAL_CONFIGURE true
USES_TERMINAL_BUILD true
USES_TERMINAL_INSTALL true
USES_TERMINAL_TEST true
)

# --- imported target
Expand Down
48 changes: 42 additions & 6 deletions cmake/zlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ else()
string(JSON zlib_tag GET ${json} zlib2 tag)
endif()

set(ZLIB_INCLUDE_DIRS ${CMAKE_INSTALL_FULL_INCLUDEDIR})

if(BUILD_SHARED_LIBS)
if(WIN32)
set(ZLIB_LIBRARIES ${CMAKE_INSTALL_FULL_BINDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}zlib1${CMAKE_SHARED_LIBRARY_SUFFIX})
else()
set(ZLIB_LIBRARIES ${CMAKE_INSTALL_FULL_LIBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}z${CMAKE_SHARED_LIBRARY_SUFFIX})
endif()
else()
if(MSVC OR (WIN32 AND zlib_legacy))
set(ZLIB_LIBRARIES ${CMAKE_INSTALL_FULL_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}zlibstatic${CMAKE_STATIC_LIBRARY_SUFFIX})
else()
set(ZLIB_LIBRARIES ${CMAKE_INSTALL_FULL_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}z${CMAKE_STATIC_LIBRARY_SUFFIX})
endif()
endif()


set(zlib_cmake_args
-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX}
-DBUILD_SHARED_LIBS:BOOL=${BUILD_SHARED_LIBS}
Expand All @@ -23,20 +40,39 @@ set(zlib_cmake_args
# NetCDF 4.9/4.6 needs fPIC

if(zlib_legacy)
ExternalProject_Add(ZLIB
set(zlib_download
URL ${zlib_url}
URL_HASH SHA256=${zlib_sha256}
CMAKE_ARGS ${zlib_cmake_args}
CONFIGURE_HANDLED_BY_BUILD ON
INACTIVITY_TIMEOUT 60
)
else()
ExternalProject_Add(ZLIB
set(zlib_download
GIT_REPOSITORY ${zlib_url}
GIT_TAG ${zlib_tag}
GIT_SHALLOW true
)
endif()

ExternalProject_Add(ZLIB
${zlib_download}
CMAKE_ARGS ${zlib_cmake_args}
BUILD_BYPRODUCTS ${ZLIB_LIBRARIES}
CONFIGURE_HANDLED_BY_BUILD ON
INACTIVITY_TIMEOUT 60
USES_TERMINAL_DOWNLOAD true
USES_TERMINAL_UPDATE true
USES_TERMINAL_PATCH true
USES_TERMINAL_CONFIGURE true
USES_TERMINAL_BUILD true
USES_TERMINAL_INSTALL true
USES_TERMINAL_TEST true
)
endif()

# --- imported target

file(MAKE_DIRECTORY ${ZLIB_INCLUDE_DIRS})
# avoid race condition

add_library(ZLIB::ZLIB INTERFACE IMPORTED GLOBAL)
add_dependencies(ZLIB::ZLIB ZLIB) # to avoid include directory race condition
target_link_libraries(ZLIB::ZLIB INTERFACE ${ZLIB_LIBRARIES})
target_include_directories(ZLIB::ZLIB INTERFACE ${ZLIB_INCLUDE_DIRS})
8 changes: 8 additions & 0 deletions options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ message(STATUS "${PROJECT_NAME} ${PROJECT_VERSION} CMake ${CMAKE_VERSION} Toolch

include(GNUInstallDirs)

option(find "try to find libraries" on)

option(ENABLE_COVERAGE "Code coverage tests")
option(tidy "Run clang-tidy on the code")

Expand All @@ -15,6 +17,12 @@ if(BUILD_SHARED_LIBS AND MSVC)
cmake -DBUILD_SHARED_LIBS=off")
endif()

if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/local" CACHE PATH "path to install" FORCE)
endif()

set_property(DIRECTORY PROPERTY EP_UPDATE_DISCONNECTED true)

# Necessary for shared library with Visual Studio / Windows oneAPI
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS true)

Expand Down
2 changes: 0 additions & 2 deletions scripts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ if(_hdf5_libprior OR _hdf5_incprior OR _hdf5_binprior)
endif()

# --- commence HDF5 build/install
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/../cmake/libraries.json json)

set_property(DIRECTORY PROPERTY EP_UPDATE_DISCONNECTED true)

if(hdf5_parallel)
Expand Down

0 comments on commit b07c51d

Please sign in to comment.