build.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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"
  25. "path/filepath"
  26. "regexp"
  27. "runtime"
  28. "strconv"
  29. "strings"
  30. "text/template"
  31. "time"
  32. )
  33. var (
  34. versionRe = regexp.MustCompile(`-[0-9]{1,3}-g[0-9a-f]{5,10}`)
  35. goarch string
  36. goos string
  37. noupgrade bool
  38. version string
  39. goCmd string
  40. goVersion float64
  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. jsonPath := filepath.Join(dir, "versioninfo.json")
  557. file, err := os.Create(filepath.Join(dir, "versioninfo.json"))
  558. if err != nil {
  559. return "", errors.New("failed to create " + jsonPath + ": " + err.Error())
  560. }
  561. major, minor, patch, build := semanticVersion()
  562. fmt.Fprintf(file, `{
  563. "FixedFileInfo": {
  564. "FileVersion": {
  565. "Major": %s,
  566. "Minor": %s,
  567. "Patch": %s,
  568. "Build": %s
  569. }
  570. },
  571. "StringFileInfo": {
  572. "FileDescription": "Open Source Continuous File Synchronization",
  573. "LegalCopyright": "The Syncthing Authors",
  574. "ProductVersion": "%s",
  575. "ProductName": "Syncthing"
  576. },
  577. "IconPath": "assets/logo.ico"
  578. }`, major, minor, patch, build, getVersion())
  579. file.Close()
  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. sep := '='
  705. if goVersion > 0 && goVersion < 1.5 {
  706. sep = ' '
  707. }
  708. b := new(bytes.Buffer)
  709. b.WriteString("-w")
  710. fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.Version%c%s", sep, version)
  711. fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.Stamp%c%d", sep, buildStamp())
  712. fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.User%c%s", sep, buildUser())
  713. fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.Host%c%s", sep, buildHost())
  714. fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.Program%c%s", sep, program)
  715. if v := os.Getenv("EXTRA_LDFLAGS"); v != "" {
  716. fmt.Fprintf(b, " %s", v)
  717. }
  718. return b.String()
  719. }
  720. func rmr(paths ...string) {
  721. for _, path := range paths {
  722. if debug {
  723. log.Println("rm -r", path)
  724. }
  725. os.RemoveAll(path)
  726. }
  727. }
  728. func getReleaseVersion() (string, error) {
  729. fd, err := os.Open("RELEASE")
  730. if err != nil {
  731. return "", err
  732. }
  733. defer fd.Close()
  734. bs, err := ioutil.ReadAll(fd)
  735. if err != nil {
  736. return "", err
  737. }
  738. return string(bytes.TrimSpace(bs)), nil
  739. }
  740. func getGitVersion() (string, error) {
  741. v, err := runError("git", "describe", "--always", "--dirty")
  742. if err != nil {
  743. return "", err
  744. }
  745. v = versionRe.ReplaceAllFunc(v, func(s []byte) []byte {
  746. s[0] = '+'
  747. return s
  748. })
  749. return string(v), nil
  750. }
  751. func getVersion() string {
  752. // First try for a RELEASE file,
  753. if ver, err := getReleaseVersion(); err == nil {
  754. return ver
  755. }
  756. // ... then see if we have a Git tag.
  757. if ver, err := getGitVersion(); err == nil {
  758. if strings.Contains(ver, "-") {
  759. // The version already contains a hash and stuff. See if we can
  760. // find a current branch name to tack onto it as well.
  761. return ver + getBranchSuffix()
  762. }
  763. return ver
  764. }
  765. // This seems to be a dev build.
  766. return "unknown-dev"
  767. }
  768. func semanticVersion() (major, minor, patch, build string) {
  769. r := regexp.MustCompile(`v(?P<Major>\d+)\.(?P<Minor>\d+).(?P<Patch>\d+).*\+(?P<CommitsAhead>\d+)`)
  770. matches := r.FindStringSubmatch(getVersion())
  771. if len(matches) != 5 {
  772. return "0", "0", "0", "0"
  773. }
  774. return matches[1], matches[2], matches[3], matches[4]
  775. }
  776. func getBranchSuffix() string {
  777. bs, err := runError("git", "branch", "-a", "--contains")
  778. if err != nil {
  779. return ""
  780. }
  781. branches := strings.Split(string(bs), "\n")
  782. if len(branches) == 0 {
  783. return ""
  784. }
  785. branch := ""
  786. for i, candidate := range branches {
  787. if strings.HasPrefix(candidate, "*") {
  788. // This is the current branch. Select it!
  789. branch = strings.TrimLeft(candidate, " \t*")
  790. break
  791. } else if i == 0 {
  792. // Otherwise the first branch in the list will do.
  793. branch = strings.TrimSpace(branch)
  794. }
  795. }
  796. if branch == "" {
  797. return ""
  798. }
  799. // The branch name may be on the form "remotes/origin/foo" from which we
  800. // just want "foo".
  801. parts := strings.Split(branch, "/")
  802. if len(parts) == 0 || len(parts[len(parts)-1]) == 0 {
  803. return ""
  804. }
  805. branch = parts[len(parts)-1]
  806. switch branch {
  807. case "master", "release":
  808. // these are not special
  809. return ""
  810. }
  811. validBranchRe := regexp.MustCompile(`^[a-zA-Z0-9_.-]+$`)
  812. if !validBranchRe.MatchString(branch) {
  813. // There's some odd stuff in the branch name. Better skip it.
  814. return ""
  815. }
  816. return "-" + branch
  817. }
  818. func buildStamp() int64 {
  819. // If SOURCE_DATE_EPOCH is set, use that.
  820. if s, _ := strconv.ParseInt(os.Getenv("SOURCE_DATE_EPOCH"), 10, 64); s > 0 {
  821. return s
  822. }
  823. // Try to get the timestamp of the latest commit.
  824. bs, err := runError("git", "show", "-s", "--format=%ct")
  825. if err != nil {
  826. // Fall back to "now".
  827. return time.Now().Unix()
  828. }
  829. s, _ := strconv.ParseInt(string(bs), 10, 64)
  830. return s
  831. }
  832. func buildUser() string {
  833. if v := os.Getenv("BUILD_USER"); v != "" {
  834. return v
  835. }
  836. u, err := user.Current()
  837. if err != nil {
  838. return "unknown-user"
  839. }
  840. return strings.Replace(u.Username, " ", "-", -1)
  841. }
  842. func buildHost() string {
  843. if v := os.Getenv("BUILD_HOST"); v != "" {
  844. return v
  845. }
  846. h, err := os.Hostname()
  847. if err != nil {
  848. return "unknown-host"
  849. }
  850. return h
  851. }
  852. func buildArch() string {
  853. os := goos
  854. if os == "darwin" {
  855. os = "macos"
  856. }
  857. return fmt.Sprintf("%s-%s", os, goarch)
  858. }
  859. func archiveName(target target) string {
  860. return fmt.Sprintf("%s-%s-%s", target.name, buildArch(), version)
  861. }
  862. func runError(cmd string, args ...string) ([]byte, error) {
  863. if debug {
  864. t0 := time.Now()
  865. log.Println("runError:", cmd, strings.Join(args, " "))
  866. defer func() {
  867. log.Println("... in", time.Since(t0))
  868. }()
  869. }
  870. ecmd := exec.Command(cmd, args...)
  871. bs, err := ecmd.CombinedOutput()
  872. return bytes.TrimSpace(bs), err
  873. }
  874. func runPrint(cmd string, args ...string) {
  875. runPrintInDir(".", cmd, args...)
  876. }
  877. func runPrintInDir(dir string, cmd string, args ...string) {
  878. if debug {
  879. t0 := time.Now()
  880. log.Println("runPrint:", cmd, strings.Join(args, " "))
  881. defer func() {
  882. log.Println("... in", time.Since(t0))
  883. }()
  884. }
  885. ecmd := exec.Command(cmd, args...)
  886. ecmd.Stdout = os.Stdout
  887. ecmd.Stderr = os.Stderr
  888. ecmd.Dir = dir
  889. err := ecmd.Run()
  890. if err != nil {
  891. log.Fatal(err)
  892. }
  893. }
  894. func runPipe(file, cmd string, args ...string) {
  895. if debug {
  896. t0 := time.Now()
  897. log.Println("runPipe:", cmd, strings.Join(args, " "))
  898. defer func() {
  899. log.Println("... in", time.Since(t0))
  900. }()
  901. }
  902. fd, err := os.Create(file)
  903. if err != nil {
  904. log.Fatal(err)
  905. }
  906. ecmd := exec.Command(cmd, args...)
  907. ecmd.Stdout = fd
  908. ecmd.Stderr = os.Stderr
  909. err = ecmd.Run()
  910. if err != nil {
  911. log.Fatal(err)
  912. }
  913. fd.Close()
  914. }
  915. func tarGz(out string, files []archiveFile) {
  916. fd, err := os.Create(out)
  917. if err != nil {
  918. log.Fatal(err)
  919. }
  920. gw, err := gzip.NewWriterLevel(fd, gzip.BestCompression)
  921. if err != nil {
  922. log.Fatal(err)
  923. }
  924. tw := tar.NewWriter(gw)
  925. for _, f := range files {
  926. sf, err := os.Open(f.src)
  927. if err != nil {
  928. log.Fatal(err)
  929. }
  930. info, err := sf.Stat()
  931. if err != nil {
  932. log.Fatal(err)
  933. }
  934. h := &tar.Header{
  935. Name: f.dst,
  936. Size: info.Size(),
  937. Mode: int64(info.Mode()),
  938. ModTime: info.ModTime(),
  939. }
  940. err = tw.WriteHeader(h)
  941. if err != nil {
  942. log.Fatal(err)
  943. }
  944. _, err = io.Copy(tw, sf)
  945. if err != nil {
  946. log.Fatal(err)
  947. }
  948. sf.Close()
  949. }
  950. err = tw.Close()
  951. if err != nil {
  952. log.Fatal(err)
  953. }
  954. err = gw.Close()
  955. if err != nil {
  956. log.Fatal(err)
  957. }
  958. err = fd.Close()
  959. if err != nil {
  960. log.Fatal(err)
  961. }
  962. }
  963. func zipFile(out string, files []archiveFile) {
  964. fd, err := os.Create(out)
  965. if err != nil {
  966. log.Fatal(err)
  967. }
  968. zw := zip.NewWriter(fd)
  969. var fw *flate.Writer
  970. // Register the deflator.
  971. zw.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {
  972. var err error
  973. if fw == nil {
  974. // Creating a flate compressor for every file is
  975. // expensive, create one and reuse it.
  976. fw, err = flate.NewWriter(out, flate.BestCompression)
  977. } else {
  978. fw.Reset(out)
  979. }
  980. return fw, err
  981. })
  982. for _, f := range files {
  983. sf, err := os.Open(f.src)
  984. if err != nil {
  985. log.Fatal(err)
  986. }
  987. info, err := sf.Stat()
  988. if err != nil {
  989. log.Fatal(err)
  990. }
  991. fh, err := zip.FileInfoHeader(info)
  992. if err != nil {
  993. log.Fatal(err)
  994. }
  995. fh.Name = filepath.ToSlash(f.dst)
  996. fh.Method = zip.Deflate
  997. if strings.HasSuffix(f.dst, ".txt") {
  998. // Text file. Read it and convert line endings.
  999. bs, err := ioutil.ReadAll(sf)
  1000. if err != nil {
  1001. log.Fatal(err)
  1002. }
  1003. bs = bytes.Replace(bs, []byte{'\n'}, []byte{'\n', '\r'}, -1)
  1004. fh.UncompressedSize = uint32(len(bs))
  1005. fh.UncompressedSize64 = uint64(len(bs))
  1006. of, err := zw.CreateHeader(fh)
  1007. if err != nil {
  1008. log.Fatal(err)
  1009. }
  1010. of.Write(bs)
  1011. } else {
  1012. // Binary file. Copy verbatim.
  1013. of, err := zw.CreateHeader(fh)
  1014. if err != nil {
  1015. log.Fatal(err)
  1016. }
  1017. _, err = io.Copy(of, sf)
  1018. if err != nil {
  1019. log.Fatal(err)
  1020. }
  1021. }
  1022. }
  1023. err = zw.Close()
  1024. if err != nil {
  1025. log.Fatal(err)
  1026. }
  1027. err = fd.Close()
  1028. if err != nil {
  1029. log.Fatal(err)
  1030. }
  1031. }
  1032. func codesign(target target) {
  1033. switch goos {
  1034. case "windows":
  1035. windowsCodesign(target.BinaryName())
  1036. case "darwin":
  1037. macosCodesign(target.BinaryName())
  1038. }
  1039. }
  1040. func macosCodesign(file string) {
  1041. if pass := os.Getenv("CODESIGN_KEYCHAIN_PASS"); pass != "" {
  1042. bs, err := runError("security", "unlock-keychain", "-p", pass)
  1043. if err != nil {
  1044. log.Println("Codesign: unlocking keychain failed:", string(bs))
  1045. return
  1046. }
  1047. }
  1048. if id := os.Getenv("CODESIGN_IDENTITY"); id != "" {
  1049. bs, err := runError("codesign", "-s", id, file)
  1050. if err != nil {
  1051. log.Println("Codesign: signing failed:", string(bs))
  1052. return
  1053. }
  1054. log.Println("Codesign: successfully signed", file)
  1055. }
  1056. }
  1057. func windowsCodesign(file string) {
  1058. st := "signtool.exe"
  1059. if path := os.Getenv("CODESIGN_SIGNTOOL"); path != "" {
  1060. st = path
  1061. }
  1062. for i, algo := range []string{"sha1", "sha256"} {
  1063. args := []string{"sign", "/fd", algo}
  1064. if f := os.Getenv("CODESIGN_CERTIFICATE_FILE"); f != "" {
  1065. args = append(args, "/f", f)
  1066. }
  1067. if p := os.Getenv("CODESIGN_CERTIFICATE_PASSWORD"); p != "" {
  1068. args = append(args, "/p", p)
  1069. }
  1070. if tr := os.Getenv("CODESIGN_TIMESTAMP_SERVER"); tr != "" {
  1071. switch algo {
  1072. case "sha256":
  1073. args = append(args, "/tr", tr, "/td", algo)
  1074. default:
  1075. args = append(args, "/t", tr)
  1076. }
  1077. }
  1078. if i > 0 {
  1079. args = append(args, "/as")
  1080. }
  1081. args = append(args, file)
  1082. bs, err := runError(st, args...)
  1083. if err != nil {
  1084. log.Println("Codesign: signing failed:", string(bs))
  1085. return
  1086. }
  1087. log.Println("Codesign: successfully signed", file, "using", algo)
  1088. }
  1089. }
  1090. func metalint() {
  1091. lazyRebuildAssets()
  1092. runPrint(goCmd, "test", "-run", "Metalint", "./meta")
  1093. }
  1094. func metalintShort() {
  1095. lazyRebuildAssets()
  1096. runPrint(goCmd, "test", "-short", "-run", "Metalint", "./meta")
  1097. }
  1098. func temporaryBuildDir() (string, error) {
  1099. // The base of our temp dir is "syncthing-xxxxxxxx" where the x:es
  1100. // are eight bytes from the sha256 of our working directory. We do
  1101. // this because we want a name in the global temp dir that doesn't
  1102. // conflict with someone else building syncthing on the same
  1103. // machine, yet is persistent between runs from the same source
  1104. // directory.
  1105. wd, err := os.Getwd()
  1106. if err != nil {
  1107. return "", err
  1108. }
  1109. hash := sha256.Sum256([]byte(wd))
  1110. base := fmt.Sprintf("syncthing-%x", hash[:4])
  1111. // The temp dir is taken from $STTMPDIR if set, otherwise the system
  1112. // default (potentially infrluenced by $TMPDIR on unixes).
  1113. var tmpDir string
  1114. if t := os.Getenv("STTMPDIR"); t != "" {
  1115. tmpDir = t
  1116. } else {
  1117. tmpDir = os.TempDir()
  1118. }
  1119. return filepath.Join(tmpDir, base), nil
  1120. }
  1121. func (t target) BinaryName() string {
  1122. if goos == "windows" {
  1123. return t.binaryName + ".exe"
  1124. }
  1125. return t.binaryName
  1126. }
  1127. func protobufVersion() string {
  1128. bs, err := runError(goCmd, "list", "-f", "{{.Version}}", "-m", "github.com/gogo/protobuf")
  1129. if err != nil {
  1130. log.Fatal("Getting protobuf version:", err)
  1131. }
  1132. return string(bs)
  1133. }