Skip to content

Commit 1ecfc09

Browse files
committed
Expose JSON_USE_EXCEPTION and JSON_HAS_INT64 as Bazel config flags with defaults that match the existing Bazel build.
Switch //:jsoncpp from using copts ro defines for JSON_USE_EXCEPTION and JSON_HAS_INT64 so that rules that depend on it get the same config. Make src/test_lib_json/fuzz.cpp respect JSON_USE_EXCEPTION.
1 parent ca98c98 commit 1ecfc09

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

BUILD.bazel

+29-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
licenses(["unencumbered"]) # Public Domain or MIT
22

3+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
4+
35
exports_files(["LICENSE"])
46

7+
bool_flag(
8+
name = "use_exception",
9+
build_setting_default = False,
10+
)
11+
12+
config_setting(
13+
name = "use_exception_cfg",
14+
flag_values = {":use_exception": "true"},
15+
)
16+
17+
bool_flag(
18+
name = "has_int64",
19+
build_setting_default = True,
20+
)
21+
22+
config_setting(
23+
name = "has_int64_cfg",
24+
flag_values = {":has_int64": "true"},
25+
)
26+
527
cc_library(
628
name = "jsoncpp",
729
srcs = [
@@ -22,10 +44,13 @@ cc_library(
2244
"include/json/version.h",
2345
"include/json/writer.h",
2446
],
25-
copts = [
26-
"-DJSON_USE_EXCEPTION=0",
27-
"-DJSON_HAS_INT64",
28-
],
47+
defines = select({
48+
":use_exception_cfg": ["JSON_USE_EXCEPTION=1"],
49+
"//conditions:default": ["JSON_USE_EXCEPTION=0"],
50+
}) + select({
51+
":has_int64_cfg": ["JSON_HAS_INT64"],
52+
"//conditions:default": [],
53+
}),
2954
includes = ["include"],
3055
visibility = ["//visibility:public"],
3156
deps = [":private"],

MODULE.bazel

+5
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ module(
1212
version = "1.9.7",
1313
compatibility_level = 1,
1414
)
15+
16+
bazel_dep(
17+
name = "bazel_skylib",
18+
version = "1.7.1",
19+
)

src/test_lib_json/fuzz.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
#include <memory>
1212
#include <string>
1313

14-
namespace Json {
15-
class Exception;
16-
}
17-
1814
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
1915
Json::CharReaderBuilder builder;
2016

@@ -45,10 +41,14 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
4541

4642
Json::Value root;
4743
const auto data_str = reinterpret_cast<const char*>(data);
44+
#if JSON_USE_EXCEPTION
4845
try {
46+
#endif // JSON_USE_EXCEPTION
4947
reader->parse(data_str, data_str + size, &root, nullptr);
48+
#if JSON_USE_EXCEPTION
5049
} catch (Json::Exception const&) {
5150
}
51+
#endif // JSON_USE_EXCEPTION
5252
// Whether it succeeded or not doesn't matter.
5353
return 0;
5454
}

0 commit comments

Comments
 (0)