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