refine: reduce unnecessary format conversion
This commit is contained in:
@@ -68,6 +68,8 @@ func buildOutFmts(pkgName string, conf *Config, multiPkg bool, crossCompile *cro
|
|||||||
return details, nil
|
return details, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
needRun := slices.Contains([]Mode{ModeRun, ModeTest, ModeCmpTest, ModeInstall}, conf.Mode)
|
||||||
|
|
||||||
if multiPkg {
|
if multiPkg {
|
||||||
details.Out, err = genTempOutputFile(pkgName, conf.AppExt)
|
details.Out, err = genTempOutputFile(pkgName, conf.AppExt)
|
||||||
if err != nil {
|
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
|
// Check emulator format if emulator mode is enabled
|
||||||
outFmt := ""
|
outFmt := ""
|
||||||
if conf.Emulator {
|
if needRun {
|
||||||
if crossCompile.Emulator != "" {
|
if conf.Emulator {
|
||||||
outFmt = firmware.ExtractFileFormatFromCommand(crossCompile.Emulator)
|
if crossCompile.Emulator != "" {
|
||||||
}
|
outFmt = firmware.ExtractFileFormatFromCommand(crossCompile.Emulator)
|
||||||
} else {
|
}
|
||||||
if crossCompile.Device.Flash.Method == "command" {
|
} else {
|
||||||
outFmt = firmware.ExtractFileFormatFromCommand(crossCompile.Device.Flash.Command)
|
if crossCompile.Device.Flash.Method == "command" {
|
||||||
|
outFmt = firmware.ExtractFileFormatFromCommand(crossCompile.Device.Flash.Command)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if outFmt != "" {
|
if outFmt != "" {
|
||||||
@@ -101,7 +105,7 @@ func buildOutFmts(pkgName string, conf *Config, multiPkg bool, crossCompile *cro
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check binary format and set corresponding format
|
// 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)
|
envName := firmware.BinaryFormatToEnvName(crossCompile.BinaryFormat)
|
||||||
if envName != "" {
|
if envName != "" {
|
||||||
setOutFmt(conf, envName)
|
setOutFmt(conf, envName)
|
||||||
@@ -112,15 +116,13 @@ func buildOutFmts(pkgName string, conf *Config, multiPkg bool, crossCompile *cro
|
|||||||
if details.Out != "" {
|
if details.Out != "" {
|
||||||
base := strings.TrimSuffix(details.Out, filepath.Ext(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"
|
details.Bin = base + ".bin"
|
||||||
}
|
}
|
||||||
if conf.OutFmts.Hex {
|
if conf.OutFmts.Hex {
|
||||||
details.Bin = base + ".bin" // hex depends on bin
|
|
||||||
details.Hex = base + ".hex"
|
details.Hex = base + ".hex"
|
||||||
}
|
}
|
||||||
if conf.OutFmts.Img {
|
if conf.OutFmts.Img {
|
||||||
details.Bin = base + ".bin" // img depends on bin
|
|
||||||
details.Img = base + ".img"
|
details.Img = base + ".img"
|
||||||
}
|
}
|
||||||
if conf.OutFmts.Uf2 {
|
if conf.OutFmts.Uf2 {
|
||||||
|
|||||||
@@ -7,8 +7,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MakeFirmwareImage creates a firmware image from the given input file.
|
// makeFirmwareImage creates a firmware image from the given input file.
|
||||||
func MakeFirmwareImage(infile, outfile, format, fmtDetail string) error {
|
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") {
|
if strings.HasPrefix(format, "esp") {
|
||||||
return makeESPFirmareImage(infile, outfile, format)
|
return makeESPFirmareImage(infile, outfile, format)
|
||||||
} else if format == "uf2" {
|
} else if format == "uf2" {
|
||||||
@@ -32,34 +33,11 @@ func ExtractFileFormatFromCommand(cmd string) string {
|
|||||||
return ""
|
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
|
// ConvertFormats processes format conversions for embedded targets only
|
||||||
func ConvertFormats(binFmt, fmtDetail string, envMap map[string]string) error {
|
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)
|
// Convert to bin format first (needed for img)
|
||||||
if envMap["bin"] != "" {
|
if envMap["bin"] != "" {
|
||||||
err := MakeFirmwareImage(envMap["out"], envMap["bin"], binFmt, fmtDetail)
|
err := makeFirmwareImage(envMap["out"], envMap["bin"], binFmt, fmtDetail)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to convert to bin format: %w", err)
|
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
|
// Convert to hex format
|
||||||
if envMap["hex"] != "" {
|
if envMap["hex"] != "" {
|
||||||
err := MakeFirmwareImage(envMap["out"], envMap["hex"], binFmt, fmtDetail)
|
err := makeFirmwareImage(envMap["out"], envMap["hex"], binFmt, fmtDetail)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to convert to hex format: %w", err)
|
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"] != "" {
|
if envMap["img"] != "" {
|
||||||
err := MakeFirmwareImage(envMap["out"], envMap["img"], binFmt+"-img", fmtDetail)
|
err := makeFirmwareImage(envMap["out"], envMap["img"], binFmt+"-img", fmtDetail)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to convert to img format: %w", err)
|
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
|
// Convert to uf2 format
|
||||||
if envMap["uf2"] != "" {
|
if envMap["uf2"] != "" {
|
||||||
err := MakeFirmwareImage(envMap["out"], envMap["uf2"], binFmt, fmtDetail)
|
err := makeFirmwareImage(envMap["out"], envMap["uf2"], binFmt, fmtDetail)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to convert to uf2 format: %w", err)
|
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
|
// Convert to zip format
|
||||||
if envMap["zip"] != "" {
|
if envMap["zip"] != "" {
|
||||||
err := MakeFirmwareImage(envMap["out"], envMap["zip"], binFmt, fmtDetail)
|
err := makeFirmwareImage(envMap["out"], envMap["zip"], binFmt, fmtDetail)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to convert to zip format: %w", err)
|
return fmt.Errorf("failed to convert to zip format: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ package firmware
|
|||||||
import (
|
import (
|
||||||
"debug/elf"
|
"debug/elf"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
|
||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -104,30 +103,3 @@ func extractROM(path string) (uint64, []byte, error) {
|
|||||||
return progs[0].Paddr, rom, nil
|
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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user