From a07fcce993522026cd150adb5aa5c0bfce2bc601 Mon Sep 17 00:00:00 2001 From: rdon Date: Thu, 3 Jul 2025 00:30:04 +0900 Subject: [PATCH 1/4] Add -o flag support to flash command. Fixes #4937 --- main.go | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index a854a75073..17724b2510 100644 --- a/main.go +++ b/main.go @@ -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 == "" { + return nil // no output file specified + } + + actualExt := filepath.Ext(outpath) + if actualExt != expectedExt { + return fmt.Errorf("output format %s does not match target format %s", actualExt, expectedExt) + } + return nil +} + // Flash builds and flashes the built binary to the given serial port. -func Flash(pkgName, port string, options *compileopts.Options) error { +func Flash(pkgName, port, outpath string, options *compileopts.Options) error { config, err := builder.NewConfig(options) if err != nil { return err @@ -389,13 +402,24 @@ func Flash(pkgName, port string, options *compileopts.Options) error { if !options.Work { defer os.RemoveAll(tmpdir) } - + // Validate output format before building + if outpath != "" { + if err := validateOutputFormat(outpath, fileExt); err != nil { + return err + } + } // Build the binary. result, err := builder.Build(pkgName, fileExt, tmpdir, config) if err != nil { return err } + // Save output file if specified (after build, before flashing) + if outpath != "" { + if err := copyFile(result.Binary, outpath); err != nil { + return fmt.Errorf("failed to save output file: %v", err) + } + } // do we need port reset to put MCU into bootloader mode? if config.Target.PortReset == "true" && flashMethod != "openocd" { port, err := getDefaultPort(port, config.Target.SerialPort) @@ -1297,6 +1321,11 @@ extension at all.` (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 @@ -1627,7 +1656,7 @@ func main() { flag.BoolVar(&flagTest, "test", false, "supply -test flag to go list") } var outpath string - if command == "help" || command == "build" || command == "test" { + if command == "help" || command == "build" || command == "test" || command == "flash" { flag.StringVar(&outpath, "o", "", "output filename") } @@ -1778,7 +1807,7 @@ func main() { case "flash", "gdb", "lldb": pkgName := filepath.ToSlash(flag.Arg(0)) if command == "flash" { - err := Flash(pkgName, *port, options) + err := Flash(pkgName, *port, outpath, options) printBuildOutput(err, *flagJSON) } else { if !options.Debug { From ef8ad821ed4b45bf867ba08ff335e2e5f34869c2 Mon Sep 17 00:00:00 2001 From: rdon Date: Fri, 4 Jul 2025 13:09:22 +0900 Subject: [PATCH 2/4] Remove empty outpath check in validateOutputFormat --- main.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/main.go b/main.go index 17724b2510..c8955780ac 100644 --- a/main.go +++ b/main.go @@ -341,10 +341,6 @@ func dirsToModuleRootAbs(maindir, modroot string) []string { // validateOutputFormat checks if the output file extension matches the expected format func validateOutputFormat(outpath, expectedExt string) error { - if outpath == "" { - return nil // no output file specified - } - actualExt := filepath.Ext(outpath) if actualExt != expectedExt { return fmt.Errorf("output format %s does not match target format %s", actualExt, expectedExt) From cf2f201f7d6b1fe3d51ca80b46fc8fdb1a903257 Mon Sep 17 00:00:00 2001 From: rdon Date: Fri, 4 Jul 2025 20:03:17 +0900 Subject: [PATCH 3/4] re-run CI From 7394a2a2ba517a7dcf9a36bb5f3035175f8c731e Mon Sep 17 00:00:00 2001 From: rdon Date: Sat, 5 Jul 2025 11:20:02 +0900 Subject: [PATCH 4/4] re-run CI