Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,42 @@ saru????.log
*.swp
libfastjson.a
.sconsign.dblite

# Bazel
MODULE.bazel.lock
# Ignore backup files.
*~
# Ignore Vim swap files.
.*.swp
# macOS-specific excludes
.DS_Store
# Ignore files generated by IDEs.
/.aswb/
/.bazelbsp/
/.cache/
/.classpath
/.clwb/
/.factorypath
/.idea/
/.ijwb/
/.project
/.settings
/.vscode/
.eclipse/
.settings/
.classpath
.project
eclipse-*bin/
/bazel.iml
# Ignore all bazel-* symlinks. There is no full list since this can change
# based on the name of the directory bazel is cloned into.
/bazel-*
# Ignore outputs generated during Bazel bootstrapping.
/output/
# Ignore jekyll build output.
/production
/.sass-cache
# Bazelisk version file
.bazelversion
# User-specific .bazelrc
user.bazelrc
23 changes: 23 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
name = "fastjson",
srcs = glob(["src/*.cpp"]),
hdrs = glob(["include/**/*.h"]),
copts = select({
"@platforms//os:windows": [],
"//conditions:default": [
"-Wno-unused-variable", # Ignore unused element - Don't pollute downstream users
],
}),
includes = ["include"], # Expose for import without include prefix
visibility = ["//visibility:public"],
)

# This must be defined in top level BUILD as we can't access files above the target(s) file
cc_library(
name = "fastjson-test-srcs",
hdrs = glob(["src/*.cpp"]),
includes = ["include/fastjson"], # Tests need access to includes without fastjson folder prefix
visibility = ["//tests:__pkg__"],
)
17 changes: 17 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
Module: fastjson
Purpose: Provides the fastjson library compileable as a Bazel target. Includes unit testing through Bazel
Notes: boost.property_tree is used for the speed test unit test. saru is used as the unit testing framework
"""

module(
name = "fastjson",
version = "0.0.0-20120701063936-485f994a61a6",
compatibility_level = 1,
)

bazel_dep(name = "rules_cc", version = "0.1.1")
bazel_dep(name = "platforms", version = "0.0.11")

bazel_dep(name = "boost.property_tree", version = "1.87.0", dev_dependency = True)
bazel_dep(name = "saru", version = "0.0.0-20130617092049-c11c375fefd7", dev_dependency = True)
12 changes: 12 additions & 0 deletions examples/address_book/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
name = "address_book",
srcs = ["address_book.cpp"],
copts = [
"-g", # Debug symbols
"-Wall", # All warnings
],
data = ["book.json"], # Include the JSON file as a runtime dependency
deps = ["//:fastjson"],
)
38 changes: 38 additions & 0 deletions tests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
load("@rules_cc//cc:defs.bzl", "cc_test")
load("tools.bzl", "test_set")

TEST_COPTS = select({
"@platforms//os:windows": [],
"//conditions:default": [
"-g", # Debug symbols
"-Wall", # All warnings
],
})

DEPS = [
"//:fastjson",
"//:fastjson-test-srcs",
"@saru",
]

test_suite(
name = "all_tests",
tests = test_set(
copts = TEST_COPTS,
test_files = glob(
["**/*.cpp"],
exclude = ["misc/speed_test_boost.cpp"],
),
deps = DEPS,
) + [":speed_test"],
)

cc_test(
name = "speed_test",
size = "medium",
srcs = ["misc/speed_test_boost.cpp"],
copts = TEST_COPTS,
deps = DEPS + [
"@boost.property_tree",
],
)
2 changes: 1 addition & 1 deletion tests/misc/speed_test_boost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ int main()
boost::property_tree::read_json(ss, n );

std::stringstream ssout;
boost::property_tree::write_json_compact( ssout, n );
boost::property_tree::write_json( ssout, n );
std::string s = ssout.str();
}

Expand Down
44 changes: 44 additions & 0 deletions tests/tools.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Tool for generating C++ test targets efficiently using Bazel rules.
"""

load("@rules_cc//cc:defs.bzl", "cc_test")

def test_set(
test_files = None,
size = "small",
srcs = [],
file_extensions = ".cpp",
**kwargs):
"""Creates C++ test targets from a list of test files.

Args:
test_files: List of test file paths to process. Defaults to None.
size: Test size parameter for cc_test rule. Defaults to "small".
srcs: Additional source files to include in all tests. Defaults to empty list.
file_extensions: Expected extension of test files. Defaults to ".cpp".
**kwargs: Additional arguments to pass to cc_test rule.

Returns:
List of test target names (e.g., [":test1", ":test2"]).

Note:
Only files ending with the specified file_extensions are processed.
Each test target is created with the filename (without extension) as its name.
"""
test_targets = []

# Process positive tests
for file in test_files:
if not file.endswith(file_extensions):
continue
name = file[:-len(file_extensions)]
target = ":" + name
cc_test(
name = name,
size = size,
srcs = srcs + [file],
**kwargs
)
test_targets.append(target)

return test_targets