| 1 |
// Copyright 2015 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"os" |
| 13 |
"os/exec" |
| 14 |
"path/filepath" |
| 15 |
"runtime" |
| 16 |
"strings" |
| 17 |
"time" |
| 18 |
|
| 19 |
"golang.org/x/mobile/internal/sdkpath" |
| 20 |
) |
| 21 |
|
| 22 |
var ( |
| 23 |
goos = runtime.GOOS |
| 24 |
goarch = runtime.GOARCH |
| 25 |
) |
| 26 |
|
| 27 |
var cmdInit = &command{ |
| 28 |
run: runInit, |
| 29 |
Name: "init", |
| 30 |
Usage: "[-openal dir]", |
| 31 |
Short: "build OpenAL for Android", |
| 32 |
Long: ` |
| 33 |
If a OpenAL source directory is specified with -openal, init will |
| 34 |
build an Android version of OpenAL for use with gomobile build |
| 35 |
and gomobile install. |
| 36 |
`, |
| 37 |
} |
| 38 |
|
| 39 |
var initOpenAL string // -openal |
| 40 |
|
| 41 |
func init() { |
| 42 |
cmdInit.flag.StringVar(&initOpenAL, "openal", "", "OpenAL source path") |
| 43 |
} |
| 44 |
|
| 45 |
func runInit(cmd *command) error { |
| 46 |
gopaths := filepath.SplitList(goEnv("GOPATH")) |
| 47 |
if len(gopaths) == 0 { |
| 48 |
return fmt.Errorf("GOPATH is not set") |
| 49 |
} |
| 50 |
gomobilepath = filepath.Join(gopaths[0], "pkg/gomobile") |
| 51 |
|
| 52 |
if buildX || buildN { |
| 53 |
fmt.Fprintln(xout, "GOMOBILE="+gomobilepath) |
| 54 |
} |
| 55 |
removeAll(gomobilepath) |
| 56 |
|
| 57 |
if err := mkdir(gomobilepath); err != nil { |
| 58 |
return err |
| 59 |
} |
| 60 |
|
| 61 |
if buildN { |
| 62 |
tmpdir = filepath.Join(gomobilepath, "work") |
| 63 |
} else { |
| 64 |
var err error |
| 65 |
tmpdir, err = os.MkdirTemp(gomobilepath, "work-") |
| 66 |
if err != nil { |
| 67 |
return err |
| 68 |
} |
| 69 |
} |
| 70 |
if buildX || buildN { |
| 71 |
fmt.Fprintln(xout, "WORK="+tmpdir) |
| 72 |
} |
| 73 |
defer func() { |
| 74 |
if buildWork { |
| 75 |
fmt.Printf("WORK=%s\n", tmpdir) |
| 76 |
return |
| 77 |
} |
| 78 |
removeAll(tmpdir) |
| 79 |
}() |
| 80 |
|
| 81 |
// Make sure gobind is up to date. |
| 82 |
if err := goInstall([]string{"golang.org/x/mobile/cmd/gobind@latest"}, nil); err != nil { |
| 83 |
return err |
| 84 |
} |
| 85 |
|
| 86 |
if buildN { |
| 87 |
initOpenAL = "$OPENAL_PATH" |
| 88 |
} else { |
| 89 |
if initOpenAL != "" { |
| 90 |
var err error |
| 91 |
if initOpenAL, err = filepath.Abs(initOpenAL); err != nil { |
| 92 |
return err |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
if err := envInit(); err != nil { |
| 97 |
return err |
| 98 |
} |
| 99 |
|
| 100 |
start := time.Now() |
| 101 |
|
| 102 |
if err := installOpenAL(gomobilepath); err != nil { |
| 103 |
return err |
| 104 |
} |
| 105 |
|
| 106 |
if buildV { |
| 107 |
took := time.Since(start) / time.Second * time.Second |
| 108 |
fmt.Fprintf(os.Stderr, "\nDone, build took %s.\n", took) |
| 109 |
} |
| 110 |
return nil |
| 111 |
} |
| 112 |
|
| 113 |
func installOpenAL(gomobilepath string) error { |
| 114 |
if initOpenAL == "" { |
| 115 |
return nil |
| 116 |
} |
| 117 |
ndkRoot, err := ndkRoot() |
| 118 |
if err != nil { |
| 119 |
return err |
| 120 |
} |
| 121 |
|
| 122 |
var cmake string |
| 123 |
if buildN { |
| 124 |
cmake = "cmake" |
| 125 |
} else { |
| 126 |
sdkRoot, err := sdkpath.AndroidHome() |
| 127 |
if err != nil { |
| 128 |
return nil |
| 129 |
} |
| 130 |
cmake, err = exec.LookPath("cmake") |
| 131 |
if err != nil { |
| 132 |
cmakePath := filepath.Join(sdkRoot, "cmake") |
| 133 |
cmakeDir, err := os.Open(cmakePath) |
| 134 |
if err != nil { |
| 135 |
if os.IsNotExist(err) { |
| 136 |
// Skip OpenAL install if the cmake package is not installed. |
| 137 |
return errors.New("cmake was not found in the PATH. Please install it through the Android SDK manager.") |
| 138 |
} |
| 139 |
return err |
| 140 |
} |
| 141 |
defer cmakeDir.Close() |
| 142 |
// There might be multiple versions of CMake installed. Use any one for now. |
| 143 |
cmakeVers, err := cmakeDir.Readdirnames(1) |
| 144 |
if err != nil || len(cmakeVers) == 0 { |
| 145 |
return errors.New("cmake was not found in the PATH. Please install it through the Android SDK manager.") |
| 146 |
} |
| 147 |
cmake = filepath.Join(cmakePath, cmakeVers[0], "bin", "cmake") |
| 148 |
} |
| 149 |
} |
| 150 |
var alTmpDir string |
| 151 |
if buildN { |
| 152 |
alTmpDir = filepath.Join(gomobilepath, "work") |
| 153 |
} else { |
| 154 |
var err error |
| 155 |
alTmpDir, err = os.MkdirTemp(gomobilepath, "openal-release-") |
| 156 |
if err != nil { |
| 157 |
return err |
| 158 |
} |
| 159 |
defer removeAll(alTmpDir) |
| 160 |
} |
| 161 |
|
| 162 |
for _, f := range []string{"include/AL/al.h", "include/AL/alc.h"} { |
| 163 |
dst := filepath.Join(gomobilepath, f) |
| 164 |
src := filepath.Join(initOpenAL, f) |
| 165 |
if err := copyFile(dst, src); err != nil { |
| 166 |
return err |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
for _, arch := range platformArchs("android") { |
| 171 |
t := ndk[arch] |
| 172 |
abi := t.arch |
| 173 |
if abi == "arm" { |
| 174 |
abi = "armeabi" |
| 175 |
} |
| 176 |
make := filepath.Join(ndkRoot, "prebuilt", archNDK(), "bin", "make") |
| 177 |
// Split android-XX to get the api version. |
| 178 |
buildDir := alTmpDir + "/build/" + abi |
| 179 |
if err := mkdir(buildDir); err != nil { |
| 180 |
return err |
| 181 |
} |
| 182 |
cmd := exec.Command(cmake, |
| 183 |
initOpenAL, |
| 184 |
"-DCMAKE_TOOLCHAIN_FILE="+initOpenAL+"/XCompile-Android.txt", |
| 185 |
"-DHOST="+t.ClangPrefix()) |
| 186 |
cmd.Dir = buildDir |
| 187 |
tcPath := filepath.Join(ndkRoot, "toolchains", "llvm", "prebuilt", archNDK(), "bin") |
| 188 |
if !buildN { |
| 189 |
orgPath := os.Getenv("PATH") |
| 190 |
cmd.Env = []string{"PATH=" + tcPath + string(os.PathListSeparator) + orgPath} |
| 191 |
} |
| 192 |
if err := runCmd(cmd); err != nil { |
| 193 |
return err |
| 194 |
} |
| 195 |
|
| 196 |
cmd = exec.Command(make) |
| 197 |
cmd.Dir = buildDir |
| 198 |
if err := runCmd(cmd); err != nil { |
| 199 |
return err |
| 200 |
} |
| 201 |
|
| 202 |
dst := filepath.Join(gomobilepath, "lib", t.abi, "libopenal.so") |
| 203 |
src := filepath.Join(alTmpDir, "build", abi, "libopenal.so") |
| 204 |
if err := copyFile(dst, src); err != nil { |
| 205 |
return err |
| 206 |
} |
| 207 |
} |
| 208 |
return nil |
| 209 |
} |
| 210 |
|
| 211 |
func mkdir(dir string) error { |
| 212 |
if buildX || buildN { |
| 213 |
printcmd("mkdir -p %s", dir) |
| 214 |
} |
| 215 |
if buildN { |
| 216 |
return nil |
| 217 |
} |
| 218 |
return os.MkdirAll(dir, 0755) |
| 219 |
} |
| 220 |
|
| 221 |
func symlink(src, dst string) error { |
| 222 |
if buildX || buildN { |
| 223 |
printcmd("ln -s %s %s", src, dst) |
| 224 |
} |
| 225 |
if buildN { |
| 226 |
return nil |
| 227 |
} |
| 228 |
if goos == "windows" { |
| 229 |
return doCopyAll(dst, src) |
| 230 |
} |
| 231 |
return os.Symlink(src, dst) |
| 232 |
} |
| 233 |
|
| 234 |
func doCopyAll(dst, src string) error { |
| 235 |
return filepath.Walk(src, func(path string, info os.FileInfo, errin error) (err error) { |
| 236 |
if errin != nil { |
| 237 |
return errin |
| 238 |
} |
| 239 |
prefixLen := len(src) |
| 240 |
if len(path) > prefixLen { |
| 241 |
prefixLen++ // file separator |
| 242 |
} |
| 243 |
outpath := filepath.Join(dst, path[prefixLen:]) |
| 244 |
if info.IsDir() { |
| 245 |
return os.Mkdir(outpath, 0755) |
| 246 |
} |
| 247 |
in, err := os.Open(path) |
| 248 |
if err != nil { |
| 249 |
return err |
| 250 |
} |
| 251 |
defer in.Close() |
| 252 |
out, err := os.OpenFile(outpath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, info.Mode()) |
| 253 |
if err != nil { |
| 254 |
return err |
| 255 |
} |
| 256 |
defer func() { |
| 257 |
if errc := out.Close(); err == nil { |
| 258 |
err = errc |
| 259 |
} |
| 260 |
}() |
| 261 |
_, err = io.Copy(out, in) |
| 262 |
return err |
| 263 |
}) |
| 264 |
} |
| 265 |
|
| 266 |
func removeAll(path string) error { |
| 267 |
if buildX || buildN { |
| 268 |
printcmd(`rm -r -f "%s"`, path) |
| 269 |
} |
| 270 |
if buildN { |
| 271 |
return nil |
| 272 |
} |
| 273 |
|
| 274 |
// os.RemoveAll behaves differently in windows. |
| 275 |
// http://golang.org/issues/9606 |
| 276 |
if goos == "windows" { |
| 277 |
resetReadOnlyFlagAll(path) |
| 278 |
} |
| 279 |
|
| 280 |
return os.RemoveAll(path) |
| 281 |
} |
| 282 |
|
| 283 |
func resetReadOnlyFlagAll(path string) error { |
| 284 |
fi, err := os.Stat(path) |
| 285 |
if err != nil { |
| 286 |
return err |
| 287 |
} |
| 288 |
if !fi.IsDir() { |
| 289 |
return os.Chmod(path, 0666) |
| 290 |
} |
| 291 |
fd, err := os.Open(path) |
| 292 |
if err != nil { |
| 293 |
return err |
| 294 |
} |
| 295 |
defer fd.Close() |
| 296 |
|
| 297 |
names, _ := fd.Readdirnames(-1) |
| 298 |
for _, name := range names { |
| 299 |
resetReadOnlyFlagAll(path + string(filepath.Separator) + name) |
| 300 |
} |
| 301 |
return nil |
| 302 |
} |
| 303 |
|
| 304 |
func goEnv(name string) string { |
| 305 |
if val := os.Getenv(name); val != "" { |
| 306 |
return val |
| 307 |
} |
| 308 |
val, err := exec.Command("go", "env", name).Output() |
| 309 |
if err != nil { |
| 310 |
panic(err) // the Go tool was tested to work earlier |
| 311 |
} |
| 312 |
return strings.TrimSpace(string(val)) |
| 313 |
} |
| 314 |
|
| 315 |
func runCmd(cmd *exec.Cmd) error { |
| 316 |
if buildX || buildN { |
| 317 |
dir := "" |
| 318 |
if cmd.Dir != "" { |
| 319 |
dir = "PWD=" + cmd.Dir + " " |
| 320 |
} |
| 321 |
env := strings.Join(cmd.Env, " ") |
| 322 |
if env != "" { |
| 323 |
env += " " |
| 324 |
} |
| 325 |
printcmd("%s%s%s", dir, env, strings.Join(cmd.Args, " ")) |
| 326 |
} |
| 327 |
|
| 328 |
buf := new(bytes.Buffer) |
| 329 |
buf.WriteByte('\n') |
| 330 |
if buildV { |
| 331 |
cmd.Stdout = os.Stdout |
| 332 |
cmd.Stderr = os.Stderr |
| 333 |
} else { |
| 334 |
cmd.Stdout = buf |
| 335 |
cmd.Stderr = buf |
| 336 |
} |
| 337 |
|
| 338 |
if buildWork { |
| 339 |
if goos == "windows" { |
| 340 |
cmd.Env = append(cmd.Env, `TEMP=`+tmpdir) |
| 341 |
cmd.Env = append(cmd.Env, `TMP=`+tmpdir) |
| 342 |
} else { |
| 343 |
cmd.Env = append(cmd.Env, `TMPDIR=`+tmpdir) |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
if !buildN { |
| 348 |
cmd.Env = environ(cmd.Env) |
| 349 |
if err := cmd.Run(); err != nil { |
| 350 |
return fmt.Errorf("%s failed: %v%s", strings.Join(cmd.Args, " "), err, buf) |
| 351 |
} |
| 352 |
} |
| 353 |
return nil |
| 354 |
} |