Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add libdir to library search path #2476

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions python/private/pypi/attrs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
"common attributes for whl_library and pip_repository"

ATTRS = {
"add_libdir_to_library_search_path": attr.bool(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: this is enabled for both, WORKSPACE and bzlmod: https://rules-python--2476.org.readthedocs.build/en/2476/api/rules_python/python/extensions/pip.html#pip.parse.add_libdir_to_library_search_path

However, bzlmod will not forward this value to underlying whl_library instances, so extension.bzl needs to be updated. I think we should only forward this value to whl_libraries that are following the legacy pip way of downloading deps or downloading an sdist.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how to do this. Can you give me a pointer in the right direction, please?

default = False,
doc = """
If true, add the lib dir of the bundled interpreter to the library search path via LDFLAGS.
""",
),
"download_only": attr.bool(
doc = """
Whether to use "pip download" instead of "pip wheel". Disables building wheels from source, but allows use of
Expand Down
25 changes: 21 additions & 4 deletions python/private/pypi/whl_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,28 @@ def _parse_optional_attrs(rctx, args, extra_pip_args = None):
if rctx.attr.enable_implicit_namespace_pkgs:
args.append("--enable_implicit_namespace_pkgs")

env = {}
if rctx.attr.environment != None:
args += [
"--environment",
json.encode(struct(arg = rctx.attr.environment)),
]
for key, value in rctx.attr.environment.items():
env[key] = value

# This is super hacky, but working out something nice is tricky.
# This is in particular needed for psycopg2 which attempts to link libpython.a,
# in order to point the linker at the correct python intepreter.
if rctx.attr.add_libdir_to_library_search_path:
if "LDFLAGS" in env:
fail("Can't set both environment LDFLAGS and add_libdir_to_library_search_path")
command = [pypi_repo_utils.resolve_python_interpreter(rctx), "-c", "import sys ; sys.stdout.write('{}/lib'.format(sys.exec_prefix))"]
result = rctx.execute(command)
if result.return_code != 0:
fail("Failed to get LDFLAGS path: command: {}, exit code: {}, stdout: {}, stderr: {}".format(command, result.return_code, result.stdout, result.stderr))
libdir = result.stdout
env["LDFLAGS"] = "-L{}".format(libdir)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we instead extend the LDFLAGS instead of replacing the value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 151 we've checked to make sure that env["LDFLAGS"] hasn't been set. We know we're not replacing anything at this point


args += [
"--environment",
json.encode(struct(arg = env)),
]

return args

Expand Down