build.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. const Codename = "Gold Grasshopper"
  19. var (
  20. // Injected by build script
  21. Version = "unknown-dev"
  22. Host = "unknown"
  23. User = "unknown"
  24. Stamp = "0"
  25. Tags = ""
  26. // Set by init()
  27. Date time.Time
  28. IsRelease bool
  29. IsCandidate bool
  30. IsBeta bool
  31. LongVersion string
  32. Extra string
  33. allowedVersionExp = regexp.MustCompile(`^v\d+\.\d+\.\d+(-[a-z0-9]+)*(\.\d+)*(\+\d+-g[0-9a-f]+|\+[0-9a-z]+)?(-[^\s]+)?$`)
  34. envTags = []string{
  35. "STGUIASSETS",
  36. "STHASHING",
  37. "STNORESTART",
  38. "STNOUPGRADE",
  39. }
  40. )
  41. const versionExtraAllowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-. "
  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. Extra = filterString(os.Getenv("STVERSIONEXTRA"), versionExtraAllowedChars)
  70. stamp, _ := strconv.Atoi(Stamp)
  71. Date = time.Unix(int64(stamp), 0)
  72. LongVersion = LongVersionFor("syncthing")
  73. }
  74. // LongVersionFor returns the long version string for the given program name.
  75. func LongVersionFor(program string) string {
  76. // This string and date format is essentially part of our external API. Never change it.
  77. date := Date.UTC().Format("2006-01-02 15:04:05 MST")
  78. v := fmt.Sprintf(`%s %s "%s" (%s %s-%s) %s@%s %s`, program, Version, Codename, runtime.Version(), runtime.GOOS, runtime.GOARCH, User, Host, date)
  79. if tags := TagsList(); len(tags) > 0 {
  80. v = fmt.Sprintf("%s [%s]", v, strings.Join(tags, ", "))
  81. }
  82. return v
  83. }
  84. func TagsList() []string {
  85. tags := strings.Split(Tags, ",")
  86. if len(tags) == 1 && tags[0] == "" {
  87. tags = tags[:0]
  88. }
  89. for _, envVar := range envTags {
  90. if os.Getenv(envVar) != "" {
  91. tags = append(tags, strings.ToLower(envVar))
  92. }
  93. }
  94. if Extra != "" {
  95. tags = append(tags, Extra)
  96. }
  97. sort.Strings(tags)
  98. return tags
  99. }
  100. // filterString returns a copy of s with all characters not in allowedChars
  101. // removed.
  102. func filterString(s, allowedChars string) string {
  103. var res strings.Builder
  104. for _, c := range s {
  105. if strings.ContainsRune(allowedChars, c) {
  106. res.WriteRune(c)
  107. }
  108. }
  109. return res.String()
  110. }