Skip to content

Commit

Permalink
Add spec preprocessing.
Browse files Browse the repository at this point in the history
  • Loading branch information
trhodeos committed Mar 18, 2018
1 parent c528ca1 commit 0d2f8be
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
8 changes: 3 additions & 5 deletions cmd/spicy/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bufio"
"flag"
"fmt"
"github.com/trhodeos/n64rom"
Expand Down Expand Up @@ -48,7 +47,7 @@ var (
// Non-standard options. Should all be optional.
ld_command = flag.String("ld_command", "mips-elf-ld", ld_command_text)
as_command = flag.String("as_command", "mips-elf-as", as_command_text)
cpp_command = flag.String("cpp_command", "mips-elf-cpp", cpp_command_text)
cpp_command = flag.String("cpp_command", "mips-elf-gcc", cpp_command_text)
objcopy_command = flag.String("objcopy_command", "mips-elf-objcopy", objcopy_command_text)
font_filename = flag.String("font_filename", "font", "Font filename")
)
Expand All @@ -71,13 +70,12 @@ Uname Is passed to cpp(1) for use during its invocation.

func main() {
flag.Parse()

f, err := os.Open(flag.Arg(0))
if err != nil {
panic(err)
}

spec, err := spicy.ParseSpec(bufio.NewReader(f))
preprocessed, err := spicy.PreprocessSpec(f, *cpp_command)
spec, err := spicy.ParseSpec(preprocessed)
if err != nil {
panic(err)
}
Expand Down
19 changes: 19 additions & 0 deletions cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"github.com/golang/glog"
"io"
"os/exec"
"strings"
)
Expand All @@ -24,3 +25,21 @@ func RunCmd(command string, args ...string) error {
}
return err
}

func RunCmdReturnStdout(command string, stdin io.Reader, args ...string) (io.Reader, error) {
fmt.Printf("About to run %s %s\n", command, strings.Join(args, " "))
cmd := exec.Command(command, args...)
var out bytes.Buffer
var errout bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &errout
cmd.Stdin = stdin
err := cmd.Run()
if glog.V(2) {
glog.V(2).Info(command, " stdout: ", out.String())
}
if err != nil {
glog.Error("Error running ", command, ". Stderr output: ", errout.String())
}
return strings.NewReader(out.String()), nil
}
2 changes: 1 addition & 1 deletion ld.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func LinkSpec(w *Wave, ld_command string) (string, error) {
return "", err
}
output_path := fmt.Sprintf("%s.out", name)
err = RunCmd(ld_command, "-G 0", "-S", "-nostartfiles", "-nodefaultlibs", "-nostdinc", "-dT", ld_path, "-o", output_path, "-M")
err = RunCmd(ld_command, "-G 0", "-S", "-noinhibit-exec", "-nostartfiles", "-nodefaultlibs", "-nostdinc", "-dT", ld_path, "-o", output_path, "-M")
if err != nil {
return "", err
}
Expand Down
4 changes: 4 additions & 0 deletions spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ func convertAstToSpec(s SpecAst) (*Spec, error) {
return out, nil
}

func PreprocessSpec(file io.Reader, gcc_command string) (io.Reader, error) {
return RunCmdReturnStdout(gcc_command, file, "-P", "-E", "-U_LANGUAGE_C", "-D_LANGUAGE_SPEC", "-")
}

func ParseSpec(r io.Reader) (*Spec, error) {
parser, err := participle.Build(&SpecAst{}, nil)
if err != nil {
Expand Down

0 comments on commit 0d2f8be

Please sign in to comment.