github.com/daragao/go-ethereum@v1.8.14-0.20180809141559-45eaef243198/build/ci.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 // +build none 18 19 /* 20 The ci command is called from Continuous Integration scripts. 21 22 Usage: go run build/ci.go <command> <command flags/arguments> 23 24 Available commands are: 25 26 install [ -arch architecture ] [ -cc compiler ] [ packages... ] -- builds packages and executables 27 test [ -coverage ] [ packages... ] -- runs the tests 28 lint -- runs certain pre-selected linters 29 archive [ -arch architecture ] [ -type zip|tar ] [ -signer key-envvar ] [ -upload dest ] -- archives build artifacts 30 importkeys -- imports signing keys from env 31 debsrc [ -signer key-id ] [ -upload dest ] -- creates a debian source package 32 nsis -- creates a Windows NSIS installer 33 aar [ -local ] [ -sign key-id ] [-deploy repo] [ -upload dest ] -- creates an Android archive 34 xcode [ -local ] [ -sign key-id ] [-deploy repo] [ -upload dest ] -- creates an iOS XCode framework 35 xgo [ -alltools ] [ options ] -- cross builds according to options 36 purge [ -store blobstore ] [ -days threshold ] -- purges old archives from the blobstore 37 38 For all commands, -n prevents execution of external programs (dry run mode). 39 40 */ 41 package main 42 43 import ( 44 "bufio" 45 "bytes" 46 "encoding/base64" 47 "flag" 48 "fmt" 49 "go/parser" 50 "go/token" 51 "io/ioutil" 52 "log" 53 "os" 54 "os/exec" 55 "path/filepath" 56 "regexp" 57 "runtime" 58 "strings" 59 "time" 60 61 "github.com/ethereum/go-ethereum/internal/build" 62 "github.com/ethereum/go-ethereum/params" 63 sv "github.com/ethereum/go-ethereum/swarm/version" 64 ) 65 66 var ( 67 // Files that end up in the geth*.zip archive. 68 gethArchiveFiles = []string{ 69 "COPYING", 70 executablePath("geth"), 71 } 72 73 // Files that end up in the geth-alltools*.zip archive. 74 allToolsArchiveFiles = []string{ 75 "COPYING", 76 executablePath("abigen"), 77 executablePath("bootnode"), 78 executablePath("evm"), 79 executablePath("geth"), 80 executablePath("puppeth"), 81 executablePath("rlpdump"), 82 executablePath("wnode"), 83 } 84 85 // Files that end up in the swarm*.zip archive. 86 swarmArchiveFiles = []string{ 87 "COPYING", 88 executablePath("swarm"), 89 } 90 91 // A debian package is created for all executables listed here. 92 debExecutables = []debExecutable{ 93 { 94 BinaryName: "abigen", 95 Description: "Source code generator to convert Ethereum contract definitions into easy to use, compile-time type-safe Go packages.", 96 }, 97 { 98 BinaryName: "bootnode", 99 Description: "Ethereum bootnode.", 100 }, 101 { 102 BinaryName: "evm", 103 Description: "Developer utility version of the EVM (Ethereum Virtual Machine) that is capable of running bytecode snippets within a configurable environment and execution mode.", 104 }, 105 { 106 BinaryName: "geth", 107 Description: "Ethereum CLI client.", 108 }, 109 { 110 BinaryName: "puppeth", 111 Description: "Ethereum private network manager.", 112 }, 113 { 114 BinaryName: "rlpdump", 115 Description: "Developer utility tool that prints RLP structures.", 116 }, 117 { 118 BinaryName: "wnode", 119 Description: "Ethereum Whisper diagnostic tool", 120 }, 121 } 122 123 // A debian package is created for all executables listed here. 124 debSwarmExecutables = []debExecutable{ 125 { 126 BinaryName: "swarm", 127 PackageName: "ethereum-swarm", 128 Description: "Ethereum Swarm daemon and tools", 129 }, 130 } 131 132 debEthereum = debPackage{ 133 Name: "ethereum", 134 Version: params.Version, 135 Executables: debExecutables, 136 } 137 138 debSwarm = debPackage{ 139 Name: "ethereum-swarm", 140 Version: sv.Version, 141 Executables: debSwarmExecutables, 142 } 143 144 // Debian meta packages to build and push to Ubuntu PPA 145 debPackages = []debPackage{ 146 debSwarm, 147 debEthereum, 148 } 149 150 // Packages to be cross-compiled by the xgo command 151 allCrossCompiledArchiveFiles = append(allToolsArchiveFiles, swarmArchiveFiles...) 152 153 // Distros for which packages are created. 154 // Note: vivid is unsupported because there is no golang-1.6 package for it. 155 // Note: wily is unsupported because it was officially deprecated on lanchpad. 156 // Note: yakkety is unsupported because it was officially deprecated on lanchpad. 157 // Note: zesty is unsupported because it was officially deprecated on lanchpad. 158 // Note: artful is unsupported because it was officially deprecated on lanchpad. 159 debDistros = []string{"trusty", "xenial", "bionic", "cosmic"} 160 ) 161 162 var GOBIN, _ = filepath.Abs(filepath.Join("build", "bin")) 163 164 func executablePath(name string) string { 165 if runtime.GOOS == "windows" { 166 name += ".exe" 167 } 168 return filepath.Join(GOBIN, name) 169 } 170 171 func main() { 172 log.SetFlags(log.Lshortfile) 173 174 if _, err := os.Stat(filepath.Join("build", "ci.go")); os.IsNotExist(err) { 175 log.Fatal("this script must be run from the root of the repository") 176 } 177 if len(os.Args) < 2 { 178 log.Fatal("need subcommand as first argument") 179 } 180 switch os.Args[1] { 181 case "install": 182 doInstall(os.Args[2:]) 183 case "test": 184 doTest(os.Args[2:]) 185 case "lint": 186 doLint(os.Args[2:]) 187 case "archive": 188 doArchive(os.Args[2:]) 189 case "debsrc": 190 doDebianSource(os.Args[2:]) 191 case "nsis": 192 doWindowsInstaller(os.Args[2:]) 193 case "aar": 194 doAndroidArchive(os.Args[2:]) 195 case "xcode": 196 doXCodeFramework(os.Args[2:]) 197 case "xgo": 198 doXgo(os.Args[2:]) 199 case "purge": 200 doPurge(os.Args[2:]) 201 default: 202 log.Fatal("unknown command ", os.Args[1]) 203 } 204 } 205 206 // Compiling 207 208 func doInstall(cmdline []string) { 209 var ( 210 arch = flag.String("arch", "", "Architecture to cross build for") 211 cc = flag.String("cc", "", "C compiler to cross build with") 212 ) 213 flag.CommandLine.Parse(cmdline) 214 env := build.Env() 215 216 // Check Go version. People regularly open issues about compilation 217 // failure with outdated Go. This should save them the trouble. 218 if !strings.Contains(runtime.Version(), "devel") { 219 // Figure out the minor version number since we can't textually compare (1.10 < 1.9) 220 var minor int 221 fmt.Sscanf(strings.TrimPrefix(runtime.Version(), "go1."), "%d", &minor) 222 223 if minor < 9 { 224 log.Println("You have Go version", runtime.Version()) 225 log.Println("go-ethereum requires at least Go version 1.9 and cannot") 226 log.Println("be compiled with an earlier version. Please upgrade your Go installation.") 227 os.Exit(1) 228 } 229 } 230 // Compile packages given as arguments, or everything if there are no arguments. 231 packages := []string{"./..."} 232 if flag.NArg() > 0 { 233 packages = flag.Args() 234 } 235 packages = build.ExpandPackagesNoVendor(packages) 236 237 if *arch == "" || *arch == runtime.GOARCH { 238 goinstall := goTool("install", buildFlags(env)...) 239 goinstall.Args = append(goinstall.Args, "-v") 240 goinstall.Args = append(goinstall.Args, packages...) 241 build.MustRun(goinstall) 242 return 243 } 244 // If we are cross compiling to ARMv5 ARMv6 or ARMv7, clean any previous builds 245 if *arch == "arm" { 246 os.RemoveAll(filepath.Join(runtime.GOROOT(), "pkg", runtime.GOOS+"_arm")) 247 for _, path := range filepath.SplitList(build.GOPATH()) { 248 os.RemoveAll(filepath.Join(path, "pkg", runtime.GOOS+"_arm")) 249 } 250 } 251 // Seems we are cross compiling, work around forbidden GOBIN 252 goinstall := goToolArch(*arch, *cc, "install", buildFlags(env)...) 253 goinstall.Args = append(goinstall.Args, "-v") 254 goinstall.Args = append(goinstall.Args, []string{"-buildmode", "archive"}...) 255 goinstall.Args = append(goinstall.Args, packages...) 256 build.MustRun(goinstall) 257 258 if cmds, err := ioutil.ReadDir("cmd"); err == nil { 259 for _, cmd := range cmds { 260 pkgs, err := parser.ParseDir(token.NewFileSet(), filepath.Join(".", "cmd", cmd.Name()), nil, parser.PackageClauseOnly) 261 if err != nil { 262 log.Fatal(err) 263 } 264 for name := range pkgs { 265 if name == "main" { 266 gobuild := goToolArch(*arch, *cc, "build", buildFlags(env)...) 267 gobuild.Args = append(gobuild.Args, "-v") 268 gobuild.Args = append(gobuild.Args, []string{"-o", executablePath(cmd.Name())}...) 269 gobuild.Args = append(gobuild.Args, "."+string(filepath.Separator)+filepath.Join("cmd", cmd.Name())) 270 build.MustRun(gobuild) 271 break 272 } 273 } 274 } 275 } 276 } 277 278 func buildFlags(env build.Environment) (flags []string) { 279 var ld []string 280 if env.Commit != "" { 281 ld = append(ld, "-X", "main.gitCommit="+env.Commit) 282 } 283 if runtime.GOOS == "darwin" { 284 ld = append(ld, "-s") 285 } 286 287 if len(ld) > 0 { 288 flags = append(flags, "-ldflags", strings.Join(ld, " ")) 289 } 290 return flags 291 } 292 293 func goTool(subcmd string, args ...string) *exec.Cmd { 294 return goToolArch(runtime.GOARCH, os.Getenv("CC"), subcmd, args...) 295 } 296 297 func goToolArch(arch string, cc string, subcmd string, args ...string) *exec.Cmd { 298 cmd := build.GoTool(subcmd, args...) 299 cmd.Env = []string{"GOPATH=" + build.GOPATH()} 300 if arch == "" || arch == runtime.GOARCH { 301 cmd.Env = append(cmd.Env, "GOBIN="+GOBIN) 302 } else { 303 cmd.Env = append(cmd.Env, "CGO_ENABLED=1") 304 cmd.Env = append(cmd.Env, "GOARCH="+arch) 305 } 306 if cc != "" { 307 cmd.Env = append(cmd.Env, "CC="+cc) 308 } 309 for _, e := range os.Environ() { 310 if strings.HasPrefix(e, "GOPATH=") || strings.HasPrefix(e, "GOBIN=") { 311 continue 312 } 313 cmd.Env = append(cmd.Env, e) 314 } 315 return cmd 316 } 317 318 // Running The Tests 319 // 320 // "tests" also includes static analysis tools such as vet. 321 322 func doTest(cmdline []string) { 323 var ( 324 coverage = flag.Bool("coverage", false, "Whether to record code coverage") 325 ) 326 flag.CommandLine.Parse(cmdline) 327 env := build.Env() 328 329 packages := []string{"./..."} 330 if len(flag.CommandLine.Args()) > 0 { 331 packages = flag.CommandLine.Args() 332 } 333 packages = build.ExpandPackagesNoVendor(packages) 334 335 // Run analysis tools before the tests. 336 build.MustRun(goTool("vet", packages...)) 337 338 // Run the actual tests. 339 gotest := goTool("test", buildFlags(env)...) 340 // Test a single package at a time. CI builders are slow 341 // and some tests run into timeouts under load. 342 gotest.Args = append(gotest.Args, "-p", "1") 343 if *coverage { 344 gotest.Args = append(gotest.Args, "-covermode=atomic", "-cover") 345 } 346 347 gotest.Args = append(gotest.Args, packages...) 348 build.MustRun(gotest) 349 } 350 351 // runs gometalinter on requested packages 352 func doLint(cmdline []string) { 353 flag.CommandLine.Parse(cmdline) 354 355 packages := []string{"./..."} 356 if len(flag.CommandLine.Args()) > 0 { 357 packages = flag.CommandLine.Args() 358 } 359 // Get metalinter and install all supported linters 360 build.MustRun(goTool("get", "gopkg.in/alecthomas/gometalinter.v2")) 361 build.MustRunCommand(filepath.Join(GOBIN, "gometalinter.v2"), "--install") 362 363 // Run fast linters batched together 364 configs := []string{ 365 "--vendor", 366 "--tests", 367 "--deadline=2m", 368 "--disable-all", 369 "--enable=goimports", 370 "--enable=varcheck", 371 "--enable=vet", 372 "--enable=gofmt", 373 "--enable=misspell", 374 "--enable=goconst", 375 "--min-occurrences=6", // for goconst 376 } 377 build.MustRunCommand(filepath.Join(GOBIN, "gometalinter.v2"), append(configs, packages...)...) 378 379 // Run slow linters one by one 380 for _, linter := range []string{"unconvert", "gosimple"} { 381 configs = []string{"--vendor", "--tests", "--deadline=10m", "--disable-all", "--enable=" + linter} 382 build.MustRunCommand(filepath.Join(GOBIN, "gometalinter.v2"), append(configs, packages...)...) 383 } 384 } 385 386 // Release Packaging 387 func doArchive(cmdline []string) { 388 var ( 389 arch = flag.String("arch", runtime.GOARCH, "Architecture cross packaging") 390 atype = flag.String("type", "zip", "Type of archive to write (zip|tar)") 391 signer = flag.String("signer", "", `Environment variable holding the signing key (e.g. LINUX_SIGNING_KEY)`) 392 upload = flag.String("upload", "", `Destination to upload the archives (usually "gethstore/builds")`) 393 ext string 394 ) 395 flag.CommandLine.Parse(cmdline) 396 switch *atype { 397 case "zip": 398 ext = ".zip" 399 case "tar": 400 ext = ".tar.gz" 401 default: 402 log.Fatal("unknown archive type: ", atype) 403 } 404 405 var ( 406 env = build.Env() 407 408 basegeth = archiveBasename(*arch, params.ArchiveVersion(env.Commit)) 409 geth = "geth-" + basegeth + ext 410 alltools = "geth-alltools-" + basegeth + ext 411 412 baseswarm = archiveBasename(*arch, sv.ArchiveVersion(env.Commit)) 413 swarm = "swarm-" + baseswarm + ext 414 ) 415 maybeSkipArchive(env) 416 if err := build.WriteArchive(geth, gethArchiveFiles); err != nil { 417 log.Fatal(err) 418 } 419 if err := build.WriteArchive(alltools, allToolsArchiveFiles); err != nil { 420 log.Fatal(err) 421 } 422 if err := build.WriteArchive(swarm, swarmArchiveFiles); err != nil { 423 log.Fatal(err) 424 } 425 for _, archive := range []string{geth, alltools, swarm} { 426 if err := archiveUpload(archive, *upload, *signer); err != nil { 427 log.Fatal(err) 428 } 429 } 430 } 431 432 func archiveBasename(arch string, archiveVersion string) string { 433 platform := runtime.GOOS + "-" + arch 434 if arch == "arm" { 435 platform += os.Getenv("GOARM") 436 } 437 if arch == "android" { 438 platform = "android-all" 439 } 440 if arch == "ios" { 441 platform = "ios-all" 442 } 443 return platform + "-" + archiveVersion 444 } 445 446 func archiveUpload(archive string, blobstore string, signer string) error { 447 // If signing was requested, generate the signature files 448 if signer != "" { 449 pgpkey, err := base64.StdEncoding.DecodeString(os.Getenv(signer)) 450 if err != nil { 451 return fmt.Errorf("invalid base64 %s", signer) 452 } 453 if err := build.PGPSignFile(archive, archive+".asc", string(pgpkey)); err != nil { 454 return err 455 } 456 } 457 // If uploading to Azure was requested, push the archive possibly with its signature 458 if blobstore != "" { 459 auth := build.AzureBlobstoreConfig{ 460 Account: strings.Split(blobstore, "/")[0], 461 Token: os.Getenv("AZURE_BLOBSTORE_TOKEN"), 462 Container: strings.SplitN(blobstore, "/", 2)[1], 463 } 464 if err := build.AzureBlobstoreUpload(archive, filepath.Base(archive), auth); err != nil { 465 return err 466 } 467 if signer != "" { 468 if err := build.AzureBlobstoreUpload(archive+".asc", filepath.Base(archive+".asc"), auth); err != nil { 469 return err 470 } 471 } 472 } 473 return nil 474 } 475 476 // skips archiving for some build configurations. 477 func maybeSkipArchive(env build.Environment) { 478 if env.IsPullRequest { 479 log.Printf("skipping because this is a PR build") 480 os.Exit(0) 481 } 482 if env.IsCronJob { 483 log.Printf("skipping because this is a cron job") 484 os.Exit(0) 485 } 486 if env.Branch != "master" && !strings.HasPrefix(env.Tag, "v1.") { 487 log.Printf("skipping because branch %q, tag %q is not on the whitelist", env.Branch, env.Tag) 488 os.Exit(0) 489 } 490 } 491 492 // Debian Packaging 493 func doDebianSource(cmdline []string) { 494 var ( 495 signer = flag.String("signer", "", `Signing key name, also used as package author`) 496 upload = flag.String("upload", "", `Where to upload the source package (usually "ppa:ethereum/ethereum")`) 497 workdir = flag.String("workdir", "", `Output directory for packages (uses temp dir if unset)`) 498 now = time.Now() 499 ) 500 flag.CommandLine.Parse(cmdline) 501 *workdir = makeWorkdir(*workdir) 502 env := build.Env() 503 maybeSkipArchive(env) 504 505 // Import the signing key. 506 if b64key := os.Getenv("PPA_SIGNING_KEY"); b64key != "" { 507 key, err := base64.StdEncoding.DecodeString(b64key) 508 if err != nil { 509 log.Fatal("invalid base64 PPA_SIGNING_KEY") 510 } 511 gpg := exec.Command("gpg", "--import") 512 gpg.Stdin = bytes.NewReader(key) 513 build.MustRun(gpg) 514 } 515 516 // Create Debian packages and upload them 517 for _, pkg := range debPackages { 518 for _, distro := range debDistros { 519 meta := newDebMetadata(distro, *signer, env, now, pkg.Name, pkg.Version, pkg.Executables) 520 pkgdir := stageDebianSource(*workdir, meta) 521 debuild := exec.Command("debuild", "-S", "-sa", "-us", "-uc") 522 debuild.Dir = pkgdir 523 build.MustRun(debuild) 524 525 changes := fmt.Sprintf("%s_%s_source.changes", meta.Name(), meta.VersionString()) 526 changes = filepath.Join(*workdir, changes) 527 if *signer != "" { 528 build.MustRunCommand("debsign", changes) 529 } 530 if *upload != "" { 531 build.MustRunCommand("dput", *upload, changes) 532 } 533 } 534 } 535 } 536 537 func makeWorkdir(wdflag string) string { 538 var err error 539 if wdflag != "" { 540 err = os.MkdirAll(wdflag, 0744) 541 } else { 542 wdflag, err = ioutil.TempDir("", "geth-build-") 543 } 544 if err != nil { 545 log.Fatal(err) 546 } 547 return wdflag 548 } 549 550 func isUnstableBuild(env build.Environment) bool { 551 if env.Tag != "" { 552 return false 553 } 554 return true 555 } 556 557 type debPackage struct { 558 Name string // the name of the Debian package to produce, e.g. "ethereum", or "ethereum-swarm" 559 Version string // the clean version of the debPackage, e.g. 1.8.12 or 0.3.0, without any metadata 560 Executables []debExecutable // executables to be included in the package 561 } 562 563 type debMetadata struct { 564 Env build.Environment 565 566 PackageName string 567 568 // go-ethereum version being built. Note that this 569 // is not the debian package version. The package version 570 // is constructed by VersionString. 571 Version string 572 573 Author string // "name <email>", also selects signing key 574 Distro, Time string 575 Executables []debExecutable 576 } 577 578 type debExecutable struct { 579 PackageName string 580 BinaryName string 581 Description string 582 } 583 584 // Package returns the name of the package if present, or 585 // fallbacks to BinaryName 586 func (d debExecutable) Package() string { 587 if d.PackageName != "" { 588 return d.PackageName 589 } 590 return d.BinaryName 591 } 592 593 func newDebMetadata(distro, author string, env build.Environment, t time.Time, name string, version string, exes []debExecutable) debMetadata { 594 if author == "" { 595 // No signing key, use default author. 596 author = "Ethereum Builds <fjl@ethereum.org>" 597 } 598 return debMetadata{ 599 PackageName: name, 600 Env: env, 601 Author: author, 602 Distro: distro, 603 Version: version, 604 Time: t.Format(time.RFC1123Z), 605 Executables: exes, 606 } 607 } 608 609 // Name returns the name of the metapackage that depends 610 // on all executable packages. 611 func (meta debMetadata) Name() string { 612 if isUnstableBuild(meta.Env) { 613 return meta.PackageName + "-unstable" 614 } 615 return meta.PackageName 616 } 617 618 // VersionString returns the debian version of the packages. 619 func (meta debMetadata) VersionString() string { 620 vsn := meta.Version 621 if meta.Env.Buildnum != "" { 622 vsn += "+build" + meta.Env.Buildnum 623 } 624 if meta.Distro != "" { 625 vsn += "+" + meta.Distro 626 } 627 return vsn 628 } 629 630 // ExeList returns the list of all executable packages. 631 func (meta debMetadata) ExeList() string { 632 names := make([]string, len(meta.Executables)) 633 for i, e := range meta.Executables { 634 names[i] = meta.ExeName(e) 635 } 636 return strings.Join(names, ", ") 637 } 638 639 // ExeName returns the package name of an executable package. 640 func (meta debMetadata) ExeName(exe debExecutable) string { 641 if isUnstableBuild(meta.Env) { 642 return exe.Package() + "-unstable" 643 } 644 return exe.Package() 645 } 646 647 // EthereumSwarmPackageName returns the name of the swarm package based on 648 // environment, e.g. "ethereum-swarm-unstable", or "ethereum-swarm". 649 // This is needed so that we make sure that "ethereum" package, 650 // depends on and installs "ethereum-swarm" 651 func (meta debMetadata) EthereumSwarmPackageName() string { 652 if isUnstableBuild(meta.Env) { 653 return debSwarm.Name + "-unstable" 654 } 655 return debSwarm.Name 656 } 657 658 // ExeConflicts returns the content of the Conflicts field 659 // for executable packages. 660 func (meta debMetadata) ExeConflicts(exe debExecutable) string { 661 if isUnstableBuild(meta.Env) { 662 // Set up the conflicts list so that the *-unstable packages 663 // cannot be installed alongside the regular version. 664 // 665 // https://www.debian.org/doc/debian-policy/ch-relationships.html 666 // is very explicit about Conflicts: and says that Breaks: should 667 // be preferred and the conflicting files should be handled via 668 // alternates. We might do this eventually but using a conflict is 669 // easier now. 670 return "ethereum, " + exe.Package() 671 } 672 return "" 673 } 674 675 func stageDebianSource(tmpdir string, meta debMetadata) (pkgdir string) { 676 pkg := meta.Name() + "-" + meta.VersionString() 677 pkgdir = filepath.Join(tmpdir, pkg) 678 if err := os.Mkdir(pkgdir, 0755); err != nil { 679 log.Fatal(err) 680 } 681 682 // Copy the source code. 683 build.MustRunCommand("git", "checkout-index", "-a", "--prefix", pkgdir+string(filepath.Separator)) 684 685 // Put the debian build files in place. 686 debian := filepath.Join(pkgdir, "debian") 687 build.Render("build/deb/"+meta.PackageName+"/deb.rules", filepath.Join(debian, "rules"), 0755, meta) 688 build.Render("build/deb/"+meta.PackageName+"/deb.changelog", filepath.Join(debian, "changelog"), 0644, meta) 689 build.Render("build/deb/"+meta.PackageName+"/deb.control", filepath.Join(debian, "control"), 0644, meta) 690 build.Render("build/deb/"+meta.PackageName+"/deb.copyright", filepath.Join(debian, "copyright"), 0644, meta) 691 build.RenderString("8\n", filepath.Join(debian, "compat"), 0644, meta) 692 build.RenderString("3.0 (native)\n", filepath.Join(debian, "source/format"), 0644, meta) 693 for _, exe := range meta.Executables { 694 install := filepath.Join(debian, meta.ExeName(exe)+".install") 695 docs := filepath.Join(debian, meta.ExeName(exe)+".docs") 696 build.Render("build/deb/"+meta.PackageName+"/deb.install", install, 0644, exe) 697 build.Render("build/deb/"+meta.PackageName+"/deb.docs", docs, 0644, exe) 698 } 699 700 return pkgdir 701 } 702 703 // Windows installer 704 func doWindowsInstaller(cmdline []string) { 705 // Parse the flags and make skip installer generation on PRs 706 var ( 707 arch = flag.String("arch", runtime.GOARCH, "Architecture for cross build packaging") 708 signer = flag.String("signer", "", `Environment variable holding the signing key (e.g. WINDOWS_SIGNING_KEY)`) 709 upload = flag.String("upload", "", `Destination to upload the archives (usually "gethstore/builds")`) 710 workdir = flag.String("workdir", "", `Output directory for packages (uses temp dir if unset)`) 711 ) 712 flag.CommandLine.Parse(cmdline) 713 *workdir = makeWorkdir(*workdir) 714 env := build.Env() 715 maybeSkipArchive(env) 716 717 // Aggregate binaries that are included in the installer 718 var ( 719 devTools []string 720 allTools []string 721 gethTool string 722 ) 723 for _, file := range allToolsArchiveFiles { 724 if file == "COPYING" { // license, copied later 725 continue 726 } 727 allTools = append(allTools, filepath.Base(file)) 728 if filepath.Base(file) == "geth.exe" { 729 gethTool = file 730 } else { 731 devTools = append(devTools, file) 732 } 733 } 734 735 // Render NSIS scripts: Installer NSIS contains two installer sections, 736 // first section contains the geth binary, second section holds the dev tools. 737 templateData := map[string]interface{}{ 738 "License": "COPYING", 739 "Geth": gethTool, 740 "DevTools": devTools, 741 } 742 build.Render("build/nsis.geth.nsi", filepath.Join(*workdir, "geth.nsi"), 0644, nil) 743 build.Render("build/nsis.install.nsh", filepath.Join(*workdir, "install.nsh"), 0644, templateData) 744 build.Render("build/nsis.uninstall.nsh", filepath.Join(*workdir, "uninstall.nsh"), 0644, allTools) 745 build.Render("build/nsis.pathupdate.nsh", filepath.Join(*workdir, "PathUpdate.nsh"), 0644, nil) 746 build.Render("build/nsis.envvarupdate.nsh", filepath.Join(*workdir, "EnvVarUpdate.nsh"), 0644, nil) 747 build.CopyFile(filepath.Join(*workdir, "SimpleFC.dll"), "build/nsis.simplefc.dll", 0755) 748 build.CopyFile(filepath.Join(*workdir, "COPYING"), "COPYING", 0755) 749 750 // Build the installer. This assumes that all the needed files have been previously 751 // built (don't mix building and packaging to keep cross compilation complexity to a 752 // minimum). 753 version := strings.Split(params.Version, ".") 754 if env.Commit != "" { 755 version[2] += "-" + env.Commit[:8] 756 } 757 installer, _ := filepath.Abs("geth-" + archiveBasename(*arch, params.ArchiveVersion(env.Commit)) + ".exe") 758 build.MustRunCommand("makensis.exe", 759 "/DOUTPUTFILE="+installer, 760 "/DMAJORVERSION="+version[0], 761 "/DMINORVERSION="+version[1], 762 "/DBUILDVERSION="+version[2], 763 "/DARCH="+*arch, 764 filepath.Join(*workdir, "geth.nsi"), 765 ) 766 767 // Sign and publish installer. 768 if err := archiveUpload(installer, *upload, *signer); err != nil { 769 log.Fatal(err) 770 } 771 } 772 773 // Android archives 774 775 func doAndroidArchive(cmdline []string) { 776 var ( 777 local = flag.Bool("local", false, `Flag whether we're only doing a local build (skip Maven artifacts)`) 778 signer = flag.String("signer", "", `Environment variable holding the signing key (e.g. ANDROID_SIGNING_KEY)`) 779 deploy = flag.String("deploy", "", `Destination to deploy the archive (usually "https://oss.sonatype.org")`) 780 upload = flag.String("upload", "", `Destination to upload the archive (usually "gethstore/builds")`) 781 ) 782 flag.CommandLine.Parse(cmdline) 783 env := build.Env() 784 785 // Sanity check that the SDK and NDK are installed and set 786 if os.Getenv("ANDROID_HOME") == "" { 787 log.Fatal("Please ensure ANDROID_HOME points to your Android SDK") 788 } 789 if os.Getenv("ANDROID_NDK") == "" { 790 log.Fatal("Please ensure ANDROID_NDK points to your Android NDK") 791 } 792 // Build the Android archive and Maven resources 793 build.MustRun(goTool("get", "golang.org/x/mobile/cmd/gomobile", "golang.org/x/mobile/cmd/gobind")) 794 build.MustRun(gomobileTool("init", "--ndk", os.Getenv("ANDROID_NDK"))) 795 build.MustRun(gomobileTool("bind", "-ldflags", "-s -w", "--target", "android", "--javapkg", "org.ethereum", "-v", "github.com/ethereum/go-ethereum/mobile")) 796 797 if *local { 798 // If we're building locally, copy bundle to build dir and skip Maven 799 os.Rename("geth.aar", filepath.Join(GOBIN, "geth.aar")) 800 return 801 } 802 meta := newMavenMetadata(env) 803 build.Render("build/mvn.pom", meta.Package+".pom", 0755, meta) 804 805 // Skip Maven deploy and Azure upload for PR builds 806 maybeSkipArchive(env) 807 808 // Sign and upload the archive to Azure 809 archive := "geth-" + archiveBasename("android", params.ArchiveVersion(env.Commit)) + ".aar" 810 os.Rename("geth.aar", archive) 811 812 if err := archiveUpload(archive, *upload, *signer); err != nil { 813 log.Fatal(err) 814 } 815 // Sign and upload all the artifacts to Maven Central 816 os.Rename(archive, meta.Package+".aar") 817 if *signer != "" && *deploy != "" { 818 // Import the signing key into the local GPG instance 819 b64key := os.Getenv(*signer) 820 key, err := base64.StdEncoding.DecodeString(b64key) 821 if err != nil { 822 log.Fatalf("invalid base64 %s", *signer) 823 } 824 gpg := exec.Command("gpg", "--import") 825 gpg.Stdin = bytes.NewReader(key) 826 build.MustRun(gpg) 827 828 keyID, err := build.PGPKeyID(string(key)) 829 if err != nil { 830 log.Fatal(err) 831 } 832 // Upload the artifacts to Sonatype and/or Maven Central 833 repo := *deploy + "/service/local/staging/deploy/maven2" 834 if meta.Develop { 835 repo = *deploy + "/content/repositories/snapshots" 836 } 837 build.MustRunCommand("mvn", "gpg:sign-and-deploy-file", "-e", "-X", 838 "-settings=build/mvn.settings", "-Durl="+repo, "-DrepositoryId=ossrh", 839 "-Dgpg.keyname="+keyID, 840 "-DpomFile="+meta.Package+".pom", "-Dfile="+meta.Package+".aar") 841 } 842 } 843 844 func gomobileTool(subcmd string, args ...string) *exec.Cmd { 845 cmd := exec.Command(filepath.Join(GOBIN, "gomobile"), subcmd) 846 cmd.Args = append(cmd.Args, args...) 847 cmd.Env = []string{ 848 "GOPATH=" + build.GOPATH(), 849 "PATH=" + GOBIN + string(os.PathListSeparator) + os.Getenv("PATH"), 850 } 851 for _, e := range os.Environ() { 852 if strings.HasPrefix(e, "GOPATH=") || strings.HasPrefix(e, "PATH=") { 853 continue 854 } 855 cmd.Env = append(cmd.Env, e) 856 } 857 return cmd 858 } 859 860 type mavenMetadata struct { 861 Version string 862 Package string 863 Develop bool 864 Contributors []mavenContributor 865 } 866 867 type mavenContributor struct { 868 Name string 869 Email string 870 } 871 872 func newMavenMetadata(env build.Environment) mavenMetadata { 873 // Collect the list of authors from the repo root 874 contribs := []mavenContributor{} 875 if authors, err := os.Open("AUTHORS"); err == nil { 876 defer authors.Close() 877 878 scanner := bufio.NewScanner(authors) 879 for scanner.Scan() { 880 // Skip any whitespace from the authors list 881 line := strings.TrimSpace(scanner.Text()) 882 if line == "" || line[0] == '#' { 883 continue 884 } 885 // Split the author and insert as a contributor 886 re := regexp.MustCompile("([^<]+) <(.+)>") 887 parts := re.FindStringSubmatch(line) 888 if len(parts) == 3 { 889 contribs = append(contribs, mavenContributor{Name: parts[1], Email: parts[2]}) 890 } 891 } 892 } 893 // Render the version and package strings 894 version := params.Version 895 if isUnstableBuild(env) { 896 version += "-SNAPSHOT" 897 } 898 return mavenMetadata{ 899 Version: version, 900 Package: "geth-" + version, 901 Develop: isUnstableBuild(env), 902 Contributors: contribs, 903 } 904 } 905 906 // XCode frameworks 907 908 func doXCodeFramework(cmdline []string) { 909 var ( 910 local = flag.Bool("local", false, `Flag whether we're only doing a local build (skip Maven artifacts)`) 911 signer = flag.String("signer", "", `Environment variable holding the signing key (e.g. IOS_SIGNING_KEY)`) 912 deploy = flag.String("deploy", "", `Destination to deploy the archive (usually "trunk")`) 913 upload = flag.String("upload", "", `Destination to upload the archives (usually "gethstore/builds")`) 914 ) 915 flag.CommandLine.Parse(cmdline) 916 env := build.Env() 917 918 // Build the iOS XCode framework 919 build.MustRun(goTool("get", "golang.org/x/mobile/cmd/gomobile", "golang.org/x/mobile/cmd/gobind")) 920 build.MustRun(gomobileTool("init")) 921 bind := gomobileTool("bind", "-ldflags", "-s -w", "--target", "ios", "--tags", "ios", "-v", "github.com/ethereum/go-ethereum/mobile") 922 923 if *local { 924 // If we're building locally, use the build folder and stop afterwards 925 bind.Dir, _ = filepath.Abs(GOBIN) 926 build.MustRun(bind) 927 return 928 } 929 archive := "geth-" + archiveBasename("ios", params.ArchiveVersion(env.Commit)) 930 if err := os.Mkdir(archive, os.ModePerm); err != nil { 931 log.Fatal(err) 932 } 933 bind.Dir, _ = filepath.Abs(archive) 934 build.MustRun(bind) 935 build.MustRunCommand("tar", "-zcvf", archive+".tar.gz", archive) 936 937 // Skip CocoaPods deploy and Azure upload for PR builds 938 maybeSkipArchive(env) 939 940 // Sign and upload the framework to Azure 941 if err := archiveUpload(archive+".tar.gz", *upload, *signer); err != nil { 942 log.Fatal(err) 943 } 944 // Prepare and upload a PodSpec to CocoaPods 945 if *deploy != "" { 946 meta := newPodMetadata(env, archive) 947 build.Render("build/pod.podspec", "Geth.podspec", 0755, meta) 948 build.MustRunCommand("pod", *deploy, "push", "Geth.podspec", "--allow-warnings", "--verbose") 949 } 950 } 951 952 type podMetadata struct { 953 Version string 954 Commit string 955 Archive string 956 Contributors []podContributor 957 } 958 959 type podContributor struct { 960 Name string 961 Email string 962 } 963 964 func newPodMetadata(env build.Environment, archive string) podMetadata { 965 // Collect the list of authors from the repo root 966 contribs := []podContributor{} 967 if authors, err := os.Open("AUTHORS"); err == nil { 968 defer authors.Close() 969 970 scanner := bufio.NewScanner(authors) 971 for scanner.Scan() { 972 // Skip any whitespace from the authors list 973 line := strings.TrimSpace(scanner.Text()) 974 if line == "" || line[0] == '#' { 975 continue 976 } 977 // Split the author and insert as a contributor 978 re := regexp.MustCompile("([^<]+) <(.+)>") 979 parts := re.FindStringSubmatch(line) 980 if len(parts) == 3 { 981 contribs = append(contribs, podContributor{Name: parts[1], Email: parts[2]}) 982 } 983 } 984 } 985 version := params.Version 986 if isUnstableBuild(env) { 987 version += "-unstable." + env.Buildnum 988 } 989 return podMetadata{ 990 Archive: archive, 991 Version: version, 992 Commit: env.Commit, 993 Contributors: contribs, 994 } 995 } 996 997 // Cross compilation 998 999 func doXgo(cmdline []string) { 1000 var ( 1001 alltools = flag.Bool("alltools", false, `Flag whether we're building all known tools, or only on in particular`) 1002 ) 1003 flag.CommandLine.Parse(cmdline) 1004 env := build.Env() 1005 1006 // Make sure xgo is available for cross compilation 1007 gogetxgo := goTool("get", "github.com/karalabe/xgo") 1008 build.MustRun(gogetxgo) 1009 1010 // If all tools building is requested, build everything the builder wants 1011 args := append(buildFlags(env), flag.Args()...) 1012 1013 if *alltools { 1014 args = append(args, []string{"--dest", GOBIN}...) 1015 for _, res := range allCrossCompiledArchiveFiles { 1016 if strings.HasPrefix(res, GOBIN) { 1017 // Binary tool found, cross build it explicitly 1018 args = append(args, "./"+filepath.Join("cmd", filepath.Base(res))) 1019 xgo := xgoTool(args) 1020 build.MustRun(xgo) 1021 args = args[:len(args)-1] 1022 } 1023 } 1024 return 1025 } 1026 // Otherwise xxecute the explicit cross compilation 1027 path := args[len(args)-1] 1028 args = append(args[:len(args)-1], []string{"--dest", GOBIN, path}...) 1029 1030 xgo := xgoTool(args) 1031 build.MustRun(xgo) 1032 } 1033 1034 func xgoTool(args []string) *exec.Cmd { 1035 cmd := exec.Command(filepath.Join(GOBIN, "xgo"), args...) 1036 cmd.Env = []string{ 1037 "GOPATH=" + build.GOPATH(), 1038 "GOBIN=" + GOBIN, 1039 } 1040 for _, e := range os.Environ() { 1041 if strings.HasPrefix(e, "GOPATH=") || strings.HasPrefix(e, "GOBIN=") { 1042 continue 1043 } 1044 cmd.Env = append(cmd.Env, e) 1045 } 1046 return cmd 1047 } 1048 1049 // Binary distribution cleanups 1050 1051 func doPurge(cmdline []string) { 1052 var ( 1053 store = flag.String("store", "", `Destination from where to purge archives (usually "gethstore/builds")`) 1054 limit = flag.Int("days", 30, `Age threshold above which to delete unstalbe archives`) 1055 ) 1056 flag.CommandLine.Parse(cmdline) 1057 1058 if env := build.Env(); !env.IsCronJob { 1059 log.Printf("skipping because not a cron job") 1060 os.Exit(0) 1061 } 1062 // Create the azure authentication and list the current archives 1063 auth := build.AzureBlobstoreConfig{ 1064 Account: strings.Split(*store, "/")[0], 1065 Token: os.Getenv("AZURE_BLOBSTORE_TOKEN"), 1066 Container: strings.SplitN(*store, "/", 2)[1], 1067 } 1068 blobs, err := build.AzureBlobstoreList(auth) 1069 if err != nil { 1070 log.Fatal(err) 1071 } 1072 // Iterate over the blobs, collect and sort all unstable builds 1073 for i := 0; i < len(blobs); i++ { 1074 if !strings.Contains(blobs[i].Name, "unstable") { 1075 blobs = append(blobs[:i], blobs[i+1:]...) 1076 i-- 1077 } 1078 } 1079 for i := 0; i < len(blobs); i++ { 1080 for j := i + 1; j < len(blobs); j++ { 1081 if blobs[i].Properties.LastModified.After(blobs[j].Properties.LastModified) { 1082 blobs[i], blobs[j] = blobs[j], blobs[i] 1083 } 1084 } 1085 } 1086 // Filter out all archives more recent that the given threshold 1087 for i, blob := range blobs { 1088 if time.Since(blob.Properties.LastModified) < time.Duration(*limit)*24*time.Hour { 1089 blobs = blobs[:i] 1090 break 1091 } 1092 } 1093 // Delete all marked as such and return 1094 if err := build.AzureBlobstoreDelete(auth, blobs); err != nil { 1095 log.Fatal(err) 1096 } 1097 }