ci.go 27 KB

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