2016-05-25 07:07:57 -05:00
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// +build none
/ *
The ci command is called from Continuous Integration scripts .
Usage : go run ci . go < command > < command flags / arguments >
Available commands are :
2016-11-02 12:36:48 -05:00
install [ - arch architecture ] [ packages ... ] -- builds packages and executables
test [ - coverage ] [ - vet ] [ packages ... ] -- runs the tests
archive [ - arch architecture ] [ - type zip | tar ] [ - signer key - envvar ] [ - upload dest ] -- archives build artefacts
importkeys -- imports signing keys from env
debsrc [ - signer key - id ] [ - upload dest ] -- creates a debian source package
xgo [ options ] -- cross builds according to options
2016-05-25 07:07:57 -05:00
For all commands , - n prevents execution of external programs ( dry run mode ) .
* /
package main
import (
"bytes"
"encoding/base64"
"flag"
"fmt"
2016-11-02 12:36:48 -05:00
"go/parser"
"go/token"
2016-05-25 07:07:57 -05:00
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
2016-11-02 11:21:05 -05:00
"github.com/ethereum/go-ethereum/internal/build"
2016-05-25 07:07:57 -05:00
)
var (
// Files that end up in the geth*.zip archive.
gethArchiveFiles = [ ] string {
"COPYING" ,
executablePath ( "geth" ) ,
}
// Files that end up in the geth-alltools*.zip archive.
allToolsArchiveFiles = [ ] string {
"COPYING" ,
executablePath ( "abigen" ) ,
executablePath ( "evm" ) ,
executablePath ( "geth" ) ,
executablePath ( "rlpdump" ) ,
}
// A debian package is created for all executables listed here.
debExecutables = [ ] debExecutable {
{
Name : "geth" ,
Description : "Ethereum CLI client." ,
} ,
{
Name : "rlpdump" ,
Description : "Developer utility tool that prints RLP structures." ,
} ,
{
Name : "evm" ,
Description : "Developer utility version of the EVM (Ethereum Virtual Machine) that is capable of running bytecode snippets within a configurable environment and execution mode." ,
} ,
{
Name : "abigen" ,
Description : "Source code generator to convert Ethereum contract definitions into easy to use, compile-time type-safe Go packages." ,
} ,
}
// Distros for which packages are created.
// Note: vivid is unsupported because there is no golang-1.6 package for it.
debDistros = [ ] string { "trusty" , "wily" , "xenial" , "yakkety" }
)
var GOBIN , _ = filepath . Abs ( filepath . Join ( "build" , "bin" ) )
func executablePath ( name string ) string {
if runtime . GOOS == "windows" {
name += ".exe"
}
return filepath . Join ( GOBIN , name )
}
func main ( ) {
log . SetFlags ( log . Lshortfile )
if _ , err := os . Stat ( filepath . Join ( "build" , "ci.go" ) ) ; os . IsNotExist ( err ) {
log . Fatal ( "this script must be run from the root of the repository" )
}
if len ( os . Args ) < 2 {
log . Fatal ( "need subcommand as first argument" )
}
switch os . Args [ 1 ] {
case "install" :
doInstall ( os . Args [ 2 : ] )
case "test" :
doTest ( os . Args [ 2 : ] )
case "archive" :
doArchive ( os . Args [ 2 : ] )
case "debsrc" :
doDebianSource ( os . Args [ 2 : ] )
2016-08-08 05:41:55 -05:00
case "xgo" :
doXgo ( os . Args [ 2 : ] )
2016-05-25 07:07:57 -05:00
default :
log . Fatal ( "unknown command " , os . Args [ 1 ] )
}
}
// Compiling
func doInstall ( cmdline [ ] string ) {
2016-11-02 12:36:48 -05:00
var (
arch = flag . String ( "arch" , "" , "Architecture to cross build for" )
)
2016-05-25 07:07:57 -05:00
flag . CommandLine . Parse ( cmdline )
2016-09-30 15:03:08 -05:00
env := build . Env ( )
2016-05-25 07:07:57 -05:00
// Check Go version. People regularly open issues about compilation
// failure with outdated Go. This should save them the trouble.
if runtime . Version ( ) < "go1.4" && ! strings . HasPrefix ( runtime . Version ( ) , "devel" ) {
log . Println ( "You have Go version" , runtime . Version ( ) )
log . Println ( "go-ethereum requires at least Go version 1.4 and cannot" )
log . Println ( "be compiled with an earlier version. Please upgrade your Go installation." )
os . Exit ( 1 )
}
// Compile packages given as arguments, or everything if there are no arguments.
packages := [ ] string { "./..." }
if flag . NArg ( ) > 0 {
packages = flag . Args ( )
}
2016-11-02 12:36:48 -05:00
if * arch == "" || * arch == runtime . GOARCH {
goinstall := goTool ( "install" , buildFlags ( env ) ... )
goinstall . Args = append ( goinstall . Args , "-v" )
goinstall . Args = append ( goinstall . Args , packages ... )
build . MustRun ( goinstall )
return
}
// Seems we are cross compiling, work around forbidden GOBIN
goinstall := goToolArch ( * arch , "install" , buildFlags ( env ) ... )
2016-05-25 07:07:57 -05:00
goinstall . Args = append ( goinstall . Args , "-v" )
2016-11-02 12:36:48 -05:00
goinstall . Args = append ( goinstall . Args , [ ] string { "-buildmode" , "archive" } ... )
2016-05-25 07:07:57 -05:00
goinstall . Args = append ( goinstall . Args , packages ... )
build . MustRun ( goinstall )
2016-11-02 12:36:48 -05:00
if cmds , err := ioutil . ReadDir ( "cmd" ) ; err == nil {
for _ , cmd := range cmds {
pkgs , err := parser . ParseDir ( token . NewFileSet ( ) , filepath . Join ( "." , "cmd" , cmd . Name ( ) ) , nil , parser . PackageClauseOnly )
if err != nil {
log . Fatal ( err )
}
for name , _ := range pkgs {
if name == "main" {
gobuild := goToolArch ( * arch , "build" , buildFlags ( env ) ... )
gobuild . Args = append ( gobuild . Args , "-v" )
gobuild . Args = append ( gobuild . Args , [ ] string { "-o" , filepath . Join ( GOBIN , cmd . Name ( ) ) } ... )
gobuild . Args = append ( gobuild . Args , "." + string ( filepath . Separator ) + filepath . Join ( "cmd" , cmd . Name ( ) ) )
build . MustRun ( gobuild )
break
}
}
}
}
2016-05-25 07:07:57 -05:00
}
2016-09-30 15:03:08 -05:00
func buildFlags ( env build . Environment ) ( flags [ ] string ) {
if os . Getenv ( "GO_OPENCL" ) != "" {
flags = append ( flags , "-tags" , "opencl" )
}
2016-05-25 07:07:57 -05:00
// Since Go 1.5, the separator char for link time assignments
// is '=' and using ' ' prints a warning. However, Go < 1.5 does
// not support using '='.
sep := " "
if runtime . Version ( ) > "go1.5" || strings . Contains ( runtime . Version ( ) , "devel" ) {
sep = "="
}
2016-09-30 15:03:08 -05:00
// Set gitCommit constant via link-time assignment.
if env . Commit != "" {
flags = append ( flags , "-ldflags" , "-X main.gitCommit" + sep + env . Commit )
2016-05-25 07:07:57 -05:00
}
return flags
}
func goTool ( subcmd string , args ... string ) * exec . Cmd {
2016-11-02 12:36:48 -05:00
return goToolArch ( runtime . GOARCH , subcmd , args ... )
}
func goToolArch ( arch string , subcmd string , args ... string ) * exec . Cmd {
2016-05-25 07:07:57 -05:00
gocmd := filepath . Join ( runtime . GOROOT ( ) , "bin" , "go" )
cmd := exec . Command ( gocmd , subcmd )
cmd . Args = append ( cmd . Args , args ... )
cmd . Env = [ ] string {
2016-10-28 12:05:01 -05:00
"GO15VENDOREXPERIMENT=1" ,
2016-05-25 07:07:57 -05:00
"GOPATH=" + build . GOPATH ( ) ,
2016-11-02 12:36:48 -05:00
}
if arch == "" || arch == runtime . GOARCH {
cmd . Env = append ( cmd . Env , "GOBIN=" + GOBIN )
} else {
cmd . Env = append ( cmd . Env , "CGO_ENABLED=1" )
cmd . Env = append ( cmd . Env , "GOARCH=" + arch )
2016-05-25 07:07:57 -05:00
}
for _ , e := range os . Environ ( ) {
if strings . HasPrefix ( e , "GOPATH=" ) || strings . HasPrefix ( e , "GOBIN=" ) {
continue
}
cmd . Env = append ( cmd . Env , e )
}
return cmd
}
// Running The Tests
//
// "tests" also includes static analysis tools such as vet.
func doTest ( cmdline [ ] string ) {
var (
vet = flag . Bool ( "vet" , false , "Whether to run go vet" )
coverage = flag . Bool ( "coverage" , false , "Whether to record code coverage" )
)
flag . CommandLine . Parse ( cmdline )
2016-10-28 12:05:01 -05:00
2016-05-25 07:07:57 -05:00
packages := [ ] string { "./..." }
if len ( flag . CommandLine . Args ( ) ) > 0 {
packages = flag . CommandLine . Args ( )
}
2016-10-28 12:05:01 -05:00
if len ( packages ) == 1 && packages [ 0 ] == "./..." {
// Resolve ./... manually since go vet will fail on vendored stuff
out , err := goTool ( "list" , "./..." ) . CombinedOutput ( )
if err != nil {
log . Fatalf ( "package listing failed: %v\n%s" , err , string ( out ) )
}
packages = [ ] string { }
for _ , line := range strings . Split ( string ( out ) , "\n" ) {
if ! strings . Contains ( line , "vendor" ) {
packages = append ( packages , strings . TrimSpace ( line ) )
}
}
}
2016-05-25 07:07:57 -05:00
// Run analysis tools before the tests.
if * vet {
build . MustRun ( goTool ( "vet" , packages ... ) )
}
// Run the actual tests.
gotest := goTool ( "test" )
2016-09-26 06:41:18 -05:00
// Test a single package at a time. CI builders are slow
// and some tests run into timeouts under load.
gotest . Args = append ( gotest . Args , "-p" , "1" )
2016-05-25 07:07:57 -05:00
if * coverage {
gotest . Args = append ( gotest . Args , "-covermode=atomic" , "-cover" )
}
gotest . Args = append ( gotest . Args , packages ... )
build . MustRun ( gotest )
}
// Release Packaging
func doArchive ( cmdline [ ] string ) {
var (
2016-11-02 12:36:48 -05:00
arch = flag . String ( "arch" , runtime . GOARCH , "Architecture cross packaging" )
2016-11-02 11:21:05 -05:00
atype = flag . String ( "type" , "zip" , "Type of archive to write (zip|tar)" )
signer = flag . String ( "signer" , "" , ` Environment variable holding the signing key (e.g. LINUX_SIGNING_KEY) ` )
upload = flag . String ( "upload" , "" , ` Destination to upload the archives (usually "gethstore/builds") ` )
ext string
2016-05-25 07:07:57 -05:00
)
flag . CommandLine . Parse ( cmdline )
switch * atype {
case "zip" :
ext = ".zip"
case "tar" :
ext = ".tar.gz"
default :
log . Fatal ( "unknown archive type: " , atype )
}
2016-09-30 15:03:08 -05:00
2016-11-03 07:44:16 -05:00
var (
env = build . Env ( )
base = archiveBasename ( * arch , env )
geth = "geth-" + base + ext
alltools = "geth-alltools-" + base + ext
)
2016-09-30 15:03:08 -05:00
maybeSkipArchive ( env )
2016-11-03 07:44:16 -05:00
if err := build . WriteArchive ( geth , gethArchiveFiles ) ; err != nil {
2016-05-25 07:07:57 -05:00
log . Fatal ( err )
}
2016-11-03 07:44:16 -05:00
if err := build . WriteArchive ( alltools , allToolsArchiveFiles ) ; err != nil {
2016-05-25 07:07:57 -05:00
log . Fatal ( err )
}
2016-11-03 07:44:16 -05:00
for _ , archive := range [ ] string { geth , alltools } {
2016-11-02 11:21:05 -05:00
if err := archiveUpload ( archive , * upload , * signer ) ; err != nil {
log . Fatal ( err )
}
}
2016-05-25 07:07:57 -05:00
}
2016-11-02 12:36:48 -05:00
func archiveBasename ( arch string , env build . Environment ) string {
platform := runtime . GOOS + "-" + arch
2016-05-25 07:07:57 -05:00
archive := platform + "-" + build . VERSION ( )
2016-11-03 07:44:16 -05:00
if isUnstableBuild ( env ) {
archive += "-unstable"
}
2016-09-30 15:03:08 -05:00
if env . Commit != "" {
archive += "-" + env . Commit [ : 8 ]
2016-05-25 07:07:57 -05:00
}
return archive
}
2016-11-02 11:21:05 -05:00
func archiveUpload ( archive string , blobstore string , signer string ) error {
// If signing was requested, generate the signature files
if signer != "" {
pgpkey , err := base64 . StdEncoding . DecodeString ( os . Getenv ( signer ) )
if err != nil {
return fmt . Errorf ( "invalid base64 %s" , signer )
}
if err := build . PGPSignFile ( archive , archive + ".asc" , string ( pgpkey ) ) ; err != nil {
return err
}
}
// If uploading to Azure was requested, push the archive possibly with its signature
if blobstore != "" {
auth := build . AzureBlobstoreConfig {
Account : strings . Split ( blobstore , "/" ) [ 0 ] ,
Token : os . Getenv ( "AZURE_BLOBSTORE_TOKEN" ) ,
Container : strings . SplitN ( blobstore , "/" , 2 ) [ 1 ] ,
}
if err := build . AzureBlobstoreUpload ( archive , archive , auth ) ; err != nil {
return err
}
if signer != "" {
if err := build . AzureBlobstoreUpload ( archive + ".asc" , archive + ".asc" , auth ) ; err != nil {
return err
}
}
}
return nil
}
2016-09-30 15:03:08 -05:00
// skips archiving for some build configurations.
func maybeSkipArchive ( env build . Environment ) {
if env . IsPullRequest {
log . Printf ( "skipping because this is a PR build" )
os . Exit ( 0 )
}
if env . Branch != "develop" && ! strings . HasPrefix ( env . Tag , "v1." ) {
log . Printf ( "skipping because branch %q, tag %q is not on the whitelist" , env . Branch , env . Tag )
os . Exit ( 0 )
}
}
2016-05-25 07:07:57 -05:00
// Debian Packaging
2016-09-30 15:03:08 -05:00
func doDebianSource ( cmdline [ ] string ) {
var (
signer = flag . String ( "signer" , "" , ` Signing key name, also used as package author ` )
upload = flag . String ( "upload" , "" , ` Where to upload the source package (usually "ppa:ethereum/ethereum") ` )
workdir = flag . String ( "workdir" , "" , ` Output directory for packages (uses temp dir if unset) ` )
now = time . Now ( )
)
2016-05-25 07:07:57 -05:00
flag . CommandLine . Parse ( cmdline )
2016-09-30 15:03:08 -05:00
* workdir = makeWorkdir ( * workdir )
env := build . Env ( )
maybeSkipArchive ( env )
2016-05-25 07:07:57 -05:00
// Import the signing key.
if b64key := os . Getenv ( "PPA_SIGNING_KEY" ) ; b64key != "" {
key , err := base64 . StdEncoding . DecodeString ( b64key )
if err != nil {
log . Fatal ( "invalid base64 PPA_SIGNING_KEY" )
}
gpg := exec . Command ( "gpg" , "--import" )
gpg . Stdin = bytes . NewReader ( key )
build . MustRun ( gpg )
}
2016-09-30 15:03:08 -05:00
// Create the packages.
2016-05-25 07:07:57 -05:00
for _ , distro := range debDistros {
2016-09-30 15:03:08 -05:00
meta := newDebMetadata ( distro , * signer , env , now )
pkgdir := stageDebianSource ( * workdir , meta )
2016-05-25 07:07:57 -05:00
debuild := exec . Command ( "debuild" , "-S" , "-sa" , "-us" , "-uc" )
debuild . Dir = pkgdir
build . MustRun ( debuild )
changes := fmt . Sprintf ( "%s_%s_source.changes" , meta . Name ( ) , meta . VersionString ( ) )
2016-09-30 15:03:08 -05:00
changes = filepath . Join ( * workdir , changes )
2016-05-25 07:07:57 -05:00
if * signer != "" {
build . MustRunCommand ( "debsign" , changes )
}
if * upload != "" {
build . MustRunCommand ( "dput" , * upload , changes )
}
}
}
2016-09-30 15:03:08 -05:00
func makeWorkdir ( wdflag string ) string {
var err error
if wdflag != "" {
err = os . MkdirAll ( wdflag , 0744 )
} else {
wdflag , err = ioutil . TempDir ( "" , "eth-deb-build-" )
}
if err != nil {
log . Fatal ( err )
}
return wdflag
}
func isUnstableBuild ( env build . Environment ) bool {
if env . Branch != "develop" && env . Tag != "" {
return false
}
return true
2016-05-25 07:07:57 -05:00
}
type debMetadata struct {
2016-09-30 15:03:08 -05:00
Env build . Environment
2016-05-25 07:07:57 -05:00
// go-ethereum version being built. Note that this
// is not the debian package version. The package version
// is constructed by VersionString.
Version string
2016-09-30 15:03:08 -05:00
Author string // "name <email>", also selects signing key
Distro , Time string
Executables [ ] debExecutable
2016-05-25 07:07:57 -05:00
}
2016-09-30 15:03:08 -05:00
type debExecutable struct {
Name , Description string
}
func newDebMetadata ( distro , author string , env build . Environment , t time . Time ) debMetadata {
2016-05-25 07:07:57 -05:00
if author == "" {
// No signing key, use default author.
author = "Ethereum Builds <fjl@ethereum.org>"
}
return debMetadata {
2016-09-30 15:03:08 -05:00
Env : env ,
2016-05-25 07:07:57 -05:00
Author : author ,
Distro : distro ,
Version : build . VERSION ( ) ,
Time : t . Format ( time . RFC1123Z ) ,
Executables : debExecutables ,
}
}
// Name returns the name of the metapackage that depends
// on all executable packages.
func ( meta debMetadata ) Name ( ) string {
2016-09-30 15:03:08 -05:00
if isUnstableBuild ( meta . Env ) {
2016-05-25 07:07:57 -05:00
return "ethereum-unstable"
}
return "ethereum"
}
// VersionString returns the debian version of the packages.
func ( meta debMetadata ) VersionString ( ) string {
vsn := meta . Version
2016-09-30 15:03:08 -05:00
if meta . Env . Buildnum != "" {
vsn += "+build" + meta . Env . Buildnum
2016-05-25 07:07:57 -05:00
}
if meta . Distro != "" {
vsn += "+" + meta . Distro
}
return vsn
}
// ExeList returns the list of all executable packages.
func ( meta debMetadata ) ExeList ( ) string {
names := make ( [ ] string , len ( meta . Executables ) )
for i , e := range meta . Executables {
names [ i ] = meta . ExeName ( e )
}
return strings . Join ( names , ", " )
}
// ExeName returns the package name of an executable package.
func ( meta debMetadata ) ExeName ( exe debExecutable ) string {
2016-09-30 15:03:08 -05:00
if isUnstableBuild ( meta . Env ) {
2016-05-25 07:07:57 -05:00
return exe . Name + "-unstable"
}
return exe . Name
}
// ExeConflicts returns the content of the Conflicts field
// for executable packages.
func ( meta debMetadata ) ExeConflicts ( exe debExecutable ) string {
2016-09-30 15:03:08 -05:00
if isUnstableBuild ( meta . Env ) {
2016-05-25 07:07:57 -05:00
// Set up the conflicts list so that the *-unstable packages
// cannot be installed alongside the regular version.
//
// https://www.debian.org/doc/debian-policy/ch-relationships.html
// is very explicit about Conflicts: and says that Breaks: should
// be preferred and the conflicting files should be handled via
// alternates. We might do this eventually but using a conflict is
// easier now.
return "ethereum, " + exe . Name
}
return ""
}
func stageDebianSource ( tmpdir string , meta debMetadata ) ( pkgdir string ) {
pkg := meta . Name ( ) + "-" + meta . VersionString ( )
pkgdir = filepath . Join ( tmpdir , pkg )
if err := os . Mkdir ( pkgdir , 0755 ) ; err != nil {
log . Fatal ( err )
}
// Copy the source code.
build . MustRunCommand ( "git" , "checkout-index" , "-a" , "--prefix" , pkgdir + string ( filepath . Separator ) )
// Put the debian build files in place.
debian := filepath . Join ( pkgdir , "debian" )
build . Render ( "build/deb.rules" , filepath . Join ( debian , "rules" ) , 0755 , meta )
build . Render ( "build/deb.changelog" , filepath . Join ( debian , "changelog" ) , 0644 , meta )
build . Render ( "build/deb.control" , filepath . Join ( debian , "control" ) , 0644 , meta )
build . Render ( "build/deb.copyright" , filepath . Join ( debian , "copyright" ) , 0644 , meta )
build . RenderString ( "8\n" , filepath . Join ( debian , "compat" ) , 0644 , meta )
build . RenderString ( "3.0 (native)\n" , filepath . Join ( debian , "source/format" ) , 0644 , meta )
for _ , exe := range meta . Executables {
2016-09-30 15:03:08 -05:00
install := filepath . Join ( debian , meta . ExeName ( exe ) + ".install" )
docs := filepath . Join ( debian , meta . ExeName ( exe ) + ".docs" )
2016-05-25 07:07:57 -05:00
build . Render ( "build/deb.install" , install , 0644 , exe )
build . Render ( "build/deb.docs" , docs , 0644 , exe )
}
return pkgdir
}
2016-08-08 05:41:55 -05:00
// Cross compilation
func doXgo ( cmdline [ ] string ) {
2016-09-30 15:03:08 -05:00
flag . CommandLine . Parse ( cmdline )
env := build . Env ( )
2016-08-08 05:41:55 -05:00
// Make sure xgo is available for cross compilation
gogetxgo := goTool ( "get" , "github.com/karalabe/xgo" )
build . MustRun ( gogetxgo )
// Execute the actual cross compilation
2016-09-30 15:03:08 -05:00
xgo := xgoTool ( append ( buildFlags ( env ) , flag . Args ( ) ... ) )
build . MustRun ( xgo )
2016-08-08 05:41:55 -05:00
}
2016-09-30 15:03:08 -05:00
func xgoTool ( args [ ] string ) * exec . Cmd {
2016-08-08 05:41:55 -05:00
cmd := exec . Command ( filepath . Join ( GOBIN , "xgo" ) , args ... )
cmd . Env = [ ] string {
"GOPATH=" + build . GOPATH ( ) ,
"GOBIN=" + GOBIN ,
}
for _ , e := range os . Environ ( ) {
if strings . HasPrefix ( e , "GOPATH=" ) || strings . HasPrefix ( e , "GOBIN=" ) {
continue
}
cmd . Env = append ( cmd . Env , e )
}
return cmd
}