github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/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/atheioschain/go-atheios/internal/build"
    60  )
    61  
    62  var (
    63  	// Files that end up in the gath*.zip archive.
    64  	gathArchiveFiles = []string{
    65  		"COPYING",
    66  		executablePath("gath"),
    67  	}
    68  
    69  	// Files that end up in the gath-alltools*.zip archive.
    70  	allToolsArchiveFiles = []string{
    71  		"COPYING",
    72  		executablePath("abigen"),
    73  		executablePath("evm"),
    74  		executablePath("gath"),
    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:        "gath",
    83  			Description: "atheios 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 !strings.Contains(runtime.Version(), "devel") {
   161  		// Figure out the minor version number since we can't textually compare (1.10 < 1.7)
   162  		var minor int
   163  		fmt.Sscanf(strings.TrimPrefix(runtime.Version(), "go1."), "%d", &minor)
   164  
   165  		if minor < 7 {
   166  			log.Println("You have Go version", runtime.Version())
   167  			log.Println("go-ethereum requires at least Go version 1.7 and cannot")
   168  			log.Println("be compiled with an earlier version. Please upgrade your Go installation.")
   169  			os.Exit(1)
   170  		}
   171  	}
   172  
   173  	// Compile packages given as arguments, or everything if there are no arguments.
   174  	packages := []string{"./..."}
   175  	if flag.NArg() > 0 {
   176  		packages = flag.Args()
   177  	}
   178  	if *arch == "" || *arch == runtime.GOARCH {
   179  		goinstall := goTool("install", buildFlags(env)...)
   180  		goinstall.Args = append(goinstall.Args, "-v")
   181  		goinstall.Args = append(goinstall.Args, packages...)
   182  		build.MustRun(goinstall)
   183  		return
   184  	}
   185  	// If we are cross compiling to ARMv5 ARMv6 or ARMv7, clean any prvious builds
   186  	if *arch == "arm" {
   187  		os.RemoveAll(filepath.Join(runtime.GOROOT(), "pkg", runtime.GOOS+"_arm"))
   188  		for _, path := range filepath.SplitList(build.GOPATH()) {
   189  			os.RemoveAll(filepath.Join(path, "pkg", runtime.GOOS+"_arm"))
   190  		}
   191  	}
   192  	// Seems we are cross compiling, work around forbidden GOBIN
   193  	goinstall := goToolArch(*arch, "install", buildFlags(env)...)
   194  	goinstall.Args = append(goinstall.Args, "-v")
   195  	goinstall.Args = append(goinstall.Args, []string{"-buildmode", "archive"}...)
   196  	goinstall.Args = append(goinstall.Args, packages...)
   197  	build.MustRun(goinstall)
   198  
   199  	if cmds, err := ioutil.ReadDir("cmd"); err == nil {
   200  		for _, cmd := range cmds {
   201  			pkgs, err := parser.ParseDir(token.NewFileSet(), filepath.Join(".", "cmd", cmd.Name()), nil, parser.PackageClauseOnly)
   202  			if err != nil {
   203  				log.Fatal(err)
   204  			}
   205  			for name := range pkgs {
   206  				if name == "main" {
   207  					gobuild := goToolArch(*arch, "build", buildFlags(env)...)
   208  					gobuild.Args = append(gobuild.Args, "-v")
   209  					gobuild.Args = append(gobuild.Args, []string{"-o", executablePath(cmd.Name())}...)
   210  					gobuild.Args = append(gobuild.Args, "."+string(filepath.Separator)+filepath.Join("cmd", cmd.Name()))
   211  					build.MustRun(gobuild)
   212  					break
   213  				}
   214  			}
   215  		}
   216  	}
   217  }
   218  
   219  func buildFlags(env build.Environment) (flags []string) {
   220  	if os.Getenv("GO_OPENCL") != "" {
   221  		flags = append(flags, "-tags", "opencl")
   222  	}
   223  
   224  	// Set gitCommit constant via link-time assignment.
   225  	if env.Commit != "" {
   226  		flags = append(flags, "-ldflags", "-X main.gitCommit="+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  
   240  	if subcmd == "build" || subcmd == "install" || subcmd == "test" {
   241  		// Go CGO has a Windows linker error prior to 1.8 (https://github.com/golang/go/issues/8756).
   242  		// Work around issue by allowing multiple definitions for <1.8 builds.
   243  
   244  		var minor int
   245  		fmt.Sscanf(strings.TrimPrefix(runtime.Version(), "go1."), "%d", &minor)
   246  
   247  		if runtime.GOOS == "windows" && minor < 8 {
   248  			cmd.Args = append(cmd.Args, []string{"-ldflags", "-extldflags -Wl,--allow-multiple-definition"}...)
   249  		}
   250  	}
   251  
   252  	cmd.Env = []string{"GOPATH=" + build.GOPATH()}
   253  	if arch == "" || arch == runtime.GOARCH {
   254  		cmd.Env = append(cmd.Env, "GOBIN="+GOBIN)
   255  	} else {
   256  		cmd.Env = append(cmd.Env, "CGO_ENABLED=1")
   257  		cmd.Env = append(cmd.Env, "GOARCH="+arch)
   258  	}
   259  	for _, e := range os.Environ() {
   260  		if strings.HasPrefix(e, "GOPATH=") || strings.HasPrefix(e, "GOBIN=") {
   261  			continue
   262  		}
   263  		cmd.Env = append(cmd.Env, e)
   264  	}
   265  	return cmd
   266  }
   267  
   268  // Running The Tests
   269  //
   270  // "tests" also includes static analysis tools such as vet.
   271  
   272  func doTest(cmdline []string) {
   273  	var (
   274  		vet      = flag.Bool("vet", false, "Whether to run go vet")
   275  		misspell = flag.Bool("misspell", false, "Whether to run the spell checker")
   276  		coverage = flag.Bool("coverage", false, "Whether to record code coverage")
   277  	)
   278  	flag.CommandLine.Parse(cmdline)
   279  
   280  	packages := []string{"./..."}
   281  	if len(flag.CommandLine.Args()) > 0 {
   282  		packages = flag.CommandLine.Args()
   283  	}
   284  	if len(packages) == 1 && packages[0] == "./..." {
   285  		// Resolve ./... manually since go vet will fail on vendored stuff
   286  		out, err := goTool("list", "./...").CombinedOutput()
   287  		if err != nil {
   288  			log.Fatalf("package listing failed: %v\n%s", err, string(out))
   289  		}
   290  		packages = []string{}
   291  		for _, line := range strings.Split(string(out), "\n") {
   292  			if !strings.Contains(line, "vendor") {
   293  				packages = append(packages, strings.TrimSpace(line))
   294  			}
   295  		}
   296  	}
   297  	// Run analysis tools before the tests.
   298  	if *vet {
   299  		build.MustRun(goTool("vet", packages...))
   300  	}
   301  	if *misspell {
   302  		spellcheck(packages)
   303  	}
   304  	// Run the actual tests.
   305  	gotest := goTool("test")
   306  	// Test a single package at a time. CI builders are slow
   307  	// and some tests run into timeouts under load.
   308  	gotest.Args = append(gotest.Args, "-p", "1")
   309  	if *coverage {
   310  		gotest.Args = append(gotest.Args, "-covermode=atomic", "-cover")
   311  	}
   312  	gotest.Args = append(gotest.Args, packages...)
   313  	build.MustRun(gotest)
   314  }
   315  
   316  // spellcheck runs the client9/misspell spellchecker package on all Go, Cgo and
   317  // test files in the requested packages.
   318  func spellcheck(packages []string) {
   319  	// Ensure the spellchecker is available
   320  	build.MustRun(goTool("get", "github.com/client9/misspell/cmd/misspell"))
   321  
   322  	// Windows chokes on long argument lists, check packages individually
   323  	for _, pkg := range packages {
   324  		// The spell checker doesn't work on packages, gather all .go files for it
   325  		out, err := goTool("list", "-f", "{{.Dir}}{{range .GoFiles}}\n{{.}}{{end}}{{range .CgoFiles}}\n{{.}}{{end}}{{range .TestGoFiles}}\n{{.}}{{end}}", pkg).CombinedOutput()
   326  		if err != nil {
   327  			log.Fatalf("source file listing failed: %v\n%s", err, string(out))
   328  		}
   329  		// Retrieve the folder and assemble the source list
   330  		lines := strings.Split(string(out), "\n")
   331  		root := lines[0]
   332  
   333  		sources := make([]string, 0, len(lines)-1)
   334  		for _, line := range lines[1:] {
   335  			if line = strings.TrimSpace(line); line != "" {
   336  				sources = append(sources, filepath.Join(root, line))
   337  			}
   338  		}
   339  		// Run the spell checker for this particular package
   340  		build.MustRunCommand(filepath.Join(GOBIN, "misspell"), append([]string{"-error"}, sources...)...)
   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 "gathstore/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  		gath     = "gath-" + base + ext
   368  		alltools = "gath-alltools-" + base + ext
   369  	)
   370  	maybeSkipArchive(env)
   371  	if err := build.WriteArchive(gath, gathArchiveFiles); 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{gath, 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.Branch != "master" && !strings.HasPrefix(env.Tag, "v1.") {
   446  		log.Printf("skipping because branch %q, tag %q is not on the whitelist", env.Branch, env.Tag)
   447  		os.Exit(0)
   448  	}
   449  }
   450  
   451  // Debian Packaging
   452  
   453  func doDebianSource(cmdline []string) {
   454  	var (
   455  		signer  = flag.String("signer", "", `Signing key name, also used as package author`)
   456  		upload  = flag.String("upload", "", `Where to upload the source package (usually "ppa:ethereum/ethereum")`)
   457  		workdir = flag.String("workdir", "", `Output directory for packages (uses temp dir if unset)`)
   458  		now     = time.Now()
   459  	)
   460  	flag.CommandLine.Parse(cmdline)
   461  	*workdir = makeWorkdir(*workdir)
   462  	env := build.Env()
   463  	maybeSkipArchive(env)
   464  
   465  	// Import the signing key.
   466  	if b64key := os.Getenv("PPA_SIGNING_KEY"); b64key != "" {
   467  		key, err := base64.StdEncoding.DecodeString(b64key)
   468  		if err != nil {
   469  			log.Fatal("invalid base64 PPA_SIGNING_KEY")
   470  		}
   471  		gpg := exec.Command("gpg", "--import")
   472  		gpg.Stdin = bytes.NewReader(key)
   473  		build.MustRun(gpg)
   474  	}
   475  
   476  	// Create the packages.
   477  	for _, distro := range debDistros {
   478  		meta := newDebMetadata(distro, *signer, env, now)
   479  		pkgdir := stageDebianSource(*workdir, meta)
   480  		debuild := exec.Command("debuild", "-S", "-sa", "-us", "-uc")
   481  		debuild.Dir = pkgdir
   482  		build.MustRun(debuild)
   483  
   484  		changes := fmt.Sprintf("%s_%s_source.changes", meta.Name(), meta.VersionString())
   485  		changes = filepath.Join(*workdir, changes)
   486  		if *signer != "" {
   487  			build.MustRunCommand("debsign", changes)
   488  		}
   489  		if *upload != "" {
   490  			build.MustRunCommand("dput", *upload, changes)
   491  		}
   492  	}
   493  }
   494  
   495  func makeWorkdir(wdflag string) string {
   496  	var err error
   497  	if wdflag != "" {
   498  		err = os.MkdirAll(wdflag, 0744)
   499  	} else {
   500  		wdflag, err = ioutil.TempDir("", "gath-build-")
   501  	}
   502  	if err != nil {
   503  		log.Fatal(err)
   504  	}
   505  	return wdflag
   506  }
   507  
   508  func isUnstableBuild(env build.Environment) bool {
   509  	if env.Tag != "" {
   510  		return false
   511  	}
   512  	return true
   513  }
   514  
   515  type debMetadata struct {
   516  	Env build.Environment
   517  
   518  	// go-atheios version being built. Note that this
   519  	// is not the debian package version. The package version
   520  	// is constructed by VersionString.
   521  	Version string
   522  
   523  	Author       string // "name <email>", also selects signing key
   524  	Distro, Time string
   525  	Executables  []debExecutable
   526  }
   527  
   528  type debExecutable struct {
   529  	Name, Description string
   530  }
   531  
   532  func newDebMetadata(distro, author string, env build.Environment, t time.Time) debMetadata {
   533  	if author == "" {
   534  		// No signing key, use default author.
   535  		author = "atheios Builds <fjl@ethereum.org>"
   536  	}
   537  	return debMetadata{
   538  		Env:         env,
   539  		Author:      author,
   540  		Distro:      distro,
   541  		Version:     build.VERSION(),
   542  		Time:        t.Format(time.RFC1123Z),
   543  		Executables: debExecutables,
   544  	}
   545  }
   546  
   547  // Name returns the name of the metapackage that depends
   548  // on all executable packages.
   549  func (meta debMetadata) Name() string {
   550  	if isUnstableBuild(meta.Env) {
   551  		return "atheios-unstable"
   552  	}
   553  	return "atheios"
   554  }
   555  
   556  // VersionString returns the debian version of the packages.
   557  func (meta debMetadata) VersionString() string {
   558  	vsn := meta.Version
   559  	if meta.Env.Buildnum != "" {
   560  		vsn += "+build" + meta.Env.Buildnum
   561  	}
   562  	if meta.Distro != "" {
   563  		vsn += "+" + meta.Distro
   564  	}
   565  	return vsn
   566  }
   567  
   568  // ExeList returns the list of all executable packages.
   569  func (meta debMetadata) ExeList() string {
   570  	names := make([]string, len(meta.Executables))
   571  	for i, e := range meta.Executables {
   572  		names[i] = meta.ExeName(e)
   573  	}
   574  	return strings.Join(names, ", ")
   575  }
   576  
   577  // ExeName returns the package name of an executable package.
   578  func (meta debMetadata) ExeName(exe debExecutable) string {
   579  	if isUnstableBuild(meta.Env) {
   580  		return exe.Name + "-unstable"
   581  	}
   582  	return exe.Name
   583  }
   584  
   585  // ExeConflicts returns the content of the Conflicts field
   586  // for executable packages.
   587  func (meta debMetadata) ExeConflicts(exe debExecutable) string {
   588  	if isUnstableBuild(meta.Env) {
   589  		// Set up the conflicts list so that the *-unstable packages
   590  		// cannot be installed alongside the regular version.
   591  		//
   592  		// https://www.debian.org/doc/debian-policy/ch-relationships.html
   593  		// is very explicit about Conflicts: and says that Breaks: should
   594  		// be preferred and the conflicting files should be handled via
   595  		// alternates. We might do this eventually but using a conflict is
   596  		// easier now.
   597  		return "atheios, " + exe.Name
   598  	}
   599  	return ""
   600  }
   601  
   602  func stageDebianSource(tmpdir string, meta debMetadata) (pkgdir string) {
   603  	pkg := meta.Name() + "-" + meta.VersionString()
   604  	pkgdir = filepath.Join(tmpdir, pkg)
   605  	if err := os.Mkdir(pkgdir, 0755); err != nil {
   606  		log.Fatal(err)
   607  	}
   608  
   609  	// Copy the source code.
   610  	build.MustRunCommand("git", "checkout-index", "-a", "--prefix", pkgdir+string(filepath.Separator))
   611  
   612  	// Put the debian build files in place.
   613  	debian := filepath.Join(pkgdir, "debian")
   614  	build.Render("build/deb.rules", filepath.Join(debian, "rules"), 0755, meta)
   615  	build.Render("build/deb.changelog", filepath.Join(debian, "changelog"), 0644, meta)
   616  	build.Render("build/deb.control", filepath.Join(debian, "control"), 0644, meta)
   617  	build.Render("build/deb.copyright", filepath.Join(debian, "copyright"), 0644, meta)
   618  	build.RenderString("8\n", filepath.Join(debian, "compat"), 0644, meta)
   619  	build.RenderString("3.0 (native)\n", filepath.Join(debian, "source/format"), 0644, meta)
   620  	for _, exe := range meta.Executables {
   621  		install := filepath.Join(debian, meta.ExeName(exe)+".install")
   622  		docs := filepath.Join(debian, meta.ExeName(exe)+".docs")
   623  		build.Render("build/deb.install", install, 0644, exe)
   624  		build.Render("build/deb.docs", docs, 0644, exe)
   625  	}
   626  
   627  	return pkgdir
   628  }
   629  
   630  // Windows installer
   631  
   632  func doWindowsInstaller(cmdline []string) {
   633  	// Parse the flags and make skip installer generation on PRs
   634  	var (
   635  		arch    = flag.String("arch", runtime.GOARCH, "Architecture for cross build packaging")
   636  		signer  = flag.String("signer", "", `Environment variable holding the signing key (e.g. WINDOWS_SIGNING_KEY)`)
   637  		upload  = flag.String("upload", "", `Destination to upload the archives (usually "gathstore/builds")`)
   638  		workdir = flag.String("workdir", "", `Output directory for packages (uses temp dir if unset)`)
   639  	)
   640  	flag.CommandLine.Parse(cmdline)
   641  	*workdir = makeWorkdir(*workdir)
   642  	env := build.Env()
   643  	maybeSkipArchive(env)
   644  
   645  	// Aggregate binaries that are included in the installer
   646  	var (
   647  		devTools []string
   648  		allTools []string
   649  		gathTool string
   650  	)
   651  	for _, file := range allToolsArchiveFiles {
   652  		if file == "COPYING" { // license, copied later
   653  			continue
   654  		}
   655  		allTools = append(allTools, filepath.Base(file))
   656  		if filepath.Base(file) == "gath.exe" {
   657  			gathTool = file
   658  		} else {
   659  			devTools = append(devTools, file)
   660  		}
   661  	}
   662  
   663  	// Render NSIS scripts: Installer NSIS contains two installer sections,
   664  	// first section contains the gath binary, second section holds the dev tools.
   665  	templateData := map[string]interface{}{
   666  		"License":  "COPYING",
   667  		"gath":     gathTool,
   668  		"DevTools": devTools,
   669  	}
   670  	build.Render("build/nsis.gath.nsi", filepath.Join(*workdir, "gath.nsi"), 0644, nil)
   671  	build.Render("build/nsis.install.nsh", filepath.Join(*workdir, "install.nsh"), 0644, templateData)
   672  	build.Render("build/nsis.uninstall.nsh", filepath.Join(*workdir, "uninstall.nsh"), 0644, allTools)
   673  	build.Render("build/nsis.pathupdate.nsh", filepath.Join(*workdir, "PathUpdate.nsh"), 0644, nil)
   674  	build.Render("build/nsis.envvarupdate.nsh", filepath.Join(*workdir, "EnvVarUpdate.nsh"), 0644, nil)
   675  	build.CopyFile(filepath.Join(*workdir, "SimpleFC.dll"), "build/nsis.simplefc.dll", 0755)
   676  	build.CopyFile(filepath.Join(*workdir, "COPYING"), "COPYING", 0755)
   677  
   678  	// Build the installer. This assumes that all the needed files have been previously
   679  	// built (don't mix building and packaging to keep cross compilation complexity to a
   680  	// minimum).
   681  	version := strings.Split(build.VERSION(), ".")
   682  	if env.Commit != "" {
   683  		version[2] += "-" + env.Commit[:8]
   684  	}
   685  	installer, _ := filepath.Abs("gath-" + archiveBasename(*arch, env) + ".exe")
   686  	build.MustRunCommand("makensis.exe",
   687  		"/DOUTPUTFILE="+installer,
   688  		"/DMAJORVERSION="+version[0],
   689  		"/DMINORVERSION="+version[1],
   690  		"/DBUILDVERSION="+version[2],
   691  		"/DARCH="+*arch,
   692  		filepath.Join(*workdir, "gath.nsi"),
   693  	)
   694  
   695  	// Sign and publish installer.
   696  	if err := archiveUpload(installer, *upload, *signer); err != nil {
   697  		log.Fatal(err)
   698  	}
   699  }
   700  
   701  // Android archives
   702  
   703  func doAndroidArchive(cmdline []string) {
   704  	var (
   705  		local  = flag.Bool("local", false, `Flag whether we're only doing a local build (skip Maven artifacts)`)
   706  		signer = flag.String("signer", "", `Environment variable holding the signing key (e.g. ANDROID_SIGNING_KEY)`)
   707  		deploy = flag.String("deploy", "", `Destination to deploy the archive (usually "https://oss.sonatype.org")`)
   708  		upload = flag.String("upload", "", `Destination to upload the archive (usually "gathstore/builds")`)
   709  	)
   710  	flag.CommandLine.Parse(cmdline)
   711  	env := build.Env()
   712  
   713  	// Sanity check that the SDK and NDK are installed and set
   714  	if os.Getenv("ANDROID_HOME") == "" {
   715  		log.Fatal("Please ensure ANDROID_HOME points to your Android SDK")
   716  	}
   717  	if os.Getenv("ANDROID_NDK") == "" {
   718  		log.Fatal("Please ensure ANDROID_NDK points to your Android NDK")
   719  	}
   720  	// Build the Android archive and Maven resources
   721  	build.MustRun(goTool("get", "golang.org/x/mobile/cmd/gomobile"))
   722  	build.MustRun(gomobileTool("init", "--ndk", os.Getenv("ANDROID_NDK")))
   723  	build.MustRun(gomobileTool("bind", "--target", "android", "--javapkg", "org.ethereum", "-v", "github.com/atheioschain/go-atheios/mobile"))
   724  
   725  	if *local {
   726  		// If we're building locally, copy bundle to build dir and skip Maven
   727  		os.Rename("gath.aar", filepath.Join(GOBIN, "gath.aar"))
   728  		return
   729  	}
   730  	meta := newMavenMetadata(env)
   731  	build.Render("build/mvn.pom", meta.Package+".pom", 0755, meta)
   732  
   733  	// Skip Maven deploy and Azure upload for PR builds
   734  	maybeSkipArchive(env)
   735  
   736  	// Sign and upload the archive to Azure
   737  	archive := "gath-" + archiveBasename("android", env) + ".aar"
   738  	os.Rename("gath.aar", archive)
   739  
   740  	if err := archiveUpload(archive, *upload, *signer); err != nil {
   741  		log.Fatal(err)
   742  	}
   743  	// Sign and upload all the artifacts to Maven Central
   744  	os.Rename(archive, meta.Package+".aar")
   745  	if *signer != "" && *deploy != "" {
   746  		// Import the signing key into the local GPG instance
   747  		if b64key := os.Getenv(*signer); b64key != "" {
   748  			key, err := base64.StdEncoding.DecodeString(b64key)
   749  			if err != nil {
   750  				log.Fatalf("invalid base64 %s", *signer)
   751  			}
   752  			gpg := exec.Command("gpg", "--import")
   753  			gpg.Stdin = bytes.NewReader(key)
   754  			build.MustRun(gpg)
   755  		}
   756  		// Upload the artifacts to Sonatype and/or Maven Central
   757  		repo := *deploy + "/service/local/staging/deploy/maven2"
   758  		if meta.Develop {
   759  			repo = *deploy + "/content/repositories/snapshots"
   760  		}
   761  		build.MustRunCommand("mvn", "gpg:sign-and-deploy-file",
   762  			"-settings=build/mvn.settings", "-Durl="+repo, "-DrepositoryId=ossrh",
   763  			"-DpomFile="+meta.Package+".pom", "-Dfile="+meta.Package+".aar")
   764  	}
   765  }
   766  
   767  func gomobileTool(subcmd string, args ...string) *exec.Cmd {
   768  	cmd := exec.Command(filepath.Join(GOBIN, "gomobile"), subcmd)
   769  	cmd.Args = append(cmd.Args, args...)
   770  	cmd.Env = []string{
   771  		"GOPATH=" + build.GOPATH(),
   772  	}
   773  	for _, e := range os.Environ() {
   774  		if strings.HasPrefix(e, "GOPATH=") {
   775  			continue
   776  		}
   777  		cmd.Env = append(cmd.Env, e)
   778  	}
   779  	return cmd
   780  }
   781  
   782  type mavenMetadata struct {
   783  	Version      string
   784  	Package      string
   785  	Develop      bool
   786  	Contributors []mavenContributor
   787  }
   788  
   789  type mavenContributor struct {
   790  	Name  string
   791  	Email string
   792  }
   793  
   794  func newMavenMetadata(env build.Environment) mavenMetadata {
   795  	// Collect the list of authors from the repo root
   796  	contribs := []mavenContributor{}
   797  	if authors, err := os.Open("AUTHORS"); err == nil {
   798  		defer authors.Close()
   799  
   800  		scanner := bufio.NewScanner(authors)
   801  		for scanner.Scan() {
   802  			// Skip any whitespace from the authors list
   803  			line := strings.TrimSpace(scanner.Text())
   804  			if line == "" || line[0] == '#' {
   805  				continue
   806  			}
   807  			// Split the author and insert as a contributor
   808  			re := regexp.MustCompile("([^<]+) <(.+)>")
   809  			parts := re.FindStringSubmatch(line)
   810  			if len(parts) == 3 {
   811  				contribs = append(contribs, mavenContributor{Name: parts[1], Email: parts[2]})
   812  			}
   813  		}
   814  	}
   815  	// Render the version and package strings
   816  	version := build.VERSION()
   817  	if isUnstableBuild(env) {
   818  		version += "-SNAPSHOT"
   819  	}
   820  	return mavenMetadata{
   821  		Version:      version,
   822  		Package:      "gath-" + version,
   823  		Develop:      isUnstableBuild(env),
   824  		Contributors: contribs,
   825  	}
   826  }
   827  
   828  // XCode frameworks
   829  
   830  func doXCodeFramework(cmdline []string) {
   831  	var (
   832  		local  = flag.Bool("local", false, `Flag whether we're only doing a local build (skip Maven artifacts)`)
   833  		signer = flag.String("signer", "", `Environment variable holding the signing key (e.g. IOS_SIGNING_KEY)`)
   834  		deploy = flag.String("deploy", "", `Destination to deploy the archive (usually "trunk")`)
   835  		upload = flag.String("upload", "", `Destination to upload the archives (usually "gathstore/builds")`)
   836  	)
   837  	flag.CommandLine.Parse(cmdline)
   838  	env := build.Env()
   839  
   840  	// Build the iOS XCode framework
   841  	build.MustRun(goTool("get", "golang.org/x/mobile/cmd/gomobile"))
   842  	build.MustRun(gomobileTool("init"))
   843  	bind := gomobileTool("bind", "--target", "ios", "--tags", "ios", "-v", "github.com/atheioschain/go-atheios/mobile")
   844  
   845  	if *local {
   846  		// If we're building locally, use the build folder and stop afterwards
   847  		bind.Dir, _ = filepath.Abs(GOBIN)
   848  		build.MustRun(bind)
   849  		return
   850  	}
   851  	archive := "gath-" + archiveBasename("ios", env)
   852  	if err := os.Mkdir(archive, os.ModePerm); err != nil {
   853  		log.Fatal(err)
   854  	}
   855  	bind.Dir, _ = filepath.Abs(archive)
   856  	build.MustRun(bind)
   857  	build.MustRunCommand("tar", "-zcvf", archive+".tar.gz", archive)
   858  
   859  	// Skip CocoaPods deploy and Azure upload for PR builds
   860  	maybeSkipArchive(env)
   861  
   862  	// Sign and upload the framework to Azure
   863  	if err := archiveUpload(archive+".tar.gz", *upload, *signer); err != nil {
   864  		log.Fatal(err)
   865  	}
   866  	// Prepare and upload a PodSpec to CocoaPods
   867  	if *deploy != "" {
   868  		meta := newPodMetadata(env, archive)
   869  		build.Render("build/pod.podspec", "gath.podspec", 0755, meta)
   870  		build.MustRunCommand("pod", *deploy, "push", "gath.podspec", "--allow-warnings", "--verbose")
   871  	}
   872  }
   873  
   874  type podMetadata struct {
   875  	Version      string
   876  	Commit       string
   877  	Archive      string
   878  	Contributors []podContributor
   879  }
   880  
   881  type podContributor struct {
   882  	Name  string
   883  	Email string
   884  }
   885  
   886  func newPodMetadata(env build.Environment, archive string) podMetadata {
   887  	// Collect the list of authors from the repo root
   888  	contribs := []podContributor{}
   889  	if authors, err := os.Open("AUTHORS"); err == nil {
   890  		defer authors.Close()
   891  
   892  		scanner := bufio.NewScanner(authors)
   893  		for scanner.Scan() {
   894  			// Skip any whitespace from the authors list
   895  			line := strings.TrimSpace(scanner.Text())
   896  			if line == "" || line[0] == '#' {
   897  				continue
   898  			}
   899  			// Split the author and insert as a contributor
   900  			re := regexp.MustCompile("([^<]+) <(.+)>")
   901  			parts := re.FindStringSubmatch(line)
   902  			if len(parts) == 3 {
   903  				contribs = append(contribs, podContributor{Name: parts[1], Email: parts[2]})
   904  			}
   905  		}
   906  	}
   907  	version := build.VERSION()
   908  	if isUnstableBuild(env) {
   909  		version += "-unstable." + env.Buildnum
   910  	}
   911  	return podMetadata{
   912  		Archive:      archive,
   913  		Version:      version,
   914  		Commit:       env.Commit,
   915  		Contributors: contribs,
   916  	}
   917  }
   918  
   919  // Cross compilation
   920  
   921  func doXgo(cmdline []string) {
   922  	flag.CommandLine.Parse(cmdline)
   923  	env := build.Env()
   924  
   925  	// Make sure xgo is available for cross compilation
   926  	gogetxgo := goTool("get", "github.com/karalabe/xgo")
   927  	build.MustRun(gogetxgo)
   928  
   929  	// Execute the actual cross compilation
   930  	xgo := xgoTool(append(buildFlags(env), flag.Args()...))
   931  	build.MustRun(xgo)
   932  }
   933  
   934  func xgoTool(args []string) *exec.Cmd {
   935  	cmd := exec.Command(filepath.Join(GOBIN, "xgo"), args...)
   936  	cmd.Env = []string{
   937  		"GOPATH=" + build.GOPATH(),
   938  		"GOBIN=" + GOBIN,
   939  	}
   940  	for _, e := range os.Environ() {
   941  		if strings.HasPrefix(e, "GOPATH=") || strings.HasPrefix(e, "GOBIN=") {
   942  			continue
   943  		}
   944  		cmd.Env = append(cmd.Env, e)
   945  	}
   946  	return cmd
   947  }