1"""Macro to support py_extension """
2
3load("@bazel_skylib//lib:selects.bzl", "selects")
4
5def py_extension(name, srcs, copts, deps = [], **kwargs):
6    """Creates a C++ library to extend python
7
8    Args:
9      name: Name of the target
10      srcs: List of source files to create the target
11      copts: List of C++ compile options to use
12      deps: Libraries that the target depends on
13    """
14
15    native.cc_binary(
16        name = name + "_binary",
17        srcs = srcs,
18        copts = copts + ["-fvisibility=hidden"],
19        linkopts = selects.with_or({
20            (
21                "//python/dist:osx_x86_64",
22                "//python/dist:osx_aarch64",
23            ): ["-undefined", "dynamic_lookup"],
24            "//python/dist:windows_x86_32": ["-static-libgcc"],
25            "//conditions:default": [],
26        }),
27        linkshared = True,
28        linkstatic = True,
29        deps = deps + select({
30            "//python:limited_api_3.7": ["@python-3.7.0//:python_headers"],
31            "//python:full_api_3.7_win32": ["@nuget_python_i686_3.7.0//:python_full_api"],
32            "//python:full_api_3.7_win64": ["@nuget_python_x86-64_3.7.0//:python_full_api"],
33            "//python:full_api_3.8_win32": ["@nuget_python_i686_3.8.0//:python_full_api"],
34            "//python:full_api_3.8_win64": ["@nuget_python_x86-64_3.8.0//:python_full_api"],
35            "//python:full_api_3.9_win32": ["@nuget_python_i686_3.9.0//:python_full_api"],
36            "//python:full_api_3.9_win64": ["@nuget_python_x86-64_3.9.0//:python_full_api"],
37            "//python:limited_api_3.10_win32": ["@nuget_python_i686_3.10.0//:python_limited_api"],
38            "//python:limited_api_3.10_win64": ["@nuget_python_x86-64_3.10.0//:python_limited_api"],
39            "//conditions:default": ["@system_python//:python_headers"],
40        }),
41        **kwargs
42    )
43
44    EXT_SUFFIX = ".abi3.so"
45    output_file = "google/_upb/" + name + EXT_SUFFIX
46
47    native.genrule(
48        name = "copy" + name,
49        srcs = [":" + name + "_binary"],
50        outs = [output_file],
51        cmd = "cp $< $@",
52        visibility = ["//python:__subpackages__"],
53    )
54
55    native.py_library(
56        name = name,
57        data = [output_file],
58        imports = ["."],
59        visibility = ["//python:__subpackages__"],
60    )
61