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.
This commit is contained in:
bcsgh 2025-03-16 15:49:39 -07:00
parent ca98c98457
commit 1ecfc091f7
3 changed files with 38 additions and 8 deletions

View File

@ -1,7 +1,29 @@
licenses(["unencumbered"]) # Public Domain or MIT licenses(["unencumbered"]) # Public Domain or MIT
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
exports_files(["LICENSE"]) exports_files(["LICENSE"])
bool_flag(
name = "use_exception",
build_setting_default = False,
)
config_setting(
name = "use_exception_cfg",
flag_values = {":use_exception": "true"},
)
bool_flag(
name = "has_int64",
build_setting_default = True,
)
config_setting(
name = "has_int64_cfg",
flag_values = {":has_int64": "true"},
)
cc_library( cc_library(
name = "jsoncpp", name = "jsoncpp",
srcs = [ srcs = [
@ -22,10 +44,13 @@ cc_library(
"include/json/version.h", "include/json/version.h",
"include/json/writer.h", "include/json/writer.h",
], ],
copts = [ defines = select({
"-DJSON_USE_EXCEPTION=0", ":use_exception_cfg": ["JSON_USE_EXCEPTION=1"],
"-DJSON_HAS_INT64", "//conditions:default": ["JSON_USE_EXCEPTION=0"],
], }) + select({
":has_int64_cfg": ["JSON_HAS_INT64"],
"//conditions:default": [],
}),
includes = ["include"], includes = ["include"],
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
deps = [":private"], deps = [":private"],

View File

@ -12,3 +12,8 @@ module(
version = "1.9.7", version = "1.9.7",
compatibility_level = 1, compatibility_level = 1,
) )
bazel_dep(
name = "bazel_skylib",
version = "1.7.1",
)

View File

@ -11,10 +11,6 @@
#include <memory> #include <memory>
#include <string> #include <string>
namespace Json {
class Exception;
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
Json::CharReaderBuilder builder; Json::CharReaderBuilder builder;
@ -45,10 +41,14 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
Json::Value root; Json::Value root;
const auto data_str = reinterpret_cast<const char*>(data); const auto data_str = reinterpret_cast<const char*>(data);
#if JSON_USE_EXCEPTION
try { try {
#endif // JSON_USE_EXCEPTION
reader->parse(data_str, data_str + size, &root, nullptr); reader->parse(data_str, data_str + size, &root, nullptr);
#if JSON_USE_EXCEPTION
} catch (Json::Exception const&) { } catch (Json::Exception const&) {
} }
#endif // JSON_USE_EXCEPTION
// Whether it succeeded or not doesn't matter. // Whether it succeeded or not doesn't matter.
return 0; return 0;
} }