Skip to content

Commit

Permalink
Move to gnu-style flags, and new logging library.
Browse files Browse the repository at this point in the history
  • Loading branch information
trhodeos committed Mar 19, 2018
1 parent f286949 commit bd07990
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 37 deletions.
28 changes: 14 additions & 14 deletions cmd/spicy/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"flag"
"fmt"
flag "github.com/ogier/pflag"
"github.com/trhodeos/n64rom"
"github.com/trhodeos/spicy"
"io/ioutil"
Expand Down Expand Up @@ -45,16 +45,16 @@ var includeFlags arrayFlags
var undefineFlags arrayFlags

var (
verbose = flag.Bool("d", false, verbose_text)
link_editor_verbose = flag.Bool("m", false, verbose_link_editor_text)
disable_overlapping_section_check = flag.Bool("o", false, disable_overlapping_section_check_text)
romsize_mbits = flag.Int("s", -1, romsize_text)
filldata = flag.Int("f", 0x0, filldata_text)
bootstrap_filename = flag.String("b", "Boot", bootstrap_filename_text)
header_filename = flag.String("h", "romheader", header_filename_text)
pif_bootstrap_filename = flag.String("p", "pif2Boot", pif_bootstrap_filename_text)
rom_image_file = flag.String("r", "rom.n64", rom_image_file_text)
elf_file = flag.String("e", "rom.out", rom_image_file_text)
verbose = flag.BoolP("verbose", "d", false, verbose_text)
link_editor_verbose = flag.BoolP("verbose_linking", "m", false, verbose_link_editor_text)
disable_overlapping_section_check = flag.BoolP("disable_overlapping_section_checks", "o", false, disable_overlapping_section_check_text)
romsize_mbits = flag.IntP("romsize", "s", -1, romsize_text)
filldata = flag.IntP("filldata_byte", "f", 0x0, filldata_text)
bootstrap_filename = flag.StringP("bootstrap_file", "b", "Boot", bootstrap_filename_text)
header_filename = flag.StringP("romheader_file", "h", "romheader", header_filename_text)
pif_bootstrap_filename = flag.StringP("pif2boot_file", "p", "pif2Boot", pif_bootstrap_filename_text)
rom_image_file = flag.StringP("rom_name", "r", "rom.n64", rom_image_file_text)
elf_file = flag.StringP("rom_elf_name", "e", "rom.out", rom_image_file_text)

// Non-standard options. Should all be optional.
ld_command = flag.String("ld_command", "mips64-elf-ld", ld_command_text)
Expand All @@ -81,9 +81,9 @@ Uname Is passed to cpp(1) for use during its invocation.
*/

func main() {
flag.Var(&defineFlags, "D", defines_text)
flag.Var(&includeFlags, "I", includes_text)
flag.Var(&undefineFlags, "U", undefine_text)
flag.VarP(&defineFlags, "define", "D", defines_text)
flag.VarP(&includeFlags, "include", "I", includes_text)
flag.VarP(&undefineFlags, "undefine", "U", undefine_text)
flag.Parse()
f, err := os.Open(flag.Arg(0))
if err != nil {
Expand Down
20 changes: 9 additions & 11 deletions cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,41 @@ package spicy
import (
"bytes"
"fmt"
"github.com/golang/glog"
"github.com/sirupsen/logrus"
"io"
"os/exec"
"strings"
)

var log = logrus.New()

func RunCmd(command string, args ...string) error {
fmt.Printf("About to run %s %s\n", command, strings.Join(args, " "))
log.Infof("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
err := cmd.Run()
if glog.V(2) {
glog.V(2).Info(command, " stdout: ", out.String())
}
log.Debug(command, " stdout: ", out.String())
if err != nil {
glog.Error("Error running ", command, ". Stderr output: ", errout.String())
log.Error("Error running ", command, ". Stderr output: ", errout.String())
}
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, " "))
log.Infof("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())
}
log.Debug(command, " stdout: ", out.String())
if err != nil {
glog.Error("Error running ", command, ". Stderr output: ", errout.String())
log.Error("Error running ", command, ". Stderr output: ", errout.String())
}
return strings.NewReader(out.String()), nil
}
14 changes: 8 additions & 6 deletions entry_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package spicy

import (
"bytes"
"github.com/golang/glog"
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
"path/filepath"
"text/template"
)

var log = logrus.New()

func createEntrySource(bootSegment *Segment) (string, error) {
t := `
.text
Expand Down Expand Up @@ -37,31 +39,31 @@ _start:
}

func generateEntryScript(w *Wave) (string, error) {
glog.V(1).Infoln("Starting to generate entry script.")
log.Debug("Starting to generate entry script.")
content, err := createEntrySource(w.GetBootSegment())
if err != nil {
return "", err
}
glog.V(2).Infoln("Entry script generated:\n", content)
log.Debugf("Entry script generated:\n%s", content)
tmpfile, err := ioutil.TempFile("", "entry-script")
path, err := filepath.Abs(tmpfile.Name())
if err != nil {
return "", err
}
glog.V(1).Infoln("Writing script to", path)
log.Debug("Writing script to", path)
if _, err := tmpfile.Write([]byte(content)); err != nil {
return "", err
}
if err := tmpfile.Close(); err != nil {
return "", err
}
glog.V(1).Infoln("Script written.")
log.Debug("Script written.")
return path, nil
}

func CreateEntryBinary(w *Wave, as_command string) (*os.File, error) {
name := w.Name
glog.Infof("Creating entry for \"%s\".", name)
log.Infof("Creating entry for \"%s\".", name)
entry_source_path, err := generateEntryScript(w)
if err != nil {
return nil, err
Expand Down
14 changes: 8 additions & 6 deletions ld.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package spicy
import (
"bytes"
"fmt"
"github.com/golang/glog"
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
"path/filepath"
"text/template"
)

var log = logrus.New()

func createLdScript(w *Wave) (string, error) {
t := `
ENTRY(_start)
Expand Down Expand Up @@ -93,31 +95,31 @@ SECTIONS {
}

func generateLdScript(w *Wave) (string, error) {
glog.V(1).Infoln("Starting to generate ld script.")
log.Infoln("Starting to generate ld script.")
content, err := createLdScript(w)
if err != nil {
return "", err
}
glog.V(2).Infoln("Ld script generated:\n", content)
log.Debugln("Ld script generated:\n", content)
tmpfile, err := ioutil.TempFile("", "ld-script")
path, err := filepath.Abs(tmpfile.Name())
if err != nil {
return "", err
}
glog.V(1).Infoln("Writing script to", path)
log.Debugln("Writing script to", path)
if _, err := tmpfile.Write([]byte(content)); err != nil {
return "", err
}
if err := tmpfile.Close(); err != nil {
return "", err
}
glog.V(1).Infoln("Script written.")
log.Debugln("Script written.")
return path, nil
}

func LinkSpec(w *Wave, ld_command string) (string, error) {
name := w.Name
glog.Infof("Linking spec \"%s\".", name)
log.Infof("Linking spec \"%s\".", name)
ld_path, err := generateLdScript(w)
if err != nil {
return "", err
Expand Down

0 comments on commit bd07990

Please sign in to comment.