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