From a4a81cf73edc002f770de4969eefdd9d88ab79ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Tue, 19 Sep 2023 15:41:35 +0200 Subject: [PATCH 01/13] Download the SDK in Bazel --- examples/basic/WORKSPACE | 5 +++- rules.bzl | 64 +++++++++++++++++++++------------------- sha256sums.bzl | 30 +++++++++++++++++++ 3 files changed, 67 insertions(+), 32 deletions(-) create mode 100644 sha256sums.bzl diff --git a/examples/basic/WORKSPACE b/examples/basic/WORKSPACE index 703df1b..9ab1992 100644 --- a/examples/basic/WORKSPACE +++ b/examples/basic/WORKSPACE @@ -9,4 +9,7 @@ local_repository( load("@rules_android_ndk//:rules.bzl", "android_ndk_repository") -android_ndk_repository(name = "androidndk") +android_ndk_repository( + name = "androidndk", + version = "r25c", +) diff --git a/rules.bzl b/rules.bzl index 3b7c2ff..698e76c 100644 --- a/rules.bzl +++ b/rules.bzl @@ -14,8 +14,20 @@ """A repository rule for integrating the Android NDK.""" +load(":sha256sums.bzl", "ndk_sha256") + +def _ndk_platform(ctx): + if ctx.os.name == "linux": + return "linux" + elif ctx.os.name.startswith("mac os"): + return "darwin" + elif ctx.os.name.startswith("windows"): + return "windows" + else: + fail("Unsupported platform for the Android NDK: {}", ctx.os.name) + def _android_ndk_repository_impl(ctx): - """Install the Android NDK files. + """Download and extract the Android NDK files. Args: ctx: An implementation context. @@ -23,23 +35,33 @@ def _android_ndk_repository_impl(ctx): Returns: A final dict of configuration attributes and values. """ - ndk_path = ctx.attr.path or ctx.os.environ.get("ANDROID_NDK_HOME", None) - if not ndk_path: - fail("Either the ANDROID_NDK_HOME environment variable or the " + - "path attribute of android_ndk_repository must be set.") + + ndk_version = ctx.attr.version + ndk_platform = _ndk_platform(ctx) + ndk_default_url = "https://dl.google.com/android/repository/android-ndk-{version}-{platform}.zip".format( + version = ndk_version, + platform = ndk_platform, + ) + ndk_url = ctx.attr.urls.get(ndk_platform, ndk_default_url) + + filename = ndk_url.split("/")[-1] + sha256 = ndk_sha256(filename, ctx) + prefix = "android-ndk-{}".format(ndk_version) + + ctx.download_and_extract(url = ndk_url, sha256 = sha256, stripPrefix = prefix) if ctx.os.name == "linux": clang_directory = "toolchains/llvm/prebuilt/linux-x86_64" elif ctx.os.name == "mac os x": # Note: darwin-x86_64 does indeed contain fat binaries with arm64 slices, too. clang_directory = "toolchains/llvm/prebuilt/darwin-x86_64" + elif ctx.os.name == "windows": + clang_directory = "toolchains/llvm/prebuilt/windows-x86_64" else: - fail("Unsupported operating system: " + ctx.os.name) + fail("Unsupported operating system:", ctx.os.name) sysroot_directory = "%s/sysroot" % clang_directory - _create_symlinks(ctx, ndk_path, clang_directory, sysroot_directory) - api_level = ctx.attr.api_level or 31 lib64_clang_directory = clang_directory + "/lib64/clang" @@ -91,33 +113,13 @@ def _android_ndk_repository_impl(ctx): executable = False, ) -# Manually create a partial symlink tree of the NDK to avoid creating BUILD -# files in the real NDK directory. -def _create_symlinks(ctx, ndk_path, clang_directory, sysroot_directory): - # Path needs to end in "/" for replace() below to work - if not ndk_path.endswith("/"): - ndk_path = ndk_path + "/" - - for p in ctx.path(ndk_path + clang_directory).readdir(): - repo_relative_path = str(p).replace(ndk_path, "") - - # Skip sysroot directory, since it gets its own BUILD file - if repo_relative_path != sysroot_directory: - ctx.symlink(p, repo_relative_path) - - for p in ctx.path(ndk_path + sysroot_directory).readdir(): - repo_relative_path = str(p).replace(ndk_path, "") - ctx.symlink(p, repo_relative_path) - - ctx.symlink(ndk_path + "sources", "sources") - - # TODO(#32): Remove this hack - ctx.symlink(ndk_path + "sources", "ndk/sources") - _android_ndk_repository = repository_rule( attrs = { "path": attr.string(), "api_level": attr.int(), + "version": attr.string(default = "r25c"), + "urls": attr.string_dict(), + "sha256s": attr.string_dict(), }, local = True, implementation = _android_ndk_repository_impl, diff --git a/sha256sums.bzl b/sha256sums.bzl new file mode 100644 index 0000000..709ce8f --- /dev/null +++ b/sha256sums.bzl @@ -0,0 +1,30 @@ +""" +SHA256 checksums for downloaded NDK archives +""" + +_NDK_PACKAGE_SHA256SUMS = { + # r26 + "android-ndk-r26-windows.zip": "", + "android-ndk-r26-darwin.zip": "b2ab2fd17f71e2d2994c8c0ba2e48e99377806e05bf7477093344c26ab71dec0", + "android-ndk-r26-linux.zip": "1505c2297a5b7a04ed20b5d44da5665e91bac2b7c0fbcd3ae99b6ccc3a61289a", + # r25c + "android-ndk-r25c-windows.zip": "f70093964f6cbbe19268f9876a20f92d3a593db3ad2037baadd25fd8d71e84e2", + "android-ndk-r25c-darwin.zip": "b01bae969a5d0bfa0da18469f650a1628dc388672f30e0ba231da5c74245bc92", + "android-ndk-r25c-linux.zip": "769ee342ea75f80619d985c2da990c48b3d8eaf45f48783a2d48870d04b46108", +} + +def ndk_sha256(filename, repository_ctx): + """Get the sha256 for a specific NDK release + + Args: + filename: the name of the NDK release file (as seen on https://developer.android.com/ndk/downloads) + repository_ctx: the repository_rule ctx + + Returns: + a sha256sum string to use with ctx.download_and_extract + """ + internal_sha256 = _NDK_PACKAGE_SHA256SUMS.get(filename) + external_sha256 = repository_ctx.attr.urls.get(filename) + if internal_sha256 == None and external_sha256 == None: + fail("This NDK version is unsupported, and you haven't supplied a custom sha256sum for", filename) + return _NDK_PACKAGE_SHA256SUMS.get(filename) From 778daf9b4802ea1f77fefc014184073a8e2aef60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Tue, 19 Sep 2023 15:48:11 +0200 Subject: [PATCH 02/13] Add README instructions --- README.md | 26 +++++++++++++++++++++----- rules.bzl | 6 +++--- sha256sums.bzl | 2 +- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 5709048..35a683b 100644 --- a/README.md +++ b/README.md @@ -28,12 +28,28 @@ To use the Android NDK rules, add the following to your `WORKSPACE` file: sha256 = RULES_ANDROID_NDK_SHA, strip_prefix = "rules_android_ndk-%s" % RULES_ANDROID_NDK_COMMIT, ) - load("@rules_android_ndk//:rules.bzl", "android_ndk_repository") - android_ndk_repository(name = "androidndk") -Then, set the `ANDROID_NDK_HOME` environment variable or the `path` attribute of -`android_ndk_repository` to the path of the local Android NDK installation -directory. + load("@rules_android_ndk//:rules.bzl", "android_ndk_repository") + android_ndk_repository(name = "androidndk", version = "r25c") + +You can also customize the `base_url` attribute if, for example, you mirror the NDK archives +on a private server. + +Some sha256 checksums are included in this repository, but these might not be up to date, +if you want to use a version of the NDK that's not included, you can also specify the `sha256sums` +attribute which maps platforms to checksums, like so: + +``` +android_ndk_repository( + name = "androidndk", + version = "r25c" + sha256sums = { + "windows": "f70093964f6cbbe19268f9876a20f92d3a593db3ad2037baadd25fd8d71e84e2", + "darwin": "b01bae969a5d0bfa0da18469f650a1628dc388672f30e0ba231da5c74245bc92", + "linux": "769ee342ea75f80619d985c2da990c48b3d8eaf45f48783a2d48870d04b46108", + } +) +``` The `api_level` attribute can also be used to set the Android API level to build against. diff --git a/rules.bzl b/rules.bzl index 698e76c..2de5ad6 100644 --- a/rules.bzl +++ b/rules.bzl @@ -38,11 +38,11 @@ def _android_ndk_repository_impl(ctx): ndk_version = ctx.attr.version ndk_platform = _ndk_platform(ctx) - ndk_default_url = "https://dl.google.com/android/repository/android-ndk-{version}-{platform}.zip".format( + ndk_url = "{base_url}/android-ndk-{version}-{platform}.zip".format( + base_url = ctx.attr.base_url, version = ndk_version, platform = ndk_platform, ) - ndk_url = ctx.attr.urls.get(ndk_platform, ndk_default_url) filename = ndk_url.split("/")[-1] sha256 = ndk_sha256(filename, ctx) @@ -118,7 +118,7 @@ _android_ndk_repository = repository_rule( "path": attr.string(), "api_level": attr.int(), "version": attr.string(default = "r25c"), - "urls": attr.string_dict(), + "base_url": attr.string(default = "https://dl.google.com/android/repository"), "sha256s": attr.string_dict(), }, local = True, diff --git a/sha256sums.bzl b/sha256sums.bzl index 709ce8f..7264cca 100644 --- a/sha256sums.bzl +++ b/sha256sums.bzl @@ -4,7 +4,7 @@ SHA256 checksums for downloaded NDK archives _NDK_PACKAGE_SHA256SUMS = { # r26 - "android-ndk-r26-windows.zip": "", + "android-ndk-r26-windows.zip": "a748c6634b96991e15cb8902ffa4a498bba2ec6aa8028526de3c4c9dfcf00663", "android-ndk-r26-darwin.zip": "b2ab2fd17f71e2d2994c8c0ba2e48e99377806e05bf7477093344c26ab71dec0", "android-ndk-r26-linux.zip": "1505c2297a5b7a04ed20b5d44da5665e91bac2b7c0fbcd3ae99b6ccc3a61289a", # r25c From 7184085e89a7179054addfb14f3121ca99c46020 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Tue, 19 Sep 2023 15:53:52 +0200 Subject: [PATCH 03/13] Reset example files --- examples/basic/MODULE.bazel | 7 +++++++ examples/basic/WORKSPACE | 5 +---- 2 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 examples/basic/MODULE.bazel diff --git a/examples/basic/MODULE.bazel b/examples/basic/MODULE.bazel new file mode 100644 index 0000000..5377318 --- /dev/null +++ b/examples/basic/MODULE.bazel @@ -0,0 +1,7 @@ +module(name = "basic_example") + +bazel_dep(name = "rules_android_ndk") +local_path_override( + module_name = "rules_android_ndk", + path = "../..", +) diff --git a/examples/basic/WORKSPACE b/examples/basic/WORKSPACE index 9ab1992..703df1b 100644 --- a/examples/basic/WORKSPACE +++ b/examples/basic/WORKSPACE @@ -9,7 +9,4 @@ local_repository( load("@rules_android_ndk//:rules.bzl", "android_ndk_repository") -android_ndk_repository( - name = "androidndk", - version = "r25c", -) +android_ndk_repository(name = "androidndk") From 33894ac9f7e09aae78376748135928331c51ca77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Tue, 19 Sep 2023 16:00:43 +0200 Subject: [PATCH 04/13] Fix typo --- sha256sums.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sha256sums.bzl b/sha256sums.bzl index 7264cca..ffe39fa 100644 --- a/sha256sums.bzl +++ b/sha256sums.bzl @@ -24,7 +24,7 @@ def ndk_sha256(filename, repository_ctx): a sha256sum string to use with ctx.download_and_extract """ internal_sha256 = _NDK_PACKAGE_SHA256SUMS.get(filename) - external_sha256 = repository_ctx.attr.urls.get(filename) + external_sha256 = repository_ctx.attr.sha256s.get(filename) if internal_sha256 == None and external_sha256 == None: fail("This NDK version is unsupported, and you haven't supplied a custom sha256sum for", filename) return _NDK_PACKAGE_SHA256SUMS.get(filename) From e019016847199588a981b668526aec3da5945ba9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Tue, 19 Sep 2023 16:22:43 +0200 Subject: [PATCH 05/13] Fixes --- BUILD.ndk_root.tpl | 6 ++++-- rules.bzl | 12 +++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/BUILD.ndk_root.tpl b/BUILD.ndk_root.tpl index 2ed0917..7b8387e 100644 --- a/BUILD.ndk_root.tpl +++ b/BUILD.ndk_root.tpl @@ -26,13 +26,15 @@ cc_library( name = "cpufeatures", srcs = glob([ "sources/android/cpufeatures/*.c", + ]) + glob([ # TODO(#32): Remove this hack "ndk/sources/android/cpufeatures/*.c", - ]), + ], allow_empty = True), hdrs = glob([ "sources/android/cpufeatures/*.h", + ]) + glob([ # TODO(#32): Remove this hack "ndk/sources/android/cpufeatures/*.h", - ]), + ], allow_empty = True), linkopts = ["-ldl"], ) diff --git a/rules.bzl b/rules.bzl index cb73e74..e2b1021 100644 --- a/rules.bzl +++ b/rules.bzl @@ -48,17 +48,19 @@ def _android_ndk_repository_impl(ctx): sha256 = ndk_sha256(filename, ctx) prefix = "android-ndk-{}".format(ndk_version) - ctx.download_and_extract(url = ndk_url, sha256 = sha256, stripPrefix = prefix) + result = ctx.download_and_extract(url = ndk_url, sha256 = sha256, stripPrefix = prefix) + if not result.success: + fail("Failed to download NDK archive", ndk_url) - if ctx.os.name == "linux": + if ndk_platform == "linux": clang_directory = "toolchains/llvm/prebuilt/linux-x86_64" - elif ctx.os.name == "mac os x": + elif ndk_platform == "mac os x": # Note: darwin-x86_64 does indeed contain fat binaries with arm64 slices, too. clang_directory = "toolchains/llvm/prebuilt/darwin-x86_64" - elif ctx.os.name == "windows": + elif ndk_platform == "windows": clang_directory = "toolchains/llvm/prebuilt/windows-x86_64" else: - fail("Unsupported operating system:", ctx.os.name) + fail("Unsupported NDK platform", ndk_platform) sysroot_directory = "%s/sysroot" % clang_directory From e55654d34e25121ea59c67f66c3b1c38283de876 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Tue, 19 Sep 2023 17:46:55 +0200 Subject: [PATCH 06/13] Lowercase OS name --- rules.bzl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rules.bzl b/rules.bzl index e2b1021..cfa8c9a 100644 --- a/rules.bzl +++ b/rules.bzl @@ -17,11 +17,12 @@ load(":sha256sums.bzl", "ndk_sha256") def _ndk_platform(ctx): - if ctx.os.name == "linux": + os_name = ctx.os.name.lower() + if os_name == "linux": return "linux" - elif ctx.os.name.startswith("mac os"): + elif os_name.startswith("mac os"): return "darwin" - elif ctx.os.name.startswith("windows"): + elif os_name.startswith("windows"): return "windows" else: fail("Unsupported platform for the Android NDK: {}", ctx.os.name) From b3a7e47d0fa76f0b4a0a425432e4884d3accd498 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Tue, 19 Sep 2023 17:47:25 +0200 Subject: [PATCH 07/13] Fix platform condition --- rules.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules.bzl b/rules.bzl index cfa8c9a..2585213 100644 --- a/rules.bzl +++ b/rules.bzl @@ -55,7 +55,7 @@ def _android_ndk_repository_impl(ctx): if ndk_platform == "linux": clang_directory = "toolchains/llvm/prebuilt/linux-x86_64" - elif ndk_platform == "mac os x": + elif ndk_platform == "darwin: # Note: darwin-x86_64 does indeed contain fat binaries with arm64 slices, too. clang_directory = "toolchains/llvm/prebuilt/darwin-x86_64" elif ndk_platform == "windows": From 9ed60c8baa8b53def356e2a0be31b8bf5852f45b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Tue, 19 Sep 2023 17:55:02 +0200 Subject: [PATCH 08/13] Use r25b as default for now --- rules.bzl | 9 ++++----- sha256sums.bzl | 4 ++++ utils/checksums.sh | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) create mode 100755 utils/checksums.sh diff --git a/rules.bzl b/rules.bzl index 2585213..370a1b9 100644 --- a/rules.bzl +++ b/rules.bzl @@ -55,7 +55,7 @@ def _android_ndk_repository_impl(ctx): if ndk_platform == "linux": clang_directory = "toolchains/llvm/prebuilt/linux-x86_64" - elif ndk_platform == "darwin: + elif ndk_platform == "darwin": # Note: darwin-x86_64 does indeed contain fat binaries with arm64 slices, too. clang_directory = "toolchains/llvm/prebuilt/darwin-x86_64" elif ndk_platform == "windows": @@ -65,7 +65,7 @@ def _android_ndk_repository_impl(ctx): sysroot_directory = "%s/sysroot" % clang_directory - api_level = ctx.attr.api_level or 31 + api_level = ctx.attr.api_level result = ctx.execute([clang_directory + "/bin/clang", "--print-resource-dir"]) if result.return_code != 0: @@ -116,9 +116,8 @@ def _android_ndk_repository_impl(ctx): _android_ndk_repository = repository_rule( attrs = { - "path": attr.string(), - "api_level": attr.int(), - "version": attr.string(default = "r25c"), + "api_level": attr.int(default = 31), + "version": attr.string(default = "r25b"), "base_url": attr.string(default = "https://dl.google.com/android/repository"), "sha256s": attr.string_dict(), }, diff --git a/sha256sums.bzl b/sha256sums.bzl index ffe39fa..0cc8027 100644 --- a/sha256sums.bzl +++ b/sha256sums.bzl @@ -11,6 +11,10 @@ _NDK_PACKAGE_SHA256SUMS = { "android-ndk-r25c-windows.zip": "f70093964f6cbbe19268f9876a20f92d3a593db3ad2037baadd25fd8d71e84e2", "android-ndk-r25c-darwin.zip": "b01bae969a5d0bfa0da18469f650a1628dc388672f30e0ba231da5c74245bc92", "android-ndk-r25c-linux.zip": "769ee342ea75f80619d985c2da990c48b3d8eaf45f48783a2d48870d04b46108", + # r25b + "android-ndk-r25b-windows.zip": "c9a72beda4663ab714c9fb3dc06bb9b9f124f2b5199957c86cd6f57eb59fd49a", + "android-ndk-r25b-darwin.zip": "7e12f1f809878d4f5d5a901809277aa31546d36c10730fade2036d7d95b3607a", + "android-ndk-r25b-linux.zip": "403ac3e3020dd0db63a848dcaba6ceb2603bf64de90949d5c4361f848e44b005", } def ndk_sha256(filename, repository_ctx): diff --git a/utils/checksums.sh b/utils/checksums.sh new file mode 100755 index 0000000..48dce07 --- /dev/null +++ b/utils/checksums.sh @@ -0,0 +1,14 @@ +#!/bin/bash +# Download a specific release of the NDK for all three platforms and calculate SHA256 checksums +# to add to the sha256sums.bzl file. +set -eu + +ndk_version=$1 + +cd "$(mktemp -d)" +echo "Working in $PWD" + +curl -LO "https://dl.google.com/android/repository/android-ndk-${ndk_version}-windows.zip" +curl -LO "https://dl.google.com/android/repository/android-ndk-${ndk_version}-darwin.zip" +curl -LO "https://dl.google.com/android/repository/android-ndk-${ndk_version}-linux.zip" +sha256sum ./* \ No newline at end of file From 9b1a35cdcf6fe74627d6e440f260b06be2e34d08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Tue, 19 Sep 2023 18:01:10 +0200 Subject: [PATCH 09/13] Fixes for ndk/sources symlink --- BUILD.ndk_root.tpl | 6 ++---- rules.bzl | 3 +++ 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/BUILD.ndk_root.tpl b/BUILD.ndk_root.tpl index 7b8387e..2ed0917 100644 --- a/BUILD.ndk_root.tpl +++ b/BUILD.ndk_root.tpl @@ -26,15 +26,13 @@ cc_library( name = "cpufeatures", srcs = glob([ "sources/android/cpufeatures/*.c", - ]) + glob([ # TODO(#32): Remove this hack "ndk/sources/android/cpufeatures/*.c", - ], allow_empty = True), + ]), hdrs = glob([ "sources/android/cpufeatures/*.h", - ]) + glob([ # TODO(#32): Remove this hack "ndk/sources/android/cpufeatures/*.h", - ], allow_empty = True), + ]), linkopts = ["-ldl"], ) diff --git a/rules.bzl b/rules.bzl index 370a1b9..89d237d 100644 --- a/rules.bzl +++ b/rules.bzl @@ -65,6 +65,9 @@ def _android_ndk_repository_impl(ctx): sysroot_directory = "%s/sysroot" % clang_directory + # TODO(#32): Remove this hack + ctx.symlink("sources", "ndk/sources") + api_level = ctx.attr.api_level result = ctx.execute([clang_directory + "/bin/clang", "--print-resource-dir"]) From df23b2c27a89d72c9ef8393fc5cb14cbec482ee4 Mon Sep 17 00:00:00 2001 From: Gabriel Feron Date: Wed, 20 Sep 2023 09:57:38 +0100 Subject: [PATCH 10/13] Avoid restarting rule multiple times --- rules.bzl | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/rules.bzl b/rules.bzl index 89d237d..b4b0e32 100644 --- a/rules.bzl +++ b/rules.bzl @@ -75,13 +75,11 @@ def _android_ndk_repository_impl(ctx): fail("Failed to execute clang: %s" % result.stderr) clang_resource_directory = result.stdout.strip().split(clang_directory)[1].strip("/") - # Use a label relative to the workspace from which this repository rule came - # to get the workspace name. repository_name = Label("//:BUILD").workspace_name ctx.template( - "BUILD", - Label("//:BUILD.ndk_root.tpl"), + "BUILD.bazel", + ctx.attr._template_ndk_root, { "{clang_directory}": clang_directory, }, @@ -90,7 +88,7 @@ def _android_ndk_repository_impl(ctx): ctx.template( "target_systems.bzl", - Label("//:target_systems.bzl.tpl"), + ctx.attr._template_target_systems, { }, executable = False, @@ -98,7 +96,7 @@ def _android_ndk_repository_impl(ctx): ctx.template( "%s/BUILD" % clang_directory, - Label("//:BUILD.ndk_clang.tpl"), + ctx.attr._template_ndk_clang, { "{repository_name}": repository_name, "{api_level}": str(api_level), @@ -110,7 +108,7 @@ def _android_ndk_repository_impl(ctx): ctx.template( "%s/BUILD" % sysroot_directory, - Label("//:BUILD.ndk_sysroot.tpl"), + ctx.attr._template_ndk_sysroot, { "{api_level}": str(api_level), }, @@ -123,6 +121,10 @@ _android_ndk_repository = repository_rule( "version": attr.string(default = "r25b"), "base_url": attr.string(default = "https://dl.google.com/android/repository"), "sha256s": attr.string_dict(), + "_template_ndk_root": attr.label(default = ":BUILD.ndk_root.tpl", allow_single_file = True), + "_template_target_systems": attr.label(default = ":target_systems.bzl.tpl", allow_single_file = True), + "_template_ndk_clang": attr.label(default = ":BUILD.ndk_clang.tpl", allow_single_file = True), + "_template_ndk_sysroot": attr.label(default = ":BUILD.ndk_sysroot.tpl", allow_single_file = True), }, local = True, implementation = _android_ndk_repository_impl, From d05a9dbfa15ebad92a6c537ff7c00babc56e5152 Mon Sep 17 00:00:00 2001 From: Gabriel Feron Date: Wed, 20 Sep 2023 14:39:33 +0200 Subject: [PATCH 11/13] Remove local = True --- rules.bzl | 1 - 1 file changed, 1 deletion(-) diff --git a/rules.bzl b/rules.bzl index b4b0e32..9e26f9d 100644 --- a/rules.bzl +++ b/rules.bzl @@ -126,7 +126,6 @@ _android_ndk_repository = repository_rule( "_template_ndk_clang": attr.label(default = ":BUILD.ndk_clang.tpl", allow_single_file = True), "_template_ndk_sysroot": attr.label(default = ":BUILD.ndk_sysroot.tpl", allow_single_file = True), }, - local = True, implementation = _android_ndk_repository_impl, ) From 5f22791c2a446ac6344518320afe61a11169bd70 Mon Sep 17 00:00:00 2001 From: Gabriel Feron Date: Wed, 20 Sep 2023 14:42:08 +0200 Subject: [PATCH 12/13] Add one more label to repository_ctx attrs --- rules.bzl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rules.bzl b/rules.bzl index 9e26f9d..cdfbfec 100644 --- a/rules.bzl +++ b/rules.bzl @@ -75,7 +75,7 @@ def _android_ndk_repository_impl(ctx): fail("Failed to execute clang: %s" % result.stderr) clang_resource_directory = result.stdout.strip().split(clang_directory)[1].strip("/") - repository_name = Label("//:BUILD").workspace_name + repository_name = ctx.attr._build.workspace_name ctx.template( "BUILD.bazel", @@ -121,6 +121,7 @@ _android_ndk_repository = repository_rule( "version": attr.string(default = "r25b"), "base_url": attr.string(default = "https://dl.google.com/android/repository"), "sha256s": attr.string_dict(), + "_build": attr.label(default = ":BUILD", allow_single_file = True), "_template_ndk_root": attr.label(default = ":BUILD.ndk_root.tpl", allow_single_file = True), "_template_target_systems": attr.label(default = ":target_systems.bzl.tpl", allow_single_file = True), "_template_ndk_clang": attr.label(default = ":BUILD.ndk_clang.tpl", allow_single_file = True), From a5e42d637fd54728a2d481141956ce4d0abdf82c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Tue, 26 Sep 2023 12:50:23 +0200 Subject: [PATCH 13/13] Remove register_toolchains --- README.md | 3 +++ rules.bzl | 9 +-------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 35a683b..0ab8d46 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,9 @@ To use the Android NDK rules, add the following to your `WORKSPACE` file: load("@rules_android_ndk//:rules.bzl", "android_ndk_repository") android_ndk_repository(name = "androidndk", version = "r25c") + # either register the toolchains, or use `--extra_toolchains` when invoking Bazel + register_toolchains("@androidndk//:all") + You can also customize the `base_url` attribute if, for example, you mirror the NDK archives on a private server. diff --git a/rules.bzl b/rules.bzl index cdfbfec..38ed0dd 100644 --- a/rules.bzl +++ b/rules.bzl @@ -115,7 +115,7 @@ def _android_ndk_repository_impl(ctx): executable = False, ) -_android_ndk_repository = repository_rule( +android_ndk_repository = repository_rule( attrs = { "api_level": attr.int(default = 31), "version": attr.string(default = "r25b"), @@ -129,10 +129,3 @@ _android_ndk_repository = repository_rule( }, implementation = _android_ndk_repository_impl, ) - -def android_ndk_repository(name, **kwargs): - _android_ndk_repository( - name = name, - **kwargs - ) - native.register_toolchains("@%s//:all" % name)