refactor: multi format generation and llgo build flags

This commit is contained in:
Li Jie
2025-09-07 13:32:11 +08:00
parent c0afe199c2
commit 16c8402065
12 changed files with 541 additions and 979 deletions

16
internal/firmware/env.go Normal file
View File

@@ -0,0 +1,16 @@
package firmware
import "strings"
// BinaryFormatToEnvName returns the environment variable name based on the binary format
// Returns the format name for template expansion (e.g., "bin", "uf2", "zip")
func BinaryFormatToEnvName(binaryFormat string) string {
if strings.HasPrefix(binaryFormat, "esp") {
return "bin"
} else if strings.HasPrefix(binaryFormat, "uf2") {
return "uf2"
} else if strings.HasPrefix(binaryFormat, "nrf-dfu") {
return "zip"
}
return ""
}

View File

@@ -1,16 +0,0 @@
package firmware
import "strings"
// BinaryExt returns the binary file extension based on the binary format
// Returns ".bin" for ESP-based formats, "" for others
func BinaryExt(binaryFormat string) string {
if strings.HasPrefix(binaryFormat, "esp") {
return ".bin"
} else if strings.HasPrefix(binaryFormat, "uf2") {
return ".uf2"
} else if strings.HasPrefix(binaryFormat, "nrf-dfu") {
return ".zip"
}
return ""
}

View File

@@ -1,31 +0,0 @@
//go:build !llgo
// +build !llgo
package firmware
import "testing"
func TestBinaryExt(t *testing.T) {
tests := []struct {
name string
binaryFormat string
expected string
}{
{"ESP32", "esp32", ".bin"},
{"ESP8266", "esp8266", ".bin"},
{"ESP32C3", "esp32c3", ".bin"},
{"UF2", "uf2", ".uf2"},
{"ELF", "elf", ""},
{"Empty", "", ""},
{"NRF-DFU", "nrf-dfu", ".zip"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := BinaryExt(tt.binaryFormat)
if result != tt.expected {
t.Errorf("BinaryExt() = %q, want %q", result, tt.expected)
}
})
}
}

View File

@@ -20,12 +20,12 @@ func MakeFirmwareImage(infile, outfile, format, fmtDetail string) error {
return fmt.Errorf("unsupported firmware format: %s", format)
}
// ExtractFileFormatFromEmulator extracts file format from emulator command template
// ExtractFileFormatFromCommand extracts file format from command template
// Returns the format if found (e.g. "bin", "hex", "zip", "img"), empty string if not found
func ExtractFileFormatFromEmulator(emulatorCmd string) string {
func ExtractFileFormatFromCommand(cmd string) string {
formats := []string{"bin", "hex", "zip", "img", "uf2"}
for _, format := range formats {
if strings.Contains(emulatorCmd, "{"+format+"}") {
if strings.Contains(cmd, "{"+format+"}") {
return format
}
}
@@ -52,21 +52,52 @@ func GetFileExtFromFormat(format string) string {
}
}
// ConvertOutput converts a binary file to the specified format.
// If binaryFormat == fileFormat, no conversion is needed.
// Otherwise, only hex format conversion is supported.
func ConvertOutput(infile, outfile, binaryFormat, fileFormat string) error {
// If formats match, no conversion needed
if binaryFormat == fileFormat {
return nil
// 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)
if err != nil {
return fmt.Errorf("failed to convert to bin format: %w", err)
}
}
// Only support conversion to hex and format
if fileFormat == "hex" {
return convertToHex(infile, outfile)
// Convert to hex format
if envMap["hex"] != "" {
err := MakeFirmwareImage(envMap["out"], envMap["hex"], binFmt, fmtDetail)
if err != nil {
return fmt.Errorf("failed to convert to hex format: %w", err)
}
}
return fmt.Errorf("unsupported format conversion from %s to %s", binaryFormat, fileFormat)
// Convert to img format (depends on bin)
if envMap["img"] != "" {
err := MakeFirmwareImage(envMap["out"], envMap["img"], binFmt+"-img", fmtDetail)
if err != nil {
return fmt.Errorf("failed to convert to img format: %w", err)
}
}
// Convert to uf2 format
if envMap["uf2"] != "" {
err := MakeFirmwareImage(envMap["out"], envMap["uf2"], binFmt, fmtDetail)
if err != nil {
return fmt.Errorf("failed to convert to uf2 format: %w", err)
}
}
// Convert to zip format
if envMap["zip"] != "" {
err := MakeFirmwareImage(envMap["out"], envMap["zip"], binFmt, fmtDetail)
if err != nil {
return fmt.Errorf("failed to convert to zip format: %w", err)
}
}
return nil
}
// convertToHex converts binary file to hex format (each byte as two hex characters)