From 1e1a1429dbcbf558a3916aed0b725b39421b7785 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Wed, 3 Jan 2024 02:42:20 -0700 Subject: [PATCH] starting to rebuild cmake build system --- CMakeLists.txt | 13 +++-- aux_progs/CMakeLists.txt | 7 +++ ftn_api/CMakeLists.txt | 28 ++++++++++ wgrib2/CMakeLists.txt | 107 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 151 insertions(+), 4 deletions(-) create mode 100644 aux_progs/CMakeLists.txt create mode 100644 ftn_api/CMakeLists.txt create mode 100644 wgrib2/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index dfa42f75..f4c17145 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,12 @@ +# This is the make cmake file for wgrib2. +# +# Kyle Gerheiser, Edward Hartnett cmake_minimum_required(VERSION 3.12) -project(wgrib2 VERSION 2.0.8 LANGUAGES Fortran C) +project(wgrib2 VERSION 3.2.0 LANGUAGES Fortran C) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") -#set default install path if not provided +# Set default install path if not provided. if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" @@ -12,6 +15,7 @@ endif() include(GNUInstallDirs) +# Set compiler flags. if(CMAKE_C_COMPILER_ID MATCHES "^(Intel)$") set(CMAKE_C_FLAGS "-g -traceback ${CMAKE_C_FLAGS}") set(CMAKE_C_FLAGS_DEBUG "-O0") @@ -28,6 +32,7 @@ elseif(CMAKE_Fortran_COMPILER_ID MATCHES "^(GNU)$") set(CMAKE_Fortran_FLAGS_DEBUG "-ggdb -O0") endif() +# Handle build options. option(USE_NETCDF3 "Use NetCDF-3?" off) option(USE_NETCDF4 "Use NetCDF-4?" off) option(USE_REGEX "Use Regex?" on) @@ -50,7 +55,6 @@ option(USE_AEC "Use AEC?" off) option(BUILD_LIB "Build wgrib2 library?" on) option(BUILD_SHARED_LIB "Build shared library?" off) - if(USE_G2CLIB) if(USE_PNG) message(FATAL_ERROR "If USE_G2CLIB is on, USE_PNG must be off") @@ -134,10 +138,11 @@ if(USE_WMO_VALIDATION) list(APPEND definitions_list -DUSE_WMO_VALIDATION) endif() +# Build these subdirectories. add_subdirectory(wgrib2) add_subdirectory(aux_progs) - +# Build the wgrib2 library if desired. if(BUILD_LIB) add_subdirectory(ftn_api) diff --git a/aux_progs/CMakeLists.txt b/aux_progs/CMakeLists.txt new file mode 100644 index 00000000..c7dd0e91 --- /dev/null +++ b/aux_progs/CMakeLists.txt @@ -0,0 +1,7 @@ +# This is the cmake file for the aux_progs directory of wgrib2. +# +# Kyle Gerheiser, Edward Hartnett + +add_executable(gmerge gmerge.c uint8.c) +add_executable(smallest_grib2 smallest_grib2.c uint8.c) +add_executable(smallest_4 smallest_4.c uint8.c) diff --git a/ftn_api/CMakeLists.txt b/ftn_api/CMakeLists.txt new file mode 100644 index 00000000..ced37c34 --- /dev/null +++ b/ftn_api/CMakeLists.txt @@ -0,0 +1,28 @@ +# This is the cmake file for the ftn_api directory of wgrib2. +# +# Kyle Gerheiser, Edward Hartnett + +set(fortran_src wgrib2api.f90 wgrib2lowapi.f90) + +set(c_src fort_wgrib2.c) + +add_library(wgrib2_api ${fortran_src} ${c_src}) + +set(module_dir "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_INCLUDEDIR}") +set_target_properties(wgrib2_api PROPERTIES Fortran_MODULE_DIRECTORY ${module_dir}) + +target_include_directories(wgrib2_api + PUBLIC $ + $) + +target_link_libraries(wgrib2_api PUBLIC wgrib2_lib) + +install(DIRECTORY ${module_dir} DESTINATION ${CMAKE_INSTALL_PREFIX}) + +install( + TARGETS wgrib2_api + EXPORT wgrib2_exports + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) + diff --git a/wgrib2/CMakeLists.txt b/wgrib2/CMakeLists.txt new file mode 100644 index 00000000..14066175 --- /dev/null +++ b/wgrib2/CMakeLists.txt @@ -0,0 +1,107 @@ +# This is the cmake file for the wgrib2 directory of wgrib2. +# +# Kyle Gerheiser, Edward Hartnett + +# sets lib_src +include("source-files.cmake") + +set(callable_src + wgrib2.c + fatal_error.c + wgrib2_api.c + ) + +# This subdirectory no longer present! +#add_subdirectory(gctpc) + +# make this an object lib so we can re-use most of object files +# The only files that differ are ${callable_src} which are compiled +# with -DCALLABLE_WGRIB2 +add_library(obj_lib OBJECT ${lib_src}) +target_compile_definitions(obj_lib PUBLIC ${definitions_list}) + +if(BUILD_LIB) + # with -DCALLABLE_WGRIB2 for the lib + if(BUILD_SHARED_LIB) + add_library(wgrib2_lib SHARED ${lib_src} $ ${callable_src}) + else() + add_library(wgrib2_lib STATIC ${lib_src} $ ${callable_src}) + endif() + + # library and executable have same name (wgrib2) but different target names + set_target_properties(wgrib2_lib PROPERTIES OUTPUT_NAME wgrib2) + target_compile_definitions(wgrib2_lib PRIVATE CALLABLE_WGRIB2 USE_REGEX) +endif() + + +# without -DCALLABLE_WGRIB2 for the executable +add_executable(wgrib2_exe ${callable_src}) +set_target_properties(wgrib2_exe PROPERTIES OUTPUT_NAME wgrib2) + +if(USE_NETCDF4) + target_link_libraries(obj_lib PUBLIC NetCDF::NetCDF_C) +endif() + +if(USE_JASPER) + target_include_directories(obj_lib PUBLIC ${JASPER_INCLUDE_DIR}) + target_link_libraries(obj_lib PUBLIC ${JASPER_LIBRARIES}) +endif() + +if(USE_PNG) + target_link_libraries(obj_lib PUBLIC PNG::PNG) +endif() + +if(OpenMP_C_FOUND) + target_link_libraries(obj_lib PUBLIC OpenMP::OpenMP_C) +endif() + +if(USE_G2CLIB) +endif() + +if(USE_IPOLATES EQUAL 3) + target_link_libraries(obj_lib PUBLIC ip2::ip2_d) + + # Link to the Fortran runtime library for each compiler if using ip2. + # The wgrib2 exectuable is created using the C compiler and + # doesn't link the necessary Fortran library required for ip2. + if(CMAKE_Fortran_COMPILER_ID MATCHES "^(Intel)$") + target_link_libraries(wgrib2_exe PRIVATE "-lifcore") + elseif(CMAKE_Fortran_COMPILER_ID MATCHES "^(GNU)$") + target_link_libraries(wgrib2_exe PRIVATE "-lgfortran") + endif() + +endif() + +target_link_libraries(obj_lib PUBLIC gctpc -lm) + +# Link to gctpc directly because oobject libraries do not link transitively +target_link_libraries(wgrib2_exe PRIVATE gctpc) +target_link_libraries(wgrib2_exe PRIVATE obj_lib) + +if(BUILD_LIB) + set(headers wgrib2_api.h wgrib2.h) + target_link_libraries(wgrib2_lib PUBLIC gctpc) + set_target_properties(wgrib2_lib PROPERTIES PUBLIC_HEADER "${headers}") + + install( + TARGETS wgrib2_lib + EXPORT wgrib2_exports + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + ) + +endif() + +install( + TARGETS wgrib2_exe obj_lib + EXPORT wgrib2_exports + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) + + + + +