Skip to content

Commit d479261

Browse files
author
Ronan Chauvin
committed
1 parent 4878735 commit d479261

File tree

5 files changed

+369
-1
lines changed

5 files changed

+369
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Module: cjson
3+
Purpose: Provides the cjson library compileable as a Bazel target. Includes general and unity tests through Bazel
4+
Note: cjson_utils target is also available
5+
"""
6+
7+
module(
8+
name = "cjson",
9+
version = "1.7.19-0.20240923110858-12c4bf1986c2.bcr.2",
10+
compatibility_level = 1,
11+
)
12+
13+
bazel_dep(name = "rules_cc", version = "0.1.1")
14+
bazel_dep(name = "platforms", version = "0.0.11")
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
diff --git a/BUILD.bazel b/BUILD.bazel
2+
new file mode 100644
3+
index 0000000..60e3a76
4+
--- /dev/null
5+
+++ b/BUILD.bazel
6+
@@ -0,0 +1,83 @@
7+
+load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
8+
+
9+
+CJSON_COPTS = select({
10+
+ "@bazel_tools//src/conditions:darwin": [
11+
+ "-std=c89", # Standards compliance
12+
+ "-Wc++-compat", # C++ compatibility
13+
+ "-fvisibility=hidden", # Symbol visibility
14+
+ ],
15+
+ "@bazel_tools//src/conditions:linux": [
16+
+ "-std=c89", # Standards compliance
17+
+ "-Wc++-compat", # C++ compatibility
18+
+ "-fstack-protector-strong", # Security (GCC/Clang on Linux supports this)
19+
+ "-fvisibility=hidden", # Symbol visibility
20+
+ ],
21+
+ "@bazel_tools//src/conditions:windows": [
22+
+ "/Za", # Equivalent to -std=c89 (enforce ANSI C)
23+
+ "/W3", # Moderate warning level (instead of -Wc++-compat)
24+
+ "/GS", # Stack protection (equivalent to -fstack-protect-strong)
25+
+ ],
26+
+ "//conditions:default": [ # Used for baremetal
27+
+ "-std=c89", # Standards compliance
28+
+ "-Wc++-compat", # C++ compatibility
29+
+ ],
30+
+})
31+
+
32+
+cc_library(
33+
+ name = "cjson",
34+
+ srcs = ["cJSON.c"],
35+
+ hdrs = ["cJSON.h"],
36+
+ includes = ["cjson"],
37+
+ strip_include_prefix = ".",
38+
+ include_prefix = "cjson",
39+
+ copts = CJSON_COPTS + [
40+
+ "-DCJSON_EXPORT_SYMBOLS",
41+
+ "-DCJSON_API_VISIBILITY",
42+
+ ],
43+
+ linkopts = select({
44+
+ "@bazel_tools//src/conditions:windows": [],
45+
+ "//conditions:default": ["-lm"], # Link math library on non-Windows
46+
+ }),
47+
+ visibility = ["//visibility:public"],
48+
+)
49+
+
50+
+cc_library(
51+
+ name = "cjson_utils",
52+
+ srcs = ["cJSON_Utils.c"],
53+
+ hdrs = ["cJSON_Utils.h"],
54+
+ copts = CJSON_COPTS + [
55+
+ "-DCJSON_EXPORT_SYMBOLS",
56+
+ "-DCJSON_API_VISIBILITY",
57+
+ ],
58+
+ visibility = ["//visibility:public"],
59+
+ deps = [":cjson"],
60+
+)
61+
+
62+
+cc_library(
63+
+ name = "cjson_tests", # Some tests directly include the .c file
64+
+ hdrs = ["cJSON.c"],
65+
+ visibility = ["//tests:__pkg__"],
66+
+)
67+
+
68+
+cc_test(
69+
+ name = "cjson_test",
70+
+ size = "small",
71+
+ srcs = ["test.c"],
72+
+ copts = CJSON_COPTS + select({
73+
+ "@platforms//os:windows": [],
74+
+ "//conditions:default": [
75+
+ "-fno-sanitize=float-divide-by-zero",
76+
+ ],
77+
+ }),
78+
+ deps = [":cjson"],
79+
+)
80+
+
81+
+test_suite(
82+
+ name = "all_tests",
83+
+ tests = [
84+
+ ":cjson_test",
85+
+ "//tests:json_patch_tests",
86+
+ "//tests:parse_examples",
87+
+ "//tests:unity_tests",
88+
+ ],
89+
+)
90+
diff --git a/MODULE.bazel b/MODULE.bazel
91+
new file mode 100644
92+
index 0000000..06f600d
93+
--- /dev/null
94+
+++ b/MODULE.bazel
95+
@@ -0,0 +1,14 @@
96+
+"""
97+
+Module: cjson
98+
+Purpose: Provides the cjson library compileable as a Bazel target. Includes general and unity tests through Bazel
99+
+Note: cjson_utils target is also available
100+
+"""
101+
+
102+
+module(
103+
+ name = "cjson",
104+
+ version = "1.7.19-0.20240923110858-12c4bf1986c2.bcr.2",
105+
+ compatibility_level = 1,
106+
+)
107+
+
108+
+bazel_dep(name = "rules_cc", version = "0.1.1")
109+
+bazel_dep(name = "platforms", version = "0.0.11")
110+
diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel
111+
new file mode 100644
112+
index 0000000..baed64d
113+
--- /dev/null
114+
+++ b/tests/BUILD.bazel
115+
@@ -0,0 +1,70 @@
116+
+load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
117+
+load(":tools.bzl", "test_set")
118+
+
119+
+DEPS = [
120+
+ "//:cjson",
121+
+ "//:cjson_tests",
122+
+ "//:cjson_utils",
123+
+ "//tests/unity",
124+
+ "//tests/unity:unity-examples",
125+
+] + select({
126+
+ "@platforms//os:windows": [":unity-win"],
127+
+ "//conditions:default": [],
128+
+})
129+
+
130+
+DATA = [
131+
+ ":patch-test_inputs",
132+
+ ":test_inputs",
133+
+]
134+
+
135+
+HDRS = glob(["*.h"])
136+
+
137+
+cc_library(
138+
+ name = "unity-win",
139+
+ srcs = ["unity_setup.c"],
140+
+)
141+
+
142+
+filegroup(
143+
+ name = "test_inputs",
144+
+ srcs = glob(["inputs/*"]),
145+
+ visibility = ["//tests:__pkg__"],
146+
+)
147+
+
148+
+filegroup(
149+
+ name = "patch-test_inputs",
150+
+ srcs = glob(["json-patch-tests/*.json"]),
151+
+ visibility = ["//tests:__pkg__"],
152+
+)
153+
+
154+
+test_suite(
155+
+ name = "unity_tests",
156+
+ tests = test_set(
157+
+ srcs = HDRS,
158+
+ data = DATA,
159+
+ test_files = glob(
160+
+ ["*.c"],
161+
+ exclude = [
162+
+ "unity_setup.c",
163+
+ "parse_examples.c",
164+
+ "json_patch_tests.c",
165+
+ ],
166+
+ ),
167+
+ deps = DEPS,
168+
+ ),
169+
+)
170+
+
171+
+cc_test(
172+
+ name = "parse_examples",
173+
+ srcs = HDRS + ["parse_examples.c"],
174+
+ copts = ['-DTEST_DIR_PATH=\\"tests/inputs/\\"'],
175+
+ data = DATA,
176+
+ deps = DEPS,
177+
+)
178+
+
179+
+cc_test(
180+
+ name = "json_patch_tests",
181+
+ srcs = HDRS + ["json_patch_tests.c"],
182+
+ copts = ['-DTEST_DIR_PATH=\\"tests/json-patch-tests/\\"'],
183+
+ data = DATA,
184+
+ deps = DEPS,
185+
+)
186+
diff --git a/tests/json_patch_tests.c b/tests/json_patch_tests.c
187+
index 7c3d6ae..731da6e 100644
188+
--- a/tests/json_patch_tests.c
189+
+++ b/tests/json_patch_tests.c
190+
@@ -31,0 +32,4 @@
191+
+#ifndef TEST_DIR_PATH
192+
+#define TEST_DIR_PATH "json-patch-tests/"
193+
+#endif
194+
+
195+
@@ -185 +189 @@ static void cjson_utils_should_pass_json_patch_test_tests(void)
196+
- cJSON *tests = parse_test_file("json-patch-tests/tests.json");
197+
+ cJSON *tests = parse_test_file(TEST_DIR_PATH "tests.json");
198+
@@ -202 +206 @@ static void cjson_utils_should_pass_json_patch_test_spec_tests(void)
199+
- cJSON *tests = parse_test_file("json-patch-tests/spec_tests.json");
200+
+ cJSON *tests = parse_test_file(TEST_DIR_PATH "spec_tests.json");
201+
@@ -219 +223 @@ static void cjson_utils_should_pass_json_patch_test_cjson_utils_tests(void)
202+
- cJSON *tests = parse_test_file("json-patch-tests/cjson-utils-tests.json");
203+
+ cJSON *tests = parse_test_file(TEST_DIR_PATH "cjson-utils-tests.json");
204+
diff --git a/tests/parse_examples.c b/tests/parse_examples.c
205+
index d35d6cf..1e12e89 100644
206+
--- a/tests/parse_examples.c
207+
+++ b/tests/parse_examples.c
208+
@@ -60,0 +61 @@ static void do_test(const char *test_name)
209+
+#ifndef TEST_DIR_PATH
210+
@@ -61,0 +63 @@ static void do_test(const char *test_name)
211+
+#endif
212+
@@ -139 +141,6 @@ static void file_test6_should_not_be_parsed(void)
213+
- test6 = read_file("inputs/test6");
214+
+ char *test_path = NULL;
215+
+ const char * test_name = "test6";
216+
+ test_path = (char*)malloc(sizeof(TEST_DIR_PATH) + strlen(test_name));
217+
+ TEST_ASSERT_NOT_NULL_MESSAGE(test_path, "Failed to allocate test_path buffer.");
218+
+ sprintf(test_path, TEST_DIR_PATH"%s", test_name);
219+
+ test6 = read_file(test_path);
220+
diff --git a/tests/tools.bzl b/tests/tools.bzl
221+
new file mode 100644
222+
index 0000000..6098b6c
223+
--- /dev/null
224+
+++ b/tests/tools.bzl
225+
@@ -0,0 +1,44 @@
226+
+"""Tool for generating C++ test targets efficiently using Bazel rules.
227+
+"""
228+
+
229+
+load("@rules_cc//cc:defs.bzl", "cc_test")
230+
+
231+
+def test_set(
232+
+ test_files = None,
233+
+ size = "small",
234+
+ srcs = [],
235+
+ file_extensions = ".c",
236+
+ **kwargs):
237+
+ """Creates C++ test targets from a list of test files.
238+
+
239+
+ Args:
240+
+ test_files: List of test file paths to process. Defaults to None.
241+
+ size: Test size parameter for cc_test rule. Defaults to "small".
242+
+ srcs: Additional source files to include in all tests. Defaults to empty list.
243+
+ file_extensions: Expected extension of test files. Defaults to ".cpp".
244+
+ **kwargs: Additional arguments to pass to cc_test rule.
245+
+
246+
+ Returns:
247+
+ List of test target names (e.g., [":test1", ":test2"]).
248+
+
249+
+ Note:
250+
+ Only files ending with the specified file_extensions are processed.
251+
+ Each test target is created with the filename (without extension) as its name.
252+
+ """
253+
+ test_targets = []
254+
+
255+
+ # Process positive tests
256+
+ for file in test_files:
257+
+ if not file.endswith(file_extensions):
258+
+ continue
259+
+ name = file[:-len(file_extensions)]
260+
+ target = ":" + name
261+
+ cc_test(
262+
+ name = name,
263+
+ size = size,
264+
+ srcs = srcs + [file],
265+
+ **kwargs
266+
+ )
267+
+ test_targets.append(target)
268+
+
269+
+ return test_targets
270+
diff --git a/tests/unity/BUILD.bazel b/tests/unity/BUILD.bazel
271+
new file mode 100644
272+
index 0000000..e8c4d59
273+
--- /dev/null
274+
+++ b/tests/unity/BUILD.bazel
275+
@@ -0,0 +1,27 @@
276+
+load("@rules_cc//cc:defs.bzl", "cc_library")
277+
+
278+
+cc_library(
279+
+ name = "unity",
280+
+ srcs = glob(["src/*.c"]),
281+
+ hdrs = glob(["src/*.h"]),
282+
+ copts = select({
283+
+ "@platforms//os:windows": [
284+
+ "/wd4100", # Disable warning C4100: unreferenced formal parameter (similar to -Wno-error)
285+
+ "/wd4065", # Disable warning C4065: switch statement contains default but no case (similar to -Wno-switch-enum)
286+
+ ],
287+
+ "//conditions:default": [
288+
+ "-fvisibility=default",
289+
+ "-fno-sanitize=float-divide-by-zero",
290+
+ "-Wno-switch-enum",
291+
+ ],
292+
+ }),
293+
+ includes = ["src"],
294+
+ visibility = ["//tests:__pkg__"],
295+
+ deps = [":unity-examples"],
296+
+)
297+
+
298+
+cc_library(
299+
+ name = "unity-examples",
300+
+ hdrs = ["examples/unity_config.h"],
301+
+ visibility = ["//tests:__pkg__"],
302+
+)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
matrix:
2+
platform:
3+
- debian10
4+
- ubuntu2004
5+
- macos
6+
- macos_arm64
7+
- windows
8+
bazel:
9+
- 8.x
10+
- 7.x
11+
- 6.x
12+
tasks:
13+
verify_targets:
14+
name: Verify build targets
15+
platform: ${{ platform }}
16+
bazel: ${{ bazel }}
17+
build_targets:
18+
- "@cjson//:cjson"
19+
- "@cjson//:cjson_utils"
20+
21+
bcr_test_module:
22+
module_path: ""
23+
matrix:
24+
platform:
25+
- debian10
26+
- ubuntu2004
27+
- macos
28+
- macos_arm64
29+
- windows
30+
bazel:
31+
- 8.x
32+
- 7.x
33+
- 6.x
34+
tasks:
35+
run_test_module:
36+
name: "Run test module"
37+
platform: ${{ platform }}
38+
bazel: ${{ bazel }}
39+
test_targets:
40+
- "//:all_tests"
41+
test_flags:
42+
- "--enable_runfiles"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"url": "https://github.com/DaveGamble/cJSON/archive/12c4bf1.tar.gz",
3+
"integrity": "sha256-Hw5F/1wtymHoi7xHslN7ZP2LzrArSrvc2FpscTXkvXU=",
4+
"patches": {
5+
"diff.patch": "sha256-fREdDNcSMzr2j4zLRlCs8qpyKy+ExlPCcJYWIu1AGE0="
6+
},
7+
"patch_strip": 1,
8+
"strip_prefix": "cJSON-12c4bf1986c288950a3d06da757109a6aa1ece38"
9+
}

modules/cjson/metadata.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
],
1414
"versions": [
1515
"1.7.19-0.20240923110858-12c4bf1986c2",
16-
"1.7.19-0.20240923110858-12c4bf1986c2.bcr.1"
16+
"1.7.19-0.20240923110858-12c4bf1986c2.bcr.1",
17+
"1.7.19-0.20240923110858-12c4bf1986c2.bcr.2"
1718
],
1819
"yanked_versions": {}
1920
}

0 commit comments

Comments
 (0)