-
Notifications
You must be signed in to change notification settings - Fork 15
/
rules.bzl
139 lines (118 loc) · 4.97 KB
/
rules.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Copyright 2022 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A repository rule for integrating the Android NDK."""
def _android_ndk_repository_impl(ctx):
"""Install the Android NDK files.
Args:
ctx: An implementation context.
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.")
if ndk_path.startswith("$WORKSPACE_ROOT"):
ndk_path = str(ctx.workspace_root) + ndk_path.removeprefix("$WORKSPACE_ROOT")
is_windows = False
executable_extension = ""
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.startswith("windows"):
clang_directory = "toolchains/llvm/prebuilt/windows-x86_64"
is_windows = True
executable_extension = ".exe"
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
result = ctx.execute([clang_directory + "/bin/clang", "--print-resource-dir"])
if result.return_code != 0:
fail("Failed to execute clang: %s" % result.stderr)
stdout = result.stdout.strip()
if is_windows:
stdout = stdout.replace("\\", "/")
clang_resource_directory = stdout.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 = ctx.attr._build.workspace_name
ctx.template(
"BUILD.bazel",
ctx.attr._template_ndk_root,
{
"{clang_directory}": clang_directory,
},
executable = False,
)
ctx.template(
"target_systems.bzl",
ctx.attr._template_target_systems,
{
},
executable = False,
)
ctx.template(
"%s/BUILD.bazel" % clang_directory,
ctx.attr._template_ndk_clang,
{
"{repository_name}": repository_name,
"{api_level}": str(api_level),
"{clang_resource_directory}": clang_resource_directory,
"{sysroot_directory}": sysroot_directory,
"{executable_extension}": executable_extension,
},
executable = False,
)
ctx.template(
"%s/BUILD.bazel" % sysroot_directory,
ctx.attr._template_ndk_sysroot,
{
"{api_level}": str(api_level),
},
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(),
"_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),
"_template_ndk_sysroot": attr.label(default = ":BUILD.ndk_sysroot.tpl", allow_single_file = True),
},
local = True,
implementation = _android_ndk_repository_impl,
)