build.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 = "Hafnium Hornet"
  19. var (
  20. // Injected by build script
  21. Version = "unknown-dev"
  22. Host = "unknown"
  23. User = "unknown"
  24. Stamp = "0"
  25. Tags = ""
  26. // Added to by other packages
  27. extraTags []string
  28. // Set by init()
  29. Date time.Time
  30. IsRelease bool
  31. IsCandidate bool
  32. IsBeta bool
  33. LongVersion string
  34. Extra string
  35. allowedVersionExp = regexp.MustCompile(`^v\d+\.\d+\.\d+(-[a-z0-9]+)*(\.\d+)*(\+\d+-g[0-9a-f]+|\+[0-9a-z]+)?(-[^\s]+)?$`)
  36. envTags = []string{
  37. "STGUIASSETS",
  38. "STNORESTART",
  39. "STNOUPGRADE",
  40. }
  41. replaceTags = map[string]string{
  42. "sqlite_omit_load_extension": "",
  43. "osusergo": "",
  44. "netgo": "",
  45. }
  46. )
  47. const versionExtraAllowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-. "
  48. func init() {
  49. if Version != "unknown-dev" {
  50. // If not a generic dev build, version string should come from git describe
  51. if !allowedVersionExp.MatchString(Version) {
  52. log.Fatalf("Invalid version string %q;\n\tdoes not match regexp %v", Version, allowedVersionExp)
  53. }
  54. }
  55. setBuildData()
  56. }
  57. func setBuildData() {
  58. // Check for a clean release build. A release is something like
  59. // "v0.1.2", with an optional suffix of letters and dot separated
  60. // numbers like "-beta3.47". If there's more stuff, like a plus sign and
  61. // a commit hash and so on, then it's not a release. If it has a dash in
  62. // it, it's some sort of beta, release candidate or special build. If it
  63. // has "-rc." in it, like "v0.14.35-rc.42", then it's a candidate build.
  64. //
  65. // So, every build that is not a stable release build has IsBeta = true.
  66. // This is used to enable some extra debugging (the deadlock detector).
  67. //
  68. // Release candidate builds are also "betas" from this point of view and
  69. // will have that debugging enabled. In addition, some features are
  70. // forced for release candidates - auto upgrade, and usage reporting.
  71. exp := regexp.MustCompile(`^v\d+\.\d+\.\d+(-[a-z]+[\d\.]+)?$`)
  72. IsRelease = exp.MatchString(Version)
  73. IsCandidate = strings.Contains(Version, "-rc.")
  74. IsBeta = strings.Contains(Version, "-")
  75. Extra = filterString(os.Getenv("STVERSIONEXTRA"), versionExtraAllowedChars)
  76. stamp, _ := strconv.Atoi(Stamp)
  77. Date = time.Unix(int64(stamp), 0)
  78. LongVersion = LongVersionFor("syncthing")
  79. }
  80. // LongVersionFor returns the long version string for the given program name.
  81. func LongVersionFor(program string) string {
  82. // This string and date format is essentially part of our external API. Never change it.
  83. date := Date.UTC().Format("2006-01-02 15:04:05 MST")
  84. v := fmt.Sprintf(`%s %s "%s" (%s %s-%s) %s@%s %s`, program, Version, Codename, runtime.Version(), runtime.GOOS, runtime.GOARCH, User, Host, date)
  85. if tags := TagsList(); len(tags) > 0 {
  86. v = fmt.Sprintf("%s [%s]", v, strings.Join(tags, ", "))
  87. }
  88. return v
  89. }
  90. func TagsList() []string {
  91. tags := strings.Split(Tags, ",")
  92. if len(tags) == 1 && tags[0] == "" {
  93. tags = tags[:0]
  94. }
  95. for _, envVar := range envTags {
  96. if os.Getenv(envVar) != "" {
  97. tags = append(tags, strings.ToLower(envVar))
  98. }
  99. }
  100. if Extra != "" {
  101. tags = append(tags, Extra)
  102. }
  103. tags = append(tags, extraTags...)
  104. // Replace any tag values we want to have more user friendly versions,
  105. // or be removed
  106. for i, tag := range tags {
  107. if repl, ok := replaceTags[tag]; ok {
  108. tags[i] = repl
  109. }
  110. }
  111. sort.Strings(tags)
  112. // Remove any empty tags, which will be at the front of the list now
  113. for len(tags) > 0 && tags[0] == "" {
  114. tags = tags[1:]
  115. }
  116. return tags
  117. }
  118. // filterString returns a copy of s with all characters not in allowedChars
  119. // removed.
  120. func filterString(s, allowedChars string) string {
  121. var res strings.Builder
  122. for _, c := range s {
  123. if strings.ContainsRune(allowedChars, c) {
  124. res.WriteRune(c)
  125. }
  126. }
  127. return res.String()
  128. }
  129. func AddTag(tag string) {
  130. extraTags = append(extraTags, tag)
  131. LongVersion = LongVersionFor("syncthing")
  132. }