ci.go 27 KB

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