Skip to content

Commit aaa3315

Browse files
committed
Add an interface for running commands so testing is easier.
1 parent ceab73f commit aaa3315

5 files changed

Lines changed: 42 additions & 39 deletions

File tree

cmd/spicy/main.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ func main() {
9595
if err != nil {
9696
panic(err)
9797
}
98-
preprocessed, err := spicy.PreprocessSpec(f, *cpp_command, includeFlags, defineFlags, undefineFlags)
98+
gcc := spicy.NewRunner(*cpp_command)
99+
ld := spicy.NewRunner(*ld_command)
100+
as := spicy.NewRunner(*as_command)
101+
objcopy := spicy.NewRunner(*objcopy_command)
102+
preprocessed, err := spicy.PreprocessSpec(f, gcc, includeFlags, defineFlags, undefineFlags)
99103
spec, err := spicy.ParseSpec(preprocessed)
100104
if err != nil {
101105
panic(err)
@@ -105,16 +109,16 @@ func main() {
105109
if err != nil {
106110
panic(err)
107111
}
108-
entry, err := spicy.CreateEntryBinary(w, *as_command)
109-
linked_object_path, err := spicy.LinkSpec(w, *ld_command)
112+
entry, err := spicy.CreateEntryBinary(w, as)
113+
linked_object_path, err := spicy.LinkSpec(w, ld)
110114
if err != nil {
111115
panic(err)
112116
}
113117
if err != nil {
114118
panic(err)
115119
}
116120
defer entry.Close()
117-
binarized_object_file, err := spicy.BinarizeObject(linked_object_path, *objcopy_command)
121+
binarized_object_file, err := spicy.BinarizeObject(linked_object_path, objcopy)
118122
if err != nil {
119123
panic(err)
120124
}

cmds.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,37 @@ package spicy
22

33
import (
44
"bytes"
5+
"errors"
6+
"fmt"
57
log "github.com/sirupsen/logrus"
68
"io"
79
"os/exec"
810
"strings"
911
)
1012

11-
func RunCmd(command string, args ...string) error {
12-
log.Infof("About to run %s %s\n", command, strings.Join(args, " "))
13-
cmd := exec.Command(command, args...)
14-
var out bytes.Buffer
15-
var errout bytes.Buffer
16-
cmd.Stdout = &out
17-
cmd.Stderr = &errout
18-
err := cmd.Run()
19-
log.Debug(command, " stdout: ", out.String())
20-
if err != nil {
21-
log.Error("Error running ", command, ". Stderr output: ", errout.String())
22-
}
23-
return err
13+
type Runner interface {
14+
Run(r io.Reader, args []string) (io.Reader, error)
15+
}
16+
17+
type ExecRunner struct {
18+
command string
19+
}
20+
21+
func NewRunner(cmd string) ExecRunner {
22+
return ExecRunner{command: cmd}
2423
}
2524

26-
func RunCmdReturnStdout(command string, stdin io.Reader, args ...string) (io.Reader, error) {
27-
log.Infof("About to run %s %s\n", command, strings.Join(args, " "))
28-
cmd := exec.Command(command, args...)
25+
func (e ExecRunner) Run(r io.Reader, args []string) (io.Reader, error) {
26+
log.Infof("About to run %s %s\n", e.command, strings.Join(args, " "))
27+
cmd := exec.Command(e.command, args...)
2928
var out bytes.Buffer
3029
var errout bytes.Buffer
3130
cmd.Stdout = &out
3231
cmd.Stderr = &errout
33-
cmd.Stdin = stdin
3432
err := cmd.Run()
35-
log.Debug(command, " stdout: ", out.String())
33+
log.Debug("stdout: ", out.String())
3634
if err != nil {
37-
log.Error("Error running ", command, ". Stderr output: ", errout.String())
35+
return nil, errors.New(fmt.Sprintf("Error running '%s': %s", e.command, errout.String()))
3836
}
39-
return strings.NewReader(out.String()), nil
37+
return &out, nil
4038
}

entry_source.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"text/template"
1010
)
1111

12+
var compileArgs = []string{"-march=vr4300", "-mtune=vr4300", "-mgp32", "-mfp32", "-non_shared"}
13+
1214
func createEntrySource(bootSegment *Segment) (string, error) {
1315
t := `
1416
.text
@@ -59,16 +61,17 @@ func generateEntryScript(w *Wave) (string, error) {
5961
return path, nil
6062
}
6163

62-
func CreateEntryBinary(w *Wave, as_command string) (*os.File, error) {
64+
func CreateEntryBinary(w *Wave, as Runner) (*os.File, error) {
6365
name := w.Name
6466
log.Infof("Creating entry for \"%s\".", name)
65-
entry_source_path, err := generateEntryScript(w)
67+
entrySourcePath, err := generateEntryScript(w)
6668
if err != nil {
6769
return nil, err
6870
}
69-
err = RunCmd(as_command, "-march=vr4300", "-mtune=vr4300", "-mgp32", "-mfp32", "-non_shared", entry_source_path)
71+
_, err = as.Run( /* stdin=*/ nil, append(compileArgs, entrySourcePath))
7072
if err != nil {
7173
return nil, err
7274
}
75+
// TODO(trhodeos): Make this not nil.
7376
return nil, err
7477
}

ld.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"text/template"
1111
)
1212

13+
var ldArgs = []string{"-G 0", "-S", "-noinhibit-exec", "-nostartfiles", "-nodefaultlibs", "-nostdinc", "-M"}
14+
1315
func createLdScript(w *Wave) (string, error) {
1416
t := `
1517
ENTRY(_start)
@@ -115,30 +117,26 @@ func generateLdScript(w *Wave) (string, error) {
115117
return path, nil
116118
}
117119

118-
func LinkSpec(w *Wave, ld_command string) (string, error) {
120+
func LinkSpec(w *Wave, ldRunner Runner) (string, error) {
119121
name := w.Name
120122
log.Infof("Linking spec \"%s\".", name)
121123
ld_path, err := generateLdScript(w)
122124
if err != nil {
123125
return "", err
124126
}
125127
output_path := fmt.Sprintf("%s.out", name)
126-
err = RunCmd(ld_command, "-G 0", "-S", "-noinhibit-exec", "-nostartfiles", "-nodefaultlibs", "-nostdinc", "-dT", ld_path, "-o", output_path, "-M")
128+
_, err = ldRunner.Run( /* stdin=*/ nil, append(ldArgs, "-dT", ld_path, "-o", output_path))
127129
if err != nil {
128130
return "", err
129131
}
130132
return output_path, err
131133
}
132134

133-
func BinarizeObject(obj_path string, objcopy_command string) (*os.File, error) {
134-
output_bin := fmt.Sprintf("%s.bin", obj_path)
135-
err := RunCmd(objcopy_command, "-O", "binary", obj_path, output_bin)
136-
if err != nil {
137-
return nil, err
138-
}
139-
file, err := os.Open(output_bin)
135+
func BinarizeObject(objPath string, objcopyRunner Runner) (*os.File, error) {
136+
outputBin := fmt.Sprintf("%s.bin", objPath)
137+
_, err := objcopyRunner.Run( /* stdin=*/ nil, []string{"-O", "binary", objPath, outputBin})
140138
if err != nil {
141139
return nil, err
142140
}
143-
return file, err
141+
return os.Open(outputBin)
144142
}

spec.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func convertAstToSpec(s SpecAst) (*Spec, error) {
245245
return out, nil
246246
}
247247

248-
func PreprocessSpec(file io.Reader, gcc_command string, includeFlags []string, defineFlags []string, undefineFlags []string) (io.Reader, error) {
248+
func PreprocessSpec(file io.Reader, gcc Runner, includeFlags []string, defineFlags []string, undefineFlags []string) (io.Reader, error) {
249249
args := []string{"-P", "-E", "-U_LANGUAGE_C", "-D_LANGUAGE_MAKEROM", "-"}
250250
for _, include := range includeFlags {
251251
args = append(args, fmt.Sprintf("-I%s", include))
@@ -257,7 +257,7 @@ func PreprocessSpec(file io.Reader, gcc_command string, includeFlags []string, d
257257
args = append(args, fmt.Sprintf("-U%s", undefine))
258258
}
259259

260-
return RunCmdReturnStdout(gcc_command, file, args...)
260+
return gcc.Run(file, args)
261261
}
262262

263263
func ParseSpec(r io.Reader) (*Spec, error) {

0 commit comments

Comments
 (0)