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