Run Openocd with addSystemCommand

Working on getting an embedded project up and running with zig. I’m trying to add a step to my build to run openocd to flash my microcontroller. I’m getting an error when I run it.

This is what I have in my build.zig for my openocd step:

    //build the elf file
    b.installArtifact(elf);

    //openocd -f interface/stlink.cfg -f target/stm32f1x.cfg -c "program gps.elf verify reset exit"
    const openocd_flash_cmd = b.addSystemCommand(&.{
        "openocd", //openocd must be in path
        "-f", "interface/stlink.cfg", //config for stlink.  stlink must be isntalled.
        "-f", "target/stm32f1x.cfg", //config for target mcu
        "-c", "\"program zig-out/bin/" ++ prj_name ++ ".elf " ++ "verify reset exit\"", //program the elf file to board
    });

    const flash_step = b.step("flash", "Runs Openocd to flash the mcu.");
    flash_step.dependOn(&openocd_flash_cmd.step);

This is the build output:

:~/git/GPS-Tracking-Device/zig$ zig build flash
Open On-Chip Debugger 0.11.0
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
invalid command name "program zig-out/bin/gps.elf verify reset exit"
flash
└─ run openocd failure
error: the following command exited with error code 1:
openocd -f interface/stlink.cfg -f target/stm32f1x.cfg -c "program zig-out/bin/gps.elf verify reset exit" 
Build Summary: 0/2 steps succeeded; 1 failed (disable with --summary none)
flash transitive failure
└─ run openocd failure
error: the following build command failed with exit code 1:
/home/fixer/git/GPS-Tracking-Device/zig/zig-cache/o/e3a2b0e00b41e9a8732956eab5922f54/build /home/fixer/.config/Code/User/globalStorage/ziglang.vscode-zig/zig_install/zig /home/fixer/git/GPS-Tracking-Device/zig /home/fixer/git/GPS-Tracking-Device/zig/zig-cache /home/fixer/.cache/zig --seed 0xf6a6fe76 -Z49d1d99eec52c0dc flash

You can see that openocd reports: invalid command name “program zig-out/bin/gps.elf verify reset exit”

But if I run openocd from the terminal in the same folder as my build.zig it executes correctly.

Any thoughts?

Don’t put literal quotes in the arguments, you’re providing the arguments as an array so it knows where each one starts and ends already.

Yep, that fixed it. Thanks!