Skip to content

Commit

Permalink
Check NDK version in Bazel
Browse files Browse the repository at this point in the history
This makes sure we refuse to build with an invalid NDK version.

Bug: b/158669059
  • Loading branch information
hevrard committed Jun 11, 2020
1 parent 693e813 commit 23f5d44
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 12 deletions.
35 changes: 26 additions & 9 deletions BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,20 @@ To fetch the required packages, using a console type:
```
cd <sdk-path>
tools\bin\sdkmanager.bat "platforms;android-26" "build-tools;29.0.2" ndk-bundle
tools\bin\sdkmanager.bat "platforms;android-26" "build-tools;29.0.2"
```
Note: this will install the latest NDK in `<sdk-path>\ndk-bundle`. The recommended version of the NDK is **r21**.
If you do not have adb installed you can do so with:
```
cd <sdk-path>
tools\bin\sdkmanager.bat platform-tools
```
Unzip the
[Android NDK **r21**](https://dl.google.com/android/repository/android-ndk-r21-windows-x86_64.zip)
into a directory of your choosing, and set the `ANDROID_NDK_HOME` environment
variable to point to this directory.
### Configure the environment
Either do this globally or in your shell every time.
Expand Down Expand Up @@ -132,17 +135,24 @@ To fetch the required packages, using a console type:
```
cd <sdk-path>
tools/bin/sdkmanager "platforms;android-26" "build-tools;29.0.2" ndk-bundle
tools/bin/sdkmanager "platforms;android-26" "build-tools;29.0.2"
```
Note: this will install the latest NDK in `<sdk-path>/ndk-bundle`. The recommended version of the NDK is **r21**.
If you do not have adb installed you can do so with:
```
cd <sdk-path>
tools/bin/sdkmanager platform-tools
```
Unzip the
[Android NDK **r21**](https://dl.google.com/android/repository/android-ndk-r21-darwin-x86_64.zip)
into a directory of your choosing, and set the `ANDROID_NDK_HOME` environment
variable to point to this directory:
```
export ANDROID_NDK_HOME=<ndk-path>
```
### Install the XCode command line tools
After installing, ensure the XCode license is signed with:
Expand Down Expand Up @@ -199,17 +209,24 @@ To fetch the required packages, using a console type:
```
cd <sdk-path>
tools/bin/sdkmanager "platforms;android-26" "build-tools;29.0.2" ndk-bundle
tools/bin/sdkmanager "platforms;android-26" "build-tools;29.0.2"
```
Note: this will install the latest NDK in `<sdk-path>/ndk-bundle`. The recommended version of the NDK is **r21**.
If you do not have adb installed you can do so with:
```
cd <sdk-path>
tools/bin/sdkmanager platform-tools
```
Unzip the
[Android NDK **r21**](https://dl.google.com/android/repository/android-ndk-r21-linux-x86_64.zip)
into a directory of your choosing, and set the `ANDROID_NDK_HOME` environment
variable to point to this directory:
```
export ANDROID_NDK_HOME=<ndk-path>
```
### Install other libraries
```
Expand Down
2 changes: 1 addition & 1 deletion gapidapk/android/apk/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def gapid_apk(name = "", abi = "", pkg = "", libs = {}, bins = {}):
"//conditions:default": [],
}),
deps = select({
"//tools/build:android-" + name: ["@ndk_vk_validation_layer//:" + abi],
"//tools/build:android-" + name: ["@ndk_version_check//:ndk_version_check", "@ndk_vk_validation_layer//:" + abi],
"//conditions:default": [],
}),
)
Expand Down
11 changes: 10 additions & 1 deletion kokoro/presubmit/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,17 @@ curl -L -k -s https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo apt-get update
sudo apt-get install -y clang-format-6.0

# Setup environment.
# Mock the correct NDK version.
# Our Bazel scripts check that the correct NDK version is installed: rather
# than downloading the 1+GB NDK just to have a matching version to pass this
# presubmit test, we "mock" the version by overwriting it manually.
export ANDROID_NDK_HOME=/opt/android-ndk-r16b
cat > $ANDROID_NDK_HOME/source.properties <<EOF
Pkg.Desc = Android NDK
Pkg.Revision = 21.0.6113669
EOF

# Setup environment.
export BAZEL=$BUILD_ROOT/bazel/bin/bazel
export BUILDIFIER=$BUILD_ROOT/tools/bin/buildifier
export BUILDOZER=$BUILD_ROOT/tools/bin/buildozer
Expand Down
27 changes: 27 additions & 0 deletions tools/build/rules/android.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,30 @@ ndk_vk_validation_layer = repository_rule(
"ANDROID_NDK_HOME",
],
)

# Enforce NDK version by checking $ANDROID_NDK_HOME/source.properties
def _ndk_version_check(repository_ctx):
# This should be updated in sync with BUILDING.md documentation
version = "21.0.6113669"
sourcePropFile = repository_ctx.os.environ["ANDROID_NDK_HOME"] + "/source.properties"
sourceProp = repository_ctx.read(sourcePropFile)
if not ("Pkg.Revision = " + version) in sourceProp:
fail("NDK version mismatch: expected version " + version + " in file " + sourcePropFile)

build = """
# Dummy rule
cc_import(
name = "ndk_version_check",
visibility = ["//visibility:public"],
)
"""

repository_ctx.file("BUILD", build)

ndk_version_check = repository_rule(
implementation = _ndk_version_check,
local = True,
environ = [
"ANDROID_NDK_HOME",
]
)
8 changes: 7 additions & 1 deletion tools/build/workspace.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# dependencies and toolchains.

load("@gapid//tools/build:cc_toolchain.bzl", "cc_configure")
load("@gapid//tools/build/rules:android.bzl", "android_native_app_glue", "ndk_vk_validation_layer")
load("@gapid//tools/build/rules:android.bzl", "android_native_app_glue", "ndk_vk_validation_layer", "ndk_version_check")
load("@gapid//tools/build/rules:repository.bzl", "github_repository", "maybe_repository")
load("@gapid//tools/build/third_party:breakpad.bzl", "breakpad")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository", "new_git_repository")
Expand Down Expand Up @@ -324,6 +324,12 @@ def gapid_dependencies(android = True, mingw = True, locals = {}):
locals = locals,
)

maybe_repository(
ndk_version_check,
name = "ndk_version_check",
locals = locals,
)

# Use the LLVM libc++ Android toolchain.
native.bind(
name = "android/crosstool",
Expand Down

0 comments on commit 23f5d44

Please sign in to comment.