0

@bazel_skylib//rules:native_binary.bzl定义native_binary可用于将本机可执行文件包装在 bazel 目标中的规则。我用它来包装一个packfolder.exe从 Sciter SDK 调用的打包工具。

我将二进制文件放入我的源代码树中third_party/sciter/packfolder.exe并编写了这个BUILD文件。

# third_party/sciter/BUILD
native_binary(name = "packfolder",
              src = "packfolder.exe",
              out = "packfolder.exe"
)

bazel run third_party/sciter:packfolder运行没有问题。现在我想在我的自定义cc_sciter_resource规则中使用这个目标。

# third_party/sciter/sciter_rules.bzl

def _impl(ctx):
    in_files = ctx.files.srcs
    output_file = ctx.actions.declare_file(ctx.label.name)
    ctx.actions.run(
        outputs = [output_file],
        inputs = in_files,
        arguments = [],
        executable = ctx.executable.packfolder.path)
    return DefaultInfo(files = depset([output_file]))

cc_sciter_resource = rule(
    implementation = _impl,
    attrs = {
        "srcs": attr.label_list(),
        "packfolder": attr.label(
            default = Label("//third_party/sciter:packfolder"),
            executable = True,
            cfg = "exec"
        ),        
    }
)

问题是,当我尝试构建使用此规则的目标时,比如说

cc_sciter_resource(
    name = "hello_world_resource.cpp"
    srcs = [...]
)

我收到以下错误。

ERROR: C:/users/marki/sciter-bazel/examples/BUILD:12:19: Action examples/hello_world_resource.cpp failed (Exit -1): packfolder.exe failed: error executing command
  cd C:/users/marki/_bazel_marki/kiodv2fz/execroot/sciter_bazel
bazel-out/x64_windows-opt-exec-2B5CBBC6/bin/third_party/sciter/packfolder.exe
Execution platform: @local_config_platform//:host. Note: Remote connection/protocol failed with: execution failed
Action failed to execute: java.io.IOException: ERROR: src/main/native/windows/process.cc(202): CreateProcessW("C:\users\marki\_bazel_marki\kiodv2fz\execroot\sciter_bazel\bazel-out\x64_windows-opt-exec-2B5CBBC6\bin\third_party\sciter\packfolder.exe"): The system cannot find the file specified.
 (error: 2)
Target //examples:hello_world_resource.cpp failed to build

该目录C:\users\marki\_bazel_marki\kiodv2fz\execroot\sciter_bazel\bazel-out\x64_windows-opt-exec-2B5CBBC6在我的计算机上不存在。所以错误是准确的,但我不知道如何解决这个问题。

4

1 回答 1

1
--- sciter_rules.bzl
+++ sciter_rules.bzl
@@ -6,7 +6,7 @@
         outputs = [output_file],
         inputs = in_files,
         arguments = [],
-        executable = ctx.executable.packfolder.path)
+        executable = ctx.executable.packfolder)
     return DefaultInfo(files = depset([output_file]))
 
 cc_sciter_resource = rule(

ctx.executable.packfolder.path只是一个字符串,因此 Bazel 不知道packfolder需要将可执行文件添加为操作的输入。

于 2020-11-24T03:39:13.710 回答