build.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. "os"
  11. "regexp"
  12. "runtime"
  13. "sort"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. var (
  19. // Injected by build script
  20. Version = "unknown-dev"
  21. Host = "unknown"
  22. User = "unknown"
  23. Stamp = "0"
  24. Tags = ""
  25. // Static
  26. Codename = "Fermium Flea"
  27. // Set by init()
  28. Date time.Time
  29. IsRelease bool
  30. IsCandidate bool
  31. IsBeta bool
  32. LongVersion string
  33. allowedVersionExp = regexp.MustCompile(`^v\d+\.\d+\.\d+(-[a-z0-9]+)*(\.\d+)*(\+\d+-g[0-9a-f]+)?(-[^\s]+)?$`)
  34. envTags = []string{
  35. "STGUIASSETS",
  36. "STHASHING",
  37. "STNORESTART",
  38. "STNOUPGRADE",
  39. "USE_BADGER",
  40. }
  41. )
  42. func init() {
  43. if Version != "unknown-dev" {
  44. // If not a generic dev build, version string should come from git describe
  45. if !allowedVersionExp.MatchString(Version) {
  46. log.Fatalf("Invalid version string %q;\n\tdoes not match regexp %v", Version, allowedVersionExp)
  47. }
  48. }
  49. setBuildData()
  50. }
  51. func setBuildData() {
  52. // Check for a clean release build. A release is something like
  53. // "v0.1.2", with an optional suffix of letters and dot separated
  54. // numbers like "-beta3.47". If there's more stuff, like a plus sign and
  55. // a commit hash and so on, then it's not a release. If it has a dash in
  56. // it, it's some sort of beta, release candidate or special build. If it
  57. // has "-rc." in it, like "v0.14.35-rc.42", then it's a candidate build.
  58. //
  59. // So, every build that is not a stable release build has IsBeta = true.
  60. // This is used to enable some extra debugging (the deadlock detector).
  61. //
  62. // Release candidate builds are also "betas" from this point of view and
  63. // will have that debugging enabled. In addition, some features are
  64. // forced for release candidates - auto upgrade, and usage reporting.
  65. exp := regexp.MustCompile(`^v\d+\.\d+\.\d+(-[a-z]+[\d\.]+)?$`)
  66. IsRelease = exp.MatchString(Version)
  67. IsCandidate = strings.Contains(Version, "-rc.")
  68. IsBeta = strings.Contains(Version, "-")
  69. stamp, _ := strconv.Atoi(Stamp)
  70. Date = time.Unix(int64(stamp), 0)
  71. LongVersion = LongVersionFor("syncthing")
  72. }
  73. // LongVersionFor returns the long version string for the given program name.
  74. func LongVersionFor(program string) string {
  75. // This string and date format is essentially part of our external API. Never change it.
  76. date := Date.UTC().Format("2006-01-02 15:04:05 MST")
  77. v := fmt.Sprintf(`%s %s "%s" (%s %s-%s) %s@%s %s`, program, Version, Codename, runtime.Version(), runtime.GOOS, runtime.GOARCH, User, Host, date)
  78. tags := strings.Split(Tags, ",")
  79. if len(tags) == 1 && tags[0] == "" {
  80. tags = tags[:0]
  81. }
  82. for _, envVar := range envTags {
  83. if os.Getenv(envVar) != "" {
  84. tags = append(tags, strings.ToLower(envVar))
  85. }
  86. }
  87. if len(tags) > 0 {
  88. sort.Strings(tags)
  89. v = fmt.Sprintf("%s [%s]", v, strings.Join(tags, ", "))
  90. }
  91. return v
  92. }