diff --git a/BUILDING.md b/BUILDING.md index 95e5c2929..891e8aea4 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -74,17 +74,20 @@ To fetch the required packages, using a console type: ``` cd -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 `\ndk-bundle`. The recommended version of the NDK is **r21**. - If you do not have adb installed you can do so with: ``` cd 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. @@ -132,17 +135,24 @@ To fetch the required packages, using a console type: ``` cd -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 `/ndk-bundle`. The recommended version of the NDK is **r21**. - If you do not have adb installed you can do so with: ``` cd 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= +``` + ### Install the XCode command line tools After installing, ensure the XCode license is signed with: @@ -199,17 +209,24 @@ To fetch the required packages, using a console type: ``` cd -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 `/ndk-bundle`. The recommended version of the NDK is **r21**. - If you do not have adb installed you can do so with: ``` cd 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= +``` + ### Install other libraries ``` diff --git a/gapidapk/android/apk/rules.bzl b/gapidapk/android/apk/rules.bzl index 81e28920e..d8e03e239 100644 --- a/gapidapk/android/apk/rules.bzl +++ b/gapidapk/android/apk/rules.bzl @@ -124,6 +124,7 @@ def gapid_apk(name = "", abi = "", pkg = "", libs = {}, bins = {}): assets = assets, assets_dir = abi, deps = [ + "@ndk_version_check//:version_check", "//gapidapk/android/app/src/main:gapid", ":" + name + "_native", ], diff --git a/tools/build/rules/android.bzl b/tools/build/rules/android.bzl index a20aa89cb..311e54832 100644 --- a/tools/build/rules/android.bzl +++ b/tools/build/rules/android.bzl @@ -135,3 +135,51 @@ 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 + expectedVersion = "21.0.6113669" + + # Extract NDK version string from $ANDROID_NDK_HOME/source.properties, + # which has the following format: + # Pkg.Desc = Android NDK + # Pkg.Revision = 21.0.6113669 + sourcePropFile = repository_ctx.os.environ["ANDROID_NDK_HOME"] + "/source.properties" + sourceProp = repository_ctx.read(sourcePropFile) + secondLine = sourceProp.split("\n")[1] + ndkVersion = secondLine.split(" ")[2] + + # We want to fail only when Bazel tries to actually build a native target + # on Android, such that e.g. the presubmit check can pass without having + # to install the expected NDK version. To this aim, we create a rule to be + # used as a dependency in android_binary() rules. This rule fails when our + # check fails, and otherwise returns a CcInfo provider needed to be a + # valid dependency of android_binary(). + ruleBody = "def _version_check(ctx):\n" + if ndkVersion != expectedVersion: + ruleBody += " fail(\"Wrong NDK version: expected " + expectedVersion + ", got " + ndkVersion + "\")\n" + else: + ruleBody += " return [CcInfo()]\n" + ruleBody += "\n" + ruleBody += "version_check = rule(implementation = _version_check)\n" + repository_ctx.file("version.bzl", ruleBody) + + # Create a BUILD file to instanciate the rule. + build = """ +load("//:version.bzl", "version_check") + +version_check( + name = "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", + ] +) diff --git a/tools/build/workspace.bzl b/tools/build/workspace.bzl index bbc3d0dea..831eb59a9 100644 --- a/tools/build/workspace.bzl +++ b/tools/build/workspace.bzl @@ -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") @@ -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",