github.com/murrekatt/go-ethereum@v1.5.8-0.20170123175102-fc52f2c007fb/build/ci.go (about)

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