2
0
mirror of synced 2025-02-23 14:58:12 +00:00
mobile/cmd/gomobile/main.go
David Crawshaw 743b9bfd9a cmd/gomobile: simplify init
The Go toolchain used to require that we rebuild the compiler and
friends (the cgo command) when building a cgo-enabled cross compiler.
This meant that gomobile init used to invoke make.bash in a temporary
copy of the GOROOT. This works, but brings with it several
complications, including needing to use -toolexec when invoking the
go tool, finding a boostrap copy of Go 1.4, long initialization
times, and a variety of unusual failure modes.

Fortunately we don't need our own compiler any more. All that's
necessary is building the standard library for the cross-compilation
targets and making sure the right C compiler is used when calling
go build (as it always was). This means most of the initialization
process can be replaced with a carefully invoked 'go install std'.

While here, remove the source install instructions (most of it is
documented already, and the final step, choosing the right git
revision should be within the skills of anyone using pre-release
software.) Some other documentation is changing because it's been a
while since go generate was run.

Change-Id: I88c10fef87867536e83c7df063ae7241b2e9eea4
Reviewed-on: https://go-review.googlesource.com/11711
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-06-29 20:40:46 +00:00

177 lines
3.6 KiB
Go

// 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
//go:generate gomobile help documentation doc.go
import (
"bufio"
"bytes"
"flag"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"os"
"unicode"
"unicode/utf8"
)
func printUsage(w io.Writer) {
bufw := bufio.NewWriter(w)
if err := usageTmpl.Execute(bufw, commands); err != nil {
panic(err)
}
bufw.Flush()
}
var gomobileName = "gomobile"
func main() {
gomobileName = os.Args[0]
flag.Usage = func() {
printUsage(os.Stderr)
os.Exit(2)
}
flag.Parse()
log.SetFlags(0)
args := flag.Args()
if len(args) < 1 {
flag.Usage()
}
if args[0] == "help" {
if len(args) == 3 && args[1] == "documentation" {
helpDocumentation(args[2])
return
}
help(args[1:])
return
}
for _, cmd := range commands {
if cmd.Name == args[0] {
cmd.flag.Usage = func() {
cmd.usage()
os.Exit(1)
}
cmd.flag.Parse(args[1:])
if err := cmd.run(cmd); err != nil {
msg := err.Error()
if msg != "" {
fmt.Fprintf(os.Stderr, "%s: %v\n", os.Args[0], err)
}
os.Exit(1)
}
return
}
}
fmt.Fprintf(os.Stderr, "%s: unknown subcommand %q\nRun 'gomobile help' for usage.\n", os.Args[0], args[0])
os.Exit(2)
}
func help(args []string) {
if len(args) == 0 {
printUsage(os.Stdout)
return // succeeded at helping
}
if len(args) != 1 {
fmt.Fprintf(os.Stderr, "usage: %s help command\n\nToo many arguments given.\n", gomobileName)
os.Exit(2) // failed to help
}
arg := args[0]
for _, cmd := range commands {
if cmd.Name == arg {
cmd.usage()
return // succeeded at helping
}
}
fmt.Fprintf(os.Stderr, "Unknown help topic %#q. Run '%s help'.\n", arg, gomobileName)
os.Exit(2)
}
const documentationHeader = `// 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.
// DO NOT EDIT. GENERATED BY 'gomobile help documentation doc.go'.
`
func helpDocumentation(path string) {
w := new(bytes.Buffer)
w.WriteString(documentationHeader)
w.WriteString("\n/*\n")
if err := usageTmpl.Execute(w, commands); err != nil {
log.Fatal(err)
}
for _, cmd := range commands {
r, rlen := utf8.DecodeRuneInString(cmd.Short)
w.WriteString("\n\n")
w.WriteRune(unicode.ToUpper(r))
w.WriteString(cmd.Short[rlen:])
w.WriteString("\n\nUsage:\n\n\tgomobile " + cmd.Name + " " + cmd.Usage + "\n")
w.WriteString(cmd.Long)
}
w.WriteString("*/\npackage main\n")
if err := ioutil.WriteFile(path, w.Bytes(), 0666); err != nil {
log.Fatal(err)
}
}
var commands = []*command{
// TODO(crawshaw): cmdRun
cmdBind,
cmdBuild,
cmdInit,
cmdInstall,
}
type command struct {
run func(*command) error
flag flag.FlagSet
Name string
Usage string
Short string
Long string
}
func (cmd *command) usage() {
fmt.Fprintf(os.Stdout, "usage: %s %s %s\n%s", gomobileName, cmd.Name, cmd.Usage, cmd.Long)
}
var usageTmpl = template.Must(template.New("usage").Parse(
`Gomobile is a tool for building and running mobile apps written in Go.
To install:
$ go get golang.org/x/mobile/cmd/gomobile
$ gomobile init
At least Go 1.5 is required. Until it is released, build tip from
source: http://golang.org/doc/install/source
Initialization rebuilds the standard library and may download
the Android NDK compiler.
Usage:
gomobile command [arguments]
Commands:
{{range .}}
{{.Name | printf "%-11s"}} {{.Short}}{{end}}
Use 'gomobile help [command]' for more information about that command.
NOTE: iOS support is not ready yet.
`))