Skip to content

Commit 2406e2e

Browse files
author
JiaYan Lin
committed
Use module_extension for androidsdk & prepare bzlmod for example/basicapp
1 parent 0a68ba6 commit 2406e2e

File tree

10 files changed

+173
-28
lines changed

10 files changed

+173
-28
lines changed

.bazelci/presubmit.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ tasks:
109109
ubuntu2004_basicapp_bzlmod:
110110
name: "Basic app ubuntu bzlmod"
111111
platform: ubuntu2004
112-
name: basicapp
113112
working_directory: examples/basicapp
114113
build_flags:
115114
<<: *rules_flags
@@ -135,4 +134,3 @@ tasks:
135134
? "--enable_bzlmod"
136135
build_targets:
137136
- "//java/com/basicapp:basic_app"
138-

BUILD

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,31 @@ platform(
5353
"@platforms//os:android",
5454
],
5555
)
56+
57+
# TODO: remove these alias when we no longer needs bind in WORKSPACE.bzlmod
58+
# Because @androidsdk is not defined in WORKSPACE.bzl, where the only valid place
59+
# we can call native function bind. Using these alias to forward the binding.
60+
alias(
61+
name = "androidsdk_sdk",
62+
actual = "@androidsdk//:sdk",
63+
)
64+
65+
alias(
66+
name = "androidsdk_d8_jar_import",
67+
actual = "@androidsdk//:d8_jar_import",
68+
)
69+
70+
alias(
71+
name = "androidsdk_dx_jar_import",
72+
actual = "@androidsdk//:dx_jar_import",
73+
)
74+
75+
alias(
76+
name = "androidsdk_files",
77+
actual = "@androidsdk//:files",
78+
)
79+
80+
alias(
81+
name = "androidsdk_has_android_sdk",
82+
actual = "@androidsdk//:has_android_sdk",
83+
)

MODULE.bazel

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,23 @@ git_override(
9898
module_name = "rules_robolectric",
9999
remote = "https://github.com/robolectric/robolectric-bazel",
100100
commit = "a5b25a8c27cc6add74bb01e62cd0dc72df8933ff",
101-
)
101+
)
102+
103+
android_sdk_repository_extension = use_extension("//rules/android_sdk_repository:rule.bzl", "android_sdk_repository_extension")
104+
use_repo(android_sdk_repository_extension, "androidsdk")
105+
106+
android_sdk_proxy_extensions = use_extension("@bazel_tools//tools/android:android_extensions.bzl", "android_sdk_proxy_extensions")
107+
android_sdk_proxy_extensions.configure(
108+
has_androidsdk = "@androidsdk//:has_androidsdk",
109+
dx_jar_import = "@androidsdk//:dx_jar_import",
110+
android_sdk_for_testing = "@androidsdk//:files",
111+
)
112+
use_repo(android_sdk_proxy_extensions, "android_external")
113+
114+
register_toolchains("@androidsdk//:sdk-toolchain")
115+
register_toolchains("@androidsdk//:all")
116+
117+
register_toolchains(
118+
"@androidsdk//:sdk-toolchain",
119+
"@androidsdk//:all",
120+
)

WORKSPACE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ load("defs_dev.bzl", "rules_android_workspace")
2222

2323
rules_android_workspace()
2424

25+
load("@bazel_tools//tools/android:android_extensions.bzl", "android_external_repository")
26+
android_external_repository(
27+
name = "android_external",
28+
has_androidsdk = "@androidsdk//:has_androidsdk",
29+
dx_jar_import = "@androidsdk//:dx_jar_import",
30+
android_sdk_for_testing = "@androidsdk//:files",
31+
)
32+
2533
register_toolchains("//toolchains/android:all")
2634

2735
register_toolchains("//toolchains/android_sdk:all")

WORKSPACE.bzlmod

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ workspace(name = "rules_android")
22

33
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
44
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
5-
load("//rules:rules.bzl", "android_sdk_repository")
6-
7-
maybe(
8-
android_sdk_repository,
9-
name = "androidsdk",
10-
)
115

126
maybe(
137
android_ndk_repository,
148
name = "androidndk",
159
)
10+
11+
12+
# Temporary until we break all //external:android dependency
13+
bind(name = "android/sdk", actual = "//:androidsdk_sdk")
14+
bind(name = "android/dx_jar_import", actual = "//:androidsdk_dx_jar_import")
15+
bind(name = "android_sdk_for_testing", actual = "//:androidsdk_files")
16+
bind(name = "has_android_sdk", actual = "//:androidsdk_has_android_sdk")

examples/basicapp/BUILD

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
1-
# Empty build file to satisfy gazelle for rules_go.
1+
# Empty build file to satisfy gazelle for rules_go.
2+
3+
# TODO: remove these alias when ../..//BUILD remove them
4+
alias(
5+
name = "androidsdk_sdk",
6+
actual = "@androidsdk//:sdk",
7+
)
8+
9+
alias(
10+
name = "androidsdk_d8_jar_import",
11+
actual = "@androidsdk//:d8_jar_import",
12+
)
13+
14+
alias(
15+
name = "androidsdk_dx_jar_import",
16+
actual = "@androidsdk//:dx_jar_import",
17+
)
18+
19+
alias(
20+
name = "androidsdk_files",
21+
actual = "@androidsdk//:files",
22+
)
23+
24+
alias(
25+
name = "androidsdk_has_android_sdk",
26+
actual = "@androidsdk//:has_android_sdk",
27+
)

examples/basicapp/MODULE.bazel

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,35 @@ bazel_dep(name = "rules_jvm_external", version = "4.5")
77
bazel_dep(name = "bazel_skylib", version = "1.3.0")
88
remote_android_extensions = use_extension("@bazel_tools//tools/android:android_extensions.bzl", "remote_android_tools_extensions")
99
use_repo(remote_android_extensions, "android_gmaven_r8", "android_tools")
10+
11+
android_sdk_proxy_extensions = use_extension("@bazel_tools//tools/android:android_extensions.bzl", "android_sdk_proxy_extensions")
12+
android_sdk_proxy_extensions.configure(
13+
has_androidsdk = "@androidsdk//:has_androidsdk",
14+
dx_jar_import = "@androidsdk//:dx_jar_import",
15+
android_sdk_for_testing = "@androidsdk//:files",
16+
)
17+
use_repo(android_sdk_proxy_extensions, "android_external")
18+
19+
20+
bazel_dep(
21+
name = "rules_android",
22+
version = "0.1.1",
23+
)
24+
25+
local_path_override(
26+
module_name = "rules_android",
27+
path = "../../",
28+
)
29+
30+
register_toolchains(
31+
"@rules_android//toolchains/android:android_default_toolchain",
32+
"@rules_android//toolchains/android_sdk:android_sdk_tools",
33+
)
34+
35+
android_sdk_repository_extension = use_extension("@rules_android//rules/android_sdk_repository:rule.bzl", "android_sdk_repository_extension")
36+
use_repo(android_sdk_repository_extension, "androidsdk")
37+
38+
register_toolchains(
39+
"@androidsdk//:sdk-toolchain",
40+
"@androidsdk//:all",
41+
)

examples/basicapp/WORKSPACE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,10 @@ register_toolchains(
4141
"@rules_android//toolchains/android_sdk:android_sdk_tools",
4242
)
4343

44+
load("@bazel_tools//tools/android:android_extensions.bzl", "android_external_repository")
45+
android_external_repository(
46+
name = "android_external",
47+
has_androidsdk = "@androidsdk//:has_androidsdk",
48+
dx_jar_import = "@androidsdk//:dx_jar_import",
49+
android_sdk_for_testing = "@androidsdk//:files",
50+
)

examples/basicapp/WORKSPACE.bzlmod

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
local_repository(
2-
name = "rules_android",
3-
path = "../..", # rules_android's WORKSPACE relative to this inner workspace
4-
)
1+
workspace(name = "basicapp")
52

6-
load("@rules_android//:prereqs.bzl", "rules_android_prereqs")
7-
rules_android_prereqs()
8-
load("@rules_android//:defs.bzl", "rules_android_workspace")
9-
rules_android_workspace()
10-
11-
load("@rules_android//rules:rules.bzl", "android_sdk_repository")
12-
android_sdk_repository(
13-
name = "androidsdk",
14-
)
15-
16-
register_toolchains(
17-
"@rules_android//toolchains/android:android_default_toolchain",
18-
"@rules_android//toolchains/android_sdk:android_sdk_tools",
19-
)
3+
# local_repository(
4+
# name = "rules_android",
5+
# path = "../../"
6+
# )
207

8+
# load("@rules_android//rules/android_sdk_repository:rule.bzl", "android_sdk_repository")
9+
# android_sdk_repository(name = "androidsdk")
2110

11+
# Temporary until we break all //external:android dependency
12+
bind(name = "android/sdk", actual = "@//:androidsdk_sdk")
13+
bind(name = "android/dx_jar_import", actual = "@//:androidsdk_dx_jar_import")
14+
bind(name = "android_sdk_for_testing", actual = "//:androidsdk_files")
15+
bind(name = "has_android_sdk", actual = "//:androidsdk_has_android_sdk")

rules/android_sdk_repository/rule.bzl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,35 @@ def android_sdk_repository(
209209

210210
native.register_toolchains("@%s//:sdk-toolchain" % name)
211211
native.register_toolchains("@%s//:all" % name)
212+
213+
def _android_sdk_repository_extension_impl(module_ctx):
214+
root_modules = [m for m in module_ctx.modules if m.is_root and m.tags.configure]
215+
if len(root_modules) > 1:
216+
fail("Expected at most one root module, found {}".format(", ".join([x.name for x in root_modules])))
217+
218+
if root_modules:
219+
module = root_modules[0]
220+
else:
221+
module = module_ctx.modules[0]
222+
223+
kwargs = {}
224+
if module.tags.configure:
225+
kwargs["api_level"] = module.tags.configure[0].api_level
226+
kwargs["build_tools_version"] = module.tags.configure[0].build_tools_version
227+
kwargs["path"] = module.tags.configure[0].path
228+
229+
_android_sdk_repository(
230+
name = "androidsdk",
231+
**kwargs
232+
)
233+
234+
android_sdk_repository_extension = module_extension(
235+
implementation = _android_sdk_repository_extension_impl,
236+
tag_classes = {
237+
"configure": tag_class(attrs = {
238+
"path": attr.string(),
239+
"api_level": attr.int(),
240+
"build_tools_version": attr.string(),
241+
}),
242+
},
243+
)

0 commit comments

Comments
 (0)