Skip to content

Commit e7227df

Browse files
aykevldeadprogram
authored andcommitted
main: implement tinygo targets to list usable targets
This patch adds the `tinygo targets` command, which lists usable targets (targets that can be used in the `-target` flag). The assumption here is that usable targets can either be flashed or emulated by TinyGo. There is one exception where it doesn't work yet: the nintendoswitch target. Right now it requires some manual steps to build a .nro file which can then be run by yuzu, hence why it doesn't show up in the list.
1 parent b99175a commit e7227df

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"go/scanner"
99
"go/types"
1010
"io"
11+
"io/ioutil"
1112
"os"
1213
"os/exec"
1314
"os/signal"
@@ -921,6 +922,35 @@ func main() {
921922
}
922923
err := Test(pkgName, options)
923924
handleCompilerError(err)
925+
case "targets":
926+
dir := filepath.Join(goenv.Get("TINYGOROOT"), "targets")
927+
entries, err := ioutil.ReadDir(dir)
928+
if err != nil {
929+
fmt.Fprintln(os.Stderr, "could not list targets:", err)
930+
os.Exit(1)
931+
return
932+
}
933+
for _, entry := range entries {
934+
if !entry.Mode().IsRegular() || !strings.HasSuffix(entry.Name(), ".json") {
935+
// Only inspect JSON files.
936+
continue
937+
}
938+
path := filepath.Join(dir, entry.Name())
939+
spec, err := compileopts.LoadTarget(path)
940+
if err != nil {
941+
fmt.Fprintln(os.Stderr, "could not list target:", err)
942+
os.Exit(1)
943+
return
944+
}
945+
if spec.FlashMethod == "" && spec.FlashCommand == "" && spec.Emulator == nil {
946+
// This doesn't look like a regular target file, but rather like
947+
// a parent target (such as targets/cortex-m.json).
948+
continue
949+
}
950+
name := entry.Name()
951+
name = name[:len(name)-5]
952+
fmt.Println(name)
953+
}
924954
case "info":
925955
if flag.NArg() == 1 {
926956
options.Target = flag.Arg(0)

0 commit comments

Comments
 (0)