Skip to content

Commit 206a64f

Browse files
author
Ronan Chauvin
committed
1 parent 4878735 commit 206a64f

File tree

5 files changed

+374
-1
lines changed

5 files changed

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