mobile/cmd/gomobile/release.go

245 lines
5.1 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.
//+build ignore
// Release is a tool for building the NDK tarballs hosted on dl.google.com.
//
// The Go toolchain only needs the gcc compiler and headers, which are ~10MB.
// The entire NDK is ~400MB. Building smaller toolchain binaries reduces the
// run time of gomobile init significantly.
package main
import (
"archive/tar"
"bufio"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
)
const ndkVersion = "ndk-r10d"
type version struct {
os string
arch string
}
var hosts = []version{
// TODO: windows
{"darwin", "x86"},
{"darwin", "x86_64"},
{"linux", "x86"},
{"linux", "x86_64"},
}
var tmpdir string
func main() {
var err error
tmpdir, err = ioutil.TempDir("", "gomobile-release-")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tmpdir)
for _, host := range hosts {
if err := mkpkg(host); err != nil {
log.Fatal(err)
}
}
}
func mkpkg(host version) (err error) {
ndkName := "android-" + ndkVersion + "-" + host.os + "-" + host.arch + "."
if host.os == "windows" {
ndkName += "exe"
} else {
ndkName += "bin"
}
url := "http://dl.google.com/android/ndk/" + ndkName
log.Printf("%s\n", url)
binPath := tmpdir + "/" + ndkName
if err := fetch(binPath, url); err != nil {
log.Fatal(err)
}
src := tmpdir + "/" + host.os + "-" + host.arch + "-src"
dst := tmpdir + "/" + host.os + "-" + host.arch + "-dst"
if err := os.Mkdir(src, 0755); err != nil {
return err
}
if err := inflate(src, binPath); err != nil {
return err
}
// The NDK is unpacked into tmpdir/linux-x86_64-src/android-ndk-r10d.
// Move the files we want into tmpdir/linux-x86_64-dst/android-ndk-r10d.
// We preserve the same file layout to make the full NDK interchangable
// with the cut down file.
usr := "android-" + ndkVersion + "/platforms/android-15/arch-arm/usr"
gcc := "android-" + ndkVersion + "/toolchains/arm-linux-androideabi-4.8/prebuilt/" + host.os + "-" + host.arch
if err := os.MkdirAll(dst+"/"+usr, 0755); err != nil {
return err
}
if err := os.MkdirAll(dst+"/"+gcc, 0755); err != nil {
return err
}
if err := move(dst+"/"+usr, src+"/"+usr, "include", "lib"); err != nil {
return err
}
if err := move(dst+"/"+gcc, src+"/"+gcc, "bin", "lib", "libexec", "COPYING", "COPYING.LIB"); err != nil {
return err
}
// Build the tarball.
f, err := os.Create("gomobile-ndk-r10d-" + host.os + "-" + host.arch + ".tar.gz")
if err != nil {
return err
}
bw := bufio.NewWriter(f)
zw, err := gzip.NewWriterLevel(bw, gzip.BestCompression)
if err != nil {
return err
}
tw := tar.NewWriter(zw)
defer func() {
err2 := f.Close()
if err == nil {
err = err2
}
}()
defer func() {
err2 := bw.Flush()
if err == nil {
err = err2
}
}()
defer func() {
err2 := zw.Close()
if err == nil {
err = err2
}
}()
defer func() {
err2 := tw.Close()
if err == nil {
err = err2
}
}()
readme := "Stripped down copy of:\n\n\t" + url + "\n\nGenerated by golang.org/x/mobile/cmd/gomobile/release.go."
err = tw.WriteHeader(&tar.Header{
Name: "README",
Mode: 0644,
Size: int64(len(readme)),
})
if err != nil {
return err
}
_, err = tw.Write([]byte(readme))
if err != nil {
return err
}
return filepath.Walk(dst, func(path string, fi os.FileInfo, err error) error {
defer func() {
if err != nil {
err = fmt.Errorf("%s: %v", path, err)
}
}()
if err != nil {
return err
}
if path == dst {
return nil
}
name := path[len(dst)+1:]
if fi.IsDir() {
return nil
}
if fi.Mode()&os.ModeSymlink != 0 {
dst, err := os.Readlink(path)
if err != nil {
log.Printf("bad symlink: %s", name)
return nil
}
//log.Printf("linking %s to %s", name, dst)
return tw.WriteHeader(&tar.Header{
Name: name,
Linkname: dst,
Typeflag: tar.TypeSymlink,
})
}
//log.Printf("writing %s (%d)", name, fi.Size())
err = tw.WriteHeader(&tar.Header{
Name: name,
Mode: int64(fi.Mode()),
Size: fi.Size(),
})
if err != nil {
return err
}
f, err := os.Open(path)
if err != nil {
return err
}
_, err = io.Copy(tw, f)
f.Close()
return err
})
}
func fetch(dst, url string) error {
f, err := os.OpenFile(dst, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0755)
if err != nil {
return err
}
resp, err := http.Get(url)
if err != nil {
return err
}
_, err = io.Copy(f, resp.Body)
err2 := resp.Body.Close()
err3 := f.Close()
if err != nil {
return err
}
if err2 != nil {
return err2
}
return err3
}
func inflate(dst, path string) error {
p7zip := "7z"
if runtime.GOOS == "darwin" {
p7zip = "/Applications/Keka.app/Contents/Resources/keka7z"
}
cmd := exec.Command(p7zip, "x", path)
cmd.Dir = dst
out, err := cmd.CombinedOutput()
if err != nil {
os.Stderr.Write(out)
return err
}
return nil
}
func move(dst, src string, names ...string) error {
for _, name := range names {
if err := os.Rename(src+"/"+name, dst+"/"+name); err != nil {
return err
}
}
return nil
}