github.com/vantum/vantum@v0.0.0-20180815184342-fe37d5f7a990/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/vantum/vantum/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.7)
   186  		var minor int
   187  		fmt.Sscanf(strings.TrimPrefix(runtime.Version(), "go1."), "%d", &minor)
   188  
   189  		if minor < 7 {
   190  			log.Println("You have Go version", runtime.Version())
   191  			log.Println("go-ethereum requires at least Go version 1.7 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  	if subcmd == "build" || subcmd == "install" || subcmd == "test" {
   266  		// Go CGO has a Windows linker error prior to 1.8 (https://github.com/golang/go/issues/8756).
   267  		// Work around issue by allowing multiple definitions for <1.8 builds.
   268  		var minor int
   269  		fmt.Sscanf(strings.TrimPrefix(runtime.Version(), "go1."), "%d", &minor)
   270  
   271  		if runtime.GOOS == "windows" && minor < 8 {
   272  			cmd.Args = append(cmd.Args, []string{"-ldflags", "-extldflags -Wl,--allow-multiple-definition"}...)
   273  		}
   274  	}
   275  	cmd.Env = []string{"GOPATH=" + build.GOPATH()}
   276  	if arch == "" || arch == runtime.GOARCH {
   277  		cmd.Env = append(cmd.Env, "GOBIN="+GOBIN)
   278  	} else {
   279  		cmd.Env = append(cmd.Env, "CGO_ENABLED=1")
   280  		cmd.Env = append(cmd.Env, "GOARCH="+arch)
   281  	}
   282  	if cc != "" {
   283  		cmd.Env = append(cmd.Env, "CC="+cc)
   284  	}
   285  	for _, e := range os.Environ() {
   286  		if strings.HasPrefix(e, "GOPATH=") || strings.HasPrefix(e, "GOBIN=") {
   287  			continue
   288  		}
   289  		cmd.Env = append(cmd.Env, e)
   290  	}
   291  	return cmd
   292  }
   293  
   294  // Running The Tests
   295  //
   296  // "tests" also includes static analysis tools such as vet.
   297  
   298  func doTest(cmdline []string) {
   299  	var (
   300  		coverage = flag.Bool("coverage", false, "Whether to record code coverage")
   301  	)
   302  	flag.CommandLine.Parse(cmdline)
   303  	env := build.Env()
   304  
   305  	packages := []string{"./..."}
   306  	if len(flag.CommandLine.Args()) > 0 {
   307  		packages = flag.CommandLine.Args()
   308  	}
   309  	packages = build.ExpandPackagesNoVendor(packages)
   310  
   311  	// Run analysis tools before the tests.
   312  	build.MustRun(goTool("vet", packages...))
   313  
   314  	// Run the actual tests.
   315  	gotest := goTool("test", buildFlags(env)...)
   316  	// Test a single package at a time. CI builders are slow
   317  	// and some tests run into timeouts under load.
   318  	gotest.Args = append(gotest.Args, "-p", "1")
   319  	if *coverage {
   320  		gotest.Args = append(gotest.Args, "-covermode=atomic", "-cover")
   321  	}
   322  
   323  	gotest.Args = append(gotest.Args, packages...)
   324  	build.MustRun(gotest)
   325  }
   326  
   327  // runs gometalinter on requested packages
   328  func doLint(cmdline []string) {
   329  	flag.CommandLine.Parse(cmdline)
   330  
   331  	packages := []string{"./..."}
   332  	if len(flag.CommandLine.Args()) > 0 {
   333  		packages = flag.CommandLine.Args()
   334  	}
   335  	// Get metalinter and install all supported linters
   336  	build.MustRun(goTool("get", "gopkg.in/alecthomas/gometalinter.v2"))
   337  	build.MustRunCommand(filepath.Join(GOBIN, "gometalinter.v2"), "--install")
   338  
   339  	// Run fast linters batched together
   340  	configs := []string{
   341  		"--vendor",
   342  		"--disable-all",
   343  		"--enable=vet",
   344  		"--enable=gofmt",
   345  		"--enable=misspell",
   346  		"--enable=goconst",
   347  		"--min-occurrences=6", // for goconst
   348  	}
   349  	build.MustRunCommand(filepath.Join(GOBIN, "gometalinter.v2"), append(configs, packages...)...)
   350  
   351  	// Run slow linters one by one
   352  	for _, linter := range []string{"unconvert", "gosimple"} {
   353  		configs = []string{"--vendor", "--deadline=10m", "--disable-all", "--enable=" + linter}
   354  		build.MustRunCommand(filepath.Join(GOBIN, "gometalinter.v2"), append(configs, packages...)...)
   355  	}
   356  }
   357  
   358  // Release Packaging
   359  
   360  func doArchive(cmdline []string) {
   361  	var (
   362  		arch   = flag.String("arch", runtime.GOARCH, "Architecture cross packaging")
   363  		atype  = flag.String("type", "zip", "Type of archive to write (zip|tar)")
   364  		signer = flag.String("signer", "", `Environment variable holding the signing key (e.g. LINUX_SIGNING_KEY)`)
   365  		upload = flag.String("upload", "", `Destination to upload the archives (usually "gethstore/builds")`)
   366  		ext    string
   367  	)
   368  	flag.CommandLine.Parse(cmdline)
   369  	switch *atype {
   370  	case "zip":
   371  		ext = ".zip"
   372  	case "tar":
   373  		ext = ".tar.gz"
   374  	default:
   375  		log.Fatal("unknown archive type: ", atype)
   376  	}
   377  
   378  	var (
   379  		env      = build.Env()
   380  		base     = archiveBasename(*arch, env)
   381  		geth     = "geth-" + base + ext
   382  		alltools = "geth-alltools-" + base + ext
   383  	)
   384  	maybeSkipArchive(env)
   385  	if err := build.WriteArchive(geth, gethArchiveFiles); err != nil {
   386  		log.Fatal(err)
   387  	}
   388  	if err := build.WriteArchive(alltools, allToolsArchiveFiles); err != nil {
   389  		log.Fatal(err)
   390  	}
   391  	for _, archive := range []string{geth, alltools} {
   392  		if err := archiveUpload(archive, *upload, *signer); err != nil {
   393  			log.Fatal(err)
   394  		}
   395  	}
   396  }
   397  
   398  func archiveBasename(arch string, env build.Environment) string {
   399  	platform := runtime.GOOS + "-" + arch
   400  	if arch == "arm" {
   401  		platform += os.Getenv("GOARM")
   402  	}
   403  	if arch == "android" {
   404  		platform = "android-all"
   405  	}
   406  	if arch == "ios" {
   407  		platform = "ios-all"
   408  	}
   409  	return platform + "-" + archiveVersion(env)
   410  }
   411  
   412  func archiveVersion(env build.Environment) string {
   413  	version := build.VERSION()
   414  	if isUnstableBuild(env) {
   415  		version += "-unstable"
   416  	}
   417  	if env.Commit != "" {
   418  		version += "-" + env.Commit[:8]
   419  	}
   420  	return version
   421  }
   422  
   423  func archiveUpload(archive string, blobstore string, signer string) error {
   424  	// If signing was requested, generate the signature files
   425  	if signer != "" {
   426  		pgpkey, err := base64.StdEncoding.DecodeString(os.Getenv(signer))
   427  		if err != nil {
   428  			return fmt.Errorf("invalid base64 %s", signer)
   429  		}
   430  		if err := build.PGPSignFile(archive, archive+".asc", string(pgpkey)); err != nil {
   431  			return err
   432  		}
   433  	}
   434  	// If uploading to Azure was requested, push the archive possibly with its signature
   435  	if blobstore != "" {
   436  		auth := build.AzureBlobstoreConfig{
   437  			Account:   strings.Split(blobstore, "/")[0],
   438  			Token:     os.Getenv("AZURE_BLOBSTORE_TOKEN"),
   439  			Container: strings.SplitN(blobstore, "/", 2)[1],
   440  		}
   441  		if err := build.AzureBlobstoreUpload(archive, filepath.Base(archive), auth); err != nil {
   442  			return err
   443  		}
   444  		if signer != "" {
   445  			if err := build.AzureBlobstoreUpload(archive+".asc", filepath.Base(archive+".asc"), auth); err != nil {
   446  				return err
   447  			}
   448  		}
   449  	}
   450  	return nil
   451  }
   452  
   453  // skips archiving for some build configurations.
   454  func maybeSkipArchive(env build.Environment) {
   455  	if env.IsPullRequest {
   456  		log.Printf("skipping because this is a PR build")
   457  		os.Exit(0)
   458  	}
   459  	if env.IsCronJob {
   460  		log.Printf("skipping because this is a cron job")
   461  		os.Exit(0)
   462  	}
   463  	if env.Branch != "master" && !strings.HasPrefix(env.Tag, "v1.") {
   464  		log.Printf("skipping because branch %q, tag %q is not on the whitelist", env.Branch, env.Tag)
   465  		os.Exit(0)
   466  	}
   467  }
   468  
   469  // Debian Packaging
   470  
   471  func doDebianSource(cmdline []string) {
   472  	var (
   473  		signer  = flag.String("signer", "", `Signing key name, also used as package author`)
   474  		upload  = flag.String("upload", "", `Where to upload the source package (usually "ppa:ethereum/ethereum")`)
   475  		workdir = flag.String("workdir", "", `Output directory for packages (uses temp dir if unset)`)
   476  		now     = time.Now()
   477  	)
   478  	flag.CommandLine.Parse(cmdline)
   479  	*workdir = makeWorkdir(*workdir)
   480  	env := build.Env()
   481  	maybeSkipArchive(env)
   482  
   483  	// Import the signing key.
   484  	if b64key := os.Getenv("PPA_SIGNING_KEY"); b64key != "" {
   485  		key, err := base64.StdEncoding.DecodeString(b64key)
   486  		if err != nil {
   487  			log.Fatal("invalid base64 PPA_SIGNING_KEY")
   488  		}
   489  		gpg := exec.Command("gpg", "--import")
   490  		gpg.Stdin = bytes.NewReader(key)
   491  		build.MustRun(gpg)
   492  	}
   493  
   494  	// Create the packages.
   495  	for _, distro := range debDistros {
   496  		meta := newDebMetadata(distro, *signer, env, now)
   497  		pkgdir := stageDebianSource(*workdir, meta)
   498  		debuild := exec.Command("debuild", "-S", "-sa", "-us", "-uc")
   499  		debuild.Dir = pkgdir
   500  		build.MustRun(debuild)
   501  
   502  		changes := fmt.Sprintf("%s_%s_source.changes", meta.Name(), meta.VersionString())
   503  		changes = filepath.Join(*workdir, changes)
   504  		if *signer != "" {
   505  			build.MustRunCommand("debsign", changes)
   506  		}
   507  		if *upload != "" {
   508  			build.MustRunCommand("dput", *upload, changes)
   509  		}
   510  	}
   511  }
   512  
   513  func makeWorkdir(wdflag string) string {
   514  	var err error
   515  	if wdflag != "" {
   516  		err = os.MkdirAll(wdflag, 0744)
   517  	} else {
   518  		wdflag, err = ioutil.TempDir("", "geth-build-")
   519  	}
   520  	if err != nil {
   521  		log.Fatal(err)
   522  	}
   523  	return wdflag
   524  }
   525  
   526  func isUnstableBuild(env build.Environment) bool {
   527  	if env.Tag != "" {
   528  		return false
   529  	}
   530  	return true
   531  }
   532  
   533  type debMetadata struct {
   534  	Env build.Environment
   535  
   536  	// go-ethereum version being built. Note that this
   537  	// is not the debian package version. The package version
   538  	// is constructed by VersionString.
   539  	Version string
   540  
   541  	Author       string // "name <email>", also selects signing key
   542  	Distro, Time string
   543  	Executables  []debExecutable
   544  }
   545  
   546  type debExecutable struct {
   547  	Name, Description string
   548  }
   549  
   550  func newDebMetadata(distro, author string, env build.Environment, t time.Time) debMetadata {
   551  	if author == "" {
   552  		// No signing key, use default author.
   553  		author = "Ethereum Builds <fjl@ethereum.org>"
   554  	}
   555  	return debMetadata{
   556  		Env:         env,
   557  		Author:      author,
   558  		Distro:      distro,
   559  		Version:     build.VERSION(),
   560  		Time:        t.Format(time.RFC1123Z),
   561  		Executables: debExecutables,
   562  	}
   563  }
   564  
   565  // Name returns the name of the metapackage that depends
   566  // on all executable packages.
   567  func (meta debMetadata) Name() string {
   568  	if isUnstableBuild(meta.Env) {
   569  		return "ethereum-unstable"
   570  	}
   571  	return "ethereum"
   572  }
   573  
   574  // VersionString returns the debian version of the packages.
   575  func (meta debMetadata) VersionString() string {
   576  	vsn := meta.Version
   577  	if meta.Env.Buildnum != "" {
   578  		vsn += "+build" + meta.Env.Buildnum
   579  	}
   580  	if meta.Distro != "" {
   581  		vsn += "+" + meta.Distro
   582  	}
   583  	return vsn
   584  }
   585  
   586  // ExeList returns the list of all executable packages.
   587  func (meta debMetadata) ExeList() string {
   588  	names := make([]string, len(meta.Executables))
   589  	for i, e := range meta.Executables {
   590  		names[i] = meta.ExeName(e)
   591  	}
   592  	return strings.Join(names, ", ")
   593  }
   594  
   595  // ExeName returns the package name of an executable package.
   596  func (meta debMetadata) ExeName(exe debExecutable) string {
   597  	if isUnstableBuild(meta.Env) {
   598  		return exe.Name + "-unstable"
   599  	}
   600  	return exe.Name
   601  }
   602  
   603  // ExeConflicts returns the content of the Conflicts field
   604  // for executable packages.
   605  func (meta debMetadata) ExeConflicts(exe debExecutable) string {
   606  	if isUnstableBuild(meta.Env) {
   607  		// Set up the conflicts list so that the *-unstable packages
   608  		// cannot be installed alongside the regular version.
   609  		//
   610  		// https://www.debian.org/doc/debian-policy/ch-relationships.html
   611  		// is very explicit about Conflicts: and says that Breaks: should
   612  		// be preferred and the conflicting files should be handled via
   613  		// alternates. We might do this eventually but using a conflict is
   614  		// easier now.
   615  		return "ethereum, " + exe.Name
   616  	}
   617  	return ""
   618  }
   619  
   620  func stageDebianSource(tmpdir string, meta debMetadata) (pkgdir string) {
   621  	pkg := meta.Name() + "-" + meta.VersionString()
   622  	pkgdir = filepath.Join(tmpdir, pkg)
   623  	if err := os.Mkdir(pkgdir, 0755); err != nil {
   624  		log.Fatal(err)
   625  	}
   626  
   627  	// Copy the source code.
   628  	build.MustRunCommand("git", "checkout-index", "-a", "--prefix", pkgdir+string(filepath.Separator))
   629  
   630  	// Put the debian build files in place.
   631  	debian := filepath.Join(pkgdir, "debian")
   632  	build.Render("build/deb.rules", filepath.Join(debian, "rules"), 0755, meta)
   633  	build.Render("build/deb.changelog", filepath.Join(debian, "changelog"), 0644, meta)
   634  	build.Render("build/deb.control", filepath.Join(debian, "control"), 0644, meta)
   635  	build.Render("build/deb.copyright", filepath.Join(debian, "copyright"), 0644, meta)
   636  	build.RenderString("8\n", filepath.Join(debian, "compat"), 0644, meta)
   637  	build.RenderString("3.0 (native)\n", filepath.Join(debian, "source/format"), 0644, meta)
   638  	for _, exe := range meta.Executables {
   639  		install := filepath.Join(debian, meta.ExeName(exe)+".install")
   640  		docs := filepath.Join(debian, meta.ExeName(exe)+".docs")
   641  		build.Render("build/deb.install", install, 0644, exe)
   642  		build.Render("build/deb.docs", docs, 0644, exe)
   643  	}
   644  
   645  	return pkgdir
   646  }
   647  
   648  // Windows installer
   649  
   650  func doWindowsInstaller(cmdline []string) {
   651  	// Parse the flags and make skip installer generation on PRs
   652  	var (
   653  		arch    = flag.String("arch", runtime.GOARCH, "Architecture for cross build packaging")
   654  		signer  = flag.String("signer", "", `Environment variable holding the signing key (e.g. WINDOWS_SIGNING_KEY)`)
   655  		upload  = flag.String("upload", "", `Destination to upload the archives (usually "gethstore/builds")`)
   656  		workdir = flag.String("workdir", "", `Output directory for packages (uses temp dir if unset)`)
   657  	)
   658  	flag.CommandLine.Parse(cmdline)
   659  	*workdir = makeWorkdir(*workdir)
   660  	env := build.Env()
   661  	maybeSkipArchive(env)
   662  
   663  	// Aggregate binaries that are included in the installer
   664  	var (
   665  		devTools []string
   666  		allTools []string
   667  		gethTool string
   668  	)
   669  	for _, file := range allToolsArchiveFiles {
   670  		if file == "COPYING" { // license, copied later
   671  			continue
   672  		}
   673  		allTools = append(allTools, filepath.Base(file))
   674  		if filepath.Base(file) == "geth.exe" {
   675  			gethTool = file
   676  		} else {
   677  			devTools = append(devTools, file)
   678  		}
   679  	}
   680  
   681  	// Render NSIS scripts: Installer NSIS contains two installer sections,
   682  	// first section contains the geth binary, second section holds the dev tools.
   683  	templateData := map[string]interface{}{
   684  		"License":  "COPYING",
   685  		"Geth":     gethTool,
   686  		"DevTools": devTools,
   687  	}
   688  	build.Render("build/nsis.geth.nsi", filepath.Join(*workdir, "geth.nsi"), 0644, nil)
   689  	build.Render("build/nsis.install.nsh", filepath.Join(*workdir, "install.nsh"), 0644, templateData)
   690  	build.Render("build/nsis.uninstall.nsh", filepath.Join(*workdir, "uninstall.nsh"), 0644, allTools)
   691  	build.Render("build/nsis.pathupdate.nsh", filepath.Join(*workdir, "PathUpdate.nsh"), 0644, nil)
   692  	build.Render("build/nsis.envvarupdate.nsh", filepath.Join(*workdir, "EnvVarUpdate.nsh"), 0644, nil)
   693  	build.CopyFile(filepath.Join(*workdir, "SimpleFC.dll"), "build/nsis.simplefc.dll", 0755)
   694  	build.CopyFile(filepath.Join(*workdir, "COPYING"), "COPYING", 0755)
   695  
   696  	// Build the installer. This assumes that all the needed files have been previously
   697  	// built (don't mix building and packaging to keep cross compilation complexity to a
   698  	// minimum).
   699  	version := strings.Split(build.VERSION(), ".")
   700  	if env.Commit != "" {
   701  		version[2] += "-" + env.Commit[:8]
   702  	}
   703  	installer, _ := filepath.Abs("geth-" + archiveBasename(*arch, env) + ".exe")
   704  	build.MustRunCommand("makensis.exe",
   705  		"/DOUTPUTFILE="+installer,
   706  		"/DMAJORVERSION="+version[0],
   707  		"/DMINORVERSION="+version[1],
   708  		"/DBUILDVERSION="+version[2],
   709  		"/DARCH="+*arch,
   710  		filepath.Join(*workdir, "geth.nsi"),
   711  	)
   712  
   713  	// Sign and publish installer.
   714  	if err := archiveUpload(installer, *upload, *signer); err != nil {
   715  		log.Fatal(err)
   716  	}
   717  }
   718  
   719  // Android archives
   720  
   721  func doAndroidArchive(cmdline []string) {
   722  	var (
   723  		local  = flag.Bool("local", false, `Flag whether we're only doing a local build (skip Maven artifacts)`)
   724  		signer = flag.String("signer", "", `Environment variable holding the signing key (e.g. ANDROID_SIGNING_KEY)`)
   725  		deploy = flag.String("deploy", "", `Destination to deploy the archive (usually "https://oss.sonatype.org")`)
   726  		upload = flag.String("upload", "", `Destination to upload the archive (usually "gethstore/builds")`)
   727  	)
   728  	flag.CommandLine.Parse(cmdline)
   729  	env := build.Env()
   730  
   731  	// Sanity check that the SDK and NDK are installed and set
   732  	if os.Getenv("ANDROID_HOME") == "" {
   733  		log.Fatal("Please ensure ANDROID_HOME points to your Android SDK")
   734  	}
   735  	if os.Getenv("ANDROID_NDK") == "" {
   736  		log.Fatal("Please ensure ANDROID_NDK points to your Android NDK")
   737  	}
   738  	// Build the Android archive and Maven resources
   739  	build.MustRun(goTool("get", "golang.org/x/mobile/cmd/gomobile"))
   740  	build.MustRun(gomobileTool("init", "--ndk", os.Getenv("ANDROID_NDK")))
   741  	build.MustRun(gomobileTool("bind", "--target", "android", "--javapkg", "org.ethereum", "-v", "github.com/vantum/vantum/mobile"))
   742  
   743  	if *local {
   744  		// If we're building locally, copy bundle to build dir and skip Maven
   745  		os.Rename("geth.aar", filepath.Join(GOBIN, "geth.aar"))
   746  		return
   747  	}
   748  	meta := newMavenMetadata(env)
   749  	build.Render("build/mvn.pom", meta.Package+".pom", 0755, meta)
   750  
   751  	// Skip Maven deploy and Azure upload for PR builds
   752  	maybeSkipArchive(env)
   753  
   754  	// Sign and upload the archive to Azure
   755  	archive := "geth-" + archiveBasename("android", env) + ".aar"
   756  	os.Rename("geth.aar", archive)
   757  
   758  	if err := archiveUpload(archive, *upload, *signer); err != nil {
   759  		log.Fatal(err)
   760  	}
   761  	// Sign and upload all the artifacts to Maven Central
   762  	os.Rename(archive, meta.Package+".aar")
   763  	if *signer != "" && *deploy != "" {
   764  		// Import the signing key into the local GPG instance
   765  		if b64key := os.Getenv(*signer); b64key != "" {
   766  			key, err := base64.StdEncoding.DecodeString(b64key)
   767  			if err != nil {
   768  				log.Fatalf("invalid base64 %s", *signer)
   769  			}
   770  			gpg := exec.Command("gpg", "--import")
   771  			gpg.Stdin = bytes.NewReader(key)
   772  			build.MustRun(gpg)
   773  		}
   774  		// Upload the artifacts to Sonatype and/or Maven Central
   775  		repo := *deploy + "/service/local/staging/deploy/maven2"
   776  		if meta.Develop {
   777  			repo = *deploy + "/content/repositories/snapshots"
   778  		}
   779  		build.MustRunCommand("mvn", "gpg:sign-and-deploy-file",
   780  			"-settings=build/mvn.settings", "-Durl="+repo, "-DrepositoryId=ossrh",
   781  			"-DpomFile="+meta.Package+".pom", "-Dfile="+meta.Package+".aar")
   782  	}
   783  }
   784  
   785  func gomobileTool(subcmd string, args ...string) *exec.Cmd {
   786  	cmd := exec.Command(filepath.Join(GOBIN, "gomobile"), subcmd)
   787  	cmd.Args = append(cmd.Args, args...)
   788  	cmd.Env = []string{
   789  		"GOPATH=" + build.GOPATH(),
   790  	}
   791  	for _, e := range os.Environ() {
   792  		if strings.HasPrefix(e, "GOPATH=") {
   793  			continue
   794  		}
   795  		cmd.Env = append(cmd.Env, e)
   796  	}
   797  	return cmd
   798  }
   799  
   800  type mavenMetadata struct {
   801  	Version      string
   802  	Package      string
   803  	Develop      bool
   804  	Contributors []mavenContributor
   805  }
   806  
   807  type mavenContributor struct {
   808  	Name  string
   809  	Email string
   810  }
   811  
   812  func newMavenMetadata(env build.Environment) mavenMetadata {
   813  	// Collect the list of authors from the repo root
   814  	contribs := []mavenContributor{}
   815  	if authors, err := os.Open("AUTHORS"); err == nil {
   816  		defer authors.Close()
   817  
   818  		scanner := bufio.NewScanner(authors)
   819  		for scanner.Scan() {
   820  			// Skip any whitespace from the authors list
   821  			line := strings.TrimSpace(scanner.Text())
   822  			if line == "" || line[0] == '#' {
   823  				continue
   824  			}
   825  			// Split the author and insert as a contributor
   826  			re := regexp.MustCompile("([^<]+) <(.+)>")
   827  			parts := re.FindStringSubmatch(line)
   828  			if len(parts) == 3 {
   829  				contribs = append(contribs, mavenContributor{Name: parts[1], Email: parts[2]})
   830  			}
   831  		}
   832  	}
   833  	// Render the version and package strings
   834  	version := build.VERSION()
   835  	if isUnstableBuild(env) {
   836  		version += "-SNAPSHOT"
   837  	}
   838  	return mavenMetadata{
   839  		Version:      version,
   840  		Package:      "geth-" + version,
   841  		Develop:      isUnstableBuild(env),
   842  		Contributors: contribs,
   843  	}
   844  }
   845  
   846  // XCode frameworks
   847  
   848  func doXCodeFramework(cmdline []string) {
   849  	var (
   850  		local  = flag.Bool("local", false, `Flag whether we're only doing a local build (skip Maven artifacts)`)
   851  		signer = flag.String("signer", "", `Environment variable holding the signing key (e.g. IOS_SIGNING_KEY)`)
   852  		deploy = flag.String("deploy", "", `Destination to deploy the archive (usually "trunk")`)
   853  		upload = flag.String("upload", "", `Destination to upload the archives (usually "gethstore/builds")`)
   854  	)
   855  	flag.CommandLine.Parse(cmdline)
   856  	env := build.Env()
   857  
   858  	// Build the iOS XCode framework
   859  	build.MustRun(goTool("get", "golang.org/x/mobile/cmd/gomobile"))
   860  	build.MustRun(gomobileTool("init"))
   861  	bind := gomobileTool("bind", "--target", "ios", "--tags", "ios", "-v", "github.com/vantum/vantum/mobile")
   862  
   863  	if *local {
   864  		// If we're building locally, use the build folder and stop afterwards
   865  		bind.Dir, _ = filepath.Abs(GOBIN)
   866  		build.MustRun(bind)
   867  		return
   868  	}
   869  	archive := "geth-" + archiveBasename("ios", env)
   870  	if err := os.Mkdir(archive, os.ModePerm); err != nil {
   871  		log.Fatal(err)
   872  	}
   873  	bind.Dir, _ = filepath.Abs(archive)
   874  	build.MustRun(bind)
   875  	build.MustRunCommand("tar", "-zcvf", archive+".tar.gz", archive)
   876  
   877  	// Skip CocoaPods deploy and Azure upload for PR builds
   878  	maybeSkipArchive(env)
   879  
   880  	// Sign and upload the framework to Azure
   881  	if err := archiveUpload(archive+".tar.gz", *upload, *signer); err != nil {
   882  		log.Fatal(err)
   883  	}
   884  	// Prepare and upload a PodSpec to CocoaPods
   885  	if *deploy != "" {
   886  		meta := newPodMetadata(env, archive)
   887  		build.Render("build/pod.podspec", "Geth.podspec", 0755, meta)
   888  		build.MustRunCommand("pod", *deploy, "push", "Geth.podspec", "--allow-warnings", "--verbose")
   889  	}
   890  }
   891  
   892  type podMetadata struct {
   893  	Version      string
   894  	Commit       string
   895  	Archive      string
   896  	Contributors []podContributor
   897  }
   898  
   899  type podContributor struct {
   900  	Name  string
   901  	Email string
   902  }
   903  
   904  func newPodMetadata(env build.Environment, archive string) podMetadata {
   905  	// Collect the list of authors from the repo root
   906  	contribs := []podContributor{}
   907  	if authors, err := os.Open("AUTHORS"); err == nil {
   908  		defer authors.Close()
   909  
   910  		scanner := bufio.NewScanner(authors)
   911  		for scanner.Scan() {
   912  			// Skip any whitespace from the authors list
   913  			line := strings.TrimSpace(scanner.Text())
   914  			if line == "" || line[0] == '#' {
   915  				continue
   916  			}
   917  			// Split the author and insert as a contributor
   918  			re := regexp.MustCompile("([^<]+) <(.+)>")
   919  			parts := re.FindStringSubmatch(line)
   920  			if len(parts) == 3 {
   921  				contribs = append(contribs, podContributor{Name: parts[1], Email: parts[2]})
   922  			}
   923  		}
   924  	}
   925  	version := build.VERSION()
   926  	if isUnstableBuild(env) {
   927  		version += "-unstable." + env.Buildnum
   928  	}
   929  	return podMetadata{
   930  		Archive:      archive,
   931  		Version:      version,
   932  		Commit:       env.Commit,
   933  		Contributors: contribs,
   934  	}
   935  }
   936  
   937  // Cross compilation
   938  
   939  func doXgo(cmdline []string) {
   940  	var (
   941  		alltools = flag.Bool("alltools", false, `Flag whether we're building all known tools, or only on in particular`)
   942  	)
   943  	flag.CommandLine.Parse(cmdline)
   944  	env := build.Env()
   945  
   946  	// Make sure xgo is available for cross compilation
   947  	gogetxgo := goTool("get", "github.com/karalabe/xgo")
   948  	build.MustRun(gogetxgo)
   949  
   950  	// If all tools building is requested, build everything the builder wants
   951  	args := append(buildFlags(env), flag.Args()...)
   952  
   953  	if *alltools {
   954  		args = append(args, []string{"--dest", GOBIN}...)
   955  		for _, res := range allToolsArchiveFiles {
   956  			if strings.HasPrefix(res, GOBIN) {
   957  				// Binary tool found, cross build it explicitly
   958  				args = append(args, "./"+filepath.Join("cmd", filepath.Base(res)))
   959  				xgo := xgoTool(args)
   960  				build.MustRun(xgo)
   961  				args = args[:len(args)-1]
   962  			}
   963  		}
   964  		return
   965  	}
   966  	// Otherwise xxecute the explicit cross compilation
   967  	path := args[len(args)-1]
   968  	args = append(args[:len(args)-1], []string{"--dest", GOBIN, path}...)
   969  
   970  	xgo := xgoTool(args)
   971  	build.MustRun(xgo)
   972  }
   973  
   974  func xgoTool(args []string) *exec.Cmd {
   975  	cmd := exec.Command(filepath.Join(GOBIN, "xgo"), args...)
   976  	cmd.Env = []string{
   977  		"GOPATH=" + build.GOPATH(),
   978  		"GOBIN=" + GOBIN,
   979  	}
   980  	for _, e := range os.Environ() {
   981  		if strings.HasPrefix(e, "GOPATH=") || strings.HasPrefix(e, "GOBIN=") {
   982  			continue
   983  		}
   984  		cmd.Env = append(cmd.Env, e)
   985  	}
   986  	return cmd
   987  }
   988  
   989  // Binary distribution cleanups
   990  
   991  func doPurge(cmdline []string) {
   992  	var (
   993  		store = flag.String("store", "", `Destination from where to purge archives (usually "gethstore/builds")`)
   994  		limit = flag.Int("days", 30, `Age threshold above which to delete unstalbe archives`)
   995  	)
   996  	flag.CommandLine.Parse(cmdline)
   997  
   998  	if env := build.Env(); !env.IsCronJob {
   999  		log.Printf("skipping because not a cron job")
  1000  		os.Exit(0)
  1001  	}
  1002  	// Create the azure authentication and list the current archives
  1003  	auth := build.AzureBlobstoreConfig{
  1004  		Account:   strings.Split(*store, "/")[0],
  1005  		Token:     os.Getenv("AZURE_BLOBSTORE_TOKEN"),
  1006  		Container: strings.SplitN(*store, "/", 2)[1],
  1007  	}
  1008  	blobs, err := build.AzureBlobstoreList(auth)
  1009  	if err != nil {
  1010  		log.Fatal(err)
  1011  	}
  1012  	// Iterate over the blobs, collect and sort all unstable builds
  1013  	for i := 0; i < len(blobs); i++ {
  1014  		if !strings.Contains(blobs[i].Name, "unstable") {
  1015  			blobs = append(blobs[:i], blobs[i+1:]...)
  1016  			i--
  1017  		}
  1018  	}
  1019  	for i := 0; i < len(blobs); i++ {
  1020  		for j := i + 1; j < len(blobs); j++ {
  1021  			iTime, err := time.Parse(time.RFC1123, blobs[i].Properties.LastModified)
  1022  			if err != nil {
  1023  				log.Fatal(err)
  1024  			}
  1025  			jTime, err := time.Parse(time.RFC1123, blobs[j].Properties.LastModified)
  1026  			if err != nil {
  1027  				log.Fatal(err)
  1028  			}
  1029  			if iTime.After(jTime) {
  1030  				blobs[i], blobs[j] = blobs[j], blobs[i]
  1031  			}
  1032  		}
  1033  	}
  1034  	// Filter out all archives more recent that the given threshold
  1035  	for i, blob := range blobs {
  1036  		timestamp, _ := time.Parse(time.RFC1123, blob.Properties.LastModified)
  1037  		if time.Since(timestamp) < time.Duration(*limit)*24*time.Hour {
  1038  			blobs = blobs[:i]
  1039  			break
  1040  		}
  1041  	}
  1042  	// Delete all marked as such and return
  1043  	if err := build.AzureBlobstoreDelete(auth, blobs); err != nil {
  1044  		log.Fatal(err)
  1045  	}
  1046  }