build.go 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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/gzip"
  13. "errors"
  14. "flag"
  15. "fmt"
  16. "io"
  17. "io/ioutil"
  18. "log"
  19. "os"
  20. "os/exec"
  21. "os/user"
  22. "path/filepath"
  23. "regexp"
  24. "runtime"
  25. "strconv"
  26. "strings"
  27. "text/template"
  28. "time"
  29. )
  30. var (
  31. versionRe = regexp.MustCompile(`-[0-9]{1,3}-g[0-9a-f]{5,10}`)
  32. goarch string
  33. goos string
  34. noupgrade bool
  35. version string
  36. goVersion float64
  37. race bool
  38. debug = os.Getenv("BUILDDEBUG") != ""
  39. )
  40. type target struct {
  41. name string
  42. buildPkg string
  43. binaryName string
  44. archiveFiles []archiveFile
  45. installationFiles []archiveFile
  46. tags []string
  47. }
  48. type archiveFile struct {
  49. src string
  50. dst string
  51. perm os.FileMode
  52. }
  53. var targets = map[string]target{
  54. "all": {
  55. // Only valid for the "build" and "install" commands as it lacks all
  56. // the archive creation stuff.
  57. buildPkg: "./cmd/...",
  58. tags: []string{"purego"},
  59. },
  60. "syncthing": {
  61. // The default target for "build", "install", "tar", "zip", "deb", etc.
  62. name: "syncthing",
  63. buildPkg: "./cmd/syncthing",
  64. binaryName: "syncthing", // .exe will be added automatically for Windows builds
  65. archiveFiles: []archiveFile{
  66. {src: "{{binary}}", dst: "{{binary}}", perm: 0755},
  67. {src: "README.md", dst: "README.txt", perm: 0644},
  68. {src: "LICENSE", dst: "LICENSE.txt", perm: 0644},
  69. {src: "AUTHORS", dst: "AUTHORS.txt", perm: 0644},
  70. // All files from etc/ and extra/ added automatically in init().
  71. },
  72. installationFiles: []archiveFile{
  73. {src: "{{binary}}", dst: "deb/usr/bin/{{binary}}", perm: 0755},
  74. {src: "README.md", dst: "deb/usr/share/doc/syncthing/README.txt", perm: 0644},
  75. {src: "LICENSE", dst: "deb/usr/share/doc/syncthing/LICENSE.txt", perm: 0644},
  76. {src: "AUTHORS", dst: "deb/usr/share/doc/syncthing/AUTHORS.txt", perm: 0644},
  77. {src: "man/syncthing.1", dst: "deb/usr/share/man/man1/syncthing.1", perm: 0644},
  78. {src: "man/syncthing-config.5", dst: "deb/usr/share/man/man5/syncthing-config.5", perm: 0644},
  79. {src: "man/syncthing-stignore.5", dst: "deb/usr/share/man/man5/syncthing-stignore.5", perm: 0644},
  80. {src: "man/syncthing-device-ids.7", dst: "deb/usr/share/man/man7/syncthing-device-ids.7", perm: 0644},
  81. {src: "man/syncthing-event-api.7", dst: "deb/usr/share/man/man7/syncthing-event-api.7", perm: 0644},
  82. {src: "man/syncthing-faq.7", dst: "deb/usr/share/man/man7/syncthing-faq.7", perm: 0644},
  83. {src: "man/syncthing-networking.7", dst: "deb/usr/share/man/man7/syncthing-networking.7", perm: 0644},
  84. {src: "man/syncthing-rest-api.7", dst: "deb/usr/share/man/man7/syncthing-rest-api.7", perm: 0644},
  85. {src: "man/syncthing-security.7", dst: "deb/usr/share/man/man7/syncthing-security.7", perm: 0644},
  86. {src: "man/syncthing-versioning.7", dst: "deb/usr/share/man/man7/syncthing-versioning.7", perm: 0644},
  87. {src: "etc/linux-systemd/system/[email protected]", dst: "deb/lib/systemd/system/[email protected]", perm: 0644},
  88. {src: "etc/linux-systemd/system/syncthing-resume.service", dst: "deb/lib/systemd/system/syncthing-resume.service", perm: 0644},
  89. {src: "etc/linux-systemd/user/syncthing.service", dst: "deb/usr/lib/systemd/user/syncthing.service", perm: 0644},
  90. {src: "etc/firewall-ufw/syncthing", dst: "deb/etc/ufw/applications.d/syncthing", perm: 0644},
  91. },
  92. },
  93. "stdiscosrv": {
  94. name: "stdiscosrv",
  95. buildPkg: "./cmd/stdiscosrv",
  96. binaryName: "stdiscosrv", // .exe will be added automatically for Windows builds
  97. archiveFiles: []archiveFile{
  98. {src: "{{binary}}", dst: "{{binary}}", perm: 0755},
  99. {src: "cmd/stdiscosrv/README.md", dst: "README.txt", perm: 0644},
  100. {src: "cmd/stdiscosrv/LICENSE", dst: "LICENSE.txt", perm: 0644},
  101. {src: "AUTHORS", dst: "AUTHORS.txt", perm: 0644},
  102. },
  103. installationFiles: []archiveFile{
  104. {src: "{{binary}}", dst: "deb/usr/bin/{{binary}}", perm: 0755},
  105. {src: "cmd/stdiscosrv/README.md", dst: "deb/usr/share/doc/stdiscosrv/README.txt", perm: 0644},
  106. {src: "cmd/stdiscosrv/LICENSE", dst: "deb/usr/share/doc/stdiscosrv/LICENSE.txt", perm: 0644},
  107. {src: "AUTHORS", dst: "deb/usr/share/doc/stdiscosrv/AUTHORS.txt", perm: 0644},
  108. {src: "man/stdiscosrv.1", dst: "deb/usr/share/man/man1/stdiscosrv.1", perm: 0644},
  109. },
  110. tags: []string{"purego"},
  111. },
  112. "strelaysrv": {
  113. name: "strelaysrv",
  114. buildPkg: "./cmd/strelaysrv",
  115. binaryName: "strelaysrv", // .exe will be added automatically for Windows builds
  116. archiveFiles: []archiveFile{
  117. {src: "{{binary}}", dst: "{{binary}}", perm: 0755},
  118. {src: "cmd/strelaysrv/README.md", dst: "README.txt", perm: 0644},
  119. {src: "cmd/strelaysrv/LICENSE", dst: "LICENSE.txt", perm: 0644},
  120. {src: "AUTHORS", dst: "AUTHORS.txt", perm: 0644},
  121. },
  122. installationFiles: []archiveFile{
  123. {src: "{{binary}}", dst: "deb/usr/bin/{{binary}}", perm: 0755},
  124. {src: "cmd/strelaysrv/README.md", dst: "deb/usr/share/doc/strelaysrv/README.txt", perm: 0644},
  125. {src: "cmd/strelaysrv/LICENSE", dst: "deb/usr/share/doc/strelaysrv/LICENSE.txt", perm: 0644},
  126. {src: "AUTHORS", dst: "deb/usr/share/doc/strelaysrv/AUTHORS.txt", perm: 0644},
  127. {src: "man/strelaysrv.1", dst: "deb/usr/share/man/man1/strelaysrv.1", perm: 0644},
  128. },
  129. },
  130. "strelaypoolsrv": {
  131. name: "strelaypoolsrv",
  132. buildPkg: "./cmd/strelaypoolsrv",
  133. binaryName: "strelaypoolsrv", // .exe will be added automatically for Windows builds
  134. archiveFiles: []archiveFile{
  135. {src: "{{binary}}", dst: "{{binary}}", perm: 0755},
  136. {src: "cmd/strelaypoolsrv/README.md", dst: "README.txt", perm: 0644},
  137. {src: "cmd/strelaypoolsrv/LICENSE", dst: "LICENSE.txt", perm: 0644},
  138. {src: "AUTHORS", dst: "AUTHORS.txt", perm: 0644},
  139. },
  140. installationFiles: []archiveFile{
  141. {src: "{{binary}}", dst: "deb/usr/bin/{{binary}}", perm: 0755},
  142. {src: "cmd/strelaypoolsrv/README.md", dst: "deb/usr/share/doc/relaysrv/README.txt", perm: 0644},
  143. {src: "cmd/strelaypoolsrv/LICENSE", dst: "deb/usr/share/doc/relaysrv/LICENSE.txt", perm: 0644},
  144. {src: "AUTHORS", dst: "deb/usr/share/doc/relaysrv/AUTHORS.txt", perm: 0644},
  145. },
  146. },
  147. }
  148. var (
  149. // fast linters complete in a fraction of a second and might as well be
  150. // run always as part of the build
  151. fastLinters = []string{
  152. "deadcode",
  153. "golint",
  154. "ineffassign",
  155. "vet",
  156. }
  157. // slow linters take several seconds and are run only as part of the
  158. // "metalint" command.
  159. slowLinters = []string{
  160. "gosimple",
  161. "staticcheck",
  162. "structcheck",
  163. "unused",
  164. "varcheck",
  165. }
  166. // Which parts of the tree to lint
  167. lintDirs = []string{".", "./lib/...", "./cmd/..."}
  168. // Messages to ignore
  169. lintExcludes = []string{
  170. ".pb.go",
  171. "should have comment",
  172. "protocol.Vector composite literal uses unkeyed fields",
  173. "cli.Requires composite literal uses unkeyed fields",
  174. "Use DialContext instead", // Go 1.7
  175. "os.SEEK_SET is deprecated", // Go 1.7
  176. }
  177. )
  178. func init() {
  179. // The "syncthing" target includes a few more files found in the "etc"
  180. // and "extra" dirs.
  181. syncthingPkg := targets["syncthing"]
  182. for _, file := range listFiles("etc") {
  183. syncthingPkg.archiveFiles = append(syncthingPkg.archiveFiles, archiveFile{src: file, dst: file, perm: 0644})
  184. }
  185. for _, file := range listFiles("extra") {
  186. syncthingPkg.archiveFiles = append(syncthingPkg.archiveFiles, archiveFile{src: file, dst: file, perm: 0644})
  187. }
  188. for _, file := range listFiles("extra") {
  189. syncthingPkg.installationFiles = append(syncthingPkg.installationFiles, archiveFile{src: file, dst: "deb/usr/share/doc/syncthing/" + filepath.Base(file), perm: 0644})
  190. }
  191. targets["syncthing"] = syncthingPkg
  192. }
  193. const minGoVersion = 1.5
  194. func main() {
  195. log.SetOutput(os.Stdout)
  196. log.SetFlags(0)
  197. if debug {
  198. t0 := time.Now()
  199. defer func() {
  200. log.Println("... build completed in", time.Since(t0))
  201. }()
  202. }
  203. if os.Getenv("GOPATH") == "" {
  204. setGoPath()
  205. }
  206. // We use Go 1.5+ vendoring.
  207. os.Setenv("GO15VENDOREXPERIMENT", "1")
  208. // Set path to $GOPATH/bin:$PATH so that we can for sure find tools we
  209. // might have installed during "build.go setup".
  210. os.Setenv("PATH", fmt.Sprintf("%s%cbin%c%s", os.Getenv("GOPATH"), os.PathSeparator, os.PathListSeparator, os.Getenv("PATH")))
  211. parseFlags()
  212. checkArchitecture()
  213. goVersion, _ = checkRequiredGoVersion()
  214. // Invoking build.go with no parameters at all builds everything (incrementally),
  215. // which is what you want for maximum error checking during development.
  216. if flag.NArg() == 0 {
  217. runCommand("install", targets["all"])
  218. } else {
  219. // with any command given but not a target, the target is
  220. // "syncthing". So "go run build.go install" is "go run build.go install
  221. // syncthing" etc.
  222. targetName := "syncthing"
  223. if flag.NArg() > 1 {
  224. targetName = flag.Arg(1)
  225. }
  226. target, ok := targets[targetName]
  227. if !ok {
  228. log.Fatalln("Unknown target", target)
  229. }
  230. runCommand(flag.Arg(0), target)
  231. }
  232. }
  233. func checkArchitecture() {
  234. switch goarch {
  235. case "386", "amd64", "arm", "arm64", "ppc64", "ppc64le":
  236. break
  237. default:
  238. log.Printf("Unknown goarch %q; proceed with caution!", goarch)
  239. }
  240. }
  241. func runCommand(cmd string, target target) {
  242. switch cmd {
  243. case "setup":
  244. setup()
  245. case "install":
  246. var tags []string
  247. if noupgrade {
  248. tags = []string{"noupgrade"}
  249. }
  250. install(target, tags)
  251. metalint(fastLinters, lintDirs)
  252. case "build":
  253. var tags []string
  254. if noupgrade {
  255. tags = []string{"noupgrade"}
  256. }
  257. build(target, tags)
  258. metalint(fastLinters, lintDirs)
  259. case "test":
  260. test("./lib/...", "./cmd/...")
  261. case "bench":
  262. bench("./lib/...", "./cmd/...")
  263. case "assets":
  264. rebuildAssets()
  265. case "proto":
  266. proto()
  267. case "translate":
  268. translate()
  269. case "transifex":
  270. transifex()
  271. case "tar":
  272. buildTar(target)
  273. case "zip":
  274. buildZip(target)
  275. case "deb":
  276. buildDeb(target)
  277. case "snap":
  278. buildSnap(target)
  279. case "clean":
  280. clean()
  281. case "vet":
  282. metalint(fastLinters, lintDirs)
  283. case "lint":
  284. metalint(fastLinters, lintDirs)
  285. case "metalint":
  286. metalint(fastLinters, lintDirs)
  287. metalint(slowLinters, lintDirs)
  288. case "version":
  289. fmt.Println(getVersion())
  290. default:
  291. log.Fatalf("Unknown command %q", cmd)
  292. }
  293. }
  294. // setGoPath sets GOPATH correctly with the assumption that we are
  295. // in $GOPATH/src/github.com/syncthing/syncthing.
  296. func setGoPath() {
  297. cwd, err := os.Getwd()
  298. if err != nil {
  299. log.Fatal(err)
  300. }
  301. gopath := filepath.Clean(filepath.Join(cwd, "../../../../"))
  302. log.Println("GOPATH is", gopath)
  303. os.Setenv("GOPATH", gopath)
  304. }
  305. func parseFlags() {
  306. flag.StringVar(&goarch, "goarch", runtime.GOARCH, "GOARCH")
  307. flag.StringVar(&goos, "goos", runtime.GOOS, "GOOS")
  308. flag.BoolVar(&noupgrade, "no-upgrade", noupgrade, "Disable upgrade functionality")
  309. flag.StringVar(&version, "version", getVersion(), "Set compiled in version string")
  310. flag.BoolVar(&race, "race", race, "Use race detector")
  311. flag.Parse()
  312. }
  313. func checkRequiredGoVersion() (float64, bool) {
  314. re := regexp.MustCompile(`go(\d+\.\d+)`)
  315. ver := runtime.Version()
  316. if m := re.FindStringSubmatch(ver); len(m) == 2 {
  317. vs := string(m[1])
  318. // This is a standard go build. Verify that it's new enough.
  319. f, err := strconv.ParseFloat(vs, 64)
  320. if err != nil {
  321. log.Printf("*** Couldn't parse Go version out of %q.\n*** This isn't known to work, proceed on your own risk.", vs)
  322. return 0, false
  323. }
  324. if f < 1.5 {
  325. log.Printf("*** Go version %.01f doesn't support the vendoring mechanism.\n*** Ensure correct dependencies in your $GOPATH.", f)
  326. } else if f < minGoVersion {
  327. log.Fatalf("*** Go version %.01f is less than required %.01f.\n*** This is known not to work, not proceeding.", f, minGoVersion)
  328. }
  329. return f, true
  330. }
  331. log.Printf("*** Unknown Go version %q.\n*** This isn't known to work, proceed on your own risk.", ver)
  332. return 0, false
  333. }
  334. func setup() {
  335. packages := []string{
  336. "github.com/alecthomas/gometalinter",
  337. "github.com/AlekSi/gocov-xml",
  338. "github.com/axw/gocov/gocov",
  339. "github.com/FiloSottile/gvt",
  340. "github.com/golang/lint/golint",
  341. "github.com/gordonklaus/ineffassign",
  342. "github.com/mdempsky/unconvert",
  343. "github.com/mitchellh/go-wordwrap",
  344. "github.com/opennota/check/cmd/...",
  345. "github.com/tsenart/deadcode",
  346. "golang.org/x/net/html",
  347. "golang.org/x/tools/cmd/cover",
  348. "honnef.co/go/simple/cmd/gosimple",
  349. "honnef.co/go/staticcheck/cmd/staticcheck",
  350. "honnef.co/go/unused/cmd/unused",
  351. }
  352. for _, pkg := range packages {
  353. fmt.Println(pkg)
  354. runPrint("go", "get", "-u", pkg)
  355. }
  356. runPrint("go", "install", "-v", "./vendor/github.com/gogo/protobuf/protoc-gen-gogofast")
  357. }
  358. func test(pkgs ...string) {
  359. lazyRebuildAssets()
  360. useRace := runtime.GOARCH == "amd64"
  361. switch runtime.GOOS {
  362. case "darwin", "linux", "freebsd", "windows":
  363. default:
  364. useRace = false
  365. }
  366. if useRace {
  367. runPrint("go", append([]string{"test", "-short", "-race", "-timeout", "60s", "-tags", "purego"}, pkgs...)...)
  368. } else {
  369. runPrint("go", append([]string{"test", "-short", "-timeout", "60s", "-tags", "purego"}, pkgs...)...)
  370. }
  371. }
  372. func bench(pkgs ...string) {
  373. lazyRebuildAssets()
  374. runPrint("go", append([]string{"test", "-run", "NONE", "-bench", "."}, pkgs...)...)
  375. }
  376. func install(target target, tags []string) {
  377. lazyRebuildAssets()
  378. tags = append(target.tags, tags...)
  379. cwd, err := os.Getwd()
  380. if err != nil {
  381. log.Fatal(err)
  382. }
  383. os.Setenv("GOBIN", filepath.Join(cwd, "bin"))
  384. args := []string{"install", "-v", "-ldflags", ldflags()}
  385. if len(tags) > 0 {
  386. args = append(args, "-tags", strings.Join(tags, " "))
  387. }
  388. if race {
  389. args = append(args, "-race")
  390. }
  391. args = append(args, target.buildPkg)
  392. os.Setenv("GOOS", goos)
  393. os.Setenv("GOARCH", goarch)
  394. runPrint("go", args...)
  395. }
  396. func build(target target, tags []string) {
  397. lazyRebuildAssets()
  398. tags = append(target.tags, tags...)
  399. rmr(target.binaryName)
  400. args := []string{"build", "-i", "-v", "-ldflags", ldflags()}
  401. if len(tags) > 0 {
  402. args = append(args, "-tags", strings.Join(tags, " "))
  403. }
  404. if race {
  405. args = append(args, "-race")
  406. }
  407. args = append(args, target.buildPkg)
  408. os.Setenv("GOOS", goos)
  409. os.Setenv("GOARCH", goarch)
  410. runPrint("go", args...)
  411. }
  412. func buildTar(target target) {
  413. name := archiveName(target)
  414. filename := name + ".tar.gz"
  415. var tags []string
  416. if noupgrade {
  417. tags = []string{"noupgrade"}
  418. name += "-noupgrade"
  419. }
  420. build(target, tags)
  421. if goos == "darwin" {
  422. macosCodesign(target.binaryName)
  423. }
  424. for i := range target.archiveFiles {
  425. target.archiveFiles[i].src = strings.Replace(target.archiveFiles[i].src, "{{binary}}", target.binaryName, 1)
  426. target.archiveFiles[i].dst = strings.Replace(target.archiveFiles[i].dst, "{{binary}}", target.binaryName, 1)
  427. target.archiveFiles[i].dst = name + "/" + target.archiveFiles[i].dst
  428. }
  429. tarGz(filename, target.archiveFiles)
  430. log.Println(filename)
  431. }
  432. func buildZip(target target) {
  433. target.binaryName += ".exe"
  434. name := archiveName(target)
  435. filename := name + ".zip"
  436. var tags []string
  437. if noupgrade {
  438. tags = []string{"noupgrade"}
  439. name += "-noupgrade"
  440. }
  441. build(target, tags)
  442. for i := range target.archiveFiles {
  443. target.archiveFiles[i].src = strings.Replace(target.archiveFiles[i].src, "{{binary}}", target.binaryName, 1)
  444. target.archiveFiles[i].dst = strings.Replace(target.archiveFiles[i].dst, "{{binary}}", target.binaryName, 1)
  445. target.archiveFiles[i].dst = name + "/" + target.archiveFiles[i].dst
  446. }
  447. zipFile(filename, target.archiveFiles)
  448. log.Println(filename)
  449. }
  450. func buildDeb(target target) {
  451. os.RemoveAll("deb")
  452. // "goarch" here is set to whatever the Debian packages expect. We correct
  453. // it to what we actually know how to build and keep the Debian variant
  454. // name in "debarch".
  455. debarch := goarch
  456. switch goarch {
  457. case "i386":
  458. goarch = "386"
  459. case "armel", "armhf":
  460. goarch = "arm"
  461. }
  462. build(target, []string{"noupgrade"})
  463. for i := range target.installationFiles {
  464. target.installationFiles[i].src = strings.Replace(target.installationFiles[i].src, "{{binary}}", target.binaryName, 1)
  465. target.installationFiles[i].dst = strings.Replace(target.installationFiles[i].dst, "{{binary}}", target.binaryName, 1)
  466. }
  467. for _, af := range target.installationFiles {
  468. if err := copyFile(af.src, af.dst, af.perm); err != nil {
  469. log.Fatal(err)
  470. }
  471. }
  472. maintainer := "Syncthing Release Management <[email protected]>"
  473. debver := version
  474. if strings.HasPrefix(debver, "v") {
  475. debver = debver[1:]
  476. }
  477. runPrint("fpm", "-t", "deb", "-s", "dir", "-C", "deb",
  478. "-n", "syncthing", "-v", debver, "-a", debarch,
  479. "--vendor", maintainer, "-m", maintainer,
  480. "-d", "libc6",
  481. "-d", "procps", // because postinst script
  482. "--url", "https://syncthing.net/",
  483. "--description", "Open Source Continuous File Synchronization",
  484. "--after-upgrade", "script/post-upgrade",
  485. "--license", "MPL-2")
  486. }
  487. func buildSnap(target target) {
  488. os.RemoveAll("snap")
  489. tmpl, err := template.ParseFiles("snapcraft.yaml.template")
  490. if err != nil {
  491. log.Fatal(err)
  492. }
  493. f, err := os.Create("snapcraft.yaml")
  494. defer f.Close()
  495. if err != nil {
  496. log.Fatal(err)
  497. }
  498. snaparch := goarch
  499. if snaparch == "armhf" {
  500. goarch = "arm"
  501. }
  502. snapver := version
  503. if strings.HasPrefix(snapver, "v") {
  504. snapver = snapver[1:]
  505. }
  506. snapgrade := "devel"
  507. if matched, _ := regexp.MatchString(`^\d+\.\d+\.\d+(-rc.\d+)?$`, snapver); matched {
  508. snapgrade = "stable"
  509. }
  510. err = tmpl.Execute(f, map[string]string{
  511. "Version": snapver,
  512. "Architecture": snaparch,
  513. "Grade": snapgrade,
  514. })
  515. if err != nil {
  516. log.Fatal(err)
  517. }
  518. runPrint("snapcraft", "clean")
  519. build(target, []string{"noupgrade"})
  520. runPrint("snapcraft")
  521. }
  522. func copyFile(src, dst string, perm os.FileMode) error {
  523. dstDir := filepath.Dir(dst)
  524. os.MkdirAll(dstDir, 0755) // ignore error
  525. srcFd, err := os.Open(src)
  526. if err != nil {
  527. return err
  528. }
  529. defer srcFd.Close()
  530. dstFd, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, perm)
  531. if err != nil {
  532. return err
  533. }
  534. defer dstFd.Close()
  535. _, err = io.Copy(dstFd, srcFd)
  536. return err
  537. }
  538. func listFiles(dir string) []string {
  539. var res []string
  540. filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error {
  541. if err != nil {
  542. return err
  543. }
  544. if fi.Mode().IsRegular() {
  545. res = append(res, path)
  546. }
  547. return nil
  548. })
  549. return res
  550. }
  551. func rebuildAssets() {
  552. runPipe("lib/auto/gui.files.go", "go", "run", "script/genassets.go", "gui")
  553. runPipe("cmd/strelaypoolsrv/auto/gui.go", "go", "run", "script/genassets.go", "cmd/strelaypoolsrv/gui")
  554. }
  555. func lazyRebuildAssets() {
  556. if shouldRebuildAssets("lib/auto/gui.files.go", "gui") || shouldRebuildAssets("cmd/strelaypoolsrv/auto/gui.go", "cmd/strelaypoolsrv/auto/gui") {
  557. rebuildAssets()
  558. }
  559. }
  560. func shouldRebuildAssets(target, srcdir string) bool {
  561. info, err := os.Stat(target)
  562. if err != nil {
  563. // If the file doesn't exist, we must rebuild it
  564. return true
  565. }
  566. // Check if any of the files in gui/ are newer than the asset file. If
  567. // so we should rebuild it.
  568. currentBuild := info.ModTime()
  569. assetsAreNewer := false
  570. stop := errors.New("no need to iterate further")
  571. filepath.Walk(srcdir, func(path string, info os.FileInfo, err error) error {
  572. if err != nil {
  573. return err
  574. }
  575. if info.ModTime().After(currentBuild) {
  576. assetsAreNewer = true
  577. return stop
  578. }
  579. return nil
  580. })
  581. return assetsAreNewer
  582. }
  583. func proto() {
  584. runPrint("go", "generate", "./lib/...")
  585. }
  586. func translate() {
  587. os.Chdir("gui/default/assets/lang")
  588. runPipe("lang-en-new.json", "go", "run", "../../../../script/translate.go", "lang-en.json", "../../../")
  589. os.Remove("lang-en.json")
  590. err := os.Rename("lang-en-new.json", "lang-en.json")
  591. if err != nil {
  592. log.Fatal(err)
  593. }
  594. os.Chdir("../../../..")
  595. }
  596. func transifex() {
  597. os.Chdir("gui/default/assets/lang")
  598. runPrint("go", "run", "../../../../script/transifexdl.go")
  599. }
  600. func clean() {
  601. rmr("bin")
  602. rmr(filepath.Join(os.Getenv("GOPATH"), fmt.Sprintf("pkg/%s_%s/github.com/syncthing", goos, goarch)))
  603. }
  604. func ldflags() string {
  605. sep := '='
  606. if goVersion > 0 && goVersion < 1.5 {
  607. sep = ' '
  608. }
  609. b := new(bytes.Buffer)
  610. b.WriteString("-w")
  611. fmt.Fprintf(b, " -X main.Version%c%s", sep, version)
  612. fmt.Fprintf(b, " -X main.BuildStamp%c%d", sep, buildStamp())
  613. fmt.Fprintf(b, " -X main.BuildUser%c%s", sep, buildUser())
  614. fmt.Fprintf(b, " -X main.BuildHost%c%s", sep, buildHost())
  615. return b.String()
  616. }
  617. func rmr(paths ...string) {
  618. for _, path := range paths {
  619. if debug {
  620. log.Println("rm -r", path)
  621. }
  622. os.RemoveAll(path)
  623. }
  624. }
  625. func getReleaseVersion() (string, error) {
  626. fd, err := os.Open("RELEASE")
  627. if err != nil {
  628. return "", err
  629. }
  630. defer fd.Close()
  631. bs, err := ioutil.ReadAll(fd)
  632. if err != nil {
  633. return "", err
  634. }
  635. return string(bytes.TrimSpace(bs)), nil
  636. }
  637. func getGitVersion() (string, error) {
  638. v, err := runError("git", "describe", "--always", "--dirty")
  639. if err != nil {
  640. return "", err
  641. }
  642. v = versionRe.ReplaceAllFunc(v, func(s []byte) []byte {
  643. s[0] = '+'
  644. return s
  645. })
  646. return string(v), nil
  647. }
  648. func getVersion() string {
  649. // First try for a RELEASE file,
  650. if ver, err := getReleaseVersion(); err == nil {
  651. return ver
  652. }
  653. // ... then see if we have a Git tag.
  654. if ver, err := getGitVersion(); err == nil {
  655. if strings.Contains(ver, "-") {
  656. // The version already contains a hash and stuff. See if we can
  657. // find a current branch name to tack onto it as well.
  658. return ver + getBranchSuffix()
  659. }
  660. return ver
  661. }
  662. // This seems to be a dev build.
  663. return "unknown-dev"
  664. }
  665. func getBranchSuffix() string {
  666. bs, err := runError("git", "branch", "-a", "--contains")
  667. if err != nil {
  668. return ""
  669. }
  670. branches := strings.Split(string(bs), "\n")
  671. if len(branches) == 0 {
  672. return ""
  673. }
  674. branch := ""
  675. for i, candidate := range branches {
  676. if strings.HasPrefix(candidate, "*") {
  677. // This is the current branch. Select it!
  678. branch = strings.TrimLeft(candidate, " \t*")
  679. break
  680. } else if i == 0 {
  681. // Otherwise the first branch in the list will do.
  682. branch = strings.TrimSpace(branch)
  683. }
  684. }
  685. if branch == "" {
  686. return ""
  687. }
  688. // The branch name may be on the form "remotes/origin/foo" from which we
  689. // just want "foo".
  690. parts := strings.Split(branch, "/")
  691. if len(parts) == 0 || len(parts[len(parts)-1]) == 0 {
  692. return ""
  693. }
  694. branch = parts[len(parts)-1]
  695. if branch == "master" {
  696. // master builds are the default.
  697. return ""
  698. }
  699. validBranchRe := regexp.MustCompile(`^[a-zA-Z0-9_.-]+$`)
  700. if !validBranchRe.MatchString(branch) {
  701. // There's some odd stuff in the branch name. Better skip it.
  702. return ""
  703. }
  704. return "-" + branch
  705. }
  706. func buildStamp() int64 {
  707. // If SOURCE_DATE_EPOCH is set, use that.
  708. if s, _ := strconv.ParseInt(os.Getenv("SOURCE_DATE_EPOCH"), 10, 64); s > 0 {
  709. return s
  710. }
  711. // Try to get the timestamp of the latest commit.
  712. bs, err := runError("git", "show", "-s", "--format=%ct")
  713. if err != nil {
  714. // Fall back to "now".
  715. return time.Now().Unix()
  716. }
  717. s, _ := strconv.ParseInt(string(bs), 10, 64)
  718. return s
  719. }
  720. func buildUser() string {
  721. if v := os.Getenv("BUILD_USER"); v != "" {
  722. return v
  723. }
  724. u, err := user.Current()
  725. if err != nil {
  726. return "unknown-user"
  727. }
  728. return strings.Replace(u.Username, " ", "-", -1)
  729. }
  730. func buildHost() string {
  731. if v := os.Getenv("BUILD_HOST"); v != "" {
  732. return v
  733. }
  734. h, err := os.Hostname()
  735. if err != nil {
  736. return "unknown-host"
  737. }
  738. return h
  739. }
  740. func buildArch() string {
  741. os := goos
  742. if os == "darwin" {
  743. os = "macosx"
  744. }
  745. return fmt.Sprintf("%s-%s", os, goarch)
  746. }
  747. func archiveName(target target) string {
  748. return fmt.Sprintf("%s-%s-%s", target.name, buildArch(), version)
  749. }
  750. func runError(cmd string, args ...string) ([]byte, error) {
  751. if debug {
  752. t0 := time.Now()
  753. log.Println("runError:", cmd, strings.Join(args, " "))
  754. defer func() {
  755. log.Println("... in", time.Since(t0))
  756. }()
  757. }
  758. ecmd := exec.Command(cmd, args...)
  759. bs, err := ecmd.CombinedOutput()
  760. return bytes.TrimSpace(bs), err
  761. }
  762. func runPrint(cmd string, args ...string) {
  763. if debug {
  764. t0 := time.Now()
  765. log.Println("runPrint:", cmd, strings.Join(args, " "))
  766. defer func() {
  767. log.Println("... in", time.Since(t0))
  768. }()
  769. }
  770. ecmd := exec.Command(cmd, args...)
  771. ecmd.Stdout = os.Stdout
  772. ecmd.Stderr = os.Stderr
  773. err := ecmd.Run()
  774. if err != nil {
  775. log.Fatal(err)
  776. }
  777. }
  778. func runPipe(file, cmd string, args ...string) {
  779. if debug {
  780. t0 := time.Now()
  781. log.Println("runPipe:", cmd, strings.Join(args, " "))
  782. defer func() {
  783. log.Println("... in", time.Since(t0))
  784. }()
  785. }
  786. fd, err := os.Create(file)
  787. if err != nil {
  788. log.Fatal(err)
  789. }
  790. ecmd := exec.Command(cmd, args...)
  791. ecmd.Stdout = fd
  792. ecmd.Stderr = os.Stderr
  793. err = ecmd.Run()
  794. if err != nil {
  795. log.Fatal(err)
  796. }
  797. fd.Close()
  798. }
  799. func tarGz(out string, files []archiveFile) {
  800. fd, err := os.Create(out)
  801. if err != nil {
  802. log.Fatal(err)
  803. }
  804. gw := gzip.NewWriter(fd)
  805. tw := tar.NewWriter(gw)
  806. for _, f := range files {
  807. sf, err := os.Open(f.src)
  808. if err != nil {
  809. log.Fatal(err)
  810. }
  811. info, err := sf.Stat()
  812. if err != nil {
  813. log.Fatal(err)
  814. }
  815. h := &tar.Header{
  816. Name: f.dst,
  817. Size: info.Size(),
  818. Mode: int64(info.Mode()),
  819. ModTime: info.ModTime(),
  820. }
  821. err = tw.WriteHeader(h)
  822. if err != nil {
  823. log.Fatal(err)
  824. }
  825. _, err = io.Copy(tw, sf)
  826. if err != nil {
  827. log.Fatal(err)
  828. }
  829. sf.Close()
  830. }
  831. err = tw.Close()
  832. if err != nil {
  833. log.Fatal(err)
  834. }
  835. err = gw.Close()
  836. if err != nil {
  837. log.Fatal(err)
  838. }
  839. err = fd.Close()
  840. if err != nil {
  841. log.Fatal(err)
  842. }
  843. }
  844. func zipFile(out string, files []archiveFile) {
  845. fd, err := os.Create(out)
  846. if err != nil {
  847. log.Fatal(err)
  848. }
  849. zw := zip.NewWriter(fd)
  850. for _, f := range files {
  851. sf, err := os.Open(f.src)
  852. if err != nil {
  853. log.Fatal(err)
  854. }
  855. info, err := sf.Stat()
  856. if err != nil {
  857. log.Fatal(err)
  858. }
  859. fh, err := zip.FileInfoHeader(info)
  860. if err != nil {
  861. log.Fatal(err)
  862. }
  863. fh.Name = filepath.ToSlash(f.dst)
  864. fh.Method = zip.Deflate
  865. if strings.HasSuffix(f.dst, ".txt") {
  866. // Text file. Read it and convert line endings.
  867. bs, err := ioutil.ReadAll(sf)
  868. if err != nil {
  869. log.Fatal(err)
  870. }
  871. bs = bytes.Replace(bs, []byte{'\n'}, []byte{'\n', '\r'}, -1)
  872. fh.UncompressedSize = uint32(len(bs))
  873. fh.UncompressedSize64 = uint64(len(bs))
  874. of, err := zw.CreateHeader(fh)
  875. if err != nil {
  876. log.Fatal(err)
  877. }
  878. of.Write(bs)
  879. } else {
  880. // Binary file. Copy verbatim.
  881. of, err := zw.CreateHeader(fh)
  882. if err != nil {
  883. log.Fatal(err)
  884. }
  885. _, err = io.Copy(of, sf)
  886. if err != nil {
  887. log.Fatal(err)
  888. }
  889. }
  890. }
  891. err = zw.Close()
  892. if err != nil {
  893. log.Fatal(err)
  894. }
  895. err = fd.Close()
  896. if err != nil {
  897. log.Fatal(err)
  898. }
  899. }
  900. func macosCodesign(file string) {
  901. if pass := os.Getenv("CODESIGN_KEYCHAIN_PASS"); pass != "" {
  902. bs, err := runError("security", "unlock-keychain", "-p", pass)
  903. if err != nil {
  904. log.Println("Codesign: unlocking keychain failed:", string(bs))
  905. return
  906. }
  907. }
  908. if id := os.Getenv("CODESIGN_IDENTITY"); id != "" {
  909. bs, err := runError("codesign", "-s", id, file)
  910. if err != nil {
  911. log.Println("Codesign: signing failed:", string(bs))
  912. return
  913. }
  914. log.Println("Codesign: successfully signed", file)
  915. }
  916. }
  917. func metalint(linters []string, dirs []string) {
  918. ok := true
  919. if isGometalinterInstalled() {
  920. if !gometalinter(linters, dirs, lintExcludes...) {
  921. ok = false
  922. }
  923. }
  924. if !ok {
  925. log.Fatal("Build succeeded, but there were lint warnings")
  926. }
  927. }
  928. func isGometalinterInstalled() bool {
  929. if _, err := runError("gometalinter", "--disable-all"); err != nil {
  930. log.Println("gometalinter is not installed")
  931. return false
  932. }
  933. return true
  934. }
  935. func gometalinter(linters []string, dirs []string, excludes ...string) bool {
  936. params := []string{"--disable-all", "--concurrency=2", "--deadline=300s"}
  937. for _, linter := range linters {
  938. params = append(params, "--enable="+linter)
  939. }
  940. for _, exclude := range excludes {
  941. params = append(params, "--exclude="+exclude)
  942. }
  943. for _, dir := range dirs {
  944. params = append(params, dir)
  945. }
  946. bs, _ := runError("gometalinter", params...)
  947. nerr := 0
  948. lines := make(map[string]struct{})
  949. for _, line := range strings.Split(string(bs), "\n") {
  950. if line == "" {
  951. continue
  952. }
  953. if _, ok := lines[line]; ok {
  954. continue
  955. }
  956. log.Println(line)
  957. if strings.Contains(line, "executable file not found") {
  958. log.Println(` - Try "go run build.go setup" to install missing tools`)
  959. }
  960. lines[line] = struct{}{}
  961. nerr++
  962. }
  963. return nerr == 0
  964. }