Move string_view switch to cmake

This ensures that cmake between the compiled objects and the headers
match. Previously, if they didn't match you could get linker issues.
This commit is contained in:
Jake Stinson 2025-05-17 10:14:47 +01:00
parent c4bd272bfb
commit 7b1d6b58ec
6 changed files with 40 additions and 26 deletions

View File

@ -46,6 +46,36 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(JSONCPP_HAS_STRING_VIEW_DEFAULT OFF)
if (${CMAKE_CXX_STANDARD} GREATER_EQUAL 17)
set(JSONCPP_HAS_STRING_VIEW_DEFAULT ON)
endif()
option(JSONCPP_HAS_STRING_VIEW "Enable support for string view" ${JSONCPP_HAS_STRING_VIEW_DEFAULT})
configure_file(configure/configure.h.in include/json/configure.h)
set(JSONCPP_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
file(
COPY include/json
DESTINATION ${JSONCPP_INCLUDE_DIR}
FILES_MATCHING
PATTERN "*.h"
)
set(PUBLIC_HEADERS
${JSONCPP_INCLUDE_DIR}/json/config.h
${JSONCPP_INCLUDE_DIR}/json/configure.h
${JSONCPP_INCLUDE_DIR}/json/forwards.h
${JSONCPP_INCLUDE_DIR}/json/json_features.h
${JSONCPP_INCLUDE_DIR}/json/value.h
${JSONCPP_INCLUDE_DIR}/json/reader.h
${JSONCPP_INCLUDE_DIR}/json/version.h
${JSONCPP_INCLUDE_DIR}/json/writer.h
${JSONCPP_INCLUDE_DIR}/json/assertions.h
)
# Ensure that CMAKE_BUILD_TYPE has a value specified for single configuration generators.
if(NOT DEFINED CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE Release CACHE STRING

1
configure/configure.h.in Normal file
View File

@ -0,0 +1 @@
#cmakedefine JSONCPP_HAS_STRING_VIEW

View File

@ -1,5 +1,3 @@
file(GLOB INCLUDE_FILES "json/*.h")
install(FILES
${INCLUDE_FILES}
${PUBLIC_HEADERS}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/json)

View File

@ -39,10 +39,6 @@
#endif
#endif
#if __cplusplus >= 201703L
#define JSONCPP_HAS_STRING_VIEW 1
#endif
#include <array>
#include <exception>
#include <map>
@ -50,6 +46,8 @@
#include <string>
#include <vector>
#include "json/configure.h"
#ifdef JSONCPP_HAS_STRING_VIEW
#include <string_view>
#endif
@ -581,7 +579,7 @@ public:
/// Do nothing if it did not exist.
/// \pre type() is objectValue or nullValue
/// \post type() is unchanged
#if JSONCPP_HAS_STRING_VIEW
#ifdef JSONCPP_HAS_STRING_VIEW
void removeMember(std::string_view key);
#else
void removeMember(const char* key);
@ -595,7 +593,7 @@ public:
* \param key may contain embedded nulls.
* \return true iff removed (no exceptions)
*/
#if JSONCPP_HAS_STRING_VIEW
#ifdef JSONCPP_HAS_STRING_VIEW
bool removeMember(std::string_view key, Value* removed);
#else
bool removeMember(String const& key, Value* removed);

View File

@ -25,19 +25,6 @@ if(NOT (HAVE_CLOCALE AND HAVE_LCONV_SIZE AND HAVE_DECIMAL_POINT AND HAVE_LOCALEC
endif()
endif()
set(JSONCPP_INCLUDE_DIR ../../include)
set(PUBLIC_HEADERS
${JSONCPP_INCLUDE_DIR}/json/config.h
${JSONCPP_INCLUDE_DIR}/json/forwards.h
${JSONCPP_INCLUDE_DIR}/json/json_features.h
${JSONCPP_INCLUDE_DIR}/json/value.h
${JSONCPP_INCLUDE_DIR}/json/reader.h
${JSONCPP_INCLUDE_DIR}/json/version.h
${JSONCPP_INCLUDE_DIR}/json/writer.h
${JSONCPP_INCLUDE_DIR}/json/assertions.h
)
source_group("Public API" FILES ${PUBLIC_HEADERS})
set(JSONCPP_SOURCES
@ -131,7 +118,7 @@ if(BUILD_SHARED_LIBS)
target_include_directories(${SHARED_LIB} PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>
$<BUILD_INTERFACE:${JSONCPP_INCLUDE_DIR}>
)
list(APPEND CMAKE_TARGETS ${SHARED_LIB})
@ -164,7 +151,7 @@ if(BUILD_STATIC_LIBS)
target_include_directories(${STATIC_LIB} PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>
$<BUILD_INTERFACE:${JSONCPP_INCLUDE_DIR}>
)
list(APPEND CMAKE_TARGETS ${STATIC_LIB})
@ -190,7 +177,7 @@ if(BUILD_OBJECT_LIBS)
target_include_directories(${OBJECT_LIB} PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>
$<BUILD_INTERFACE:${JSONCPP_INCLUDE_DIR}>
)
list(APPEND CMAKE_TARGETS ${OBJECT_LIB})
@ -202,4 +189,3 @@ install(TARGETS ${CMAKE_TARGETS} ${INSTALL_EXPORT}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
OBJECTS DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

View File

@ -5,6 +5,7 @@
#if !defined(JSON_IS_AMALGAMATION)
#include <json/assertions.h>
#include <json/configure.h>
#include <json/value.h>
#include <json/writer.h>
#endif // if !defined(JSON_IS_AMALGAMATION)