This repository has been archived by the owner on May 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
make_cmd.go
122 lines (100 loc) · 3.06 KB
/
make_cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package src
import (
"io"
"log"
"os"
"path/filepath"
"sourcegraph.com/sourcegraph/makex"
"strings"
"sourcegraph.com/sourcegraph/srclib/buildstore"
"sourcegraph.com/sourcegraph/srclib/config"
"sourcegraph.com/sourcegraph/srclib/flagutil"
"sourcegraph.com/sourcegraph/srclib/plan"
)
func init() {
c, err := CLI.AddCommand("make",
"plans and executes plan",
`Generates a plan (in Makefile form, in memory) for analyzing the tree and executes the plan. `,
&makeCmd,
)
if err != nil {
log.Fatal(err)
}
setDefaultRepoURIOpt(c)
setDefaultRepoSubdirOpt(c)
}
type MakeCmd struct {
config.Options
ToolchainExecOpt `group:"execution"`
BuildCacheOpt `group:"build cache"`
Quiet bool `short:"q" long:"quiet" description:"silence all output"`
DryRun bool `short:"n" long:"dry-run" description:"print what would be done and exit"`
Dir Directory `short:"C" long:"directory" description:"change to DIR before doing anything" value-name:"DIR"`
Args struct {
Goals []string `name:"GOALS..." description:"Makefile targets to build (default: all)"`
} `positional-args:"yes"`
}
var makeCmd MakeCmd
func (c *MakeCmd) Execute(args []string) error {
if c.Dir != "" {
if err := os.Chdir(c.Dir.String()); err != nil {
return err
}
}
mf, err := CreateMakefile(c.ToolchainExecOpt, c.BuildCacheOpt)
if err != nil {
return err
}
goals := c.Args.Goals
if len(goals) == 0 {
if defaultRule := mf.DefaultRule(); defaultRule != nil {
goals = []string{defaultRule.Target()}
}
}
mkConf := &makex.Default
mk := mkConf.NewMaker(mf, goals...)
if c.Quiet {
mk.RuleOutput = func(r makex.Rule) (out io.WriteCloser, err io.WriteCloser, logger *log.Logger) {
return nopWriteCloser{}, nopWriteCloser{},
log.New(nopWriteCloser{}, "", 0)
}
}
if c.DryRun {
return mk.DryRun(os.Stdout)
}
return mk.Run()
}
// CreateMakefile creates a Makefile to build a tree. The cwd should
// be the root of the tree you want to make (due to some probably
// unnecessary assumptions that CreateMaker makes).
func CreateMakefile(execOpt ToolchainExecOpt, cacheOpt BuildCacheOpt) (*makex.Makefile, error) {
localRepo, err := OpenRepo(".")
if err != nil {
return nil, err
}
buildStore, err := buildstore.LocalRepo(localRepo.RootDir)
if err != nil {
return nil, err
}
treeConfig, err := config.ReadCached(buildStore.Commit(localRepo.CommitID))
if err != nil {
return nil, err
}
if len(treeConfig.SourceUnits) == 0 {
log.Println("No source unit files found. Did you mean to run `src config`? (This is not an error; it just means that src didn't find anything to build or analyze here.)")
}
toolchainExecOptArgs, err := flagutil.MarshalArgs(&execOpt)
if err != nil {
return nil, err
}
// TODO(sqs): buildDataDir is hardcoded.
buildDataDir := filepath.Join(buildstore.BuildDataDirName, localRepo.CommitID)
mf, err := plan.CreateMakefile(buildDataDir, buildStore, localRepo.VCSType, treeConfig, plan.Options{
ToolchainExecOpt: strings.Join(toolchainExecOptArgs, " "),
NoCache: cacheOpt.NoCacheWrite,
})
if err != nil {
return nil, err
}
return mf, nil
}