Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check NDK version in Bazel #331

Merged
merged 4 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions gapidapk/android/apk/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down
48 changes: 48 additions & 0 deletions tools/build/rules/android.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
)
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