Add support for the Bazel build of QUICHE.

Current limitations:
* Only Linux build has been tested (though MacOS would probably work too).
* Some tests fail when compiled without `-c opt`.
* epoll_server and other Linux-specific targets are not built (including the unit test)

PiperOrigin-RevId: 449624054
diff --git a/.bazelrc b/.bazelrc
new file mode 100644
index 0000000..4748ed8
--- /dev/null
+++ b/.bazelrc
@@ -0,0 +1,8 @@
+build --cxxopt=-std=c++17
+build --cxxopt=-fno-rtti
+
+# Enable Abseil/Googletest integration
+build --define absl=1
+
+# Don't fail on converting "0xff" to char
+build --copt=-Wno-narrowing
diff --git a/BUILD.bazel b/BUILD.bazel
new file mode 100644
index 0000000..044dc1f
--- /dev/null
+++ b/BUILD.bazel
@@ -0,0 +1,7 @@
+# Copyright 2022 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+licenses(["notice"])
+
+exports_files(["LICENSE"])
diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel
new file mode 100644
index 0000000..620ac8a
--- /dev/null
+++ b/WORKSPACE.bazel
@@ -0,0 +1,87 @@
+# Copyright 2022 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+workspace(name = "com_google_quiche")
+
+load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
+
+# -------- Bazel tooling dependencies --------
+
+http_archive(
+    name = "bazel_skylib",
+    sha256 = "f7be3474d42aae265405a592bb7da8e171919d74c16f082a5457840f06054728",  # Last updated 2022-05-18
+    urls = ["https://github.com/bazelbuild/bazel-skylib/releases/download/1.2.1/bazel-skylib-1.2.1.tar.gz"],
+)
+
+# -------- Dependencies used in core QUICHE build --------
+
+http_archive(
+    name = "com_google_absl",
+    sha256 = "44634eae586a7158dceedda7d8fd5cec6d1ebae08c83399f75dd9ce76324de40",  # Last updated 2022-05-18
+    strip_prefix = "abseil-cpp-3e04aade4e7a53aebbbed1a1268117f1f522bfb0",
+    urls = ["https://github.com/abseil/abseil-cpp/archive/3e04aade4e7a53aebbbed1a1268117f1f522bfb0.zip"],
+)
+
+http_archive(
+    name = "com_google_protobuf",
+    sha256 = "8b28fdd45bab62d15db232ec404248901842e5340299a57765e48abe8a80d930",  # Last updated 2022-05-18
+    strip_prefix = "protobuf-3.20.1",
+    urls = ["https://github.com/protocolbuffers/protobuf/archive/refs/tags/v3.20.1.tar.gz"],
+)
+
+http_archive(
+    name = "boringssl",
+    sha256 = "482a59ea63d03fbb4d3cab22e40043fb4eef2cbbdc2eb944cd627675ae8b0cf3",  # Last updated 2022-05-18
+    strip_prefix = "boringssl-02c6711efbb6c9dc6236a1c3779af1bcb0274dfd",
+    urls = ["https://github.com/google/boringssl/archive/02c6711efbb6c9dc6236a1c3779af1bcb0274dfd.tar.gz"],
+)
+
+http_archive(
+    name = "com_google_quic_trace",
+    sha256 = "079331de8c3cbf145a3b57adb3ad4e73d733ecfa84d3486e1c5a9eaeef286549",  # Last updated 2022-05-18
+    strip_prefix = "quic-trace-c7b993eb750e60c307e82f75763600d9c06a6de1",
+    urls = ["https://github.com/google/quic-trace/archive/c7b993eb750e60c307e82f75763600d9c06a6de1.tar.gz"],
+)
+
+http_archive(
+    name = "com_google_googleurl",
+    sha256 = "a1bc96169d34dcc1406ffb750deef3bc8718bd1f9069a2878838e1bd905de989",
+    urls = ["https://storage.googleapis.com/quiche-envoy-integration/googleurl_9cdb1f4d1a365ebdbcbf179dadf7f8aa5ee802e7.tar.gz"],
+)
+
+http_archive(
+    name = "zlib",
+    build_file = "//build:zlib.BUILD",
+    sha256 = "d8688496ea40fb61787500e863cc63c9afcbc524468cedeb478068924eb54932",  # Last updated 2022-05-18
+    strip_prefix = "zlib-1.2.12",
+    urls = ["https://github.com/madler/zlib/archive/refs/tags/v1.2.12.tar.gz"],
+)
+
+# -------- Dependencies used by QUICHE tests and extra tooling --------
+
+http_archive(
+    name = "com_google_googletest",
+    sha256 = "7ee83802222f9392452c57b4757185697a51639b69b64590f2c2188f58618581",  # Last updated 2022-05-18
+    strip_prefix = "googletest-8d51dc50eb7e7698427fed81b85edad0e032112e",
+    urls = ["https://github.com/google/googletest/archive/8d51dc50eb7e7698427fed81b85edad0e032112e.zip"],
+)
+
+# Note this must use a commit from the `abseil` branch of the RE2 project.
+# https://github.com/google/re2/tree/abseil
+http_archive(
+    name = "com_googlesource_code_re2",
+    sha256 = "906d0df8ff48f8d3a00a808827f009a840190f404559f649cb8e4d7143255ef9",
+    strip_prefix = "re2-a276a8c738735a0fe45a6ee590fe2df69bcf4502",
+    urls = ["https://github.com/google/re2/archive/a276a8c738735a0fe45a6ee590fe2df69bcf4502.zip"],  # 2022-04-08
+)
+
+# -------- Load and call dependencies of underlying libraries --------
+
+load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
+
+bazel_skylib_workspace()
+
+load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
+
+protobuf_deps()
diff --git a/build/BUILD.bazel b/build/BUILD.bazel
new file mode 100644
index 0000000..0ba41e4
--- /dev/null
+++ b/build/BUILD.bazel
@@ -0,0 +1,7 @@
+# Copyright 2022 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+licenses(["notice"])
+
+exports_files(["source_list.json"])
diff --git a/build/test.bzl b/build/test.bzl
new file mode 100644
index 0000000..9beb0ca
--- /dev/null
+++ b/build/test.bzl
@@ -0,0 +1,26 @@
+"""Tools for building QUICHE tests."""
+
+load("@bazel_skylib//lib:paths.bzl", "paths")
+
+def test_suite_from_source_list(name, srcs, **kwargs):
+    """
+    Generates a test target for every individual test source file specified.
+
+    Args:
+        name: the name of the resulting test_suite target.
+        srcs: the list of source files from which the test targets are generated.
+        **kwargs: other arguments that are passed to the cc_test rule directly.s
+    """
+
+    tests = []
+    for sourcefile in srcs:
+        if not sourcefile.endswith("_test.cc"):
+            fail("All source files passed to test_suite_from_source_list() must end with _test.cc")
+        test_name, _ = paths.split_extension(paths.basename(sourcefile))
+        native.cc_test(
+            name = test_name,
+            srcs = [sourcefile],
+            **kwargs
+        )
+        tests.append(test_name)
+    native.test_suite(name = name, tests = tests)
diff --git a/build/zlib.BUILD b/build/zlib.BUILD
new file mode 100644
index 0000000..61a5507
--- /dev/null
+++ b/build/zlib.BUILD
@@ -0,0 +1,25 @@
+# Copyright 2022 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+licenses(["notice"])
+
+cc_library(
+    name = "zlib",
+    srcs = glob(
+        include = [
+            "*.c",
+            "*.h",
+        ],
+        exclude = [
+            "zlib.h",
+            "zconf.h",
+        ],
+    ),
+    hdrs = [
+        "zconf.h",
+        "zlib.h",
+    ],
+    copts = ["-Wno-implicit-function-declaration"],
+    visibility = ["//visibility:public"],
+)
diff --git a/quiche/BUILD.bazel b/quiche/BUILD.bazel
new file mode 100644
index 0000000..6650f84
--- /dev/null
+++ b/quiche/BUILD.bazel
@@ -0,0 +1,270 @@
+# Copyright 2022 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+load(
+    "//build:source_list.bzl",
+    "default_platform_impl_hdrs",
+    "default_platform_impl_srcs",
+    "default_platform_impl_test_support_hdrs",
+    "default_platform_impl_test_support_srcs",
+    "default_platform_impl_tool_support_hdrs",
+    "default_platform_impl_tool_support_srcs",
+    "quiche_core_hdrs",
+    "quiche_core_srcs",
+    "quiche_test_support_hdrs",
+    "quiche_test_support_srcs",
+    "quiche_tests_srcs",
+    "quiche_tool_support_hdrs",
+    "quiche_tool_support_srcs",
+)
+load("//build:test.bzl", "test_suite_from_source_list")
+
+licenses(["notice"])
+
+package(
+    default_visibility = ["//visibility:private"],
+    features = [
+        "parse_headers",
+        "layering_check",
+    ],
+)
+
+cc_library(
+    name = "quic_flags_list",
+    textual_hdrs = [
+        "quic/core/quic_flags_list.h",
+        "quic/core/quic_protocol_flags_list.h",
+    ],
+)
+
+# QUICHE_EXPORT is used by all platform definitions, and thus needs to be handled separately.
+cc_library(
+    name = "quiche_platform_default_quiche_export",
+    hdrs = [
+        "common/platform/default/quiche_platform_impl/quiche_export_impl.h",
+    ],
+    strip_include_prefix = "common/platform/default",
+)
+
+cc_library(
+    name = "quiche_platform_quiche_export",
+    hdrs = [
+        "common/platform/api/quiche_export.h",
+    ],
+    deps = [":quiche_platform_default_quiche_export"],
+)
+
+cc_library(
+    name = "quiche_platform_default",
+    srcs = default_platform_impl_srcs,
+    hdrs = default_platform_impl_hdrs,
+    strip_include_prefix = "common/platform/default",
+    deps = [
+        ":quic_flags_list",
+        ":quiche_platform_quiche_export",
+        "@com_google_absl//absl/base:core_headers",
+        "@com_google_absl//absl/container:flat_hash_map",
+        "@com_google_absl//absl/container:flat_hash_set",
+        "@com_google_absl//absl/debugging:stacktrace",
+        "@com_google_absl//absl/debugging:symbolize",
+        "@com_google_absl//absl/strings",
+        "@com_google_absl//absl/strings:str_format",
+        "@com_google_absl//absl/synchronization",
+        "@com_google_absl//absl/time",
+        "@com_google_absl//absl/types:optional",
+        "@com_google_googleurl//url",
+    ],
+)
+
+cc_library(
+    name = "quiche_platform_default_tools",
+    srcs = default_platform_impl_tool_support_srcs,
+    hdrs = default_platform_impl_tool_support_hdrs,
+    strip_include_prefix = "common/platform/default",
+    deps = [
+        ":quiche_core",
+        "@com_google_absl//absl/flags:flag",
+        "@com_google_absl//absl/flags:parse",
+        "@com_google_absl//absl/flags:usage",
+        "@com_google_absl//absl/strings",
+        "@com_google_absl//absl/types:optional",
+        "@com_google_googletest//:gtest",
+    ],
+)
+
+cc_library(
+    name = "quiche_platform_default_testonly",
+    testonly = 1,
+    srcs = default_platform_impl_test_support_srcs,
+    hdrs = default_platform_impl_test_support_hdrs,
+    strip_include_prefix = "common/platform/default",
+    deps = [
+        ":quiche_core",
+        "@com_google_absl//absl/flags:flag",
+        "@com_google_absl//absl/flags:parse",
+        "@com_google_absl//absl/flags:usage",
+        "@com_google_googletest//:gtest",
+    ],
+)
+
+proto_library(
+    name = "quiche_protobufs",
+    srcs = [
+        "quic/core/proto/cached_network_parameters.proto",
+        "quic/core/proto/crypto_server_config.proto",
+        "quic/core/proto/source_address_token.proto",
+    ],
+)
+
+cc_proto_library(
+    name = "quiche_protobufs_cc_proto",
+    deps = [":quiche_protobufs"],
+)
+
+proto_library(
+    name = "quiche_protobufs_testonly",
+    srcs = [
+        "quic/test_tools/send_algorithm_test_result.proto",
+    ],
+)
+
+cc_proto_library(
+    name = "quiche_protobufs_testonly_cc_proto",
+    deps = [":quiche_protobufs_testonly"],
+)
+
+cc_library(
+    name = "quiche_core",
+    srcs = quiche_core_srcs,
+    hdrs = quiche_core_hdrs,
+    textual_hdrs = ["http2/hpack/hpack_static_table_entries.inc"],
+    deps = [
+        ":quiche_platform_default",
+        ":quiche_protobufs_cc_proto",
+        "@boringssl//:crypto",
+        "@boringssl//:ssl",
+        "@com_google_absl//absl/algorithm:container",
+        "@com_google_absl//absl/base:core_headers",
+        "@com_google_absl//absl/container:btree",
+        "@com_google_absl//absl/container:flat_hash_map",
+        "@com_google_absl//absl/container:flat_hash_set",
+        "@com_google_absl//absl/container:inlined_vector",
+        "@com_google_absl//absl/container:node_hash_map",
+        "@com_google_absl//absl/hash",
+        "@com_google_absl//absl/memory",
+        "@com_google_absl//absl/numeric:int128",
+        "@com_google_absl//absl/strings",
+        "@com_google_absl//absl/strings:str_format",
+        "@com_google_absl//absl/types:optional",
+        "@com_google_absl//absl/types:span",
+        "@com_google_googleurl//url",
+        "@com_google_quic_trace//quic_trace:quic_trace_cc_proto",
+        "@zlib",
+    ],
+)
+
+cc_library(
+    name = "quiche_tool_support",
+    srcs = quiche_tool_support_srcs,
+    hdrs = quiche_tool_support_hdrs,
+    deps = [
+        ":quiche_core",
+        ":quiche_platform_default_tools",
+        "@boringssl//:crypto",
+        "@com_google_absl//absl/base:core_headers",
+        "@com_google_absl//absl/container:flat_hash_map",
+        "@com_google_absl//absl/container:flat_hash_set",
+        "@com_google_absl//absl/memory",
+        "@com_google_absl//absl/strings",
+        "@com_google_absl//absl/strings:str_format",
+        "@com_google_absl//absl/types:optional",
+        "@com_google_googletest//:gtest",
+        "@com_google_googleurl//url",
+    ],
+)
+
+cc_library(
+    name = "quiche_test_support",
+    testonly = 1,
+    srcs = quiche_test_support_srcs,
+    hdrs = quiche_test_support_hdrs,
+    deps = [
+        ":quiche_core",
+        ":quiche_platform_default_testonly",
+        ":quiche_protobufs_testonly_cc_proto",
+        ":quiche_tool_support",
+        "@boringssl//:crypto",
+        "@boringssl//:ssl",
+        "@com_google_absl//absl/base:core_headers",
+        "@com_google_absl//absl/container:flat_hash_map",
+        "@com_google_absl//absl/container:flat_hash_set",
+        "@com_google_absl//absl/memory",
+        "@com_google_absl//absl/strings",
+        "@com_google_absl//absl/strings:str_format",
+        "@com_google_absl//absl/types:optional",
+        "@com_google_googletest//:gtest",
+    ],
+)
+
+test_suite_from_source_list(
+    name = "quiche_tests",
+    srcs = quiche_tests_srcs,
+    data = glob([
+        "common/platform/api/testdir/**",
+        "quic/test_tools/quic_http_response_cache_data/**",
+    ]),
+    deps = [
+        ":quiche_core",
+        ":quiche_platform_default_testonly",
+        ":quiche_protobufs_testonly_cc_proto",
+        ":quiche_test_support",
+        ":quiche_tool_support",
+        "@boringssl//:crypto",
+        "@boringssl//:ssl",
+        "@com_google_absl//absl/algorithm:container",
+        "@com_google_absl//absl/base:core_headers",
+        "@com_google_absl//absl/container:flat_hash_map",
+        "@com_google_absl//absl/container:flat_hash_set",
+        "@com_google_absl//absl/container:node_hash_map",
+        "@com_google_absl//absl/functional:bind_front",
+        "@com_google_absl//absl/hash",
+        "@com_google_absl//absl/memory",
+        "@com_google_absl//absl/numeric:int128",
+        "@com_google_absl//absl/strings",
+        "@com_google_absl//absl/strings:str_format",
+        "@com_google_absl//absl/types:optional",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
+
+# TODO(vasilvv): make a rule that generates cc_binary rules for all _bin targets.
+cc_binary(
+    name = "quic_packet_printer",
+    srcs = ["quic/tools/quic_packet_printer_bin.cc"],
+    deps = [
+        ":quiche_core",
+        ":quiche_tool_support",
+        "@com_google_absl//absl/strings",
+    ],
+)
+
+# Indicate that QUICHE APIs are explicitly unstable by providing only
+# appropriately named aliases as publicly visible targets.
+alias(
+    name = "quiche_unstable_api",
+    actual = ":quiche_core",
+    visibility = ["//visibility:public"],
+)
+
+alias(
+    name = "quiche_unstable_api_tool_support",
+    actual = ":quiche_tool_support",
+    visibility = ["//visibility:public"],
+)
+
+alias(
+    name = "quiche_unstable_api_test_support",
+    actual = ":quiche_test_support",
+    visibility = ["//visibility:public"],
+)