diff options
author | sago35 <[email protected]> | 2023-11-01 08:20:37 +0900 |
---|---|---|
committer | Ron Evans <[email protected]> | 2023-11-02 15:37:43 +0100 |
commit | a531ed614a58346f9790add14717a768ac95af9c (patch) | |
tree | fd1befc0a304199230f422c55a439b5101f66c94 /compileopts | |
parent | 24ae6fdf293502f032ed8cec857515b356b32b4a (diff) | |
download | tinygo-a531ed614a58346f9790add14717a768ac95af9c.tar.gz tinygo-a531ed614a58346f9790add14717a768ac95af9c.zip |
main, compileopts: move GetTargetSpecs() to compileopts package
Diffstat (limited to 'compileopts')
-rw-r--r-- | compileopts/target.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/compileopts/target.go b/compileopts/target.go index 92561f1e0..b21a0183a 100644 --- a/compileopts/target.go +++ b/compileopts/target.go @@ -246,6 +246,43 @@ func LoadTarget(options *Options) (*TargetSpec, error) { return spec, nil } +// GetTargetSpecs retrieves target specifications from the TINYGOROOT targets +// directory. Only valid target JSON files are considered, and the function +// returns a map of target names to their respective TargetSpec. +func GetTargetSpecs() (map[string]*TargetSpec, error) { + dir := filepath.Join(goenv.Get("TINYGOROOT"), "targets") + entries, err := os.ReadDir(dir) + if err != nil { + return nil, fmt.Errorf("could not list targets: %w", err) + } + + maps := map[string]*TargetSpec{} + for _, entry := range entries { + entryInfo, err := entry.Info() + if err != nil { + return nil, fmt.Errorf("could not get entry info: %w", err) + } + if !entryInfo.Mode().IsRegular() || !strings.HasSuffix(entry.Name(), ".json") { + // Only inspect JSON files. + continue + } + path := filepath.Join(dir, entry.Name()) + spec, err := LoadTarget(&Options{Target: path}) + if err != nil { + return nil, fmt.Errorf("could not list target: %w", err) + } + if spec.FlashMethod == "" && spec.FlashCommand == "" && spec.Emulator == "" { + // This doesn't look like a regular target file, but rather like + // a parent target (such as targets/cortex-m.json). + continue + } + name := entry.Name() + name = name[:len(name)-5] + maps[name] = spec + } + return maps, nil +} + func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) { // No target spec available. Use the default one, useful on most systems // with a regular OS. |