Skip to content

Commit

Permalink
starting to rebuild cmake build system
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardhartnett committed Jan 3, 2024
1 parent cc1457c commit 1e1a142
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 4 deletions.
13 changes: 9 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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)

Expand Down
7 changes: 7 additions & 0 deletions aux_progs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
28 changes: 28 additions & 0 deletions ftn_api/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 $<BUILD_INTERFACE:${module_dir}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}>)

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})

107 changes: 107 additions & 0 deletions wgrib2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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} $<TARGET_OBJECTS:gctpc> ${callable_src})
else()
add_library(wgrib2_lib STATIC ${lib_src} $<TARGET_OBJECTS:gctpc> ${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})





0 comments on commit 1e1a142

Please sign in to comment.