build.go 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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. err = ioutil.WriteFile(jsonPath, bs, 0644)
  580. if err != nil {
  581. return "", errors.New("failed to create " + jsonPath + ": " + err.Error())
  582. }
  583. defer func() {
  584. if err := os.Remove(jsonPath); err != nil {
  585. log.Printf("Warning: unable to remove generated %s: %v. Please remove it manually.", jsonPath, err)
  586. }
  587. }()
  588. sysoPath := filepath.Join(dir, "cmd", "syncthing", "resource.syso")
  589. if _, err := runError("goversioninfo", "-o", sysoPath); err != nil {
  590. return "", errors.New("failed to create " + sysoPath + ": " + err.Error())
  591. }
  592. return sysoPath, nil
  593. }
  594. func shouldCleanupSyso(sysoFilePath string) {
  595. if sysoFilePath == "" {
  596. return
  597. }
  598. if err := os.Remove(sysoFilePath); err != nil {
  599. log.Printf("Warning: unable to remove generated %s: %v. Please remove it manually.", sysoFilePath, err)
  600. }
  601. }
  602. // copyFile copies a file from src to dst, ensuring the containing directory
  603. // exists. The permission bits are copied as well. If dst already exists and
  604. // the contents are identical to src the modification time is not updated.
  605. func copyFile(src, dst string, perm os.FileMode) error {
  606. in, err := ioutil.ReadFile(src)
  607. if err != nil {
  608. return err
  609. }
  610. out, err := ioutil.ReadFile(dst)
  611. if err != nil {
  612. // The destination probably doesn't exist, we should create
  613. // it.
  614. goto copy
  615. }
  616. if bytes.Equal(in, out) {
  617. // The permission bits may have changed without the contents
  618. // changing so we always mirror them.
  619. os.Chmod(dst, perm)
  620. return nil
  621. }
  622. copy:
  623. os.MkdirAll(filepath.Dir(dst), 0777)
  624. if err := ioutil.WriteFile(dst, in, perm); err != nil {
  625. return err
  626. }
  627. return nil
  628. }
  629. func listFiles(dir string) []string {
  630. var res []string
  631. filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error {
  632. if err != nil {
  633. return err
  634. }
  635. if fi.Mode().IsRegular() {
  636. res = append(res, path)
  637. }
  638. return nil
  639. })
  640. return res
  641. }
  642. func rebuildAssets() {
  643. os.Setenv("SOURCE_DATE_EPOCH", fmt.Sprint(buildStamp()))
  644. runPrint(goCmd, "generate", "github.com/syncthing/syncthing/lib/auto", "github.com/syncthing/syncthing/cmd/strelaypoolsrv/auto")
  645. }
  646. func lazyRebuildAssets() {
  647. if shouldRebuildAssets("lib/auto/gui.files.go", "gui") || shouldRebuildAssets("cmd/strelaypoolsrv/auto/gui.files.go", "cmd/strelaypoolsrv/auto/gui") {
  648. rebuildAssets()
  649. }
  650. }
  651. func shouldRebuildAssets(target, srcdir string) bool {
  652. info, err := os.Stat(target)
  653. if err != nil {
  654. // If the file doesn't exist, we must rebuild it
  655. return true
  656. }
  657. // Check if any of the files in gui/ are newer than the asset file. If
  658. // so we should rebuild it.
  659. currentBuild := info.ModTime()
  660. assetsAreNewer := false
  661. stop := errors.New("no need to iterate further")
  662. filepath.Walk(srcdir, func(path string, info os.FileInfo, err error) error {
  663. if err != nil {
  664. return err
  665. }
  666. if info.ModTime().After(currentBuild) {
  667. assetsAreNewer = true
  668. return stop
  669. }
  670. return nil
  671. })
  672. return assetsAreNewer
  673. }
  674. func proto() {
  675. pv := protobufVersion()
  676. dependencyRepos = append(dependencyRepos,
  677. dependencyRepo{path: "protobuf", repo: "https://github.com/gogo/protobuf.git", commit: pv},
  678. )
  679. runPrint(goCmd, "get", fmt.Sprintf("github.com/gogo/protobuf/protoc-gen-gogofast@%v", pv))
  680. os.MkdirAll("repos", 0755)
  681. for _, dep := range dependencyRepos {
  682. path := filepath.Join("repos", dep.path)
  683. if _, err := os.Stat(path); err != nil {
  684. runPrintInDir("repos", "git", "clone", dep.repo, dep.path)
  685. } else {
  686. runPrintInDir(path, "git", "fetch")
  687. }
  688. runPrintInDir(path, "git", "checkout", dep.commit)
  689. }
  690. runPrint(goCmd, "generate", "github.com/syncthing/syncthing/lib/...", "github.com/syncthing/syncthing/cmd/stdiscosrv")
  691. }
  692. func translate() {
  693. os.Chdir("gui/default/assets/lang")
  694. runPipe("lang-en-new.json", goCmd, "run", "../../../../script/translate.go", "lang-en.json", "../../../")
  695. os.Remove("lang-en.json")
  696. err := os.Rename("lang-en-new.json", "lang-en.json")
  697. if err != nil {
  698. log.Fatal(err)
  699. }
  700. os.Chdir("../../../..")
  701. }
  702. func transifex() {
  703. os.Chdir("gui/default/assets/lang")
  704. runPrint(goCmd, "run", "../../../../script/transifexdl.go")
  705. }
  706. func ldflags(program string) string {
  707. b := new(strings.Builder)
  708. b.WriteString("-w")
  709. fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.Version=%s", version)
  710. fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.Stamp=%d", buildStamp())
  711. fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.User=%s", buildUser())
  712. fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.Host=%s", buildHost())
  713. fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.Program=%s", program)
  714. if v := os.Getenv("EXTRA_LDFLAGS"); v != "" {
  715. fmt.Fprintf(b, " %s", v)
  716. }
  717. return b.String()
  718. }
  719. func rmr(paths ...string) {
  720. for _, path := range paths {
  721. if debug {
  722. log.Println("rm -r", path)
  723. }
  724. os.RemoveAll(path)
  725. }
  726. }
  727. func getReleaseVersion() (string, error) {
  728. bs, err := ioutil.ReadFile("RELEASE")
  729. if err != nil {
  730. return "", err
  731. }
  732. return string(bytes.TrimSpace(bs)), nil
  733. }
  734. func getGitVersion() (string, error) {
  735. v, err := runError("git", "describe", "--always", "--dirty")
  736. if err != nil {
  737. return "", err
  738. }
  739. v = versionRe.ReplaceAllFunc(v, func(s []byte) []byte {
  740. s[0] = '+'
  741. return s
  742. })
  743. return string(v), nil
  744. }
  745. func getVersion() string {
  746. // First try for a RELEASE file,
  747. if ver, err := getReleaseVersion(); err == nil {
  748. return ver
  749. }
  750. // ... then see if we have a Git tag.
  751. if ver, err := getGitVersion(); err == nil {
  752. if strings.Contains(ver, "-") {
  753. // The version already contains a hash and stuff. See if we can
  754. // find a current branch name to tack onto it as well.
  755. return ver + getBranchSuffix()
  756. }
  757. return ver
  758. }
  759. // This seems to be a dev build.
  760. return "unknown-dev"
  761. }
  762. func semanticVersion() (major, minor, patch, build int) {
  763. r := regexp.MustCompile(`v(?P<Major>\d+)\.(?P<Minor>\d+).(?P<Patch>\d+).*\+(?P<CommitsAhead>\d+)`)
  764. matches := r.FindStringSubmatch(getVersion())
  765. if len(matches) != 5 {
  766. return 0, 0, 0, 0
  767. }
  768. var ints [4]int
  769. for i := 1; i < 5; i++ {
  770. value, err := strconv.Atoi(matches[i])
  771. if err != nil {
  772. return 0, 0, 0, 0
  773. }
  774. ints[i-1] = value
  775. }
  776. return ints[0], ints[1], ints[2], ints[3]
  777. }
  778. func getBranchSuffix() string {
  779. bs, err := runError("git", "branch", "-a", "--contains")
  780. if err != nil {
  781. return ""
  782. }
  783. branches := strings.Split(string(bs), "\n")
  784. if len(branches) == 0 {
  785. return ""
  786. }
  787. branch := ""
  788. for i, candidate := range branches {
  789. if strings.HasPrefix(candidate, "*") {
  790. // This is the current branch. Select it!
  791. branch = strings.TrimLeft(candidate, " \t*")
  792. break
  793. } else if i == 0 {
  794. // Otherwise the first branch in the list will do.
  795. branch = strings.TrimSpace(branch)
  796. }
  797. }
  798. if branch == "" {
  799. return ""
  800. }
  801. // The branch name may be on the form "remotes/origin/foo" from which we
  802. // just want "foo".
  803. parts := strings.Split(branch, "/")
  804. if len(parts) == 0 || len(parts[len(parts)-1]) == 0 {
  805. return ""
  806. }
  807. branch = parts[len(parts)-1]
  808. switch branch {
  809. case "master", "release":
  810. // these are not special
  811. return ""
  812. }
  813. validBranchRe := regexp.MustCompile(`^[a-zA-Z0-9_.-]+$`)
  814. if !validBranchRe.MatchString(branch) {
  815. // There's some odd stuff in the branch name. Better skip it.
  816. return ""
  817. }
  818. return "-" + branch
  819. }
  820. func buildStamp() int64 {
  821. // If SOURCE_DATE_EPOCH is set, use that.
  822. if s, _ := strconv.ParseInt(os.Getenv("SOURCE_DATE_EPOCH"), 10, 64); s > 0 {
  823. return s
  824. }
  825. // Try to get the timestamp of the latest commit.
  826. bs, err := runError("git", "show", "-s", "--format=%ct")
  827. if err != nil {
  828. // Fall back to "now".
  829. return time.Now().Unix()
  830. }
  831. s, _ := strconv.ParseInt(string(bs), 10, 64)
  832. return s
  833. }
  834. func buildUser() string {
  835. if v := os.Getenv("BUILD_USER"); v != "" {
  836. return v
  837. }
  838. u, err := user.Current()
  839. if err != nil {
  840. return "unknown-user"
  841. }
  842. return strings.Replace(u.Username, " ", "-", -1)
  843. }
  844. func buildHost() string {
  845. if v := os.Getenv("BUILD_HOST"); v != "" {
  846. return v
  847. }
  848. h, err := os.Hostname()
  849. if err != nil {
  850. return "unknown-host"
  851. }
  852. return h
  853. }
  854. func buildArch() string {
  855. os := goos
  856. if os == "darwin" {
  857. os = "macos"
  858. }
  859. return fmt.Sprintf("%s-%s", os, goarch)
  860. }
  861. func archiveName(target target) string {
  862. return fmt.Sprintf("%s-%s-%s", target.name, buildArch(), version)
  863. }
  864. func runError(cmd string, args ...string) ([]byte, error) {
  865. if debug {
  866. t0 := time.Now()
  867. log.Println("runError:", cmd, strings.Join(args, " "))
  868. defer func() {
  869. log.Println("... in", time.Since(t0))
  870. }()
  871. }
  872. ecmd := exec.Command(cmd, args...)
  873. bs, err := ecmd.CombinedOutput()
  874. return bytes.TrimSpace(bs), err
  875. }
  876. func runPrint(cmd string, args ...string) {
  877. runPrintInDir(".", cmd, args...)
  878. }
  879. func runPrintInDir(dir string, cmd string, args ...string) {
  880. if debug {
  881. t0 := time.Now()
  882. log.Println("runPrint:", cmd, strings.Join(args, " "))
  883. defer func() {
  884. log.Println("... in", time.Since(t0))
  885. }()
  886. }
  887. ecmd := exec.Command(cmd, args...)
  888. ecmd.Stdout = os.Stdout
  889. ecmd.Stderr = os.Stderr
  890. ecmd.Dir = dir
  891. err := ecmd.Run()
  892. if err != nil {
  893. log.Fatal(err)
  894. }
  895. }
  896. func runPipe(file, cmd string, args ...string) {
  897. if debug {
  898. t0 := time.Now()
  899. log.Println("runPipe:", cmd, strings.Join(args, " "))
  900. defer func() {
  901. log.Println("... in", time.Since(t0))
  902. }()
  903. }
  904. fd, err := os.Create(file)
  905. if err != nil {
  906. log.Fatal(err)
  907. }
  908. ecmd := exec.Command(cmd, args...)
  909. ecmd.Stdout = fd
  910. ecmd.Stderr = os.Stderr
  911. err = ecmd.Run()
  912. if err != nil {
  913. log.Fatal(err)
  914. }
  915. fd.Close()
  916. }
  917. func tarGz(out string, files []archiveFile) {
  918. fd, err := os.Create(out)
  919. if err != nil {
  920. log.Fatal(err)
  921. }
  922. gw, err := gzip.NewWriterLevel(fd, gzip.BestCompression)
  923. if err != nil {
  924. log.Fatal(err)
  925. }
  926. tw := tar.NewWriter(gw)
  927. for _, f := range files {
  928. sf, err := os.Open(f.src)
  929. if err != nil {
  930. log.Fatal(err)
  931. }
  932. info, err := sf.Stat()
  933. if err != nil {
  934. log.Fatal(err)
  935. }
  936. h := &tar.Header{
  937. Name: f.dst,
  938. Size: info.Size(),
  939. Mode: int64(info.Mode()),
  940. ModTime: info.ModTime(),
  941. }
  942. err = tw.WriteHeader(h)
  943. if err != nil {
  944. log.Fatal(err)
  945. }
  946. _, err = io.Copy(tw, sf)
  947. if err != nil {
  948. log.Fatal(err)
  949. }
  950. sf.Close()
  951. }
  952. err = tw.Close()
  953. if err != nil {
  954. log.Fatal(err)
  955. }
  956. err = gw.Close()
  957. if err != nil {
  958. log.Fatal(err)
  959. }
  960. err = fd.Close()
  961. if err != nil {
  962. log.Fatal(err)
  963. }
  964. }
  965. func zipFile(out string, files []archiveFile) {
  966. fd, err := os.Create(out)
  967. if err != nil {
  968. log.Fatal(err)
  969. }
  970. zw := zip.NewWriter(fd)
  971. var fw *flate.Writer
  972. // Register the deflator.
  973. zw.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {
  974. var err error
  975. if fw == nil {
  976. // Creating a flate compressor for every file is
  977. // expensive, create one and reuse it.
  978. fw, err = flate.NewWriter(out, flate.BestCompression)
  979. } else {
  980. fw.Reset(out)
  981. }
  982. return fw, err
  983. })
  984. for _, f := range files {
  985. sf, err := os.Open(f.src)
  986. if err != nil {
  987. log.Fatal(err)
  988. }
  989. info, err := sf.Stat()
  990. if err != nil {
  991. log.Fatal(err)
  992. }
  993. fh, err := zip.FileInfoHeader(info)
  994. if err != nil {
  995. log.Fatal(err)
  996. }
  997. fh.Name = filepath.ToSlash(f.dst)
  998. fh.Method = zip.Deflate
  999. if strings.HasSuffix(f.dst, ".txt") {
  1000. // Text file. Read it and convert line endings.
  1001. bs, err := ioutil.ReadAll(sf)
  1002. if err != nil {
  1003. log.Fatal(err)
  1004. }
  1005. bs = bytes.Replace(bs, []byte{'\n'}, []byte{'\n', '\r'}, -1)
  1006. fh.UncompressedSize = uint32(len(bs))
  1007. fh.UncompressedSize64 = uint64(len(bs))
  1008. of, err := zw.CreateHeader(fh)
  1009. if err != nil {
  1010. log.Fatal(err)
  1011. }
  1012. of.Write(bs)
  1013. } else {
  1014. // Binary file. Copy verbatim.
  1015. of, err := zw.CreateHeader(fh)
  1016. if err != nil {
  1017. log.Fatal(err)
  1018. }
  1019. _, err = io.Copy(of, sf)
  1020. if err != nil {
  1021. log.Fatal(err)
  1022. }
  1023. }
  1024. }
  1025. err = zw.Close()
  1026. if err != nil {
  1027. log.Fatal(err)
  1028. }
  1029. err = fd.Close()
  1030. if err != nil {
  1031. log.Fatal(err)
  1032. }
  1033. }
  1034. func codesign(target target) {
  1035. switch goos {
  1036. case "windows":
  1037. windowsCodesign(target.BinaryName())
  1038. case "darwin":
  1039. macosCodesign(target.BinaryName())
  1040. }
  1041. }
  1042. func macosCodesign(file string) {
  1043. if pass := os.Getenv("CODESIGN_KEYCHAIN_PASS"); pass != "" {
  1044. bs, err := runError("security", "unlock-keychain", "-p", pass)
  1045. if err != nil {
  1046. log.Println("Codesign: unlocking keychain failed:", string(bs))
  1047. return
  1048. }
  1049. }
  1050. if id := os.Getenv("CODESIGN_IDENTITY"); id != "" {
  1051. bs, err := runError("codesign", "-s", id, file)
  1052. if err != nil {
  1053. log.Println("Codesign: signing failed:", string(bs))
  1054. return
  1055. }
  1056. log.Println("Codesign: successfully signed", file)
  1057. }
  1058. }
  1059. func windowsCodesign(file string) {
  1060. st := "signtool.exe"
  1061. if path := os.Getenv("CODESIGN_SIGNTOOL"); path != "" {
  1062. st = path
  1063. }
  1064. for i, algo := range []string{"sha1", "sha256"} {
  1065. args := []string{"sign", "/fd", algo}
  1066. if f := os.Getenv("CODESIGN_CERTIFICATE_FILE"); f != "" {
  1067. args = append(args, "/f", f)
  1068. }
  1069. if p := os.Getenv("CODESIGN_CERTIFICATE_PASSWORD"); p != "" {
  1070. args = append(args, "/p", p)
  1071. }
  1072. if tr := os.Getenv("CODESIGN_TIMESTAMP_SERVER"); tr != "" {
  1073. switch algo {
  1074. case "sha256":
  1075. args = append(args, "/tr", tr, "/td", algo)
  1076. default:
  1077. args = append(args, "/t", tr)
  1078. }
  1079. }
  1080. if i > 0 {
  1081. args = append(args, "/as")
  1082. }
  1083. args = append(args, file)
  1084. bs, err := runError(st, args...)
  1085. if err != nil {
  1086. log.Println("Codesign: signing failed:", string(bs))
  1087. return
  1088. }
  1089. log.Println("Codesign: successfully signed", file, "using", algo)
  1090. }
  1091. }
  1092. func metalint() {
  1093. lazyRebuildAssets()
  1094. runPrint(goCmd, "test", "-run", "Metalint", "./meta")
  1095. }
  1096. func metalintShort() {
  1097. lazyRebuildAssets()
  1098. runPrint(goCmd, "test", "-short", "-run", "Metalint", "./meta")
  1099. }
  1100. func temporaryBuildDir() (string, error) {
  1101. // The base of our temp dir is "syncthing-xxxxxxxx" where the x:es
  1102. // are eight bytes from the sha256 of our working directory. We do
  1103. // this because we want a name in the global temp dir that doesn't
  1104. // conflict with someone else building syncthing on the same
  1105. // machine, yet is persistent between runs from the same source
  1106. // directory.
  1107. wd, err := os.Getwd()
  1108. if err != nil {
  1109. return "", err
  1110. }
  1111. hash := sha256.Sum256([]byte(wd))
  1112. base := fmt.Sprintf("syncthing-%x", hash[:4])
  1113. // The temp dir is taken from $STTMPDIR if set, otherwise the system
  1114. // default (potentially infrluenced by $TMPDIR on unixes).
  1115. var tmpDir string
  1116. if t := os.Getenv("STTMPDIR"); t != "" {
  1117. tmpDir = t
  1118. } else {
  1119. tmpDir = os.TempDir()
  1120. }
  1121. return filepath.Join(tmpDir, base), nil
  1122. }
  1123. func (t target) BinaryName() string {
  1124. if goos == "windows" {
  1125. return t.binaryName + ".exe"
  1126. }
  1127. return t.binaryName
  1128. }
  1129. func protobufVersion() string {
  1130. bs, err := runError(goCmd, "list", "-f", "{{.Version}}", "-m", "github.com/gogo/protobuf")
  1131. if err != nil {
  1132. log.Fatal("Getting protobuf version:", err)
  1133. }
  1134. return string(bs)
  1135. }