Skip to content

Commit

Permalink
Add extension_linker_flags to Python toolchain
Browse files Browse the repository at this point in the history
Summary:
Add a new `extension_linker_flags` to the Python toolchain, which
allows specifying linker flags that only affect native extensions (e.g.
`cxx_python_extension`).

Reviewed By: Yingquan-Y

Differential Revision: D62672175

fbshipit-source-id: 5dee8200d99f294be4b53b6e7760fb26244cc4ab
  • Loading branch information
andrewjcg authored and facebook-github-bot committed Sep 14, 2024
1 parent 81fe8aa commit db2eca3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
5 changes: 4 additions & 1 deletion prelude/python/cxx_python_extension.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ load(
)
load("@prelude//linking:types.bzl", "Linkage")
load("@prelude//os_lookup:defs.bzl", "OsLookup")
load("@prelude//python:toolchain.bzl", "PythonPlatformInfo", "get_platform_attr")
load("@prelude//python:toolchain.bzl", "PythonPlatformInfo", "PythonToolchainInfo", "get_platform_attr")
load("@prelude//unix:providers.bzl", "UnixEnv", "create_unix_env_info")
load("@prelude//utils:expect.bzl", "expect")
load("@prelude//utils:utils.bzl", "value_or")
Expand Down Expand Up @@ -110,6 +110,8 @@ def cxx_python_extension_impl(ctx: AnalysisContext) -> list[Provider]:
preprocessor_for_tests = False,
)

python_toolchain = ctx.attrs._python_toolchain[PythonToolchainInfo]

impl_params = CxxRuleConstructorParams(
build_empty_so = True,
rule_type = "cxx_python_extension",
Expand All @@ -122,6 +124,7 @@ def cxx_python_extension_impl(ctx: AnalysisContext) -> list[Provider]:
compiler_flags = ctx.attrs.compiler_flags,
lang_compiler_flags = ctx.attrs.lang_compiler_flags,
platform_compiler_flags = ctx.attrs.platform_compiler_flags,
extra_link_flags = python_toolchain.extension_linker_flags,
lang_platform_compiler_flags = ctx.attrs.lang_platform_compiler_flags,
preprocessor_flags = ctx.attrs.preprocessor_flags,
lang_preprocessor_flags = ctx.attrs.lang_preprocessor_flags,
Expand Down
8 changes: 7 additions & 1 deletion prelude/python/python_wheel.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ load(
)
load("@prelude//python:manifest.bzl", "create_manifest_for_entries")
load("@prelude//python:python.bzl", "PythonLibraryInfo")
load("@prelude//python:toolchain.bzl", "PythonToolchainInfo")
load("@prelude//utils:expect.bzl", "expect")
load(
"@prelude//utils:graph_utils.bzl",
Expand Down Expand Up @@ -112,6 +113,7 @@ def _impl(ctx: AnalysisContext) -> list[Provider]:
expect(not manifests.resources[1])
srcs.append(manifests.resources[0])
if manifests.extensions != None:
python_toolchain = ctx.attrs._python_toolchain[PythonToolchainInfo]
toolchain_info = get_cxx_toolchain_info(ctx)
items = manifests.extensions.items()
expect(len(items) == 1)
Expand Down Expand Up @@ -148,7 +150,10 @@ def _impl(ctx: AnalysisContext) -> list[Provider]:
ctx = ctx,
output = extension,
opts = link_options(
links = [LinkArgs(infos = inputs)],
links = [
LinkArgs(flags = python_toolchain.extension_linker_flags),
LinkArgs(infos = inputs),
],
category_suffix = "native_extension",
identifier = extension,
link_execution_preference = LinkExecutionPreference("any"),
Expand Down Expand Up @@ -222,5 +227,6 @@ python_wheel = rule(
prefer_stripped_objects = attrs.default_only(attrs.bool(default = False)),
_wheel = attrs.default_only(attrs.exec_dep(default = "prelude//python/tools:wheel")),
_cxx_toolchain = toolchains_common.cxx(),
_python_toolchain = toolchains_common.python(),
),
)
1 change: 1 addition & 0 deletions prelude/python/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ PythonToolchainInfo = provider(
"native_link_strategy": provider_field(typing.Any, default = None),
"linker_flags": provider_field(typing.Any, default = None),
"binary_linker_flags": provider_field(typing.Any, default = None),
"extension_linker_flags": provider_field(typing.Any, default = None),
"generate_static_extension_info": provider_field(typing.Any, default = None),
"parse_imports": provider_field(typing.Any, default = None),
"traverse_dep_manifest": provider_field(typing.Any, default = None),
Expand Down

0 comments on commit db2eca3

Please sign in to comment.