Skip to content

Commit b923736

Browse files
authored
Merge pull request #1 from scivision/hdf5-reader
Fix CMake
2 parents 5af4651 + 7ae9b08 commit b923736

File tree

7 files changed

+164
-88
lines changed

7 files changed

+164
-88
lines changed

CMakeLists.txt

Lines changed: 19 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,24 @@
11
# CMake version, project name, language
22
cmake_minimum_required(VERSION 3.20)
3-
project(neural-fortran Fortran)
4-
5-
# Set output paths for modules, archives, and executables
6-
set(CMAKE_Fortran_MODULE_DIRECTORY ${PROJECT_BINARY_DIR}/include)
7-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
8-
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
9-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
103

114
# If build type not specified, default to release
125
if(NOT CMAKE_BUILD_TYPE)
13-
set(CMAKE_BUILD_TYPE "release")
14-
endif()
15-
16-
if(SERIAL)
17-
message(STATUS "Configuring build for serial execution")
18-
else()
19-
message(STATUS "Configuring build for parallel execution")
6+
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Default build Release")
207
endif()
218

22-
find_package(HDF5 COMPONENTS Fortran HL)
23-
include_directories(${HDF5_INCLUDE_DIRS})
24-
25-
include(FetchContent)
26-
27-
FetchContent_Declare(
28-
h5fortran
29-
GIT_REPOSITORY https://github.com/geospace-code/h5fortran
30-
GIT_TAG v4.6.3
9+
project(neural-fortran
10+
LANGUAGES C Fortran
3111
)
3212

33-
FetchContent_Declare(
34-
jsonfortran
35-
GIT_REPOSITORY https://github.com/jacobwilliams/json-fortran
36-
GIT_TAG 8.3.0
37-
)
38-
39-
FetchContent_MakeAvailable(h5fortran jsonfortran)
40-
41-
file(MAKE_DIRECTORY ${h5fortran_BINARY_DIR}/include)
42-
include_directories(${h5fortran_BINARY_DIR}/include)
43-
44-
file(MAKE_DIRECTORY ${jsonfortran_BINARY_DIR}/include)
45-
include_directories(${jsonfortran_BINARY_DIR})
46-
47-
# compiler flags for gfortran
48-
if(CMAKE_Fortran_COMPILER_ID MATCHES GNU)
49-
50-
if(SERIAL)
51-
message(STATUS "Configuring to build with -fcoarray=single")
52-
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fcoarray=single")
53-
endif()
54-
55-
if(BLAS)
56-
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fexternal-blas ${BLAS}")
57-
set(LIBS "${LIBS} blas")
58-
message(STATUS "Configuring build to use BLAS from ${BLAS}")
59-
endif()
60-
61-
set(CMAKE_Fortran_FLAGS_DEBUG "-O0 -g -fcheck=bounds -fbacktrace")
62-
set(CMAKE_Fortran_FLAGS_RELEASE "-Ofast -fno-frontend-optimize")
63-
endif()
64-
65-
# compiler flags for ifort
66-
if(CMAKE_Fortran_COMPILER_ID MATCHES Intel)
67-
68-
if(SERIAL)
69-
message(STATUS "Configuring to build with -coarray=single")
70-
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -coarray=single")
71-
endif()
13+
enable_testing()
7214

73-
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -assume byterecl")
74-
set(CMAKE_Fortran_FLAGS_DEBUG "-O0 -g -C -traceback")
75-
set(CMAKE_Fortran_FLAGS_RELEASE "-O3")
15+
include(FetchContent)
7616

77-
if(NOT SERIAL)
78-
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -coarray=shared")
79-
endif()
17+
include(cmake/options.cmake)
18+
include(cmake/compilers.cmake)
8019

81-
endif()
82-
83-
# compiler flags for Cray ftn
84-
if(CMAKE_Fortran_COMPILER_ID MATCHES Cray)
85-
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -h noomp")
86-
set(CMAKE_Fortran_FLAGS_DEBUG "-O0 -g")
87-
set(CMAKE_Fortran_FLAGS_RELEASE "-O3")
88-
endif()
20+
include(cmake/h5fortran.cmake)
21+
include(cmake/json.cmake)
8922

9023
# library to archive (libneural.a)
9124
add_library(neural
@@ -129,19 +62,17 @@ add_library(neural
12962
src/nf/io/nf_io_hdf5.f90
13063
src/nf/io/nf_io_hdf5_submodule.f90
13164
)
65+
target_link_libraries(neural PRIVATE h5fortran::h5fortran HDF5::HDF5 jsonfortran::jsonfortran)
66+
67+
install(TARGETS neural)
13268

13369
# Remove leading or trailing whitespace
13470
string(REGEX REPLACE "^ | $" "" LIBS "${LIBS}")
13571

136-
# tests
137-
enable_testing()
138-
foreach(execid input1d_layer input3d_layer dense_layer conv2d_layer maxpool2d_layer flatten_layer dense_network dense_network_from_keras conv2d_network io_hdf5 keras_read_model)
139-
add_executable(test_${execid} test/test_${execid}.f90)
140-
target_link_libraries(test_${execid} PRIVATE neural h5fortran::h5fortran jsonfortran ${LIBS})
141-
add_test(test_${execid} bin/test_${execid})
142-
endforeach()
143-
144-
foreach(execid cnn mnist mnist_from_keras simple sine)
145-
add_executable(${execid} example/${execid}.f90)
146-
target_link_libraries(${execid} PRIVATE neural h5fortran::h5fortran jsonfortran ${LIBS})
147-
endforeach()
72+
if(${PROJECT_NAME}_BUILD_TESTING)
73+
add_subdirectory(test)
74+
endif()
75+
76+
if(${PROJECT_NAME}_BUILD_EXAMPLES)
77+
add_subdirectory(example)
78+
endif()

cmake/compilers.cmake

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# compiler flags for gfortran
2+
if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
3+
4+
if(SERIAL)
5+
message(STATUS "Configuring to build with -fcoarray=single")
6+
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-fcoarray=single>")
7+
else()
8+
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-fcoarray=lib>")
9+
endif()
10+
11+
if(BLAS)
12+
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-fexternal-blas;${BLAS}>")
13+
list(APPEND LIBS "blas")
14+
message(STATUS "Configuring build to use BLAS from ${BLAS}")
15+
endif()
16+
17+
add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:Fortran>,$<CONFIG:Debug>>:-fcheck=bounds;-fbacktrace>")
18+
add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:Fortran>,$<CONFIG:Release>>:-Ofast;-fno-frontend-optimize;-fno-backtrace>")
19+
20+
elseif(CMAKE_Fortran_COMPILER_ID MATCHES "^Intel")
21+
# compiler flags for ifort
22+
23+
if(SERIAL)
24+
message(STATUS "Configuring to build with -coarray=single")
25+
if(WIN32)
26+
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:/Qcoarray:single>")
27+
else()
28+
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-coarray=single>")
29+
endif()
30+
else()
31+
if(WIN32)
32+
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:/Qcoarray:shared>")
33+
else()
34+
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-coarray=shared>")
35+
endif()
36+
endif()
37+
38+
if(WIN32)
39+
string(APPEND CMAKE_Fortran_FLAGS " /assume:byterecl")
40+
else()
41+
string(APPEND CMAKE_Fortran_FLAGS " -assume byterecl")
42+
endif()
43+
add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:Fortran>,$<CONFIG:Debug>>:-check;-traceback>")
44+
# add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:Fortran>,$<CONFIG:Release>>:-O3>")
45+
46+
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "Cray")
47+
# compiler flags for Cray ftn
48+
string(APPEND CMAKE_Fortran_FLAGS " -h noomp")
49+
add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:Fortran>,$<CONFIG:Debug>>:-O0;-g>")
50+
add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:Fortran>,$<CONFIG:Release>>:-O3>")
51+
endif()

cmake/h5fortran.cmake

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
set(h5fortran_BUILD_TESTING false)
2+
3+
FetchContent_Declare(h5fortran
4+
GIT_REPOSITORY https://github.com/geospace-code/h5fortran
5+
GIT_TAG v4.6.3
6+
GIT_SHALLOW true
7+
)
8+
9+
FetchContent_MakeAvailable(h5fortran)
10+
11+
file(MAKE_DIRECTORY ${h5fortran_BINARY_DIR}/include)
12+
13+
14+
list(APPEND CMAKE_MODULE_PATH ${h5fortran_SOURCE_DIR}/cmake/Modules)
15+
find_package(HDF5 COMPONENTS Fortran REQUIRED)

cmake/json.cmake

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# use our own CMake script to build jsonfortran instead of jsonfortran/CMakelists.txt
2+
3+
FetchContent_Declare(jsonfortran
4+
GIT_REPOSITORY https://github.com/jacobwilliams/json-fortran
5+
GIT_TAG 8.3.0
6+
GIT_SHALLOW true
7+
)
8+
9+
FetchContent_Populate(jsonfortran)
10+
11+
SET(JSON_REAL_KIND "REAL64")
12+
SET(JSON_INT_KIND "INT32")
13+
14+
set(_src ${jsonfortran_SOURCE_DIR}/src)
15+
16+
set (JF_LIB_SRCS
17+
${_src}/json_kinds.F90
18+
${_src}/json_parameters.F90
19+
${_src}/json_string_utilities.F90
20+
${_src}/json_value_module.F90
21+
${_src}/json_file_module.F90
22+
${_src}/json_module.F90
23+
)
24+
25+
add_library(jsonfortran ${JF_LIB_SRCS})
26+
target_compile_definitions(jsonfortran PRIVATE ${JSON_REAL_KIND} ${JSON_INT_KIND})
27+
target_include_directories(jsonfortran PUBLIC
28+
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include>
29+
$<INSTALL_INTERFACE:include>
30+
)
31+
32+
add_library(jsonfortran::jsonfortran INTERFACE IMPORTED GLOBAL)
33+
target_link_libraries(jsonfortran::jsonfortran INTERFACE jsonfortran)
34+
35+
install(TARGETS jsonfortran)

cmake/options.cmake

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
option(SERIAL "Serial execution")
2+
option(${PROJECT_NAME}_BUILD_TESTING "build ${PROJECT_NAME} tests" true)
3+
option(${PROJECT_NAME}_BUILD_EXAMPLES "build ${PROJECT_NAME} examples" true)
4+
5+
# Set output paths for modules, archives, and executables
6+
set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/include)
7+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
8+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
9+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
10+
11+
if(SERIAL)
12+
message(STATUS "Configuring build for serial execution")
13+
else()
14+
message(STATUS "Configuring build for parallel execution")
15+
endif()
16+
17+
# --- Generally useful CMake project options
18+
19+
# Rpath options necessary for shared library install to work correctly in user projects
20+
set(CMAKE_INSTALL_NAME_DIR ${CMAKE_INSTALL_PREFIX}/lib)
21+
set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib)
22+
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH true)
23+
24+
# Necessary for shared library with Visual Studio / Windows oneAPI
25+
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS true)
26+
27+
# --- auto-ignore build directory
28+
if(NOT EXISTS ${PROJECT_BINARY_DIR}/.gitignore)
29+
file(WRITE ${PROJECT_BINARY_DIR}/.gitignore "*")
30+
endif()

example/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
foreach(execid cnn mnist mnist_from_keras simple sine)
2+
add_executable(${execid} ${execid}.f90)
3+
target_link_libraries(${execid} PRIVATE neural h5fortran::h5fortran jsonfortran::jsonfortran ${LIBS})
4+
endforeach()

test/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
foreach(execid input1d_layer input3d_layer dense_layer conv2d_layer maxpool2d_layer flatten_layer dense_network dense_network_from_keras conv2d_network io_hdf5 keras_read_model)
2+
add_executable(test_${execid} test_${execid}.f90)
3+
target_link_libraries(test_${execid} PRIVATE neural h5fortran::h5fortran jsonfortran::jsonfortran ${LIBS})
4+
5+
add_test(NAME test_${execid} COMMAND test_${execid})
6+
endforeach()
7+
8+
set_tests_properties(test_dense_network_from_keras test_io_hdf5 test_keras_read_model PROPERTIES
9+
RESOURCE_LOCK download
10+
)

0 commit comments

Comments
 (0)