blob: 9a2828b950d307261cfdf15833407a4f4c106283 [file] [log] [blame]
Adam Cozzette501ecec2023-09-26 14:36:20 -07001"""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({
Adam Cozzette8f831e92023-09-28 16:10:31 -070030 "//python:limited_api_3.8": ["@python-3.8.0//:python_headers"],
Adam Cozzette501ecec2023-09-26 14:36:20 -070031 "//python:full_api_3.8_win32": ["@nuget_python_i686_3.8.0//:python_full_api"],
32 "//python:full_api_3.8_win64": ["@nuget_python_x86-64_3.8.0//:python_full_api"],
33 "//python:full_api_3.9_win32": ["@nuget_python_i686_3.9.0//:python_full_api"],
34 "//python:full_api_3.9_win64": ["@nuget_python_x86-64_3.9.0//:python_full_api"],
35 "//python:limited_api_3.10_win32": ["@nuget_python_i686_3.10.0//:python_limited_api"],
36 "//python:limited_api_3.10_win64": ["@nuget_python_x86-64_3.10.0//:python_limited_api"],
37 "//conditions:default": ["@system_python//:python_headers"],
38 }),
39 **kwargs
40 )
41
42 EXT_SUFFIX = ".abi3.so"
43 output_file = "google/_upb/" + name + EXT_SUFFIX
44
45 native.genrule(
46 name = "copy" + name,
47 srcs = [":" + name + "_binary"],
48 outs = [output_file],
49 cmd = "cp $< $@",
50 visibility = ["//python:__subpackages__"],
51 )
52
53 native.py_library(
54 name = name,
55 data = [output_file],
56 imports = ["."],
Mike Kruskalc23d5332023-11-10 15:18:00 -080057 visibility = [
58 "//python:__subpackages__",
59 "//conformance:__pkg__",
60 ],
Adam Cozzette501ecec2023-09-26 14:36:20 -070061 )