ci.go 27 KB

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