Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 1 | """Macro to support py_extension """ |
| 2 | |
| 3 | load("@bazel_skylib//lib:selects.bzl", "selects") |
| 4 | |
| 5 | def 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 Cozzette | 8f831e9 | 2023-09-28 16:10:31 -0700 | [diff] [blame] | 30 | "//python:limited_api_3.8": ["@python-3.8.0//:python_headers"], |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 31 | "//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 Kruskal | c23d533 | 2023-11-10 15:18:00 -0800 | [diff] [blame] | 57 | visibility = [ |
| 58 | "//python:__subpackages__", |
| 59 | "//conformance:__pkg__", |
| 60 | ], |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 61 | ) |