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