all: implement tinygo flash command

This will now just work:

    tinygo flash -target=arduino examples/blinky1
Этот коммит содержится в:
Ayke van Laethem 2018-09-14 20:59:28 +02:00
родитель 6450daa3c8
коммит 112f6dc01a
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: E97FF5335DFDFDED
4 изменённых файлов: 54 добавлений и 6 удалений

53
main.go
Просмотреть файл

@ -16,9 +16,7 @@ import (
) )
// Helper function for Compiler object. // Helper function for Compiler object.
func Compile(pkgName, outpath, target string, printIR, dumpSSA bool) error { func Compile(pkgName, outpath string, spec *TargetSpec, printIR, dumpSSA bool, action func(string) error) error {
spec, err := LoadTarget(target)
c, err := NewCompiler(pkgName, spec.Triple, dumpSSA) c, err := NewCompiler(pkgName, spec.Triple, dumpSSA)
if err != nil { if err != nil {
return err return err
@ -98,7 +96,17 @@ func Compile(pkgName, outpath, target string, printIR, dumpSSA bool) error {
return err return err
} }
} }
return action(tmppath)
}
}
func Build(pkgName, outpath, target string, printIR, dumpSSA bool) error {
spec, err := LoadTarget(target)
if err != nil {
return err
}
return Compile(pkgName, outpath, spec, printIR, dumpSSA, func(tmppath string) error {
if err := os.Rename(tmppath, outpath); err != nil { if err := os.Rename(tmppath, outpath); err != nil {
// Moving failed. Do a file copy. // Moving failed. Do a file copy.
inf, err := os.Open(tmppath) inf, err := os.Open(tmppath)
@ -123,7 +131,31 @@ func Compile(pkgName, outpath, target string, printIR, dumpSSA bool) error {
// Move was successful. // Move was successful.
return nil return nil
} }
})
}
func Flash(pkgName, target, port string, printIR, dumpSSA bool) error {
spec, err := LoadTarget(target)
if err != nil {
return err
} }
return Compile(pkgName, ".hex", spec, printIR, dumpSSA, func(tmppath string) error {
// Create the command.
flashCmd := spec.Flasher
parts := strings.Split(flashCmd, " ") // TODO: this should be a real shell split
for i, part := range parts {
part = strings.Replace(part, "{hex}", tmppath, -1)
part = strings.Replace(part, "{port}", port, -1)
parts[i] = part
}
// Execute the command.
cmd := exec.Command(parts[0], parts[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
})
} }
// Run the specified package directly (using JIT or interpretation). // Run the specified package directly (using JIT or interpretation).
@ -160,6 +192,7 @@ func usage() {
fmt.Fprintf(os.Stderr, "usage: %s command [-printir] [-target=<target>] -o <output> <input>\n", os.Args[0]) fmt.Fprintf(os.Stderr, "usage: %s command [-printir] [-target=<target>] -o <output> <input>\n", os.Args[0])
fmt.Fprintln(os.Stderr, "\ncommands:") fmt.Fprintln(os.Stderr, "\ncommands:")
fmt.Fprintln(os.Stderr, " build: compile packages and dependencies") fmt.Fprintln(os.Stderr, " build: compile packages and dependencies")
fmt.Fprintln(os.Stderr, " flash: compile and flash to the device")
fmt.Fprintln(os.Stderr, " help: print this help text") fmt.Fprintln(os.Stderr, " help: print this help text")
fmt.Fprintln(os.Stderr, " run: run package in an interpreter") fmt.Fprintln(os.Stderr, " run: run package in an interpreter")
fmt.Fprintln(os.Stderr, "\nflags:") fmt.Fprintln(os.Stderr, "\nflags:")
@ -171,6 +204,7 @@ func main() {
printIR := flag.Bool("printir", false, "print LLVM IR") printIR := flag.Bool("printir", false, "print LLVM IR")
dumpSSA := flag.Bool("dumpssa", false, "dump internal Go SSA") dumpSSA := flag.Bool("dumpssa", false, "dump internal Go SSA")
target := flag.String("target", llvm.DefaultTargetTriple(), "LLVM target") target := flag.String("target", llvm.DefaultTargetTriple(), "LLVM target")
port := flag.String("port", "/dev/ttyACM0", "flash port")
if len(os.Args) < 2 { if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "No command-line arguments supplied.") fmt.Fprintln(os.Stderr, "No command-line arguments supplied.")
@ -195,7 +229,18 @@ func main() {
usage() usage()
os.Exit(1) os.Exit(1)
} }
err := Compile(flag.Arg(0), *outpath, *target, *printIR, *dumpSSA) err := Build(flag.Arg(0), *outpath, *target, *printIR, *dumpSSA)
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
case "flash":
if *outpath != "" {
fmt.Fprintln(os.Stderr, "Output cannot be specified with the flash command.")
usage()
os.Exit(1)
}
err := Flash(flag.Arg(0), *target, *port, *printIR, *dumpSSA)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "error:", err) fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1) os.Exit(1)

Просмотреть файл

@ -19,6 +19,7 @@ type TargetSpec struct {
Linker string `json:"linker"` Linker string `json:"linker"`
PreLinkArgs []string `json:"pre-link-args"` PreLinkArgs []string `json:"pre-link-args"`
Objcopy string `json:"objcopy"` Objcopy string `json:"objcopy"`
Flasher string `json:"flash"`
} }
// Load a target specification // Load a target specification

Просмотреть файл

@ -3,5 +3,6 @@
"build-tags": ["avr", "avr8", "atmega", "atmega328p", "js", "wasm"], "build-tags": ["avr", "avr8", "atmega", "atmega328p", "js", "wasm"],
"linker": "avr-gcc", "linker": "avr-gcc",
"pre-link-args": ["-nostdlib", "-T", "targets/avr.ld", "-Wl,--gc-sections", "targets/avr.S"], "pre-link-args": ["-nostdlib", "-T", "targets/avr.ld", "-Wl,--gc-sections", "targets/avr.S"],
"objcopy": "avr-objcopy" "objcopy": "avr-objcopy",
"flash": "avrdude -c arduino -p atmega328p -P {port} -U flash:w:{hex}"
} }

Просмотреть файл

@ -3,5 +3,6 @@
"build-tags": ["nrf", "nrf52", "nrf52832", "js", "wasm"], "build-tags": ["nrf", "nrf52", "nrf52832", "js", "wasm"],
"linker": "arm-none-eabi-gcc", "linker": "arm-none-eabi-gcc",
"pre-link-args": ["-nostdlib", "-nostartfiles", "-mcpu=cortex-m4", "-mthumb", "-T", "targets/arm.ld", "-Wl,--gc-sections", "-fno-exceptions", "-fno-unwind-tables", "-ffunction-sections", "-fdata-sections", "-Os", "-DNRF52832_XXAA", "-D__STARTUP_CLEAR_BSS", "-Ilib/CMSIS/CMSIS/Include", "lib/nrfx/mdk/gcc_startup_nrf51.S", "lib/nrfx/mdk/system_nrf52.c"], "pre-link-args": ["-nostdlib", "-nostartfiles", "-mcpu=cortex-m4", "-mthumb", "-T", "targets/arm.ld", "-Wl,--gc-sections", "-fno-exceptions", "-fno-unwind-tables", "-ffunction-sections", "-fdata-sections", "-Os", "-DNRF52832_XXAA", "-D__STARTUP_CLEAR_BSS", "-Ilib/CMSIS/CMSIS/Include", "lib/nrfx/mdk/gcc_startup_nrf51.S", "lib/nrfx/mdk/system_nrf52.c"],
"objcopy": "arm-none-eabi-objcopy" "objcopy": "arm-none-eabi-objcopy",
"flash": "nrfjprog -f nrf52 --sectorerase --program {hex} --reset"
} }