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