From 0c7a9be3594deca3d7bfc3e99c0b5e5f5b15d598 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Mon, 20 Dec 2021 18:59:55 +1000 Subject: [PATCH] fix Windows build with latest rules_rust the extra .lib file that is being output for .dll files was tripping up the copy; we need to select only the .dll file --- pylib/anki/_backend/BUILD.bazel | 22 +++-------------- pylib/anki/_backend/python_lib.bzl | 38 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 19 deletions(-) create mode 100644 pylib/anki/_backend/python_lib.bzl diff --git a/pylib/anki/_backend/BUILD.bazel b/pylib/anki/_backend/BUILD.bazel index a4f54cddc..edaf407c7 100644 --- a/pylib/anki/_backend/BUILD.bazel +++ b/pylib/anki/_backend/BUILD.bazel @@ -2,6 +2,7 @@ load("@rules_python//python:defs.bzl", "py_binary") load("@py_deps//:requirements.bzl", "requirement") load("@bazel_skylib//rules:copy_file.bzl", "copy_file") load("@bazel_skylib//lib:selects.bzl", "selects") +load(":python_lib.bzl", "copy_python_lib") py_binary( name = "genbackend", @@ -44,26 +45,9 @@ genrule( ], ) -copy_file( - name = "rsbridge_unix", - src = "//pylib/rsbridge", - out = "rsbridge.so", -) - -copy_file( - name = "rsbridge_win", - src = "//pylib/rsbridge", - out = "rsbridge.pyd", -) - -alias( +copy_python_lib( name = "rsbridge", - actual = selects.with_or({ - ( - "@rules_rust//rust/platform:x86_64-pc-windows-msvc", - ): ":rsbridge_win", - "//conditions:default": ":rsbridge_unix", - }), + src = "//pylib/rsbridge", ) _py_srcs = [ diff --git a/pylib/anki/_backend/python_lib.bzl b/pylib/anki/_backend/python_lib.bzl new file mode 100644 index 000000000..34a9a55ba --- /dev/null +++ b/pylib/anki/_backend/python_lib.bzl @@ -0,0 +1,38 @@ +load("@bazel_skylib//lib:paths.bzl", "paths") + +def _copy_python_lib_impl(ctx): + desired_file = None + target_name = ctx.label.name + if ctx.attr.is_windows: + target_name += ".pyd" + for file in ctx.files.src: + if file.extension == "dll": + desired_file = file + break + else: + target_name += ".so" + desired_file = ctx.files.src[0] + + file = ctx.actions.declare_file(target_name) + ctx.actions.symlink(output = file, target_file = desired_file) + + return [DefaultInfo(files = depset([file]))] + +_copy_python_lib = rule( + implementation = _copy_python_lib_impl, + attrs = { + "src": attr.label(), + "is_windows": attr.bool(mandatory = True), + }, +) + +def copy_python_lib(name, src): + "Copy a dynamic library, renaming it to the extension Python expects." + _copy_python_lib( + name = name, + src = src, + is_windows = select({ + "@bazel_tools//src/conditions:host_windows": True, + "//conditions:default": False, + }), + )