1# Copyright (C) 2021 The Android Open Source Project 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 15PrebuiltFileInfo = provider( 16 "Info needed for prebuilt_file modules", 17 fields = { 18 "src": "Source file of this prebuilt", 19 "dir": "Directory into which to install", 20 "filename": "Optional name for the installed file", 21 "installable": "Whether this is directly installable into one of the partitions", 22 }, 23) 24_handled_dirs = ["etc", "usr/share"] 25 26def _prebuilt_file_rule_impl(ctx): 27 srcs = ctx.files.src 28 if len(srcs) != 1: 29 fail("src for", ctx.label.name, "is expected to be singular, but is of len", len(srcs), ":\n", srcs) 30 31 # Is this an acceptable directory, or a subdir under one? 32 dir = ctx.attr.dir 33 acceptable = False 34 for d in _handled_dirs: 35 if dir == d or dir.startswith(d + "/"): 36 acceptable = True 37 break 38 if not acceptable: 39 fail("dir for", ctx.label.name, "is `", dir, "`, but we only handle these:\n", _handled_dirs) 40 41 if ctx.attr.filename_from_src and ctx.attr.filename != "": 42 fail("filename is set. filename_from_src cannot be true") 43 elif ctx.attr.filename != "": 44 filename = ctx.attr.filename 45 elif ctx.attr.filename_from_src: 46 filename = srcs[0].basename 47 else: 48 filename = ctx.attr.name 49 50 return [ 51 PrebuiltFileInfo( 52 src = srcs[0], 53 dir = dir, 54 filename = filename, 55 installable = ctx.attr.installable, 56 ), 57 DefaultInfo( 58 files = depset(srcs), 59 ), 60 ] 61 62_prebuilt_file = rule( 63 implementation = _prebuilt_file_rule_impl, 64 attrs = { 65 "src": attr.label( 66 mandatory = True, 67 allow_files = True, 68 # TODO(b/217908237): reenable allow_single_file 69 # allow_single_file = True, 70 ), 71 "dir": attr.string(mandatory = True), 72 "filename": attr.string(), 73 "filename_from_src": attr.bool(default = True), 74 "installable": attr.bool(default = True), 75 }, 76) 77 78def prebuilt_file( 79 name, 80 src, 81 dir, 82 filename = None, 83 installable = True, 84 # TODO(b/207489266): Fully support; 85 # data is currently dropped to prevent breakages from e.g. prebuilt_etc 86 filename_from_src = False, 87 data = [], # @unused 88 **kwargs): 89 "Bazel macro to correspond with the e.g. prebuilt_etc and prebuilt_usr_share Soong modules." 90 91 _prebuilt_file( 92 name = name, 93 src = src, 94 dir = dir, 95 filename = filename, 96 installable = installable, 97 filename_from_src = filename_from_src, 98 **kwargs 99 ) 100