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