Skip to content

Commit e7d2f09

Browse files
shs96caignas
andauthored
fix: Add libdir to library search path (#2476)
We discovered when dealing with libraries such as `psycopg2` that the wheel would attempt to link against `libpython.a`. This fix points the linker at the correct python version being used. --------- Co-authored-by: Ignas Anikevicius <[email protected]>
1 parent b499560 commit e7d2f09

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ Unreleased changes template.
7777
* {obj}`//python/bin:python`: convenience target for directly running an
7878
interpreter. {obj}`--//python/bin:python_src` can be used to specify a
7979
binary whose interpreter to use.
80+
* (pypi) An extra argument to add the interpreter lib dir to `LDFLAGS` when
81+
building wheels from `sdist`.
8082

8183
{#v0-0-0-removed}
8284
### Removed

python/private/pypi/attrs.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
"common attributes for whl_library and pip_repository"
1616

1717
ATTRS = {
18+
"add_libdir_to_library_search_path": attr.bool(
19+
default = False,
20+
doc = """
21+
If true, add the lib dir of the bundled interpreter to the library search path via `LDFLAGS`.
22+
23+
:::{versionadded} VERSION_NEXT_FEATURE
24+
:::
25+
""",
26+
),
1827
"download_only": attr.bool(
1928
doc = """
2029
Whether to use "pip download" instead of "pip wheel". Disables building wheels from source, but allows use of

python/private/pypi/extension.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ def _create_whl_repos(
203203
)
204204
maybe_args = dict(
205205
# The following values are safe to omit if they have false like values
206+
add_libdir_to_library_search_path = pip_attr.add_libdir_to_library_search_path,
206207
annotation = whl_modifications.get(whl_name),
207208
download_only = pip_attr.download_only,
208209
enable_implicit_namespace_pkgs = pip_attr.enable_implicit_namespace_pkgs,

python/private/pypi/whl_library.bzl

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,28 @@ def _parse_optional_attrs(rctx, args, extra_pip_args = None):
140140
if rctx.attr.enable_implicit_namespace_pkgs:
141141
args.append("--enable_implicit_namespace_pkgs")
142142

143+
env = {}
143144
if rctx.attr.environment != None:
144-
args += [
145-
"--environment",
146-
json.encode(struct(arg = rctx.attr.environment)),
147-
]
145+
for key, value in rctx.attr.environment.items():
146+
env[key] = value
147+
148+
# This is super hacky, but working out something nice is tricky.
149+
# This is in particular needed for psycopg2 which attempts to link libpython.a,
150+
# in order to point the linker at the correct python intepreter.
151+
if rctx.attr.add_libdir_to_library_search_path:
152+
if "LDFLAGS" in env:
153+
fail("Can't set both environment LDFLAGS and add_libdir_to_library_search_path")
154+
command = [pypi_repo_utils.resolve_python_interpreter(rctx), "-c", "import sys ; sys.stdout.write('{}/lib'.format(sys.exec_prefix))"]
155+
result = rctx.execute(command)
156+
if result.return_code != 0:
157+
fail("Failed to get LDFLAGS path: command: {}, exit code: {}, stdout: {}, stderr: {}".format(command, result.return_code, result.stdout, result.stderr))
158+
libdir = result.stdout
159+
env["LDFLAGS"] = "-L{}".format(libdir)
160+
161+
args += [
162+
"--environment",
163+
json.encode(struct(arg = env)),
164+
]
148165

149166
return args
150167

tests/pypi/extension/extension_tests.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def _parse(
7777
hub_name,
7878
python_version,
7979
_evaluate_markers_srcs = [],
80+
add_libdir_to_library_search_path = False,
8081
auth_patterns = {},
8182
download_only = False,
8283
enable_implicit_namespace_pkgs = False,
@@ -105,6 +106,7 @@ def _parse(
105106
return struct(
106107
_evaluate_markers_srcs = _evaluate_markers_srcs,
107108
auth_patterns = auth_patterns,
109+
add_libdir_to_library_search_path = add_libdir_to_library_search_path,
108110
download_only = download_only,
109111
enable_implicit_namespace_pkgs = enable_implicit_namespace_pkgs,
110112
environment = environment,

0 commit comments

Comments
 (0)