From 76bac8da7112084b08bba86ddfa58774ee490a88 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Sat, 21 May 2022 14:37:23 +0100 Subject: [PATCH] ECC-1393: CMake: Add option to build ecCodes with C++ --- CMakeLists.txt | 13 +++++++++++++ src/CMakeLists.txt | 8 ++++++++ tools/CMakeLists.txt | 7 +++++++ tools/grib_get_data.c | 4 ++-- tools/grib_tools.h | 8 ++++++++ 5 files changed, 38 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d4c7df8b0..df0920c04 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,6 +65,13 @@ ecbuild_info("Operating system=${CMAKE_SYSTEM} (${EC_OS_BITS} bits)") ############################################################################### # some variables/options of this project +ecbuild_add_option( FEATURE EXPERIMENTAL_BUILD_WITH_CXX + DESCRIPTION "Build ecCodes with a C++ compiler (Experimental)" + DEFAULT OFF ) +if( HAVE_EXPERIMENTAL_BUILD_WITH_CXX ) + enable_language( CXX ) +endif() + ecbuild_add_option( FEATURE PRODUCT_GRIB DESCRIPTION "Support for the product GRIB" DEFAULT ON ) @@ -515,3 +522,9 @@ ecbuild_info(" | bindings from PyPI with: |") ecbuild_info(" | $ pip3 install eccodes |") ecbuild_info(" +--------------------------------------+") ecbuild_info("") + +if( HAVE_EXPERIMENTAL_BUILD_WITH_CXX ) + ecbuild_warn("Please note:\n" + "The option of building with C++ is currently experimental." + "Do not use in operations.") +endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 37a2d00bf..ea0a12b4a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -427,6 +427,14 @@ if( HAVE_MEMFS ) list(APPEND ECCODES_EXTRA_LIBRARIES eccodes_memfs) endif() +if( HAVE_EXPERIMENTAL_BUILD_WITH_CXX ) + foreach( _src ${eccodes_src_files} ) + get_filename_component( _ext ${_src} EXT ) + if( _ext STREQUAL ".c" ) + set_source_files_properties( ${_src} PROPERTIES LANGUAGE CXX ) + endif() + endforeach() +endif() ecbuild_add_library( TARGET eccodes SOURCES ${CMAKE_CURRENT_BINARY_DIR}/grib_api_version.c diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 781e6f22a..edae7ab62 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -18,6 +18,10 @@ if( EC_OS_NAME MATCHES "windows" ) list( APPEND ecc_tools_sources wingetopt.c ) endif() +if( HAVE_EXPERIMENTAL_BUILD_WITH_CXX ) + set_source_files_properties(${ecc_tools_sources} PROPERTIES LANGUAGE CXX) +endif() + # tools library ecbuild_add_library( TARGET ecc_tools TYPE STATIC @@ -48,6 +52,9 @@ list( APPEND ecc_tools_binaries_extra # Install generic tools foreach( tool ${ecc_tools_binaries} ) # here we use the fact that each tool has only one C file that matches its name + if( HAVE_EXPERIMENTAL_BUILD_WITH_CXX ) + set_source_files_properties(${tool}.c PROPERTIES LANGUAGE CXX) + endif() ecbuild_add_executable( TARGET ${tool} SOURCES ${tool}.c LIBS ecc_tools ) diff --git a/tools/grib_get_data.c b/tools/grib_get_data.c index 4d0e054a7..69f409649 100644 --- a/tools/grib_get_data.c +++ b/tools/grib_get_data.c @@ -84,7 +84,7 @@ int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h) char* format_values = NULL; char format_latlons[32] = {0,}; char* default_format_values = "%.10e"; - char* default_format_latlons = "%9.3f%9.3f"; + const char* default_format_latlons = "%9.3f%9.3f"; int print_keys = grib_options_on("p:"); long numberOfPoints = 0; long bitmapPresent = 0; @@ -305,7 +305,7 @@ static grib_values* get_key_values(grib_runtime_options* options, grib_handle* h int i = 0; int ret = 0; char value[MAX_STRING_LEN] = {0,}; - char* notfound = "not found"; + const char* notfound = "not found"; for (i = 0; i < options->print_keys_count; i++) { size_t len = MAX_STRING_LEN; diff --git a/tools/grib_tools.h b/tools/grib_tools.h index d11c8d11a..1ea85e216 100644 --- a/tools/grib_tools.h +++ b/tools/grib_tools.h @@ -45,6 +45,10 @@ #define MODE_TAF 5 #define MODE_ANY 6 +#ifdef __cplusplus +extern "C" { +#endif + typedef union grib_typed_value { long* long_value; @@ -208,4 +212,8 @@ int grib_tool_new_filename_action(grib_runtime_options* options, const char* fil int grib_no_handle_action(grib_runtime_options* options, int err); int exit_if_input_is_directory(const char* toolname, const char* filename); +#ifdef __cplusplus +} +#endif + #endif /* GRIB_TOOLS_H */