From 9c70da3863626c480638959daa76a10a33c06332 Mon Sep 17 00:00:00 2001 From: Zhanhui Li Date: Mon, 7 Feb 2022 22:09:46 +0800 Subject: [PATCH 1/4] Support build with bazel. --- .bazelrc | 161 ++++++++++++++++++++++++++++++++ .github/workflows/bazel.yml | 31 ++++++ .gitignore | 9 +- BUILD.bazel | 26 ++++++ WORKSPACE | 5 + bazel/BUILD | 0 bazel/get_workspace_status | 36 +++++++ bazel/repository.bzl | 28 ++++++ cert.pem | 32 +++++++ examples/BUILD.bazel | 53 +++++++++++ examples/calculator/BUILD.bazel | 13 +++ examples/echo/BUILD.bazel | 13 +++ key.pem | 52 +++++++++++ tests/BUILD.bazel | 10 ++ tests/unit/BUILD.bazel | 108 +++++++++++++++++++++ tests/unit/cert.csr | 28 ++++++ third_party/BUILD | 0 third_party/asio.BUILD | 8 ++ 18 files changed, 612 insertions(+), 1 deletion(-) create mode 100644 .bazelrc create mode 100644 .github/workflows/bazel.yml create mode 100644 BUILD.bazel create mode 100644 WORKSPACE create mode 100644 bazel/BUILD create mode 100755 bazel/get_workspace_status create mode 100644 bazel/repository.bzl create mode 100644 cert.pem create mode 100644 examples/BUILD.bazel create mode 100644 examples/calculator/BUILD.bazel create mode 100644 examples/echo/BUILD.bazel create mode 100644 key.pem create mode 100644 tests/BUILD.bazel create mode 100644 tests/unit/BUILD.bazel create mode 100644 tests/unit/cert.csr create mode 100644 third_party/BUILD create mode 100644 third_party/asio.BUILD diff --git a/.bazelrc b/.bazelrc new file mode 100644 index 00000000..91aae0af --- /dev/null +++ b/.bazelrc @@ -0,0 +1,161 @@ +# Bazel doesn't need more than 200MB of memory for local build based on memory profiling: +# https://docs.bazel.build/versions/master/skylark/performance.html#memory-profiling +# The default JVM max heapsize is 1/4 of physical memory up to 32GB which could be large +# enough to consume all memory constrained by cgroup in large host. +# Limiting JVM heapsize here to let it do GC more when approaching the limit to +# leave room for compiler/linker. +# The number 2G is chosen heuristically to both support large VM and small VM with RBE. +# Startup options cannot be selected via config. +startup --host_jvm_args=-Xmx2g + +run --color=yes + +build --color=yes +build --workspace_status_command="bash bazel/get_workspace_status" +build --incompatible_strict_action_env +build --host_force_python=PY3 +build --java_runtime_version=remotejdk_11 +build --tool_java_runtime_version=remotejdk_11 + +build --enable_platform_specific_config + +# Allow tags to influence execution requirements +common --experimental_allow_tags_propagation + +# Enable position independent code (this is the default on macOS and Windows) +# (Workaround for https://github.com/bazelbuild/rules_foreign_cc/issues/421) +build:linux --copt=-fPIC +build:linux --copt=-Wno-deprecated-declarations +build:linux --cxxopt=-std=c++17 +build:linux --conlyopt=-fexceptions +build:linux --fission=dbg,opt +build:linux --features=per_object_debug_info +build:linux --action_env=BAZEL_LINKLIBS=-l%:libstdc++.a +build:linux --action_env=BAZEL_LINKOPTS=-lm + +# We already have absl in the build, define absl=1 to tell googletest to use absl for backtrace. +build --define absl=1 + +# Pass PATH, CC, CXX and LLVM_CONFIG variables from the environment. +build --action_env=CC +build --action_env=CXX +build --action_env=LLVM_CONFIG +build --action_env=PATH + +# Common flags for sanitizers +build:sanitizer --define tcmalloc=disabled +build:sanitizer --linkopt -ldl +build:sanitizer --build_tag_filters=-no_san +build:sanitizer --test_tag_filters=-no_san + +# Common flags for Clang +build:clang --action_env=BAZEL_COMPILER=clang +build:clang --action_env=CC=clang --action_env=CXX=clang++ +build:clang --linkopt=-fuse-ld=lld + +# Flags for Clang + PCH +build:clang-pch --spawn_strategy=local + +# Basic ASAN/UBSAN that works for gcc +build:asan --config=sanitizer +# ASAN install its signal handler, disable ours so the stacktrace will be printed by ASAN +build:asan --define signal_trace=disabled +build:asan --copt -fsanitize=address,undefined +build:asan --linkopt -fsanitize=address,undefined +# vptr and function sanitizer are enabled in clang-asan if it is set up via bazel/setup_clang.sh. +build:asan --copt -fno-sanitize=vptr,function +build:asan --linkopt -fno-sanitize=vptr,function +build:asan --copt -DADDRESS_SANITIZER=1 +build:asan --copt -D__SANITIZE_ADDRESS__ +build:asan --test_env=ASAN_OPTIONS=handle_abort=1:allow_addr2line=true:check_initialization_order=true:strict_init_order=true:detect_odr_violation=1 +build:asan --test_env=UBSAN_OPTIONS=halt_on_error=true:print_stacktrace=1 +build:asan --test_env=ASAN_SYMBOLIZER_PATH +# ASAN needs -O1 to get reasonable performance. +build:asan --copt -O1 +build:asan --copt -fno-optimize-sibling-calls + +# Clang ASAN/UBSAN +build:clang-asan --config=clang +build:clang-asan --config=asan +build:clang-asan --linkopt -fuse-ld=lld +build:clang-asan --linkopt --rtlib=compiler-rt +build:clang-asan --linkopt --unwindlib=libgcc + +# macOS +build:macos --cxxopt=-std=c++17 +build:macos --action_env=PATH=/usr/bin:/bin:/opt/homebrew/bin:/usr/local/bin:/opt/local/bin +build:macos --host_action_env=PATH=/usr/bin:/bin:/opt/homebrew/bin:/usr/local/bin:/opt/local/bin +build:macos --define tcmalloc=disabled + +# macOS ASAN/UBSAN +build:macos-asan --config=asan +# Workaround, see https://github.com/bazelbuild/bazel/issues/6932 +build:macos-asan --copt -Wno-macro-redefined +build:macos-asan --copt -D_FORTIFY_SOURCE=0 +# Workaround, see https://github.com/bazelbuild/bazel/issues/4341 +build:macos-asan --copt -DGRPC_BAZEL_BUILD +# Dynamic link cause issues like: `dyld: malformed mach-o: load commands size (59272) > 32768` +build:macos-asan --dynamic_mode=off + +# Clang TSAN +build:clang-tsan --config=sanitizer +build:clang-tsan --copt -fsanitize=thread +build:clang-tsan --linkopt -fsanitize=thread +build:clang-tsan --linkopt -fuse-ld=lld +build:clang-tsan --build_tag_filters=-no_san,-no_tsan +build:clang-tsan --test_tag_filters=-no_san,-no_tsan +# Needed due to https://github.com/libevent/libevent/issues/777 +build:clang-tsan --copt -DEVENT__DISABLE_DEBUG_MODE +# https://github.com/abseil/abseil-cpp/issues/760 +# https://github.com/google/sanitizers/issues/953 +build:clang-tsan --test_env="TSAN_OPTIONS=report_atomic_races=0" + +# Clang MSAN - this is the base config for remote-msan and docker-msan. To run this config without +# our build image, follow https://github.com/google/sanitizers/wiki/MemorySanitizerLibcxxHowTo +# with libc++ instruction and provide corresponding `--copt` and `--linkopt` as well. +build:clang-msan --config=sanitizer +build:clang-msan --copt -fsanitize=memory +build:clang-msan --linkopt -fsanitize=memory +build:clang-msan --linkopt -fuse-ld=lld +build:clang-msan --copt -fsanitize-memory-track-origins=2 +build:clang-msan --test_env=MSAN_SYMBOLIZER_PATH +# MSAN needs -O1 to get reasonable performance. +build:clang-msan --copt -O1 +build:clang-msan --copt -fno-optimize-sibling-calls + +# Clang with libc++ +build:libc++ --config=clang +build:libc++ --action_env=CXXFLAGS=-stdlib=libc++ +build:libc++ --action_env=LDFLAGS=-stdlib=libc++ +build:libc++ --action_env=BAZEL_CXXOPTS=-stdlib=libc++ +build:libc++ --action_env=BAZEL_LINKLIBS=-l%:libc++.a:-l%:libc++abi.a +build:libc++ --action_env=BAZEL_LINKOPTS=-lm:-pthread +build:libc++ --define force_libcpp=enabled + +# Optimize build for binary size reduction. +build:sizeopt -c opt --copt -Os + +# Test options +build --test_env=HEAPCHECK=normal --test_env=PPROF_PATH + +# Coverage options +coverage --config=coverage +coverage --build_tests_only +build:coverage --action_env=BAZEL_USE_LLVM_NATIVE_COVERAGE=1 +build:coverage --action_env=GCOV=llvm-profdata +build:coverage --copt=-DNDEBUG +# 1.5x original timeout + 300s for trace merger in all categories +build:coverage --test_timeout=390,750,1500,5700 +build:coverage --define=dynamic_link_tests=true +build:coverage --test_env=HEAPCHECK= +build:coverage --combined_report=lcov +build:coverage --strategy=TestRunner=sandboxed,local +build:coverage --strategy=CoverageReport=sandboxed,local +build:coverage --experimental_use_llvm_covmap +build:coverage --collect_code_coverage +build:coverage --test_tag_filters=-nocoverage +build:coverage --instrumentation_filter="//src[/:],//include[/:]" +build:test-coverage --test_arg="-l trace" +build:fuzz-coverage --config=plain-fuzzer + +test --enable_runfiles=True \ No newline at end of file diff --git a/.github/workflows/bazel.yml b/.github/workflows/bazel.yml new file mode 100644 index 00000000..3d4ea8db --- /dev/null +++ b/.github/workflows/bazel.yml @@ -0,0 +1,31 @@ +name: build-with-bazel + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch + push: + branches: + - master + - bazel + pull_request: + branches: + - master + - bazel + +jobs: + build: + runs-on: ubuntu-latest + timeout-minutes: 30 + + steps: + - uses: actions/checkout@v2 + + - name: Setup Bazel + run: | + sudo apt-get -qq install npm + sudo npm install -g @bazel/bazelisk + bazel -h + - name: Build All Targets + run: bazel build -c dbg //... + - name: Run All Tests + run: bazel test -c dbg //tests/... diff --git a/.gitignore b/.gitignore index f61e1407..e350ac59 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,11 @@ nbproject/ *.pdb *.exe *.ilk -*.lib \ No newline at end of file +*.lib +.cache +cmake-build-debug +compile_commands.json +bazel-bin +bazel-out +bazel-testlogs +bazel-NuRaft \ No newline at end of file diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 00000000..f39dc49e --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,26 @@ +load("@rules_cc//cc:defs.bzl", "cc_library") + +cc_library( + name = "api", + hdrs = glob(["include/**/*.hxx"]), + visibility = ["//visibility:public"], + strip_include_prefix = "//include/libnuraft", +) + +cc_library( + name = "nuraft", + hdrs = glob(["src/*.hxx"]) + glob(["src/*.h"]), + srcs = glob(["src/*.cxx"]), + strip_include_prefix = "//src", + deps = [ + ":api", + "@asio//:asio", + "@boringssl//:ssl", + ], + visibility = ["//visibility:public"], +) + +exports_files([ + "key.pem", + "cert.pem", +]) \ No newline at end of file diff --git a/WORKSPACE b/WORKSPACE new file mode 100644 index 00000000..5118395a --- /dev/null +++ b/WORKSPACE @@ -0,0 +1,5 @@ +workspace(name = "com_github_ebay_nuraft") + + +load("@com_github_ebay_nuraft//bazel:repository.bzl", "nuraft_deps") +nuraft_deps() \ No newline at end of file diff --git a/bazel/BUILD b/bazel/BUILD new file mode 100644 index 00000000..e69de29b diff --git a/bazel/get_workspace_status b/bazel/get_workspace_status new file mode 100755 index 00000000..0b28cf01 --- /dev/null +++ b/bazel/get_workspace_status @@ -0,0 +1,36 @@ +#!/bin/bash + +# This file was imported from https://github.com/bazelbuild/bazel at d6fec93. + +# This script will be run bazel when building process starts to +# generate key-value information that represents the status of the +# workspace. The output should be like +# +# KEY1 VALUE1 +# KEY2 VALUE2 +# +# If the script exits with non-zero code, it's considered as a failure +# and the output will be discarded. +# If this SOURCE_VERSION file exists then it must have been placed here by a +# distribution doing a non-git, source build. +# Distributions would be expected to echo the commit/tag as BUILD_SCM_REVISION +if [ -f SOURCE_VERSION ] +then + echo "BUILD_SCM_REVISION $(cat SOURCE_VERSION)" + echo "STABLE_BUILD_SCM_REVISION $(cat SOURCE_VERSION)" + echo "BUILD_SCM_STATUS Distribution" + exit 0 +fi + +# The code below presents an implementation that works for git repository +git_rev=$(git rev-parse HEAD) || exit 1 +echo "BUILD_SCM_REVISION ${git_rev}" +echo "STABLE_BUILD_SCM_REVISION ${git_rev}" + +# Check whether there are any uncommitted changes +tree_status="Clean" +git diff-index --quiet HEAD -- || { + tree_status="Modified" +} +echo "BUILD_SCM_STATUS ${tree_status}" +echo "STABLE_BUILD_SCM_STATUS ${tree_status}" diff --git a/bazel/repository.bzl b/bazel/repository.bzl new file mode 100644 index 00000000..8c90c0dc --- /dev/null +++ b/bazel/repository.bzl @@ -0,0 +1,28 @@ +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") + +def nuraft_deps(): + maybe( + http_archive, + name = "asio", + sha256 = "4cd5cd0ad97e752a4075f02778732a3737b587f5eeefab59cd98dc43b0dcadb3", + urls = [ + "https://shutian.oss-cn-hangzhou.aliyuncs.com/cdn/asio/asio-1.20.0.tar.gz", + ], + strip_prefix = "asio-1.20.0", + build_file = "@com_github_ebay_nuraft//third_party:asio.BUILD", + ) + + maybe( + http_archive, + name = "boringssl", + # Use github mirror instead of https://boringssl.googlesource.com/boringssl + # to obtain a boringssl archive with consistent sha256 + sha256 = "534fa658bd845fd974b50b10f444d392dfd0d93768c4a51b61263fd37d851c40", + strip_prefix = "boringssl-b9232f9e27e5668bc0414879dcdedb2a59ea75f2", + urls = [ + "https://shutian.oss-cn-hangzhou.aliyuncs.com/cdn/boringssl/boringssl-b9232f9e27e5668bc0414879dcdedb2a59ea75f2.tar.gz", + "https://storage.googleapis.com/grpc-bazel-mirror/github.com/google/boringssl/archive/b9232f9e27e5668bc0414879dcdedb2a59ea75f2.tar.gz", + "https://github.com/google/boringssl/archive/b9232f9e27e5668bc0414879dcdedb2a59ea75f2.tar.gz", + ], + ) \ No newline at end of file diff --git a/cert.pem b/cert.pem new file mode 100644 index 00000000..4b20c68f --- /dev/null +++ b/cert.pem @@ -0,0 +1,32 @@ +-----BEGIN CERTIFICATE----- +MIIFdTCCA12gAwIBAgIULIMoUtM/TIzRdVT5NUCDHoNac+AwDQYJKoZIhvcNAQEL +BQAwSjELMAkGA1UEBhMCQUIxCzAJBgNVBAgMAkNEMQwwCgYDVQQHDANFRkcxDDAK +BgNVBAoMA09SRzESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTIyMDIwODA2MjAyNloX +DTIzMDIwODA2MjAyNlowSjELMAkGA1UEBhMCQUIxCzAJBgNVBAgMAkNEMQwwCgYD +VQQHDANFRkcxDDAKBgNVBAoMA09SRzESMBAGA1UEAwwJbG9jYWxob3N0MIICIjAN +BgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAqDQc/RJn4nPWtLMdZ/53XIT8e6nQ +m/ydoK7RzlyR/gwAQMbBAsP1KnjTPeY6mdMwLkQ8Xas6SfwhtXM4hGIkoeA29S1J +5NhMBOr7joh4LZ0P9n1h1yLZC8zTioAKYOCVyq9Z1Of1mlqKFl6irDnmMxzLgCl2 +6a9boR0lnl/q5gpbEBI9rlWNhKFlRxzZ1GXZQXZZf0DfwZIrAvSDMoRt2oCUMdNT +iCUybq8r+USfa/dCjtxZUia8Zkb4kV1Zm1ATL8RojeDIBz/7R3uM44gVh4i8uGJM +qB/Rjj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YKGDUdfUJdU5wxGvC7j4Gsc+X +BhD+HCsWSOSIHL3MgJjz9lGldbUilHDySFAl+uDi/t9jjPDTz6OBHguRsSowQUxF +9Vg2gdEMzO78lDzrue9I1ylhcLYiyCAfxmV+T7ZtlztHzQ4Hw3A8Y/dTvfo0LrJ3 +KdIYJ7m7Ti/t +-----END CERTIFICATE----- diff --git a/examples/BUILD.bazel b/examples/BUILD.bazel new file mode 100644 index 00000000..22a56d88 --- /dev/null +++ b/examples/BUILD.bazel @@ -0,0 +1,53 @@ +load("@rules_cc//cc:defs.bzl", "cc_library") + +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "logger", + hdrs = [ + "backtrace.h", + "logger.h", + "logger_wrapper.hxx", + ], + srcs = [ + "logger.cc", + ], + strip_include_prefix = "//examples", +) + +cc_library( + name = "in_memory_log_store", + hdrs = [ + "in_memory_log_store.hxx", + ], + srcs = [ + "in_memory_log_store.cxx", + ], + deps = [ + ":logger", + "//:nuraft", + ], + strip_include_prefix = "//examples", +) + +cc_library( + name = "in_memory_state_mgr", + hdrs = [ + "in_memory_state_mgr.hxx", + ], + strip_include_prefix = "//examples", + deps = [ + ":in_memory_log_store", + ], +) + +cc_library( + name = "example_common", + hdrs = [ + "example_common.hxx", + ], + deps = [ + ":in_memory_state_mgr", + ], + strip_include_prefix = "//examples", +) \ No newline at end of file diff --git a/examples/calculator/BUILD.bazel b/examples/calculator/BUILD.bazel new file mode 100644 index 00000000..a496324d --- /dev/null +++ b/examples/calculator/BUILD.bazel @@ -0,0 +1,13 @@ +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "calculator", + srcs = [ + "calc_server.cxx", + "calc_state_machine.hxx", + ], + deps = [ + "//examples:example_common", + "//tests:test_common", + ], +) \ No newline at end of file diff --git a/examples/echo/BUILD.bazel b/examples/echo/BUILD.bazel new file mode 100644 index 00000000..47e8a26c --- /dev/null +++ b/examples/echo/BUILD.bazel @@ -0,0 +1,13 @@ +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "echo", + srcs = [ + "echo_server.cxx", + "echo_state_machine.hxx", + ], + deps = [ + "//examples:example_common", + "//tests:test_common", + ], +) \ No newline at end of file diff --git a/key.pem b/key.pem new file mode 100644 index 00000000..235b9ed7 --- /dev/null +++ b/key.pem @@ -0,0 +1,52 @@ +-----BEGIN PRIVATE KEY----- +MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQCoNBz9Emfic9a0 +sx1n/ndchPx7qdCb/J2grtHOXJH+DABAxsECw/UqeNM95jqZ0zAuRDxdqzpJ/CG1 +cziEYiSh4Db1LUnk2EwE6vuOiHgtnQ/2fWHXItkLzNOKgApg4JXKr1nU5/WaWooW +XqKsOeYzHMuAKXbpr1uhHSWeX+rmClsQEj2uVY2EoWVHHNnUZdlBdll/QN/BkisC +9IMyhG3agJQx01OIJTJuryv5RJ9r90KO3FlSJrxmRviRXVmbUBMvxGiN4MgHP/tH +e4zjiBWHiLy4YkyoH9GONY37aNqo5H7W/4kYUT1EZYO28ace2vhJDOH1GAk/BLrl +QZOKgTlL8fm/eo99B1LapuhR5oCnNtvplueYpJ8nEge6nj+YeIdeUOmFuAjsxGfq ++hEuobpCw23gEYpbFlAUefQNFbRdLRhAyBzGKbDVEbLV3jfnRxfYsc8hFBZBIDXU +1f9RbMAdq9GkoFB4DanJrDLa7bCqc/TONkQdpNCoELxdizEJ/o1CK1sni0TNgbS0 +VZCP8FsXiAaJCfZU8Knt5eqGPn+fA3OZ5iFS8CK3G8T0w/ap+gLAoYSsuTBIsx2A +GAaUIuPKjYM3B85YAYZFsrPNx19WXMia1g59yVJy/8JStE16cg6ZkJQDJmoRdJbL ++BHtMX6jUEkJcWyP0OF1L6THuzWzzQIDAQABAoICAEMPq/MWMIbvqk2XEAVTRiGD +/+oJ9VsLp4jVyl5dN/OyszMgSvmgSRHq5EXMkdachZwcpnit+OpgdMwUKRdWzcYg +T6kGqmxr9DsNuMfGDywYb7HVoXmVs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w9lYvmuPiITMap4BZhCUuO1LaNLJUcI8HSjOY9OxJKwQaO/qjmc +U+eWBOX0Dl+eii/6+ZKUts+EVMY890pNZXh4v6PEg0rH5vDpKjqJxzegZ3N+OC6S +6wLMJ8ZDnv1q3L3XvBU3bVawTEGvtCL6rteNDaQ2/WqzWJzbvZp2L4KSRxz0wMWb +Mybrd5xgmcDmx4DQiX9B/rUViagvJg== +-----END PRIVATE KEY----- diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel new file mode 100644 index 00000000..d1cb96ae --- /dev/null +++ b/tests/BUILD.bazel @@ -0,0 +1,10 @@ +load("@rules_cc//cc:defs.bzl", "cc_library") + +cc_library( + name = "test_common", + hdrs = [ + "test_common.h", + ], + visibility = ["//visibility:public"], + strip_include_prefix = "//tests", +) \ No newline at end of file diff --git a/tests/unit/BUILD.bazel b/tests/unit/BUILD.bazel new file mode 100644 index 00000000..f8674f68 --- /dev/null +++ b/tests/unit/BUILD.bazel @@ -0,0 +1,108 @@ +load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test") + +cc_library( + name = "unit_test_common", + hdrs = glob(["*.hxx"]), + srcs = [ + "fake_network.cxx", + ], + strip_include_prefix = "//tests/unit", + deps = [ + "//:nuraft", + "//examples:in_memory_state_mgr", + ], +) + +cc_test( + name = "asio_service_test", + srcs = [ + "asio_service_test.cxx", + ], + data = [ + "//:key.pem", + "//:cert.pem", + ], + deps = [ + ":unit_test_common", + "//tests:test_common", + ], + timeout = "long", + tags = ["manual"], +) + +cc_test( + name = "buffer_test", + srcs = [ + "buffer_test.cxx", + ], + deps = [ + "//tests:test_common", + "//:nuraft", + ], +) + +cc_test( + name = "failure_test", + srcs = [ + "failure_test.cxx", + ], + deps = [ + ":unit_test_common", + "//tests:test_common", + ], +) + +cc_test( + name = "raft_server_test", + srcs = [ + "raft_server_test.cxx", + ], + deps = [ + ":unit_test_common", + "//tests:test_common", + ], +) + +cc_test( + name = "serialization_test", + srcs = [ + "serialization_test.cxx", + ], + deps = [ + ":unit_test_common", + "//tests:test_common", + ], +) + +cc_test( + name = "stat_mgr_test", + srcs = [ + "stat_mgr_test.cxx", + ], + deps = [ + ":unit_test_common", + "//tests:test_common", + ], +) + +cc_test( + name = "strfmt_test", + srcs = [ + "strfmt_test.cxx", + ], + deps = [ + ":unit_test_common", + "//tests:test_common", + ], +) + +cc_test( + name = "timer_test", + srcs = [ + "timer_test.cxx", + ], + deps = [ + ":unit_test_common", + "//tests:test_common", + ], +) diff --git a/tests/unit/cert.csr b/tests/unit/cert.csr new file mode 100644 index 00000000..51dd1cd1 --- /dev/null +++ b/tests/unit/cert.csr @@ -0,0 +1,28 @@ +-----BEGIN CERTIFICATE REQUEST----- +MIIEtTCCAp0CAQAwcDELMAkGA1UEBhMCVVMxDzANBgNVBAgMBk9yZWdvbjERMA8G +A1UEBwwIUG9ydGxhbmQxFTATBgNVBAoMDENvbXBhbnkgTmFtZTEMMAoGA1UECwwD +T3JnMRgwFgYDVQQDDA93d3cuZXhhbXBsZS5jb20wggIiMA0GCSqGSIb3DQEBAQUA +A4ICDwAwggIKAoICAQDTPg82x1nnsB3RRpyilbVqWjHFOdhDBCAAErUbZIwncVFe +KE46+VRvA6HeM9IZBdFke6LUanzn1DHvbElGTjnxeVAAFMysyVy692a+y7FB6RdL +1XSD9QTtYP/GEVQZ0N4zUbCEkIC6jHoNiCLsJVifgpkbVGh9+u0YbG9XFUPKcr95 +VLw19RkrmvEPsT+NCt+qJlTMJPt7vtLJURpuPvXkAHjtBrwyZmzi7BHxcrWfDIfQ +He5TFKcyDyku9YDaolpyj8y3wYWL76i+a0Fnq6b5c9cMUkci8EadstE3UMrmsBfg +flQKkNMUdt9ZU0dSsxMNsngTMtdbJbkMIUkL0we5ndQ8ARMgv/QZDaQ67jzxo4n1 +KKT/zArVfi/HWFlkutVFx9t8AhcH/Zzb9cl+MbxJFK8f6M0Te7lFeyEW51SYB+sp +/ipwA0unOM9AoxhlrvO63HlqEEbYYjZIXhiKLLiQCc5Vab0FfRxZlN3HmtlzgH6B +zz0bDLVcc4FNM4MlgddBOtqq4036gCBTMT0cNHLJemarfea5pTeQ7mfrkWrawNub +5Dc0nTLxlPsbyJiL5dJynBirc/rtJNILaKS3pNp2pIH5kZ1AsbsdizbrsXp2dwTQ +Mgi53Mvy/JOCC+pM+eBa0vRjAD1UJ1QIRp+0PkpOghNzGxVJHPVXw/wHo30QVQID +AQABoAAwDQYJKoZIhvcNAQELBQADggIBAMbnA7q6EAiSddqtk3q9p/c1H2yZOLYY +dAnZ7pcX2VAfHxpARcZokKNlYkKnCXYQ0LOAYNko6phJY8MxjNpFqk8s5yyQueps +SVKPei2LFDvEk7sQPdMKZSR/dvvMEDS8XLWFYBneWifxtQG3L9KY2Hj6DInSDMXP +yUKfeStBCr/34KiuXeMib5ceq10+Mx3vZ6rHSjqPrj6WkWtVrsx0iC+zqyLitKYB +nngmxq1tNq79ZhZkJG6FJJsl5kHx9P1D6VfytlY3oCigTZIck6fxQyUtm26kSwi/ ++oCFBhs9jUDcG4XJUh+z80xXAqzkxnWW0jjSCrK8oz9BcWZRgzQPeEDnJoTPcL28 +1vCQkCAiUrIML/B7xfo4vEj5wf0DW8Sr2N7BDAvpKoP51U8uSQaL2u5QDKB0L7I8 +37EZptIMuO3YNEYJjIgTkI0jwNzF1jv4EczK9OVLUrr9Bud0bK0+90Xg+4HEERsi +QJIAUGEC1UH6hDYWr6W55bfc4f6eXAlAGrDSk7ff9oZK0FuUERuvLelAohCx+swH +RPg+WwRLQWSCQgdKz7tNPdWKT0lZ3KX5t/4otzjAongYNxErEnmT5AyBSvCNzs+2 +JJB/Kyzbf2h5oD0xwVnEo8ccT5tOcQOFZ/vRu7y9iVVp5gb2wD2HAPvBB2BlauFQ +s+kyBZMev1hG +-----END CERTIFICATE REQUEST----- diff --git a/third_party/BUILD b/third_party/BUILD new file mode 100644 index 00000000..e69de29b diff --git a/third_party/asio.BUILD b/third_party/asio.BUILD new file mode 100644 index 00000000..079c8503 --- /dev/null +++ b/third_party/asio.BUILD @@ -0,0 +1,8 @@ +load("@rules_cc//cc:defs.bzl", "cc_library") +cc_library( + name = "asio", + hdrs = glob(["include/**/*.hpp"]) + glob(["include/**/*.ipp"]), + defines = ["ASIO_STANDALONE"], + visibility = ["//visibility:public"], + strip_include_prefix = "//include", +) \ No newline at end of file From 5ea2135970ac1ace0ebfef2d197caf748075a8cb Mon Sep 17 00:00:00 2001 From: Li Zhanhui Date: Thu, 10 Feb 2022 19:21:12 +0800 Subject: [PATCH 2/4] Update README.md --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index d57de214..2395e494 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,25 @@ C:\NuRaft\build> c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\V ``` +Build with Bazel +---------- +#### 1. Install Bazel +Follow [install instructions](https://docs.bazel.build/versions/5.0.0/install.html) + +#### 2. Build static/shared libraries +```sh +bazel build -c opt //:nuraft +``` +If debug version is desirable, run +```sh +bazel build -c dbg //:nuraft +``` + +#### 3. Run unit tests +```sh +bazel test -c dbg //tests/... +``` + How to Use ---------- Please refer to [this document](./docs/how_to_use.md). From c35f08a862b5eb8e1f398ef34540a843b556d96c Mon Sep 17 00:00:00 2001 From: Li Zhanhui Date: Fri, 11 Feb 2022 10:32:11 +0800 Subject: [PATCH 3/4] Clarify bazel versions supported --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2395e494..1b247844 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,8 @@ C:\NuRaft\build> c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\V Build with Bazel ---------- #### 1. Install Bazel -Follow [install instructions](https://docs.bazel.build/versions/5.0.0/install.html) +Follow [install instructions](https://docs.bazel.build/versions/5.0.0/install.html). +LTS versions(4.2.2, 5.0.0) are verified compatible. Bazelisk is preferred as it helps [manage Bazel versions](https://docs.bazel.build/versions/main/updating-bazel.html#managing-bazel-versions-with-bazelisk). #### 2. Build static/shared libraries ```sh From 694f22c4863e7f9b64417c9c91a4d3b7a29a04de Mon Sep 17 00:00:00 2001 From: Li Zhanhui Date: Fri, 11 Feb 2022 10:46:21 +0800 Subject: [PATCH 4/4] Combine BUILD files --- examples/BUILD.bazel | 26 +++++++- examples/calculator/BUILD.bazel | 13 ---- examples/echo/BUILD.bazel | 13 ---- tests/BUILD.bazel | 111 +++++++++++++++++++++++++++++++- tests/unit/BUILD.bazel | 108 ------------------------------- 5 files changed, 134 insertions(+), 137 deletions(-) delete mode 100644 examples/calculator/BUILD.bazel delete mode 100644 examples/echo/BUILD.bazel delete mode 100644 tests/unit/BUILD.bazel diff --git a/examples/BUILD.bazel b/examples/BUILD.bazel index 22a56d88..4befdeae 100644 --- a/examples/BUILD.bazel +++ b/examples/BUILD.bazel @@ -1,4 +1,4 @@ -load("@rules_cc//cc:defs.bzl", "cc_library") +load("@rules_cc//cc:defs.bzl", "cc_library", "cc_binary") package(default_visibility = ["//visibility:public"]) @@ -50,4 +50,28 @@ cc_library( ":in_memory_state_mgr", ], strip_include_prefix = "//examples", +) + +cc_binary( + name = "echo", + srcs = [ + "echo/echo_server.cxx", + "echo/echo_state_machine.hxx", + ], + deps = [ + ":example_common", + "//tests:test_common", + ], +) + +cc_binary( + name = "calculator", + srcs = [ + "calculator/calc_server.cxx", + "calculator/calc_state_machine.hxx", + ], + deps = [ + ":example_common", + "//tests:test_common", + ], ) \ No newline at end of file diff --git a/examples/calculator/BUILD.bazel b/examples/calculator/BUILD.bazel deleted file mode 100644 index a496324d..00000000 --- a/examples/calculator/BUILD.bazel +++ /dev/null @@ -1,13 +0,0 @@ -load("@rules_cc//cc:defs.bzl", "cc_binary") - -cc_binary( - name = "calculator", - srcs = [ - "calc_server.cxx", - "calc_state_machine.hxx", - ], - deps = [ - "//examples:example_common", - "//tests:test_common", - ], -) \ No newline at end of file diff --git a/examples/echo/BUILD.bazel b/examples/echo/BUILD.bazel deleted file mode 100644 index 47e8a26c..00000000 --- a/examples/echo/BUILD.bazel +++ /dev/null @@ -1,13 +0,0 @@ -load("@rules_cc//cc:defs.bzl", "cc_binary") - -cc_binary( - name = "echo", - srcs = [ - "echo_server.cxx", - "echo_state_machine.hxx", - ], - deps = [ - "//examples:example_common", - "//tests:test_common", - ], -) \ No newline at end of file diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel index d1cb96ae..477a3675 100644 --- a/tests/BUILD.bazel +++ b/tests/BUILD.bazel @@ -1,4 +1,4 @@ -load("@rules_cc//cc:defs.bzl", "cc_library") +load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test") cc_library( name = "test_common", @@ -7,4 +7,111 @@ cc_library( ], visibility = ["//visibility:public"], strip_include_prefix = "//tests", -) \ No newline at end of file +) + +cc_library( + name = "unit_test_common", + hdrs = glob(["unit/*.hxx"]), + srcs = [ + "unit/fake_network.cxx", + ], + strip_include_prefix = "//tests/unit", + deps = [ + "//:nuraft", + "//examples:in_memory_state_mgr", + ], +) + +cc_test( + name = "asio_service_test", + srcs = [ + "unit/asio_service_test.cxx", + ], + data = [ + "//:key.pem", + "//:cert.pem", + ], + deps = [ + ":unit_test_common", + ":test_common", + ], + timeout = "long", + tags = ["manual"], +) + +cc_test( + name = "buffer_test", + srcs = [ + "unit/buffer_test.cxx", + ], + deps = [ + ":test_common", + "//:nuraft", + ], +) + +cc_test( + name = "failure_test", + srcs = [ + "unit/failure_test.cxx", + ], + deps = [ + ":unit_test_common", + ":test_common", + ], +) + +cc_test( + name = "raft_server_test", + srcs = [ + "unit/raft_server_test.cxx", + ], + deps = [ + ":unit_test_common", + ":test_common", + ], +) + +cc_test( + name = "serialization_test", + srcs = [ + "unit/serialization_test.cxx", + ], + deps = [ + ":unit_test_common", + ":test_common", + ], +) + +cc_test( + name = "stat_mgr_test", + srcs = [ + "unit/stat_mgr_test.cxx", + ], + deps = [ + ":unit_test_common", + ":test_common", + ], +) + +cc_test( + name = "strfmt_test", + srcs = [ + "unit/strfmt_test.cxx", + ], + deps = [ + ":unit_test_common", + ":test_common", + ], +) + +cc_test( + name = "timer_test", + srcs = [ + "unit/timer_test.cxx", + ], + deps = [ + ":unit_test_common", + ":test_common", + ], +) diff --git a/tests/unit/BUILD.bazel b/tests/unit/BUILD.bazel deleted file mode 100644 index f8674f68..00000000 --- a/tests/unit/BUILD.bazel +++ /dev/null @@ -1,108 +0,0 @@ -load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test") - -cc_library( - name = "unit_test_common", - hdrs = glob(["*.hxx"]), - srcs = [ - "fake_network.cxx", - ], - strip_include_prefix = "//tests/unit", - deps = [ - "//:nuraft", - "//examples:in_memory_state_mgr", - ], -) - -cc_test( - name = "asio_service_test", - srcs = [ - "asio_service_test.cxx", - ], - data = [ - "//:key.pem", - "//:cert.pem", - ], - deps = [ - ":unit_test_common", - "//tests:test_common", - ], - timeout = "long", - tags = ["manual"], -) - -cc_test( - name = "buffer_test", - srcs = [ - "buffer_test.cxx", - ], - deps = [ - "//tests:test_common", - "//:nuraft", - ], -) - -cc_test( - name = "failure_test", - srcs = [ - "failure_test.cxx", - ], - deps = [ - ":unit_test_common", - "//tests:test_common", - ], -) - -cc_test( - name = "raft_server_test", - srcs = [ - "raft_server_test.cxx", - ], - deps = [ - ":unit_test_common", - "//tests:test_common", - ], -) - -cc_test( - name = "serialization_test", - srcs = [ - "serialization_test.cxx", - ], - deps = [ - ":unit_test_common", - "//tests:test_common", - ], -) - -cc_test( - name = "stat_mgr_test", - srcs = [ - "stat_mgr_test.cxx", - ], - deps = [ - ":unit_test_common", - "//tests:test_common", - ], -) - -cc_test( - name = "strfmt_test", - srcs = [ - "strfmt_test.cxx", - ], - deps = [ - ":unit_test_common", - "//tests:test_common", - ], -) - -cc_test( - name = "timer_test", - srcs = [ - "timer_test.cxx", - ], - deps = [ - ":unit_test_common", - "//tests:test_common", - ], -)