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