2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-02-10 17:00:30 +00:00
|
|
|
|
|
|
|
// Package version provides the version that the binary was built at.
|
|
|
|
package version
|
|
|
|
|
2021-10-22 17:44:37 +01:00
|
|
|
import (
|
2023-02-07 06:06:17 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2022-03-16 20:55:23 +00:00
|
|
|
"runtime/debug"
|
2023-02-07 06:06:17 +00:00
|
|
|
"strconv"
|
2021-10-22 17:44:37 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
tailscaleroot "tailscale.com"
|
|
|
|
)
|
|
|
|
|
2023-02-07 06:06:17 +00:00
|
|
|
var (
|
|
|
|
// Long is a full version number for this build, of the form
|
|
|
|
// "x.y.z-commithash" for builds stamped in the usual way (see build_dist.sh
|
|
|
|
// in the root) or, for binaries built by hand with the go tool, it's of the
|
|
|
|
// form "1.23.0-dev20220316-t29837428937{,-dirty}" where "1.23.0" comes from
|
|
|
|
// ../VERSION.txt and the part after dev is YYYYMMDD of the commit time, and
|
|
|
|
// the part after -t is the commit hash. The dirty suffix is whether there
|
|
|
|
// are uncommitted changes.
|
|
|
|
Long string
|
|
|
|
|
|
|
|
// Short is a short version number for this build, of the form
|
|
|
|
// "x.y.z" for builds stamped in the usual way (see
|
|
|
|
// build_dist.sh in the root) or, for binaries built by hand with the
|
|
|
|
// go tool, it's like Long's dev form, but ending at the date part,
|
|
|
|
// of the form "1.23.0-dev20220316".
|
|
|
|
Short string
|
|
|
|
|
|
|
|
// GitCommit, if non-empty, is the git commit of the
|
|
|
|
// github.com/tailscale/tailscale repository at which Tailscale was
|
|
|
|
// built. Its format is the one returned by `git describe --always
|
|
|
|
// --exclude "*" --dirty --abbrev=200`.
|
|
|
|
GitCommit string
|
|
|
|
|
|
|
|
// GitDirty is whether Go stamped the binary as having dirty version
|
|
|
|
// control changes in the working directory (debug.ReadBuildInfo
|
|
|
|
// setting "vcs.modified" was true).
|
|
|
|
GitDirty bool
|
|
|
|
|
|
|
|
// ExtraGitCommit, if non-empty, is the git commit of a "supplemental"
|
|
|
|
// repository at which Tailscale was built. Its format is the same as
|
|
|
|
// gitCommit.
|
|
|
|
//
|
|
|
|
// ExtraGitCommit is used to track the source revision when the main
|
|
|
|
// Tailscale repository is integrated into and built from another
|
|
|
|
// repository (for example, Tailscale's proprietary code, or the
|
|
|
|
// Android OSS repository). Together, GitCommit and ExtraGitCommit
|
|
|
|
// exactly describe what repositories and commits were used in a
|
|
|
|
// build.
|
|
|
|
ExtraGitCommit = ""
|
|
|
|
|
|
|
|
// isUnstable is whether the current build appears to be an unstable, i.e. with
|
|
|
|
// an odd minor version number.
|
|
|
|
isUnstable bool
|
|
|
|
|
|
|
|
// legacyOS is runtime.GOOS, except on apple devices where it's either "iOS" or
|
|
|
|
// "macOS" (with that exact case).
|
|
|
|
//
|
|
|
|
// This used to be a thing because Go reported both macOS and iOS as "darwin"
|
|
|
|
// and we needed to tell them apart. But then Go learned GOOS=ios and
|
|
|
|
// GOOS=darwin as separate things, but we're still stuck with this function
|
|
|
|
// because of the odd casing we picked, which has ossified into databases.
|
|
|
|
legacyOS string
|
|
|
|
|
|
|
|
// isMobile is whether the current build is for a mobile device.
|
|
|
|
isMobile bool
|
|
|
|
|
|
|
|
// isSandboxedMacOS is whether the current binary is any binary in the mac store
|
|
|
|
// or standalone sysext mac apps.
|
|
|
|
isSandboxedMacOS bool
|
|
|
|
|
|
|
|
// isMacSysExt is whether the current binary is the mac system extension binary.
|
|
|
|
isMacSysExt bool
|
|
|
|
|
|
|
|
// isWindowsGUI is whether the current binary is the Windows GUI binary.
|
|
|
|
isWindowsGUI bool
|
2023-02-07 07:23:00 +00:00
|
|
|
|
|
|
|
// majorMinorPatch is the major.minor.patch portion of Short.
|
|
|
|
majorMinorPatch string
|
2023-02-07 06:06:17 +00:00
|
|
|
)
|
2020-10-27 04:23:58 +00:00
|
|
|
|
2021-06-03 19:19:29 +01:00
|
|
|
func init() {
|
2023-02-07 06:06:17 +00:00
|
|
|
initVersion()
|
|
|
|
initUnstable()
|
|
|
|
initMiscTraits()
|
|
|
|
}
|
|
|
|
|
|
|
|
func initVersion() {
|
2023-02-07 07:23:00 +00:00
|
|
|
defer func() {
|
|
|
|
// Must be run after Short has been initialized, easiest way to do that
|
|
|
|
// is a defer.
|
|
|
|
majorMinorPatch, _, _ = strings.Cut(Short, "-")
|
|
|
|
}()
|
|
|
|
|
2022-03-16 20:55:23 +00:00
|
|
|
if Long != "" && Short != "" {
|
|
|
|
// Built in the recommended way, using build_dist.sh.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-07 06:06:17 +00:00
|
|
|
// Otherwise, make approximate version info using Go 1.18's built-in git
|
|
|
|
// stamping.
|
2022-03-16 20:55:23 +00:00
|
|
|
bi, ok := debug.ReadBuildInfo()
|
|
|
|
if !ok {
|
2023-02-07 07:23:00 +00:00
|
|
|
Long = strings.TrimSpace(tailscaleroot.VersionDotTxt) + "-ERR-BuildInfo"
|
2021-06-03 19:19:29 +01:00
|
|
|
Short = Long
|
2022-03-16 20:55:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var dirty string // "-dirty" suffix if dirty
|
2022-12-24 20:42:53 +00:00
|
|
|
var commitDate string
|
2022-03-16 20:55:23 +00:00
|
|
|
for _, s := range bi.Settings {
|
|
|
|
switch s.Key {
|
|
|
|
case "vcs.revision":
|
|
|
|
GitCommit = s.Value
|
|
|
|
case "vcs.time":
|
|
|
|
if len(s.Value) >= len("yyyy-mm-dd") {
|
|
|
|
commitDate = s.Value[:len("yyyy-mm-dd")]
|
|
|
|
commitDate = strings.ReplaceAll(commitDate, "-", "")
|
|
|
|
}
|
|
|
|
case "vcs.modified":
|
|
|
|
if s.Value == "true" {
|
|
|
|
dirty = "-dirty"
|
|
|
|
GitDirty = true
|
|
|
|
}
|
|
|
|
}
|
2021-06-03 19:19:29 +01:00
|
|
|
}
|
2022-12-24 20:42:53 +00:00
|
|
|
commitHashAbbrev := GitCommit
|
|
|
|
if len(commitHashAbbrev) >= 9 {
|
|
|
|
commitHashAbbrev = commitHashAbbrev[:9]
|
|
|
|
}
|
2022-03-16 20:55:23 +00:00
|
|
|
|
|
|
|
// Backup path, using Go 1.18's built-in git stamping.
|
2023-02-07 07:23:00 +00:00
|
|
|
Short = strings.TrimSpace(tailscaleroot.VersionDotTxt) + "-dev" + commitDate
|
2022-03-16 20:55:23 +00:00
|
|
|
Long = Short + "-t" + commitHashAbbrev + dirty
|
2021-06-03 19:19:29 +01:00
|
|
|
}
|
2020-10-27 04:23:58 +00:00
|
|
|
|
2023-02-07 06:06:17 +00:00
|
|
|
func initUnstable() {
|
|
|
|
_, rest, ok := strings.Cut(Short, ".")
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
minorStr, _, ok := strings.Cut(rest, ".")
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
minor, err := strconv.Atoi(minorStr)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
isUnstable = minor%2 == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func initMiscTraits() {
|
|
|
|
exe, _ := os.Executable()
|
|
|
|
base := filepath.Base(exe)
|
|
|
|
|
|
|
|
legacyOS = runtime.GOOS
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "darwin":
|
|
|
|
legacyOS = "macOS"
|
|
|
|
isMacSysExt = strings.HasPrefix(base, "io.tailscale.ipn.macsys.network-extension")
|
|
|
|
isSandboxedMacOS = isMacSysExt || strings.HasSuffix(exe, "/Contents/MacOS/Tailscale") || strings.HasSuffix(exe, "/Contents/MacOS/IPNExtension")
|
|
|
|
case "ios":
|
|
|
|
legacyOS = "iOS"
|
|
|
|
isMobile = true
|
|
|
|
case "android":
|
|
|
|
isMobile = true
|
|
|
|
case "windows":
|
|
|
|
isWindowsGUI = strings.EqualFold(base, "tailscale-ipn.exe") || strings.EqualFold(base, "tailscale-ipn")
|
|
|
|
}
|
|
|
|
}
|