Skip to content

Commit a531ed6

Browse files
sago35deadprogram
authored andcommitted
main, compileopts: move GetTargetSpecs() to compileopts package
1 parent 24ae6fd commit a531ed6

File tree

3 files changed

+40
-40
lines changed

3 files changed

+40
-40
lines changed

compileopts/target.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,43 @@ func LoadTarget(options *Options) (*TargetSpec, error) {
246246
return spec, nil
247247
}
248248

249+
// GetTargetSpecs retrieves target specifications from the TINYGOROOT targets
250+
// directory. Only valid target JSON files are considered, and the function
251+
// returns a map of target names to their respective TargetSpec.
252+
func GetTargetSpecs() (map[string]*TargetSpec, error) {
253+
dir := filepath.Join(goenv.Get("TINYGOROOT"), "targets")
254+
entries, err := os.ReadDir(dir)
255+
if err != nil {
256+
return nil, fmt.Errorf("could not list targets: %w", err)
257+
}
258+
259+
maps := map[string]*TargetSpec{}
260+
for _, entry := range entries {
261+
entryInfo, err := entry.Info()
262+
if err != nil {
263+
return nil, fmt.Errorf("could not get entry info: %w", err)
264+
}
265+
if !entryInfo.Mode().IsRegular() || !strings.HasSuffix(entry.Name(), ".json") {
266+
// Only inspect JSON files.
267+
continue
268+
}
269+
path := filepath.Join(dir, entry.Name())
270+
spec, err := LoadTarget(&Options{Target: path})
271+
if err != nil {
272+
return nil, fmt.Errorf("could not list target: %w", err)
273+
}
274+
if spec.FlashMethod == "" && spec.FlashCommand == "" && spec.Emulator == "" {
275+
// This doesn't look like a regular target file, but rather like
276+
// a parent target (such as targets/cortex-m.json).
277+
continue
278+
}
279+
name := entry.Name()
280+
name = name[:len(name)-5]
281+
maps[name] = spec
282+
}
283+
return maps, nil
284+
}
285+
249286
func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) {
250287
// No target spec available. Use the default one, useful on most systems
251288
// with a regular OS.

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1743,7 +1743,7 @@ func main() {
17431743
handleCompilerError(err)
17441744
}
17451745
case "targets":
1746-
specs, err := GetTargetSpecs()
1746+
specs, err := compileopts.GetTargetSpecs()
17471747
if err != nil {
17481748
fmt.Fprintln(os.Stderr, "could not list targets:", err)
17491749
os.Exit(1)

monitor.go

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"io"
1212
"os"
1313
"os/signal"
14-
"path/filepath"
1514
"regexp"
1615
"strconv"
1716
"strings"
@@ -20,7 +19,6 @@ import (
2019
"github.com/mattn/go-tty"
2120
"github.com/tinygo-org/tinygo/builder"
2221
"github.com/tinygo-org/tinygo/compileopts"
23-
"github.com/tinygo-org/tinygo/goenv"
2422

2523
"go.bug.st/serial"
2624
"go.bug.st/serial/enumerator"
@@ -145,9 +143,9 @@ type SerialPortInfo struct {
145143
}
146144

147145
// ListSerialPort returns serial port information and any detected TinyGo
148-
// target
146+
// target.
149147
func ListSerialPorts() ([]SerialPortInfo, error) {
150-
maps, err := GetTargetSpecs()
148+
maps, err := compileopts.GetTargetSpecs()
151149
if err != nil {
152150
return nil, err
153151
}
@@ -186,41 +184,6 @@ func ListSerialPorts() ([]SerialPortInfo, error) {
186184
return serialPortInfo, nil
187185
}
188186

189-
func GetTargetSpecs() (map[string]*compileopts.TargetSpec, error) {
190-
dir := filepath.Join(goenv.Get("TINYGOROOT"), "targets")
191-
entries, err := os.ReadDir(dir)
192-
if err != nil {
193-
return nil, fmt.Errorf("could not list targets: %w", err)
194-
}
195-
196-
maps := map[string]*compileopts.TargetSpec{}
197-
for _, entry := range entries {
198-
entryInfo, err := entry.Info()
199-
if err != nil {
200-
return nil, fmt.Errorf("could not get entry info: %w", err)
201-
}
202-
if !entryInfo.Mode().IsRegular() || !strings.HasSuffix(entry.Name(), ".json") {
203-
// Only inspect JSON files.
204-
continue
205-
}
206-
path := filepath.Join(dir, entry.Name())
207-
spec, err := compileopts.LoadTarget(&compileopts.Options{Target: path})
208-
if err != nil {
209-
return nil, fmt.Errorf("cnuld not list target: %w", err)
210-
}
211-
if spec.FlashMethod == "" && spec.FlashCommand == "" && spec.Emulator == "" {
212-
// This doesn't look like a regular target file, but rather like
213-
// a parent target (such as targets/cortex-m.json).
214-
continue
215-
}
216-
name := entry.Name()
217-
name = name[:len(name)-5]
218-
//fmt.Println(name)
219-
maps[name] = spec
220-
}
221-
return maps, nil
222-
}
223-
224187
var addressMatch = regexp.MustCompile(`^panic: runtime error at 0x([0-9a-f]+): `)
225188

226189
// Extract the address from the "panic: runtime error at" message.

0 commit comments

Comments
 (0)