Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 1 | package impl |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "go/build" |
| 6 | "io/ioutil" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "runtime" |
| 10 | "strings" |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 11 | "time" |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 12 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 13 | "veyron.io/veyron/veyron/lib/cmdline" |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 14 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 15 | "veyron.io/veyron/veyron2/context" |
| 16 | "veyron.io/veyron/veyron2/rt" |
| 17 | vbuild "veyron.io/veyron/veyron2/services/mgmt/build" |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 18 | ) |
| 19 | |
| 20 | var ( |
| 21 | flagArch string |
| 22 | flagOS string |
| 23 | ) |
| 24 | |
| 25 | func init() { |
| 26 | cmdBuild.Flags.StringVar(&flagArch, "arch", runtime.GOARCH, "Target architecture.") |
| 27 | cmdBuild.Flags.StringVar(&flagOS, "os", runtime.GOOS, "Target operating system.") |
| 28 | } |
| 29 | |
| 30 | var cmdRoot = &cmdline.Command{ |
Todd Wang | fcb72a5 | 2014-10-01 09:53:56 -0700 | [diff] [blame^] | 31 | Name: "build", |
| 32 | Short: "Tool for interacting with the veyron build server", |
| 33 | Long: ` |
| 34 | The build tool tool facilitates interaction with the veyron build server. |
| 35 | `, |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 36 | Children: []*cmdline.Command{cmdBuild}, |
| 37 | } |
| 38 | |
| 39 | // Root returns a command that represents the root of the veyron tool. |
| 40 | func Root() *cmdline.Command { |
| 41 | return cmdRoot |
| 42 | } |
| 43 | |
| 44 | var cmdBuild = &cmdline.Command{ |
| 45 | Run: runBuild, |
| 46 | Name: "build", |
| 47 | Short: "Build veyron Go packages", |
| 48 | Long: ` |
| 49 | Build veyron Go packages using a remote build server. The command |
| 50 | collects all source code files that are not part of the Go standard |
| 51 | library that the target packages depend on, sends them to a build |
| 52 | server, and receives the built binaries. |
| 53 | `, |
| 54 | ArgsName: "<name> <packages>", |
| 55 | ArgsLong: ` |
| 56 | <name> is a veyron object name of a build server |
| 57 | <packages> is a list of packages to build, specified as arguments for |
| 58 | each command. The format is similar to the go tool. In its simplest |
| 59 | form each package is an import path; e.g. "veyron/tools/build". A |
| 60 | package that ends with "..." does a wildcard match against all |
| 61 | packages with that prefix. |
| 62 | `, |
| 63 | } |
| 64 | |
| 65 | // TODO(jsimsa): Add support for importing (and remotely building) |
| 66 | // packages from multiple package source root GOPATH directories with |
| 67 | // identical names. |
| 68 | func importPackages(paths []string, pkgMap map[string]*build.Package) error { |
| 69 | for _, path := range paths { |
| 70 | recurse := false |
| 71 | if strings.HasSuffix(path, "...") { |
| 72 | recurse = true |
| 73 | path = strings.TrimSuffix(path, "...") |
| 74 | } |
| 75 | if _, exists := pkgMap[path]; !exists { |
| 76 | srcDir, mode := "", build.ImportMode(0) |
| 77 | pkg, err := build.Import(path, srcDir, mode) |
| 78 | if err != nil { |
Asim Shankar | 553bfb8 | 2014-08-16 10:09:03 -0700 | [diff] [blame] | 79 | // "C" is a pseudo-package for cgo: http://golang.org/cmd/cgo/ |
| 80 | // Do not attempt recursive imports. |
| 81 | if pkg.ImportPath == "C" { |
| 82 | continue |
| 83 | } |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 84 | return fmt.Errorf("Import(%q,%q,%v) failed: %v", path, srcDir, mode, err) |
| 85 | } |
| 86 | if pkg.Goroot { |
| 87 | continue |
| 88 | } |
| 89 | pkgMap[path] = pkg |
| 90 | if err := importPackages(pkg.Imports, pkgMap); err != nil { |
| 91 | return err |
| 92 | } |
| 93 | } |
| 94 | if recurse { |
| 95 | pkg := pkgMap[path] |
| 96 | fis, err := ioutil.ReadDir(pkg.Dir) |
| 97 | if err != nil { |
| 98 | return fmt.Errorf("ReadDir(%v) failed: %v", pkg.Dir) |
| 99 | } |
| 100 | for _, fi := range fis { |
| 101 | if fi.IsDir() { |
| 102 | subPath := filepath.Join(path, fi.Name(), "...") |
| 103 | if err := importPackages([]string{subPath}, pkgMap); err != nil { |
| 104 | return err |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | return nil |
| 111 | } |
| 112 | |
| 113 | func getSources(pkgMap map[string]*build.Package, cancel <-chan struct{}, errchan chan<- error) <-chan vbuild.File { |
| 114 | sources := make(chan vbuild.File) |
| 115 | go func() { |
| 116 | defer close(sources) |
| 117 | for _, pkg := range pkgMap { |
| 118 | for _, files := range [][]string{pkg.CFiles, pkg.CgoFiles, pkg.GoFiles, pkg.SFiles} { |
| 119 | for _, file := range files { |
| 120 | path := filepath.Join(pkg.Dir, file) |
| 121 | bytes, err := ioutil.ReadFile(path) |
| 122 | if err != nil { |
| 123 | errchan <- fmt.Errorf("ReadFile(%v) failed: %v", path, err) |
| 124 | return |
| 125 | } |
| 126 | select { |
| 127 | case sources <- vbuild.File{Contents: bytes, Name: filepath.Join(pkg.ImportPath, file)}: |
| 128 | case <-cancel: |
| 129 | errchan <- nil |
| 130 | return |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | errchan <- nil |
| 136 | }() |
| 137 | return sources |
| 138 | } |
| 139 | |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 140 | func invokeBuild(ctx context.T, name string, sources <-chan vbuild.File, cancel <-chan struct{}, errchan chan<- error) <-chan vbuild.File { |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 141 | binaries := make(chan vbuild.File) |
| 142 | go func() { |
| 143 | defer close(binaries) |
| 144 | rt.Init() |
| 145 | client, err := vbuild.BindBuilder(name) |
| 146 | if err != nil { |
| 147 | errchan <- fmt.Errorf("BindBuilder(%v) failed: %v", name, err) |
| 148 | return |
| 149 | } |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 150 | stream, err := client.Build(ctx, vbuild.Architecture(flagArch), vbuild.OperatingSystem(flagOS)) |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 151 | if err != nil { |
| 152 | errchan <- fmt.Errorf("Build() failed: %v", err) |
| 153 | return |
| 154 | } |
| 155 | sender := stream.SendStream() |
| 156 | for source := range sources { |
| 157 | if err := sender.Send(source); err != nil { |
| 158 | stream.Cancel() |
| 159 | errchan <- fmt.Errorf("Send() failed: %v", err) |
| 160 | return |
| 161 | } |
| 162 | } |
| 163 | if err := sender.Close(); err != nil { |
| 164 | errchan <- fmt.Errorf("Close() failed: %v", err) |
| 165 | return |
| 166 | } |
| 167 | iterator := stream.RecvStream() |
| 168 | for iterator.Advance() { |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 169 | // TODO(mattr): This custom cancellation can probably be folded into the |
| 170 | // cancellation mechanism provided by the context. |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 171 | select { |
| 172 | case binaries <- iterator.Value(): |
| 173 | case <-cancel: |
| 174 | errchan <- nil |
| 175 | return |
| 176 | } |
| 177 | } |
| 178 | if err := iterator.Err(); err != nil { |
| 179 | errchan <- fmt.Errorf("Advance() failed: %v", err) |
| 180 | return |
| 181 | } |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 182 | if out, err := stream.Finish(); err != nil { |
Jiri Simsa | 701cdc6 | 2014-08-21 16:52:10 -0700 | [diff] [blame] | 183 | errchan <- fmt.Errorf("Finish() failed: (%v, %v)", string(out), err) |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 184 | return |
| 185 | } |
| 186 | errchan <- nil |
| 187 | }() |
| 188 | return binaries |
| 189 | } |
| 190 | |
| 191 | func saveBinaries(prefix string, binaries <-chan vbuild.File, cancel chan<- struct{}, errchan chan<- error) { |
| 192 | go func() { |
| 193 | for binary := range binaries { |
| 194 | path, perm := filepath.Join(prefix, filepath.Base(binary.Name)), os.FileMode(0755) |
| 195 | if err := ioutil.WriteFile(path, binary.Contents, perm); err != nil { |
| 196 | errchan <- fmt.Errorf("WriteFile(%v, %v) failed: %v", path, perm, err) |
| 197 | return |
| 198 | } |
| 199 | fmt.Printf("Generated binary %v\n", path) |
| 200 | } |
| 201 | errchan <- nil |
| 202 | }() |
| 203 | } |
| 204 | |
| 205 | // runBuild identifies the source files needed to build the packages |
| 206 | // specified on command-line and then creates a pipeline that |
| 207 | // concurrently 1) reads the source files, 2) sends them to the build |
| 208 | // server and receives binaries from the build server, and 3) writes |
| 209 | // the binaries out to the disk. |
| 210 | func runBuild(command *cmdline.Command, args []string) error { |
| 211 | name, paths := args[0], args[1:] |
| 212 | pkgMap := map[string]*build.Package{} |
| 213 | if err := importPackages(paths, pkgMap); err != nil { |
| 214 | return err |
| 215 | } |
| 216 | cancel, errchan := make(chan struct{}), make(chan error) |
| 217 | defer close(errchan) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 218 | |
| 219 | ctx, ctxCancel := rt.R().NewContext().WithTimeout(time.Minute) |
| 220 | defer ctxCancel() |
| 221 | |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 222 | // Start all stages of the pipeline. |
| 223 | sources := getSources(pkgMap, cancel, errchan) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 224 | binaries := invokeBuild(ctx, name, sources, cancel, errchan) |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 225 | saveBinaries(os.TempDir(), binaries, cancel, errchan) |
| 226 | // Wait for all stages of the pipeline to terminate. |
| 227 | cancelled, errors, numStages := false, []error{}, 3 |
| 228 | for i := 0; i < numStages; i++ { |
| 229 | if err := <-errchan; err != nil { |
| 230 | errors = append(errors, err) |
| 231 | if !cancelled { |
| 232 | close(cancel) |
| 233 | cancelled = true |
| 234 | } |
| 235 | } |
| 236 | } |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 237 | if len(errors) != 0 { |
Asim Shankar | ff5b635 | 2014-08-07 14:52:03 -0700 | [diff] [blame] | 238 | return fmt.Errorf("build failed(%v)", errors) |
Jiri Simsa | 9d22b7d | 2014-08-05 14:10:22 -0700 | [diff] [blame] | 239 | } |
| 240 | return nil |
| 241 | } |