test output format for all target/emuator/flash-method

This commit is contained in:
Li Jie
2025-09-06 18:58:43 +08:00
parent 7cad146013
commit 549beeb101
5 changed files with 335 additions and 41 deletions

View File

@@ -3,7 +3,9 @@ package build
import (
"os"
"path/filepath"
"strings"
"github.com/goplus/llgo/internal/crosscompile"
"github.com/goplus/llgo/internal/firmware"
)
@@ -19,15 +21,16 @@ type OutputCfg struct {
}
// genOutputs generates appropriate output paths based on the configuration
func genOutputs(conf *Config, pkgName string, multiPkg bool, emulator, binFmt string) (OutputCfg, error) {
func genOutputs(conf *Config, pkgName string, multiPkg bool, crossCompile *crosscompile.Export) (OutputCfg, error) {
var cfg OutputCfg
// Calculate binary extension and set up format info
binFmt := crossCompile.BinaryFormat
binExt := firmware.BinaryExt(binFmt)
cfg.BinFmt = binFmt
// Determine output format and extension
cfg.FileFmt, cfg.OutExt = determineFormat(conf, emulator)
cfg.FileFmt, cfg.OutExt = determineFormat(conf, crossCompile)
// Handle special .img case and set conversion flags
cfg.DirectGen = shouldDirectGen(cfg.OutExt, binExt)
@@ -52,22 +55,70 @@ func genOutputs(conf *Config, pkgName string, multiPkg bool, emulator, binFmt st
}
// determineFormat determines the file format and extension
func determineFormat(conf *Config, emulator string) (format, ext string) {
func determineFormat(conf *Config, crossCompile *crosscompile.Export) (format, ext string) {
if conf.FileFormat != "" {
// User specified file format
return conf.FileFormat, firmware.GetFileExtFromFormat(conf.FileFormat)
}
if conf.Mode == ModeRun && conf.Emulator && emulator != "" {
if conf.Mode == ModeRun && conf.Emulator && crossCompile.Emulator != "" {
// Emulator mode - extract format from emulator command
if emulatorFmt := firmware.ExtractFileFormatFromEmulator(emulator); emulatorFmt != "" {
if emulatorFmt := firmware.ExtractFileFormatFromEmulator(crossCompile.Emulator); emulatorFmt != "" {
return emulatorFmt, firmware.GetFileExtFromFormat(emulatorFmt)
}
}
// Device flashing - determine format based on flash method and target
if conf.Target != "" && (conf.Mode == ModeInstall || conf.Mode == ModeRun || conf.Mode == ModeTest || conf.Mode == ModeCmpTest) {
if flashExt := determineFlashFormat(crossCompile); flashExt != "" {
return flashExt[1:], flashExt // Remove the dot for format, keep for ext
}
}
return "", ""
}
// determineFlashFormat determines the required file format for flashing based on flash method
func determineFlashFormat(crossCompile *crosscompile.Export) string {
if crossCompile == nil {
return ""
}
flashMethod := crossCompile.Flash.Method
switch flashMethod {
case "command", "":
// Extract format from flash command tokens
flashCommand := crossCompile.Flash.Command
switch {
case strings.Contains(flashCommand, "{hex}"):
return ".hex"
case strings.Contains(flashCommand, "{elf}"):
return ".elf"
case strings.Contains(flashCommand, "{bin}"):
return ".bin"
case strings.Contains(flashCommand, "{uf2}"):
return ".uf2"
case strings.Contains(flashCommand, "{zip}"):
return ".zip"
case strings.Contains(flashCommand, "{img}"):
return ".img"
default:
return ""
}
case "msd":
if crossCompile.MSD.FirmwareName == "" {
return ""
}
return filepath.Ext(crossCompile.MSD.FirmwareName)
case "openocd":
return ".hex"
case "bmp":
return ".elf"
default:
return ""
}
}
// shouldDirectGen determines if direct firmware generation is possible
func shouldDirectGen(outExt, binExt string) bool {
return outExt == "" || outExt == binExt || outExt == ".img"