1# Copyright 2023 The Bazel Authors. All rights reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""A repository rule for integrating the Android SDK.""" 16 17def _parse_version(version): 18 # e.g.: 19 # "33.1.1" -> 330101 20 # "4.0.0" -> 40000 21 # "33.1.1" < "4.0.0" but 330101 > 40000 22 major, minor, micro = version.split(".") 23 return (int(major) * 10000 + int(minor) * 100 + int(micro), version) 24 25def _android_sdk_supplemental_repository_impl(ctx): 26 """A repository for additional SDK content. 27 28 Needed until android_sdk_repository is fully in Starlark. 29 30 Args: 31 ctx: An implementation context. 32 33 Returns: 34 A final dict of configuration attributes and values. 35 """ 36 sdk_path = ctx.attr.path or ctx.os.environ.get("ANDROID_HOME", None) 37 if not sdk_path: 38 fail("Either the ANDROID_HOME environment variable or the " + 39 "path attribute of android_sdk_supplemental_repository " + 40 "must be set.") 41 42 build_tools_dirs = ctx.path(sdk_path + "/build-tools").readdir() 43 _, highest_build_tool_version = ( 44 max([_parse_version(v.basename) for v in build_tools_dirs]) 45 ) 46 ctx.symlink( 47 sdk_path + "/build-tools/" + highest_build_tool_version, 48 "build-tools/" + highest_build_tool_version, 49 ) 50 ctx.file( 51 "BUILD", 52 """ 53filegroup( 54 name = "dexdump", 55 srcs = ["build-tools/%s/dexdump"], 56 visibility = ["//visibility:public"], 57) 58""" % highest_build_tool_version, 59 ) 60 61android_sdk_supplemental_repository = repository_rule( 62 attrs = { 63 "path": attr.string(), 64 }, 65 local = True, 66 implementation = _android_sdk_supplemental_repository_impl, 67) 68