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