Skip to content
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
16 changes: 13 additions & 3 deletions uv/private/pep517_whl/build_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,22 @@
# Get a path to the outdir which will be valid after we cd
outdir = path.abspath(opts.outdir)

# Resolve CC/CXX to absolute paths so they remain valid after cwd changes
# into the extracted source tree (Bazel sets these as exec-root-relative).
for _cc_var in ("CC", "CXX"):
# Resolve compiler variables to absolute paths so they remain valid after cwd
# changes into the extracted source tree (Bazel sets these as
# exec-root-relative).
for _cc_var in ("CC", "CXX", "SYSROOT"):
if _cc_var in os.environ and not path.isabs(os.environ[_cc_var]):
os.environ[_cc_var] = path.abspath(os.environ[_cc_var])

if "SYSROOT" in os.environ:
_sysroot = os.environ["SYSROOT"]
_sysroot_cflag = f'--sysroot="{_sysroot}"'
if "CFLAGS" in os.environ:
_cflags = os.environ["CFLAGS"]
os.environ["CFLAGS"] = f"{_cflags} {_sysroot_cflag}"
else:
os.environ["CFLAGS"] = _sysroot_cflag

# Preserve PATH so native sdist builds can find compilers (clang, gcc).
build_env = dict(os.environ)
build_env.update({
Expand Down
25 changes: 24 additions & 1 deletion uv/private/pep517_whl/rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ build backend the sdist declares in its `[build-system]` table.
"""

load("@bazel_tools//tools/cpp:toolchain_utils.bzl", find_cc_toolchain = "find_cpp_toolchain")
load("@rules_cc//cc:action_names.bzl", "C_COMPILE_ACTION_NAME")
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
load("//py/private/toolchain:types.bzl", "NATIVE_BUILD_TOOLCHAIN", "PY_TOOLCHAIN")

CC_TOOLCHAIN = "@bazel_tools//tools/cpp:toolchain_type"
Expand Down Expand Up @@ -70,9 +72,30 @@ def _pep517_native_whl(ctx):
# rather than falling back to whatever is on the system PATH.
cc_toolchain = find_cc_toolchain(ctx, mandatory = False)
if cc_toolchain:
env["CC"] = cc_toolchain.compiler_executable
feature_configuration = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
)
c_compiler_path = cc_common.get_tool_for_action(
feature_configuration = feature_configuration,
action_name = C_COMPILE_ACTION_NAME,
)

# Note that these paths are relative to the bazel exec root,
# and they need to be absolutized if they're to be invoked from any
# other working directory. (e.g. in build_helper.py for sdist builds.)
env["CC"] = c_compiler_path
extra_inputs.append(cc_toolchain.all_files)

# We can extract the relative path to the @llvm-provided sysroot from
# the list of include directories.
# It still needs to be absolutized, so we can't add it directly to
# CFLAGS here.
for c in cc_toolchain.built_in_include_directories:
if c.endswith("sysroot"):
env["SYSROOT"] = c
break

ctx.actions.run(
mnemonic = "PySdistNativeBuild",
progress_message = "Native source compiling {} to a whl".format(archive.basename),
Expand Down
Loading