ci.go 29 KB

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