ci.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. xgo [ options ] -- cross builds according to options
  27. For all commands, -n prevents execution of external programs (dry run mode).
  28. */
  29. package main
  30. import (
  31. "bytes"
  32. "encoding/base64"
  33. "flag"
  34. "fmt"
  35. "go/parser"
  36. "go/token"
  37. "io/ioutil"
  38. "log"
  39. "os"
  40. "os/exec"
  41. "path/filepath"
  42. "runtime"
  43. "strings"
  44. "time"
  45. "github.com/ethereum/go-ethereum/internal/build"
  46. )
  47. var (
  48. // Files that end up in the geth*.zip archive.
  49. gethArchiveFiles = []string{
  50. "COPYING",
  51. executablePath("geth"),
  52. }
  53. // Files that end up in the geth-alltools*.zip archive.
  54. allToolsArchiveFiles = []string{
  55. "COPYING",
  56. executablePath("abigen"),
  57. executablePath("evm"),
  58. executablePath("geth"),
  59. executablePath("rlpdump"),
  60. }
  61. // A debian package is created for all executables listed here.
  62. debExecutables = []debExecutable{
  63. {
  64. Name: "geth",
  65. Description: "Ethereum CLI client.",
  66. },
  67. {
  68. Name: "rlpdump",
  69. Description: "Developer utility tool that prints RLP structures.",
  70. },
  71. {
  72. Name: "evm",
  73. Description: "Developer utility version of the EVM (Ethereum Virtual Machine) that is capable of running bytecode snippets within a configurable environment and execution mode.",
  74. },
  75. {
  76. Name: "abigen",
  77. Description: "Source code generator to convert Ethereum contract definitions into easy to use, compile-time type-safe Go packages.",
  78. },
  79. }
  80. // Distros for which packages are created.
  81. // Note: vivid is unsupported because there is no golang-1.6 package for it.
  82. debDistros = []string{"trusty", "wily", "xenial", "yakkety"}
  83. )
  84. var GOBIN, _ = filepath.Abs(filepath.Join("build", "bin"))
  85. func executablePath(name string) string {
  86. if runtime.GOOS == "windows" {
  87. name += ".exe"
  88. }
  89. return filepath.Join(GOBIN, name)
  90. }
  91. func main() {
  92. log.SetFlags(log.Lshortfile)
  93. if _, err := os.Stat(filepath.Join("build", "ci.go")); os.IsNotExist(err) {
  94. log.Fatal("this script must be run from the root of the repository")
  95. }
  96. if len(os.Args) < 2 {
  97. log.Fatal("need subcommand as first argument")
  98. }
  99. switch os.Args[1] {
  100. case "install":
  101. doInstall(os.Args[2:])
  102. case "test":
  103. doTest(os.Args[2:])
  104. case "archive":
  105. doArchive(os.Args[2:])
  106. case "debsrc":
  107. doDebianSource(os.Args[2:])
  108. case "xgo":
  109. doXgo(os.Args[2:])
  110. default:
  111. log.Fatal("unknown command ", os.Args[1])
  112. }
  113. }
  114. // Compiling
  115. func doInstall(cmdline []string) {
  116. var (
  117. arch = flag.String("arch", "", "Architecture to cross build for")
  118. )
  119. flag.CommandLine.Parse(cmdline)
  120. env := build.Env()
  121. // Check Go version. People regularly open issues about compilation
  122. // failure with outdated Go. This should save them the trouble.
  123. if runtime.Version() < "go1.4" && !strings.HasPrefix(runtime.Version(), "devel") {
  124. log.Println("You have Go version", runtime.Version())
  125. log.Println("go-ethereum requires at least Go version 1.4 and cannot")
  126. log.Println("be compiled with an earlier version. Please upgrade your Go installation.")
  127. os.Exit(1)
  128. }
  129. // Compile packages given as arguments, or everything if there are no arguments.
  130. packages := []string{"./..."}
  131. if flag.NArg() > 0 {
  132. packages = flag.Args()
  133. }
  134. if *arch == "" || *arch == runtime.GOARCH {
  135. goinstall := goTool("install", buildFlags(env)...)
  136. goinstall.Args = append(goinstall.Args, "-v")
  137. goinstall.Args = append(goinstall.Args, packages...)
  138. build.MustRun(goinstall)
  139. return
  140. }
  141. // If we are cross compiling to ARMv5 ARMv6 or ARMv7, clean any prvious builds
  142. if *arch == "arm" {
  143. os.RemoveAll(filepath.Join(runtime.GOROOT(), "pkg", runtime.GOOS+"_arm"))
  144. for _, path := range filepath.SplitList(build.GOPATH()) {
  145. os.RemoveAll(filepath.Join(path, "pkg", runtime.GOOS+"_arm"))
  146. }
  147. }
  148. // Seems we are cross compiling, work around forbidden GOBIN
  149. goinstall := goToolArch(*arch, "install", buildFlags(env)...)
  150. goinstall.Args = append(goinstall.Args, "-v")
  151. goinstall.Args = append(goinstall.Args, []string{"-buildmode", "archive"}...)
  152. goinstall.Args = append(goinstall.Args, packages...)
  153. build.MustRun(goinstall)
  154. if cmds, err := ioutil.ReadDir("cmd"); err == nil {
  155. for _, cmd := range cmds {
  156. pkgs, err := parser.ParseDir(token.NewFileSet(), filepath.Join(".", "cmd", cmd.Name()), nil, parser.PackageClauseOnly)
  157. if err != nil {
  158. log.Fatal(err)
  159. }
  160. for name, _ := range pkgs {
  161. if name == "main" {
  162. gobuild := goToolArch(*arch, "build", buildFlags(env)...)
  163. gobuild.Args = append(gobuild.Args, "-v")
  164. gobuild.Args = append(gobuild.Args, []string{"-o", filepath.Join(GOBIN, cmd.Name())}...)
  165. gobuild.Args = append(gobuild.Args, "."+string(filepath.Separator)+filepath.Join("cmd", cmd.Name()))
  166. build.MustRun(gobuild)
  167. break
  168. }
  169. }
  170. }
  171. }
  172. }
  173. func buildFlags(env build.Environment) (flags []string) {
  174. if os.Getenv("GO_OPENCL") != "" {
  175. flags = append(flags, "-tags", "opencl")
  176. }
  177. // Since Go 1.5, the separator char for link time assignments
  178. // is '=' and using ' ' prints a warning. However, Go < 1.5 does
  179. // not support using '='.
  180. sep := " "
  181. if runtime.Version() > "go1.5" || strings.Contains(runtime.Version(), "devel") {
  182. sep = "="
  183. }
  184. // Set gitCommit constant via link-time assignment.
  185. if env.Commit != "" {
  186. flags = append(flags, "-ldflags", "-X main.gitCommit"+sep+env.Commit)
  187. }
  188. return flags
  189. }
  190. func goTool(subcmd string, args ...string) *exec.Cmd {
  191. return goToolArch(runtime.GOARCH, subcmd, args...)
  192. }
  193. func goToolArch(arch string, subcmd string, args ...string) *exec.Cmd {
  194. gocmd := filepath.Join(runtime.GOROOT(), "bin", "go")
  195. cmd := exec.Command(gocmd, subcmd)
  196. cmd.Args = append(cmd.Args, args...)
  197. cmd.Env = []string{
  198. "GO15VENDOREXPERIMENT=1",
  199. "GOPATH=" + build.GOPATH(),
  200. }
  201. if arch == "" || arch == runtime.GOARCH {
  202. cmd.Env = append(cmd.Env, "GOBIN="+GOBIN)
  203. } else {
  204. cmd.Env = append(cmd.Env, "CGO_ENABLED=1")
  205. cmd.Env = append(cmd.Env, "GOARCH="+arch)
  206. }
  207. for _, e := range os.Environ() {
  208. if strings.HasPrefix(e, "GOPATH=") || strings.HasPrefix(e, "GOBIN=") {
  209. continue
  210. }
  211. cmd.Env = append(cmd.Env, e)
  212. }
  213. return cmd
  214. }
  215. // Running The Tests
  216. //
  217. // "tests" also includes static analysis tools such as vet.
  218. func doTest(cmdline []string) {
  219. var (
  220. vet = flag.Bool("vet", false, "Whether to run go vet")
  221. coverage = flag.Bool("coverage", false, "Whether to record code coverage")
  222. )
  223. flag.CommandLine.Parse(cmdline)
  224. packages := []string{"./..."}
  225. if len(flag.CommandLine.Args()) > 0 {
  226. packages = flag.CommandLine.Args()
  227. }
  228. if len(packages) == 1 && packages[0] == "./..." {
  229. // Resolve ./... manually since go vet will fail on vendored stuff
  230. out, err := goTool("list", "./...").CombinedOutput()
  231. if err != nil {
  232. log.Fatalf("package listing failed: %v\n%s", err, string(out))
  233. }
  234. packages = []string{}
  235. for _, line := range strings.Split(string(out), "\n") {
  236. if !strings.Contains(line, "vendor") {
  237. packages = append(packages, strings.TrimSpace(line))
  238. }
  239. }
  240. }
  241. // Run analysis tools before the tests.
  242. if *vet {
  243. build.MustRun(goTool("vet", packages...))
  244. }
  245. // Run the actual tests.
  246. gotest := goTool("test")
  247. // Test a single package at a time. CI builders are slow
  248. // and some tests run into timeouts under load.
  249. gotest.Args = append(gotest.Args, "-p", "1")
  250. if *coverage {
  251. gotest.Args = append(gotest.Args, "-covermode=atomic", "-cover")
  252. }
  253. gotest.Args = append(gotest.Args, packages...)
  254. build.MustRun(gotest)
  255. }
  256. // Release Packaging
  257. func doArchive(cmdline []string) {
  258. var (
  259. arch = flag.String("arch", runtime.GOARCH, "Architecture cross packaging")
  260. atype = flag.String("type", "zip", "Type of archive to write (zip|tar)")
  261. signer = flag.String("signer", "", `Environment variable holding the signing key (e.g. LINUX_SIGNING_KEY)`)
  262. upload = flag.String("upload", "", `Destination to upload the archives (usually "gethstore/builds")`)
  263. ext string
  264. )
  265. flag.CommandLine.Parse(cmdline)
  266. switch *atype {
  267. case "zip":
  268. ext = ".zip"
  269. case "tar":
  270. ext = ".tar.gz"
  271. default:
  272. log.Fatal("unknown archive type: ", atype)
  273. }
  274. var (
  275. env = build.Env()
  276. base = archiveBasename(*arch, env)
  277. geth = "geth-" + base + ext
  278. alltools = "geth-alltools-" + base + ext
  279. )
  280. maybeSkipArchive(env)
  281. if err := build.WriteArchive(geth, gethArchiveFiles); err != nil {
  282. log.Fatal(err)
  283. }
  284. if err := build.WriteArchive(alltools, allToolsArchiveFiles); err != nil {
  285. log.Fatal(err)
  286. }
  287. for _, archive := range []string{geth, alltools} {
  288. if err := archiveUpload(archive, *upload, *signer); err != nil {
  289. log.Fatal(err)
  290. }
  291. }
  292. }
  293. func archiveBasename(arch string, env build.Environment) string {
  294. platform := runtime.GOOS + "-" + arch
  295. if arch == "arm" {
  296. platform += os.Getenv("GOARM")
  297. }
  298. archive := platform + "-" + build.VERSION()
  299. if isUnstableBuild(env) {
  300. archive += "-unstable"
  301. }
  302. if env.Commit != "" {
  303. archive += "-" + env.Commit[:8]
  304. }
  305. return archive
  306. }
  307. func archiveUpload(archive string, blobstore string, signer string) error {
  308. // If signing was requested, generate the signature files
  309. if signer != "" {
  310. pgpkey, err := base64.StdEncoding.DecodeString(os.Getenv(signer))
  311. if err != nil {
  312. return fmt.Errorf("invalid base64 %s", signer)
  313. }
  314. if err := build.PGPSignFile(archive, archive+".asc", string(pgpkey)); err != nil {
  315. return err
  316. }
  317. }
  318. // If uploading to Azure was requested, push the archive possibly with its signature
  319. if blobstore != "" {
  320. auth := build.AzureBlobstoreConfig{
  321. Account: strings.Split(blobstore, "/")[0],
  322. Token: os.Getenv("AZURE_BLOBSTORE_TOKEN"),
  323. Container: strings.SplitN(blobstore, "/", 2)[1],
  324. }
  325. if err := build.AzureBlobstoreUpload(archive, archive, auth); err != nil {
  326. return err
  327. }
  328. if signer != "" {
  329. if err := build.AzureBlobstoreUpload(archive+".asc", archive+".asc", auth); err != nil {
  330. return err
  331. }
  332. }
  333. }
  334. return nil
  335. }
  336. // skips archiving for some build configurations.
  337. func maybeSkipArchive(env build.Environment) {
  338. if env.IsPullRequest {
  339. log.Printf("skipping because this is a PR build")
  340. os.Exit(0)
  341. }
  342. if env.Branch != "develop" && !strings.HasPrefix(env.Tag, "v1.") {
  343. log.Printf("skipping because branch %q, tag %q is not on the whitelist", env.Branch, env.Tag)
  344. os.Exit(0)
  345. }
  346. }
  347. // Debian Packaging
  348. func doDebianSource(cmdline []string) {
  349. var (
  350. signer = flag.String("signer", "", `Signing key name, also used as package author`)
  351. upload = flag.String("upload", "", `Where to upload the source package (usually "ppa:ethereum/ethereum")`)
  352. workdir = flag.String("workdir", "", `Output directory for packages (uses temp dir if unset)`)
  353. now = time.Now()
  354. )
  355. flag.CommandLine.Parse(cmdline)
  356. *workdir = makeWorkdir(*workdir)
  357. env := build.Env()
  358. maybeSkipArchive(env)
  359. // Import the signing key.
  360. if b64key := os.Getenv("PPA_SIGNING_KEY"); b64key != "" {
  361. key, err := base64.StdEncoding.DecodeString(b64key)
  362. if err != nil {
  363. log.Fatal("invalid base64 PPA_SIGNING_KEY")
  364. }
  365. gpg := exec.Command("gpg", "--import")
  366. gpg.Stdin = bytes.NewReader(key)
  367. build.MustRun(gpg)
  368. }
  369. // Create the packages.
  370. for _, distro := range debDistros {
  371. meta := newDebMetadata(distro, *signer, env, now)
  372. pkgdir := stageDebianSource(*workdir, meta)
  373. debuild := exec.Command("debuild", "-S", "-sa", "-us", "-uc")
  374. debuild.Dir = pkgdir
  375. build.MustRun(debuild)
  376. changes := fmt.Sprintf("%s_%s_source.changes", meta.Name(), meta.VersionString())
  377. changes = filepath.Join(*workdir, changes)
  378. if *signer != "" {
  379. build.MustRunCommand("debsign", changes)
  380. }
  381. if *upload != "" {
  382. build.MustRunCommand("dput", *upload, changes)
  383. }
  384. }
  385. }
  386. func makeWorkdir(wdflag string) string {
  387. var err error
  388. if wdflag != "" {
  389. err = os.MkdirAll(wdflag, 0744)
  390. } else {
  391. wdflag, err = ioutil.TempDir("", "eth-deb-build-")
  392. }
  393. if err != nil {
  394. log.Fatal(err)
  395. }
  396. return wdflag
  397. }
  398. func isUnstableBuild(env build.Environment) bool {
  399. if env.Branch != "develop" && env.Tag != "" {
  400. return false
  401. }
  402. return true
  403. }
  404. type debMetadata struct {
  405. Env build.Environment
  406. // go-ethereum version being built. Note that this
  407. // is not the debian package version. The package version
  408. // is constructed by VersionString.
  409. Version string
  410. Author string // "name <email>", also selects signing key
  411. Distro, Time string
  412. Executables []debExecutable
  413. }
  414. type debExecutable struct {
  415. Name, Description string
  416. }
  417. func newDebMetadata(distro, author string, env build.Environment, t time.Time) debMetadata {
  418. if author == "" {
  419. // No signing key, use default author.
  420. author = "Ethereum Builds <fjl@ethereum.org>"
  421. }
  422. return debMetadata{
  423. Env: env,
  424. Author: author,
  425. Distro: distro,
  426. Version: build.VERSION(),
  427. Time: t.Format(time.RFC1123Z),
  428. Executables: debExecutables,
  429. }
  430. }
  431. // Name returns the name of the metapackage that depends
  432. // on all executable packages.
  433. func (meta debMetadata) Name() string {
  434. if isUnstableBuild(meta.Env) {
  435. return "ethereum-unstable"
  436. }
  437. return "ethereum"
  438. }
  439. // VersionString returns the debian version of the packages.
  440. func (meta debMetadata) VersionString() string {
  441. vsn := meta.Version
  442. if meta.Env.Buildnum != "" {
  443. vsn += "+build" + meta.Env.Buildnum
  444. }
  445. if meta.Distro != "" {
  446. vsn += "+" + meta.Distro
  447. }
  448. return vsn
  449. }
  450. // ExeList returns the list of all executable packages.
  451. func (meta debMetadata) ExeList() string {
  452. names := make([]string, len(meta.Executables))
  453. for i, e := range meta.Executables {
  454. names[i] = meta.ExeName(e)
  455. }
  456. return strings.Join(names, ", ")
  457. }
  458. // ExeName returns the package name of an executable package.
  459. func (meta debMetadata) ExeName(exe debExecutable) string {
  460. if isUnstableBuild(meta.Env) {
  461. return exe.Name + "-unstable"
  462. }
  463. return exe.Name
  464. }
  465. // ExeConflicts returns the content of the Conflicts field
  466. // for executable packages.
  467. func (meta debMetadata) ExeConflicts(exe debExecutable) string {
  468. if isUnstableBuild(meta.Env) {
  469. // Set up the conflicts list so that the *-unstable packages
  470. // cannot be installed alongside the regular version.
  471. //
  472. // https://www.debian.org/doc/debian-policy/ch-relationships.html
  473. // is very explicit about Conflicts: and says that Breaks: should
  474. // be preferred and the conflicting files should be handled via
  475. // alternates. We might do this eventually but using a conflict is
  476. // easier now.
  477. return "ethereum, " + exe.Name
  478. }
  479. return ""
  480. }
  481. func stageDebianSource(tmpdir string, meta debMetadata) (pkgdir string) {
  482. pkg := meta.Name() + "-" + meta.VersionString()
  483. pkgdir = filepath.Join(tmpdir, pkg)
  484. if err := os.Mkdir(pkgdir, 0755); err != nil {
  485. log.Fatal(err)
  486. }
  487. // Copy the source code.
  488. build.MustRunCommand("git", "checkout-index", "-a", "--prefix", pkgdir+string(filepath.Separator))
  489. // Put the debian build files in place.
  490. debian := filepath.Join(pkgdir, "debian")
  491. build.Render("build/deb.rules", filepath.Join(debian, "rules"), 0755, meta)
  492. build.Render("build/deb.changelog", filepath.Join(debian, "changelog"), 0644, meta)
  493. build.Render("build/deb.control", filepath.Join(debian, "control"), 0644, meta)
  494. build.Render("build/deb.copyright", filepath.Join(debian, "copyright"), 0644, meta)
  495. build.RenderString("8\n", filepath.Join(debian, "compat"), 0644, meta)
  496. build.RenderString("3.0 (native)\n", filepath.Join(debian, "source/format"), 0644, meta)
  497. for _, exe := range meta.Executables {
  498. install := filepath.Join(debian, meta.ExeName(exe)+".install")
  499. docs := filepath.Join(debian, meta.ExeName(exe)+".docs")
  500. build.Render("build/deb.install", install, 0644, exe)
  501. build.Render("build/deb.docs", docs, 0644, exe)
  502. }
  503. return pkgdir
  504. }
  505. // Cross compilation
  506. func doXgo(cmdline []string) {
  507. flag.CommandLine.Parse(cmdline)
  508. env := build.Env()
  509. // Make sure xgo is available for cross compilation
  510. gogetxgo := goTool("get", "github.com/karalabe/xgo")
  511. build.MustRun(gogetxgo)
  512. // Execute the actual cross compilation
  513. xgo := xgoTool(append(buildFlags(env), flag.Args()...))
  514. build.MustRun(xgo)
  515. }
  516. func xgoTool(args []string) *exec.Cmd {
  517. cmd := exec.Command(filepath.Join(GOBIN, "xgo"), args...)
  518. cmd.Env = []string{
  519. "GOPATH=" + build.GOPATH(),
  520. "GOBIN=" + GOBIN,
  521. }
  522. for _, e := range os.Environ() {
  523. if strings.HasPrefix(e, "GOPATH=") || strings.HasPrefix(e, "GOBIN=") {
  524. continue
  525. }
  526. cmd.Env = append(cmd.Env, e)
  527. }
  528. return cmd
  529. }