aboutsummaryrefslogtreecommitdiffhomepage
path: root/compileopts
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2022-04-22 17:45:19 +0200
committerRon Evans <[email protected]>2022-04-28 07:50:03 +0200
commitbd56636d5802cdeb323851ec2431bb3f78f94eed (patch)
tree86ba8d730bc0c7883bf78d137c4cf6240acfabef /compileopts
parent4fe3a379a507282cea2e131879cd832cfe5a2928 (diff)
downloadtinygo-bd56636d5802cdeb323851ec2431bb3f78f94eed.tar.gz
tinygo-bd56636d5802cdeb323851ec2431bb3f78f94eed.zip
all: make emulator command a string instead of a []string
This matches the flash-command and is generally a bit easier to work with. This commit also prepares for allowing multiple formats to be used in the emulator command, which is necessary for the esp32.
Diffstat (limited to 'compileopts')
-rw-r--r--compileopts/config.go36
-rw-r--r--compileopts/target.go29
-rw-r--r--compileopts/target_test.go5
3 files changed, 40 insertions, 30 deletions
diff --git a/compileopts/config.go b/compileopts/config.go
index 9fb6a3bbc..d7dbb41c3 100644
--- a/compileopts/config.go
+++ b/compileopts/config.go
@@ -10,6 +10,7 @@ import (
"regexp"
"strings"
+ "github.com/google/shlex"
"github.com/tinygo-org/tinygo/goenv"
)
@@ -494,13 +495,38 @@ func (c *Config) WasmAbi() string {
return c.Target.WasmAbi
}
-// Emulator returns the emulator target config
-func (c *Config) Emulator() []string {
+// EmulatorName is a shorthand to get the command for this emulator, something
+// like qemu-system-arm or simavr.
+func (c *Config) EmulatorName() string {
+ parts := strings.SplitN(c.Target.Emulator, " ", 2)
+ if len(parts) > 1 {
+ return parts[0]
+ }
+ return ""
+}
+
+// EmulatorFormat returns the binary format for the emulator and the associated
+// file extension. An empty string means to pass directly whatever the linker
+// produces directly without conversion.
+func (c *Config) EmulatorFormat() (format, fileExt string) {
+ return "", ""
+}
+
+// Emulator returns a ready-to-run command to run the given binary in an
+// emulator. Give it the format (returned by EmulatorFormat()) and the path to
+// the compiled binary.
+func (c *Config) Emulator(format, binary string) ([]string, error) {
+ parts, err := shlex.Split(c.Target.Emulator)
+ if err != nil {
+ return nil, fmt.Errorf("could not parse emulator command: %w", err)
+ }
var emulator []string
- for _, s := range c.Target.Emulator {
- emulator = append(emulator, strings.ReplaceAll(s, "{root}", goenv.Get("TINYGOROOT")))
+ for _, s := range parts {
+ s = strings.ReplaceAll(s, "{root}", goenv.Get("TINYGOROOT"))
+ s = strings.ReplaceAll(s, "{"+format+"}", binary)
+ emulator = append(emulator, s)
}
- return emulator
+ return emulator, nil
}
type TestConfig struct {
diff --git a/compileopts/target.go b/compileopts/target.go
index 26f007538..6a1dbf126 100644
--- a/compileopts/target.go
+++ b/compileopts/target.go
@@ -44,8 +44,8 @@ type TargetSpec struct {
LDFlags []string `json:"ldflags"`
LinkerScript string `json:"linkerscript"`
ExtraFiles []string `json:"extra-files"`
- RP2040BootPatch *bool `json:"rp2040-boot-patch"` // Patch RP2040 2nd stage bootloader checksum
- Emulator []string `json:"emulator" override:"copy"` // inherited Emulator must not be append
+ RP2040BootPatch *bool `json:"rp2040-boot-patch"` // Patch RP2040 2nd stage bootloader checksum
+ Emulator string `json:"emulator"`
FlashCommand string `json:"flash-command"`
GDB []string `json:"gdb"`
PortReset string `json:"flash-1200-bps-reset"`
@@ -90,19 +90,8 @@ func (spec *TargetSpec) overrideProperties(child *TargetSpec) {
if !src.IsNil() {
dst.Set(src)
}
- case reflect.Slice: // for slices...
- if src.Len() > 0 { // ... if not empty ...
- switch tag := field.Tag.Get("override"); tag {
- case "copy":
- // copy the field of child to spec
- dst.Set(src)
- case "append", "":
- // or append the field of child to spec
- dst.Set(reflect.AppendSlice(dst, src))
- default:
- panic("override mode must be 'copy' or 'append' (default). I don't know how to '" + tag + "'.")
- }
- }
+ case reflect.Slice: // for slices, append the field
+ dst.Set(reflect.AppendSlice(dst, src))
default:
panic("unknown field type : " + kind.String())
}
@@ -335,20 +324,20 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) {
case "386":
// amd64 can _usually_ run 32-bit programs, so skip the emulator in that case.
if runtime.GOARCH != "amd64" {
- spec.Emulator = []string{"qemu-i386"}
+ spec.Emulator = "qemu-i386 {}"
}
case "amd64":
- spec.Emulator = []string{"qemu-x86_64"}
+ spec.Emulator = "qemu-x86_64 {}"
case "arm":
- spec.Emulator = []string{"qemu-arm"}
+ spec.Emulator = "qemu-arm {}"
case "arm64":
- spec.Emulator = []string{"qemu-aarch64"}
+ spec.Emulator = "qemu-aarch64 {}"
}
}
}
if goos != runtime.GOOS {
if goos == "windows" {
- spec.Emulator = []string{"wine"}
+ spec.Emulator = "wine {}"
}
}
return &spec, nil
diff --git a/compileopts/target_test.go b/compileopts/target_test.go
index 6aea7e817..8f7b99b2e 100644
--- a/compileopts/target_test.go
+++ b/compileopts/target_test.go
@@ -29,7 +29,6 @@ func TestOverrideProperties(t *testing.T) {
CPU: "baseCpu",
CFlags: []string{"-base-foo", "-base-bar"},
BuildTags: []string{"bt1", "bt2"},
- Emulator: []string{"be1", "be2"},
DefaultStackSize: 42,
AutoStackSize: &baseAutoStackSize,
}
@@ -38,7 +37,6 @@ func TestOverrideProperties(t *testing.T) {
GOOS: "",
CPU: "chlidCpu",
CFlags: []string{"-child-foo", "-child-bar"},
- Emulator: []string{"ce1", "ce2"},
AutoStackSize: &childAutoStackSize,
DefaultStackSize: 64,
}
@@ -57,9 +55,6 @@ func TestOverrideProperties(t *testing.T) {
if !reflect.DeepEqual(base.BuildTags, []string{"bt1", "bt2"}) {
t.Errorf("Overriding failed : got %v", base.BuildTags)
}
- if !reflect.DeepEqual(base.Emulator, []string{"ce1", "ce2"}) {
- t.Errorf("Overriding failed : got %v", base.Emulator)
- }
if *base.AutoStackSize != false {
t.Errorf("Overriding failed : got %v", base.AutoStackSize)
}