# Copyright (c) 2009-2021, 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//rules:common_settings.bzl", "bool_flag", "string_flag")
load("//python:build_targets.bzl", "build_targets")
load("//python:py_extension.bzl", "py_extension")
load("//upb/bazel:copts.bzl", "UPB_DEFAULT_COPTS", "UPB_DEFAULT_FEATURES")

build_targets(name = "python")

licenses(["notice"])

package(
    default_applicable_licenses = ["//:license"],
    default_visibility = ["//python/dist:__pkg__"],
)

LIMITED_API_FLAG_SELECT = {
    ":limited_api_3.10": ["-DPy_LIMITED_API=0x030a0000"],
    "//conditions:default": [],
}

bool_flag(
    name = "limited_api",
    build_setting_default = True,
)

string_flag(
    name = "python_version",
    build_setting_default = "system",
    values = [
        "system",
        "39",
        "310",
        "311",
    ],
)

config_setting(
    name = "limited_api_3.10",
    flag_values = {
        ":limited_api": "True",
        ":python_version": "310",
    },
)

config_setting(
    name = "limited_api_3.10_win32",
    constraint_values = [
        "@platforms//cpu:x86_32",
        "@platforms//os:windows",
    ],
    flag_values = {
        ":limited_api": "True",
        ":python_version": "310",
    },
)

config_setting(
    name = "limited_api_3.10_win64",
    constraint_values = [
        "@platforms//cpu:x86_64",
        "@platforms//os:windows",
    ],
    flag_values = {
        ":limited_api": "True",
        ":python_version": "310",
    },
)

_message_target_compatible_with = {
    "@system_python//:none": ["@platforms//:incompatible"],
    "@system_python//:unsupported": ["@platforms//:incompatible"],
    "//conditions:default": [],
}

filegroup(
    name = "message_srcs",
    srcs = [
        "buffer_convert.c",
        "buffer_convert.h",
        "convert.c",
        "convert.h",
        "descriptor.c",
        "descriptor.h",
        "descriptor_containers.c",
        "descriptor_containers.h",
        "descriptor_pool.c",
        "descriptor_pool.h",
        "extension_dict.c",
        "extension_dict.h",
        "map.c",
        "map.h",
        "message.c",
        "message.h",
        "protobuf.c",
        "protobuf.h",
        "python_api.h",
        "repeated.c",
        "repeated.h",
        "unknown_fields.c",
        "unknown_fields.h",
    ],
)

py_extension(
    name = "_message",
    srcs = [":message_srcs"],
    copts = UPB_DEFAULT_COPTS + select(LIMITED_API_FLAG_SELECT),
    features = UPB_DEFAULT_FEATURES,
    target_compatible_with = select(_message_target_compatible_with),
    deps = [
        ":breaking_changes",
        "//src/google/protobuf:descriptor_upb_minitable_proto",
        "//src/google/protobuf:descriptor_upb_reflection_proto",
        "//third_party/utf8_range",
        "//upb/base",
        "//upb/hash",
        "//upb/mem",
        "//upb/message",
        "//upb/message:compare",
        "//upb/message:copy",
        "//upb/message:message_unknowns",
        "//upb/mini_table",
        "//upb/port",
        "//upb/reflection",
        "//upb/text",
        "//upb/util:def_to_proto",
        "//upb/util:required_fields",
        "//upb/wire",
        "//upb/wire:encode_extension",
        "//upb/wire:eps_copy_input_stream",
        "//upb/wire:reader",
        "@abseil-cpp//absl/base:core_headers",
    ],
)
