# Copyright (c) 2009-2022, Google LLC
# All rights reserved.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd

load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//lib:selects.bzl", "selects")
load("@protobuf_pip_deps//:requirements.bzl", "requirement")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
load("@rules_python//python:packaging.bzl", "py_wheel")
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
load("@system_python//:version.bzl", "SYSTEM_PYTHON_VERSION")
load("//:protobuf_version.bzl", "PROTOBUF_PYTHON_VERSION")
load(":dist.bzl", "py_dist", "py_dist_module")
load(":py_proto_library.bzl", "py_proto_library")

licenses(["notice"])

py_dist_module(
    name = "message_mod",
    extension = "//python:_message_binary",
    module_name = "google._upb._message",
)

py_proto_library(
    name = "well_known_proto_py_pb2",
    deps = [
        "//:any_proto",
        "//:api_proto",
        "//:descriptor_proto",
        "//:duration_proto",
        "//:empty_proto",
        "//:field_mask_proto",
        "//:source_context_proto",
        "//:struct_proto",
        "//:timestamp_proto",
        "//:type_proto",
        "//:wrappers_proto",
    ],
)

py_proto_library(
    name = "plugin_py_pb2",
    deps = ["//:compiler_plugin_proto"],
)

config_setting(
    name = "linux_aarch64_release",
    constraint_values = [
        "@platforms//os:linux",
        "@platforms//cpu:aarch64",
    ],
    flag_values = {
        "//toolchain:release": "True",
    },
)

config_setting(
    name = "linux_aarch64_local",
    constraint_values = [
        "@platforms//os:linux",
        "@platforms//cpu:aarch64",
    ],
    flag_values = {
        "//toolchain:release": "False",
    },
)

config_setting(
    name = "linux_x86_64_release",
    constraint_values = [
        "@platforms//os:linux",
        "@platforms//cpu:x86_64",
    ],
    flag_values = {
        "//toolchain:release": "True",
    },
)

config_setting(
    name = "linux_x86_64_local",
    constraint_values = [
        "@platforms//os:linux",
        "@platforms//cpu:x86_64",
    ],
    flag_values = {
        "//toolchain:release": "False",
    },
)

config_setting(
    name = "linux_s390x_release",
    constraint_values = [
        "@platforms//os:linux",
        "@platforms//cpu:s390x",
    ],
    flag_values = {
        "//toolchain:release": "True",
    },
)

config_setting(
    name = "linux_s390x_local",
    constraint_values = [
        "@platforms//os:linux",
        "@platforms//cpu:s390x",
    ],
    flag_values = {
        "//toolchain:release": "False",
    },
)

config_setting(
    name = "osx_x86_64_release",
    constraint_values = [
        "@platforms//os:osx",
        "@platforms//cpu:x86_64",
    ],
    flag_values = {
        "//toolchain:release": "True",
    },
)

config_setting(
    name = "osx_x86_64_local",
    constraint_values = [
        "@platforms//os:osx",
        "@platforms//cpu:x86_64",
    ],
    flag_values = {
        "//toolchain:release": "False",
    },
)

selects.config_setting_group(
    name = "osx_x86_64",
    match_any = [
        ":osx_x86_64_release",
        ":osx_x86_64_local",
    ],
    visibility = ["//python:__subpackages__"],
)

config_setting(
    name = "osx_aarch64_release",
    constraint_values = [
        "@platforms//os:osx",
        "@platforms//cpu:aarch64",
    ],
    flag_values = {
        "//toolchain:release": "True",
    },
)

config_setting(
    name = "osx_aarch64_local",
    constraint_values = [
        "@platforms//os:osx",
        "@platforms//cpu:aarch64",
    ],
    flag_values = {
        "//toolchain:release": "False",
    },
)

selects.config_setting_group(
    name = "osx_aarch64",
    match_any = [
        ":osx_aarch64_release",
        ":osx_aarch64_local",
    ],
    visibility = ["//python:__subpackages__"],
)

config_setting(
    name = "osx_universal2",
    constraint_values = [
        "@platforms//os:osx",
    ],
)

config_setting(
    name = "windows_x86_32_release",
    constraint_values = [
        "@platforms//os:windows",
        "@platforms//cpu:x86_32",
    ],
    flag_values = {
        "//toolchain:release": "True",
    },
)

config_setting(
    name = "windows_x86_32_local",
    constraint_values = [
        "@platforms//os:windows",
        "@platforms//cpu:x86_32",
    ],
    flag_values = {
        "//toolchain:release": "False",
    },
)

selects.config_setting_group(
    name = "windows_x86_32",
    match_any = [
        ":windows_x86_32_release",
        ":windows_x86_32_local",
    ],
    visibility = ["//python:__subpackages__"],
)

config_setting(
    name = "windows_x86_64_release",
    constraint_values = [
        "@platforms//os:windows",
        "@platforms//cpu:x86_64",
    ],
    flag_values = {
        "//toolchain:release": "True",
    },
)

config_setting(
    name = "windows_x86_64_local",
    constraint_values = [
        "@platforms//os:windows",
        "@platforms//cpu:x86_64",
    ],
    flag_values = {
        "//toolchain:release": "False",
    },
)

selects.config_setting_group(
    name = "windows_x86_64",
    match_any = [
        ":windows_x86_64_release",
        ":windows_x86_64_local",
    ],
)

pkg_files(
    name = "generated_wkt",
    srcs = [
        ":well_known_proto_py_pb2",
        "//src/google/protobuf:descriptor_upb_c_proto",
        "//src/google/protobuf:descriptor_upb_minitable_proto",
        "//src/google/protobuf:descriptor_upb_reflection_proto",
        "//upb/reflection:descriptor_upb_proto",
    ],
    prefix = "google/protobuf",
)

pkg_files(
    name = "generated_wkt_compiler",
    srcs = [
        ":plugin_py_pb2",
    ],
    prefix = "google/protobuf/compiler",
)

pkg_files(
    name = "utf8_range_source_files",
    srcs = ["//third_party/utf8_range:utf8_range_srcs"],
    prefix = "utf8_range",
)

pkg_files(
    name = "dist_source_files",
    srcs = [
        "MANIFEST.in",
        "setup.py",
    ],
)

# Passing filegroups to pkg_tar directly results in incorrect
# `protobuf/external/upb/` directory structure when built from the protobuf
# repo. This can be removed once repositories are merged.
pkg_files(
    name = "filegroup_source_files",
    srcs = [
        "//:LICENSE",
        "//python:message_srcs",
        "//upb:source_files",
        "//upb/base:source_files",
        "//upb/hash:source_files",
        "//upb/lex:source_files",
        "//upb/mem:source_files",
        "//upb/message:source_files",
        "//upb/mini_descriptor:source_files",
        "//upb/mini_table:source_files",
        "//upb/port:source_files",
        "//upb/reflection:source_files",
        "//upb/text:source_files",
        "//upb/util:source_files",
        "//upb/wire:source_files",
    ],
    strip_prefix = strip_prefix.from_root(""),
)

# NOTE: This package currently only works for macos and ubuntu, MSVC users
# should use a binary wheel.
pkg_tar(
    name = "source_tarball",
    srcs = [
        ":dist_source_files",
        ":filegroup_source_files",
        ":generated_wkt",
        ":generated_wkt_compiler",
        ":utf8_range_source_files",
        "//python:python_source_files",
    ],
    extension = "tar.gz",
    package_dir = "protobuf",
    package_file_name = "protobuf.tar.gz",
    strip_prefix = ".",
    target_compatible_with = select({
        "@system_python//:none": ["@platforms//:incompatible"],
        "//conditions:default": [],
    }),
)

sh_binary(
    name = "setup_wrapper",
    srcs = ["setup_wrapper.sh"],
    data = [requirement("setuptools")],
    visibility = ["//visibility:private"],
)

genrule(
    name = "source_wheel",
    srcs = [":source_tarball"],
    outs = ["protobuf-%s.tar.gz" % PROTOBUF_PYTHON_VERSION],
    cmd = """
        set -eux
        tar -xzvf $(location :source_tarball)
        $(location :setup_wrapper) sdist
        mv protobuf/dist/*.tar.gz $@
    """,
    target_compatible_with = select({
        "@system_python//:none": ["@platforms//:incompatible"],
        "//conditions:default": [],
    }),
    tools = [":setup_wrapper"],
)

py_wheel(
    name = "binary_wheel",
    abi = select({
        "//conditions:default": "abi3",
    }),
    author = "protobuf@googlegroups.com",
    author_email = "protobuf@googlegroups.com",
    classifiers = [
        "Programming Language :: Python",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.10",
        "Programming Language :: Python :: 3.11",
        "Programming Language :: Python :: 3.12",
        "Programming Language :: Python :: 3.13",
        "Programming Language :: Python :: 3.14",
    ],
    distribution = "protobuf",
    extra_distinfo_files = {
        "//:LICENSE": "LICENSE",
    },
    homepage = "https://developers.google.com/protocol-buffers/",
    license = "3-Clause BSD License",
    platform = select({
        ":linux_x86_64_local": "linux_x86_64",
        ":linux_x86_64_release": "manylinux2014_x86_64",
        ":linux_aarch64_local": "linux_aarch64",
        ":linux_aarch64_release": "manylinux2014_aarch64",
        ":linux_s390x_local": "linux_s390x",
        ":linux_s390x_release": "manylinux2014_s390x",
        ":osx_universal2": "macosx_10_9_universal2",
        ":osx_aarch64": "macosx_11_0_arm64",
        ":windows_x86_32": "win32",
        ":windows_x86_64": "win_amd64",
        "//conditions:default": "any",
    }),
    python_requires = ">=3.10",
    # LINT.IfChange(python_tag)
    python_tag = selects.with_or({
        "//python:limited_api_3.10": "cp310",
        "//conditions:default": "cp" + SYSTEM_PYTHON_VERSION,
    }),
    # LINT.ThenChange(
    #    :limited_api_wheels,
    # )
    strip_path_prefixes = [
        "python/dist/",
        "python/",
        "src/",
    ],
    target_compatible_with = select({
        "@system_python//:none": ["@platforms//:incompatible"],
        "//conditions:default": [],
    }),
    version = PROTOBUF_PYTHON_VERSION,
    deps = [
        ":message_mod",
        ":plugin_py_pb2",
        ":well_known_proto_py_pb2",
        "//:python_srcs",
    ],
)

py_wheel(
    name = "pure_python_wheel",
    abi = "none",
    author = "protobuf@googlegroups.com",
    author_email = "protobuf@googlegroups.com",
    classifiers = [
        "Programming Language :: Python",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.10",
        "Programming Language :: Python :: 3.11",
        "Programming Language :: Python :: 3.12",
        "Programming Language :: Python :: 3.13",
        "Programming Language :: Python :: 3.14",
    ],
    distribution = "protobuf",
    extra_distinfo_files = {
        "//:LICENSE": "LICENSE",
    },
    homepage = "https://developers.google.com/protocol-buffers/",
    license = "3-Clause BSD License",
    platform = "any",
    python_requires = ">=3.10",
    python_tag = "py3",
    strip_path_prefixes = [
        "python/",
        "src/",
    ],
    target_compatible_with = select({
        "@system_python//:none": ["@platforms//:incompatible"],
        "//conditions:default": [],
    }),
    version = PROTOBUF_PYTHON_VERSION,
    deps = [
        ":plugin_py_pb2",
        ":well_known_proto_py_pb2",
        "//:python_srcs",
    ],
)

py_wheel(
    name = "test_wheel",
    testonly = True,
    abi = "none",
    distribution = "protobuftests",
    extra_distinfo_files = {
        "//:LICENSE": "LICENSE",
    },
    platform = "any",
    python_tag = "py3",
    strip_path_prefixes = [
        "python/",
        "src/",
    ],
    target_compatible_with = select({
        "@system_python//:none": ["@platforms//:incompatible"],
        "//conditions:default": [],
    }),
    version = PROTOBUF_PYTHON_VERSION,
    deps = [
        "//:python_common_test_protos",
        "//:python_specific_test_protos",
        "//:python_test_srcs",
        "//src/google/protobuf:testdata",
        "@com_google_absl_py//absl/testing:parameterized",
    ],
)

py_dist(
    name = "dist",
    binary_wheel = ":binary_wheel",
    # Limited API: these wheels will satisfy any Python version >= the
    # given version.
    #
    # Technically the limited API doesn't have the functions we need until
    # 3.10, but on Linux we can get away with using 3.1+ (see ../python_api.h for
    # details).
    # LINT.IfChange(limited_api_wheels)
    limited_api_platforms = {
        "//build_defs:win32": "310",
        "//build_defs:win64": "310",
        "//build_defs:linux-x86_64": "310",
        "//build_defs:linux-aarch_64": "310",
        "//build_defs:linux-s390_64": "310",
        "//build_defs:osx-universal2": "310",
    },
    # LINT.ThenChange(:python_tag)
    pure_python_wheel = ":pure_python_wheel",
    tags = ["manual"],
)

# py_proto_library() is private rule, only intended for internal use by upb.
# Hopefully py_proto_library() will eventually be available in rules_proto or
# another upstream package.
bzl_library(
    name = "py_proto_library_bzl",
    srcs = ["py_proto_library.bzl"],
    deps = [
        "@rules_python//python:py_info_bzl",
    ],
)
