From df07513a37eb1a71ebed1fae797e98fe915301bc Mon Sep 17 00:00:00 2001 From: Li Jie Date: Sun, 7 Sep 2025 14:31:59 +0800 Subject: [PATCH] refine: reduce unnecessary format conversion --- internal/build/outputs.go | 24 +++++++++++---------- internal/firmware/firmware.go | 40 ++++++++--------------------------- internal/firmware/objcopy.go | 28 ------------------------ 3 files changed, 22 insertions(+), 70 deletions(-) diff --git a/internal/build/outputs.go b/internal/build/outputs.go index 0fa64b9e..0fc4ab60 100644 --- a/internal/build/outputs.go +++ b/internal/build/outputs.go @@ -68,6 +68,8 @@ func buildOutFmts(pkgName string, conf *Config, multiPkg bool, crossCompile *cro return details, nil } + needRun := slices.Contains([]Mode{ModeRun, ModeTest, ModeCmpTest, ModeInstall}, conf.Mode) + if multiPkg { details.Out, err = genTempOutputFile(pkgName, conf.AppExt) if err != nil { @@ -87,13 +89,15 @@ func buildOutFmts(pkgName string, conf *Config, multiPkg bool, crossCompile *cro // Check emulator format if emulator mode is enabled outFmt := "" - if conf.Emulator { - if crossCompile.Emulator != "" { - outFmt = firmware.ExtractFileFormatFromCommand(crossCompile.Emulator) - } - } else { - if crossCompile.Device.Flash.Method == "command" { - outFmt = firmware.ExtractFileFormatFromCommand(crossCompile.Device.Flash.Command) + if needRun { + if conf.Emulator { + if crossCompile.Emulator != "" { + outFmt = firmware.ExtractFileFormatFromCommand(crossCompile.Emulator) + } + } else { + if crossCompile.Device.Flash.Method == "command" { + outFmt = firmware.ExtractFileFormatFromCommand(crossCompile.Device.Flash.Command) + } } } if outFmt != "" { @@ -101,7 +105,7 @@ func buildOutFmts(pkgName string, conf *Config, multiPkg bool, crossCompile *cro } // Check binary format and set corresponding format - if crossCompile.BinaryFormat != "" && slices.Contains([]Mode{ModeRun, ModeTest, ModeCmpTest, ModeInstall}, conf.Mode) { + if crossCompile.BinaryFormat != "" && needRun { envName := firmware.BinaryFormatToEnvName(crossCompile.BinaryFormat) if envName != "" { setOutFmt(conf, envName) @@ -112,15 +116,13 @@ func buildOutFmts(pkgName string, conf *Config, multiPkg bool, crossCompile *cro if details.Out != "" { base := strings.TrimSuffix(details.Out, filepath.Ext(details.Out)) - if conf.OutFmts.Bin || conf.OutFmts.Img { + if conf.OutFmts.Bin || conf.OutFmts.Img || conf.OutFmts.Hex { details.Bin = base + ".bin" } if conf.OutFmts.Hex { - details.Bin = base + ".bin" // hex depends on bin details.Hex = base + ".hex" } if conf.OutFmts.Img { - details.Bin = base + ".bin" // img depends on bin details.Img = base + ".img" } if conf.OutFmts.Uf2 { diff --git a/internal/firmware/firmware.go b/internal/firmware/firmware.go index 0528378c..0b23d8a8 100644 --- a/internal/firmware/firmware.go +++ b/internal/firmware/firmware.go @@ -7,8 +7,9 @@ import ( "strings" ) -// MakeFirmwareImage creates a firmware image from the given input file. -func MakeFirmwareImage(infile, outfile, format, fmtDetail string) error { +// makeFirmwareImage creates a firmware image from the given input file. +func makeFirmwareImage(infile, outfile, format, fmtDetail string) error { + fmt.Fprintf(os.Stderr, "Generating firmware image: %s -> %s (format: %s, detail: %s)\n", infile, outfile, format, fmtDetail) if strings.HasPrefix(format, "esp") { return makeESPFirmareImage(infile, outfile, format) } else if format == "uf2" { @@ -32,34 +33,11 @@ func ExtractFileFormatFromCommand(cmd string) string { return "" } -// GetFileExtFromFormat converts file format to file extension -func GetFileExtFromFormat(format string) string { - switch format { - case "bin": - return ".bin" - case "hex": - return ".hex" - case "elf": - return "" - case "uf2": - return ".uf2" - case "zip": - return ".zip" - case "img": - return ".img" - default: - return "" - } -} - // ConvertFormats processes format conversions for embedded targets only func ConvertFormats(binFmt, fmtDetail string, envMap map[string]string) error { - fmt.Printf("Converting formats based on binary format: %s\n", binFmt) - fmt.Printf("Format details: %s\n", fmtDetail) - fmt.Printf("Environment map: %+v\n", envMap) // Convert to bin format first (needed for img) if envMap["bin"] != "" { - err := MakeFirmwareImage(envMap["out"], envMap["bin"], binFmt, fmtDetail) + err := makeFirmwareImage(envMap["out"], envMap["bin"], binFmt, fmtDetail) if err != nil { return fmt.Errorf("failed to convert to bin format: %w", err) } @@ -67,15 +45,15 @@ func ConvertFormats(binFmt, fmtDetail string, envMap map[string]string) error { // Convert to hex format if envMap["hex"] != "" { - err := MakeFirmwareImage(envMap["out"], envMap["hex"], binFmt, fmtDetail) + err := makeFirmwareImage(envMap["out"], envMap["hex"], binFmt, fmtDetail) if err != nil { return fmt.Errorf("failed to convert to hex format: %w", err) } } - // Convert to img format (depends on bin) + // Convert to img format if envMap["img"] != "" { - err := MakeFirmwareImage(envMap["out"], envMap["img"], binFmt+"-img", fmtDetail) + err := makeFirmwareImage(envMap["out"], envMap["img"], binFmt+"-img", fmtDetail) if err != nil { return fmt.Errorf("failed to convert to img format: %w", err) } @@ -83,7 +61,7 @@ func ConvertFormats(binFmt, fmtDetail string, envMap map[string]string) error { // Convert to uf2 format if envMap["uf2"] != "" { - err := MakeFirmwareImage(envMap["out"], envMap["uf2"], binFmt, fmtDetail) + err := makeFirmwareImage(envMap["out"], envMap["uf2"], binFmt, fmtDetail) if err != nil { return fmt.Errorf("failed to convert to uf2 format: %w", err) } @@ -91,7 +69,7 @@ func ConvertFormats(binFmt, fmtDetail string, envMap map[string]string) error { // Convert to zip format if envMap["zip"] != "" { - err := MakeFirmwareImage(envMap["out"], envMap["zip"], binFmt, fmtDetail) + err := makeFirmwareImage(envMap["out"], envMap["zip"], binFmt, fmtDetail) if err != nil { return fmt.Errorf("failed to convert to zip format: %w", err) } diff --git a/internal/firmware/objcopy.go b/internal/firmware/objcopy.go index e163f41f..dd978667 100644 --- a/internal/firmware/objcopy.go +++ b/internal/firmware/objcopy.go @@ -5,7 +5,6 @@ package firmware import ( "debug/elf" "io" - "os" "sort" ) @@ -104,30 +103,3 @@ func extractROM(path string) (uint64, []byte, error) { return progs[0].Paddr, rom, nil } } - -// objcopy converts an ELF file to a different (simpler) output file format: -// .bin or .hex. It extracts only the .text section. -func objcopy(infile, outfile, binaryFormat string) error { - f, err := os.OpenFile(outfile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) - if err != nil { - return err - } - defer f.Close() - - // Read the .text segment. - _, data, err := extractROM(infile) - if err != nil { - return err - } - - // Write to the file, in the correct format. - switch binaryFormat { - case "bin": - // The start address is not stored in raw firmware files (therefore you - // should use .hex files in most cases). - _, err := f.Write(data) - return err - default: - panic("unreachable") - } -}