Skip to content

Commit 412a4b5

Browse files
committed
fix compiler options and link to fetchcontent
1 parent daf9361 commit 412a4b5

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

CMakeLists.txt

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ if(NOT CMAKE_BUILD_TYPE)
66
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Default build Release")
77
endif()
88

9-
project(neural-fortran LANGUAGES Fortran)
9+
project(neural-fortran
10+
LANGUAGES C Fortran
11+
)
12+
13+
enable_testing()
1014

1115
option(SERIAL "Serial execution")
1216

@@ -22,11 +26,10 @@ else()
2226
message(STATUS "Configuring build for parallel execution")
2327
endif()
2428

25-
find_package(HDF5 COMPONENTS Fortran HL)
26-
include_directories(${HDF5_INCLUDE_DIRS})
27-
2829
include(FetchContent)
2930

31+
set(h5fortran_BUILD_TESTING false)
32+
3033
FetchContent_Declare(
3134
h5fortran
3235
GIT_REPOSITORY https://github.com/geospace-code/h5fortran
@@ -47,47 +50,55 @@ include_directories(${h5fortran_BINARY_DIR}/include)
4750
file(MAKE_DIRECTORY ${jsonfortran_BINARY_DIR}/include)
4851
include_directories(${jsonfortran_BINARY_DIR})
4952

53+
list(APPEND CMAKE_MODULE_PATH ${h5fortran_SOURCE_DIR}/cmake/Modules)
54+
find_package(HDF5 COMPONENTS Fortran REQUIRED)
55+
5056
# compiler flags for gfortran
51-
if(CMAKE_Fortran_COMPILER_ID MATCHES GNU)
57+
if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
5258

5359
if(SERIAL)
5460
message(STATUS "Configuring to build with -fcoarray=single")
55-
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fcoarray=single")
61+
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-fcoarray=single>")
62+
else()
63+
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-fcoarray=lib>")
5664
endif()
5765

5866
if(BLAS)
59-
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fexternal-blas ${BLAS}")
60-
set(LIBS "${LIBS} blas")
67+
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-fexternal-blas;${BLAS}>")
68+
list(APPEND LIBS "blas")
6169
message(STATUS "Configuring build to use BLAS from ${BLAS}")
6270
endif()
6371

64-
set(CMAKE_Fortran_FLAGS_DEBUG "-O0 -g -fcheck=bounds -fbacktrace")
65-
set(CMAKE_Fortran_FLAGS_RELEASE "-Ofast -fno-frontend-optimize")
66-
endif()
72+
add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:Fortran>,$<CONFIG:Debug>>:-fcheck=bounds;-fbacktrace>")
73+
add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:Fortran>,$<CONFIG:Release>>:-Ofast;-fno-frontend-optimize;-fno-backtrace>")
6774

68-
# compiler flags for ifort
69-
if(CMAKE_Fortran_COMPILER_ID MATCHES Intel)
75+
elseif(CMAKE_Fortran_COMPILER_ID MATCHES "^Intel")
76+
# compiler flags for ifort
7077

7178
if(SERIAL)
7279
message(STATUS "Configuring to build with -coarray=single")
73-
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -coarray=single")
80+
if(WIN32)
81+
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:/Qcoarray:single>")
82+
else()
83+
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-coarray=single>")
84+
endif()
85+
else()
86+
if(WIN32)
87+
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:/Qcoarray:shared>")
88+
else()
89+
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-coarray=shared>")
90+
endif()
7491
endif()
7592

76-
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -assume byterecl")
77-
set(CMAKE_Fortran_FLAGS_DEBUG "-O0 -g -C -traceback")
78-
set(CMAKE_Fortran_FLAGS_RELEASE "-O3")
93+
string(APPEND CMAKE_Fortran_FLAGS " -assume byterecl")
94+
add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:Fortran>,$<CONFIG:Debug>>:-check;-traceback>")
95+
# add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:Fortran>,$<CONFIG:Release>>:-O3>")
7996

80-
if(NOT SERIAL)
81-
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -coarray=shared")
82-
endif()
83-
84-
endif()
85-
86-
# compiler flags for Cray ftn
87-
if(CMAKE_Fortran_COMPILER_ID MATCHES Cray)
88-
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -h noomp")
89-
set(CMAKE_Fortran_FLAGS_DEBUG "-O0 -g")
90-
set(CMAKE_Fortran_FLAGS_RELEASE "-O3")
97+
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "Cray")
98+
# compiler flags for Cray ftn
99+
string(APPEND CMAKE_Fortran_FLAGS " -h noomp")
100+
add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:Fortran>,$<CONFIG:Debug>>:-O0;-g>")
101+
add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:Fortran>,$<CONFIG:Release>>:-O3>")
91102
endif()
92103

93104
# library to archive (libneural.a)
@@ -132,12 +143,14 @@ add_library(neural
132143
src/nf/io/nf_io_hdf5.f90
133144
src/nf/io/nf_io_hdf5_submodule.f90
134145
)
146+
target_link_libraries(neural PRIVATE HDF5::HDF5)
147+
target_include_directories(neural PRIVATE ${jsonfortran_BINARY_DIR}/include)
135148

136149
# Remove leading or trailing whitespace
137150
string(REGEX REPLACE "^ | $" "" LIBS "${LIBS}")
138151

139152
# tests
140-
enable_testing()
153+
141154
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)
142155
add_executable(test_${execid} test/test_${execid}.f90)
143156
target_link_libraries(test_${execid} PRIVATE neural h5fortran::h5fortran jsonfortran ${LIBS})

0 commit comments

Comments
 (0)