ci.go 29 KB

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