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