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