File: version.go

package info (click to toggle)
golang-golang-x-mobile 0.0~git20250520.a1d9079%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,784 kB
  • sloc: objc: 1,512; java: 1,489; ansic: 1,159; xml: 365; asm: 34; sh: 14; makefile: 5
file content (82 lines) | stat: -rw-r--r-- 2,343 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"bytes"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"golang.org/x/mobile/internal/sdkpath"
)

var cmdVersion = &command{
	run:   runVersion,
	Name:  "version",
	Usage: "",
	Short: "print version",
	Long: `
Version prints versions of the gomobile binary and tools
`,
}

func runVersion(cmd *command) (err error) {
	// Check this binary matches the version in golang.org/x/mobile/cmd/gomobile
	// source code in GOPATH. If they don't match, currently there is no
	// way to reliably identify the revision number this binary was built
	// against.
	version, err := func() (string, error) {
		bin, err := exec.LookPath(os.Args[0])
		if err != nil {
			return "", err
		}
		bindir := filepath.Dir(bin)
		cmd := exec.Command("go", "list", "-f", "{{.Stale}}", "golang.org/x/mobile/cmd/gomobile")
		cmd.Env = append(os.Environ(), "GOBIN="+bindir)
		out, err := cmd.CombinedOutput()
		if err != nil {
			return "", fmt.Errorf("cannot test gomobile binary: %v, %s", err, out)
		}
		if strings.TrimSpace(string(out)) != "false" {
			return "", fmt.Errorf("binary is out of date, re-install it")
		}
		return mobileRepoRevision()
	}()
	if err != nil {
		fmt.Printf("gomobile version unknown: %v\n", err)
		return nil
	}

	// Supported platforms
	platforms := "android"
	if xcodeAvailable() {
		platforms += "," + strings.Join(applePlatforms, ",")
	}

	androidapi, _ := sdkpath.AndroidAPIPath(buildAndroidAPI)

	fmt.Printf("gomobile version %s (%s); androidSDK=%s\n", version, platforms, androidapi)
	return nil
}

func mobileRepoRevision() (rev string, err error) {
	b, err := exec.Command("go", "list", "-f", "{{.Dir}}", "golang.org/x/mobile/app").CombinedOutput()
	if err != nil {
		return "", fmt.Errorf("mobile repo not found: %v, %s", err, b)
	}

	repo := filepath.Dir(string(b))
	if err := os.Chdir(repo); err != nil {
		return "", fmt.Errorf("mobile repo %q not accessible: %v", repo, err)
	}
	revision, err := exec.Command("git", "log", "-n", "1", "--format=format: +%h %cd", "HEAD").CombinedOutput()
	if err != nil {
		return "", fmt.Errorf("mobile repo git log failed: %v, %s", err, revision)
	}
	return string(bytes.Trim(revision, " \t\r\n")), nil
}