Add source distribution packaging rules (#9835)

This change adds `rules_pkg`-based targets that will produce source distribution archives, similar to `make dist`.

These rules produce nearly the same outputs as `make dist`. However, there are some differences and caveats:

1. The outputs do not contain vendored googletest sources.
2. You have to run `autogen.sh` before `blaze build pkg:all`. This produces several autotools-related files directly into the source tree.
3. The output .zip files do not have a directory prefix like `protobuf-3.20.1-rc-1` (this will be addressed after [Substitute package variables in `pkg_zip#package_dir`. bazelbuild/rules_pkg#577](https://github.com/bazelbuild/rules_pkg/pull/577); the tar files do have this prefix, though.)
4. One file is missing from the archives, which is produced during the `make` build: benchmarks/gogo/cpp_no_group/cpp_benchmark.cc
5. In several places, I have explicitly excluded some files that are not in the autotools distribution outputs. I think most of those files should probably be included, but for now, I'm aiming for parity with `make dist`. These are marked with comments, so it should be easy to clean them up later.
diff --git a/BUILD b/BUILD
index e437626..568fd4c 100644
--- a/BUILD
+++ b/BUILD
@@ -2,6 +2,7 @@
 
 load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
 load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test", "objc_library", native_cc_proto_library = "cc_proto_library")
+load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix")
 load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain", "proto_library")
 load("@rules_python//python:defs.bzl", "py_library")
 load("@rules_java//java:defs.bzl", "java_binary", "java_lite_proto_library", "java_proto_library")
@@ -1445,3 +1446,140 @@
     visibility = ["//java:__subpackages__"],
     deps = [":kt_proto3_unittest"],
 )
+
+################################################################################
+# Packaging rules
+################################################################################
+
+# Files included in all source distributions
+pkg_files(
+    name = "common_dist_files",
+    srcs = glob([
+        "*.bzl",
+        "cmake/*.cmake",
+        "cmake/*.in",
+        "editors/*",
+
+        # Several of these files are generated by autogen.sh, so using
+        # glob() lets us ignore them if they are missing. (This is not good
+        # practice, though.)
+        "Makefile.in",
+        "aclocal.m4",
+        "ar-lib",
+        "compile",
+        "config*",
+        "depcomp",
+        "install-sh",
+        "ltmain.sh",
+        "m4/*.m4",
+        "missing",
+        "protobuf*.pc.in",
+        "test-driver",
+    ]) + [
+        "BUILD",
+        "CHANGES.txt",
+        "CMakeLists.txt",
+        "CONTRIBUTORS.txt",
+        "LICENSE",
+        "Makefile.am",
+        "README.md",
+        "WORKSPACE",
+        "autogen.sh",
+        "build_files_updated_unittest.sh",
+        "cmake/CMakeLists.txt",
+        "cmake/README.md",
+        "generate_descriptor_proto.sh",
+        "maven_install.json",
+        "update_file_lists.sh",
+        "//third_party:zlib.BUILD",
+        "//util/python:BUILD",
+    ],
+    strip_prefix = strip_prefix.from_root(""),
+    visibility = ["//pkg:__pkg__"],
+)
+
+# Conformance tests
+pkg_files(
+    name = "conformance_dist_files",
+    srcs = glob(
+        ["conformance/**/*"],
+        exclude = [
+            # The following are not in autotools dist:
+            "conformance/autoload.php",
+            "conformance/conformance_nodejs.js",
+            "conformance/conformance_test_runner.sh",
+            "conformance/failure_list_java_lite.txt",
+            "conformance/failure_list_jruby.txt",
+            "conformance/text_format_failure_list_*.txt",
+            "conformance/update_failure_list.py",
+        ],
+    ),
+    strip_prefix = strip_prefix.from_root(""),
+    visibility = ["//pkg:__pkg__"],
+)
+
+# C++ runtime
+pkg_files(
+    name = "cpp_dist_files",
+    srcs = glob(
+        ["src/**/*"],
+        exclude = [
+            "src/google/protobuf/compiler/objectivec/method_dump.sh",  # not in autotools dist
+        ],
+    ),
+    strip_prefix = strip_prefix.from_root(""),
+    visibility = ["//pkg:__pkg__"],
+)
+
+# Additional files for C#
+pkg_files(
+    name = "csharp_dist_files",
+    srcs = [
+        "global.json",
+    ],
+    visibility = ["//pkg:__pkg__"],
+)
+
+# Additional files for ObjC
+pkg_files(
+    name = "objectivec_dist_files",
+    srcs = [
+        "Protobuf.podspec",
+    ],
+    visibility = ["//pkg:__pkg__"],
+)
+
+# Additional files for PHP
+pkg_files(
+    name = "php_dist_files",
+    srcs = [
+        "composer.json",
+    ],
+    visibility = ["//pkg:__pkg__"],
+)
+
+# Python runtime
+pkg_files(
+    name = "python_dist_files",
+    srcs = glob([
+        "python/google/**/*.proto",
+        "python/google/**/*.py",
+        "python/google/protobuf/internal/*.cc",
+        "python/google/protobuf/pyext/*.cc",
+        "python/google/protobuf/pyext/*.h",
+    ]) + [
+        "python/MANIFEST.in",
+        "python/README.md",
+        "python/google/protobuf/proto_api.h",
+        "python/google/protobuf/pyext/README",
+        "python/google/protobuf/python_protobuf.h",
+        "python/mox.py",
+        "python/release.sh",
+        "python/setup.cfg",
+        "python/setup.py",
+        "python/stubout.py",
+        "python/tox.ini",
+    ],
+    strip_prefix = strip_prefix.from_root(""),
+    visibility = ["//pkg:__pkg__"],
+)