Skip to content

Commit 5ce625b

Browse files
committed
update like h5fortran
1 parent 72f2b90 commit 5ce625b

File tree

10 files changed

+165
-77
lines changed

10 files changed

+165
-77
lines changed

CMakeLists.txt

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,31 @@
1-
cmake_minimum_required(VERSION 3.14)
1+
cmake_minimum_required(VERSION 3.15)
22

3-
if(NOT CMAKE_BUILD_TYPE)
4-
set(CMAKE_BUILD_TYPE Release CACHE STRING "Debug or Release")
5-
endif()
3+
include(cmake/compiler_find.cmake)
64

75
project(nc4fortran
8-
LANGUAGES Fortran
9-
VERSION 1.0.2
6+
LANGUAGES C Fortran
7+
VERSION 1.1.0
108
DESCRIPTION "thin, light object-oriented NetCDF4 Fortran interface"
119
HOMEPAGE_URL https://github.com/geospace-code/nc4fortran)
1210

1311
include(CTest)
1412

1513
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules)
1614

17-
# this helps linters e.g. Visual Studio Intellicode work properly
18-
set(CMAKE_EXPORT_COMPILE_COMMANDS on)
15+
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/policy.cmake)
16+
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/compilers.cmake)
1917

18+
# --- find NetCDF4
2019

21-
if(NOT DEFINED ${PROJECT_NAME}_BUILD_TESTING)
22-
set(${PROJECT_NAME}_BUILD_TESTING ${BUILD_TESTING})
20+
if(PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
21+
find_package(NetCDF COMPONENTS Fortran REQUIRED)
22+
else()
23+
find_package(NetCDF COMPONENTS Fortran)
2324
endif()
2425

25-
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/compilers.cmake)
26-
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/netcdf.cmake)
27-
28-
if(NOT NCDFOK)
29-
message(STATUS "nc4fortran: NetCDF4 not working")
30-
if(PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
31-
message(FATAL_ERROR)
32-
else()
33-
return()
34-
endif()
26+
if(NOT NetCDF_FOUND)
27+
message(STATUS "nc4fortran: NetCDF4 not working, so nc4fortran is not available.")
28+
return()
3529
endif()
3630

3731
# --- build

Examples/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ else()
1111
include(FetchContent)
1212
FetchContent_Declare(nc4fortran_proj
1313
GIT_REPOSITORY https://github.com/geospace-code/nc4fortran.git
14-
GIT_TAG v1.0.2)
14+
GIT_TAG master)
1515
FetchContent_MakeAvailable(nc4fortran_proj)
1616
endif()
1717

18-
if(NOT NCDFOK)
18+
if(NOT TARGET nc4fortran::nc4fortran)
1919
message(FATAL_ERROR "NetCDF4 is not working on your system, so nc4fortran cannot work.")
2020
endif()
2121

cmake/Modules/FindNetCDF.cmake

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,25 @@ if(NOT NetCDF_C_LIBRARY)
6262
return()
6363
endif()
6464

65+
set(CMAKE_REQUIRED_FLAGS)
66+
set(CMAKE_REQUIRED_INCLUDES ${NetCDF_C_INCLUDE_DIR})
67+
set(CMAKE_REQUIRED_LIBRARIES ${NetCDF_C_LIBRARY})
68+
69+
include(CheckCSourceCompiles)
70+
check_c_source_compiles("
71+
#include <netcdf.h>
72+
#include <stdio.h>
73+
74+
int main(void){
75+
printf(\"%s\", nc_inq_libvers());
76+
return 0;
77+
}
78+
" NetCDF_C_links)
79+
80+
if(NOT NetCDF_C_links)
81+
return()
82+
endif()
83+
6584
set(NetCDF_C_FOUND true PARENT_SCOPE)
6685
set(NetCDF_C_INCLUDE_DIR ${NetCDF_C_INCLUDE_DIR} PARENT_SCOPE)
6786
set(NetCDF_C_LIBRARY ${NetCDF_C_LIBRARY} PARENT_SCOPE)
@@ -95,6 +114,17 @@ if(NOT NetCDF_Fortran_LIBRARY)
95114
return()
96115
endif()
97116

117+
set(CMAKE_REQUIRED_FLAGS)
118+
set(CMAKE_REQUIRED_INCLUDES ${NetCDF_Fortran_INCLUDE_DIR})
119+
set(CMAKE_REQUIRED_LIBRARIES ${NetCDF_Fortran_LIBRARY})
120+
121+
include(CheckFortranSourceCompiles)
122+
check_fortran_source_compiles("use netcdf; end" NetCDF_Fortran_links SRC_EXT f90)
123+
124+
if(NOT NetCDF_Fortran_links)
125+
return()
126+
endif()
127+
98128
set(NetCDF_Fortran_FOUND true PARENT_SCOPE)
99129
set(NetCDF_Fortran_INCLUDE_DIR ${NetCDF_Fortran_INCLUDE_DIR} PARENT_SCOPE)
100130
set(NetCDF_Fortran_LIBRARY ${NetCDF_Fortran_LIBRARY} PARENT_SCOPE)
@@ -140,6 +170,10 @@ if(Fortran IN_LIST NetCDF_FIND_COMPONENTS)
140170
list(APPEND _ncdf_req ${NetCDF_Fortran_LIBRARY})
141171
endif()
142172

173+
set(CMAKE_REQUIRED_FLAGS)
174+
set(CMAKE_REQUIRED_INCLUDES)
175+
set(CMAKE_REQUIRED_LIBRARIES)
176+
143177
mark_as_advanced(NetCDF_C_INCLUDE_DIR NetCDF_Fortran_INCLUDE_DIR NetCDF_C_LIBRARY NetCDF_Fortran_LIBRARY)
144178

145179
include(FindPackageHandleStandardArgs)

cmake/compiler_find.cmake

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# this must be include() before CMakeLists.txt project()
2+
3+
if(NOT CMAKE_BUILD_TYPE)
4+
set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Debug or Release")
5+
endif()
6+
7+
set(CMAKE_CONFIGURATION_TYPES "Release;RelWithDebInfo;Debug" CACHE STRING "Build type selections" FORCE)
8+
9+
# Help CMake find matching compilers, especially needed for MacOS
10+
11+
if(NOT DEFINED ENV{FC})
12+
find_program(FC NAMES gfortran)
13+
if(FC)
14+
set(ENV{FC} ${FC})
15+
endif()
16+
endif()
17+
18+
if(DEFINED ENV{FC})
19+
set(FC $ENV{FC})
20+
21+
if(NOT DEFINED ENV{CC})
22+
# use same compiler for C and Fortran, which CMake might not do itself
23+
if(FC MATCHES ".*ifort")
24+
if(WIN32)
25+
set(ENV{CC} icl)
26+
else()
27+
set(ENV{CC} icc)
28+
endif()
29+
elseif(FC MATCHES ".*gfortran")
30+
# intel compilers don't need find_program for this to work, but GCC does...
31+
# remember, Apple has "/usr/bin/gcc" which is really clang
32+
# the technique below is NECESSARY to work on Mac and not find the wrong GCC
33+
get_filename_component(_gcc ${FC} DIRECTORY)
34+
find_program(CC NAMES gcc gcc-11 gcc-10 gcc-9 gcc-8 gcc-7
35+
HINTS ${_gcc}
36+
NO_SYSTEM_ENVIRONMENT_PATH NO_CMAKE_SYSTEM_PATH)
37+
# these parameters NECESSARY for Mac
38+
if(CC)
39+
set(ENV{CC} ${CC})
40+
endif()
41+
endif()
42+
endif()
43+
endif()

cmake/compilers.cmake

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
set(CMAKE_CONFIGURATION_TYPES "Release;RelWithDebInfo;Debug" CACHE STRING "Build type selections" FORCE)
1+
if(NOT CMAKE_Fortran_COMPILER_ID STREQUAL ${CMAKE_C_COMPILER_ID})
2+
message(FATAL_ERROR "C compiler ${CMAKE_C_COMPILER_ID} does not match Fortran compiler ${CMAKE_Fortran_COMPILER_ID}.
3+
Set environment variables CC and FC to control compiler selection in general.")
4+
endif()
25

36
include(CheckFortranCompilerFlag)
47

58
if(CMAKE_Fortran_COMPILER_ID STREQUAL Intel)
69
if(WIN32)
710
add_compile_options(/arch:native)
811
string(APPEND CMAKE_Fortran_FLAGS " /stand:f18 /traceback /warn /heap-arrays")
12+
string(APPEND CMAKE_Fortran_FLAGS_DEBUG " /check:bounds /debug:all")
913
else()
1014
add_compile_options(-march=native)
1115
string(APPEND CMAKE_Fortran_FLAGS " -stand f18 -traceback -warn -heap-arrays")
16+
string(APPEND CMAKE_Fortran_FLAGS_DEBUG " -check all -debug extended")
1217
endif()
1318
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL GNU)
1419
add_compile_options(-mtune=native -Wall -Wextra)
@@ -21,8 +26,11 @@ elseif(CMAKE_Fortran_COMPILER_ID STREQUAL GNU)
2126
endif()
2227

2328
if(CMAKE_Fortran_COMPILER_VERSION VERSION_EQUAL 9.3.0)
24-
# makes a lot of spurious warnngs on alloctable scalar character
29+
# makes a lot of spurious warnings on allocatable scalar character
2530
string(APPEND CMAKE_Fortran_FLAGS " -Wno-maybe-uninitialized")
31+
elseif(CMAKE_Fortran_COMPILER_VERSION VERSION_EQUAL 10.2.0)
32+
# avoid spurious warning on intrinsic :: rank
33+
string(APPEND CMAKE_Fortran_FLAGS " -Wno-surprising")
2634
endif()
2735
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL PGI)
2836
string(APPEND CMAKE_Fortran_FLAGS " -C -Mdclchk")

cmake/netcdf.cmake

Lines changed: 0 additions & 14 deletions
This file was deleted.

cmake/policy.cmake

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
set(CMAKE_EXPORT_COMPILE_COMMANDS on)
2+
3+
if(NOT DEFINED ${PROJECT_NAME}_BUILD_TESTING)
4+
set(${PROJECT_NAME}_BUILD_TESTING ${BUILD_TESTING})
5+
endif()
6+
7+
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
8+
message(FATAL_ERROR "use cmake -B build or similar to avoid building in-source, which is messy")
9+
endif()
10+
11+
if(CMAKE_VERSION VERSION_EQUAL 3.19.0-rc1)
12+
message(FATAL_ERROR "CMake 3.19.0-rc1 has breaking bugs for any project. Please use a different CMake version.")
13+
endif()
14+
15+
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.20)
16+
# explicit source file extensions
17+
cmake_policy(SET CMP0115 NEW)
18+
endif()
19+
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.19)
20+
# make missing imported targets fail immediately
21+
cmake_policy(SET CMP0111 NEW)
22+
endif()
23+
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.18)
24+
# saner ALIAS target policies
25+
cmake_policy(SET CMP0107 NEW)
26+
cmake_policy(SET CMP0108 NEW)
27+
endif()
28+
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.17)
29+
cmake_policy(SET CMP0099 NEW)
30+
endif()

meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
project('nc4fortran', 'fortran',
22
meson_version : '>=0.52.0',
3-
version: '1.0.2',
3+
version: '1.1.0',
44
default_options : ['default_library=static', 'buildtype=release', 'warning_level=3'])
55

66
subdir('meson')

setup.cmake

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,32 @@ set(_opts)
77

88
# --- boilerplate follows
99
message(STATUS "CMake ${CMAKE_VERSION}")
10-
if(CMAKE_VERSION VERSION_LESS 3.14)
11-
message(FATAL_ERROR "Please update CMake >= 3.14")
10+
if(CMAKE_VERSION VERSION_LESS 3.15)
11+
message(FATAL_ERROR "Please update CMake >= 3.15")
12+
endif()
13+
14+
# CTEST_CMAKE_GENERATOR must always be defined
15+
if(NOT DEFINED CTEST_CMAKE_GENERATOR AND CMAKE_VERSION VERSION_GREATER_EQUAL 3.17)
16+
find_program(_gen NAMES ninja ninja-build samu)
17+
if(_gen)
18+
execute_process(COMMAND ${_gen} --version
19+
OUTPUT_VARIABLE _ninja_version
20+
OUTPUT_STRIP_TRAILING_WHITESPACE
21+
RESULT_VARIABLE _gen_ok
22+
TIMEOUT 10)
23+
if(_gen_ok EQUAL 0 AND _ninja_version VERSION_GREATER_EQUAL 1.10)
24+
set(CTEST_CMAKE_GENERATOR "Ninja")
25+
endif()
26+
endif(_gen)
27+
endif()
28+
if(NOT DEFINED CTEST_CMAKE_GENERATOR)
29+
if(WIN32)
30+
set(CTEST_CMAKE_GENERATOR "MinGW Makefiles")
31+
set(CTEST_BUILD_FLAGS -j) # not --parallel as this goes to generator directly
32+
else()
33+
set(CTEST_CMAKE_GENERATOR "Unix Makefiles")
34+
set(CTEST_BUILD_FLAGS -j) # not --parallel as this goes to generator directly
35+
endif()
1236
endif()
1337

1438
# site is OS name
@@ -21,24 +45,6 @@ include(ProcessorCount)
2145
ProcessorCount(Ncpu)
2246
message(STATUS "${Ncpu} CPU cores detected")
2347

24-
# test name is Fortran compiler in FC
25-
# Note: ctest scripts cannot read cache variables like CMAKE_Fortran_COMPILER
26-
if(DEFINED ENV{FC})
27-
set(FC $ENV{FC})
28-
set(CTEST_BUILD_NAME ${FC})
29-
30-
if(NOT DEFINED ENV{CC})
31-
# use same compiler for C and Fortran, which CMake might not do itself
32-
if(FC STREQUAL ifort)
33-
if(WIN32)
34-
set(ENV{CC} icl)
35-
else()
36-
set(ENV{CC} icc)
37-
endif()
38-
endif()
39-
endif()
40-
endif()
41-
4248
if(NOT DEFINED CTEST_BUILD_CONFIGURATION)
4349
set(CTEST_BUILD_CONFIGURATION "Release")
4450
endif()
@@ -48,20 +54,6 @@ if(NOT DEFINED CTEST_BINARY_DIRECTORY)
4854
set(CTEST_BINARY_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/build)
4955
endif()
5056

51-
# CTEST_CMAKE_GENERATOR must be defined in any case here.
52-
if(NOT DEFINED CTEST_CMAKE_GENERATOR)
53-
find_program(_gen NAMES ninja ninja-build samu)
54-
if(_gen)
55-
set(CTEST_CMAKE_GENERATOR "Ninja")
56-
elseif(WIN32)
57-
set(CTEST_CMAKE_GENERATOR "MinGW Makefiles")
58-
set(CTEST_BUILD_FLAGS -j) # not --parallel as this goes to generator directly
59-
else()
60-
set(CTEST_CMAKE_GENERATOR "Unix Makefiles")
61-
set(CTEST_BUILD_FLAGS -j) # not --parallel as this goes to generator directly
62-
endif()
63-
endif()
64-
6557
# -- build and test
6658
ctest_start("Experimental" ${CTEST_SOURCE_DIRECTORY} ${CTEST_BINARY_DIRECTORY})
6759

src/CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ set(is_windows .false.)
33
if(WIN32)
44
set(is_windows .true.)
55
endif()
6-
configure_file(pathlib.in.f90 pathlib.f90)
6+
configure_file(pathlib.in.f90 pathlib.f90 @ONLY)
77

88
file(READ reader_template.in.f90 reader_template)
9-
configure_file(reader.in.f90 reader.f90)
9+
configure_file(reader.in.f90 reader.f90 @ONLY)
1010

1111
file(READ writer_template.in.f90 writer_template)
12-
configure_file(writer.in.f90 writer.f90)
12+
configure_file(writer.in.f90 writer.f90 @ONLY)
1313

1414
target_sources(nc4fortran PRIVATE
15-
interface.f90 attributes.f90
15+
interface.f90
1616
read.f90 ${CMAKE_CURRENT_BINARY_DIR}/reader.f90
1717
write.f90 ${CMAKE_CURRENT_BINARY_DIR}/writer.f90
18+
attributes.f90
1819
${CMAKE_CURRENT_BINARY_DIR}/pathlib.f90)

0 commit comments

Comments
 (0)