build.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (C) 2019 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. package build
  7. import (
  8. "fmt"
  9. "log"
  10. "regexp"
  11. "runtime"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. var (
  17. // Injected by build script
  18. Version = "unknown-dev"
  19. Host = "unknown" // Set by build script
  20. User = "unknown" // Set by build script
  21. Stamp = "0" // Set by build script
  22. // Static
  23. Codename = "Erbium Earthworm"
  24. // Set by init()
  25. Date time.Time
  26. IsRelease bool
  27. IsCandidate bool
  28. IsBeta bool
  29. LongVersion string
  30. // Set by Go build tags
  31. Tags []string
  32. allowedVersionExp = regexp.MustCompile(`^v\d+\.\d+\.\d+(-[a-z0-9]+)*(\.\d+)*(\+\d+-g[0-9a-f]+)?(-[^\s]+)?$`)
  33. )
  34. func init() {
  35. if Version != "unknown-dev" {
  36. // If not a generic dev build, version string should come from git describe
  37. if !allowedVersionExp.MatchString(Version) {
  38. log.Fatalf("Invalid version string %q;\n\tdoes not match regexp %v", Version, allowedVersionExp)
  39. }
  40. }
  41. setBuildData()
  42. }
  43. func setBuildData() {
  44. // Check for a clean release build. A release is something like
  45. // "v0.1.2", with an optional suffix of letters and dot separated
  46. // numbers like "-beta3.47". If there's more stuff, like a plus sign and
  47. // a commit hash and so on, then it's not a release. If it has a dash in
  48. // it, it's some sort of beta, release candidate or special build. If it
  49. // has "-rc." in it, like "v0.14.35-rc.42", then it's a candidate build.
  50. //
  51. // So, every build that is not a stable release build has IsBeta = true.
  52. // This is used to enable some extra debugging (the deadlock detector).
  53. //
  54. // Release candidate builds are also "betas" from this point of view and
  55. // will have that debugging enabled. In addition, some features are
  56. // forced for release candidates - auto upgrade, and usage reporting.
  57. exp := regexp.MustCompile(`^v\d+\.\d+\.\d+(-[a-z]+[\d\.]+)?$`)
  58. IsRelease = exp.MatchString(Version)
  59. IsCandidate = strings.Contains(Version, "-rc.")
  60. IsBeta = strings.Contains(Version, "-")
  61. stamp, _ := strconv.Atoi(Stamp)
  62. Date = time.Unix(int64(stamp), 0)
  63. date := Date.UTC().Format("2006-01-02 15:04:05 MST")
  64. LongVersion = fmt.Sprintf(`syncthing %s "%s" (%s %s-%s) %s@%s %s`, Version, Codename, runtime.Version(), runtime.GOOS, runtime.GOARCH, User, Host, date)
  65. if len(Tags) > 0 {
  66. LongVersion = fmt.Sprintf("%s [%s]", LongVersion, strings.Join(Tags, ", "))
  67. }
  68. }