Skip to content

Commit d6753e8

Browse files
committed
build jsonfortran ourselves
1 parent db83a2c commit d6753e8

File tree

5 files changed

+123
-91
lines changed

5 files changed

+123
-91
lines changed

CMakeLists.txt

Lines changed: 9 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -12,96 +12,13 @@ LANGUAGES C Fortran
1212

1313
enable_testing()
1414

15-
option(SERIAL "Serial execution")
16-
17-
# Set output paths for modules, archives, and executables
18-
set(CMAKE_Fortran_MODULE_DIRECTORY ${PROJECT_BINARY_DIR}/include)
19-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
20-
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
21-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
22-
23-
if(SERIAL)
24-
message(STATUS "Configuring build for serial execution")
25-
else()
26-
message(STATUS "Configuring build for parallel execution")
27-
endif()
28-
2915
include(FetchContent)
3016

31-
set(h5fortran_BUILD_TESTING false)
32-
33-
FetchContent_Declare(
34-
h5fortran
35-
GIT_REPOSITORY https://github.com/geospace-code/h5fortran
36-
GIT_TAG v4.6.3
37-
GIT_SHALLOW true
38-
)
39-
40-
FetchContent_Declare(
41-
jsonfortran
42-
GIT_REPOSITORY https://github.com/jacobwilliams/json-fortran
43-
GIT_TAG 8.3.0
44-
GIT_SHALLOW true
45-
)
46-
47-
FetchContent_MakeAvailable(h5fortran jsonfortran)
48-
49-
file(MAKE_DIRECTORY ${h5fortran_BINARY_DIR}/include)
50-
include_directories(${h5fortran_BINARY_DIR}/include)
51-
52-
file(MAKE_DIRECTORY ${jsonfortran_BINARY_DIR}/include)
53-
include_directories(${jsonfortran_BINARY_DIR})
17+
include(cmake/options.cmake)
18+
include(cmake/compilers.cmake)
5419

55-
list(APPEND CMAKE_MODULE_PATH ${h5fortran_SOURCE_DIR}/cmake/Modules)
56-
find_package(HDF5 COMPONENTS Fortran REQUIRED)
57-
58-
# compiler flags for gfortran
59-
if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
60-
61-
if(SERIAL)
62-
message(STATUS "Configuring to build with -fcoarray=single")
63-
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-fcoarray=single>")
64-
else()
65-
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-fcoarray=lib>")
66-
endif()
67-
68-
if(BLAS)
69-
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-fexternal-blas;${BLAS}>")
70-
list(APPEND LIBS "blas")
71-
message(STATUS "Configuring build to use BLAS from ${BLAS}")
72-
endif()
73-
74-
add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:Fortran>,$<CONFIG:Debug>>:-fcheck=bounds;-fbacktrace>")
75-
add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:Fortran>,$<CONFIG:Release>>:-Ofast;-fno-frontend-optimize;-fno-backtrace>")
76-
77-
elseif(CMAKE_Fortran_COMPILER_ID MATCHES "^Intel")
78-
# compiler flags for ifort
79-
80-
if(SERIAL)
81-
message(STATUS "Configuring to build with -coarray=single")
82-
if(WIN32)
83-
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:/Qcoarray:single>")
84-
else()
85-
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-coarray=single>")
86-
endif()
87-
else()
88-
if(WIN32)
89-
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:/Qcoarray:shared>")
90-
else()
91-
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-coarray=shared>")
92-
endif()
93-
endif()
94-
95-
string(APPEND CMAKE_Fortran_FLAGS " -assume byterecl")
96-
add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:Fortran>,$<CONFIG:Debug>>:-check;-traceback>")
97-
# add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:Fortran>,$<CONFIG:Release>>:-O3>")
98-
99-
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "Cray")
100-
# compiler flags for Cray ftn
101-
string(APPEND CMAKE_Fortran_FLAGS " -h noomp")
102-
add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:Fortran>,$<CONFIG:Debug>>:-O0;-g>")
103-
add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:Fortran>,$<CONFIG:Release>>:-O3>")
104-
endif()
20+
include(cmake/h5fortran.cmake)
21+
include(cmake/json.cmake)
10522

10623
# library to archive (libneural.a)
10724
add_library(neural
@@ -145,8 +62,9 @@ add_library(neural
14562
src/nf/io/nf_io_hdf5.f90
14663
src/nf/io/nf_io_hdf5_submodule.f90
14764
)
148-
target_link_libraries(neural PRIVATE h5fortran::h5fortran HDF5::HDF5)
149-
target_include_directories(neural PRIVATE ${jsonfortran_BINARY_DIR}/include)
65+
target_link_libraries(neural PRIVATE h5fortran::h5fortran HDF5::HDF5 jsonfortran::jsonfortran)
66+
67+
install(TARGETS neural)
15068

15169
# Remove leading or trailing whitespace
15270
string(REGEX REPLACE "^ | $" "" LIBS "${LIBS}")
@@ -155,11 +73,11 @@ string(REGEX REPLACE "^ | $" "" LIBS "${LIBS}")
15573

15674
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)
15775
add_executable(test_${execid} test/test_${execid}.f90)
158-
target_link_libraries(test_${execid} PRIVATE neural h5fortran::h5fortran jsonfortran ${LIBS})
76+
target_link_libraries(test_${execid} PRIVATE neural h5fortran::h5fortran jsonfortran::jsonfortran ${LIBS})
15977
add_test(test_${execid} bin/test_${execid})
16078
endforeach()
16179

16280
foreach(execid cnn mnist mnist_from_keras simple sine)
16381
add_executable(${execid} example/${execid}.f90)
164-
target_link_libraries(${execid} PRIVATE neural h5fortran::h5fortran jsonfortran ${LIBS})
82+
target_link_libraries(${execid} PRIVATE neural h5fortran::h5fortran jsonfortran::jsonfortran ${LIBS})
16583
endforeach()

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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
option(SERIAL "Serial execution")
2+
3+
# Set output paths for modules, archives, and executables
4+
set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/include)
5+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
6+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
7+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
8+
9+
if(SERIAL)
10+
message(STATUS "Configuring build for serial execution")
11+
else()
12+
message(STATUS "Configuring build for parallel execution")
13+
endif()

0 commit comments

Comments
 (0)