mk/recipe.go

209 lines
3.9 KiB
Go
Raw Normal View History

2013-03-03 17:50:00 -08:00
// Various function for dealing with recipes.
2013-02-25 23:52:08 -08:00
package main
import (
2013-03-03 17:51:00 -08:00
"bufio"
"fmt"
2013-02-26 11:33:07 -08:00
"io"
"log"
"os"
"os/exec"
2013-03-03 17:51:00 -08:00
"strings"
2013-03-03 18:57:14 -08:00
"unicode/utf8"
2013-02-25 23:52:08 -08:00
)
2013-03-03 17:50:00 -08:00
// Try to unindent a recipe, so that it begins an column 0. (This is mainly for
// recipes in python, or other indentation-significant languages.)
2013-03-03 18:57:14 -08:00
func stripIndentation(s string, mincol int) string {
// trim leading whitespace
reader := bufio.NewReader(strings.NewReader(s))
output := ""
for {
line, err := reader.ReadString('\n')
col := 0
i := 0
for i < len(line) && col < mincol {
c, w := utf8.DecodeRuneInString(line[i:])
if strings.IndexRune(" \t\n", c) >= 0 {
col += 1
i += w
} else {
break
}
}
output += line[i:]
if err != nil {
break
}
}
2013-03-03 17:50:00 -08:00
2013-03-03 18:57:14 -08:00
return output
}
2013-03-03 17:50:00 -08:00
// Indent each line of a recipe.
2013-03-03 18:57:14 -08:00
func printIndented(out io.Writer, s string, ind int) {
indentation := strings.Repeat(" ", ind)
2013-03-03 17:51:00 -08:00
reader := bufio.NewReader(strings.NewReader(s))
firstline := true
2013-03-03 17:51:00 -08:00
for {
line, err := reader.ReadString('\n')
if len(line) > 0 {
if !firstline {
io.WriteString(out, indentation)
}
2013-03-03 17:51:00 -08:00
io.WriteString(out, line)
}
if err != nil {
break
}
firstline = false
2013-03-03 17:51:00 -08:00
}
2013-03-03 17:50:00 -08:00
}
// Execute a recipe.
func dorecipe(target string, u *node, e *edge, dryrun bool) bool {
2013-03-03 17:51:00 -08:00
vars := make(map[string][]string)
vars["target"] = []string{target}
if e.r.ismeta {
if e.r.attributes.regex {
for i := range e.matches {
vars[fmt.Sprintf("stem%d", i)] = e.matches[i : i+1]
}
} else {
vars["stem"] = []string{e.stem}
}
}
// TODO: other variables to set
// alltargets
// newprereq
prereqs := make([]string, 0)
for i := range u.prereqs {
if u.prereqs[i].r == e.r && u.prereqs[i].v != nil {
prereqs = append(prereqs, u.prereqs[i].v.name)
}
}
2013-03-09 20:54:13 -08:00
vars["prereq"] = prereqs
2013-03-03 17:51:00 -08:00
input := expandRecipeSigils(e.r.recipe, vars)
sh := "sh"
args := []string{}
if len(e.r.shell) > 0 {
sh = e.r.shell[0]
args = e.r.shell[1:]
}
mkPrintRecipe(target, input, e.r.attributes.quiet)
2013-03-03 17:50:00 -08:00
2013-03-03 17:51:00 -08:00
if dryrun {
return true
}
_, success := subprocess(
sh,
args,
input,
false)
return success
}
2013-03-03 17:50:00 -08:00
// Execute a subprocess (typically a recipe).
//
// Args:
// program: Program path or name located in PATH
// input: String piped into the program's stdin
// capture_out: If true, capture and return the program's stdout rather than echoing it.
//
// Returns
// (output, success)
// output is an empty string of catputer_out is false, or the collected output from the profram is true.
//
// success is true if the exit code was 0 and false otherwise
//
2013-03-03 17:50:00 -08:00
func subprocess(program string,
2013-02-26 11:33:07 -08:00
args []string,
input string,
2013-03-03 17:50:00 -08:00
capture_out bool) (string, bool) {
program_path, err := exec.LookPath(program)
if err != nil {
log.Fatal(err)
2013-02-26 11:33:07 -08:00
}
proc_args := []string{program}
proc_args = append(proc_args, args...)
2013-02-26 11:33:07 -08:00
stdin_pipe_read, stdin_pipe_write, err := os.Pipe()
if err != nil {
log.Fatal(err)
2013-02-26 11:33:07 -08:00
}
attr := os.ProcAttr{Files: []*os.File{stdin_pipe_read, os.Stdout, os.Stderr}}
output := make([]byte, 0)
capture_done := make(chan bool)
2013-02-26 11:33:07 -08:00
if capture_out {
stdout_pipe_read, stdout_pipe_write, err := os.Pipe()
if err != nil {
log.Fatal(err)
2013-02-26 22:41:25 -08:00
}
attr.Files[1] = stdout_pipe_write
go func() {
buf := make([]byte, 1024)
for {
n, err := stdout_pipe_read.Read(buf)
2013-08-13 13:03:21 -07:00
if err == io.EOF && n == 0 {
break
2013-08-13 13:03:21 -07:00
} else if err != nil {
log.Fatal(err)
}
output = append(output, buf[:n]...)
}
capture_done <- true
}()
2013-02-26 11:33:07 -08:00
}
proc, err := os.StartProcess(program_path, proc_args, &attr)
2013-02-26 11:33:07 -08:00
if err != nil {
log.Fatal(err)
}
go func() {
_, err := stdin_pipe_write.WriteString(input)
if err != nil {
2013-03-03 17:51:00 -08:00
log.Fatal(err)
}
err = stdin_pipe_write.Close()
if err != nil {
log.Fatal(err)
}
}()
state, err := proc.Wait()
2013-08-13 13:03:21 -07:00
if attr.Files[1] != os.Stdout {
attr.Files[1].Close()
}
if err != nil {
log.Fatal(err)
}
// wait until stdout copying in finished
if capture_out {
<-capture_done
2013-02-26 11:33:07 -08:00
}
return string(output), state.Success()
2013-02-25 23:52:08 -08:00
}