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

Create a partial symlink tree of the NDK rather than symlinking just … #14

Merged
merged 1 commit into from
Sep 13, 2022
Merged
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
31 changes: 23 additions & 8 deletions rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ def _android_ndk_repository_impl(ctx):
fail("Either the ANDROID_NDK_HOME environment variable or the " +
"path attribute of android_ndk_repository must be set.")

ctx.symlink(ndk_path + "/toolchains", "toolchains")

if ctx.os.name == "linux":
clang_directory = "toolchains/llvm/prebuilt/linux-x86_64"
elif ctx.os.name == "mac os x":
clang_directory = "toolchains/llvm/prebuilt/darwin-x86_64"
else:
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

clang_version = "14.0.6"
clang_resource_directory = "lib64/clang/%s" % clang_version

sysroot_directory = "%s/sysroot" % clang_directory

# Use a label relative to the workspace from which this repository rule came
# to get the workspace name.
repository_name = Label("//:BUILD").workspace_name
Expand All @@ -60,8 +60,6 @@ def _android_ndk_repository_impl(ctx):
executable = False,
)

# TODO: This creates a build file in the real ndk directory. This can be
# avoided by creating a symlink tree (or partial one).
ctx.template(
"%s/BUILD" % clang_directory,
Label("//:BUILD.ndk_clang.tpl"),
Expand All @@ -74,8 +72,6 @@ def _android_ndk_repository_impl(ctx):
executable = False,
)

# TODO: This creates a build file in the real ndk directory. This can be
# avoided by creating a symlink tree (or partial one).
ctx.template(
"%s/BUILD" % sysroot_directory,
Label("//:BUILD.ndk_sysroot"),
Expand All @@ -85,6 +81,25 @@ 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)


android_ndk_repository = repository_rule(
implementation = _android_ndk_repository_impl,
attrs = {
Expand Down