Skip to content

Commit b07c51d

Browse files
committed
autobuild HDF5 if needed
add cmake -Dfind option to disable finding HDF5 particularly useful for testing HDF5 autobuild
1 parent 61d11fd commit b07c51d

File tree

6 files changed

+65
-26
lines changed

6 files changed

+65
-26
lines changed

CMakeLists.txt

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,16 @@ include(options.cmake)
3535
include(cmake/compilers.cmake)
3636
include(cmake/CheckHDF5.cmake)
3737

38-
find_package(HDF5 COMPONENTS Fortran)
39-
if(NOT HDF5_FOUND)
40-
message(FATAL_ERROR "HDF5 not found or working on the system. First build HDF5 by:
41-
42-
cmake -S ${CMAKE_CURRENT_SOURCE_DIR}/scripts -B /tmp/build_hdf5 -DCMAKE_INSTALL_PREFIX=~/hdf5
43-
cmake --build /tmp/build_hdf5
44-
45-
The '~/hdf5' is an arbitrary directory.
46-
Then build h5fortran, referring to this HDF5 installation:
47-
48-
cmake -S ${CMAKE_CURRENT_SOURCE_DIR} -B ${PROJECT_BINARY_DIR} -DCMAKE_PREFIX_PATH=~/hdf5
49-
cmake --build ${PROJECT_BINARY_DIR}
50-
")
38+
if(find)
39+
find_package(HDF5 COMPONENTS Fortran)
5140
endif()
5241

53-
if(HDF5_VERSION VERSION_LESS 1.8.7)
54-
message(WARNING "HDF5 >= 1.8.7 required for ${PROJECT_NAME}")
42+
if(HDF5_FOUND)
43+
check_hdf5()
44+
else()
45+
include(cmake/hdf5.cmake)
5546
endif()
5647

57-
check_hdf5()
58-
5948
if(hdf5_parallel OR HDF5_HAVE_PARALLEL)
6049
target_link_libraries(HDF5::HDF5 INTERFACE MPI::MPI_Fortran)
6150
endif()

CMakePresets.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"configurePresets": [
55
{
66
"name": "default",
7-
"generator": "Ninja",
87
"binaryDir": "${sourceDir}/build",
98
"cacheVariables": {
109
"CMAKE_COMPILE_WARNING_AS_ERROR": true

cmake/hdf5.cmake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ endforeach()
2828

2929
set(HDF5_INCLUDE_DIRS ${CMAKE_INSTALL_FULL_INCLUDEDIR})
3030

31+
file(READ ${CMAKE_CURRENT_LIST_DIR}/libraries.json json)
32+
3133
# --- Zlib
3234
if(NOT TARGET ZLIB::ZLIB)
3335
include(${CMAKE_CURRENT_LIST_DIR}/zlib.cmake)
@@ -79,6 +81,13 @@ BUILD_BYPRODUCTS ${HDF5_LIBRARIES}
7981
DEPENDS ZLIB
8082
CONFIGURE_HANDLED_BY_BUILD ON
8183
INACTIVITY_TIMEOUT 60
84+
USES_TERMINAL_DOWNLOAD true
85+
USES_TERMINAL_UPDATE true
86+
USES_TERMINAL_PATCH true
87+
USES_TERMINAL_CONFIGURE true
88+
USES_TERMINAL_BUILD true
89+
USES_TERMINAL_INSTALL true
90+
USES_TERMINAL_TEST true
8291
)
8392

8493
# --- imported target

cmake/zlib.cmake

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ else()
1111
string(JSON zlib_tag GET ${json} zlib2 tag)
1212
endif()
1313

14+
set(ZLIB_INCLUDE_DIRS ${CMAKE_INSTALL_FULL_INCLUDEDIR})
15+
16+
if(BUILD_SHARED_LIBS)
17+
if(WIN32)
18+
set(ZLIB_LIBRARIES ${CMAKE_INSTALL_FULL_BINDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}zlib1${CMAKE_SHARED_LIBRARY_SUFFIX})
19+
else()
20+
set(ZLIB_LIBRARIES ${CMAKE_INSTALL_FULL_LIBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}z${CMAKE_SHARED_LIBRARY_SUFFIX})
21+
endif()
22+
else()
23+
if(MSVC OR (WIN32 AND zlib_legacy))
24+
set(ZLIB_LIBRARIES ${CMAKE_INSTALL_FULL_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}zlibstatic${CMAKE_STATIC_LIBRARY_SUFFIX})
25+
else()
26+
set(ZLIB_LIBRARIES ${CMAKE_INSTALL_FULL_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}z${CMAKE_STATIC_LIBRARY_SUFFIX})
27+
endif()
28+
endif()
29+
30+
1431
set(zlib_cmake_args
1532
-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX}
1633
-DBUILD_SHARED_LIBS:BOOL=${BUILD_SHARED_LIBS}
@@ -23,20 +40,39 @@ set(zlib_cmake_args
2340
# NetCDF 4.9/4.6 needs fPIC
2441

2542
if(zlib_legacy)
26-
ExternalProject_Add(ZLIB
43+
set(zlib_download
2744
URL ${zlib_url}
2845
URL_HASH SHA256=${zlib_sha256}
29-
CMAKE_ARGS ${zlib_cmake_args}
30-
CONFIGURE_HANDLED_BY_BUILD ON
31-
INACTIVITY_TIMEOUT 60
3246
)
3347
else()
34-
ExternalProject_Add(ZLIB
48+
set(zlib_download
3549
GIT_REPOSITORY ${zlib_url}
3650
GIT_TAG ${zlib_tag}
3751
GIT_SHALLOW true
52+
)
53+
endif()
54+
55+
ExternalProject_Add(ZLIB
56+
${zlib_download}
3857
CMAKE_ARGS ${zlib_cmake_args}
58+
BUILD_BYPRODUCTS ${ZLIB_LIBRARIES}
3959
CONFIGURE_HANDLED_BY_BUILD ON
4060
INACTIVITY_TIMEOUT 60
61+
USES_TERMINAL_DOWNLOAD true
62+
USES_TERMINAL_UPDATE true
63+
USES_TERMINAL_PATCH true
64+
USES_TERMINAL_CONFIGURE true
65+
USES_TERMINAL_BUILD true
66+
USES_TERMINAL_INSTALL true
67+
USES_TERMINAL_TEST true
4168
)
42-
endif()
69+
70+
# --- imported target
71+
72+
file(MAKE_DIRECTORY ${ZLIB_INCLUDE_DIRS})
73+
# avoid race condition
74+
75+
add_library(ZLIB::ZLIB INTERFACE IMPORTED GLOBAL)
76+
add_dependencies(ZLIB::ZLIB ZLIB) # to avoid include directory race condition
77+
target_link_libraries(ZLIB::ZLIB INTERFACE ${ZLIB_LIBRARIES})
78+
target_include_directories(ZLIB::ZLIB INTERFACE ${ZLIB_INCLUDE_DIRS})

options.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ message(STATUS "${PROJECT_NAME} ${PROJECT_VERSION} CMake ${CMAKE_VERSION} Toolch
22

33
include(GNUInstallDirs)
44

5+
option(find "try to find libraries" on)
6+
57
option(ENABLE_COVERAGE "Code coverage tests")
68
option(tidy "Run clang-tidy on the code")
79

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

20+
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
21+
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/local" CACHE PATH "path to install" FORCE)
22+
endif()
23+
24+
set_property(DIRECTORY PROPERTY EP_UPDATE_DISCONNECTED true)
25+
1826
# Necessary for shared library with Visual Studio / Windows oneAPI
1927
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS true)
2028

scripts/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ if(_hdf5_libprior OR _hdf5_incprior OR _hdf5_binprior)
5656
endif()
5757

5858
# --- commence HDF5 build/install
59-
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/../cmake/libraries.json json)
60-
6159
set_property(DIRECTORY PROPERTY EP_UPDATE_DISCONNECTED true)
6260

6361
if(hdf5_parallel)

0 commit comments

Comments
 (0)