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