ci.go 27 KB

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