Skip to content

Commit

Permalink
feat: use cpp fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
oxidase committed Jul 2, 2024
1 parent 9ef0f12 commit 5bd8f47
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 21 deletions.
29 changes: 28 additions & 1 deletion python/poetry_deps.bzl
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
load("@rules_python//python:defs.bzl", StarPyInfo = "PyInfo")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//lib:versions.bzl", "versions")
load("@rules_python//python:defs.bzl", StarPyInfo = "PyInfo")
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load("//python/private:poetry_deps.bzl", _derive_environment_markers = "derive_environment_markers", _include_dep = "include_dep")
load("//python/private:poetry_deps.bzl", _get_imports = "get_imports", _get_transitive_sources = "get_transitive_sources")
load("//python/private:poetry_deps.bzl", _DEFAULT_PLATFORMS = "DEFAULT_PLATFORMS")

PYTHON_BINARY = ["bin/python3", "python/py3wrapper.sh"]

def get_tool(ctx, cc_toolchain, feature_configuration, action_name):
binary = cc_common.get_tool_for_action(feature_configuration = feature_configuration, action_name = action_name)
flags = cc_common.get_memory_inefficient_command_line(
feature_configuration = feature_configuration,
action_name = action_name,
variables = cc_common.create_compile_variables(
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
include_directories = depset(direct = cc_toolchain.built_in_include_directories),
user_compile_flags = ctx.attr.copts if hasattr(ctx.attr, "copts") else [],
use_pic = True,
),
)
return binary, flags

def _package_impl(ctx):
"""
Rule to install a Python package.
Expand Down Expand Up @@ -69,7 +85,17 @@ def _package_impl(ctx):
cc_toolchain = ctx.toolchains["@bazel_tools//tools/cpp:toolchain_type"]
if cc_toolchain and hasattr(cc_toolchain, "cc") and type(cc_toolchain.cc) == "CcToolchainInfo":
cc = cc_toolchain.cc
feature_configuration = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc,
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
cc_attr = {k: getattr(cc, k) for k in dir(cc) if type(getattr(cc, k)) != "depset" and type(getattr(cc, k)) != "builtin_function_or_method"}
cc_attr["AS"], cc_attr["ASFLAGS"] = get_tool(ctx, cc, feature_configuration, ACTION_NAMES.assemble)
cc_attr["CC"], cc_attr["CFLAGS"] = get_tool(ctx, cc, feature_configuration, ACTION_NAMES.c_compile)
cc_attr["CXX"], cc_attr["CXXFLAGS"] = get_tool(ctx, cc, feature_configuration, ACTION_NAMES.cpp_compile)
cc_attr["LD"], cc_attr["LDFLAGS"] = get_tool(ctx, cc, feature_configuration, ACTION_NAMES.cpp_link_dynamic_library)
arguments.append("--cc_toolchain=" + json.encode(cc_attr))

install_inputs = depset(transitive = [install_inputs, cc.all_files])
Expand Down Expand Up @@ -126,4 +152,5 @@ package = rule(
"@bazel_tools//tools/python:toolchain_type",
"@bazel_tools//tools/cpp:toolchain_type",
],
fragments = ["cpp"],
)
46 changes: 30 additions & 16 deletions python/poetry_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,23 @@ def get_platform_args(args):
# https://clang.llvm.org/docs/CrossCompilation.html#target-triple
cc_compiler_cpu_to_cflags = {
"clang": {
"darwin_arm64": "-arch arm64",
"darwin_x86_64": "-arch x86_64",
"darwin_arm64e": "-arch arm64e",
"darwin_arm64": ["-arch arm64"],
"darwin_x86_64": ["-arch x86_64"],
"darwin_arm64e": ["-arch arm64e"],
}
}

cc_cpu_to_cmake_args = {
"darwin_arm64": "-DCMAKE_SYSTEM_NAME=Darwin -DCMAKE_SYSTEM_PROCESSOR=arm64",
"darwin_x86_64": "-DCMAKE_SYSTEM_NAME=Darwin -DCMAKE_SYSTEM_PROCESSOR=x86_64",
"darwin_arm64e": "-DCMAKE_SYSTEM_NAME=Darwin -DCMAKE_SYSTEM_PROCESSOR=arm64e",
"darwin_arm64": ["-DCMAKE_SYSTEM_NAME=Darwin", "-DCMAKE_SYSTEM_PROCESSOR=arm64"],
"darwin_x86_64": ["-DCMAKE_SYSTEM_NAME=Darwin", "-DCMAKE_SYSTEM_PROCESSOR=x86_64"],
"darwin_arm64e": ["-DCMAKE_SYSTEM_NAME=Darwin", "-DCMAKE_SYSTEM_PROCESSOR=arm64e"],
}


def join(flags):
return " ".join([flag for flag in flags if flag])


def install(args):
output_path = Path(args.output)

Expand Down Expand Up @@ -115,35 +119,45 @@ def install(args):
"--no-dependencies",
"--disable-pip-version-check",
"--use-pep517",
"--quiet",
# "--quiet",
"--verbose",
]

if args.cc_toolchain is not None:
cc = json.loads(args.cc_toolchain)
compiler = cc.get("compiler")
cpu = cc.get("cpu")

paths = dict(
AS="AS",
CC="CC",
CXX="CXX",
LD="LD",
AR="ar_executable",
CC="compiler_executable",
CPP="preprocessor_executable",
GCOV="gcov_executable",
LD="ld_executable",
NM="nm_executable",
OBJCOPY="objcopy_executable",
OBJDUMP="objdump_executable",
STRIP="strip_executable",
)

cflags = [cc_compiler_cpu_to_cflags.get(cc.get("compiler"), {}).get(cc.get("cpu"), "")]
cxxflags = cflags + ["-I{}".format(dir) for dir in cc.get("built_in_include_directories", [])]
cpu_flags = cc_compiler_cpu_to_cflags.get(compiler, {}).get(cpu, [])
asflags = cpu_flags + cc.get("ASFLAGS", [])
cflags = cpu_flags + cc.get("CFLAGS", [])
cxxflags = cpu_flags + cc.get("CXXFLAGS", [])
ldflags = ["-Wl,-rpath,{}".format(cc.get("dynamic_runtime_solib_dir", ""))] + cc.get("LDFLAGS", [])
flags = dict(
CFLAGS=" ".join([flag for flag in cflags if flag]),
CXXFLAGS=" ".join([flag for flag in cxxflags if flag]),
LDFLAGS="-Wl,-rpath,{}".format(cc.get("dynamic_runtime_solib_dir", "")),
CMAKE_ARGS=cc_cpu_to_cmake_args.get(cc.get("cpu"), ""),
ASMFLAGS=join(asflags),
ASFLAGS=join(asflags),
CFLAGS=join(cflags),
CXXFLAGS=join(cxxflags),
LDFLAGS=join(ldflags),
CMAKE_ARGS=join(cc_cpu_to_cmake_args.get(cpu, []) + ["-DCMAKE_VERBOSE_MAKEFILE=ON"]),
)

os.environ.update(flags)
os.environ.update({k: os.fspath(Path(cc[v]).resolve()) for k, v in paths.items() if v in cc})
os.environ.update({k: os.fspath(Path(cc[v]).resolve()) for k, v in paths.items() if k in cc or v in cc})

if retcode := install_command.main(install_args + get_platform_args(args)):
logging.error(f"pip install returned {retcode}")
Expand Down
4 changes: 2 additions & 2 deletions python/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ _PACKAGES_ENDPOINT = "https://files.pythonhosted.org/packages/"
_INTERNAL_DEPS = [
(
"pip",
_PACKAGES_ENDPOINT + "8a/6a/19e9fe04fca059ccf770861c7d5721ab4c2aebc539889e97c7977528a53b/pip-24.0-py3-none-any.whl",
"ba0d021a166865d2265246961bec0152ff124de910c5cc39f1156ce3fa7c69dc",
_PACKAGES_ENDPOINT + "f4/ab/e3c039b5ddba9335bd8f82d599eb310de1d2a2db0411b8d804d507405c74/pip-24.1.1-py3-none-any.whl",
"efca15145a95e95c00608afeab66311d40bfb73bb2266a855befd705e6bb15a0",
["@rules_ophiuchus//python:patches/scripts_executable.patch"],
),
]
Expand Down
11 changes: 9 additions & 2 deletions python/tests/poetry_deps_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,11 @@ def test_get_data(self):
args = InstallArgs()
args.cc_toolchain = json.dumps(
dict(
compiler_executable=".",
CC="CC",
CXX="CXX",
LD="LD",
CXXFLAGS=["-I.", "-Ix", "-I/x"],
compiler_executable="compiler_executable",
dynamic_runtime_solib_dir=".",
built_in_include_directories=[".", "x", "/x"],
compiler="clang",
Expand All @@ -170,9 +174,12 @@ def main(self, args):
assert "AR" not in os.environ

assert Path(os.environ["CC"]).is_absolute()
assert os.environ["ASMFLAGS"] == os.environ["ASFLAGS"]
assert os.environ["ASMFLAGS"] == "-arch arm64"
assert os.environ["CFLAGS"] == "-arch arm64"
assert os.environ["CXXFLAGS"] == "-arch arm64 -I. -Ix -I/x"
assert os.environ["LDFLAGS"] == "-Wl,-rpath,."
# assert os.environ["LDFLAGS"] == "-Wl,-rpath,."
assert "Darwin" in os.environ["CMAKE_ARGS"]

return 42

Expand Down

0 comments on commit 5bd8f47

Please sign in to comment.