build.go 35 KB

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