github.com/FUSIONFoundation/efsn@v3.6.2-0.20200916075423-dbb5dd5d2cc7+incompatible/build/ci.go (about)

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