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. "STNORESTART",
  37. "STNOUPGRADE",
  38. }
  39. )
  40. const versionExtraAllowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-. "
  41. func init() {
  42. if Version != "unknown-dev" {
  43. // If not a generic dev build, version string should come from git describe
  44. if !allowedVersionExp.MatchString(Version) {
  45. log.Fatalf("Invalid version string %q;\n\tdoes not match regexp %v", Version, allowedVersionExp)
  46. }
  47. }
  48. setBuildData()
  49. }
  50. func setBuildData() {
  51. // Check for a clean release build. A release is something like
  52. // "v0.1.2", with an optional suffix of letters and dot separated
  53. // numbers like "-beta3.47". If there's more stuff, like a plus sign and
  54. // a commit hash and so on, then it's not a release. If it has a dash in
  55. // it, it's some sort of beta, release candidate or special build. If it
  56. // has "-rc." in it, like "v0.14.35-rc.42", then it's a candidate build.
  57. //
  58. // So, every build that is not a stable release build has IsBeta = true.
  59. // This is used to enable some extra debugging (the deadlock detector).
  60. //
  61. // Release candidate builds are also "betas" from this point of view and
  62. // will have that debugging enabled. In addition, some features are
  63. // forced for release candidates - auto upgrade, and usage reporting.
  64. exp := regexp.MustCompile(`^v\d+\.\d+\.\d+(-[a-z]+[\d\.]+)?$`)
  65. IsRelease = exp.MatchString(Version)
  66. IsCandidate = strings.Contains(Version, "-rc.")
  67. IsBeta = strings.Contains(Version, "-")
  68. Extra = filterString(os.Getenv("STVERSIONEXTRA"), versionExtraAllowedChars)
  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. if tags := TagsList(); len(tags) > 0 {
  79. v = fmt.Sprintf("%s [%s]", v, strings.Join(tags, ", "))
  80. }
  81. return v
  82. }
  83. func TagsList() []string {
  84. tags := strings.Split(Tags, ",")
  85. if len(tags) == 1 && tags[0] == "" {
  86. tags = tags[:0]
  87. }
  88. for _, envVar := range envTags {
  89. if os.Getenv(envVar) != "" {
  90. tags = append(tags, strings.ToLower(envVar))
  91. }
  92. }
  93. if Extra != "" {
  94. tags = append(tags, Extra)
  95. }
  96. sort.Strings(tags)
  97. return tags
  98. }
  99. // filterString returns a copy of s with all characters not in allowedChars
  100. // removed.
  101. func filterString(s, allowedChars string) string {
  102. var res strings.Builder
  103. for _, c := range s {
  104. if strings.ContainsRune(allowedChars, c) {
  105. res.WriteRune(c)
  106. }
  107. }
  108. return res.String()
  109. }