build.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. // +build ignore
  7. package main
  8. import (
  9. "archive/tar"
  10. "archive/zip"
  11. "bytes"
  12. "compress/flate"
  13. "compress/gzip"
  14. "crypto/sha256"
  15. "encoding/json"
  16. "errors"
  17. "flag"
  18. "fmt"
  19. "io"
  20. "io/ioutil"
  21. "log"
  22. "os"
  23. "os/exec"
  24. "os/user"
  25. "path/filepath"
  26. "regexp"
  27. "runtime"
  28. "strconv"
  29. "strings"
  30. "time"
  31. )
  32. var (
  33. versionRe = regexp.MustCompile(`-[0-9]{1,3}-g[0-9a-f]{5,10}`)
  34. goarch string
  35. goos string
  36. noupgrade bool
  37. version string
  38. goCmd string
  39. race bool
  40. debug = os.Getenv("BUILDDEBUG") != ""
  41. extraTags string
  42. installSuffix string
  43. pkgdir string
  44. cc string
  45. debugBinary bool
  46. coverage bool
  47. timeout = "120s"
  48. )
  49. type target struct {
  50. name string
  51. debname string
  52. debdeps []string
  53. debpre string
  54. debpost string
  55. description string
  56. buildPkgs []string
  57. binaryName string
  58. archiveFiles []archiveFile
  59. systemdServices []string
  60. installationFiles []archiveFile
  61. tags []string
  62. }
  63. type archiveFile struct {
  64. src string
  65. dst string
  66. perm os.FileMode
  67. }
  68. var targets = map[string]target{
  69. "all": {
  70. // Only valid for the "build" and "install" commands as it lacks all
  71. // the archive creation stuff. buildPkgs gets filled out in init()
  72. tags: []string{"purego"},
  73. },
  74. "syncthing": {
  75. // The default target for "build", "install", "tar", "zip", "deb", etc.
  76. name: "syncthing",
  77. debname: "syncthing",
  78. debdeps: []string{"libc6", "procps"},
  79. debpost: "script/post-upgrade",
  80. description: "Open Source Continuous File Synchronization",
  81. buildPkgs: []string{"github.com/syncthing/syncthing/cmd/syncthing"},
  82. binaryName: "syncthing", // .exe will be added automatically for Windows builds
  83. archiveFiles: []archiveFile{
  84. {src: "{{binary}}", dst: "{{binary}}", perm: 0755},
  85. {src: "README.md", dst: "README.txt", perm: 0644},
  86. {src: "LICENSE", dst: "LICENSE.txt", perm: 0644},
  87. {src: "AUTHORS", dst: "AUTHORS.txt", perm: 0644},
  88. // All files from etc/ and extra/ added automatically in init().
  89. },
  90. installationFiles: []archiveFile{
  91. {src: "{{binary}}", dst: "deb/usr/bin/{{binary}}", perm: 0755},
  92. {src: "README.md", dst: "deb/usr/share/doc/syncthing/README.txt", perm: 0644},
  93. {src: "LICENSE", dst: "deb/usr/share/doc/syncthing/LICENSE.txt", perm: 0644},
  94. {src: "AUTHORS", dst: "deb/usr/share/doc/syncthing/AUTHORS.txt", perm: 0644},
  95. {src: "man/syncthing.1", dst: "deb/usr/share/man/man1/syncthing.1", perm: 0644},
  96. {src: "man/syncthing-config.5", dst: "deb/usr/share/man/man5/syncthing-config.5", perm: 0644},
  97. {src: "man/syncthing-stignore.5", dst: "deb/usr/share/man/man5/syncthing-stignore.5", perm: 0644},
  98. {src: "man/syncthing-device-ids.7", dst: "deb/usr/share/man/man7/syncthing-device-ids.7", perm: 0644},
  99. {src: "man/syncthing-event-api.7", dst: "deb/usr/share/man/man7/syncthing-event-api.7", perm: 0644},
  100. {src: "man/syncthing-faq.7", dst: "deb/usr/share/man/man7/syncthing-faq.7", perm: 0644},
  101. {src: "man/syncthing-networking.7", dst: "deb/usr/share/man/man7/syncthing-networking.7", perm: 0644},
  102. {src: "man/syncthing-rest-api.7", dst: "deb/usr/share/man/man7/syncthing-rest-api.7", perm: 0644},
  103. {src: "man/syncthing-security.7", dst: "deb/usr/share/man/man7/syncthing-security.7", perm: 0644},
  104. {src: "man/syncthing-versioning.7", dst: "deb/usr/share/man/man7/syncthing-versioning.7", perm: 0644},
  105. {src: "etc/linux-systemd/system/[email protected]", dst: "deb/lib/systemd/system/[email protected]", perm: 0644},
  106. {src: "etc/linux-systemd/system/syncthing-resume.service", dst: "deb/lib/systemd/system/syncthing-resume.service", perm: 0644},
  107. {src: "etc/linux-systemd/user/syncthing.service", dst: "deb/usr/lib/systemd/user/syncthing.service", perm: 0644},
  108. {src: "etc/firewall-ufw/syncthing", dst: "deb/etc/ufw/applications.d/syncthing", perm: 0644},
  109. {src: "etc/linux-desktop/syncthing-start.desktop", dst: "deb/usr/share/applications/syncthing-start.desktop", perm: 0644},
  110. {src: "etc/linux-desktop/syncthing-ui.desktop", dst: "deb/usr/share/applications/syncthing-ui.desktop", perm: 0644},
  111. {src: "assets/logo-32.png", dst: "deb/usr/share/icons/hicolor/32x32/apps/syncthing.png", perm: 0644},
  112. {src: "assets/logo-64.png", dst: "deb/usr/share/icons/hicolor/64x64/apps/syncthing.png", perm: 0644},
  113. {src: "assets/logo-128.png", dst: "deb/usr/share/icons/hicolor/128x128/apps/syncthing.png", perm: 0644},
  114. {src: "assets/logo-256.png", dst: "deb/usr/share/icons/hicolor/256x256/apps/syncthing.png", perm: 0644},
  115. {src: "assets/logo-512.png", dst: "deb/usr/share/icons/hicolor/512x512/apps/syncthing.png", perm: 0644},
  116. {src: "assets/logo-only.svg", dst: "deb/usr/share/icons/hicolor/scalable/apps/syncthing.svg", perm: 0644},
  117. },
  118. },
  119. "stdiscosrv": {
  120. name: "stdiscosrv",
  121. debname: "syncthing-discosrv",
  122. debdeps: []string{"libc6"},
  123. debpre: "cmd/stdiscosrv/scripts/preinst",
  124. description: "Syncthing Discovery Server",
  125. buildPkgs: []string{"github.com/syncthing/syncthing/cmd/stdiscosrv"},
  126. binaryName: "stdiscosrv", // .exe will be added automatically for Windows builds
  127. archiveFiles: []archiveFile{
  128. {src: "{{binary}}", dst: "{{binary}}", perm: 0755},
  129. {src: "cmd/stdiscosrv/README.md", dst: "README.txt", perm: 0644},
  130. {src: "LICENSE", dst: "LICENSE.txt", perm: 0644},
  131. {src: "AUTHORS", dst: "AUTHORS.txt", perm: 0644},
  132. },
  133. systemdServices: []string{
  134. "cmd/stdiscosrv/etc/linux-systemd/stdiscosrv.service",
  135. },
  136. installationFiles: []archiveFile{
  137. {src: "{{binary}}", dst: "deb/usr/bin/{{binary}}", perm: 0755},
  138. {src: "cmd/stdiscosrv/README.md", dst: "deb/usr/share/doc/syncthing-discosrv/README.txt", perm: 0644},
  139. {src: "LICENSE", dst: "deb/usr/share/doc/syncthing-discosrv/LICENSE.txt", perm: 0644},
  140. {src: "AUTHORS", dst: "deb/usr/share/doc/syncthing-discosrv/AUTHORS.txt", perm: 0644},
  141. {src: "man/stdiscosrv.1", dst: "deb/usr/share/man/man1/stdiscosrv.1", perm: 0644},
  142. {src: "cmd/stdiscosrv/etc/linux-systemd/default", dst: "deb/etc/default/syncthing-discosrv", perm: 0644},
  143. {src: "cmd/stdiscosrv/etc/firewall-ufw/stdiscosrv", dst: "deb/etc/ufw/applications.d/stdiscosrv", perm: 0644},
  144. },
  145. tags: []string{"purego"},
  146. },
  147. "strelaysrv": {
  148. name: "strelaysrv",
  149. debname: "syncthing-relaysrv",
  150. debdeps: []string{"libc6"},
  151. debpre: "cmd/strelaysrv/scripts/preinst",
  152. description: "Syncthing Relay Server",
  153. buildPkgs: []string{"github.com/syncthing/syncthing/cmd/strelaysrv"},
  154. binaryName: "strelaysrv", // .exe will be added automatically for Windows builds
  155. archiveFiles: []archiveFile{
  156. {src: "{{binary}}", dst: "{{binary}}", perm: 0755},
  157. {src: "cmd/strelaysrv/README.md", dst: "README.txt", perm: 0644},
  158. {src: "cmd/strelaysrv/LICENSE", dst: "LICENSE.txt", perm: 0644},
  159. {src: "LICENSE", dst: "LICENSE.txt", perm: 0644},
  160. {src: "AUTHORS", dst: "AUTHORS.txt", perm: 0644},
  161. },
  162. systemdServices: []string{
  163. "cmd/strelaysrv/etc/linux-systemd/strelaysrv.service",
  164. },
  165. installationFiles: []archiveFile{
  166. {src: "{{binary}}", dst: "deb/usr/bin/{{binary}}", perm: 0755},
  167. {src: "cmd/strelaysrv/README.md", dst: "deb/usr/share/doc/syncthing-relaysrv/README.txt", perm: 0644},
  168. {src: "cmd/strelaysrv/LICENSE", dst: "deb/usr/share/doc/syncthing-relaysrv/LICENSE.txt", perm: 0644},
  169. {src: "LICENSE", dst: "deb/usr/share/doc/syncthing-relaysrv/LICENSE.txt", perm: 0644},
  170. {src: "AUTHORS", dst: "deb/usr/share/doc/syncthing-relaysrv/AUTHORS.txt", perm: 0644},
  171. {src: "man/strelaysrv.1", dst: "deb/usr/share/man/man1/strelaysrv.1", perm: 0644},
  172. {src: "cmd/strelaysrv/etc/linux-systemd/default", dst: "deb/etc/default/syncthing-relaysrv", perm: 0644},
  173. {src: "cmd/strelaysrv/etc/firewall-ufw/strelaysrv", dst: "deb/etc/ufw/applications.d/strelaysrv", perm: 0644},
  174. },
  175. },
  176. "strelaypoolsrv": {
  177. name: "strelaypoolsrv",
  178. debname: "syncthing-relaypoolsrv",
  179. debdeps: []string{"libc6"},
  180. description: "Syncthing Relay Pool Server",
  181. buildPkgs: []string{"github.com/syncthing/syncthing/cmd/strelaypoolsrv"},
  182. binaryName: "strelaypoolsrv", // .exe will be added automatically for Windows builds
  183. archiveFiles: []archiveFile{
  184. {src: "{{binary}}", dst: "{{binary}}", perm: 0755},
  185. {src: "cmd/strelaypoolsrv/README.md", dst: "README.txt", perm: 0644},
  186. {src: "cmd/strelaypoolsrv/LICENSE", dst: "LICENSE.txt", perm: 0644},
  187. {src: "AUTHORS", dst: "AUTHORS.txt", perm: 0644},
  188. },
  189. installationFiles: []archiveFile{
  190. {src: "{{binary}}", dst: "deb/usr/bin/{{binary}}", perm: 0755},
  191. {src: "cmd/strelaypoolsrv/README.md", dst: "deb/usr/share/doc/syncthing-relaypoolsrv/README.txt", perm: 0644},
  192. {src: "cmd/strelaypoolsrv/LICENSE", dst: "deb/usr/share/doc/syncthing-relaypoolsrv/LICENSE.txt", perm: 0644},
  193. {src: "AUTHORS", dst: "deb/usr/share/doc/syncthing-relaypoolsrv/AUTHORS.txt", perm: 0644},
  194. },
  195. },
  196. }
  197. // These are repos we need to clone to run "go generate"
  198. type dependencyRepo struct {
  199. path string
  200. repo string
  201. commit string
  202. }
  203. var dependencyRepos = []dependencyRepo{
  204. {path: "xdr", repo: "https://github.com/calmh/xdr.git", commit: "08e072f9cb16"},
  205. }
  206. func init() {
  207. all := targets["all"]
  208. pkgs, _ := filepath.Glob("cmd/*")
  209. for _, pkg := range pkgs {
  210. pkg = filepath.Base(pkg)
  211. if strings.HasPrefix(pkg, ".") {
  212. // ignore dotfiles
  213. continue
  214. }
  215. all.buildPkgs = append(all.buildPkgs, fmt.Sprintf("github.com/syncthing/syncthing/cmd/%s", pkg))
  216. }
  217. targets["all"] = all
  218. // The "syncthing" target includes a few more files found in the "etc"
  219. // and "extra" dirs.
  220. syncthingPkg := targets["syncthing"]
  221. for _, file := range listFiles("etc") {
  222. syncthingPkg.archiveFiles = append(syncthingPkg.archiveFiles, archiveFile{src: file, dst: file, perm: 0644})
  223. }
  224. for _, file := range listFiles("extra") {
  225. syncthingPkg.archiveFiles = append(syncthingPkg.archiveFiles, archiveFile{src: file, dst: file, perm: 0644})
  226. }
  227. for _, file := range listFiles("extra") {
  228. syncthingPkg.installationFiles = append(syncthingPkg.installationFiles, archiveFile{src: file, dst: "deb/usr/share/doc/syncthing/" + filepath.Base(file), perm: 0644})
  229. }
  230. targets["syncthing"] = syncthingPkg
  231. }
  232. func main() {
  233. log.SetFlags(0)
  234. parseFlags()
  235. if debug {
  236. t0 := time.Now()
  237. defer func() {
  238. log.Println("... build completed in", time.Since(t0))
  239. }()
  240. }
  241. // Invoking build.go with no parameters at all builds everything (incrementally),
  242. // which is what you want for maximum error checking during development.
  243. if flag.NArg() == 0 {
  244. runCommand("install", targets["all"])
  245. } else {
  246. // with any command given but not a target, the target is
  247. // "syncthing". So "go run build.go install" is "go run build.go install
  248. // syncthing" etc.
  249. targetName := "syncthing"
  250. if flag.NArg() > 1 {
  251. targetName = flag.Arg(1)
  252. }
  253. target, ok := targets[targetName]
  254. if !ok {
  255. log.Fatalln("Unknown target", target)
  256. }
  257. runCommand(flag.Arg(0), target)
  258. }
  259. }
  260. func runCommand(cmd string, target target) {
  261. switch cmd {
  262. case "install":
  263. var tags []string
  264. if noupgrade {
  265. tags = []string{"noupgrade"}
  266. }
  267. tags = append(tags, strings.Fields(extraTags)...)
  268. install(target, tags)
  269. metalintShort()
  270. case "build":
  271. var tags []string
  272. if noupgrade {
  273. tags = []string{"noupgrade"}
  274. }
  275. tags = append(tags, strings.Fields(extraTags)...)
  276. build(target, tags)
  277. case "test":
  278. test("github.com/syncthing/syncthing/lib/...", "github.com/syncthing/syncthing/cmd/...")
  279. case "bench":
  280. bench("github.com/syncthing/syncthing/lib/...", "github.com/syncthing/syncthing/cmd/...")
  281. case "assets":
  282. rebuildAssets()
  283. case "proto":
  284. proto()
  285. case "translate":
  286. translate()
  287. case "transifex":
  288. transifex()
  289. case "tar":
  290. buildTar(target)
  291. case "zip":
  292. buildZip(target)
  293. case "deb":
  294. buildDeb(target)
  295. case "vet":
  296. metalintShort()
  297. case "lint":
  298. metalintShort()
  299. case "metalint":
  300. metalint()
  301. case "version":
  302. fmt.Println(getVersion())
  303. default:
  304. log.Fatalf("Unknown command %q", cmd)
  305. }
  306. }
  307. func parseFlags() {
  308. flag.StringVar(&goarch, "goarch", runtime.GOARCH, "GOARCH")
  309. flag.StringVar(&goos, "goos", runtime.GOOS, "GOOS")
  310. flag.StringVar(&goCmd, "gocmd", "go", "Specify `go` command")
  311. flag.BoolVar(&noupgrade, "no-upgrade", noupgrade, "Disable upgrade functionality")
  312. flag.StringVar(&version, "version", getVersion(), "Set compiled in version string")
  313. flag.BoolVar(&race, "race", race, "Use race detector")
  314. flag.StringVar(&extraTags, "tags", extraTags, "Extra tags, space separated")
  315. flag.StringVar(&installSuffix, "installsuffix", installSuffix, "Install suffix, optional")
  316. flag.StringVar(&pkgdir, "pkgdir", "", "Set -pkgdir parameter for `go build`")
  317. flag.StringVar(&cc, "cc", os.Getenv("CC"), "Set CC environment variable for `go build`")
  318. flag.BoolVar(&debugBinary, "debug-binary", debugBinary, "Create unoptimized binary to use with delve, set -gcflags='-N -l' and omit -ldflags")
  319. flag.BoolVar(&coverage, "coverage", coverage, "Write coverage profile of tests to coverage.txt")
  320. flag.Parse()
  321. }
  322. func test(pkgs ...string) {
  323. lazyRebuildAssets()
  324. args := []string{"test", "-short", "-timeout", timeout, "-tags", "purego"}
  325. if runtime.GOARCH == "amd64" {
  326. switch runtime.GOOS {
  327. case "darwin", "linux", "freebsd": // , "windows": # See https://github.com/golang/go/issues/27089
  328. args = append(args, "-race")
  329. }
  330. }
  331. if coverage {
  332. args = append(args, "-covermode", "atomic", "-coverprofile", "coverage.txt", "-coverpkg", strings.Join(pkgs, ","))
  333. }
  334. runPrint(goCmd, append(args, pkgs...)...)
  335. }
  336. func bench(pkgs ...string) {
  337. lazyRebuildAssets()
  338. runPrint(goCmd, append([]string{"test", "-run", "NONE", "-bench", "."}, pkgs...)...)
  339. }
  340. func install(target target, tags []string) {
  341. lazyRebuildAssets()
  342. tags = append(target.tags, tags...)
  343. cwd, err := os.Getwd()
  344. if err != nil {
  345. log.Fatal(err)
  346. }
  347. os.Setenv("GOBIN", filepath.Join(cwd, "bin"))
  348. os.Setenv("GOOS", goos)
  349. os.Setenv("GOARCH", goarch)
  350. os.Setenv("CC", cc)
  351. // On Windows generate a special file which the Go compiler will
  352. // automatically use when generating Windows binaries to set things like
  353. // the file icon, version, etc.
  354. if goos == "windows" {
  355. sysoPath, err := shouldBuildSyso(cwd)
  356. if err != nil {
  357. log.Printf("Warning: Windows binaries will not have file information encoded: %v", err)
  358. }
  359. defer shouldCleanupSyso(sysoPath)
  360. }
  361. args := []string{"install", "-v"}
  362. args = appendParameters(args, tags, target.buildPkgs...)
  363. runPrint(goCmd, args...)
  364. }
  365. func build(target target, tags []string) {
  366. lazyRebuildAssets()
  367. tags = append(target.tags, tags...)
  368. rmr(target.BinaryName())
  369. os.Setenv("GOOS", goos)
  370. os.Setenv("GOARCH", goarch)
  371. os.Setenv("CC", cc)
  372. // On Windows generate a special file which the Go compiler will
  373. // automatically use when generating Windows binaries to set things like
  374. // the file icon, version, etc.
  375. if goos == "windows" {
  376. cwd, err := os.Getwd()
  377. if err != nil {
  378. log.Fatal(err)
  379. }
  380. sysoPath, err := shouldBuildSyso(cwd)
  381. if err != nil {
  382. log.Printf("Warning: Windows binaries will not have file information encoded: %v", err)
  383. }
  384. defer shouldCleanupSyso(sysoPath)
  385. }
  386. args := []string{"build", "-v"}
  387. args = appendParameters(args, tags, target.buildPkgs...)
  388. runPrint(goCmd, args...)
  389. }
  390. func appendParameters(args []string, tags []string, pkgs ...string) []string {
  391. if pkgdir != "" {
  392. args = append(args, "-pkgdir", pkgdir)
  393. }
  394. if len(tags) > 0 {
  395. args = append(args, "-tags", strings.Join(tags, " "))
  396. }
  397. if installSuffix != "" {
  398. args = append(args, "-installsuffix", installSuffix)
  399. }
  400. if race {
  401. args = append(args, "-race")
  402. }
  403. if !debugBinary {
  404. // Regular binaries get version tagged and skip some debug symbols
  405. args = append(args, "-ldflags", ldflags())
  406. } else {
  407. // -gcflags to disable optimizations and inlining. Skip -ldflags
  408. // because `Could not launch program: decoding dwarf section info at
  409. // offset 0x0: too short` on 'dlv exec ...' see
  410. // https://github.com/derekparker/delve/issues/79
  411. args = append(args, "-gcflags", "-N -l")
  412. }
  413. return append(args, pkgs...)
  414. }
  415. func buildTar(target target) {
  416. name := archiveName(target)
  417. filename := name + ".tar.gz"
  418. var tags []string
  419. if noupgrade {
  420. tags = []string{"noupgrade"}
  421. name += "-noupgrade"
  422. }
  423. build(target, tags)
  424. codesign(target)
  425. for i := range target.archiveFiles {
  426. target.archiveFiles[i].src = strings.Replace(target.archiveFiles[i].src, "{{binary}}", target.BinaryName(), 1)
  427. target.archiveFiles[i].dst = strings.Replace(target.archiveFiles[i].dst, "{{binary}}", target.BinaryName(), 1)
  428. target.archiveFiles[i].dst = name + "/" + target.archiveFiles[i].dst
  429. }
  430. tarGz(filename, target.archiveFiles)
  431. fmt.Println(filename)
  432. }
  433. func buildZip(target target) {
  434. name := archiveName(target)
  435. filename := name + ".zip"
  436. var tags []string
  437. if noupgrade {
  438. tags = []string{"noupgrade"}
  439. name += "-noupgrade"
  440. }
  441. build(target, tags)
  442. codesign(target)
  443. for i := range target.archiveFiles {
  444. target.archiveFiles[i].src = strings.Replace(target.archiveFiles[i].src, "{{binary}}", target.BinaryName(), 1)
  445. target.archiveFiles[i].dst = strings.Replace(target.archiveFiles[i].dst, "{{binary}}", target.BinaryName(), 1)
  446. target.archiveFiles[i].dst = name + "/" + target.archiveFiles[i].dst
  447. }
  448. zipFile(filename, target.archiveFiles)
  449. fmt.Println(filename)
  450. }
  451. func buildDeb(target target) {
  452. os.RemoveAll("deb")
  453. // "goarch" here is set to whatever the Debian packages expect. We correct
  454. // it to what we actually know how to build and keep the Debian variant
  455. // name in "debarch".
  456. debarch := goarch
  457. switch goarch {
  458. case "i386":
  459. goarch = "386"
  460. case "armel", "armhf":
  461. goarch = "arm"
  462. }
  463. build(target, []string{"noupgrade"})
  464. for i := range target.installationFiles {
  465. target.installationFiles[i].src = strings.Replace(target.installationFiles[i].src, "{{binary}}", target.BinaryName(), 1)
  466. target.installationFiles[i].dst = strings.Replace(target.installationFiles[i].dst, "{{binary}}", target.BinaryName(), 1)
  467. }
  468. for _, af := range target.installationFiles {
  469. if err := copyFile(af.src, af.dst, af.perm); err != nil {
  470. log.Fatal(err)
  471. }
  472. }
  473. maintainer := "Syncthing Release Management <[email protected]>"
  474. debver := version
  475. if strings.HasPrefix(debver, "v") {
  476. debver = debver[1:]
  477. // Debian interprets dashes as separator between main version and
  478. // Debian package version, and thus thinks 0.14.26-rc.1 is better
  479. // than just 0.14.26. This rectifies that.
  480. debver = strings.Replace(debver, "-", "~", -1)
  481. }
  482. args := []string{
  483. "-t", "deb",
  484. "-s", "dir",
  485. "-C", "deb",
  486. "-n", target.debname,
  487. "-v", debver,
  488. "-a", debarch,
  489. "-m", maintainer,
  490. "--vendor", maintainer,
  491. "--description", target.description,
  492. "--url", "https://syncthing.net/",
  493. "--license", "MPL-2",
  494. }
  495. for _, dep := range target.debdeps {
  496. args = append(args, "-d", dep)
  497. }
  498. for _, service := range target.systemdServices {
  499. args = append(args, "--deb-systemd", service)
  500. }
  501. if target.debpost != "" {
  502. args = append(args, "--after-upgrade", target.debpost)
  503. }
  504. if target.debpre != "" {
  505. args = append(args, "--before-install", target.debpre)
  506. }
  507. runPrint("fpm", args...)
  508. }
  509. func shouldBuildSyso(dir string) (string, error) {
  510. type M map[string]interface{}
  511. version := getVersion()
  512. version = strings.TrimPrefix(version, "v")
  513. major, minor, patch := semanticVersion()
  514. bs, err := json.Marshal(M{
  515. "FixedFileInfo": M{
  516. "FileVersion": M{
  517. "Major": major,
  518. "Minor": minor,
  519. "Patch": patch,
  520. },
  521. "ProductVersion": M{
  522. "Major": major,
  523. "Minor": minor,
  524. "Patch": patch,
  525. },
  526. },
  527. "StringFileInfo": M{
  528. "FileDescription": "Open Source Continuous File Synchronization",
  529. "LegalCopyright": "The Syncthing Authors",
  530. "FileVersion": version,
  531. "ProductVersion": version,
  532. "ProductName": "Syncthing",
  533. },
  534. "IconPath": "assets/logo.ico",
  535. })
  536. if err != nil {
  537. return "", err
  538. }
  539. jsonPath := filepath.Join(dir, "versioninfo.json")
  540. err = ioutil.WriteFile(jsonPath, bs, 0644)
  541. if err != nil {
  542. return "", errors.New("failed to create " + jsonPath + ": " + err.Error())
  543. }
  544. defer func() {
  545. if err := os.Remove(jsonPath); err != nil {
  546. log.Printf("Warning: unable to remove generated %s: %v. Please remove it manually.", jsonPath, err)
  547. }
  548. }()
  549. sysoPath := filepath.Join(dir, "cmd", "syncthing", "resource.syso")
  550. if _, err := runError("goversioninfo", "-o", sysoPath); err != nil {
  551. return "", errors.New("failed to create " + sysoPath + ": " + err.Error())
  552. }
  553. return sysoPath, nil
  554. }
  555. func shouldCleanupSyso(sysoFilePath string) {
  556. if sysoFilePath == "" {
  557. return
  558. }
  559. if err := os.Remove(sysoFilePath); err != nil {
  560. log.Printf("Warning: unable to remove generated %s: %v. Please remove it manually.", sysoFilePath, err)
  561. }
  562. }
  563. // copyFile copies a file from src to dst, ensuring the containing directory
  564. // exists. The permission bits are copied as well. If dst already exists and
  565. // the contents are identical to src the modification time is not updated.
  566. func copyFile(src, dst string, perm os.FileMode) error {
  567. in, err := ioutil.ReadFile(src)
  568. if err != nil {
  569. return err
  570. }
  571. out, err := ioutil.ReadFile(dst)
  572. if err != nil {
  573. // The destination probably doesn't exist, we should create
  574. // it.
  575. goto copy
  576. }
  577. if bytes.Equal(in, out) {
  578. // The permission bits may have changed without the contents
  579. // changing so we always mirror them.
  580. os.Chmod(dst, perm)
  581. return nil
  582. }
  583. copy:
  584. os.MkdirAll(filepath.Dir(dst), 0777)
  585. if err := ioutil.WriteFile(dst, in, perm); err != nil {
  586. return err
  587. }
  588. return nil
  589. }
  590. func listFiles(dir string) []string {
  591. var res []string
  592. filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error {
  593. if err != nil {
  594. return err
  595. }
  596. if fi.Mode().IsRegular() {
  597. res = append(res, path)
  598. }
  599. return nil
  600. })
  601. return res
  602. }
  603. func rebuildAssets() {
  604. os.Setenv("SOURCE_DATE_EPOCH", fmt.Sprint(buildStamp()))
  605. runPrint(goCmd, "generate", "github.com/syncthing/syncthing/lib/api/auto", "github.com/syncthing/syncthing/cmd/strelaypoolsrv/auto")
  606. }
  607. func lazyRebuildAssets() {
  608. if shouldRebuildAssets("lib/api/auto/gui.files.go", "gui") || shouldRebuildAssets("cmd/strelaypoolsrv/auto/gui.files.go", "cmd/strelaypoolsrv/auto/gui") {
  609. rebuildAssets()
  610. }
  611. }
  612. func shouldRebuildAssets(target, srcdir string) bool {
  613. info, err := os.Stat(target)
  614. if err != nil {
  615. // If the file doesn't exist, we must rebuild it
  616. return true
  617. }
  618. // Check if any of the files in gui/ are newer than the asset file. If
  619. // so we should rebuild it.
  620. currentBuild := info.ModTime()
  621. assetsAreNewer := false
  622. stop := errors.New("no need to iterate further")
  623. filepath.Walk(srcdir, func(path string, info os.FileInfo, err error) error {
  624. if err != nil {
  625. return err
  626. }
  627. if info.ModTime().After(currentBuild) {
  628. assetsAreNewer = true
  629. return stop
  630. }
  631. return nil
  632. })
  633. return assetsAreNewer
  634. }
  635. func proto() {
  636. pv := protobufVersion()
  637. dependencyRepos = append(dependencyRepos,
  638. dependencyRepo{path: "protobuf", repo: "https://github.com/gogo/protobuf.git", commit: pv},
  639. )
  640. runPrint(goCmd, "get", fmt.Sprintf("github.com/gogo/protobuf/protoc-gen-gogofast@%v", pv))
  641. os.MkdirAll("repos", 0755)
  642. for _, dep := range dependencyRepos {
  643. path := filepath.Join("repos", dep.path)
  644. if _, err := os.Stat(path); err != nil {
  645. runPrintInDir("repos", "git", "clone", dep.repo, dep.path)
  646. } else {
  647. runPrintInDir(path, "git", "fetch")
  648. }
  649. runPrintInDir(path, "git", "checkout", dep.commit)
  650. }
  651. runPrint(goCmd, "generate", "github.com/syncthing/syncthing/lib/...", "github.com/syncthing/syncthing/cmd/stdiscosrv")
  652. }
  653. func translate() {
  654. os.Chdir("gui/default/assets/lang")
  655. runPipe("lang-en-new.json", goCmd, "run", "../../../../script/translate.go", "lang-en.json", "../../../")
  656. os.Remove("lang-en.json")
  657. err := os.Rename("lang-en-new.json", "lang-en.json")
  658. if err != nil {
  659. log.Fatal(err)
  660. }
  661. os.Chdir("../../../..")
  662. }
  663. func transifex() {
  664. os.Chdir("gui/default/assets/lang")
  665. runPrint(goCmd, "run", "../../../../script/transifexdl.go")
  666. }
  667. func ldflags() string {
  668. b := new(strings.Builder)
  669. b.WriteString("-w")
  670. fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.Version=%s", version)
  671. fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.Stamp=%d", buildStamp())
  672. fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.User=%s", buildUser())
  673. fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.Host=%s", buildHost())
  674. if v := os.Getenv("EXTRA_LDFLAGS"); v != "" {
  675. fmt.Fprintf(b, " %s", v)
  676. }
  677. return b.String()
  678. }
  679. func rmr(paths ...string) {
  680. for _, path := range paths {
  681. if debug {
  682. log.Println("rm -r", path)
  683. }
  684. os.RemoveAll(path)
  685. }
  686. }
  687. func getReleaseVersion() (string, error) {
  688. bs, err := ioutil.ReadFile("RELEASE")
  689. if err != nil {
  690. return "", err
  691. }
  692. return string(bytes.TrimSpace(bs)), nil
  693. }
  694. func getGitVersion() (string, error) {
  695. v, err := runError("git", "describe", "--always", "--dirty")
  696. if err != nil {
  697. return "", err
  698. }
  699. v = versionRe.ReplaceAllFunc(v, func(s []byte) []byte {
  700. s[0] = '+'
  701. return s
  702. })
  703. return string(v), nil
  704. }
  705. func getVersion() string {
  706. // First try for a RELEASE file,
  707. if ver, err := getReleaseVersion(); err == nil {
  708. return ver
  709. }
  710. // ... then see if we have a Git tag.
  711. if ver, err := getGitVersion(); err == nil {
  712. if strings.Contains(ver, "-") {
  713. // The version already contains a hash and stuff. See if we can
  714. // find a current branch name to tack onto it as well.
  715. return ver + getBranchSuffix()
  716. }
  717. return ver
  718. }
  719. // This seems to be a dev build.
  720. return "unknown-dev"
  721. }
  722. func semanticVersion() (major, minor, patch int) {
  723. r := regexp.MustCompile(`v(\d+)\.(\d+).(\d+)`)
  724. matches := r.FindStringSubmatch(getVersion())
  725. if len(matches) != 4 {
  726. return 0, 0, 0
  727. }
  728. var ints [3]int
  729. for i, s := range matches[1:] {
  730. ints[i], _ = strconv.Atoi(s)
  731. }
  732. return ints[0], ints[1], ints[2]
  733. }
  734. func getBranchSuffix() string {
  735. bs, err := runError("git", "branch", "-a", "--contains")
  736. if err != nil {
  737. return ""
  738. }
  739. branches := strings.Split(string(bs), "\n")
  740. if len(branches) == 0 {
  741. return ""
  742. }
  743. branch := ""
  744. for i, candidate := range branches {
  745. if strings.HasPrefix(candidate, "*") {
  746. // This is the current branch. Select it!
  747. branch = strings.TrimLeft(candidate, " \t*")
  748. break
  749. } else if i == 0 {
  750. // Otherwise the first branch in the list will do.
  751. branch = strings.TrimSpace(branch)
  752. }
  753. }
  754. if branch == "" {
  755. return ""
  756. }
  757. // The branch name may be on the form "remotes/origin/foo" from which we
  758. // just want "foo".
  759. parts := strings.Split(branch, "/")
  760. if len(parts) == 0 || len(parts[len(parts)-1]) == 0 {
  761. return ""
  762. }
  763. branch = parts[len(parts)-1]
  764. switch branch {
  765. case "master", "release":
  766. // these are not special
  767. return ""
  768. }
  769. validBranchRe := regexp.MustCompile(`^[a-zA-Z0-9_.-]+$`)
  770. if !validBranchRe.MatchString(branch) {
  771. // There's some odd stuff in the branch name. Better skip it.
  772. return ""
  773. }
  774. return "-" + branch
  775. }
  776. func buildStamp() int64 {
  777. // If SOURCE_DATE_EPOCH is set, use that.
  778. if s, _ := strconv.ParseInt(os.Getenv("SOURCE_DATE_EPOCH"), 10, 64); s > 0 {
  779. return s
  780. }
  781. // Try to get the timestamp of the latest commit.
  782. bs, err := runError("git", "show", "-s", "--format=%ct")
  783. if err != nil {
  784. // Fall back to "now".
  785. return time.Now().Unix()
  786. }
  787. s, _ := strconv.ParseInt(string(bs), 10, 64)
  788. return s
  789. }
  790. func buildUser() string {
  791. if v := os.Getenv("BUILD_USER"); v != "" {
  792. return v
  793. }
  794. u, err := user.Current()
  795. if err != nil {
  796. return "unknown-user"
  797. }
  798. return strings.Replace(u.Username, " ", "-", -1)
  799. }
  800. func buildHost() string {
  801. if v := os.Getenv("BUILD_HOST"); v != "" {
  802. return v
  803. }
  804. h, err := os.Hostname()
  805. if err != nil {
  806. return "unknown-host"
  807. }
  808. return h
  809. }
  810. func buildArch() string {
  811. os := goos
  812. if os == "darwin" {
  813. os = "macos"
  814. }
  815. return fmt.Sprintf("%s-%s", os, goarch)
  816. }
  817. func archiveName(target target) string {
  818. return fmt.Sprintf("%s-%s-%s", target.name, buildArch(), version)
  819. }
  820. func runError(cmd string, args ...string) ([]byte, error) {
  821. if debug {
  822. t0 := time.Now()
  823. log.Println("runError:", cmd, strings.Join(args, " "))
  824. defer func() {
  825. log.Println("... in", time.Since(t0))
  826. }()
  827. }
  828. ecmd := exec.Command(cmd, args...)
  829. bs, err := ecmd.CombinedOutput()
  830. return bytes.TrimSpace(bs), err
  831. }
  832. func runPrint(cmd string, args ...string) {
  833. runPrintInDir(".", cmd, args...)
  834. }
  835. func runPrintInDir(dir string, cmd string, args ...string) {
  836. if debug {
  837. t0 := time.Now()
  838. log.Println("runPrint:", cmd, strings.Join(args, " "))
  839. defer func() {
  840. log.Println("... in", time.Since(t0))
  841. }()
  842. }
  843. ecmd := exec.Command(cmd, args...)
  844. ecmd.Stdout = os.Stdout
  845. ecmd.Stderr = os.Stderr
  846. ecmd.Dir = dir
  847. err := ecmd.Run()
  848. if err != nil {
  849. log.Fatal(err)
  850. }
  851. }
  852. func runPipe(file, cmd string, args ...string) {
  853. if debug {
  854. t0 := time.Now()
  855. log.Println("runPipe:", cmd, strings.Join(args, " "))
  856. defer func() {
  857. log.Println("... in", time.Since(t0))
  858. }()
  859. }
  860. fd, err := os.Create(file)
  861. if err != nil {
  862. log.Fatal(err)
  863. }
  864. ecmd := exec.Command(cmd, args...)
  865. ecmd.Stdout = fd
  866. ecmd.Stderr = os.Stderr
  867. err = ecmd.Run()
  868. if err != nil {
  869. log.Fatal(err)
  870. }
  871. fd.Close()
  872. }
  873. func tarGz(out string, files []archiveFile) {
  874. fd, err := os.Create(out)
  875. if err != nil {
  876. log.Fatal(err)
  877. }
  878. gw, err := gzip.NewWriterLevel(fd, gzip.BestCompression)
  879. if err != nil {
  880. log.Fatal(err)
  881. }
  882. tw := tar.NewWriter(gw)
  883. for _, f := range files {
  884. sf, err := os.Open(f.src)
  885. if err != nil {
  886. log.Fatal(err)
  887. }
  888. info, err := sf.Stat()
  889. if err != nil {
  890. log.Fatal(err)
  891. }
  892. h := &tar.Header{
  893. Name: f.dst,
  894. Size: info.Size(),
  895. Mode: int64(info.Mode()),
  896. ModTime: info.ModTime(),
  897. }
  898. err = tw.WriteHeader(h)
  899. if err != nil {
  900. log.Fatal(err)
  901. }
  902. _, err = io.Copy(tw, sf)
  903. if err != nil {
  904. log.Fatal(err)
  905. }
  906. sf.Close()
  907. }
  908. err = tw.Close()
  909. if err != nil {
  910. log.Fatal(err)
  911. }
  912. err = gw.Close()
  913. if err != nil {
  914. log.Fatal(err)
  915. }
  916. err = fd.Close()
  917. if err != nil {
  918. log.Fatal(err)
  919. }
  920. }
  921. func zipFile(out string, files []archiveFile) {
  922. fd, err := os.Create(out)
  923. if err != nil {
  924. log.Fatal(err)
  925. }
  926. zw := zip.NewWriter(fd)
  927. var fw *flate.Writer
  928. // Register the deflator.
  929. zw.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {
  930. var err error
  931. if fw == nil {
  932. // Creating a flate compressor for every file is
  933. // expensive, create one and reuse it.
  934. fw, err = flate.NewWriter(out, flate.BestCompression)
  935. } else {
  936. fw.Reset(out)
  937. }
  938. return fw, err
  939. })
  940. for _, f := range files {
  941. sf, err := os.Open(f.src)
  942. if err != nil {
  943. log.Fatal(err)
  944. }
  945. info, err := sf.Stat()
  946. if err != nil {
  947. log.Fatal(err)
  948. }
  949. fh, err := zip.FileInfoHeader(info)
  950. if err != nil {
  951. log.Fatal(err)
  952. }
  953. fh.Name = filepath.ToSlash(f.dst)
  954. fh.Method = zip.Deflate
  955. if strings.HasSuffix(f.dst, ".txt") {
  956. // Text file. Read it and convert line endings.
  957. bs, err := ioutil.ReadAll(sf)
  958. if err != nil {
  959. log.Fatal(err)
  960. }
  961. bs = bytes.Replace(bs, []byte{'\n'}, []byte{'\n', '\r'}, -1)
  962. fh.UncompressedSize = uint32(len(bs))
  963. fh.UncompressedSize64 = uint64(len(bs))
  964. of, err := zw.CreateHeader(fh)
  965. if err != nil {
  966. log.Fatal(err)
  967. }
  968. of.Write(bs)
  969. } else {
  970. // Binary file. Copy verbatim.
  971. of, err := zw.CreateHeader(fh)
  972. if err != nil {
  973. log.Fatal(err)
  974. }
  975. _, err = io.Copy(of, sf)
  976. if err != nil {
  977. log.Fatal(err)
  978. }
  979. }
  980. }
  981. err = zw.Close()
  982. if err != nil {
  983. log.Fatal(err)
  984. }
  985. err = fd.Close()
  986. if err != nil {
  987. log.Fatal(err)
  988. }
  989. }
  990. func codesign(target target) {
  991. switch goos {
  992. case "windows":
  993. windowsCodesign(target.BinaryName())
  994. case "darwin":
  995. macosCodesign(target.BinaryName())
  996. }
  997. }
  998. func macosCodesign(file string) {
  999. if pass := os.Getenv("CODESIGN_KEYCHAIN_PASS"); pass != "" {
  1000. bs, err := runError("security", "unlock-keychain", "-p", pass)
  1001. if err != nil {
  1002. log.Println("Codesign: unlocking keychain failed:", string(bs))
  1003. return
  1004. }
  1005. }
  1006. if id := os.Getenv("CODESIGN_IDENTITY"); id != "" {
  1007. bs, err := runError("codesign", "-s", id, file)
  1008. if err != nil {
  1009. log.Println("Codesign: signing failed:", string(bs))
  1010. return
  1011. }
  1012. log.Println("Codesign: successfully signed", file)
  1013. }
  1014. }
  1015. func windowsCodesign(file string) {
  1016. st := "signtool.exe"
  1017. if path := os.Getenv("CODESIGN_SIGNTOOL"); path != "" {
  1018. st = path
  1019. }
  1020. for i, algo := range []string{"sha1", "sha256"} {
  1021. args := []string{"sign", "/fd", algo}
  1022. if f := os.Getenv("CODESIGN_CERTIFICATE_FILE"); f != "" {
  1023. args = append(args, "/f", f)
  1024. }
  1025. if p := os.Getenv("CODESIGN_CERTIFICATE_PASSWORD"); p != "" {
  1026. args = append(args, "/p", p)
  1027. }
  1028. if tr := os.Getenv("CODESIGN_TIMESTAMP_SERVER"); tr != "" {
  1029. switch algo {
  1030. case "sha256":
  1031. args = append(args, "/tr", tr, "/td", algo)
  1032. default:
  1033. args = append(args, "/t", tr)
  1034. }
  1035. }
  1036. if i > 0 {
  1037. args = append(args, "/as")
  1038. }
  1039. args = append(args, file)
  1040. bs, err := runError(st, args...)
  1041. if err != nil {
  1042. log.Println("Codesign: signing failed:", string(bs))
  1043. return
  1044. }
  1045. log.Println("Codesign: successfully signed", file, "using", algo)
  1046. }
  1047. }
  1048. func metalint() {
  1049. lazyRebuildAssets()
  1050. runPrint(goCmd, "test", "-run", "Metalint", "./meta")
  1051. }
  1052. func metalintShort() {
  1053. lazyRebuildAssets()
  1054. runPrint(goCmd, "test", "-short", "-run", "Metalint", "./meta")
  1055. }
  1056. func temporaryBuildDir() (string, error) {
  1057. // The base of our temp dir is "syncthing-xxxxxxxx" where the x:es
  1058. // are eight bytes from the sha256 of our working directory. We do
  1059. // this because we want a name in the global temp dir that doesn't
  1060. // conflict with someone else building syncthing on the same
  1061. // machine, yet is persistent between runs from the same source
  1062. // directory.
  1063. wd, err := os.Getwd()
  1064. if err != nil {
  1065. return "", err
  1066. }
  1067. hash := sha256.Sum256([]byte(wd))
  1068. base := fmt.Sprintf("syncthing-%x", hash[:4])
  1069. // The temp dir is taken from $STTMPDIR if set, otherwise the system
  1070. // default (potentially infrluenced by $TMPDIR on unixes).
  1071. var tmpDir string
  1072. if t := os.Getenv("STTMPDIR"); t != "" {
  1073. tmpDir = t
  1074. } else {
  1075. tmpDir = os.TempDir()
  1076. }
  1077. return filepath.Join(tmpDir, base), nil
  1078. }
  1079. func (t target) BinaryName() string {
  1080. if goos == "windows" {
  1081. return t.binaryName + ".exe"
  1082. }
  1083. return t.binaryName
  1084. }
  1085. func protobufVersion() string {
  1086. bs, err := runError(goCmd, "list", "-f", "{{.Version}}", "-m", "github.com/gogo/protobuf")
  1087. if err != nil {
  1088. log.Fatal("Getting protobuf version:", err)
  1089. }
  1090. return string(bs)
  1091. }