Skip to content
Snippets Groups Projects

M300433/cmake improvements and define cleanups

Merged Oliver Heidmann requested to merge m300433/cmake_improvements_and_define_cleanups into develop
4 unresolved threads
+ 67
28
cmake_minimum_required( VERSION 3.27 FATAL_ERROR )
message(VERBOSE "Entering libcdi")
project(libcdi VERSION 2.5.1 LANGUAGES C )
project(cdilib VERSION 2.5.1 LANGUAGES C )
set(CMAKE_C_STANDARD 11)
include(CTest)
include(CheckIncludeFile)
# Finding libraries
### -------------- Pthread ---------------------------
option(enable_pthread "Use the pthread library [default=ON]" ON)
if(${enable_pthread})
include(FindThreads)
find_package(Threads REQUIRED)
set_target_properties(Threads::Threads PROPERTIES THREADS_PREFER_PTHREAD_FLAG TRUE)
list(APPEND cdi_compile_defs HAVE_PTHREAD=1)
endif()
# enable default internal libs
add_compile_definitions(HAVE_LIBGRIB=1)
add_compile_definitions(HAVE_LIBCGRIBEX=1)
add_compile_definitions(HAVE_LIBEXTRA=1)
add_compile_definitions(HAVE_LIBSERVICE=1)
add_compile_definitions(HAVE_LIBIEG=1)
option(enable_libgrib "GRIB support [default=ON]" ON)
if(${enable_libgrib})
list(APPEND cdi_compile_defs HAVE_LIBGRIB=1)
endif()
option(enable_cgribex "Use the CGRIBEX library [default=ON]" ON)
if(${enable_cgribex})
list(APPEND cdi_compile_defs LIBCGRIBEX=1)
endif()
option(enable_extra "Use the extra library [default=ON]" ON)
if(${enable_extra})
list(APPEND cdi_compile_defs HAVE_LIBEXTRA=1)
endif()
option(enable_ieg "Use the extra library [default=ON]" ON)
if(${enable_ieg})
list(APPEND cdi_compile_defs HAVE_LIBIEG=1)
endif()
option(enable_service "Use the extra library [default=ON]" ON)
if(${enable_service})
list(APPEND cdi_compile_defs HAVE_LIBSERVICE=1)
endif()
# ecCodes
find_package(eccodes)
if (${eccodes_FOUND})
add_compile_definitions(HAVE_LIBGRIB_API=${eccodes_FOUND})
message(VERBOSE "added compile definition HAVE_LIBGRIB_API=${eccodes_FOUND}")
else()
message(WARNING "netcdf not found, compiling without netcdf")
endif ()
option(enable_eccodes "Use the eccodes library [default=ON]" ON)
if(${enable_eccodes} OR eccodes_ROOT)
find_package(eccodes)
if (${eccodes_FOUND})
list(APPEND cdi_compile_defs HAVE_LIBGRIB_API=${eccodes_FOUND})
message(VERBOSE "added compile definition HAVE_LIBGRIB_API=${eccodes_FOUND}")
else()
message(WARNING "eccodes not found, compiling without eccodes")
endif ()
endif()
# NetCDF
find_package(netCDF COMPONENTS C REQUIRED)
check_include_file("netcdf.h" ${netcdf_flag} C)
if (${netCDF_FOUND})
message(VERBOSE "added compile definition HAVE_LIBNETCDF=${netCDF_FOUND}")
message(VERBOSE "added compile definition HAVE_NETCDF4=${netCDF_FOUND}")
add_compile_definitions(HAVE_LIBNETCDF=${netCDF_FOUND})
add_compile_definitions(HAVE_NETCDF4=${netCDF_FOUND})
else()
message(WARNING "netcdf not found, compiling without netcdf")
endif ()
option(enable_netcdf "Use the netcdf library [default=ON]" ON)
if(${enable_netcdf} OR netCDF_ROOT )
find_package(netCDF COMPONENTS C REQUIRED)
if (TARGET netCDF::netcdf)
list(APPEND cdi_compile_defs
HAVE_LIBNETCDF=${netCDF_FOUND}
HAVE_LIBNC_DAP=${netCDF_FOUND}
HAVE_NETCDF4=${netCDF_FOUND}
HAVE_LIBGRIB_API=${netCDF_FOUND}
HAVE_LIBGRIB=${netCDF_FOUND}
Please register or sign in to reply
)
else()
message(WARNING "netcdf target not found, compiling without netcdf")
    • I think this code is not reachable, as the find_package is REQUIRED.

      • Author Maintainer

        Why is (enable_netcdf OR netCDF_ROOT) not enough to be able to reach the find_package? I have trouble following you in some cases. Please explain in short why something does not work.

      • find_package(netCDF COMPONENTS C REQUIRED) will either find netCDF::netcdf or fail with an error right away because of the REQUIRED keyword, which means if (TARGET netCDF::netcdf) is always true, at least according to FindNetCDF.cmake documentation and logic. I just quickly read it to make sure.

        By the way it is considered a good practice to use REQUIRED, so I'm not advocating to remove it, however I do think the else() and warning below cannot be reached and can be removed.

      • Author Maintainer

        Oh you mean the else. Ok that makes sense. Thanks for clarifying.

      • Please register or sign in to reply
Please register or sign in to reply
endif ()
endif()
message(VERBOSE "looking for config.h in: ${PROJECT_BINARY_DIR}/src")
include_directories( "${PROJECT_BINARY_DIR}/src/config.h")
#adding subdirectories
## lib and general files
list(APPEND cdi_compile_defs CDI=1 CDI_SIZE_TYPE=size_t PACKAGE_NAME="${PROJECT_NAME}" VERSION="${CMAKE_PROJECT_VERSION}")
add_subdirectory(src)
## cdi executable
add_subdirectory(app)
#tests
add_subdirectory( tests )
add_subdirectory(app)
add_subdirectory(tests)
Loading