ci.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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. // +build none
  17. /*
  18. The ci command is called from Continuous Integration scripts.
  19. Usage: go run ci.go <command> <command flags/arguments>
  20. Available commands are:
  21. install [-arch architecture] [ packages... ] -- builds packages and executables
  22. test [ -coverage ] [ -vet ] [ packages... ] -- runs the tests
  23. archive [-arch architecture] [ -type zip|tar ] [ -signer key-envvar ] [ -upload dest ] -- archives build artefacts
  24. importkeys -- imports signing keys from env
  25. debsrc [ -signer key-id ] [ -upload dest ] -- creates a debian source package
  26. nsis -- creates a Windows NSIS installer
  27. aar [ -sign key-id ] [ -upload dest ] -- creates an android archive
  28. xgo [ options ] -- cross builds according to options
  29. For all commands, -n prevents execution of external programs (dry run mode).
  30. */
  31. package main
  32. import (
  33. "bufio"
  34. "bytes"
  35. "encoding/base64"
  36. "flag"
  37. "fmt"
  38. "go/parser"
  39. "go/token"
  40. "io/ioutil"
  41. "log"
  42. "os"
  43. "os/exec"
  44. "path/filepath"
  45. "regexp"
  46. "runtime"
  47. "strings"
  48. "time"
  49. "github.com/ethereum/go-ethereum/internal/build"
  50. )
  51. var (
  52. // Files that end up in the geth*.zip archive.
  53. gethArchiveFiles = []string{
  54. "COPYING",
  55. executablePath("geth"),
  56. }
  57. // Files that end up in the geth-alltools*.zip archive.
  58. allToolsArchiveFiles = []string{
  59. "COPYING",
  60. executablePath("abigen"),
  61. executablePath("evm"),
  62. executablePath("geth"),
  63. executablePath("rlpdump"),
  64. }
  65. // A debian package is created for all executables listed here.
  66. debExecutables = []debExecutable{
  67. {
  68. Name: "geth",
  69. Description: "Ethereum CLI client.",
  70. },
  71. {
  72. Name: "rlpdump",
  73. Description: "Developer utility tool that prints RLP structures.",
  74. },
  75. {
  76. Name: "evm",
  77. Description: "Developer utility version of the EVM (Ethereum Virtual Machine) that is capable of running bytecode snippets within a configurable environment and execution mode.",
  78. },
  79. {
  80. Name: "abigen",
  81. Description: "Source code generator to convert Ethereum contract definitions into easy to use, compile-time type-safe Go packages.",
  82. },
  83. }
  84. // Distros for which packages are created.
  85. // Note: vivid is unsupported because there is no golang-1.6 package for it.
  86. debDistros = []string{"trusty", "wily", "xenial", "yakkety"}
  87. )
  88. var GOBIN, _ = filepath.Abs(filepath.Join("build", "bin"))
  89. func executablePath(name string) string {
  90. if runtime.GOOS == "windows" {
  91. name += ".exe"
  92. }
  93. return filepath.Join(GOBIN, name)
  94. }
  95. func main() {
  96. log.SetFlags(log.Lshortfile)
  97. if _, err := os.Stat(filepath.Join("build", "ci.go")); os.IsNotExist(err) {
  98. log.Fatal("this script must be run from the root of the repository")
  99. }
  100. if len(os.Args) < 2 {
  101. log.Fatal("need subcommand as first argument")
  102. }
  103. switch os.Args[1] {
  104. case "install":
  105. doInstall(os.Args[2:])
  106. case "test":
  107. doTest(os.Args[2:])
  108. case "archive":
  109. doArchive(os.Args[2:])
  110. case "debsrc":
  111. doDebianSource(os.Args[2:])
  112. case "nsis":
  113. doWindowsInstaller(os.Args[2:])
  114. case "aar":
  115. doAndroidArchive(os.Args[2:])
  116. case "xgo":
  117. doXgo(os.Args[2:])
  118. default:
  119. log.Fatal("unknown command ", os.Args[1])
  120. }
  121. }
  122. // Compiling
  123. func doInstall(cmdline []string) {
  124. var (
  125. arch = flag.String("arch", "", "Architecture to cross build for")
  126. )
  127. flag.CommandLine.Parse(cmdline)
  128. env := build.Env()
  129. // Check Go version. People regularly open issues about compilation
  130. // failure with outdated Go. This should save them the trouble.
  131. if runtime.Version() < "go1.4" && !strings.HasPrefix(runtime.Version(), "devel") {
  132. log.Println("You have Go version", runtime.Version())
  133. log.Println("go-ethereum requires at least Go version 1.4 and cannot")
  134. log.Println("be compiled with an earlier version. Please upgrade your Go installation.")
  135. os.Exit(1)
  136. }
  137. // Compile packages given as arguments, or everything if there are no arguments.
  138. packages := []string{"./..."}
  139. if flag.NArg() > 0 {
  140. packages = flag.Args()
  141. }
  142. if *arch == "" || *arch == runtime.GOARCH {
  143. goinstall := goTool("install", buildFlags(env)...)
  144. goinstall.Args = append(goinstall.Args, "-v")
  145. goinstall.Args = append(goinstall.Args, packages...)
  146. build.MustRun(goinstall)
  147. return
  148. }
  149. // If we are cross compiling to ARMv5 ARMv6 or ARMv7, clean any prvious builds
  150. if *arch == "arm" {
  151. os.RemoveAll(filepath.Join(runtime.GOROOT(), "pkg", runtime.GOOS+"_arm"))
  152. for _, path := range filepath.SplitList(build.GOPATH()) {
  153. os.RemoveAll(filepath.Join(path, "pkg", runtime.GOOS+"_arm"))
  154. }
  155. }
  156. // Seems we are cross compiling, work around forbidden GOBIN
  157. goinstall := goToolArch(*arch, "install", buildFlags(env)...)
  158. goinstall.Args = append(goinstall.Args, "-v")
  159. goinstall.Args = append(goinstall.Args, []string{"-buildmode", "archive"}...)
  160. goinstall.Args = append(goinstall.Args, packages...)
  161. build.MustRun(goinstall)
  162. if cmds, err := ioutil.ReadDir("cmd"); err == nil {
  163. for _, cmd := range cmds {
  164. pkgs, err := parser.ParseDir(token.NewFileSet(), filepath.Join(".", "cmd", cmd.Name()), nil, parser.PackageClauseOnly)
  165. if err != nil {
  166. log.Fatal(err)
  167. }
  168. for name, _ := range pkgs {
  169. if name == "main" {
  170. gobuild := goToolArch(*arch, "build", buildFlags(env)...)
  171. gobuild.Args = append(gobuild.Args, "-v")
  172. gobuild.Args = append(gobuild.Args, []string{"-o", executablePath(cmd.Name())}...)
  173. gobuild.Args = append(gobuild.Args, "."+string(filepath.Separator)+filepath.Join("cmd", cmd.Name()))
  174. build.MustRun(gobuild)
  175. break
  176. }
  177. }
  178. }
  179. }
  180. }
  181. func buildFlags(env build.Environment) (flags []string) {
  182. if os.Getenv("GO_OPENCL") != "" {
  183. flags = append(flags, "-tags", "opencl")
  184. }
  185. // Since Go 1.5, the separator char for link time assignments
  186. // is '=' and using ' ' prints a warning. However, Go < 1.5 does
  187. // not support using '='.
  188. sep := " "
  189. if runtime.Version() > "go1.5" || strings.Contains(runtime.Version(), "devel") {
  190. sep = "="
  191. }
  192. // Set gitCommit constant via link-time assignment.
  193. if env.Commit != "" {
  194. flags = append(flags, "-ldflags", "-X main.gitCommit"+sep+env.Commit)
  195. }
  196. return flags
  197. }
  198. func goTool(subcmd string, args ...string) *exec.Cmd {
  199. return goToolArch(runtime.GOARCH, subcmd, args...)
  200. }
  201. func goToolArch(arch string, subcmd string, args ...string) *exec.Cmd {
  202. gocmd := filepath.Join(runtime.GOROOT(), "bin", "go")
  203. cmd := exec.Command(gocmd, subcmd)
  204. cmd.Args = append(cmd.Args, args...)
  205. cmd.Env = []string{
  206. "GO15VENDOREXPERIMENT=1",
  207. "GOPATH=" + build.GOPATH(),
  208. }
  209. if arch == "" || arch == runtime.GOARCH {
  210. cmd.Env = append(cmd.Env, "GOBIN="+GOBIN)
  211. } else {
  212. cmd.Env = append(cmd.Env, "CGO_ENABLED=1")
  213. cmd.Env = append(cmd.Env, "GOARCH="+arch)
  214. }
  215. for _, e := range os.Environ() {
  216. if strings.HasPrefix(e, "GOPATH=") || strings.HasPrefix(e, "GOBIN=") {
  217. continue
  218. }
  219. cmd.Env = append(cmd.Env, e)
  220. }
  221. return cmd
  222. }
  223. // Running The Tests
  224. //
  225. // "tests" also includes static analysis tools such as vet.
  226. func doTest(cmdline []string) {
  227. var (
  228. vet = flag.Bool("vet", false, "Whether to run go vet")
  229. coverage = flag.Bool("coverage", false, "Whether to record code coverage")
  230. )
  231. flag.CommandLine.Parse(cmdline)
  232. packages := []string{"./..."}
  233. if len(flag.CommandLine.Args()) > 0 {
  234. packages = flag.CommandLine.Args()
  235. }
  236. if len(packages) == 1 && packages[0] == "./..." {
  237. // Resolve ./... manually since go vet will fail on vendored stuff
  238. out, err := goTool("list", "./...").CombinedOutput()
  239. if err != nil {
  240. log.Fatalf("package listing failed: %v\n%s", err, string(out))
  241. }
  242. packages = []string{}
  243. for _, line := range strings.Split(string(out), "\n") {
  244. if !strings.Contains(line, "vendor") {
  245. packages = append(packages, strings.TrimSpace(line))
  246. }
  247. }
  248. }
  249. // Run analysis tools before the tests.
  250. if *vet {
  251. build.MustRun(goTool("vet", packages...))
  252. }
  253. // Run the actual tests.
  254. gotest := goTool("test")
  255. // Test a single package at a time. CI builders are slow
  256. // and some tests run into timeouts under load.
  257. gotest.Args = append(gotest.Args, "-p", "1")
  258. if *coverage {
  259. gotest.Args = append(gotest.Args, "-covermode=atomic", "-cover")
  260. }
  261. gotest.Args = append(gotest.Args, packages...)
  262. build.MustRun(gotest)
  263. }
  264. // Release Packaging
  265. func doArchive(cmdline []string) {
  266. var (
  267. arch = flag.String("arch", runtime.GOARCH, "Architecture cross packaging")
  268. atype = flag.String("type", "zip", "Type of archive to write (zip|tar)")
  269. signer = flag.String("signer", "", `Environment variable holding the signing key (e.g. LINUX_SIGNING_KEY)`)
  270. upload = flag.String("upload", "", `Destination to upload the archives (usually "gethstore/builds")`)
  271. ext string
  272. )
  273. flag.CommandLine.Parse(cmdline)
  274. switch *atype {
  275. case "zip":
  276. ext = ".zip"
  277. case "tar":
  278. ext = ".tar.gz"
  279. default:
  280. log.Fatal("unknown archive type: ", atype)
  281. }
  282. var (
  283. env = build.Env()
  284. base = archiveBasename(*arch, env)
  285. geth = "geth-" + base + ext
  286. alltools = "geth-alltools-" + base + ext
  287. )
  288. maybeSkipArchive(env)
  289. if err := build.WriteArchive(geth, gethArchiveFiles); err != nil {
  290. log.Fatal(err)
  291. }
  292. if err := build.WriteArchive(alltools, allToolsArchiveFiles); err != nil {
  293. log.Fatal(err)
  294. }
  295. for _, archive := range []string{geth, alltools} {
  296. if err := archiveUpload(archive, *upload, *signer); err != nil {
  297. log.Fatal(err)
  298. }
  299. }
  300. }
  301. func archiveBasename(arch string, env build.Environment) string {
  302. platform := runtime.GOOS + "-" + arch
  303. if arch == "arm" {
  304. platform += os.Getenv("GOARM")
  305. }
  306. if arch == "android" {
  307. platform = "android-all"
  308. }
  309. archive := platform + "-" + build.VERSION()
  310. if isUnstableBuild(env) {
  311. archive += "-unstable"
  312. }
  313. if env.Commit != "" {
  314. archive += "-" + env.Commit[:8]
  315. }
  316. return archive
  317. }
  318. func archiveUpload(archive string, blobstore string, signer string) error {
  319. // If signing was requested, generate the signature files
  320. if signer != "" {
  321. pgpkey, err := base64.StdEncoding.DecodeString(os.Getenv(signer))
  322. if err != nil {
  323. return fmt.Errorf("invalid base64 %s", signer)
  324. }
  325. if err := build.PGPSignFile(archive, archive+".asc", string(pgpkey)); err != nil {
  326. return err
  327. }
  328. }
  329. // If uploading to Azure was requested, push the archive possibly with its signature
  330. if blobstore != "" {
  331. auth := build.AzureBlobstoreConfig{
  332. Account: strings.Split(blobstore, "/")[0],
  333. Token: os.Getenv("AZURE_BLOBSTORE_TOKEN"),
  334. Container: strings.SplitN(blobstore, "/", 2)[1],
  335. }
  336. if err := build.AzureBlobstoreUpload(archive, filepath.Base(archive), auth); err != nil {
  337. return err
  338. }
  339. if signer != "" {
  340. if err := build.AzureBlobstoreUpload(archive+".asc", filepath.Base(archive+".asc"), auth); err != nil {
  341. return err
  342. }
  343. }
  344. }
  345. return nil
  346. }
  347. // skips archiving for some build configurations.
  348. func maybeSkipArchive(env build.Environment) {
  349. if env.IsPullRequest {
  350. log.Printf("skipping because this is a PR build")
  351. os.Exit(0)
  352. }
  353. if env.Branch != "develop" && !strings.HasPrefix(env.Tag, "v1.") {
  354. log.Printf("skipping because branch %q, tag %q is not on the whitelist", env.Branch, env.Tag)
  355. os.Exit(0)
  356. }
  357. }
  358. // Debian Packaging
  359. func doDebianSource(cmdline []string) {
  360. var (
  361. signer = flag.String("signer", "", `Signing key name, also used as package author`)
  362. upload = flag.String("upload", "", `Where to upload the source package (usually "ppa:ethereum/ethereum")`)
  363. workdir = flag.String("workdir", "", `Output directory for packages (uses temp dir if unset)`)
  364. now = time.Now()
  365. )
  366. flag.CommandLine.Parse(cmdline)
  367. *workdir = makeWorkdir(*workdir)
  368. env := build.Env()
  369. maybeSkipArchive(env)
  370. // Import the signing key.
  371. if b64key := os.Getenv("PPA_SIGNING_KEY"); b64key != "" {
  372. key, err := base64.StdEncoding.DecodeString(b64key)
  373. if err != nil {
  374. log.Fatal("invalid base64 PPA_SIGNING_KEY")
  375. }
  376. gpg := exec.Command("gpg", "--import")
  377. gpg.Stdin = bytes.NewReader(key)
  378. build.MustRun(gpg)
  379. }
  380. // Create the packages.
  381. for _, distro := range debDistros {
  382. meta := newDebMetadata(distro, *signer, env, now)
  383. pkgdir := stageDebianSource(*workdir, meta)
  384. debuild := exec.Command("debuild", "-S", "-sa", "-us", "-uc")
  385. debuild.Dir = pkgdir
  386. build.MustRun(debuild)
  387. changes := fmt.Sprintf("%s_%s_source.changes", meta.Name(), meta.VersionString())
  388. changes = filepath.Join(*workdir, changes)
  389. if *signer != "" {
  390. build.MustRunCommand("debsign", changes)
  391. }
  392. if *upload != "" {
  393. build.MustRunCommand("dput", *upload, changes)
  394. }
  395. }
  396. }
  397. func makeWorkdir(wdflag string) string {
  398. var err error
  399. if wdflag != "" {
  400. err = os.MkdirAll(wdflag, 0744)
  401. } else {
  402. wdflag, err = ioutil.TempDir("", "geth-build-")
  403. }
  404. if err != nil {
  405. log.Fatal(err)
  406. }
  407. return wdflag
  408. }
  409. func isUnstableBuild(env build.Environment) bool {
  410. if env.Branch != "develop" && env.Tag != "" {
  411. return false
  412. }
  413. return true
  414. }
  415. type debMetadata struct {
  416. Env build.Environment
  417. // go-ethereum version being built. Note that this
  418. // is not the debian package version. The package version
  419. // is constructed by VersionString.
  420. Version string
  421. Author string // "name <email>", also selects signing key
  422. Distro, Time string
  423. Executables []debExecutable
  424. }
  425. type debExecutable struct {
  426. Name, Description string
  427. }
  428. func newDebMetadata(distro, author string, env build.Environment, t time.Time) debMetadata {
  429. if author == "" {
  430. // No signing key, use default author.
  431. author = "Ethereum Builds <fjl@ethereum.org>"
  432. }
  433. return debMetadata{
  434. Env: env,
  435. Author: author,
  436. Distro: distro,
  437. Version: build.VERSION(),
  438. Time: t.Format(time.RFC1123Z),
  439. Executables: debExecutables,
  440. }
  441. }
  442. // Name returns the name of the metapackage that depends
  443. // on all executable packages.
  444. func (meta debMetadata) Name() string {
  445. if isUnstableBuild(meta.Env) {
  446. return "ethereum-unstable"
  447. }
  448. return "ethereum"
  449. }
  450. // VersionString returns the debian version of the packages.
  451. func (meta debMetadata) VersionString() string {
  452. vsn := meta.Version
  453. if meta.Env.Buildnum != "" {
  454. vsn += "+build" + meta.Env.Buildnum
  455. }
  456. if meta.Distro != "" {
  457. vsn += "+" + meta.Distro
  458. }
  459. return vsn
  460. }
  461. // ExeList returns the list of all executable packages.
  462. func (meta debMetadata) ExeList() string {
  463. names := make([]string, len(meta.Executables))
  464. for i, e := range meta.Executables {
  465. names[i] = meta.ExeName(e)
  466. }
  467. return strings.Join(names, ", ")
  468. }
  469. // ExeName returns the package name of an executable package.
  470. func (meta debMetadata) ExeName(exe debExecutable) string {
  471. if isUnstableBuild(meta.Env) {
  472. return exe.Name + "-unstable"
  473. }
  474. return exe.Name
  475. }
  476. // ExeConflicts returns the content of the Conflicts field
  477. // for executable packages.
  478. func (meta debMetadata) ExeConflicts(exe debExecutable) string {
  479. if isUnstableBuild(meta.Env) {
  480. // Set up the conflicts list so that the *-unstable packages
  481. // cannot be installed alongside the regular version.
  482. //
  483. // https://www.debian.org/doc/debian-policy/ch-relationships.html
  484. // is very explicit about Conflicts: and says that Breaks: should
  485. // be preferred and the conflicting files should be handled via
  486. // alternates. We might do this eventually but using a conflict is
  487. // easier now.
  488. return "ethereum, " + exe.Name
  489. }
  490. return ""
  491. }
  492. func stageDebianSource(tmpdir string, meta debMetadata) (pkgdir string) {
  493. pkg := meta.Name() + "-" + meta.VersionString()
  494. pkgdir = filepath.Join(tmpdir, pkg)
  495. if err := os.Mkdir(pkgdir, 0755); err != nil {
  496. log.Fatal(err)
  497. }
  498. // Copy the source code.
  499. build.MustRunCommand("git", "checkout-index", "-a", "--prefix", pkgdir+string(filepath.Separator))
  500. // Put the debian build files in place.
  501. debian := filepath.Join(pkgdir, "debian")
  502. build.Render("build/deb.rules", filepath.Join(debian, "rules"), 0755, meta)
  503. build.Render("build/deb.changelog", filepath.Join(debian, "changelog"), 0644, meta)
  504. build.Render("build/deb.control", filepath.Join(debian, "control"), 0644, meta)
  505. build.Render("build/deb.copyright", filepath.Join(debian, "copyright"), 0644, meta)
  506. build.RenderString("8\n", filepath.Join(debian, "compat"), 0644, meta)
  507. build.RenderString("3.0 (native)\n", filepath.Join(debian, "source/format"), 0644, meta)
  508. for _, exe := range meta.Executables {
  509. install := filepath.Join(debian, meta.ExeName(exe)+".install")
  510. docs := filepath.Join(debian, meta.ExeName(exe)+".docs")
  511. build.Render("build/deb.install", install, 0644, exe)
  512. build.Render("build/deb.docs", docs, 0644, exe)
  513. }
  514. return pkgdir
  515. }
  516. // Windows installer
  517. func doWindowsInstaller(cmdline []string) {
  518. // Parse the flags and make skip installer generation on PRs
  519. var (
  520. arch = flag.String("arch", runtime.GOARCH, "Architecture for cross build packaging")
  521. signer = flag.String("signer", "", `Environment variable holding the signing key (e.g. WINDOWS_SIGNING_KEY)`)
  522. upload = flag.String("upload", "", `Destination to upload the archives (usually "gethstore/builds")`)
  523. workdir = flag.String("workdir", "", `Output directory for packages (uses temp dir if unset)`)
  524. )
  525. flag.CommandLine.Parse(cmdline)
  526. *workdir = makeWorkdir(*workdir)
  527. env := build.Env()
  528. maybeSkipArchive(env)
  529. // Aggregate binaries that are included in the installer
  530. var (
  531. devTools []string
  532. allTools []string
  533. gethTool string
  534. )
  535. for _, file := range allToolsArchiveFiles {
  536. if file == "COPYING" { // license, copied later
  537. continue
  538. }
  539. allTools = append(allTools, filepath.Base(file))
  540. if filepath.Base(file) == "geth.exe" {
  541. gethTool = file
  542. } else {
  543. devTools = append(devTools, file)
  544. }
  545. }
  546. // Render NSIS scripts: Installer NSIS contains two installer sections,
  547. // first section contains the geth binary, second section holds the dev tools.
  548. templateData := map[string]interface{}{
  549. "License": "COPYING",
  550. "Geth": gethTool,
  551. "DevTools": devTools,
  552. }
  553. build.Render("build/nsis.geth.nsi", filepath.Join(*workdir, "geth.nsi"), 0644, nil)
  554. build.Render("build/nsis.install.nsh", filepath.Join(*workdir, "install.nsh"), 0644, templateData)
  555. build.Render("build/nsis.uninstall.nsh", filepath.Join(*workdir, "uninstall.nsh"), 0644, allTools)
  556. build.Render("build/nsis.envvarupdate.nsh", filepath.Join(*workdir, "EnvVarUpdate.nsh"), 0644, nil)
  557. build.CopyFile(filepath.Join(*workdir, "SimpleFC.dll"), "build/nsis.simplefc.dll", 0755)
  558. build.CopyFile(filepath.Join(*workdir, "COPYING"), "COPYING", 0755)
  559. // Build the installer. This assumes that all the needed files have been previously
  560. // built (don't mix building and packaging to keep cross compilation complexity to a
  561. // minimum).
  562. version := strings.Split(build.VERSION(), ".")
  563. if env.Commit != "" {
  564. version[2] += "-" + env.Commit[:8]
  565. }
  566. installer, _ := filepath.Abs("geth-" + archiveBasename(*arch, env) + ".exe")
  567. build.MustRunCommand("makensis.exe",
  568. "/DOUTPUTFILE="+installer,
  569. "/DMAJORVERSION="+version[0],
  570. "/DMINORVERSION="+version[1],
  571. "/DBUILDVERSION="+version[2],
  572. "/DARCH="+*arch,
  573. filepath.Join(*workdir, "geth.nsi"),
  574. )
  575. // Sign and publish installer.
  576. if err := archiveUpload(installer, *upload, *signer); err != nil {
  577. log.Fatal(err)
  578. }
  579. // Android archives
  580. func doAndroidArchive(cmdline []string) {
  581. var (
  582. signer = flag.String("signer", "", `Environment variable holding the signing key (e.g. ANDROID_SIGNING_KEY)`)
  583. deploy = flag.String("deploy", "", `Where to upload the deploy the archive (usually "https://oss.sonatype.org")`)
  584. upload = flag.String("upload", "", `Destination to upload the archives (usually "gethstore/builds")`)
  585. )
  586. flag.CommandLine.Parse(cmdline)
  587. env := build.Env()
  588. // Build the Android archive and Maven resources
  589. build.MustRun(goTool("get", "golang.org/x/mobile/cmd/gomobile"))
  590. build.MustRun(gomobileTool("init"))
  591. build.MustRun(gomobileTool("bind", "--target", "android", "--javapkg", "org.ethereum", "-v", "github.com/ethereum/go-ethereum/mobile"))
  592. meta := newMavenMetadata(env)
  593. build.Render("build/mvn.pom", meta.PackageString()+".pom", 0755, meta)
  594. // Skip Maven deploy and Azure upload for PR builds
  595. maybeSkipArchive(env)
  596. // Sign and upload all the artifacts to Maven Central
  597. os.Rename("geth.aar", meta.PackageString()+".aar")
  598. if *signer != "" && *deploy != "" {
  599. // Import the signing key into the local GPG instance
  600. if b64key := os.Getenv(*signer); b64key != "" {
  601. key, err := base64.StdEncoding.DecodeString(b64key)
  602. if err != nil {
  603. log.Fatalf("invalid base64 %s", *signer)
  604. }
  605. gpg := exec.Command("gpg", "--import")
  606. gpg.Stdin = bytes.NewReader(key)
  607. build.MustRun(gpg)
  608. }
  609. // Upload the artifacts to Sonatype and/or Maven Central
  610. repo := *deploy + "/service/local/staging/deploy/maven2"
  611. if meta.Unstable {
  612. repo = *deploy + "/content/repositories/snapshots"
  613. }
  614. build.MustRunCommand("mvn", "gpg:sign-and-deploy-file",
  615. "-settings=build/mvn.settings", "-Durl="+repo, "-DrepositoryId=ossrh",
  616. "-DpomFile="+meta.PackageString()+".pom", "-Dfile="+meta.PackageString()+".aar")
  617. }
  618. // Sign and upload the archive to Azure
  619. archive := "geth-" + archiveBasename("android", env) + ".aar"
  620. os.Rename(meta.PackageString()+".aar", archive)
  621. if err := archiveUpload(archive, *upload, *signer); err != nil {
  622. log.Fatal(err)
  623. }
  624. }
  625. func gomobileTool(subcmd string, args ...string) *exec.Cmd {
  626. cmd := exec.Command(filepath.Join(GOBIN, "gomobile"), subcmd)
  627. cmd.Args = append(cmd.Args, args...)
  628. cmd.Env = []string{
  629. "GOPATH=" + build.GOPATH(),
  630. }
  631. for _, e := range os.Environ() {
  632. if strings.HasPrefix(e, "GOPATH=") {
  633. continue
  634. }
  635. cmd.Env = append(cmd.Env, e)
  636. }
  637. return cmd
  638. }
  639. type mavenMetadata struct {
  640. Env build.Environment
  641. Version string
  642. Unstable bool
  643. Contributors []mavenContributor
  644. }
  645. type mavenContributor struct {
  646. Name string
  647. Email string
  648. }
  649. func newMavenMetadata(env build.Environment) mavenMetadata {
  650. // Collect the list of authors from the repo root
  651. contribs := []mavenContributor{}
  652. if authors, err := os.Open("AUTHORS"); err == nil {
  653. defer authors.Close()
  654. scanner := bufio.NewScanner(authors)
  655. for scanner.Scan() {
  656. // Skip any whitespace from the authors list
  657. line := strings.TrimSpace(scanner.Text())
  658. if line == "" || line[0] == '#' {
  659. continue
  660. }
  661. // Split the author and insert as a contributor
  662. re := regexp.MustCompile("([^<]+) <(.+>)")
  663. parts := re.FindStringSubmatch(line)
  664. if len(parts) == 3 {
  665. contribs = append(contribs, mavenContributor{Name: parts[1], Email: parts[2]})
  666. }
  667. }
  668. }
  669. return mavenMetadata{
  670. Env: env,
  671. Version: build.VERSION(),
  672. Unstable: isUnstableBuild(env),
  673. Contributors: contribs,
  674. }
  675. }
  676. func (meta mavenMetadata) VersionString() string {
  677. if meta.Unstable {
  678. return meta.Version + "-SNAPSHOT"
  679. }
  680. return meta.Version
  681. }
  682. func (meta mavenMetadata) PackageString() string {
  683. return "geth-" + meta.VersionString()
  684. }
  685. // Cross compilation
  686. func doXgo(cmdline []string) {
  687. flag.CommandLine.Parse(cmdline)
  688. env := build.Env()
  689. // Make sure xgo is available for cross compilation
  690. gogetxgo := goTool("get", "github.com/karalabe/xgo")
  691. build.MustRun(gogetxgo)
  692. // Execute the actual cross compilation
  693. xgo := xgoTool(append(buildFlags(env), flag.Args()...))
  694. build.MustRun(xgo)
  695. }
  696. func xgoTool(args []string) *exec.Cmd {
  697. cmd := exec.Command(filepath.Join(GOBIN, "xgo"), args...)
  698. cmd.Env = []string{
  699. "GOPATH=" + build.GOPATH(),
  700. "GOBIN=" + GOBIN,
  701. }
  702. for _, e := range os.Environ() {
  703. if strings.HasPrefix(e, "GOPATH=") || strings.HasPrefix(e, "GOBIN=") {
  704. continue
  705. }
  706. cmd.Env = append(cmd.Env, e)
  707. }
  708. return cmd
  709. }