Skip to content

Commit 8a09ab3

Browse files
committed
Add libdir to library search path
1 parent b5ed3e4 commit 8a09ab3

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

python/private/pypi/attrs.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
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+
),
1824
"download_only": attr.bool(
1925
doc = """
2026
Whether to use "pip download" instead of "pip wheel". Disables building wheels from source, but allows use of

python/private/pypi/whl_library.bzl

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

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

148165
return args
149166

0 commit comments

Comments
 (0)