Skip to content

Commit 7b1d6b5

Browse files
committed
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.
1 parent c4bd272 commit 7b1d6b5

File tree

6 files changed

+40
-26
lines changed

6 files changed

+40
-26
lines changed

CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,36 @@ set(CMAKE_CXX_STANDARD 11)
4646
set(CMAKE_CXX_EXTENSIONS OFF)
4747
set(CMAKE_CXX_STANDARD_REQUIRED ON)
4848

49+
set(JSONCPP_HAS_STRING_VIEW_DEFAULT OFF)
50+
if (${CMAKE_CXX_STANDARD} GREATER_EQUAL 17)
51+
set(JSONCPP_HAS_STRING_VIEW_DEFAULT ON)
52+
endif()
53+
54+
option(JSONCPP_HAS_STRING_VIEW "Enable support for string view" ${JSONCPP_HAS_STRING_VIEW_DEFAULT})
55+
56+
configure_file(configure/configure.h.in include/json/configure.h)
57+
58+
set(JSONCPP_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
59+
60+
file(
61+
COPY include/json
62+
DESTINATION ${JSONCPP_INCLUDE_DIR}
63+
FILES_MATCHING
64+
PATTERN "*.h"
65+
)
66+
67+
set(PUBLIC_HEADERS
68+
${JSONCPP_INCLUDE_DIR}/json/config.h
69+
${JSONCPP_INCLUDE_DIR}/json/configure.h
70+
${JSONCPP_INCLUDE_DIR}/json/forwards.h
71+
${JSONCPP_INCLUDE_DIR}/json/json_features.h
72+
${JSONCPP_INCLUDE_DIR}/json/value.h
73+
${JSONCPP_INCLUDE_DIR}/json/reader.h
74+
${JSONCPP_INCLUDE_DIR}/json/version.h
75+
${JSONCPP_INCLUDE_DIR}/json/writer.h
76+
${JSONCPP_INCLUDE_DIR}/json/assertions.h
77+
)
78+
4979
# Ensure that CMAKE_BUILD_TYPE has a value specified for single configuration generators.
5080
if(NOT DEFINED CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES)
5181
set(CMAKE_BUILD_TYPE Release CACHE STRING

configure/configure.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#cmakedefine JSONCPP_HAS_STRING_VIEW

include/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
file(GLOB INCLUDE_FILES "json/*.h")
21
install(FILES
3-
${INCLUDE_FILES}
2+
${PUBLIC_HEADERS}
43
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/json)
5-

include/json/value.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,15 @@
3939
#endif
4040
#endif
4141

42-
#if __cplusplus >= 201703L
43-
#define JSONCPP_HAS_STRING_VIEW 1
44-
#endif
45-
4642
#include <array>
4743
#include <exception>
4844
#include <map>
4945
#include <memory>
5046
#include <string>
5147
#include <vector>
5248

49+
#include "json/configure.h"
50+
5351
#ifdef JSONCPP_HAS_STRING_VIEW
5452
#include <string_view>
5553
#endif
@@ -581,7 +579,7 @@ class JSON_API Value {
581579
/// Do nothing if it did not exist.
582580
/// \pre type() is objectValue or nullValue
583581
/// \post type() is unchanged
584-
#if JSONCPP_HAS_STRING_VIEW
582+
#ifdef JSONCPP_HAS_STRING_VIEW
585583
void removeMember(std::string_view key);
586584
#else
587585
void removeMember(const char* key);
@@ -595,7 +593,7 @@ class JSON_API Value {
595593
* \param key may contain embedded nulls.
596594
* \return true iff removed (no exceptions)
597595
*/
598-
#if JSONCPP_HAS_STRING_VIEW
596+
#ifdef JSONCPP_HAS_STRING_VIEW
599597
bool removeMember(std::string_view key, Value* removed);
600598
#else
601599
bool removeMember(String const& key, Value* removed);

src/lib_json/CMakeLists.txt

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,6 @@ if(NOT (HAVE_CLOCALE AND HAVE_LCONV_SIZE AND HAVE_DECIMAL_POINT AND HAVE_LOCALEC
2525
endif()
2626
endif()
2727

28-
set(JSONCPP_INCLUDE_DIR ../../include)
29-
30-
set(PUBLIC_HEADERS
31-
${JSONCPP_INCLUDE_DIR}/json/config.h
32-
${JSONCPP_INCLUDE_DIR}/json/forwards.h
33-
${JSONCPP_INCLUDE_DIR}/json/json_features.h
34-
${JSONCPP_INCLUDE_DIR}/json/value.h
35-
${JSONCPP_INCLUDE_DIR}/json/reader.h
36-
${JSONCPP_INCLUDE_DIR}/json/version.h
37-
${JSONCPP_INCLUDE_DIR}/json/writer.h
38-
${JSONCPP_INCLUDE_DIR}/json/assertions.h
39-
)
40-
4128
source_group("Public API" FILES ${PUBLIC_HEADERS})
4229

4330
set(JSONCPP_SOURCES
@@ -131,7 +118,7 @@ if(BUILD_SHARED_LIBS)
131118

132119
target_include_directories(${SHARED_LIB} PUBLIC
133120
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
134-
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>
121+
$<BUILD_INTERFACE:${JSONCPP_INCLUDE_DIR}>
135122
)
136123

137124
list(APPEND CMAKE_TARGETS ${SHARED_LIB})
@@ -164,7 +151,7 @@ if(BUILD_STATIC_LIBS)
164151

165152
target_include_directories(${STATIC_LIB} PUBLIC
166153
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
167-
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>
154+
$<BUILD_INTERFACE:${JSONCPP_INCLUDE_DIR}>
168155
)
169156

170157
list(APPEND CMAKE_TARGETS ${STATIC_LIB})
@@ -190,7 +177,7 @@ if(BUILD_OBJECT_LIBS)
190177

191178
target_include_directories(${OBJECT_LIB} PUBLIC
192179
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
193-
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>
180+
$<BUILD_INTERFACE:${JSONCPP_INCLUDE_DIR}>
194181
)
195182

196183
list(APPEND CMAKE_TARGETS ${OBJECT_LIB})
@@ -202,4 +189,3 @@ install(TARGETS ${CMAKE_TARGETS} ${INSTALL_EXPORT}
202189
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
203190
OBJECTS DESTINATION ${CMAKE_INSTALL_LIBDIR}
204191
)
205-

src/lib_json/json_value.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#if !defined(JSON_IS_AMALGAMATION)
77
#include <json/assertions.h>
8+
#include <json/configure.h>
89
#include <json/value.h>
910
#include <json/writer.h>
1011
#endif // if !defined(JSON_IS_AMALGAMATION)

0 commit comments

Comments
 (0)