1# Copyright (C) 2022 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 15load( 16 "//build/bazel/rules/sysprop:sysprop_library.bzl", 17 "SyspropGenInfo", 18) 19load( 20 ":cc_library_common.bzl", 21 "create_ccinfo_for_includes", 22) 23load(":cc_library_shared.bzl", "cc_library_shared") 24load(":cc_library_static.bzl", "cc_library_static") 25 26# TODO(b/240466571): Implement determination of exported includes 27def _cc_gen_sysprop_impl(ctx): 28 outputs = [] 29 output_headers = [] 30 all_srcs = [] 31 [ 32 all_srcs.extend(src.files.to_list()) 33 for src in ctx.attr.dep[SyspropGenInfo].srcs 34 ] 35 for src_file in all_srcs: 36 output_subpath = src_file.short_path.replace( 37 ctx.label.package + "/", 38 "", 39 1, 40 ) 41 action_outputs = [] 42 args = ctx.actions.args() 43 output_src_file = ctx.actions.declare_file( 44 "sysprop/%s.cpp" % output_subpath, 45 ) 46 action_outputs.append(output_src_file) 47 48 output_header_file = ctx.actions.declare_file( 49 "sysprop/include/%s.h" % output_subpath, 50 ) 51 action_outputs.append(output_header_file) 52 output_headers.append(output_header_file) 53 54 # TODO(b/240466571): This will in some cases be exported with the 55 # linked bug 56 output_public_header_file = ctx.actions.declare_file( 57 "sysprop/public/include/%s.h" % output_subpath, 58 ) 59 action_outputs.append(output_public_header_file) 60 61 args.add("--header-dir", output_header_file.dirname) 62 args.add("--public-header-dir", output_public_header_file.dirname) 63 args.add("--source-dir", output_src_file.dirname) 64 args.add("--include-name", "%s.h" % output_subpath) 65 args.add(src_file.path) 66 ctx.actions.run( 67 executable = ctx.executable._sysprop_cpp, 68 arguments = [args], 69 inputs = [src_file], 70 outputs = action_outputs, 71 mnemonic = "syspropcc", 72 progress_message = "Generating sources from %s" % ( 73 src_file.short_path, 74 ), 75 ) 76 outputs.extend(action_outputs) 77 return [ 78 DefaultInfo(files = depset(outputs)), 79 create_ccinfo_for_includes( 80 ctx = ctx, 81 hdrs = output_headers, 82 # TODO(b/240466571): This will be determined dynamically with the 83 # linked bug 84 includes = ["sysprop/include"], 85 ), 86 ] 87 88# Visible For Testing 89cc_gen_sysprop = rule( 90 implementation = _cc_gen_sysprop_impl, 91 doc = """compilation of sysprop sources into cpp sources and headers""", 92 attrs = { 93 "dep": attr.label( 94 providers = [SyspropGenInfo], 95 mandatory = True, 96 ), 97 "_sysprop_cpp": attr.label( 98 default = "//system/tools/sysprop:sysprop_cpp", 99 executable = True, 100 cfg = "exec", 101 ), 102 }, 103 provides = [CcInfo], 104) 105 106def _cc_gen_sysprop_common( 107 name, 108 dep): 109 sysprop_gen_name = name + "_sysprop_gen" 110 cc_gen_sysprop( 111 name = sysprop_gen_name, 112 dep = dep, 113 tags = ["manual"], 114 ) 115 116 return sysprop_gen_name 117 118sysprop_deps = select({ 119 "//build/bazel_common_rules/platforms/os:android": ["//system/libbase:libbase_headers"], 120 "//conditions:default": [ 121 "//system/libbase:libbase_bp2build_cc_library_static", 122 "//system/logging/liblog:liblog_bp2build_cc_library_static", 123 ], 124}) 125 126sysprop_dynamic_deps = select({ 127 "//build/bazel_common_rules/platforms/os:android": [ 128 "//system/logging/liblog", 129 ], 130 "//conditions:default": [], 131}) 132 133def cc_sysprop_library_shared( 134 name, 135 dep, 136 min_sdk_version = "", 137 **kwargs): 138 sysprop_gen_name = _cc_gen_sysprop_common(name, dep) 139 140 cc_library_shared( 141 name = name, 142 srcs = [":" + sysprop_gen_name], 143 min_sdk_version = min_sdk_version, 144 deps = sysprop_deps + [sysprop_gen_name], 145 dynamic_deps = sysprop_dynamic_deps, 146 **kwargs 147 ) 148 149def cc_sysprop_library_static( 150 name, 151 dep, 152 min_sdk_version = "", 153 **kwargs): 154 sysprop_gen_name = _cc_gen_sysprop_common(name, dep) 155 cc_library_static( 156 name = name, 157 srcs = [":" + sysprop_gen_name], 158 min_sdk_version = min_sdk_version, 159 deps = sysprop_deps + [sysprop_gen_name], 160 dynamic_deps = sysprop_dynamic_deps, 161 **kwargs 162 ) 163