build.go 35 KB

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