Integrating STLink into Bazel

129 views
Skip to first unread message

Uma Patil

unread,
Sep 8, 2022, 3:48:04 PM9/8/22
to bazel-discuss
Hi,

I'm facing issues with integrating STLink CLI into Bazel. I tried by creating a rule to run shell script. But getting error as Unable to open file when I run.

Here is the code.
Build File of application:
sh_binary(
  name = "run",
  srcs = ["//tools/run:st-link-flash"],
  visibility = ["//visibility:public"],
)

Build file of Tools:
stlink_binary1(
    name = "st-link-flash",
    srcs = ":hex_file",
    flash_offset = "0x8000000",
    transport = "SWD",
    device_sn = select({
        "stm32f407IG": "066DFF505252836687125942",
    }),
    visibility = ["//visibility:public"],
)

def _stlink_flash_impl(ctx):
   
    script = ctx.actions.declare_file("%s.sh" % ctx.label.name)
    input = ctx.file.srcs

    script_content="ST-LINK_CLI.exe -c "+"SN="+ctx.attr.device_sn+ " " +ctx.attr.transport+ " -P " + input.path+" "+ctx.attr.flash_offset
    ctx.actions.write(script, script_content, is_executable=True)
    runfiles = ctx.runfiles(files = [ctx.file.srcs])
    return [DefaultInfo(executable= script, runfiles = runfiles)]

stlink_binary1 = rule(
    implementation = _stlink_flash_impl,
    attrs = {
        "device_sn": attr.string(
            doc = "Device serial number",
            mandatory = False,
        ),
        "transport": attr.string(
            doc = "Debugger transport interface",
            default = "SWD",
            mandatory = False,
        ),
        "srcs": attr.label(
            doc = "Binary image to flash",
            mandatory = True,
            allow_single_file = True,
        ),
        "flash_offset": attr.string(
            doc = "The starting point of the flash memory",
            mandatory = False,
            default = "0x8000000",
        ),
        # "deps" : attr.label_list(mandatory=False, providers=["files"]),
    },
    executable = True,
)

Command :
bazel run //examples/application:run --platforms=//examples/microcontroller:stm32f407 --subcommands --verbose_failures

Error:

ST-LINK SN: 066DFF505252836687125942
ST-LINK Firmware version: V2J37M26
Connected via SWD.
SWD Frequency = 4000K.
Target voltage = 2.9 V
Connection mode: Normal
Reset mode: Software reset
Device ID: 0x413
Device flash Size: 1024 Kbytes
Device family: STM32F405xx/F407xx/F415xx/F417xx

Loading file...
Unable to open file!

Problem: If I use absolute path instead of input.path it works. But I don't want to hard code the path.
Also I need to detect if there are multiple boards connected and route the execution depending on platform accordingly. 
Example: If 2 boards are connected(stm32f407 and stm32401) if I have built the executable for stm32f407 it has to be directed automatically. Any way to do this? 

Any help would be much appreciated. 

Thank You

David Turner

unread,
Sep 9, 2022, 5:51:55 AM9/9/22
to Uma Patil, bazel-discuss
Hello,

Not a Bazel expert here, but have you tried using input.short_path instead of input.path?. See https://stackoverflow.com/questions/47856239/runfiles-location-substitution


--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/147997c7-b936-43ec-b189-6789e5a9035an%40googlegroups.com.

Uma Patil

unread,
Sep 9, 2022, 11:38:05 AM9/9/22
to bazel-discuss
Thank you for the response:). I did try file.short_path and it throws same error. 

Austin Schuh

unread,
Sep 9, 2022, 5:17:09 PM9/9/22
to Uma Patil, bazel-discuss
There's always the trick of having a wrapper shell script for your
tool convert paths to absolute. That way bazel stays in the relative
path space.

For selecting from multiple builds, the canonical solution would be to
do a configuration transition and build both versions. And then have
your script detect which one and use the corresponding version.

Austin
> To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/436f0166-0262-43db-93f2-0784731befadn%40googlegroups.com.

Nathaniel Brough

unread,
Sep 9, 2022, 5:45:05 PM9/9/22
to bazel-discuss
Hey not sure if this helps, but I wrote some rules to integrate with openocd (which should be compatible with your stlink as I often develop with stm32s). If you ctrl-f search for "openocd" in the readme here https://github.com/bazelembedded/bazel-embedded there are some instructions on how to use it. You are of course welcome to adapt the openocd code to work with the stlink binaries if you need too.

Hope that helps :)

Nat

Reply all
Reply to author
Forward
0 new messages