You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by ji...@apache.org on 2022/08/31 13:35:35 UTC

[incubator-brpc] branch master updated: Chore: rework Bazel build system

This is an automated email from the ASF dual-hosted git repository.

jiashunzhu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new ad200820 Chore: rework Bazel build system
     new 6457ad74 Merge pull request #1657 from hcoona/private/hcoona/improve-bazel-build
ad200820 is described below

commit ad20082010bcffa9204737656f5be64b7c6579ea
Author: Shuai Zhang <zh...@gmail.com>
AuthorDate: Tue Dec 28 22:21:09 2021 +0800

    Chore: rework Bazel build system
---
 .bazelrc                                           |  35 +-
 .bazelversion                                      |   1 +
 .travis.yml                                        |   2 +-
 BUILD.bazel                                        | 281 ++++++------
 WORKSPACE                                          | 289 ++++++++++--
 bazel/{brpc.bzl => BUILD.bazel}                    |  29 +-
 bazel/config/BUILD.bazel                           |  99 ++++
 .bazelrc => bazel/third_party/BUILD.bazel          |  13 +-
 .bazelrc => bazel/third_party/crc32c/BUILD.bazel   |  13 +-
 bazel/third_party/crc32c/crc32c.BUILD              |  93 ++++
 .bazelrc => bazel/third_party/event/BUILD.bazel    |  13 +-
 bazel/third_party/event/event.BUILD                |  59 +++
 .../glog/0001-mark-override-resolve-warning.patch  |  36 ++
 .bazelrc => bazel/third_party/glog/BUILD.bazel     |  13 +-
 .bazelrc => bazel/third_party/leveldb/BUILD.bazel  |  15 +-
 bazel/third_party/leveldb/leveldb.BUILD            |  72 +++
 bazel/third_party/leveldb/port.h                   |  34 ++
 bazel/third_party/leveldb/port_config.h            |  38 ++
 .bazelrc => bazel/third_party/openssl/BUILD.bazel  |  13 +-
 bazel/third_party/openssl/openssl.BUILD            | 165 +++++++
 .bazelrc => bazel/third_party/protobuf/BUILD.bazel |  13 +-
 bazel/third_party/protobuf/protobuf.BUILD          | 498 +++++++++++++++++++++
 .bazelrc => bazel/third_party/snappy/BUILD.bazel   |  13 +-
 bazel/third_party/snappy/snappy.BUILD              | 122 +++++
 .bazelrc => bazel/third_party/thrift/BUILD.bazel   |  13 +-
 bazel/third_party/thrift/thrift.BUILD              |  75 ++++
 .bazelrc => bazel/third_party/zlib/BUILD.bazel     |  13 +-
 bazel/third_party/zlib/zlib.BUILD                  | 111 +++++
 example/{BUILD => BUILD.bazel}                     |   9 +-
 .bazelrc => example/build_with_old_bazel/.bazelrc  |   8 -
 .../build_with_old_bazel/.bazelversion             |  10 +-
 .../build_with_old_bazel/BUILD.bazel               |  22 +-
 example/build_with_old_bazel/WORKSPACE             | 134 ++++++
 .../build_with_old_bazel/leveldb.BUILD             |   0
 .../build_with_old_bazel/openssl.BUILD             |  15 +
 .../build_with_old_bazel/zlib.BUILD                |  17 +-
 glog.BUILD                                         | 171 -------
 test/BUILD.bazel                                   | 123 ++---
 tools/BUILD                                        |   2 +-
 zlib.BUILD                                         |   8 -
 40 files changed, 2103 insertions(+), 587 deletions(-)

diff --git a/.bazelrc b/.bazelrc
index 8ae4f0d4..f3d406b0 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -13,12 +13,29 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-build --copt -DHAVE_ZLIB=1
-# bazel build with glog
-# build --define=with_glog=true
-build -c opt
-build --incompatible_disable_deprecated_attr_params=false
-build --incompatible_new_actions_api=false
-# unittest 
-test --define=unittest=true
-test --copt=-g
+#
+# Default build options. These are applied first and unconditionally.
+#
+
+build --cxxopt="-std=c++11"
+# Use gnu11 for asm keyword.
+build --conlyopt="-std=gnu11"
+
+# Enable position independent code (this is the default on macOS and Windows)
+# (Workaround for https://github.com/bazelbuild/rules_foreign_cc/issues/421)
+build --copt=-fPIC
+build --fission=dbg,opt
+build --features=per_object_debug_info
+
+# We already have absl in the build, define absl=1 to tell googletest to use absl for backtrace.
+build --define absl=1
+
+# For brpc.
+build --define=BRPC_WITH_GLOG=true
+test --define=BRPC_BUILD_FOR_UNITTEST=true
+
+# 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
diff --git a/.bazelversion b/.bazelversion
new file mode 100644
index 00000000..af8c8ec7
--- /dev/null
+++ b/.bazelversion
@@ -0,0 +1 @@
+4.2.2
diff --git a/.travis.yml b/.travis.yml
index 9200d960..96ada385 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -40,7 +40,7 @@ before_script:
 - sudo bash -c "echo 'core.%e.%p' > /proc/sys/kernel/core_pattern"
 
 before_install:
-- wget --no-clobber https://github.com/bazelbuild/bazel/releases/download/0.25.1/bazel_0.25.1-linux-x86_64.deb && sudo dpkg -i bazel_0.25.1-linux-x86_64.deb
+- wget --no-clobber https://github.com/bazelbuild/bazel/releases/download/4.2.2/bazel_4.2.2-linux-x86_64.deb && sudo dpkg -i bazel_4.2.2-linux-x86_64.deb
 - sudo apt-get install automake bison flex g++ git libboost-all-dev libevent-dev libssl-dev libtool make pkg-config # thrift dependencies
 - wget https://archive.apache.org/dist/thrift/0.11.0/thrift-0.11.0.tar.gz && tar -xf thrift-0.11.0.tar.gz && cd thrift-0.11.0/ && ./configure --prefix=/usr --with-rs=no --with-ruby=no --with-python=no --with-java=no --with-go=no --with-perl=no --with-php=no --with-csharp=no --with-erlang=no --with-lua=no --with-nodejs=no CXXFLAGS="-Wno-unused-variable" && make -j4 && sudo make install && cd -
 
diff --git a/BUILD.bazel b/BUILD.bazel
index 0cb60fa4..5e59bd1c 100644
--- a/BUILD.bazel
+++ b/BUILD.bazel
@@ -13,47 +13,13 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+load("@rules_proto//proto:defs.bzl", "proto_library")
+load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_proto_library", "objc_library")
+
 licenses(["notice"])  # Apache v2
 
 exports_files(["LICENSE"])
 
-load(":bazel/brpc.bzl", "brpc_proto_library")
-
-config_setting(
-    name = "with_glog",
-    define_values = {"with_glog": "true"},
-    visibility = ["//visibility:public"],
-)
-
-config_setting(
-    name = "with_mesalink",
-    define_values = {"with_mesalink": "true"},
-    visibility = ["//visibility:public"],
-)
-
-config_setting(
-    name = "with_thrift",
-    define_values = {"with_thrift": "true"},
-    visibility = ["//visibility:public"],
-)
-
-config_setting(
-    name = "unittest",
-    define_values = {"unittest": "true"},
-)
-
-config_setting(
-    name = "darwin",
-    values = {"cpu": "darwin"},
-    visibility = ["//visibility:public"],
-)
-
-config_setting(
-    name = "linux",
-    values = {"cpu": "linux"},
-    visibility = ["//visibility:public"],
-)
-
 COPTS = [
     "-DBTHREAD_USE_FAST_PTHREAD_MUTEX",
     "-D__const__=__unused__",
@@ -65,21 +31,24 @@ COPTS = [
     "-D__STDC_CONSTANT_MACROS",
     "-DGFLAGS_NS=google",
 ] + select({
-    ":with_glog": ["-DBRPC_WITH_GLOG=1"],
+    "//bazel/config:brpc_with_glog": ["-DBRPC_WITH_GLOG=1"],
     "//conditions:default": ["-DBRPC_WITH_GLOG=0"],
 }) + select({
-    ":with_mesalink": ["-DUSE_MESALINK"],
+    "//bazel/config:brpc_with_mesalink": ["-DUSE_MESALINK"],
     "//conditions:default": [""],
 }) + select({
-    ":with_thrift": ["-DENABLE_THRIFT_FRAMED_PROTOCOL=1"],
+    "//bazel/config:brpc_with_thrift": ["-DENABLE_THRIFT_FRAMED_PROTOCOL=1"],
     "//conditions:default": [""],
+}) + select({
+    "//bazel/config:brpc_with_thrift_legacy_version": [],
+    "//conditions:default": ["-DTHRIFT_STDCXX=std"],
 })
 
 LINKOPTS = [
-    "-lpthread",
+    "-pthread",
     "-ldl",
 ] + select({
-    ":darwin": [
+    "@bazel_tools//tools/osx:darwin": [
         "-framework CoreFoundation",
         "-framework CoreGraphics",
         "-framework CoreData",
@@ -92,19 +61,13 @@ LINKOPTS = [
         "-Wl,-U,_RegisterThriftProtocol",
     ],
     "//conditions:default": [
-      "-lrt",
+        "-lrt",
     ],
 }) + select({
-    ":with_mesalink": [
+    "//bazel/config:brpc_with_mesalink": [
         "-lmesalink",
     ],
     "//conditions:default": [],
-}) + select({
-    ":with_thrift": [
-        "-lthriftnb",
-        "-levent",
-        "-lthrift"],
-    "//conditions:default": [],
 })
 
 genrule(
@@ -116,18 +79,17 @@ genrule(
 // This file is auto-generated.
 #ifndef  BUTIL_CONFIG_H
 #define  BUTIL_CONFIG_H
-
 #ifdef BRPC_WITH_GLOG
 #undef BRPC_WITH_GLOG
 #endif
 #define BRPC_WITH_GLOG """ + select({
-    ":with_glog": "1",
-    "//conditions:default": "0",
-}) +
-"""
+              "//bazel/config:brpc_with_glog": "1",
+              "//conditions:default": "0",
+          }) +
+          """
 #endif  // BUTIL_CONFIG_H
 EOF
-    """
+    """,
 )
 
 BUTIL_SRCS = [
@@ -244,20 +206,20 @@ BUTIL_SRCS = [
     "src/butil/recordio.cc",
     "src/butil/popen.cpp",
 ] + select({
-        ":darwin": [
-            "src/butil/time/time_mac.cc",
-            "src/butil/mac/scoped_mach_port.cc",
-        ],
-        "//conditions:default": [
-            "src/butil/file_util_linux.cc",
-            "src/butil/threading/platform_thread_linux.cc",
-            "src/butil/strings/sys_string_conversions_posix.cc",
-        ],
+    "@bazel_tools//tools/osx:darwin": [
+        "src/butil/time/time_mac.cc",
+        "src/butil/mac/scoped_mach_port.cc",
+    ],
+    "//conditions:default": [
+        "src/butil/file_util_linux.cc",
+        "src/butil/threading/platform_thread_linux.cc",
+        "src/butil/strings/sys_string_conversions_posix.cc",
+    ],
 })
 
 objc_library(
     name = "macos_lib",
-    hdrs = [":config_h",
+    hdrs = [
         "src/butil/atomicops.h",
         "src/butil/atomicops_internals_atomicword_compat.h",
         "src/butil/atomicops_internals_mac.h",
@@ -269,10 +231,10 @@ objc_library(
         "src/butil/containers/hash_tables.h",
         "src/butil/debug/debugger.h",
         "src/butil/debug/leak_annotations.h",
-        "src/butil/file_util.h",
         "src/butil/file_descriptor_posix.h",
-        "src/butil/files/file_path.h",
+        "src/butil/file_util.h",
         "src/butil/files/file.h",
+        "src/butil/files/file_path.h",
         "src/butil/files/scoped_file.h",
         "src/butil/lazy_instance.h",
         "src/butil/logging.h",
@@ -294,15 +256,18 @@ objc_library(
         "src/butil/strings/string_util_posix.h",
         "src/butil/strings/sys_string_conversions.h",
         "src/butil/synchronization/lock.h",
-        "src/butil/time/time.h",
-        "src/butil/time.h",
         "src/butil/third_party/dynamic_annotations/dynamic_annotations.h",
+        "src/butil/third_party/murmurhash3/murmurhash3.h",
         "src/butil/threading/platform_thread.h",
-        "src/butil/threading/thread_restrictions.h",
         "src/butil/threading/thread_id_name_manager.h",
+        "src/butil/threading/thread_restrictions.h",
+        "src/butil/time.h",
+        "src/butil/time/time.h",
         "src/butil/type_traits.h",
-        "src/butil/third_party/murmurhash3/murmurhash3.h",
+        ":config_h",
     ],
+    enable_modules = True,
+    includes = ["src/"],
     non_arc_srcs = [
         "src/butil/mac/bundle_locations.mm",
         "src/butil/mac/foundation_util.mm",
@@ -310,15 +275,13 @@ objc_library(
         "src/butil/threading/platform_thread_mac.mm",
         "src/butil/strings/sys_string_conversions_mac.mm",
     ],
+    tags = ["manual"],
     deps = [
         "@com_github_gflags_gflags//:gflags",
     ] + select({
-        ":with_glog": ["@com_github_google_glog//:glog"],
+        "//bazel/config:brpc_with_glog": ["@com_github_google_glog//:glog"],
         "//conditions:default": [],
     }),
-    includes = ["src/"],
-    enable_modules = True,
-    tags = ["manual"],
 )
 
 cc_library(
@@ -330,48 +293,52 @@ cc_library(
         "src/butil/**/*.h",
         "src/butil/**/*.hpp",
         "src/butil/**/**/*.h",
-        "src/butil/**/**/*.hpp", "src/butil/**/**/**/*.h",
+        "src/butil/**/**/*.hpp",
+        "src/butil/**/**/**/*.h",
         "src/butil/**/**/**/*.hpp",
+    ]) + [
         "src/butil/third_party/dmg_fp/dtoa.cc",
-    ]) + [":config_h"],
-    deps = [
-        "@com_google_protobuf//:protobuf",
-        "@com_github_gflags_gflags//:gflags",
-        "@zlib//:zlib",
-    ] + select({
-        ":with_glog": ["@com_github_google_glog//:glog"],
-        "//conditions:default": [],
-    }) + select({
-        ":darwin": [":macos_lib"],
-        "//conditions:default": [],
-    }) + select({
-        ":darwin": ["//external:ssl_macos"],
-        "//conditions:default": ["//external:ssl"],
-    }),
-    includes = [
-        "src/",
+        ":config_h",
     ],
     copts = COPTS + select({
-        ":unittest": [
+        "//bazel/config:brpc_build_for_unittest": [
             "-DBVAR_NOT_LINK_DEFAULT_VARIABLES",
             "-DUNIT_TEST",
         ],
         "//conditions:default": [],
     }),
+    includes = [
+        "src/",
+    ],
     linkopts = LINKOPTS,
     visibility = ["//visibility:public"],
+    deps = [
+        "@com_github_gflags_gflags//:gflags",
+        "@com_github_madler_zlib//:zlib",
+        "@com_google_protobuf//:protobuf",
+        "@openssl//:crypto",
+        "@openssl//:ssl",
+    ] + select({
+        "//bazel/config:brpc_with_glog": ["@com_github_google_glog//:glog"],
+        "//conditions:default": [],
+    }) + select({
+        "@bazel_tools//tools/osx:darwin": [":macos_lib"],
+        "//conditions:default": [],
+    }),
 )
 
 cc_library(
     name = "bvar",
-    srcs = glob([
-        "src/bvar/*.cpp",
-        "src/bvar/detail/*.cpp",
-    ],
-    exclude = [
-        "src/bvar/default_variables.cpp",
-    ]) + select({
-        ":unittest": [],
+    srcs = glob(
+        [
+            "src/bvar/*.cpp",
+            "src/bvar/detail/*.cpp",
+        ],
+        exclude = [
+            "src/bvar/default_variables.cpp",
+        ],
+    ) + select({
+        "//bazel/config:brpc_build_for_unittest": [],
         "//conditions:default": ["src/bvar/default_variables.cpp"],
     }),
     hdrs = glob([
@@ -379,21 +346,21 @@ cc_library(
         "src/bvar/utils/*.h",
         "src/bvar/detail/*.h",
     ]),
-    includes = [
-        "src/",
-    ],
-    deps = [
-        ":butil",
-    ],
     copts = COPTS + select({
-        ":unittest": [
+        "//bazel/config:brpc_build_for_unittest": [
             "-DBVAR_NOT_LINK_DEFAULT_VARIABLES",
             "-DUNIT_TEST",
         ],
         "//conditions:default": [],
     }),
+    includes = [
+        "src/",
+    ],
     linkopts = LINKOPTS,
     visibility = ["//visibility:public"],
+    deps = [
+        ":butil",
+    ],
 )
 
 cc_library(
@@ -405,16 +372,16 @@ cc_library(
         "src/bthread/*.h",
         "src/bthread/*.list",
     ]),
+    copts = COPTS,
     includes = [
-        "src/"
+        "src/",
     ],
+    linkopts = LINKOPTS,
+    visibility = ["//visibility:public"],
     deps = [
         ":butil",
         ":bvar",
     ],
-    copts = COPTS,
-    linkopts = LINKOPTS,
-    visibility = ["//visibility:public"],
 )
 
 cc_library(
@@ -425,15 +392,15 @@ cc_library(
     hdrs = glob([
         "src/json2pb/*.h",
     ]),
+    copts = COPTS,
     includes = [
         "src/",
     ],
+    linkopts = LINKOPTS,
+    visibility = ["//visibility:public"],
     deps = [
         ":butil",
     ],
-    copts = COPTS,
-    linkopts = LINKOPTS,
-    visibility = ["//visibility:public"],
 )
 
 cc_library(
@@ -447,42 +414,66 @@ cc_library(
     hdrs = glob([
         "src/mcpack2pb/*.h",
     ]),
+    copts = COPTS,
     includes = [
         "src/",
     ],
+    linkopts = LINKOPTS,
+    visibility = ["//visibility:public"],
     deps = [
+        ":brpc_idl_options_cc_proto",
         ":butil",
-        ":cc_brpc_idl_options_proto",
         "@com_google_protobuf//:protoc_lib",
     ],
-    copts = COPTS,
-    linkopts = LINKOPTS,
-    visibility = ["//visibility:public"],
 )
 
-brpc_proto_library(
-    name = "cc_brpc_idl_options_proto",
+filegroup(
+    name = "brpc_idl_options_proto_srcs",
     srcs = [
         "src/idl_options.proto",
     ],
+    visibility = ["//visibility:public"],
+)
+
+proto_library(
+    name = "brpc_idl_options_proto",
+    srcs = [":brpc_idl_options_proto_srcs"],
+    visibility = ["//visibility:public"],
     deps = [
-        "@com_google_protobuf//:cc_wkt_protos"
+        "@com_google_protobuf//:descriptor_proto",
     ],
+)
+
+cc_proto_library(
+    name = "brpc_idl_options_cc_proto",
     visibility = ["//visibility:public"],
+    deps = [":brpc_idl_options_proto"],
 )
 
-brpc_proto_library(
-    name = "cc_brpc_internal_proto",
+filegroup(
+    name = "brpc_internal_proto_srcs",
     srcs = glob([
         "src/brpc/*.proto",
         "src/brpc/policy/*.proto",
     ]),
-    include = "src/",
+    visibility = ["//visibility:public"],
+)
+
+proto_library(
+    name = "brpc_internal_proto",
+    srcs = [":brpc_internal_proto_srcs"],
+    strip_import_prefix = "src",
+    visibility = ["//visibility:public"],
     deps = [
-        ":cc_brpc_idl_options_proto",
-        "@com_google_protobuf//:cc_wkt_protos"
+        ":brpc_idl_options_proto",
+        "@com_google_protobuf//:descriptor_proto",
     ],
+)
+
+cc_proto_library(
+    name = "brpc_internal_cc_proto",
     visibility = ["//visibility:public"],
+    deps = [":brpc_internal_proto"],
 )
 
 cc_library(
@@ -498,10 +489,11 @@ cc_library(
         "src/brpc/event_dispatcher_epoll.cpp",
         "src/brpc/event_dispatcher_kqueue.cpp",
     ]) + select({
-        ":with_thrift" : glob([
+        "//bazel/config:brpc_with_thrift": glob([
             "src/brpc/thrift*.cpp",
-            "src/brpc/**/thrift*.cpp"]),
-        "//conditions:default" : [],
+            "src/brpc/**/thrift*.cpp",
+        ]),
+        "//conditions:default": [],
     }),
     hdrs = glob([
         "src/brpc/*.h",
@@ -509,21 +501,26 @@ cc_library(
         "src/brpc/event_dispatcher_epoll.cpp",
         "src/brpc/event_dispatcher_kqueue.cpp",
     ]),
+    copts = COPTS,
     includes = [
         "src/",
     ],
+    linkopts = LINKOPTS,
+    visibility = ["//visibility:public"],
     deps = [
-        ":butil",
+        ":brpc_internal_cc_proto",
         ":bthread",
+        ":butil",
         ":bvar",
         ":json2pb",
         ":mcpack2pb",
-        ":cc_brpc_internal_proto",
         "@com_github_google_leveldb//:leveldb",
-    ],
-    copts = COPTS,
-    linkopts = LINKOPTS,
-    visibility = ["//visibility:public"],
+    ] + select({
+        "//bazel/config:brpc_with_thrift": [
+            "@org_apache_thrift//:thrift",
+        ],
+        "//conditions:default": [],
+    }),
 )
 
 cc_binary(
@@ -531,11 +528,11 @@ cc_binary(
     srcs = [
         "src/mcpack2pb/generator.cpp",
     ],
-    deps = [
-        ":cc_brpc_idl_options_proto",
-        ":brpc",
-    ],
     copts = COPTS,
     linkopts = LINKOPTS,
     visibility = ["//visibility:public"],
+    deps = [
+        ":brpc",
+        ":brpc_idl_options_cc_proto",
+    ],
 )
diff --git a/WORKSPACE b/WORKSPACE
index 441fd16d..9ad4ada1 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -19,77 +19,278 @@ workspace(name = "com_github_brpc_brpc")
 
 load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
 
-skylib_version = "0.8.0"
+#
+# Constants
+#
+
+BAZEL_IO_VERSION = "4.2.2"  # 2021-12-03T09:26:35Z
+
+BAZEL_IO_SHA256 = "4c179ce66bbfff6ac5d81b8895518096e7f750866d08da2d4a574d1b8029e914"
+
+BAZEL_SKYLIB_VERSION = "1.1.1"  # 2021-09-27T17:33:49Z
+
+BAZEL_SKYLIB_SHA256 = "c6966ec828da198c5d9adbaa94c05e3a1c7f21bd012a0b29ba8ddbccb2c93b0d"
+
+BAZEL_PLATFORMS_VERSION = "0.0.4"  # 2021-02-26
+
+BAZEL_PLATFORMS_SHA256 = "079945598e4b6cc075846f7fd6a9d0857c33a7afc0de868c2ccb96405225135d"
+
+RULES_PROTO_TAG = "4.0.0"  # 2021-09-15T14:13:21Z
+
+RULES_PROTO_SHA256 = "66bfdf8782796239d3875d37e7de19b1d94301e8972b3cbd2446b332429b4df1"
+
+RULES_CC_COMMIT_ID = "0913abc3be0edff60af681c0473518f51fb9eeef"  # 2021-08-12T14:14:28Z
+
+RULES_CC_SHA256 = "04d22a8c6f0caab1466ff9ae8577dbd12a0c7d0bc468425b75de094ec68ab4f9"
+
+#
+# Starlark libraries
+#
+
+http_archive(
+    name = "io_bazel",
+    sha256 = BAZEL_IO_SHA256,
+    strip_prefix = "bazel-" + BAZEL_IO_VERSION,
+    url = "https://github.com/bazelbuild/bazel/archive/" + BAZEL_IO_VERSION + ".zip",
+)
+
 http_archive(
     name = "bazel_skylib",
-    type = "tar.gz",
-    url = "https://github.com/bazelbuild/bazel-skylib/releases/download/{}/bazel-skylib.{}.tar.gz".format (skylib_version, skylib_version),
-    sha256 = "2ef429f5d7ce7111263289644d233707dba35e39696377ebab8b0bc701f7818e",
+    sha256 = BAZEL_SKYLIB_SHA256,
+    urls = [
+        "https://github.com/bazelbuild/bazel-skylib/releases/download/{version}/bazel-skylib-{version}.tar.gz".format(version = BAZEL_SKYLIB_VERSION),
+        "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/{version}/bazel-skylib-{version}.tar.gz".format(version = BAZEL_SKYLIB_VERSION),
+    ],
+)
+
+http_archive(
+    name = "platforms",
+    sha256 = BAZEL_PLATFORMS_SHA256,
+    urls = [
+        "https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/{version}/platforms-{version}.tar.gz".format(version = BAZEL_PLATFORMS_VERSION),
+        "https://github.com/bazelbuild/platforms/releases/download/{version}/platforms-{version}.tar.gz".format(version = BAZEL_PLATFORMS_VERSION),
+    ],
+)
+
+http_archive(
+    name = "rules_proto",
+    sha256 = RULES_PROTO_SHA256,
+    strip_prefix = "rules_proto-{version}".format(version = RULES_PROTO_TAG),
+    urls = ["https://github.com/bazelbuild/rules_proto/archive/refs/tags/{version}.tar.gz".format(version = RULES_PROTO_TAG)],
+)
+
+http_archive(
+    name = "rules_cc",
+    sha256 = RULES_CC_SHA256,
+    strip_prefix = "rules_cc-{commit_id}".format(commit_id = RULES_CC_COMMIT_ID),
+    urls = [
+        "https://github.com/bazelbuild/rules_cc/archive/{commit_id}.tar.gz".format(commit_id = RULES_CC_COMMIT_ID),
+    ],
+)
+
+http_archive(
+    name = "rules_perl",  # 2021-09-23T03:21:58Z
+    sha256 = "55fbe071971772758ad669615fc9aac9b126db6ae45909f0f36de499f6201dd3",
+    strip_prefix = "rules_perl-2f4f36f454375e678e81e5ca465d4d497c5c02da",
+    urls = [
+        "https://github.com/bazelbuild/rules_perl/archive/2f4f36f454375e678e81e5ca465d4d497c5c02da.tar.gz",
+    ],
+)
+
+# Use rules_foreign_cc as fewer as possible.
+#
+# 1. Build very basic libraries without any further dependencies.
+# 2. Build too complex to bazelize library.
+http_archive(
+    name = "rules_foreign_cc",  # 2021-12-03T17:15:40Z
+    sha256 = "1df78c7d7eed2dc21b8b325a2853c31933a81e7b780f9a59a5d078be9008b13a",
+    strip_prefix = "rules_foreign_cc-0.7.0",
+    url = "https://github.com/bazelbuild/rules_foreign_cc/archive/0.7.0.tar.gz",
+)
+
+#
+# Starlark rules
+#
+
+load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")
+
+rules_foreign_cc_dependencies(register_preinstalled_tools = False)
+
+#
+# C++ Dependencies
+#
+# Ordered lexicographical.
+#
+
+http_archive(
+    name = "boost",  # 2021-08-05T01:30:05Z
+    build_file = "@com_github_nelhage_rules_boost//:BUILD.boost",
+    patch_cmds = ["rm -f doc/pdf/BUILD"],
+    patch_cmds_win = ["Remove-Item -Force doc/pdf/BUILD"],
+    sha256 = "5347464af5b14ac54bb945dc68f1dd7c56f0dad7262816b956138fc53bcc0131",
+    strip_prefix = "boost_1_77_0",
+    urls = [
+        "https://boostorg.jfrog.io/artifactory/main/release/1.77.0/source/boost_1_77_0.tar.gz",
+    ],
+)
+
+http_archive(
+    name = "com_github_gflags_gflags",  # 2018-11-11T21:30:10Z
+    sha256 = "34af2f15cf7367513b352bdcd2493ab14ce43692d2dcd9dfc499492966c64dcf",
+    strip_prefix = "gflags-2.2.2",
+    urls = ["https://github.com/gflags/gflags/archive/v2.2.2.tar.gz"],
+)
+
+http_archive(
+    name = "com_github_google_crc32c",  # 2021-10-05T19:47:30Z
+    build_file = "//bazel/third_party/crc32c:crc32c.BUILD",
+    sha256 = "ac07840513072b7fcebda6e821068aa04889018f24e10e46181068fb214d7e56",
+    strip_prefix = "crc32c-1.1.2",
+    urls = ["https://github.com/google/crc32c/archive/1.1.2.tar.gz"],
+)
+
+http_archive(
+    name = "com_github_google_glog",  # 2021-05-07T23:06:39Z
+    patch_args = ["-p1"],
+    patches = [
+        "//bazel/third_party/glog:0001-mark-override-resolve-warning.patch",
+    ],
+    sha256 = "21bc744fb7f2fa701ee8db339ded7dce4f975d0d55837a97be7d46e8382dea5a",
+    strip_prefix = "glog-0.5.0",
+    urls = ["https://github.com/google/glog/archive/v0.5.0.zip"],
 )
 
 http_archive(
-  name = "com_google_protobuf",
-  strip_prefix = "protobuf-3.6.1.3",
-  sha256 = "9510dd2afc29e7245e9e884336f848c8a6600a14ae726adb6befdb4f786f0be2",
-  type = "zip",
-  url = "https://github.com/protocolbuffers/protobuf/archive/v3.6.1.3.zip",
+    name = "com_github_google_leveldb",  # 2021-02-23T21:51:12Z
+    build_file = "//bazel/third_party/leveldb:leveldb.BUILD",
+    sha256 = "9a37f8a6174f09bd622bc723b55881dc541cd50747cbd08831c2a82d620f6d76",
+    strip_prefix = "leveldb-1.23",
+    urls = [
+        "https://github.com/google/leveldb/archive/refs/tags/1.23.tar.gz",
+    ],
 )
 
 http_archive(
-    name = "com_github_gflags_gflags",
-    strip_prefix = "gflags-46f73f88b18aee341538c0dfc22b1710a6abedef",
-    url = "https://github.com/gflags/gflags/archive/46f73f88b18aee341538c0dfc22b1710a6abedef.tar.gz",
+    name = "com_github_google_snappy",  # 2017-08-25
+    build_file = "//bazel/third_party/snappy:snappy.BUILD",
+    sha256 = "3dfa02e873ff51a11ee02b9ca391807f0c8ea0529a4924afa645fbf97163f9d4",
+    strip_prefix = "snappy-1.1.7",
+    urls = [
+        "https://storage.googleapis.com/mirror.tensorflow.org/github.com/google/snappy/archive/1.1.7.tar.gz",
+        "https://github.com/google/snappy/archive/1.1.7.tar.gz",
+    ],
 )
 
-bind(
-    name = "gflags",
-    actual = "@com_github_gflags_gflags//:gflags",
+http_archive(
+    name = "com_github_libevent_libevent",  # 2020-07-05T13:33:03Z
+    build_file = "//bazel/third_party/event:event.BUILD",
+    sha256 = "92e6de1be9ec176428fd2367677e61ceffc2ee1cb119035037a27d346b0403bb",
+    strip_prefix = "libevent-2.1.12-stable",
+    urls = [
+        "https://github.com/libevent/libevent/releases/download/release-2.1.12-stable/libevent-2.1.12-stable.tar.gz",
+    ],
 )
 
+# TODO: SIMD optimization.
+# https://github.com/cloudflare/zlib
 http_archive(
-    name = "com_github_google_leveldb",
-    build_file = "//:leveldb.BUILD",
-    strip_prefix = "leveldb-a53934a3ae1244679f812d998a4f16f2c7f309a6",
-    url = "https://github.com/google/leveldb/archive/a53934a3ae1244679f812d998a4f16f2c7f309a6.tar.gz"
+    name = "com_github_madler_zlib",  # 2017-01-15T17:57:23Z
+    build_file = "//bazel/third_party/zlib:zlib.BUILD",
+    sha256 = "c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1",
+    strip_prefix = "zlib-1.2.11",
+    urls = [
+        "https://downloads.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz",
+        "https://zlib.net/fossils/zlib-1.2.11.tar.gz",
+    ],
 )
 
 http_archive(
-    name = "com_github_google_glog",
-    build_file = "//:glog.BUILD",
-    strip_prefix = "glog-a6a166db069520dbbd653c97c2e5b12e08a8bb26",
-    url = "https://github.com/google/glog/archive/a6a166db069520dbbd653c97c2e5b12e08a8bb26.tar.gz"
+    name = "com_github_nelhage_rules_boost",  # 2021-08-27T15:46:06Z
+    patch_cmds = ["sed -i 's/net_zlib_zlib/com_github_madler_zlib/g' BUILD.boost"],
+    patch_cmds_win = [
+        """$content = (Get-Content 'BUILD.boost') -replace "net_zlib_zlib", "com_github_madler_zlib"
+Set-Content BUILD.boost -Value $content -Encoding UTF8
+""",
+    ],
+    sha256 = "2d0b2eef7137730dbbb180397fe9c3d601f8f25950c43222cb3ee85256a21869",
+    strip_prefix = "rules_boost-fce83babe3f6287bccb45d2df013a309fa3194b8",
+    urls = [
+        "https://github.com/nelhage/rules_boost/archive/fce83babe3f6287bccb45d2df013a309fa3194b8.tar.gz",
+    ],
 )
 
 http_archive(
-    name = "com_google_googletest",
-    strip_prefix = "googletest-0fe96607d85cf3a25ac40da369db62bbee2939a5",
-    url = "https://github.com/google/googletest/archive/0fe96607d85cf3a25ac40da369db62bbee2939a5.tar.gz",
+    name = "com_google_absl",  # 2021-09-27T18:06:52Z
+    sha256 = "2f0d9c7bc770f32bda06a9548f537b63602987d5a173791485151aba28a90099",
+    strip_prefix = "abseil-cpp-7143e49e74857a009e16c51f6076eb197b6ccb49",
+    urls = ["https://github.com/abseil/abseil-cpp/archive/7143e49e74857a009e16c51f6076eb197b6ccb49.zip"],
 )
 
-new_local_repository(
-    name = "openssl",
-    path = "/usr",
-    build_file = "//:openssl.BUILD",
+http_archive(
+    name = "com_google_googletest",  # 2021-07-09T13:28:13Z
+    sha256 = "12ef65654dc01ab40f6f33f9d02c04f2097d2cd9fbe48dc6001b29543583b0ad",
+    strip_prefix = "googletest-8d51ffdfab10b3fba636ae69bc03da4b54f8c235",
+    urls = ["https://github.com/google/googletest/archive/8d51ffdfab10b3fba636ae69bc03da4b54f8c235.zip"],
 )
 
-new_local_repository(
-    name = "openssl_macos",
-    build_file = "//:openssl.BUILD",
-    path = "/usr/local/opt/openssl",
+http_archive(
+    name = "com_google_protobuf",  # 2021-10-29T00:04:02Z
+    build_file = "//bazel/third_party/protobuf:protobuf.BUILD",
+    patch_cmds = [
+        "sed -i protobuf.bzl -re '4,4d;417,508d'",
+    ],
+    patch_cmds_win = [
+        """$content = Get-Content 'protobuf.bzl' | Where-Object {
+    -not ($_.ReadCount -ne 4) -and
+    -not ($_.ReadCount -ge 418 -and $_.ReadCount -le 509)
+}
+Set-Content protobuf.bzl -Value $content -Encoding UTF8
+""",
+    ],
+    sha256 = "87407cd28e7a9c95d9f61a098a53cf031109d451a7763e7dd1253abf8b4df422",
+    strip_prefix = "protobuf-3.19.1",
+    urls = ["https://github.com/protocolbuffers/protobuf/archive/refs/tags/v3.19.1.tar.gz"],
 )
 
-bind(
-    name = "ssl",
-    actual = "@openssl//:ssl"
+# bRPC cannot use boringssl. Build openssl.
+http_archive(
+    name = "openssl",  # 2021-12-14T15:45:01Z
+    build_file = "//bazel/third_party/openssl:openssl.BUILD",
+    sha256 = "f89199be8b23ca45fc7cb9f1d8d3ee67312318286ad030f5316aca6462db6c96",
+    strip_prefix = "openssl-1.1.1m",
+    urls = [
+        "https://www.openssl.org/source/openssl-1.1.1m.tar.gz",
+        "https://github.com/openssl/openssl/archive/OpenSSL_1_1_1m.tar.gz",
+    ],
 )
 
-bind(
-    name = "ssl_macos",
-    actual = "@openssl_macos//:ssl"
+http_archive(
+    name = "org_apache_thrift",  # 2021-09-11T11:54:01Z
+    build_file = "//bazel/third_party/thrift:thrift.BUILD",
+    sha256 = "d5883566d161f8f6ddd4e21f3a9e3e6b8272799d054820f1c25b11e86718f86b",
+    strip_prefix = "thrift-0.15.0",
+    urls = ["https://archive.apache.org/dist/thrift/0.15.0/thrift-0.15.0.tar.gz"],
 )
 
-new_local_repository(
-    name = "zlib",
-    build_file = "//:zlib.BUILD",
-    path = "/usr",
+#
+# Perl Dependencies
+#
+
+load("@rules_perl//perl:deps.bzl", "perl_register_toolchains")
+
+perl_register_toolchains()
+
+#
+# Tools Dependencies
+#
+
+http_archive(
+    name = "com_grail_bazel_compdb",
+    sha256 = "d32835b26dd35aad8fd0ba0d712265df6565a3ad860d39e4c01ad41059ea7eda",
+    strip_prefix = "bazel-compilation-database-0.5.2",
+    urls = ["https://github.com/grailbio/bazel-compilation-database/archive/0.5.2.tar.gz"],
 )
+
+load("@com_grail_bazel_compdb//:deps.bzl", "bazel_compdb_deps")
+
+bazel_compdb_deps()
diff --git a/bazel/brpc.bzl b/bazel/BUILD.bazel
similarity index 52%
rename from bazel/brpc.bzl
rename to bazel/BUILD.bazel
index 137c1ffa..5100f2ee 100644
--- a/bazel/brpc.bzl
+++ b/bazel/BUILD.bazel
@@ -13,19 +13,18 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-load("@com_google_protobuf//:protobuf.bzl", "cc_proto_library")
-
-def brpc_proto_library(name, srcs, deps=[], include=None, visibility=None, testonly=0):
-    native.filegroup(name=name + "_proto_srcs",
-                     srcs=srcs,
-                     visibility=visibility,)
-    cc_proto_library(name=name,
-                     srcs=srcs,
-                     deps=deps,
-                     cc_libs=["@com_google_protobuf//:protobuf"],
-                     include=include,
-                     protoc="@com_google_protobuf//:protoc",
-                     default_runtime="@com_google_protobuf//:protobuf",
-                     testonly=testonly,
-                     visibility=visibility,)
+load("@com_grail_bazel_compdb//:defs.bzl", "compilation_database")
+load("@com_grail_bazel_output_base_util//:defs.bzl", "OUTPUT_BASE")
 
+compilation_database(
+    name = "brpc_compdb",
+    # OUTPUT_BASE is a dynamic value that will vary for each user workspace.
+    # If you would like your build outputs to be the same across users, then
+    # skip supplying this value, and substitute the default constant value
+    # "__OUTPUT_BASE__" through an external tool like `sed` or `jq` (see
+    # below shell commands for usage).
+    output_base = OUTPUT_BASE,
+    targets = [
+        "//:brpc",
+    ],
+)
diff --git a/bazel/config/BUILD.bazel b/bazel/config/BUILD.bazel
new file mode 100644
index 00000000..4471bad1
--- /dev/null
+++ b/bazel/config/BUILD.bazel
@@ -0,0 +1,99 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+load("@bazel_skylib//lib:selects.bzl", "selects")
+
+licenses(["notice"])  # Apache v2
+
+selects.config_setting_group(
+    name = "brpc_with_glog",
+    match_any = [
+        ":brpc_with_glog_deprecated_flag",
+        ":brpc_with_glog_new_flag",
+    ],
+    visibility = ["//visibility:public"],
+)
+
+config_setting(
+    name = "brpc_with_glog_deprecated_flag",
+    define_values = {"with_glog": "true"},
+)
+
+config_setting(
+    name = "brpc_with_glog_new_flag",
+    define_values = {"BRPC_WITH_GLOG": "true"},
+)
+
+selects.config_setting_group(
+    name = "brpc_with_mesalink",
+    match_any = [
+        ":brpc_with_mesalink_deprecated_flag",
+        ":brpc_with_mesalink_new_flag",
+    ],
+    visibility = ["//visibility:public"],
+)
+
+config_setting(
+    name = "brpc_with_mesalink_deprecated_flag",
+    define_values = {"with_mesalink": "true"},
+)
+
+config_setting(
+    name = "brpc_with_mesalink_new_flag",
+    define_values = {"BRPC_WITH_MESALINK": "true"},
+)
+
+selects.config_setting_group(
+    name = "brpc_with_thrift",
+    match_any = [
+        ":brpc_with_thrift_deprecated_flag",
+        ":brpc_with_thrift_new_flag",
+    ],
+    visibility = ["//visibility:public"],
+)
+
+config_setting(
+    name = "brpc_with_thrift_legacy_version",
+    define_values = {"BRPC_WITH_THRIFT_LEGACY_VERSION": "true"},
+    visibility = ["//visibility:public"],
+)
+
+config_setting(
+    name = "brpc_with_thrift_deprecated_flag",
+    define_values = {"with_thrift": "true"},
+)
+
+config_setting(
+    name = "brpc_with_thrift_new_flag",
+    define_values = {"BRPC_WITH_THRIFT": "true"},
+)
+
+config_setting(
+    name = "brpc_build_for_unittest",
+    define_values = {"BRPC_BUILD_FOR_UNITTEST": "true"},
+    visibility = ["//visibility:public"],
+)
+
+config_setting(
+    name = "brpc_with_sse42",
+    define_values = {"BRPC_WITH_SSE42": "true"},
+    visibility = ["//visibility:public"],
+)
+
+config_setting(
+    name = "darwin",
+    values = {"cpu": "darwin"},
+    visibility = ["//:__subpkgs__"],
+)
diff --git a/.bazelrc b/bazel/third_party/BUILD.bazel
similarity index 75%
copy from .bazelrc
copy to bazel/third_party/BUILD.bazel
index 8ae4f0d4..fefa6c3f 100644
--- a/.bazelrc
+++ b/bazel/third_party/BUILD.bazel
@@ -12,13 +12,6 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
-
-build --copt -DHAVE_ZLIB=1
-# bazel build with glog
-# build --define=with_glog=true
-build -c opt
-build --incompatible_disable_deprecated_attr_params=false
-build --incompatible_new_actions_api=false
-# unittest 
-test --define=unittest=true
-test --copt=-g
+#
+# Thie empty BUILD.bazel file is required to make Bazel treat
+# this directory as a package.
diff --git a/.bazelrc b/bazel/third_party/crc32c/BUILD.bazel
similarity index 75%
copy from .bazelrc
copy to bazel/third_party/crc32c/BUILD.bazel
index 8ae4f0d4..fefa6c3f 100644
--- a/.bazelrc
+++ b/bazel/third_party/crc32c/BUILD.bazel
@@ -12,13 +12,6 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
-
-build --copt -DHAVE_ZLIB=1
-# bazel build with glog
-# build --define=with_glog=true
-build -c opt
-build --incompatible_disable_deprecated_attr_params=false
-build --incompatible_new_actions_api=false
-# unittest 
-test --define=unittest=true
-test --copt=-g
+#
+# Thie empty BUILD.bazel file is required to make Bazel treat
+# this directory as a package.
diff --git a/bazel/third_party/crc32c/crc32c.BUILD b/bazel/third_party/crc32c/crc32c.BUILD
new file mode 100644
index 00000000..72715d48
--- /dev/null
+++ b/bazel/third_party/crc32c/crc32c.BUILD
@@ -0,0 +1,93 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
+
+genrule(
+    name = "crc32c_config_h",
+    srcs = ["src/crc32c_config.h.in"],
+    outs = ["crc32c/crc32c_config.h"],
+    cmd = """
+sed -e 's/#cmakedefine01/#define/' \
+""" + select({
+        "@//bazel/config:brpc_with_sse42": """-e 's/ HAVE_SSE42/ HAVE_SSE42 1/' \
+""",
+        "//conditions:default": """-e 's/ HAVE_SSE42/ HAVE_SSE42 0/' \
+""",
+    }) + select({
+        "@//bazel/config:brpc_with_glog": """-e 's/ CRC32C_TESTS_BUILT_WITH_GLOG/ CRC32C_TESTS_BUILT_WITH_GLOG 1/' \
+""",
+        "//conditions:default": """-e 's/ CRC32C_TESTS_BUILT_WITH_GLOG/ CRC32C_TESTS_BUILT_WITH_GLOG 0/' \
+""",
+    }) + """-e 's/ BYTE_ORDER_BIG_ENDIAN/ BYTE_ORDER_BIG_ENDIAN 0/' \
+    -e 's/ HAVE_BUILTIN_PREFETCH/ HAVE_BUILTIN_PREFETCH 0/' \
+    -e 's/ HAVE_MM_PREFETCH/ HAVE_MM_PREFETCH 0/' \
+    -e 's/ HAVE_ARM64_CRC32C/ HAVE_ARM64_CRC32C 0/' \
+    -e 's/ HAVE_STRONG_GETAUXVAL/ HAVE_STRONG_GETAUXVAL 0/' \
+    -e 's/ HAVE_WEAK_GETAUXVAL/ HAVE_WEAK_GETAUXVAL 0/' \
+    < $< > $@
+""",
+)
+
+cc_library(
+    name = "crc32c",
+    srcs = [
+        "src/crc32c.cc",
+        "src/crc32c_arm64.cc",
+        "src/crc32c_arm64.h",
+        "src/crc32c_arm64_check.h",
+        "src/crc32c_internal.h",
+        "src/crc32c_portable.cc",
+        "src/crc32c_prefetch.h",
+        "src/crc32c_read_le.h",
+        "src/crc32c_round_up.h",
+        "src/crc32c_sse42.cc",
+        "src/crc32c_sse42.h",
+        "src/crc32c_sse42_check.h",
+        ":crc32c_config_h",
+    ],
+    hdrs = [
+        "include/crc32c/crc32c.h",
+    ],
+    copts = select({
+        "@//bazel/config:brpc_with_sse42": ["-msse4.2"],
+        "//conditions:default": [],
+    }),
+    strip_include_prefix = "include",
+    visibility = ["//visibility:public"],
+)
+
+cc_test(
+    name = "crc32c_test",
+    srcs = [
+        "src/crc32c_arm64_unittest.cc",
+        "src/crc32c_extend_unittests.h",
+        "src/crc32c_portable_unittest.cc",
+        "src/crc32c_prefetch_unittest.cc",
+        "src/crc32c_read_le_unittest.cc",
+        "src/crc32c_round_up_unittest.cc",
+        "src/crc32c_sse42_unittest.cc",
+        "src/crc32c_test_main.cc",
+        "src/crc32c_unittest.cc",
+    ],
+    deps = [
+        ":crc32c",
+        "@com_google_googletest//:gtest",
+        "@com_google_googletest//:gtest_main",
+    ] + select({
+        "@//bazel/config:brpc_with_glog": ["@com_github_google_glog//:glog"],
+        "//conditions:default": [],
+    }),
+)
diff --git a/.bazelrc b/bazel/third_party/event/BUILD.bazel
similarity index 75%
copy from .bazelrc
copy to bazel/third_party/event/BUILD.bazel
index 8ae4f0d4..fefa6c3f 100644
--- a/.bazelrc
+++ b/bazel/third_party/event/BUILD.bazel
@@ -12,13 +12,6 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
-
-build --copt -DHAVE_ZLIB=1
-# bazel build with glog
-# build --define=with_glog=true
-build -c opt
-build --incompatible_disable_deprecated_attr_params=false
-build --incompatible_new_actions_api=false
-# unittest 
-test --define=unittest=true
-test --copt=-g
+#
+# Thie empty BUILD.bazel file is required to make Bazel treat
+# this directory as a package.
diff --git a/bazel/third_party/event/event.BUILD b/bazel/third_party/event/event.BUILD
new file mode 100644
index 00000000..6fd67592
--- /dev/null
+++ b/bazel/third_party/event/event.BUILD
@@ -0,0 +1,59 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
+
+filegroup(
+    name = "all_srcs",
+    srcs = glob(["**"]),
+)
+
+cmake(
+    name = "event",
+    cache_entries = {
+        "EVENT__DISABLE_BENCHMARK": "ON",
+        "EVENT__DISABLE_TESTS": "ON",
+        "EVENT__DISABLE_SAMPLES": "ON",
+        "EVENT__LIBRARY_TYPE": "STATIC",
+        "OPENSSL_ROOT_DIR": "$$EXT_BUILD_DEPS$$/openssl",
+    },
+    generate_args = ["-GNinja"],
+    lib_source = ":all_srcs",
+    linkopts = [
+        "-pthread",
+    ],
+    out_static_libs = select({
+        "@platforms//os:windows": [
+            "event.lib",
+            "event_core.lib",
+            "event_extra.lib",
+            "event_openssl.lib",
+            "event_pthreads.lib",
+        ],
+        "//conditions:default": [
+            "libevent.a",
+            "libevent_core.a",
+            "libevent_extra.a",
+            "libevent_openssl.a",
+            "libevent_pthreads.a",
+        ],
+    }),
+    visibility = ["//visibility:public"],
+    deps = [
+        # Zlib is only used for testing.
+        "@openssl//:crypto",
+        "@openssl//:ssl",
+    ],
+)
diff --git a/bazel/third_party/glog/0001-mark-override-resolve-warning.patch b/bazel/third_party/glog/0001-mark-override-resolve-warning.patch
new file mode 100644
index 00000000..7a9bbb8f
--- /dev/null
+++ b/bazel/third_party/glog/0001-mark-override-resolve-warning.patch
@@ -0,0 +1,36 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+diff --git a/src/glog/logging.h.in b/src/glog/logging.h.in
+index 421f1e0..a363141 100755
+--- a/src/glog/logging.h.in
++++ b/src/glog/logging.h.in
+@@ -1334,7 +1334,7 @@ class GOOGLE_GLOG_DLL_DECL LogStreamBuf : public std::streambuf {
+   }
+ 
+   // This effectively ignores overflow.
+-  int_type overflow(int_type ch) {
++  int_type overflow(int_type ch) override {
+     return ch;
+   }
+ 
+@@ -1862,7 +1862,7 @@ class GOOGLE_GLOG_DLL_DECL NullStreamFatal : public NullStream {
+   NullStreamFatal() { }
+   NullStreamFatal(const char* file, int line, const CheckOpString& result) :
+       NullStream(file, line, result) { }
+-  @ac_cv___attribute___noreturn@ ~NullStreamFatal() throw () { _exit(1); }
++  @ac_cv___attribute___noreturn@ ~NullStreamFatal() throw () override { _exit(1); }
+ };
+ 
+ // Install a signal handler that will dump signal information and a stack
diff --git a/.bazelrc b/bazel/third_party/glog/BUILD.bazel
similarity index 75%
copy from .bazelrc
copy to bazel/third_party/glog/BUILD.bazel
index 8ae4f0d4..fefa6c3f 100644
--- a/.bazelrc
+++ b/bazel/third_party/glog/BUILD.bazel
@@ -12,13 +12,6 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
-
-build --copt -DHAVE_ZLIB=1
-# bazel build with glog
-# build --define=with_glog=true
-build -c opt
-build --incompatible_disable_deprecated_attr_params=false
-build --incompatible_new_actions_api=false
-# unittest 
-test --define=unittest=true
-test --copt=-g
+#
+# Thie empty BUILD.bazel file is required to make Bazel treat
+# this directory as a package.
diff --git a/.bazelrc b/bazel/third_party/leveldb/BUILD.bazel
similarity index 75%
copy from .bazelrc
copy to bazel/third_party/leveldb/BUILD.bazel
index 8ae4f0d4..ea0c1091 100644
--- a/.bazelrc
+++ b/bazel/third_party/leveldb/BUILD.bazel
@@ -13,12 +13,9 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-build --copt -DHAVE_ZLIB=1
-# bazel build with glog
-# build --define=with_glog=true
-build -c opt
-build --incompatible_disable_deprecated_attr_params=false
-build --incompatible_new_actions_api=false
-# unittest 
-test --define=unittest=true
-test --copt=-g
+exports_files(
+    [
+        "port_config.h",
+        "port.h",
+    ],
+)
diff --git a/bazel/third_party/leveldb/leveldb.BUILD b/bazel/third_party/leveldb/leveldb.BUILD
new file mode 100644
index 00000000..787f77f3
--- /dev/null
+++ b/bazel/third_party/leveldb/leveldb.BUILD
@@ -0,0 +1,72 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+load("@rules_cc//cc:defs.bzl", "cc_library")
+load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
+
+copy_file(
+    name = "port_config_h",
+    src = "@//bazel/third_party/leveldb:port_config.h",
+    out = "port/port_config.h",
+    allow_symlink = True,
+)
+
+copy_file(
+    name = "port_h",
+    src = "@//bazel/third_party/leveldb:port.h",
+    out = "port/port.h",
+    allow_symlink = True,
+)
+
+cc_library(
+    name = "leveldb",
+    srcs = glob(
+        [
+            "db/**/*.cc",
+            "db/**/*.h",
+            "helpers/**/*.cc",
+            "helpers/**/*.h",
+            "port/**/*.cc",
+            "port/**/*.h",
+            "table/**/*.cc",
+            "table/**/*.h",
+            "util/**/*.cc",
+            "util/**/*.h",
+        ],
+        exclude = [
+            "**/*_test.cc",
+            "**/testutil.*",
+            "**/*_bench.cc",
+            "**/*_windows*",
+            "db/leveldbutil.cc",
+        ],
+    ),
+    hdrs = glob(
+        ["include/**/*.h"],
+        exclude = ["doc/**"],
+    ) + [
+        ":port_h",
+        ":port_config_h",
+    ],
+    includes = [
+        ".",
+        "include",
+    ],
+    visibility = ["//visibility:public"],
+    deps = [
+        "@com_github_google_crc32c//:crc32c",
+        "@com_github_google_snappy//:snappy",
+    ],
+)
diff --git a/bazel/third_party/leveldb/port.h b/bazel/third_party/leveldb/port.h
new file mode 100644
index 00000000..8c9a4eaf
--- /dev/null
+++ b/bazel/third_party/leveldb/port.h
@@ -0,0 +1,34 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file. See the AUTHORS file for names of contributors.
+
+#ifndef STORAGE_LEVELDB_PORT_PORT_H_
+#define STORAGE_LEVELDB_PORT_PORT_H_
+
+#include <string.h>
+
+#define LEVELDB_HAS_PORT_CONFIG_H 1
+
+// Include the appropriate platform specific file below.  If you are
+// porting to a new platform, see "port_example.h" for documentation
+// of what the new port_<platform>.h file must provide.
+#include "port/port_stdcxx.h"
+
+#endif  // STORAGE_LEVELDB_PORT_PORT_H_
diff --git a/bazel/third_party/leveldb/port_config.h b/bazel/third_party/leveldb/port_config.h
new file mode 100644
index 00000000..4ccdebfb
--- /dev/null
+++ b/bazel/third_party/leveldb/port_config.h
@@ -0,0 +1,38 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+// Copyright 2017 The LevelDB Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file. See the AUTHORS file for names of contributors.
+
+#ifndef STORAGE_LEVELDB_PORT_PORT_CONFIG_H_
+#define STORAGE_LEVELDB_PORT_PORT_CONFIG_H_
+
+// Define to 1 if you have a definition for fdatasync() in <unistd.h>.
+#define HAVE_FUNC_FDATASYNC 1
+
+// Define to 1 if you have Google CRC32C.
+#define HAVE_CRC32C 1
+
+// Define to 1 if you have Google Snappy.
+#define HAVE_SNAPPY 1
+
+// Define to 1 if your processor stores words with the most significant byte
+// first (like Motorola and SPARC, unlike Intel and VAX).
+#define LEVELDB_IS_BIG_ENDIAN 0
+
+#endif  // STORAGE_LEVELDB_PORT_PORT_CONFIG_H_
diff --git a/.bazelrc b/bazel/third_party/openssl/BUILD.bazel
similarity index 75%
copy from .bazelrc
copy to bazel/third_party/openssl/BUILD.bazel
index 8ae4f0d4..fefa6c3f 100644
--- a/.bazelrc
+++ b/bazel/third_party/openssl/BUILD.bazel
@@ -12,13 +12,6 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
-
-build --copt -DHAVE_ZLIB=1
-# bazel build with glog
-# build --define=with_glog=true
-build -c opt
-build --incompatible_disable_deprecated_attr_params=false
-build --incompatible_new_actions_api=false
-# unittest 
-test --define=unittest=true
-test --copt=-g
+#
+# Thie empty BUILD.bazel file is required to make Bazel treat
+# this directory as a package.
diff --git a/bazel/third_party/openssl/openssl.BUILD b/bazel/third_party/openssl/openssl.BUILD
new file mode 100644
index 00000000..c02cb6fb
--- /dev/null
+++ b/bazel/third_party/openssl/openssl.BUILD
@@ -0,0 +1,165 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# Copyright 2016 The Bazel Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# Copied from https://github.com/bazelbuild/rules_foreign_cc/blob/0.7.0/examples/third_party/openssl/BUILD.openssl.bazel
+#
+# Modifications:
+# 1. Create alias `ssl` & `crypto` to align with boringssl.
+# 2. Build with `@com_github_madler_zlib//:zlib`.
+# 3. Add more configure options coming from debian openssl package configurations.
+
+load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make", "configure_make_variant")
+
+filegroup(
+    name = "all_srcs",
+    srcs = glob(["**"]),
+)
+
+CONFIGURE_OPTIONS = [
+    "no-idea",
+    "no-mdc2",
+    "no-rc5",
+    "no-ssl3",
+    "no-ssl3-method",
+    "enable-rfc3779",
+    "enable-cms",
+    "no-capieng",
+    "enable-ec_nistp_64_gcc_128",
+    "--with-zlib-include=$$EXT_BUILD_DEPS$$",
+    "--with-zlib-lib=$$EXT_BUILD_DEPS$$",
+    # https://stackoverflow.com/questions/36220341/struct-in6-addr-has-no-member-named-s6-addr32-with-ansi
+    "-D_DEFAULT_SOURCE=1",
+    "-DPEDANTIC",
+]
+
+LIB_NAME = "openssl"
+
+MAKE_TARGETS = [
+    "build_libs",
+    "install_dev",
+]
+
+config_setting(
+    name = "msvc_compiler",
+    flag_values = {
+        "@bazel_tools//tools/cpp:compiler": "msvc-cl",
+    },
+    visibility = ["//visibility:public"],
+)
+
+alias(
+    name = "ssl",
+    actual = "openssl",
+    visibility = ["//visibility:public"],
+)
+
+alias(
+    name = "crypto",
+    actual = "openssl",
+    visibility = ["//visibility:public"],
+)
+
+alias(
+    name = "openssl",
+    actual = select({
+        ":msvc_compiler": "openssl_msvc",
+        "//conditions:default": "openssl_default",
+    }),
+    visibility = ["//visibility:public"],
+)
+
+configure_make_variant(
+    name = "openssl_msvc",
+    build_data = [
+        "@nasm//:nasm",
+        "@perl//:perl",
+    ],
+    configure_command = "Configure",
+    configure_in_place = True,
+    configure_options = CONFIGURE_OPTIONS + [
+        "VC-WIN64A",
+        # Unset Microsoft Assembler (MASM) flags set by built-in MSVC toolchain,
+        # as NASM is unsed to build OpenSSL rather than MASM
+        "ASFLAGS=\" \"",
+    ],
+    configure_prefix = "$PERL",
+    env = {
+        # The Zi flag must be set otherwise OpenSSL fails to build due to missing .pdb files
+        "CFLAGS": "-Zi",
+        "PATH": "$$(dirname $(execpath @nasm//:nasm)):$$PATH",
+        "PERL": "$(execpath @perl//:perl)",
+    },
+    lib_name = LIB_NAME,
+    lib_source = ":all_srcs",
+    out_static_libs = [
+        "libssl.lib",
+        "libcrypto.lib",
+    ],
+    targets = MAKE_TARGETS,
+    toolchain = "@rules_foreign_cc//toolchains:preinstalled_nmake_toolchain",
+    deps = [
+        "@com_github_madler_zlib//:zlib",
+    ],
+)
+
+# https://wiki.openssl.org/index.php/Compilation_and_Installation
+configure_make(
+    name = "openssl_default",
+    configure_command = "config",
+    configure_in_place = True,
+    configure_options = CONFIGURE_OPTIONS,
+    env = select({
+        "@platforms//os:macos": {
+            "AR": "",
+            "PERL": "$$EXT_BUILD_ROOT$$/$(PERL)",
+        },
+        "//conditions:default": {
+            "PERL": "$$EXT_BUILD_ROOT$$/$(PERL)",
+        },
+    }),
+    lib_name = LIB_NAME,
+    lib_source = ":all_srcs",
+    # Note that for Linux builds, libssl must come before libcrypto on the linker command-line.
+    # As such, libssl must be listed before libcrypto
+    out_static_libs = [
+        "libssl.a",
+        "libcrypto.a",
+    ],
+    targets = MAKE_TARGETS,
+    toolchains = ["@rules_perl//:current_toolchain"],
+    deps = [
+        "@com_github_madler_zlib//:zlib",
+    ],
+)
+
+filegroup(
+    name = "gen_dir",
+    srcs = [":openssl"],
+    output_group = "gen_dir",
+)
diff --git a/.bazelrc b/bazel/third_party/protobuf/BUILD.bazel
similarity index 75%
copy from .bazelrc
copy to bazel/third_party/protobuf/BUILD.bazel
index 8ae4f0d4..fefa6c3f 100644
--- a/.bazelrc
+++ b/bazel/third_party/protobuf/BUILD.bazel
@@ -12,13 +12,6 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
-
-build --copt -DHAVE_ZLIB=1
-# bazel build with glog
-# build --define=with_glog=true
-build -c opt
-build --incompatible_disable_deprecated_attr_params=false
-build --incompatible_new_actions_api=false
-# unittest 
-test --define=unittest=true
-test --copt=-g
+#
+# Thie empty BUILD.bazel file is required to make Bazel treat
+# this directory as a package.
diff --git a/bazel/third_party/protobuf/protobuf.BUILD b/bazel/third_party/protobuf/protobuf.BUILD
new file mode 100644
index 00000000..0d5188ea
--- /dev/null
+++ b/bazel/third_party/protobuf/protobuf.BUILD
@@ -0,0 +1,498 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# Copyright 2008 Google Inc.  All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+#     * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#     * Redistributions in binary form must reproduce the above
+# copyright notice, this list of conditions and the following disclaimer
+# in the documentation and/or other materials provided with the
+# distribution.
+#     * Neither the name of Google Inc. nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+# Copied from https://github.com/protocolbuffers/protobuf/blob/v3.19.1/BUILD
+#
+# Modifications:
+# 1. Remove all non-cxx rules.
+# 2. Remove android support.
+# 3. zlib use @com_github_madler_zlib//:zlib
+
+# Bazel (https://bazel.build/) BUILD file for Protobuf.
+
+load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", native_cc_proto_library = "cc_proto_library")
+load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain", "proto_library")
+load(":compiler_config_setting.bzl", "create_compiler_config_setting")
+load(
+    ":protobuf.bzl",
+    "adapt_proto_library",
+)
+
+licenses(["notice"])
+
+exports_files(["LICENSE"])
+
+################################################################################
+# build configuration
+################################################################################
+
+################################################################################
+# ZLIB configuration
+################################################################################
+
+ZLIB_DEPS = ["@com_github_madler_zlib//:zlib"]
+
+################################################################################
+# Protobuf Runtime Library
+################################################################################
+
+MSVC_COPTS = [
+    "/wd4018",  # -Wno-sign-compare
+    "/wd4065",  # switch statement contains 'default' but no 'case' labels
+    "/wd4146",  # unary minus operator applied to unsigned type, result still unsigned
+    "/wd4244",  # 'conversion' conversion from 'type1' to 'type2', possible loss of data
+    "/wd4251",  # 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
+    "/wd4267",  # 'var' : conversion from 'size_t' to 'type', possible loss of data
+    "/wd4305",  # 'identifier' : truncation from 'type1' to 'type2'
+    "/wd4307",  # 'operator' : integral constant overflow
+    "/wd4309",  # 'conversion' : truncation of constant value
+    "/wd4334",  # 'operator' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
+    "/wd4355",  # 'this' : used in base member initializer list
+    "/wd4506",  # no definition for inline function 'function'
+    "/wd4800",  # 'type' : forcing value to bool 'true' or 'false' (performance warning)
+    "/wd4996",  # The compiler encountered a deprecated declaration.
+]
+
+COPTS = select({
+    ":msvc": MSVC_COPTS,
+    "//conditions:default": [
+        "-DHAVE_ZLIB",
+        "-Wmissing-field-initializers",
+        "-Woverloaded-virtual",
+        "-Wno-sign-compare",
+    ],
+})
+
+create_compiler_config_setting(
+    name = "msvc",
+    value = "msvc-cl",
+    visibility = [
+        # Public, but Protobuf only visibility.
+        "//:__subpackages__",
+    ],
+)
+
+# Android and MSVC builds do not need to link in a separate pthread library.
+LINK_OPTS = select({
+    ":msvc": [
+        # Suppress linker warnings about files with no symbols defined.
+        "-ignore:4221",
+    ],
+    "//conditions:default": [
+        "-lpthread",
+        "-lm",
+    ],
+})
+
+cc_library(
+    name = "protobuf_lite",
+    srcs = [
+        # AUTOGEN(protobuf_lite_srcs)
+        "src/google/protobuf/any_lite.cc",
+        "src/google/protobuf/arena.cc",
+        "src/google/protobuf/arenastring.cc",
+        "src/google/protobuf/extension_set.cc",
+        "src/google/protobuf/generated_enum_util.cc",
+        "src/google/protobuf/generated_message_table_driven_lite.cc",
+        "src/google/protobuf/generated_message_tctable_lite.cc",
+        "src/google/protobuf/generated_message_util.cc",
+        "src/google/protobuf/implicit_weak_message.cc",
+        "src/google/protobuf/inlined_string_field.cc",
+        "src/google/protobuf/io/coded_stream.cc",
+        "src/google/protobuf/io/io_win32.cc",
+        "src/google/protobuf/io/strtod.cc",
+        "src/google/protobuf/io/zero_copy_stream.cc",
+        "src/google/protobuf/io/zero_copy_stream_impl.cc",
+        "src/google/protobuf/io/zero_copy_stream_impl_lite.cc",
+        "src/google/protobuf/map.cc",
+        "src/google/protobuf/message_lite.cc",
+        "src/google/protobuf/parse_context.cc",
+        "src/google/protobuf/repeated_field.cc",
+        "src/google/protobuf/repeated_ptr_field.cc",
+        "src/google/protobuf/stubs/bytestream.cc",
+        "src/google/protobuf/stubs/common.cc",
+        "src/google/protobuf/stubs/int128.cc",
+        "src/google/protobuf/stubs/status.cc",
+        "src/google/protobuf/stubs/statusor.cc",
+        "src/google/protobuf/stubs/stringpiece.cc",
+        "src/google/protobuf/stubs/stringprintf.cc",
+        "src/google/protobuf/stubs/structurally_valid.cc",
+        "src/google/protobuf/stubs/strutil.cc",
+        "src/google/protobuf/stubs/time.cc",
+        "src/google/protobuf/wire_format_lite.cc",
+    ],
+    hdrs = glob([
+        "src/google/protobuf/**/*.h",
+        "src/google/protobuf/**/*.inc",
+    ]),
+    copts = COPTS,
+    includes = ["src/"],
+    linkopts = LINK_OPTS,
+    visibility = ["//visibility:public"],
+)
+
+PROTOBUF_DEPS = select({
+    ":msvc": [],
+    "//conditions:default": ZLIB_DEPS,
+})
+
+cc_library(
+    name = "protobuf",
+    srcs = [
+        # AUTOGEN(protobuf_srcs)
+        "src/google/protobuf/any.cc",
+        "src/google/protobuf/any.pb.cc",
+        "src/google/protobuf/api.pb.cc",
+        "src/google/protobuf/compiler/importer.cc",
+        "src/google/protobuf/compiler/parser.cc",
+        "src/google/protobuf/descriptor.cc",
+        "src/google/protobuf/descriptor.pb.cc",
+        "src/google/protobuf/descriptor_database.cc",
+        "src/google/protobuf/duration.pb.cc",
+        "src/google/protobuf/dynamic_message.cc",
+        "src/google/protobuf/empty.pb.cc",
+        "src/google/protobuf/extension_set_heavy.cc",
+        "src/google/protobuf/field_mask.pb.cc",
+        "src/google/protobuf/generated_message_bases.cc",
+        "src/google/protobuf/generated_message_reflection.cc",
+        "src/google/protobuf/generated_message_table_driven.cc",
+        "src/google/protobuf/generated_message_tctable_full.cc",
+        "src/google/protobuf/io/gzip_stream.cc",
+        "src/google/protobuf/io/printer.cc",
+        "src/google/protobuf/io/tokenizer.cc",
+        "src/google/protobuf/map_field.cc",
+        "src/google/protobuf/message.cc",
+        "src/google/protobuf/reflection_ops.cc",
+        "src/google/protobuf/service.cc",
+        "src/google/protobuf/source_context.pb.cc",
+        "src/google/protobuf/struct.pb.cc",
+        "src/google/protobuf/stubs/substitute.cc",
+        "src/google/protobuf/text_format.cc",
+        "src/google/protobuf/timestamp.pb.cc",
+        "src/google/protobuf/type.pb.cc",
+        "src/google/protobuf/unknown_field_set.cc",
+        "src/google/protobuf/util/delimited_message_util.cc",
+        "src/google/protobuf/util/field_comparator.cc",
+        "src/google/protobuf/util/field_mask_util.cc",
+        "src/google/protobuf/util/internal/datapiece.cc",
+        "src/google/protobuf/util/internal/default_value_objectwriter.cc",
+        "src/google/protobuf/util/internal/error_listener.cc",
+        "src/google/protobuf/util/internal/field_mask_utility.cc",
+        "src/google/protobuf/util/internal/json_escaping.cc",
+        "src/google/protobuf/util/internal/json_objectwriter.cc",
+        "src/google/protobuf/util/internal/json_stream_parser.cc",
+        "src/google/protobuf/util/internal/object_writer.cc",
+        "src/google/protobuf/util/internal/proto_writer.cc",
+        "src/google/protobuf/util/internal/protostream_objectsource.cc",
+        "src/google/protobuf/util/internal/protostream_objectwriter.cc",
+        "src/google/protobuf/util/internal/type_info.cc",
+        "src/google/protobuf/util/internal/utility.cc",
+        "src/google/protobuf/util/json_util.cc",
+        "src/google/protobuf/util/message_differencer.cc",
+        "src/google/protobuf/util/time_util.cc",
+        "src/google/protobuf/util/type_resolver_util.cc",
+        "src/google/protobuf/wire_format.cc",
+        "src/google/protobuf/wrappers.pb.cc",
+    ],
+    hdrs = glob([
+        "src/**/*.h",
+        "src/**/*.inc",
+    ]),
+    copts = COPTS,
+    includes = ["src/"],
+    linkopts = LINK_OPTS,
+    visibility = ["//visibility:public"],
+    deps = [":protobuf_lite"] + PROTOBUF_DEPS,
+)
+
+# This provides just the header files for use in projects that need to build
+# shared libraries for dynamic loading. This target is available until Bazel
+# adds native support for such use cases.
+# TODO(keveman): Remove this target once the support gets added to Bazel.
+cc_library(
+    name = "protobuf_headers",
+    hdrs = glob([
+        "src/**/*.h",
+        "src/**/*.inc",
+    ]),
+    includes = ["src/"],
+    visibility = ["//visibility:public"],
+)
+
+# Map of all well known protos.
+# name => (include path, imports)
+WELL_KNOWN_PROTO_MAP = {
+    "any": ("src/google/protobuf/any.proto", []),
+    "api": (
+        "src/google/protobuf/api.proto",
+        [
+            "source_context",
+            "type",
+        ],
+    ),
+    "compiler_plugin": (
+        "src/google/protobuf/compiler/plugin.proto",
+        ["descriptor"],
+    ),
+    "descriptor": ("src/google/protobuf/descriptor.proto", []),
+    "duration": ("src/google/protobuf/duration.proto", []),
+    "empty": ("src/google/protobuf/empty.proto", []),
+    "field_mask": ("src/google/protobuf/field_mask.proto", []),
+    "source_context": ("src/google/protobuf/source_context.proto", []),
+    "struct": ("src/google/protobuf/struct.proto", []),
+    "timestamp": ("src/google/protobuf/timestamp.proto", []),
+    "type": (
+        "src/google/protobuf/type.proto",
+        [
+            "any",
+            "source_context",
+        ],
+    ),
+    "wrappers": ("src/google/protobuf/wrappers.proto", []),
+}
+
+WELL_KNOWN_PROTOS = [value[0] for value in WELL_KNOWN_PROTO_MAP.values()]
+
+LITE_WELL_KNOWN_PROTO_MAP = {
+    "any": ("src/google/protobuf/any.proto", []),
+    "api": (
+        "src/google/protobuf/api.proto",
+        [
+            "source_context",
+            "type",
+        ],
+    ),
+    "duration": ("src/google/protobuf/duration.proto", []),
+    "empty": ("src/google/protobuf/empty.proto", []),
+    "field_mask": ("src/google/protobuf/field_mask.proto", []),
+    "source_context": ("src/google/protobuf/source_context.proto", []),
+    "struct": ("src/google/protobuf/struct.proto", []),
+    "timestamp": ("src/google/protobuf/timestamp.proto", []),
+    "type": (
+        "src/google/protobuf/type.proto",
+        [
+            "any",
+            "source_context",
+        ],
+    ),
+    "wrappers": ("src/google/protobuf/wrappers.proto", []),
+}
+
+LITE_WELL_KNOWN_PROTOS = [value[0] for value in LITE_WELL_KNOWN_PROTO_MAP.values()]
+
+filegroup(
+    name = "well_known_protos",
+    srcs = WELL_KNOWN_PROTOS,
+    visibility = ["//visibility:public"],
+)
+
+filegroup(
+    name = "lite_well_known_protos",
+    srcs = LITE_WELL_KNOWN_PROTOS,
+    visibility = ["//visibility:public"],
+)
+
+adapt_proto_library(
+    name = "cc_wkt_protos_genproto",
+    visibility = ["//visibility:public"],
+    deps = [proto + "_proto" for proto in WELL_KNOWN_PROTO_MAP.keys()],
+)
+
+cc_library(
+    name = "cc_wkt_protos",
+    deprecation = "Only for backward compatibility. Do not use.",
+    visibility = ["//visibility:public"],
+)
+
+################################################################################
+# Well Known Types Proto Library Rules
+#
+# These proto_library rules can be used with one of the language specific proto
+# library rules i.e. java_proto_library:
+#
+# java_proto_library(
+#   name = "any_java_proto",
+#   deps = ["@com_google_protobuf//:any_proto],
+# )
+################################################################################
+
+[proto_library(
+    name = proto[0] + "_proto",
+    srcs = [proto[1][0]],
+    strip_import_prefix = "src",
+    visibility = ["//visibility:public"],
+    deps = [dep + "_proto" for dep in proto[1][1]],
+) for proto in WELL_KNOWN_PROTO_MAP.items()]
+
+[native_cc_proto_library(
+    name = proto + "_cc_proto",
+    visibility = ["//visibility:private"],
+    deps = [proto + "_proto"],
+) for proto in WELL_KNOWN_PROTO_MAP.keys()]
+
+################################################################################
+# Protocol Buffers Compiler
+################################################################################
+
+cc_library(
+    name = "protoc_lib",
+    srcs = [
+        # AUTOGEN(protoc_lib_srcs)
+        "src/google/protobuf/compiler/code_generator.cc",
+        "src/google/protobuf/compiler/command_line_interface.cc",
+        "src/google/protobuf/compiler/cpp/cpp_enum.cc",
+        "src/google/protobuf/compiler/cpp/cpp_enum_field.cc",
+        "src/google/protobuf/compiler/cpp/cpp_extension.cc",
+        "src/google/protobuf/compiler/cpp/cpp_field.cc",
+        "src/google/protobuf/compiler/cpp/cpp_file.cc",
+        "src/google/protobuf/compiler/cpp/cpp_generator.cc",
+        "src/google/protobuf/compiler/cpp/cpp_helpers.cc",
+        "src/google/protobuf/compiler/cpp/cpp_map_field.cc",
+        "src/google/protobuf/compiler/cpp/cpp_message.cc",
+        "src/google/protobuf/compiler/cpp/cpp_message_field.cc",
+        "src/google/protobuf/compiler/cpp/cpp_padding_optimizer.cc",
+        "src/google/protobuf/compiler/cpp/cpp_parse_function_generator.cc",
+        "src/google/protobuf/compiler/cpp/cpp_primitive_field.cc",
+        "src/google/protobuf/compiler/cpp/cpp_service.cc",
+        "src/google/protobuf/compiler/cpp/cpp_string_field.cc",
+        "src/google/protobuf/compiler/csharp/csharp_doc_comment.cc",
+        "src/google/protobuf/compiler/csharp/csharp_enum.cc",
+        "src/google/protobuf/compiler/csharp/csharp_enum_field.cc",
+        "src/google/protobuf/compiler/csharp/csharp_field_base.cc",
+        "src/google/protobuf/compiler/csharp/csharp_generator.cc",
+        "src/google/protobuf/compiler/csharp/csharp_helpers.cc",
+        "src/google/protobuf/compiler/csharp/csharp_map_field.cc",
+        "src/google/protobuf/compiler/csharp/csharp_message.cc",
+        "src/google/protobuf/compiler/csharp/csharp_message_field.cc",
+        "src/google/protobuf/compiler/csharp/csharp_primitive_field.cc",
+        "src/google/protobuf/compiler/csharp/csharp_reflection_class.cc",
+        "src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc",
+        "src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc",
+        "src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc",
+        "src/google/protobuf/compiler/csharp/csharp_source_generator_base.cc",
+        "src/google/protobuf/compiler/csharp/csharp_wrapper_field.cc",
+        "src/google/protobuf/compiler/java/java_context.cc",
+        "src/google/protobuf/compiler/java/java_doc_comment.cc",
+        "src/google/protobuf/compiler/java/java_enum.cc",
+        "src/google/protobuf/compiler/java/java_enum_field.cc",
+        "src/google/protobuf/compiler/java/java_enum_field_lite.cc",
+        "src/google/protobuf/compiler/java/java_enum_lite.cc",
+        "src/google/protobuf/compiler/java/java_extension.cc",
+        "src/google/protobuf/compiler/java/java_extension_lite.cc",
+        "src/google/protobuf/compiler/java/java_field.cc",
+        "src/google/protobuf/compiler/java/java_file.cc",
+        "src/google/protobuf/compiler/java/java_generator.cc",
+        "src/google/protobuf/compiler/java/java_generator_factory.cc",
+        "src/google/protobuf/compiler/java/java_helpers.cc",
+        "src/google/protobuf/compiler/java/java_kotlin_generator.cc",
+        "src/google/protobuf/compiler/java/java_map_field.cc",
+        "src/google/protobuf/compiler/java/java_map_field_lite.cc",
+        "src/google/protobuf/compiler/java/java_message.cc",
+        "src/google/protobuf/compiler/java/java_message_builder.cc",
+        "src/google/protobuf/compiler/java/java_message_builder_lite.cc",
+        "src/google/protobuf/compiler/java/java_message_field.cc",
+        "src/google/protobuf/compiler/java/java_message_field_lite.cc",
+        "src/google/protobuf/compiler/java/java_message_lite.cc",
+        "src/google/protobuf/compiler/java/java_name_resolver.cc",
+        "src/google/protobuf/compiler/java/java_primitive_field.cc",
+        "src/google/protobuf/compiler/java/java_primitive_field_lite.cc",
+        "src/google/protobuf/compiler/java/java_service.cc",
+        "src/google/protobuf/compiler/java/java_shared_code_generator.cc",
+        "src/google/protobuf/compiler/java/java_string_field.cc",
+        "src/google/protobuf/compiler/java/java_string_field_lite.cc",
+        "src/google/protobuf/compiler/js/js_generator.cc",
+        "src/google/protobuf/compiler/js/well_known_types_embed.cc",
+        "src/google/protobuf/compiler/objectivec/objectivec_enum.cc",
+        "src/google/protobuf/compiler/objectivec/objectivec_enum_field.cc",
+        "src/google/protobuf/compiler/objectivec/objectivec_extension.cc",
+        "src/google/protobuf/compiler/objectivec/objectivec_field.cc",
+        "src/google/protobuf/compiler/objectivec/objectivec_file.cc",
+        "src/google/protobuf/compiler/objectivec/objectivec_generator.cc",
+        "src/google/protobuf/compiler/objectivec/objectivec_helpers.cc",
+        "src/google/protobuf/compiler/objectivec/objectivec_map_field.cc",
+        "src/google/protobuf/compiler/objectivec/objectivec_message.cc",
+        "src/google/protobuf/compiler/objectivec/objectivec_message_field.cc",
+        "src/google/protobuf/compiler/objectivec/objectivec_oneof.cc",
+        "src/google/protobuf/compiler/objectivec/objectivec_primitive_field.cc",
+        "src/google/protobuf/compiler/php/php_generator.cc",
+        "src/google/protobuf/compiler/plugin.cc",
+        "src/google/protobuf/compiler/plugin.pb.cc",
+        "src/google/protobuf/compiler/python/python_generator.cc",
+        "src/google/protobuf/compiler/ruby/ruby_generator.cc",
+        "src/google/protobuf/compiler/subprocess.cc",
+        "src/google/protobuf/compiler/zip_writer.cc",
+    ],
+    copts = COPTS,
+    includes = ["src/"],
+    linkopts = LINK_OPTS,
+    visibility = ["//visibility:public"],
+    deps = [":protobuf"],
+)
+
+cc_binary(
+    name = "protoc",
+    srcs = ["src/google/protobuf/compiler/main.cc"],
+    linkopts = LINK_OPTS,
+    visibility = ["//visibility:public"],
+    deps = [":protoc_lib"],
+)
+
+proto_lang_toolchain(
+    name = "cc_toolchain",
+    blacklisted_protos = [proto + "_proto" for proto in WELL_KNOWN_PROTO_MAP.keys()],
+    command_line = "--cpp_out=$(OUT)",
+    runtime = ":protobuf",
+    visibility = ["//visibility:public"],
+)
+
+alias(
+    name = "objectivec",
+    actual = "//objectivec",
+    visibility = ["//visibility:public"],
+)
+
+alias(
+    name = "protobuf_objc",
+    actual = "//objectivec",
+    visibility = ["//visibility:public"],
+)
diff --git a/.bazelrc b/bazel/third_party/snappy/BUILD.bazel
similarity index 75%
copy from .bazelrc
copy to bazel/third_party/snappy/BUILD.bazel
index 8ae4f0d4..fefa6c3f 100644
--- a/.bazelrc
+++ b/bazel/third_party/snappy/BUILD.bazel
@@ -12,13 +12,6 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
-
-build --copt -DHAVE_ZLIB=1
-# bazel build with glog
-# build --define=with_glog=true
-build -c opt
-build --incompatible_disable_deprecated_attr_params=false
-build --incompatible_new_actions_api=false
-# unittest 
-test --define=unittest=true
-test --copt=-g
+#
+# Thie empty BUILD.bazel file is required to make Bazel treat
+# this directory as a package.
diff --git a/bazel/third_party/snappy/snappy.BUILD b/bazel/third_party/snappy/snappy.BUILD
new file mode 100644
index 00000000..9cfb1ade
--- /dev/null
+++ b/bazel/third_party/snappy/snappy.BUILD
@@ -0,0 +1,122 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Copied from https://github.com/tensorflow/tensorflow/blob/bdd8bf316e4ab7d699127d192d30eb614a158462/third_party/snappy.BUILD
+
+load("@rules_cc//cc:defs.bzl", "cc_library")
+
+cc_library(
+    name = "snappy",
+    srcs = [
+        "config.h",
+        "snappy.cc",
+        "snappy.h",
+        "snappy-internal.h",
+        "snappy-sinksource.cc",
+        "snappy-stubs-internal.cc",
+        "snappy-stubs-internal.h",
+        "snappy-stubs-public.h",
+    ],
+    hdrs = [
+        "snappy.h",
+        "snappy-sinksource.h",
+    ],
+    copts = [
+        "-DHAVE_CONFIG_H",
+        "-fno-exceptions",
+        "-Wno-sign-compare",
+        "-Wno-shift-negative-value",
+    ],
+    includes = ["."],
+    visibility = ["//visibility:public"],
+)
+
+genrule(
+    name = "config_h",
+    outs = ["config.h"],
+    cmd = "\n".join([
+        "cat <<'EOF' >$@",
+        "#define HAVE_STDDEF_H 1",
+        "#define HAVE_STDINT_H 1",
+        "",
+        "#ifdef __has_builtin",
+        "#  if !defined(HAVE_BUILTIN_EXPECT) && __has_builtin(__builtin_expect)",
+        "#    define HAVE_BUILTIN_EXPECT 1",
+        "#  endif",
+        "#  if !defined(HAVE_BUILTIN_CTZ) && __has_builtin(__builtin_ctzll)",
+        "#    define HAVE_BUILTIN_CTZ 1",
+        "#  endif",
+        "#elif defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4)",
+        "#  ifndef HAVE_BUILTIN_EXPECT",
+        "#    define HAVE_BUILTIN_EXPECT 1",
+        "#  endif",
+        "#  ifndef HAVE_BUILTIN_CTZ",
+        "#    define HAVE_BUILTIN_CTZ 1",
+        "#  endif",
+        "#endif",
+        "",
+        "#ifdef __has_include",
+        "#  if !defined(HAVE_BYTESWAP_H) && __has_include(<byteswap.h>)",
+        "#    define HAVE_BYTESWAP_H 1",
+        "#  endif",
+        "#  if !defined(HAVE_UNISTD_H) && __has_include(<unistd.h>)",
+        "#    define HAVE_UNISTD_H 1",
+        "#  endif",
+        "#  if !defined(HAVE_SYS_ENDIAN_H) && __has_include(<sys/endian.h>)",
+        "#    define HAVE_SYS_ENDIAN_H 1",
+        "#  endif",
+        "#  if !defined(HAVE_SYS_MMAN_H) && __has_include(<sys/mman.h>)",
+        "#    define HAVE_SYS_MMAN_H 1",
+        "#  endif",
+        "#  if !defined(HAVE_SYS_UIO_H) && __has_include(<sys/uio.h>)",
+        "#    define HAVE_SYS_UIO_H 1",
+        "#  endif",
+        "#endif",
+        "",
+        "#ifndef SNAPPY_IS_BIG_ENDIAN",
+        "#  ifdef __s390x__",
+        "#    define SNAPPY_IS_BIG_ENDIAN 1",
+        "#  elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__",
+        "#    define SNAPPY_IS_BIG_ENDIAN 1",
+        "#  endif",
+        "#endif",
+        "EOF",
+    ]),
+)
+
+genrule(
+    name = "snappy_stubs_public_h",
+    srcs = ["snappy-stubs-public.h.in"],
+    outs = ["snappy-stubs-public.h"],
+    cmd = ("sed " +
+           "-e 's/$${\\(.*\\)_01}/\\1/g' " +
+           "-e 's/$${SNAPPY_MAJOR}/1/g' " +
+           "-e 's/$${SNAPPY_MINOR}/1/g' " +
+           "-e 's/$${SNAPPY_PATCHLEVEL}/7/g' " +
+           "$< >$@"),
+)
diff --git a/.bazelrc b/bazel/third_party/thrift/BUILD.bazel
similarity index 75%
copy from .bazelrc
copy to bazel/third_party/thrift/BUILD.bazel
index 8ae4f0d4..fefa6c3f 100644
--- a/.bazelrc
+++ b/bazel/third_party/thrift/BUILD.bazel
@@ -12,13 +12,6 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
-
-build --copt -DHAVE_ZLIB=1
-# bazel build with glog
-# build --define=with_glog=true
-build -c opt
-build --incompatible_disable_deprecated_attr_params=false
-build --incompatible_new_actions_api=false
-# unittest 
-test --define=unittest=true
-test --copt=-g
+#
+# Thie empty BUILD.bazel file is required to make Bazel treat
+# this directory as a package.
diff --git a/bazel/third_party/thrift/thrift.BUILD b/bazel/third_party/thrift/thrift.BUILD
new file mode 100644
index 00000000..079606aa
--- /dev/null
+++ b/bazel/third_party/thrift/thrift.BUILD
@@ -0,0 +1,75 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
+
+filegroup(
+    name = "all_srcs",
+    srcs = glob(["**"]),
+)
+
+cmake(
+    name = "thrift",
+    cache_entries = {
+        "BUILD_COMPILER": "OFF",
+        "BUILD_TESTING": "OFF",
+        "BUILD_TUTORIALS": "OFF",
+        "WITH_AS3": "OFF",
+        "WITH_CPP": "ON",
+        "WITH_C_GLIB": "OFF",
+        "WITH_JAVA": "OFF",
+        "WITH_JAVASCRIPT": "OFF",
+        "WITH_NODEJS": "OFF",
+        "WITH_PYTHON": "OFF",
+        "BUILD_SHARED_LIBS": "OFF",
+        "Boost_USE_STATIC_LIBS": "ON",
+        "BOOST_ROOT": "$$EXT_BUILD_DEPS$$",
+        "LIBEVENT_INCLUDE_DIRS": "$$EXT_BUILD_DEPS$$/event/include",
+        "LIBEVENT_LIBRARIES": "$$EXT_BUILD_DEPS$$/event/lib/libevent.a",
+        "OPENSSL_ROOT_DIR": "$$EXT_BUILD_DEPS$$/openssl",
+        "ZLIB_ROOT": "$$EXT_BUILD_DEPS$$/zlib",
+    },
+    generate_args = ["-GNinja"],
+    lib_source = ":all_srcs",
+    linkopts = [
+        "-pthread",
+    ],
+    out_static_libs = select({
+        "@platforms//os:windows": [
+            "thrift.lib",
+            "thriftnb.lib",
+            "thriftz.lib",
+        ],
+        "//conditions:default": [
+            "libthrift.a",
+            "libthriftnb.a",
+            "libthriftz.a",
+        ],
+    }),
+    visibility = ["//visibility:public"],
+    deps = [
+        "@boost//:algorithm",
+        "@boost//:locale",
+        "@boost//:noncopyable",
+        "@boost//:numeric_conversion",
+        "@boost//:scoped_array",
+        "@boost//:smart_ptr",
+        "@boost//:tokenizer",
+        "@com_github_libevent_libevent//:event",
+        "@com_github_madler_zlib//:zlib",
+        "@openssl//:crypto",
+        "@openssl//:ssl",
+    ],
+)
diff --git a/.bazelrc b/bazel/third_party/zlib/BUILD.bazel
similarity index 75%
copy from .bazelrc
copy to bazel/third_party/zlib/BUILD.bazel
index 8ae4f0d4..fefa6c3f 100644
--- a/.bazelrc
+++ b/bazel/third_party/zlib/BUILD.bazel
@@ -12,13 +12,6 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
-
-build --copt -DHAVE_ZLIB=1
-# bazel build with glog
-# build --define=with_glog=true
-build -c opt
-build --incompatible_disable_deprecated_attr_params=false
-build --incompatible_new_actions_api=false
-# unittest 
-test --define=unittest=true
-test --copt=-g
+#
+# Thie empty BUILD.bazel file is required to make Bazel treat
+# this directory as a package.
diff --git a/bazel/third_party/zlib/zlib.BUILD b/bazel/third_party/zlib/zlib.BUILD
new file mode 100644
index 00000000..d8139b63
--- /dev/null
+++ b/bazel/third_party/zlib/zlib.BUILD
@@ -0,0 +1,111 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# Copyright 2008 Google Inc.  All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+#     * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#     * Redistributions in binary form must reproduce the above
+# copyright notice, this list of conditions and the following disclaimer
+# in the documentation and/or other materials provided with the
+# distribution.
+#     * Neither the name of Google Inc. nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+# Copied from https://github.com/protocolbuffers/protobuf/blob/v3.9.1/third_party/zlib.BUILD
+
+load("@rules_cc//cc:defs.bzl", "cc_library")
+
+_ZLIB_HEADERS = [
+    "crc32.h",
+    "deflate.h",
+    "gzguts.h",
+    "inffast.h",
+    "inffixed.h",
+    "inflate.h",
+    "inftrees.h",
+    "trees.h",
+    "zconf.h",
+    "zlib.h",
+    "zutil.h",
+]
+
+_ZLIB_PREFIXED_HEADERS = ["zlib/include/" + hdr for hdr in _ZLIB_HEADERS]
+
+# In order to limit the damage from the `includes` propagation
+# via `:zlib`, copy the public headers to a subdirectory and
+# expose those.
+genrule(
+    name = "copy_public_headers",
+    srcs = _ZLIB_HEADERS,
+    outs = _ZLIB_PREFIXED_HEADERS,
+    cmd = "cp $(SRCS) $(@D)/zlib/include/",
+    visibility = ["//visibility:private"],
+)
+
+cc_library(
+    name = "zlib",
+    srcs = [
+        "adler32.c",
+        "compress.c",
+        "crc32.c",
+        "deflate.c",
+        "gzclose.c",
+        "gzlib.c",
+        "gzread.c",
+        "gzwrite.c",
+        "infback.c",
+        "inffast.c",
+        "inflate.c",
+        "inftrees.c",
+        "trees.c",
+        "uncompr.c",
+        "zutil.c",
+    ],
+    hdrs = _ZLIB_PREFIXED_HEADERS,
+    copts = select({
+        ":windows": [],
+        "//conditions:default": [
+            "-Wno-unused-variable",
+            "-Wno-implicit-function-declaration",
+        ],
+    }),
+    includes = ["zlib/include/"],
+    visibility = ["//visibility:public"],
+)
+
+config_setting(
+    name = "windows",
+    constraint_values = [
+        "@platforms//os:windows",
+    ],
+)
diff --git a/example/BUILD b/example/BUILD.bazel
similarity index 91%
rename from example/BUILD
rename to example/BUILD.bazel
index d688749d..5ef87e0a 100644
--- a/example/BUILD
+++ b/example/BUILD.bazel
@@ -13,6 +13,9 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+load("@rules_proto//proto:defs.bzl", "proto_library")
+load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_proto_library")
+
 COPTS = [
     "-D__STDC_FORMAT_MACROS",
     "-DBTHREAD_USE_FAST_PTHREAD_MUTEX",
@@ -27,7 +30,7 @@ COPTS = [
     "-fno-omit-frame-pointer",
     "-DGFLAGS_NS=google",
 ] + select({
-    "//:with_glog": ["-DBRPC_WITH_GLOG=1"],
+    "//bazel/config:brpc_with_glog": ["-DBRPC_WITH_GLOG=1"],
     "//conditions:default": ["-DBRPC_WITH_GLOG=0"],
 })
 
@@ -50,6 +53,7 @@ cc_binary(
     srcs = [
         "echo_c++/server.cpp",
     ],
+    copts = COPTS,
     includes = [
         "echo_c++",
     ],
@@ -57,7 +61,6 @@ cc_binary(
         ":cc_echo_c++_proto",
         "//:brpc",
     ],
-    copts = COPTS,
 )
 
 cc_binary(
@@ -65,6 +68,7 @@ cc_binary(
     srcs = [
         "echo_c++/client.cpp",
     ],
+    copts = COPTS,
     includes = [
         "echo_c++",
     ],
@@ -72,5 +76,4 @@ cc_binary(
         ":cc_echo_c++_proto",
         "//:brpc",
     ],
-    copts = COPTS,
 )
diff --git a/.bazelrc b/example/build_with_old_bazel/.bazelrc
similarity index 81%
copy from .bazelrc
copy to example/build_with_old_bazel/.bazelrc
index 8ae4f0d4..14a9b508 100644
--- a/.bazelrc
+++ b/example/build_with_old_bazel/.bazelrc
@@ -13,12 +13,4 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-build --copt -DHAVE_ZLIB=1
-# bazel build with glog
-# build --define=with_glog=true
-build -c opt
 build --incompatible_disable_deprecated_attr_params=false
-build --incompatible_new_actions_api=false
-# unittest 
-test --define=unittest=true
-test --copt=-g
diff --git a/.bazelrc b/example/build_with_old_bazel/.bazelversion
similarity index 75%
copy from .bazelrc
copy to example/build_with_old_bazel/.bazelversion
index 8ae4f0d4..e3eed59f 100644
--- a/.bazelrc
+++ b/example/build_with_old_bazel/.bazelversion
@@ -13,12 +13,4 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-build --copt -DHAVE_ZLIB=1
-# bazel build with glog
-# build --define=with_glog=true
-build -c opt
-build --incompatible_disable_deprecated_attr_params=false
-build --incompatible_new_actions_api=false
-# unittest 
-test --define=unittest=true
-test --copt=-g
+0.28.0
diff --git a/.bazelrc b/example/build_with_old_bazel/BUILD.bazel
similarity index 75%
copy from .bazelrc
copy to example/build_with_old_bazel/BUILD.bazel
index 8ae4f0d4..8394dcb6 100644
--- a/.bazelrc
+++ b/example/build_with_old_bazel/BUILD.bazel
@@ -13,12 +13,16 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-build --copt -DHAVE_ZLIB=1
-# bazel build with glog
-# build --define=with_glog=true
-build -c opt
-build --incompatible_disable_deprecated_attr_params=false
-build --incompatible_new_actions_api=false
-# unittest 
-test --define=unittest=true
-test --copt=-g
+genrule(
+    name = "empty_cc",
+    outs = ["empty.cc"],
+    cmd = "echo 'int main(){return 0;}' > $@",
+)
+
+cc_binary(
+    name = "empty",
+    srcs = [":empty_cc"],
+    deps = [
+        "@com_github_brpc_brpc//:brpc",
+    ],
+)
diff --git a/example/build_with_old_bazel/WORKSPACE b/example/build_with_old_bazel/WORKSPACE
new file mode 100644
index 00000000..02ef4c52
--- /dev/null
+++ b/example/build_with_old_bazel/WORKSPACE
@@ -0,0 +1,134 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+#
+# WARNING: This example is not a best practice for how to build with bRPC in bazel.
+#
+
+workspace(name = "com_github_brpc_brpc_example_build_with_old_bazel")
+
+load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
+
+#
+# Constants
+#
+
+BAZEL_SKYLIB_VERSION = "1.1.1"  # 2021-09-27T17:33:49Z
+
+BAZEL_SKYLIB_SHA256 = "c6966ec828da198c5d9adbaa94c05e3a1c7f21bd012a0b29ba8ddbccb2c93b0d"
+
+RULES_PROTO_COMMIT_ID = "9f8407ec90b579cba157ce481682b2beb1f7409f"
+
+RULES_PROTO_SHA256 = "3a27bf90d4cd3e4546afa801857d35c3c4db5f0680c840167f6fb2f7078de177"
+
+RULES_CC_COMMIT_ID = "b7fe9697c0c76ab2fd431a891dbb9a6a32ed7c3e"
+
+RULES_CC_SHA256 = "29daf0159f0cf552fcff60b49d8bcd4f08f08506d2da6e41b07058ec50cfeaec"
+
+#
+# Starlark libraries
+#
+
+http_archive(
+    name = "bazel_skylib",
+    sha256 = BAZEL_SKYLIB_SHA256,
+    urls = [
+        "https://github.com/bazelbuild/bazel-skylib/releases/download/{version}/bazel-skylib-{version}.tar.gz".format(version = BAZEL_SKYLIB_VERSION),
+        "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/{version}/bazel-skylib-{version}.tar.gz".format(version = BAZEL_SKYLIB_VERSION),
+    ],
+)
+
+http_archive(
+    name = "rules_proto",
+    sha256 = RULES_PROTO_SHA256,
+    strip_prefix = "rules_proto-{version}".format(version = RULES_PROTO_COMMIT_ID),
+    urls = ["https://github.com/bazelbuild/rules_proto/archive/{version}.tar.gz".format(version = RULES_PROTO_COMMIT_ID)],
+)
+
+http_archive(
+    name = "rules_cc",
+    sha256 = RULES_CC_SHA256,
+    strip_prefix = "rules_cc-{commit_id}".format(commit_id = RULES_CC_COMMIT_ID),
+    urls = [
+        "https://github.com/bazelbuild/rules_cc/archive/{commit_id}.tar.gz".format(commit_id = RULES_CC_COMMIT_ID),
+    ],
+)
+
+#
+# C++ Dependencies
+#
+# Ordered lexicographical.
+#
+
+local_repository(
+    name = "com_github_brpc_brpc",
+    path = "../../",
+)
+
+http_archive(
+    name = "com_github_gflags_gflags",
+    sha256 = "a8263376b409900dd46830e4e34803a170484707327854cc252fc5865275a57d",
+    strip_prefix = "gflags-46f73f88b18aee341538c0dfc22b1710a6abedef",
+    url = "https://github.com/gflags/gflags/archive/46f73f88b18aee341538c0dfc22b1710a6abedef.tar.gz",
+)
+
+http_archive(
+    name = "com_github_google_glog",
+    build_file = "//:glog.BUILD",
+    strip_prefix = "glog-a6a166db069520dbbd653c97c2e5b12e08a8bb26",
+    url = "https://github.com/google/glog/archive/a6a166db069520dbbd653c97c2e5b12e08a8bb26.tar.gz",
+)
+
+http_archive(
+    name = "com_github_google_leveldb",
+    build_file = "//:leveldb.BUILD",
+    sha256 = "3912ac36dbb264a62797d68687711c8024919640d89b6733f9342ada1d16cda1",
+    strip_prefix = "leveldb-a53934a3ae1244679f812d998a4f16f2c7f309a6",
+    url = "https://github.com/google/leveldb/archive/a53934a3ae1244679f812d998a4f16f2c7f309a6.tar.gz",
+)
+
+http_archive(
+    name = "com_github_madler_zlib",  # 2017-01-15T17:57:23Z
+    build_file = "@com_github_brpc_brpc//bazel/third_party/zlib:zlib.BUILD",
+    sha256 = "c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1",
+    strip_prefix = "zlib-1.2.11",
+    urls = ["https://zlib.net/zlib-1.2.11.tar.gz"],
+)
+
+http_archive(
+    name = "com_google_googletest",
+    strip_prefix = "googletest-0fe96607d85cf3a25ac40da369db62bbee2939a5",
+    url = "https://github.com/google/googletest/archive/0fe96607d85cf3a25ac40da369db62bbee2939a5.tar.gz",
+)
+
+http_archive(
+    name = "com_google_protobuf",
+    sha256 = "9510dd2afc29e7245e9e884336f848c8a6600a14ae726adb6befdb4f786f0be2",
+    strip_prefix = "protobuf-3.6.1.3",
+    type = "zip",
+    url = "https://github.com/protocolbuffers/protobuf/archive/v3.6.1.3.zip",
+)
+
+# This is not a correct approach, just for simplicity.
+# rules_foreign_cc didn't support too early version of bazel.
+# bRPC need to be patched to work with boringssl for now.
+
+new_local_repository(
+    name = "openssl",
+    build_file = "//:openssl.BUILD",
+    path = "/usr",
+)
diff --git a/leveldb.BUILD b/example/build_with_old_bazel/leveldb.BUILD
similarity index 100%
rename from leveldb.BUILD
rename to example/build_with_old_bazel/leveldb.BUILD
diff --git a/openssl.BUILD b/example/build_with_old_bazel/openssl.BUILD
similarity index 51%
rename from openssl.BUILD
rename to example/build_with_old_bazel/openssl.BUILD
index e2d02eb0..92a687a0 100644
--- a/openssl.BUILD
+++ b/example/build_with_old_bazel/openssl.BUILD
@@ -1,3 +1,18 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
 package(
     default_visibility=["//visibility:public"]
 )
diff --git a/.bazelrc b/example/build_with_old_bazel/zlib.BUILD
similarity index 75%
copy from .bazelrc
copy to example/build_with_old_bazel/zlib.BUILD
index 8ae4f0d4..0830cc0f 100644
--- a/.bazelrc
+++ b/example/build_with_old_bazel/zlib.BUILD
@@ -13,12 +13,11 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-build --copt -DHAVE_ZLIB=1
-# bazel build with glog
-# build --define=with_glog=true
-build -c opt
-build --incompatible_disable_deprecated_attr_params=false
-build --incompatible_new_actions_api=false
-# unittest 
-test --define=unittest=true
-test --copt=-g
+package(
+    default_visibility=["//visibility:public"]
+)
+
+cc_library(
+    name = "zlib",
+    linkopts = ["-lz"],
+)
diff --git a/glog.BUILD b/glog.BUILD
deleted file mode 100644
index c8f96dc8..00000000
--- a/glog.BUILD
+++ /dev/null
@@ -1,171 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one or more
-# contributor license agreements.  See the NOTICE file distributed with
-# this work for additional information regarding copyright ownership.
-# The ASF licenses this file to You under the Apache License, Version 2.0
-# (the "License"); you may not use this file except in compliance with
-# the License.  You may obtain a copy of the License at
-#
-#    http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-licenses(["notice"])
-
-cc_library(
-    name = "glog",
-    srcs = [
-        "src/base/commandlineflags.h",
-        "src/base/googleinit.h",
-        "src/demangle.cc",
-        "src/logging.cc",
-        "src/raw_logging.cc",
-        "src/symbolize.cc",
-        "src/utilities.cc",
-        "src/vlog_is_on.cc",
-    ],
-    hdrs = [
-        "raw_logging_h",
-        "src/base/mutex.h",
-        "src/demangle.h",
-        "src/symbolize.h",
-        "src/utilities.h",
-        "src/glog/log_severity.h",
-        ":config_h",
-        ":logging_h",
-        ":stl_logging_h",
-        ":vlog_is_on_h",
-    ],
-    copts = [
-        # Disable warnings that exists in glog
-        "-Wno-sign-compare",
-        "-Wno-unused-local-typedefs",
-        # Inject google namespace as "google"
-        "-D_START_GOOGLE_NAMESPACE_='namespace google {'",
-        "-D_END_GOOGLE_NAMESPACE_='}'",
-        "-DGOOGLE_NAMESPACE='google'",
-        # Allows src/base/mutex.h to include pthread.h.
-        "-DHAVE_PTHREAD",
-        # Allows src/logging.cc to determine the host name.
-        "-DHAVE_SYS_UTSNAME_H",
-        # System header files enabler for src/utilities.cc
-        # Enable system calls from syscall.h
-        "-DHAVE_SYS_SYSCALL_H",
-        # Enable system calls from sys/time.h
-        "-DHAVE_SYS_TIME_H",
-        "-DHAVE_STDINT_H",
-        "-DHAVE_STRING_H",
-        # For logging.cc
-        "-DHAVE_PREAD",
-        "-DHAVE_FCNTL",
-        "-DHAVE_SYS_TYPES_H",
-        # Allows syslog support
-        "-DHAVE_SYSLOG_H",
-        # GFlags
-        "-isystem $(GENDIR)/external/com_github_gflags_gflags/",
-        "-DHAVE_LIB_GFLAGS",
-        # Necessary for creating soft links of log files
-        "-DHAVE_UNISTD_H",
-    ],
-    includes = [
-        ".",
-        "src",
-    ],
-    visibility = ["//visibility:public"],
-    deps = [
-        "//external:gflags",
-    ],
-)
-
-# Below are the generation rules that generates the necessary header
-# files for glog. Originally they are generated by CMAKE
-# configure_file() command, which replaces certain template
-# placeholders in the .in files with provided values.
-
-# gen_sh is a bash script that provides the values for generated
-# header files. Under the hood it is just a wrapper over sed.
-genrule(
-    name = "gen_sh",
-    outs = [
-        "gen.sh",
-    ],
-    cmd = """
-cat > $@ <<"EOF"
-#! /bin/sh
-sed -e 's/@ac_cv_have_unistd_h@/1/g' \
-    -e 's/@ac_cv_have_stdint_h@/1/g' \
-    -e 's/@ac_cv_have_systypes_h@/1/g' \
-    -e 's/@ac_cv_have_libgflags_h@/1/g' \
-    -e 's/@ac_cv_have_uint16_t@/1/g' \
-    -e 's/@ac_cv_have___builtin_expect@/1/g' \
-    -e 's/@ac_cv_have_.*@/0/g' \
-    -e 's/@ac_google_start_namespace@/namespace google {/g' \
-    -e 's/@ac_google_end_namespace@/}/g' \
-    -e 's/@ac_google_namespace@/google/g' \
-    -e 's/@ac_cv___attribute___noinline@/__attribute__((noinline))/g' \
-    -e 's/@ac_cv___attribute___noreturn@/__attribute__((noreturn))/g' \
-    -e 's/@ac_cv___attribute___printf_4_5@/__attribute__((__format__ (__printf__, 4, 5)))/g'
-EOF""",
-)
-
-genrule(
-    name = "config_h",
-    srcs = [
-        "src/config.h.cmake.in",
-    ],
-    outs = [
-        "config.h",
-    ],
-    cmd = "awk '{ gsub(/^#cmakedefine/, \"//cmakedefine\"); print; }' $(<) > $(@)",
-)
-
-genrule(
-    name = "logging_h",
-    srcs = [
-        "src/glog/logging.h.in",
-    ],
-    outs = [
-        "glog/logging.h",
-    ],
-    cmd = "$(location :gen_sh) < $(<) > $(@)",
-    tools = [":gen_sh"],
-)
-
-genrule(
-    name = "raw_logging_h",
-    srcs = [
-        "src/glog/raw_logging.h.in",
-    ],
-    outs = [
-        "glog/raw_logging.h",
-    ],
-    cmd = "$(location :gen_sh) < $(<) > $(@)",
-    tools = [":gen_sh"],
-)
-
-genrule(
-    name = "stl_logging_h",
-    srcs = [
-        "src/glog/stl_logging.h.in",
-    ],
-    outs = [
-        "glog/stl_logging.h",
-    ],
-    cmd = "$(location :gen_sh) < $(<) > $(@)",
-    tools = [":gen_sh"],
-)
-
-genrule(
-    name = "vlog_is_on_h",
-    srcs = [
-        "src/glog/vlog_is_on.h.in",
-    ],
-    outs = [
-        "glog/vlog_is_on.h",
-    ],
-    cmd = "$(location :gen_sh) < $(<) > $(@)",
-    tools = [":gen_sh"],
-)
diff --git a/test/BUILD.bazel b/test/BUILD.bazel
index a170eaf8..a2eea9cf 100644
--- a/test/BUILD.bazel
+++ b/test/BUILD.bazel
@@ -13,13 +13,10 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-load("//:bazel/brpc.bzl", "brpc_proto_library")
-
-config_setting(
-    name = "darwin",
-    values = {"cpu": "darwin"},
-    visibility = ["//visibility:public"],
-)
+load("@rules_proto//proto:defs.bzl", "proto_library")
+load("@rules_cc//cc:defs.bzl", "cc_library", "cc_proto_library", "cc_test")
+load("@com_grail_bazel_compdb//:defs.bzl", "compilation_database")
+load("@com_grail_bazel_output_base_util//:defs.bzl", "OUTPUT_BASE")
 
 COPTS = [
     "-D__STDC_FORMAT_MACROS",
@@ -41,24 +38,10 @@ COPTS = [
     "-DBVAR_NOT_LINK_DEFAULT_VARIABLES",
     "-DUNIT_TEST",
 ] + select({
-    "//:with_glog": ["-DBRPC_WITH_GLOG=1"],
+    "//bazel/config:brpc_with_glog": ["-DBRPC_WITH_GLOG=1"],
     "//conditions:default": ["-DBRPC_WITH_GLOG=0"],
 })
 
-LINKOPTS = [
-    "-lpthread",
-    "-lssl",
-    "-lcrypto",
-    "-ldl",
-    "-lz",
-] + select({
-    ":darwin": [],
-    "//conditions:default": [
-      "-lrt",
-
-    ],
-})
-
 TEST_BUTIL_SOURCES = [
     "at_exit_unittest.cc",
     "atomicops_unittest.cc",
@@ -157,38 +140,39 @@ TEST_BUTIL_SOURCES = [
     "bounded_queue_unittest.cc",
     "butil_unittest_main.cpp",
 ] + select({
-        "@bazel_tools//tools/osx:darwin": [],
-        "//conditions:default": [
-           "test_file_util_linux.cc",
-           "proc_maps_linux_unittest.cc",
-        ],
+    "@bazel_tools//tools/osx:darwin": [],
+    "//conditions:default": [
+        "test_file_util_linux.cc",
+        "proc_maps_linux_unittest.cc",
+    ],
 })
 
 proto_library(
     name = "test_proto",
-    srcs = glob([
-        "*.proto",
-      ],
-      exclude = [
-          "echo.proto",
-      ]
+    srcs = glob(
+        [
+            "*.proto",
+        ],
+        exclude = [
+            "echo.proto",
+        ],
     ),
     visibility = ["//visibility:public"],
 )
 
 cc_proto_library(
     name = "cc_test_proto",
+    visibility = ["//visibility:public"],
     deps = [
         ":test_proto",
     ],
-    visibility = ["//visibility:public"],
 )
 
 cc_library(
     name = "sstream_workaround",
     hdrs = [
         "sstream_workaround.h",
-    ]
+    ],
 )
 
 cc_test(
@@ -198,53 +182,74 @@ cc_test(
         "multiprocess_func_list.h",
         "test_switches.h",
     ],
+    copts = COPTS,
     deps = [
-        ":sstream_workaround",
         ":cc_test_proto",
+        ":sstream_workaround",
         "//:brpc",
         "@com_google_googletest//:gtest",
     ],
-    copts = COPTS,
 )
 
 cc_test(
     name = "bvar_test",
-    srcs = glob([
-        "bvar_*_unittest.cpp",
-    ],
-    exclude = [
-        "bvar_lock_timer_unittest.cpp",
-        "bvar_recorder_unittest.cpp",
-    ]),
+    srcs = glob(
+        [
+            "bvar_*_unittest.cpp",
+        ],
+        exclude = [
+            "bvar_lock_timer_unittest.cpp",
+            "bvar_recorder_unittest.cpp",
+        ],
+    ),
+    copts = COPTS,
     deps = [
         ":sstream_workaround",
         "//:bvar",
         "@com_google_googletest//:gtest",
     ],
-    copts = COPTS,
 )
 
 cc_test(
     name = "bthread_test",
-    srcs = glob([
-        "bthread_*_unittest.cpp",
-    ],
-    exclude = [
-        "bthread_cond_unittest.cpp",
-        "bthread_execution_queue_unittest.cpp",
-        "bthread_dispatcher_unittest.cpp",
-        "bthread_fd_unittest.cpp",
-        "bthread_mutex_unittest.cpp",
-        "bthread_setconcurrency_unittest.cpp",
-        # glog CHECK die with a fatal error
-        "bthread_key_unittest.cpp"
-    ]),
+    srcs = glob(
+        [
+            "bthread_*_unittest.cpp",
+        ],
+        exclude = [
+            "bthread_cond_unittest.cpp",
+            "bthread_execution_queue_unittest.cpp",
+            "bthread_dispatcher_unittest.cpp",
+            "bthread_fd_unittest.cpp",
+            "bthread_mutex_unittest.cpp",
+            "bthread_setconcurrency_unittest.cpp",
+            # glog CHECK die with a fatal error
+            "bthread_key_unittest.cpp",
+        ],
+    ),
+    copts = COPTS,
     deps = [
         ":sstream_workaround",
         "//:brpc",
         "@com_google_googletest//:gtest",
         "@com_google_googletest//:gtest_main",
     ],
-    copts = COPTS,
 )
 
+compilation_database(
+    name = "brpc_test_compdb",
+    # Use test profile
+    testonly = True,
+    # OUTPUT_BASE is a dynamic value that will vary for each user workspace.
+    # If you would like your build outputs to be the same across users, then
+    # skip supplying this value, and substitute the default constant value
+    # "__OUTPUT_BASE__" through an external tool like `sed` or `jq` (see
+    # below shell commands for usage).
+    output_base = OUTPUT_BASE,
+    targets = [
+        "//:brpc",
+        ":bvar_test",
+        ":bthread_test",
+        ":butil_test",
+    ],
+)
diff --git a/tools/BUILD b/tools/BUILD
index 04b3abbf..a836cb04 100644
--- a/tools/BUILD
+++ b/tools/BUILD
@@ -27,7 +27,7 @@ COPTS = [
     "-fno-omit-frame-pointer",
     "-DGFLAGS_NS=google",
 ] + select({
-    "//:with_glog": ["-DBRPC_WITH_GLOG=1"],
+    "//bazel/config:brpc_with_glog": ["-DBRPC_WITH_GLOG=1"],
     "//conditions:default": ["-DBRPC_WITH_GLOG=0"],
 })
 
diff --git a/zlib.BUILD b/zlib.BUILD
deleted file mode 100644
index 8b789527..00000000
--- a/zlib.BUILD
+++ /dev/null
@@ -1,8 +0,0 @@
-package(
-    default_visibility=["//visibility:public"]
-)
-
-cc_library(
-    name = "zlib",
-    linkopts = ["-lz"],
-)


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org