Merge branch 'release-v4.6.0'
[WPS.git] / cmake / modules / FindnetCDF-Fortran.cmake
blob0ead239a576135a60267dba0348de1d69025e1a4
1 # Find netcdf
2 # Eventually replace with netCDF-Fortran's actual config if using that
3 # Once found this file will define:
4 #  netCDF-Fortran_FOUND - System has netcdf
5 #  netCDF-Fortran_INCLUDE_DIRS - The netcdf include directories
6 #  netCDF-Fortran_LIBRARIES - The libraries needed to use netcdf
7 #  netCDF-Fortran_DEFINITIONS - Compiler switches required for using netcdf
9 # list( REMOVE_ITEM CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR} )
10 # find_package( netCDF-Fortran )
11 # list( APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR} )
14 # exit early if we don't even need to be here
15 if ( netCDF-Fortran_FOUND )
16   return()
17 endif()
19 ###############################################################################
20 # First try to find using netCDF-Fortran native cmake build
21 # TODO : Enable this when netCDF-Fortran native cmake build works well as an imported package
22 # find_package( netCDF-Fortran CONFIG )
23 # if ( netCDF-Fortran_FOUND )
24 #   message( STATUS "Found netCDF-Fortran through native cmake build" )
25 #   return()
26 # endif()
27 ###############################################################################
29 # else
30 # Use nf-config
31 find_program( 
32                 NETCDF-FORTRAN_PROGRAM
33                 nf-config
34                 QUIET
35                 )
37 if ( ${NETCDF-FORTRAN_PROGRAM} MATCHES "-NOTFOUND$" )
38   message( STATUS "No nf-config found" )
39 else()
40   message( STATUS "Found NETCDF-FORTRAN_PROGRAM : ${NETCDF-FORTRAN_PROGRAM}" )
42   execute_process( COMMAND ${NETCDF-FORTRAN_PROGRAM} --includedir   OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE netCDF-Fortran_INCLUDE_DIR )
43   execute_process( COMMAND ${NETCDF-FORTRAN_PROGRAM} --prefix       OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE netCDF-Fortran_PREFIX )
44   execute_process( COMMAND ${NETCDF-FORTRAN_PROGRAM} --flibs        OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE netCDF-Fortran_FLIBS   )
45   execute_process( COMMAND ${NETCDF-FORTRAN_PROGRAM} --version      OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE netCDF-Fortran_VERSION_RAW )
46   execute_process( COMMAND ${NETCDF-FORTRAN_PROGRAM} --has-nc4      OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE netCDF-Fortran_NC4_YES )
48   # check for large file support
49   find_file( netCDF-Fortran_INCLUDE_FILE netcdf.inc ${netCDF-Fortran_INCLUDE_DIR} )
50   file( READ ${netCDF-Fortran_INCLUDE_FILE} netCDF-Fortran_INCLUDE_FILE_STR )
51   string( FIND "${netCDF-Fortran_INCLUDE_FILE_STR}" "nf_format_64bit_data" netCDF-Fortran_LARGE_FILE_SUPPORT_FOUND )
52   if ( ${netCDF-Fortran_LARGE_FILE_SUPPORT_FOUND} EQUAL -1 )
53     set( netCDF-Fortran_LARGE_FILE_SUPPORT "NO" )
54   else()
55     set( netCDF-Fortran_LARGE_FILE_SUPPORT "YES" )
56   endif()
58   # Sanitize version
59   string( REPLACE " " ";" netCDF-Fortran_VERSION_LIST ${netCDF-Fortran_VERSION_RAW} )
60   list( GET netCDF-Fortran_VERSION_LIST -1 netCDF-Fortran_VERSION )
62   # Convert to YES/NO - Note cannot be generator expression if you want to use it during configuration time
63   string( TOUPPER ${netCDF-Fortran_NC4_YES}      netCDF-Fortran_NC4      )
65   set( netCDF-Fortran_DEFINITIONS  )
66   set( netCDF-Fortran_LIBRARY_DIR  ${netCDF-Fortran_PREFIX}/lib )
68   set( netCDF-Fortran_LIBRARIES
69       $<$<LINK_LANGUAGE:Fortran>:${netCDF-Fortran_FLIBS}>
70       )
72   # Because we may need this for in-situ manual preprocessing do not use genex
73   set( netCDF-Fortran_INCLUDE_DIRS ${netCDF-Fortran_INCLUDE_DIR} )
74 endif()
76 find_package( PkgConfig )
78 include(FindPackageHandleStandardArgs)
80 # handle the QUIETLY and REQUIRED arguments and set netCDF-Fortran_FOUND to TRUE
81 # if all listed variables are TRUE
82 find_package_handle_standard_args(
83                                   netCDF-Fortran  DEFAULT_MSG
84                                   netCDF-Fortran_INCLUDE_DIRS
85                                   netCDF-Fortran_FLIBS
86                                   netCDF-Fortran_VERSION
87                                   )
89 mark_as_advanced( netCDF-Fortran_FLIBS netCDF-Fortran_PREFIX netCDF-Fortran_LIBRARY_DIR )