Skip to content

Commit

Permalink
Add Bazel as an Alternative Build System and Package Manager (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
hankhsu1996 authored Aug 5, 2024
1 parent 11ab23b commit 03a27bc
Show file tree
Hide file tree
Showing 13 changed files with 981 additions and 14 deletions.
8 changes: 8 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# .bazelrc

# Set the C++ standard to C++17
build --cxxopt='-std=c++17'

# Enable warnings
build --cxxopt='-Wall'
build --cxxopt='-Wextra'
24 changes: 23 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- main

jobs:
build-and-test:
build-and-test-cmake:
runs-on: ubuntu-latest

steps:
Expand Down Expand Up @@ -46,6 +46,28 @@ jobs:
- name: Run tests
run: ctest --preset release

build-and-test-bazel:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Bazel
uses: bazel-contrib/setup-bazel@0.8.5
with:
bazelisk-cache: true
disk-cache: ${{ github.workflow }}
repository-cache: true

- name: Build with Bazel
run: |
bazel build //...
- name: Test with Bazel
run: |
bazel test //...
docs:
runs-on: ubuntu-latest
container: hankhsu1996/doxygen:1.11.0
Expand Down
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ CTestTestfile.cmake
Makefile
build/

# Bazel output directories
bazel-*
*.runfiles/
*.log
*.err
*.out
*.runfiles_manifest
bazel-out/
bazel-bin/
bazel-testlogs/
bazel-genfiles/
bazel-*

# Doxygen generated files
docs/generated/

Expand Down
19 changes: 19 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""This is the MODULE.bazel file for the jsonrpc module."""

module(
name = "jsonrpc",
version = "1.0.0",
)

# Register the rules_foreign_cc and rules_cc dependencies
bazel_dep(name = "rules_foreign_cc", version = "0.11.1")
bazel_dep(name = "rules_cc", version = "0.0.9")

# Register the nlohmann_json dependency
bazel_dep(name = "nlohmann_json", version = "3.11.3")

# Register the spdlog dependency
bazel_dep(name = "spdlog", version = "1.14.1")

# Register the catch2 dependency
bazel_dep(name = "catch2", version = "3.6.0")
761 changes: 761 additions & 0 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

43 changes: 32 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Welcome to the **JSON-RPC 2.0 Modern C++ Library**! This library provides a ligh

## 🚀 Getting Started

To include this library in your project, you can use either CMake's FetchContent module or Conan 2.
To include this library in your project, you can use CMake's FetchContent, Conan 2, or Bazel.

### Using CMake FetchContent

Expand Down Expand Up @@ -44,7 +44,13 @@ CMakeToolchain

```

You can then install the dependencies using Conan 2 and configure your project with CMake.
### Using Bazel

Add the following to your `MODULE.bazel` file:

```bazel
bazel_dep(name = "jsonrpc", version = "1.0.0")
```

## 📖 Usage and Examples

Expand Down Expand Up @@ -103,9 +109,29 @@ These examples demonstrate the basic usage of setting up a JSON-RPC server and c

## 🛠️ Developer Guide

To build and test the project, follow these steps:
To build and test the project, follow these steps. Bazel is the preferred method.

### Option 1: Using Bazel (Preferred)

**Step 1: Build the Project**

Simply run:

```bash
bazel build //...
```

### Step 1: Install Dependencies
**Step 2: Run Tests**

To run all tests:

```bash
bazel test //...
```

### Option 2: Using CMake and Conan

**Step 1: Install Dependencies**

Ensure you have a Conan profile configured. If the default profile (`.conan2/profiles/default`) is missing, create it:

Expand All @@ -119,7 +145,7 @@ Next, install dependencies and generate `ConanPresets.json`:
conan install . --build=missing
```

### Step 2: Configure and Build the Project
**Step 2: Configure and Build the Project**

Use CMake presets to configure and build the project. Ensure CMake 3.19+ is installed.

Expand All @@ -130,7 +156,7 @@ cmake --preset release
cmake --build --preset release
```

### Step 3: Run Tests
**Step 3: Run Tests**

Run tests using the appropriate CMake preset:

Expand All @@ -149,11 +175,6 @@ cmake --build --preset debug
ctest --preset debug
```

### Additional Notes

- Execute commands from the top-level project directory.
- Presets handle configuration and build paths automatically.

## 🤝 Contributing

We welcome contributions! If you have suggestions or find any issues, feel free to open an issue or pull request.
Expand Down
9 changes: 9 additions & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "thread_pool",
build_file = "//third_party/thread_pool:BUILD.bazel",
sha256 = "be7abecbc420bb87919eeef729b13ff7c29d5ce547bdae284923296c695415bd",
strip_prefix = "thread-pool-4.1.0",
urls = ["https://github.com/bshoshany/thread-pool/archive/refs/tags/v4.1.0.tar.gz"],
)
16 changes: 16 additions & 0 deletions examples/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# examples/BUILD

cc_binary(
name = "stdio_client",
srcs = ["stdio_client.cpp"],
deps = ["//src:jsonrpc_lib"],
)

cc_binary(
name = "stdio_server",
srcs = [
"calculator.hpp",
"stdio_server.cpp",
],
deps = ["//src:jsonrpc_lib"],
)
5 changes: 5 additions & 0 deletions include/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
filegroup(
name = "jsonrpc_headers",
srcs = glob(["jsonrpc/**/*.hpp"]),
visibility = ["//visibility:public"],
)
13 changes: 13 additions & 0 deletions src/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cc_library(
name = "jsonrpc_lib",
srcs = glob(["**/*.cpp"]),
hdrs = ["//include:jsonrpc_headers"],
includes = ["../include"],
linkopts = ["-pthread"],
visibility = ["//visibility:public"],
deps = [
"@nlohmann_json//:json",
"@spdlog",
"@thread_pool",
],
)
74 changes: 74 additions & 0 deletions tests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# tests/BUILD

cc_test(
name = "test_client",
size = "small",
srcs = ["client/test_client.cpp"],
deps = [
"//src:jsonrpc_lib",
"@catch2//:catch2_main",
],
)

cc_test(
name = "test_stdio_client_transport",
size = "small",
srcs = [
"client/transports/test_stdio_client_transport.cpp",
"common/test_utilities.hpp",
],
deps = [
"//src:jsonrpc_lib",
"@catch2//:catch2_main",
],
)

cc_test(
name = "test_dispatcher",
size = "small",
srcs = ["server/test_dispatcher.cpp"],
deps = [
"//src:jsonrpc_lib",
"@catch2//:catch2_main",
],
)

cc_test(
name = "test_request",
size = "small",
srcs = ["server/test_request.cpp"],
deps = [
"//src:jsonrpc_lib",
"@catch2//:catch2_main",
],
)

cc_test(
name = "test_response",
size = "small",
srcs = ["server/test_response.cpp"],
deps = [
"//src:jsonrpc_lib",
"@catch2//:catch2_main",
],
)

cc_test(
name = "test_server",
size = "small",
srcs = ["server/test_server.cpp"],
deps = [
"//src:jsonrpc_lib",
"@catch2//:catch2_main",
],
)

cc_test(
name = "test_framed_transport",
size = "small",
srcs = ["common/transports/test_framed_transport.cpp"],
deps = [
"//src:jsonrpc_lib",
"@catch2//:catch2_main",
],
)
4 changes: 2 additions & 2 deletions tests/server/test_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ TEST_CASE("Server method registration", "[server]") {
Server server(std::move(transport));

MethodCallHandler methodHandler =
[](const std::optional<nlohmann::json> &params) -> nlohmann::json {
[](const std::optional<nlohmann::json> &) -> nlohmann::json {
return {{"result", "testMethod"}};
};

NotificationHandler notificationHandler =
[](const std::optional<nlohmann::json> &params) {};
[](const std::optional<nlohmann::json> &) {};

REQUIRE_NOTHROW(server.RegisterMethodCall("testMethod", methodHandler));
REQUIRE_NOTHROW(
Expand Down
6 changes: 6 additions & 0 deletions third_party/thread_pool/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cc_library(
name = "thread_pool",
hdrs = glob(["include/*.hpp"]),
includes = ["include"],
visibility = ["//visibility:public"],
)

0 comments on commit 03a27bc

Please sign in to comment.