Skip to content

Commit 6c34643

Browse files
committed
Compile jsoncpp into a different namespace when building as part of core
This allows it to be compiled into statically linked builds without causing ODR violations.
1 parent 877b01a commit 6c34643

4 files changed

Lines changed: 51 additions & 10 deletions

File tree

CMakeLists.txt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@ if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
1717
endif()
1818
endif()
1919

20-
file(GLOB BN_API_SOURCES CONFIGURE_DEPENDS *.cpp *.h json/json.h json/json-forwards.h)
21-
if(NOT DEMO)
22-
list(APPEND BN_API_SOURCES json/jsoncpp.cpp)
23-
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
24-
set_source_files_properties(json/jsoncpp.cpp PROPERTIES COMPILE_FLAGS /w)
25-
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
26-
set_source_files_properties(json/jsoncpp.cpp PROPERTIES COMPILE_FLAGS -Wno-everything)
27-
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
28-
set_source_files_properties(json/jsoncpp.cpp PROPERTIES COMPILE_FLAGS -w)
29-
endif()
20+
file(GLOB BN_API_SOURCES CONFIGURE_DEPENDS *.cpp *.h json/*.cpp json/*.h)
21+
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
22+
set_source_files_properties(json/jsoncpp.cpp PROPERTIES COMPILE_FLAGS /w)
23+
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
24+
set_source_files_properties(json/jsoncpp.cpp PROPERTIES COMPILE_FLAGS -Wno-everything)
25+
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
26+
set_source_files_properties(json/jsoncpp.cpp PROPERTIES COMPILE_FLAGS -w)
3027
endif()
3128

3229
if(NOT BN_INTERNAL_BUILD)

json/json-forwards.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ license you like.
7979
/// to prevent private header inclusion.
8080
#define JSON_IS_AMALGAMATION
8181

82+
#ifdef BINARYNINJACORE_LIBRARY
83+
#define JSONCPP_INLINE_NS_BEGIN inline namespace Core {
84+
#define JSONCPP_INLINE_NS_END }
85+
#else
86+
#define JSONCPP_INLINE_NS_BEGIN
87+
#define JSONCPP_INLINE_NS_END
88+
#endif
89+
8290
// //////////////////////////////////////////////////////////////////////
8391
// Beginning of content of file: include/json/config.h
8492
// //////////////////////////////////////////////////////////////////////
@@ -243,6 +251,7 @@ license you like.
243251
#endif // if !defined(JSON_IS_AMALGAMATION)
244252

245253
namespace Json {
254+
JSONCPP_INLINE_NS_BEGIN
246255
typedef int Int;
247256
typedef unsigned int UInt;
248257
#if defined(JSON_NO_INT64)
@@ -280,6 +289,7 @@ typedef UInt64 LargestUInt;
280289
#define JSONCPP_ISTRINGSTREAM std::istringstream
281290
#define JSONCPP_ISTREAM std::istream
282291
#endif // if JSONCPP_USING_SECURE_MEMORY
292+
JSONCPP_INLINE_NS_END
283293
} // end namespace Json
284294

285295
#endif // JSON_CONFIG_H_INCLUDED
@@ -310,6 +320,7 @@ typedef UInt64 LargestUInt;
310320
#endif // if !defined(JSON_IS_AMALGAMATION)
311321

312322
namespace Json {
323+
JSONCPP_INLINE_NS_BEGIN
313324

314325
// writer.h
315326
class FastWriter;
@@ -331,6 +342,7 @@ class ValueIteratorBase;
331342
class ValueIterator;
332343
class ValueConstIterator;
333344

345+
JSONCPP_INLINE_NS_END
334346
} // namespace Json
335347

336348
#endif // JSON_FORWARDS_H_INCLUDED

json/json.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ license you like.
7878
/// to prevent private header inclusion.
7979
#define JSON_IS_AMALGAMATION
8080

81+
#ifdef BINARYNINJACORE_LIBRARY
82+
#define JSONCPP_INLINE_NS_BEGIN inline namespace Core {
83+
#define JSONCPP_INLINE_NS_END }
84+
#else
85+
#define JSONCPP_INLINE_NS_BEGIN
86+
#define JSONCPP_INLINE_NS_END
87+
#endif
88+
8189
// //////////////////////////////////////////////////////////////////////
8290
// Beginning of content of file: include/json/version.h
8391
// //////////////////////////////////////////////////////////////////////
@@ -278,6 +286,7 @@ license you like.
278286
#endif // if !defined(JSON_IS_AMALGAMATION)
279287

280288
namespace Json {
289+
JSONCPP_INLINE_NS_BEGIN
281290
typedef int Int;
282291
typedef unsigned int UInt;
283292
#if defined(JSON_NO_INT64)
@@ -315,6 +324,7 @@ typedef UInt64 LargestUInt;
315324
#define JSONCPP_ISTRINGSTREAM std::istringstream
316325
#define JSONCPP_ISTREAM std::istream
317326
#endif // if JSONCPP_USING_SECURE_MEMORY
327+
JSONCPP_INLINE_NS_END
318328
} // end namespace Json
319329

320330
#endif // JSON_CONFIG_H_INCLUDED
@@ -345,6 +355,7 @@ typedef UInt64 LargestUInt;
345355
#endif // if !defined(JSON_IS_AMALGAMATION)
346356

347357
namespace Json {
358+
JSONCPP_INLINE_NS_BEGIN
348359

349360
// writer.h
350361
class FastWriter;
@@ -366,6 +377,7 @@ class ValueIteratorBase;
366377
class ValueIterator;
367378
class ValueConstIterator;
368379

380+
JSONCPP_INLINE_NS_END
369381
} // namespace Json
370382

371383
#endif // JSON_FORWARDS_H_INCLUDED
@@ -398,6 +410,7 @@ class ValueConstIterator;
398410
#pragma pack(push, 8)
399411

400412
namespace Json {
413+
JSONCPP_INLINE_NS_BEGIN
401414

402415
/** \brief Configuration passed to reader and writer.
403416
* This configuration object can be used to force the Reader or Writer
@@ -439,6 +452,7 @@ class JSON_API Features {
439452
bool allowNumericKeys_;
440453
};
441454

455+
JSONCPP_INLINE_NS_END
442456
} // namespace Json
443457

444458
#pragma pack(pop)
@@ -515,6 +529,7 @@ class JSON_API Features {
515529
/** \brief JSON (JavaScript Object Notation).
516530
*/
517531
namespace Json {
532+
JSONCPP_INLINE_NS_BEGIN
518533

519534
/** Base class for all exceptions we throw.
520535
*
@@ -1364,6 +1379,7 @@ class JSON_API ValueIterator : public ValueIteratorBase {
13641379

13651380
inline void swap(Value& a, Value& b) { a.swap(b); }
13661381

1382+
JSONCPP_INLINE_NS_END
13671383
} // namespace Json
13681384

13691385
#pragma pack(pop)
@@ -1415,6 +1431,7 @@ inline void swap(Value& a, Value& b) { a.swap(b); }
14151431
#pragma pack(push, 8)
14161432

14171433
namespace Json {
1434+
JSONCPP_INLINE_NS_BEGIN
14181435

14191436
/** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a
14201437
*Value.
@@ -1798,6 +1815,7 @@ bool JSON_API parseFromStream(CharReader::Factory const&,
17981815
*/
17991816
JSON_API JSONCPP_ISTREAM& operator>>(JSONCPP_ISTREAM&, Value&);
18001817

1818+
JSONCPP_INLINE_NS_END
18011819
} // namespace Json
18021820

18031821
#pragma pack(pop)
@@ -1846,6 +1864,7 @@ JSON_API JSONCPP_ISTREAM& operator>>(JSONCPP_ISTREAM&, Value&);
18461864
#pragma pack(push, 8)
18471865

18481866
namespace Json {
1867+
JSONCPP_INLINE_NS_BEGIN
18491868

18501869
class Value;
18511870

@@ -2180,6 +2199,7 @@ JSONCPP_STRING JSON_API valueToQuotedString(const char* value);
21802199
/// \see Json::operator>>()
21812200
JSON_API JSONCPP_OSTREAM& operator<<(JSONCPP_OSTREAM&, const Value& root);
21822201

2202+
JSONCPP_INLINE_NS_END
21832203
} // namespace Json
21842204

21852205
#pragma pack(pop)

json/jsoncpp.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ license you like.
114114
*/
115115

116116
namespace Json {
117+
JSONCPP_INLINE_NS_BEGIN
117118
static inline char getDecimalPoint() {
118119
#ifdef JSONCPP_NO_LOCALE_SUPPORT
119120
return '\0';
@@ -217,6 +218,7 @@ template <typename Iter> Iter fixZerosInTheEnd(Iter begin, Iter end) {
217218
return end;
218219
}
219220

221+
JSONCPP_INLINE_NS_END
220222
} // namespace Json
221223

222224
#endif // LIB_JSONCPP_JSON_TOOL_H_INCLUDED
@@ -291,6 +293,7 @@ static size_t const stackLimit_g =
291293
JSONCPP_DEPRECATED_STACK_LIMIT; // see readValue()
292294

293295
namespace Json {
296+
JSONCPP_INLINE_NS_BEGIN
294297

295298
#if __cplusplus >= 201103L || (defined(_CPPLIB_VER) && _CPPLIB_VER >= 520)
296299
typedef std::unique_ptr<CharReader> CharReaderPtr;
@@ -2265,6 +2268,7 @@ JSONCPP_ISTREAM& operator>>(JSONCPP_ISTREAM& sin, Value& root) {
22652268
return sin;
22662269
}
22672270

2271+
JSONCPP_INLINE_NS_END
22682272
} // namespace Json
22692273

22702274
// //////////////////////////////////////////////////////////////////////
@@ -2288,6 +2292,7 @@ JSONCPP_ISTREAM& operator>>(JSONCPP_ISTREAM& sin, Value& root) {
22882292
// included by json_value.cpp
22892293

22902294
namespace Json {
2295+
JSONCPP_INLINE_NS_BEGIN
22912296

22922297
// //////////////////////////////////////////////////////////////////
22932298
// //////////////////////////////////////////////////////////////////
@@ -2446,6 +2451,7 @@ ValueIterator& ValueIterator::operator=(const SelfType& other) {
24462451
return *this;
24472452
}
24482453

2454+
JSONCPP_INLINE_NS_END
24492455
} // namespace Json
24502456

24512457
// //////////////////////////////////////////////////////////////////////
@@ -2490,6 +2496,7 @@ ValueIterator& ValueIterator::operator=(const SelfType& other) {
24902496
#define JSON_ASSERT_UNREACHABLE assert(false)
24912497

24922498
namespace Json {
2499+
JSONCPP_INLINE_NS_BEGIN
24932500

24942501
// This is a walkaround to avoid the static initialization of Value::null.
24952502
// kNull must be word-aligned to avoid crashing on ARM. We use an alignment of
@@ -2637,6 +2644,7 @@ static inline void releasePrefixedStringValue(char* value) { free(value); }
26372644
static inline void releaseStringValue(char* value, unsigned) { free(value); }
26382645
#endif // JSONCPP_USING_SECURE_MEMORY
26392646

2647+
JSONCPP_INLINE_NS_END
26402648
} // namespace Json
26412649

26422650
// //////////////////////////////////////////////////////////////////
@@ -2652,6 +2660,7 @@ static inline void releaseStringValue(char* value, unsigned) { free(value); }
26522660
#endif // if !defined(JSON_IS_AMALGAMATION)
26532661

26542662
namespace Json {
2663+
JSONCPP_INLINE_NS_BEGIN
26552664

26562665
// BN: subclass
26572666
Exception::Exception(JSONCPP_STRING const& msg) : JSONCPP_EXCEPTION(msg.c_str()) {}
@@ -4157,6 +4166,7 @@ Value& Path::make(Value& root) const {
41574166
return *node;
41584167
}
41594168

4169+
JSONCPP_INLINE_NS_END
41604170
} // namespace Json
41614171

41624172
// //////////////////////////////////////////////////////////////////////
@@ -4257,6 +4267,7 @@ Value& Path::make(Value& root) const {
42574267
#endif
42584268

42594269
namespace Json {
4270+
JSONCPP_INLINE_NS_BEGIN
42604271

42614272
#if __cplusplus >= 201103L || (defined(_CPPLIB_VER) && _CPPLIB_VER >= 520)
42624273
typedef std::unique_ptr<StreamWriter> StreamWriterPtr;
@@ -5434,6 +5445,7 @@ JSONCPP_OSTREAM& operator<<(JSONCPP_OSTREAM& sout, Value const& root) {
54345445
return sout;
54355446
}
54365447

5448+
JSONCPP_INLINE_NS_END
54375449
} // namespace Json
54385450

54395451
// //////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)