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