From 6e02dfc8bb808d0fb1e34f249304d8d8636bd4d1 Mon Sep 17 00:00:00 2001 From: Luke Aguilar Date: Tue, 1 Apr 2025 11:36:28 +1100 Subject: [PATCH 1/3] Added full Bazel support + tests + example Minor correct in speed test for newest Boost Property Tree functions --- .gitignore | 39 +++++++++++++++++++++++++++ BUILD.bazel | 18 +++++++++++++ MODULE.bazel | 21 +++++++++++++++ examples/address_book/BUILD.bazel | 12 +++++++++ tests/BUILD.bazel | 35 ++++++++++++++++++++++++ tests/misc/speed_test_boost.cpp | 2 +- tests/tools.bzl | 44 +++++++++++++++++++++++++++++++ 7 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 BUILD.bazel create mode 100644 MODULE.bazel create mode 100644 examples/address_book/BUILD.bazel create mode 100644 tests/BUILD.bazel create mode 100644 tests/tools.bzl diff --git a/.gitignore b/.gitignore index 98d9fdc..eb410f6 100644 --- a/.gitignore +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 0000000..731d837 --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,18 @@ +load("@rules_cc//cc:defs.bzl", "cc_library") + +cc_library( + name = "fastjson", + srcs = glob(["src/*.cpp"]), + hdrs = glob(["include/**/*.h"]), + copts = ["-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__"], +) diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 0000000..42489e1 --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,21 @@ +""" +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 = "1.0.0", + compatibility_level = 1, +) + +bazel_dep(name = "rules_cc", version = "0.1.1") + +bazel_dep(name = "boost.property_tree", version = "1.87.0", dev_dependency = True) +bazel_dep(name = "saru", version = "0.0.1", dev_dependency = True) +git_override( + module_name = "saru", + remote = "git@github.com:Quasiflo/saru.git", # TODO - Change to "git@github.com:mikeando/saru.git" once merged + branch = "feat/bazel", +) diff --git a/examples/address_book/BUILD.bazel b/examples/address_book/BUILD.bazel new file mode 100644 index 0000000..99f75c1 --- /dev/null +++ b/examples/address_book/BUILD.bazel @@ -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"], +) diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel new file mode 100644 index 0000000..0a2b128 --- /dev/null +++ b/tests/BUILD.bazel @@ -0,0 +1,35 @@ +load("@rules_cc//cc:defs.bzl", "cc_test") +load("tools.bzl", "test_set") + +TEST_COPTS = [ + "-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", + ], +) diff --git a/tests/misc/speed_test_boost.cpp b/tests/misc/speed_test_boost.cpp index 490cab5..11df3e2 100644 --- a/tests/misc/speed_test_boost.cpp +++ b/tests/misc/speed_test_boost.cpp @@ -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(); } diff --git a/tests/tools.bzl b/tests/tools.bzl new file mode 100644 index 0000000..af74519 --- /dev/null +++ b/tests/tools.bzl @@ -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 From e2ede2800cf86de66b42810f7c4c8b4ed53cbb19 Mon Sep 17 00:00:00 2001 From: Luke Aguilar Date: Tue, 1 Apr 2025 18:56:15 +1100 Subject: [PATCH 2/3] Fix version --- MODULE.bazel | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/MODULE.bazel b/MODULE.bazel index 42489e1..2284eee 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -6,16 +6,11 @@ Notes: boost.property_tree is used for the speed test unit test. saru is used as module( name = "fastjson", - version = "1.0.0", + version = "0.0.0-20120701063936-485f994a61a6", compatibility_level = 1, ) bazel_dep(name = "rules_cc", version = "0.1.1") bazel_dep(name = "boost.property_tree", version = "1.87.0", dev_dependency = True) -bazel_dep(name = "saru", version = "0.0.1", dev_dependency = True) -git_override( - module_name = "saru", - remote = "git@github.com:Quasiflo/saru.git", # TODO - Change to "git@github.com:mikeando/saru.git" once merged - branch = "feat/bazel", -) +bazel_dep(name = "saru", version = "0.0.0-20130617092049-c11c375fefd7", dev_dependency = True) From a194a5921f1fcabe31c7e80436a8bec4fef190ed Mon Sep 17 00:00:00 2001 From: Luke Aguilar Date: Wed, 2 Apr 2025 12:54:02 +1100 Subject: [PATCH 3/3] Fix copts --- BUILD.bazel | 7 ++++++- MODULE.bazel | 1 + tests/BUILD.bazel | 11 +++++++---- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index 731d837..d010ec4 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -4,7 +4,12 @@ cc_library( name = "fastjson", srcs = glob(["src/*.cpp"]), hdrs = glob(["include/**/*.h"]), - copts = ["-Wno-unused-variable"], # Ignore unused element - Don't pollute downstream users + 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"], ) diff --git a/MODULE.bazel b/MODULE.bazel index 2284eee..778f7fe 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -11,6 +11,7 @@ module( ) 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) diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel index 0a2b128..96554fa 100644 --- a/tests/BUILD.bazel +++ b/tests/BUILD.bazel @@ -1,10 +1,13 @@ load("@rules_cc//cc:defs.bzl", "cc_test") load("tools.bzl", "test_set") -TEST_COPTS = [ - "-g", # Debug symbols - "-Wall", # All warnings -] +TEST_COPTS = select({ + "@platforms//os:windows": [], + "//conditions:default": [ + "-g", # Debug symbols + "-Wall", # All warnings + ], +}) DEPS = [ "//:fastjson",