Skip to content

Commit 40d7f0c

Browse files
Ronan0912Ronan Chauvin
and
Ronan Chauvin
authored
Allow to be added as a system include using angle brackets. Co-authored-by: Ronan Chauvin <[email protected]>
1 parent bd60005 commit 40d7f0c

File tree

5 files changed

+375
-1
lines changed

5 files changed

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