cmd/gomobile: fall back to $ANDROID_NDK_HOME for the NDK path

After trying $ANDROID_HOME/ndk-bundle, we also try $ANDROID_NDK_HOME.

ANDROID_NDK_HOME is advised in this NDK sample code wiki:
https://github.com/googlesamples/android-ndk/wiki
Mentioned in the Bazel buildsystem Android app tutorial:
https://docs.bazel.build/versions/master/tutorial/android-app.html

On Ubuntu, the google-android-ndk-installer leaves the NDK in
/usr/lib/android-ndk, and it seems to be up to the user to set
ANDROID_NDK_HOME.

On Arch Linux, the android-ndk package installs the NDK into /opt/android-ndk
and sets ANDROID_NDK_HOME to there using an /etc/profile.d/file

Fixes golang/go#31461

Change-Id: I9f7f7e24b19e0047419f9725b67bd6daf2b1d328
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/171938
Run-TryBot: Elias Naur <mail@eliasnaur.com>
Reviewed-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Daniel Lublin 2019-04-14 20:50:36 +02:00 committed by Elias Naur
parent 28c689b77b
commit 3e0bab5405
2 changed files with 82 additions and 7 deletions

View File

@ -161,16 +161,25 @@ func ndkRoot() (string, error) {
if buildN {
return "$NDK_PATH", nil
}
androidHome := os.Getenv("ANDROID_HOME")
if androidHome == "" {
return "", errors.New("The Android SDK was not found. Please set ANDROID_HOME to the root of the Android SDK.")
if androidHome != "" {
ndkRoot := filepath.Join(androidHome, "ndk-bundle")
_, err := os.Stat(ndkRoot)
if err == nil {
return ndkRoot, nil
}
}
ndkRoot := filepath.Join(androidHome, "ndk-bundle")
_, err := os.Stat(ndkRoot)
if err != nil {
return "", fmt.Errorf("The NDK was not found in $ANDROID_HOME/ndk-bundle (%q). Install the NDK with `sdkmanager 'ndk-bundle'`", ndkRoot)
ndkRoot := os.Getenv("ANDROID_NDK_HOME")
if ndkRoot != "" {
_, err := os.Stat(ndkRoot)
if err == nil {
return ndkRoot, nil
}
}
return ndkRoot, nil
return "", fmt.Errorf("no Android NDK found in $ANDROID_HOME/ndk-bundle nor in $ANDROID_NDK_HOME")
}
func envClang(sdkName string) (clang, cflags string, err error) {

66
cmd/gomobile/env_test.go Normal file
View File

@ -0,0 +1,66 @@
// Copyright 2019 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 (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestNdkRoot(t *testing.T) {
home, err := ioutil.TempDir("", "gomobile-test-")
if err != nil {
t.Fatal(err)
}
homeorig := os.Getenv("ANDROID_HOME")
ndkhomeorig := os.Getenv("ANDROID_NDK_HOME")
defer func() {
os.Setenv("ANDROID_HOME", homeorig)
os.Setenv("ANDROID_NDK_HOME", ndkhomeorig)
os.RemoveAll(home)
}()
os.Setenv("ANDROID_HOME", home)
if ndk, err := ndkRoot(); err == nil {
t.Errorf("expected error but got %q", ndk)
}
sdkNDK := filepath.Join(home, "ndk-bundle")
envNDK := filepath.Join(home, "android-ndk")
for _, dir := range []string{sdkNDK, envNDK} {
if err := os.Mkdir(dir, 0755); err != nil {
t.Fatalf("couldn't mkdir %q", dir)
}
}
os.Setenv("ANDROID_NDK_HOME", envNDK)
if ndk, _ := ndkRoot(); ndk != sdkNDK {
t.Errorf("got %q want %q", ndk, sdkNDK)
}
os.Unsetenv("ANDROID_HOME")
if ndk, _ := ndkRoot(); ndk != envNDK {
t.Errorf("got %q want %q", ndk, envNDK)
}
os.RemoveAll(envNDK)
if ndk, err := ndkRoot(); err == nil {
t.Errorf("expected error but got %q", ndk)
}
os.Setenv("ANDROID_HOME", home)
if ndk, _ := ndkRoot(); ndk != sdkNDK {
t.Errorf("got %q want %q", ndk, sdkNDK)
}
}