]> Cypherpunks.ru repositories - goredo.git/blobdiff - run.go
default.do-s must not depend on themselves
[goredo.git] / run.go
diff --git a/run.go b/run.go
index af71b3b8b3b4c4b08d39082e295d2fa48316ed62..0178d726b9d9b476dbd01d1db45e713d0f5c80c0 100644 (file)
--- a/run.go
+++ b/run.go
@@ -25,23 +25,26 @@ import (
        "flag"
        "fmt"
        "io"
+       "log"
        "os"
        "os/exec"
        "path"
        "path/filepath"
+       "strconv"
        "strings"
        "sync"
        "syscall"
        "time"
 
        "go.cypherpunks.ru/recfile"
-       "go.cypherpunks.ru/tai64n"
+       "go.cypherpunks.ru/tai64n/v2"
        "golang.org/x/sys/unix"
 )
 
 const (
        EnvDepFd        = "REDO_DEP_FD"
        EnvDirPrefix    = "REDO_DIRPREFIX"
+       EnvDepCwd       = "REDO_DEP_CWD"
        EnvBuildUUID    = "REDO_BUILD_UUID"
        EnvStderrPrefix = "REDO_STDERR_PREFIX"
        EnvTrace        = "REDO_TRACE"
@@ -49,11 +52,12 @@ const (
        EnvStderrSilent = "REDO_SILENT"
        EnvNoSync       = "REDO_NO_SYNC"
 
-       RedoDir    = ".redo"
-       LockSuffix = ".lock"
-       DepSuffix  = ".rec"
-       TmpPrefix  = ".redo."
-       LogSuffix  = ".log"
+       RedoDir      = ".redo"
+       LockSuffix   = ".lock"
+       DepSuffix    = ".rec"
+       TmpPrefix    = ".redo."
+       LogSuffix    = ".log"
+       LogRecSuffix = ".log-rec"
 )
 
 var (
@@ -63,12 +67,31 @@ var (
        StderrPrefix string
        Jobs         sync.WaitGroup
 
-       flagTrace        = flag.Bool("x", false, "trace (sh -x) current targets")
-       flagTraceAll     = flag.Bool("xx", false, fmt.Sprintf("trace (sh -x) all targets (%s=1)", EnvTrace))
-       flagStderrKeep   = flag.Bool("logs", false, fmt.Sprintf("keep job's stderr (%s=1)", EnvStderrKeep))
-       flagStderrSilent = flag.Bool("silent", false, fmt.Sprintf("do not print job's stderr (%s=1)", EnvStderrSilent))
+       flagTrace        *bool
+       flagTraceAll     *bool
+       flagStderrKeep   *bool
+       flagStderrSilent *bool
+
+       TracedAll bool
+
+       RunningProcs  = map[int]*os.Process{}
+       RunningProcsM sync.Mutex
 )
 
+func init() {
+       cmdName := CmdName()
+       if !(cmdName == CmdNameRedo || cmdName == CmdNameRedoIfchange) {
+               return
+       }
+       flagTrace = flag.Bool("x", false, "trace (sh -x) current targets")
+       flagTraceAll = flag.Bool("xx", false,
+               fmt.Sprintf("trace (sh -x) all targets (%s=1)", EnvTrace))
+       flagStderrKeep = flag.Bool("k", false,
+               fmt.Sprintf("keep job's stderr (%s=1)", EnvStderrKeep))
+       flagStderrSilent = flag.Bool("s", false,
+               fmt.Sprintf("silent, do not print job's stderr (%s=1)", EnvStderrSilent))
+}
+
 type RunErr struct {
        Tgt      string
        DoFile   string
@@ -87,7 +110,7 @@ func (e *RunErr) Name() string {
        if e.Finished == nil {
                return name
        }
-       return fmt.Sprintf("%s (%fsec)", name, e.Finished.Sub(*e.Started).Seconds())
+       return fmt.Sprintf("%s (%.3fs)", name, e.Finished.Sub(*e.Started).Seconds())
 }
 
 func (e RunErr) Error() string {
@@ -120,7 +143,7 @@ func isModified(cwd, redoDir, tgt string) (bool, *Inode, error) {
                        }
                        return false, nil, err
                }
-               if m["Target"] != tgt {
+               if m["Type"] != DepTypeIfchange || m["Target"] != tgt {
                        continue
                }
                fd, err := os.Open(path.Join(cwd, tgt))
@@ -304,7 +327,7 @@ func runScript(tgtOrig string, errs chan error, traced bool) error {
                args = make([]string, 0, 3)
        } else {
                cmdName = "/bin/sh"
-               if traced {
+               if traced || TracedAll {
                        args = append(args, "-ex")
                } else {
                        args = append(args, "-e")
@@ -360,31 +383,18 @@ func runScript(tgtOrig string, errs chan error, traced bool) error {
                fdNum++
        }
 
-       if JSR == nil {
-               // infinite jobs
-               cmd.Env = append(cmd.Env, fmt.Sprintf("%s=NO", EnvJSFd))
-       } else {
-               cmd.ExtraFiles = append(cmd.ExtraFiles, JSR)
-               cmd.ExtraFiles = append(cmd.ExtraFiles, JSW)
-               cmd.Env = append(cmd.Env, fmt.Sprintf(
-                       "%s=%d,%d", EnvJSFd, 3+fdNum+0, 3+fdNum+1,
-               ))
-               fdNum += 2
-       }
-
        // Preparing stderr
        var fdStderr *os.File
        if StderrKeep {
                fdStderr, err = os.OpenFile(
                        path.Join(redoDir, tgt+LogSuffix),
-                       os.O_WRONLY|os.O_CREATE,
+                       os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
                        os.FileMode(0666),
                )
                if err != nil {
                        cleanup()
                        return TgtErr{tgtOrig, err}
                }
-               fdStderr.Truncate(0)
        }
        shCtx := fmt.Sprintf(
                "sh: %s: %s %s cwd:%s dirprefix:%s",
@@ -394,11 +404,28 @@ func runScript(tgtOrig string, errs chan error, traced bool) error {
 
        Jobs.Add(1)
        go func() {
-               jsAcquire(shCtx)
+               jsToken := jsAcquire(shCtx)
+               if JSR == nil {
+                       // infinite jobs
+                       cmd.Env = append(cmd.Env, fmt.Sprintf("%s=NO", EnvJobs))
+               } else {
+                       cmd.ExtraFiles = append(cmd.ExtraFiles, JSR)
+                       cmd.ExtraFiles = append(cmd.ExtraFiles, JSW)
+                       makeFlags := fmt.Sprintf(
+                               "%s %s%d,%d", MakeFlags, MakeJSArg, 3+fdNum+0, 3+fdNum+1,
+                       )
+                       makeFlags = strings.Trim(makeFlags, " ")
+                       cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", MakeFlagsName, makeFlags))
+                       fdNum += 2
+                       cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%d", EnvJSToken, jsToken))
+               }
+
                if FdStatus != nil {
                        FdStatus.Write([]byte{StatusRun})
                }
 
+               var finished time.Time
+               var exitErr *exec.ExitError
                started := time.Now()
                runErr.Started = &started
                fdStdout, err = os.OpenFile(stdoutPath, os.O_RDWR, os.FileMode(0666))
@@ -417,15 +444,89 @@ func runScript(tgtOrig string, errs chan error, traced bool) error {
                cmd.ExtraFiles = append(cmd.ExtraFiles, fdDep)
                cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%d", EnvDepFd, 3+fdNum))
                fdNum++
+               cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", EnvDepCwd, cwd))
 
                defer func() {
-                       jsRelease(shCtx)
-                       lockRelease()
+                       jsRelease(shCtx, jsToken)
                        fdDep.Close()
                        fdStdout.Close()
                        if fdStderr != nil {
                                fdStderr.Close()
+                               logRecPath := path.Join(redoDir, tgt+LogRecSuffix)
+                               if fdStderr, err = os.OpenFile(
+                                       logRecPath,
+                                       os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
+                                       os.FileMode(0666),
+                               ); err == nil {
+                                       fields := []recfile.Field{
+                                               {Name: "Build", Value: BuildUUID},
+                                               {Name: "PPID", Value: strconv.Itoa(os.Getpid())},
+                                               {Name: "Cwd", Value: cwd},
+                                       }
+                                       if cmd.Process != nil {
+                                               fields = append(fields, recfile.Field{
+                                                       Name: "PID", Value: strconv.Itoa(cmd.Process.Pid),
+                                               })
+                                       }
+                                       ts := new(tai64n.TAI64N)
+                                       ts.FromTime(started)
+                                       fields = append(fields,
+                                               recfile.Field{Name: "Started", Value: tai64n.Encode(ts[:])},
+                                       )
+                                       ts.FromTime(finished)
+                                       fields = append(fields,
+                                               recfile.Field{Name: "Finished", Value: tai64n.Encode(ts[:])})
+                                       fields = append(fields, recfile.Field{
+                                               Name:  "Duration",
+                                               Value: strconv.FormatInt(finished.Sub(started).Nanoseconds(), 10),
+                                       })
+                                       fields = append(fields, recfile.Field{Name: "Cmd", Value: cmdName})
+                                       for _, arg := range args {
+                                               fields = append(fields, recfile.Field{Name: "Arg", Value: arg})
+                                       }
+                                       for _, env := range cmd.Env {
+                                               fields = append(fields, recfile.Field{Name: "Env", Value: env})
+                                       }
+                                       if exitErr != nil {
+                                               fields = append(fields, recfile.Field{
+                                                       Name:  "ExitCode",
+                                                       Value: strconv.Itoa(exitErr.ProcessState.ExitCode()),
+                                               })
+                                       }
+                                       w := bufio.NewWriter(fdStderr)
+
+                                       var depInfo *DepInfo
+                                       fdDep, err := os.Open(fdDepPath)
+                                       if err != nil {
+                                               goto Err
+                                       }
+                                       depInfo, err = depRead(fdDep)
+                                       fdDep.Close()
+                                       if err != nil {
+                                               goto Err
+                                       }
+                                       for _, dep := range depInfo.ifchanges {
+                                               fields = append(fields, recfile.Field{
+                                                       Name:  "Ifchange",
+                                                       Value: dep["Target"],
+                                               })
+                                       }
+                                       _, err = recfile.NewWriter(w).WriteFields(fields...)
+                                       if err != nil {
+                                               goto Err
+                                       }
+                                       err = w.Flush()
+                               Err:
+                                       if err != nil {
+                                               log.Println(err)
+                                               os.Remove(logRecPath)
+                                       }
+                                       fdStderr.Close()
+                               } else {
+                                       log.Println("can not open", logRecPath, ":", err)
+                               }
                        }
+                       lockRelease()
                        os.Remove(fdDep.Name())
                        os.Remove(fdStdout.Name())
                        os.Remove(tmpPath)
@@ -448,6 +549,9 @@ func runScript(tgtOrig string, errs chan error, traced bool) error {
                        errs <- runErr
                        return
                }
+               RunningProcsM.Lock()
+               RunningProcs[cmd.Process.Pid] = cmd.Process
+               RunningProcsM.Unlock()
                pid := fmt.Sprintf("[%d]", cmd.Process.Pid)
                trace(CDebug, "%s runs %s", tgtOrig, pid)
 
@@ -464,9 +568,9 @@ func runScript(tgtOrig string, errs chan error, traced bool) error {
                                        continue
                                }
                                if fdStderr != nil {
-                                       tai64n.FromTime(time.Now(), ts)
+                                       ts.FromTime(time.Now())
                                        LogMutex.Lock()
-                                       fmt.Fprintf(fdStderr, "%s %s\n", ts.Encode(), line)
+                                       fmt.Fprintln(fdStderr, tai64n.Encode(ts[:]), line)
                                        LogMutex.Unlock()
                                }
                                if StderrSilent {
@@ -484,15 +588,42 @@ func runScript(tgtOrig string, errs chan error, traced bool) error {
                // Wait for job completion
                <-stderrTerm
                err = cmd.Wait()
-               finished := time.Now()
+               RunningProcsM.Lock()
+               delete(RunningProcs, cmd.Process.Pid)
+               RunningProcsM.Unlock()
+               finished = time.Now()
                runErr.Finished = &finished
                if err != nil {
+                       exitErr = err.(*exec.ExitError)
                        runErr.Err = err
                        errs <- runErr
                        return
                }
 
                // Was $1 touched?
+               if fd, err := os.Open(path.Join(cwdOrig, tgt)); err == nil {
+                       errTouched := errors.New("$1 was explicitly touched")
+                       if inodePrev == nil {
+                               fd.Close()
+                               runErr.Err = errTouched
+                               errs <- runErr
+                               return
+                       } else {
+                               inode, err := inodeFromFile(fd)
+                               fd.Close()
+                               if err != nil {
+                                       runErr.Err = err
+                                       errs <- runErr
+                                       return
+                               }
+                               if !inode.Equals(inodePrev) {
+                                       runErr.Err = errTouched
+                                       errs <- runErr
+                                       return
+                               }
+                       }
+               }
+
                if inodePrev != nil {
                        if fd, err := os.Open(path.Join(cwdOrig, tgt)); err == nil {
                                inode, err := inodeFromFile(fd)
@@ -576,7 +707,8 @@ func runScript(tgtOrig string, errs chan error, traced bool) error {
                                goto Finish
                        }
                }
-               err = os.Rename(fdDep.Name(), path.Join(redoDir, tgt+DepSuffix))
+               fdDepPath = path.Join(redoDir, tgt+DepSuffix)
+               err = os.Rename(fdDep.Name(), fdDepPath)
                if err != nil {
                        goto Finish
                }