From 3e0bab5405d63a8f5dd9d9764a24c8e5ac4997fa Mon Sep 17 00:00:00 2001 From: Daniel Lublin Date: Sun, 14 Apr 2019 20:50:36 +0200 Subject: [PATCH] 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 Reviewed-by: Elias Naur --- cmd/gomobile/env.go | 23 +++++++++----- cmd/gomobile/env_test.go | 66 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 cmd/gomobile/env_test.go diff --git a/cmd/gomobile/env.go b/cmd/gomobile/env.go index dbf9c8c..c70b2a1 100644 --- a/cmd/gomobile/env.go +++ b/cmd/gomobile/env.go @@ -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) { diff --git a/cmd/gomobile/env_test.go b/cmd/gomobile/env_test.go new file mode 100644 index 0000000..c041313 --- /dev/null +++ b/cmd/gomobile/env_test.go @@ -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) + } +}