Skip to content

flash: add -o flag support to save built binary (Fixes #4937) #4942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

rdon-key
Copy link
Contributor

@rdon-key rdon-key commented Jul 2, 2025

This PR adds -o flag support to the tinygo flash command, allowing users to save the built binary to a specified output file. This brings flash in line with the behavior of tinygo build, promoting consistency and improving usability.

Fixes #4937

Changes

  • Added -o flag support to the flash command
  • Introduced validateOutputFormat function to check compatibility with the target format
  • Saved output file after build and before flashing
  • Updated help text for the flash command with usage examples

Behavior

  • When -o is specified, the binary is both saved and flashed
  • Output file format must match the target's expected format (e.g., .uf2, .hex)
  • If the format is invalid, the command fails before building
  • The saved file is identical to what is flashed

Testing Results

# Build works as usual
$ tinygo build -o build.uf2 --target waveshare-rp2040-zero .
   code    data     bss |   flash     ram
 258388    1664    5352 |  260052    7016

# Flash with -o saves file and flashes the device
$ tinygo flash -o flash.uf2 --target waveshare-rp2040-zero .
   code    data     bss |   flash     ram
 258388    1664    5352 |  260052    7016

# Format validation works
$ tinygo flash -o flash.hex --target waveshare-rp2040-zero .
output format .hex does not match target format .uf2

# File content is identical between build and flash
$ sha256sum flash.uf2 build.uf2
95cc493412d9937433cd0ba9e8d667be92de19bab18eaba6cc49ada4b8d3a8c5 *flash.uf2
95cc493412d9937433cd0ba9e8d667be92de19bab18eaba6cc49ada4b8d3a8c5 *build.uf2

# Updated Help Output
$ tinygo help flash
Flash the program to a microcontroller. Some common flags are described below.

        -target={name}:
                        Specifies the type of microcontroller that is used. The name of the
                        microcontroller is given on the individual pages for each board type
                        listed under Microcontrollers
                        (https://tinygo.org/docs/reference/microcontrollers/).
                        Examples: "arduino-nano", "d1mini", "xiao".

        -o={filename}:
                        Save the built binary to the specified output file. The file
                        format must match the target's expected format (e.g., .hex,
                        .uf2). Both flashing and saving will be performed.

        -monitor:
                        Start the serial monitor (see below) immediately after
                        flashing. However, some microcontrollers need a split second
                        or two to configure the serial port after flashing, and
                        using the "-monitor" flag can fail because the serial
                        monitor starts too quickly. In that case, use the "tinygo
                        monitor" command explicitly.

@@ -339,8 +339,21 @@ func dirsToModuleRootAbs(maindir, modroot string) []string {
return dirs
}

// validateOutputFormat checks if the output file extension matches the expected format
func validateOutputFormat(outpath, expectedExt string) error {
if outpath == "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code checks for this condition already before calling this function, so this seems redundant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants