diff --git a/python/poetry_deps.bzl b/python/poetry_deps.bzl index daf8689..cee9046 100644 --- a/python/poetry_deps.bzl +++ b/python/poetry_deps.bzl @@ -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. @@ -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]) @@ -126,4 +152,5 @@ package = rule( "@bazel_tools//tools/python:toolchain_type", "@bazel_tools//tools/cpp:toolchain_type", ], + fragments = ["cpp"], ) diff --git a/python/poetry_deps.py b/python/poetry_deps.py index 0761449..4d869b0 100644 --- a/python/poetry_deps.py +++ b/python/poetry_deps.py @@ -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) @@ -115,31 +119,41 @@ 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) diff --git a/python/repositories.bzl b/python/repositories.bzl index 285ec32..06d6444 100644 --- a/python/repositories.bzl +++ b/python/repositories.bzl @@ -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"], ), ] diff --git a/python/tests/poetry_deps_test.py b/python/tests/poetry_deps_test.py index a402a3e..c618d67 100644 --- a/python/tests/poetry_deps_test.py +++ b/python/tests/poetry_deps_test.py @@ -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", @@ -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 "Darwin" in os.environ["CMAKE_ARGS"] return 42