Mark
Mark

Reputation: 5499

Executing a native_binary inside a bazel rule

@bazel_skylib//rules:native_binary.bzl defines the native_binary rule which can be used to wrap native executables inside a bazel target. I used it to wrap a packaging tool called packfolder.exe from the Sciter SDK.

I placed the binary into my source tree at third_party/sciter/packfolder.exe and wrote this BUILD file.

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

bazel run third_party/sciter:packfolder runs with no issues. Now I want to use this target inside my custom cc_sciter_resource rule.

# 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"
        ),        
    }
)

The trouble is, when I try to build a target that uses this rule, say

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

I get the following error.

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

The directory C:\users\marki\_bazel_marki\kiodv2fz\execroot\sciter_bazel\bazel-out\x64_windows-opt-exec-2B5CBBC6 does not exist on my computer. So the error is accurate, but I don't know how to resolve the issue.

Upvotes: 1

Views: 2429

Answers (1)

Benjamin Peterson
Benjamin Peterson

Reputation: 20520

--- 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 is just a string, so Bazel doesn't know that the packfolder executable needs to be added as an input to the action.

Upvotes: 1

Related Questions