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 15"""A macro to handle build number stamping.""" 16 17def stamp_build_number(ctx, prefix = "", extension = ""): 18 if len(ctx.files.src) != 1: 19 fail("Expected only one input file for build number stamping") 20 21 out_file = ctx.actions.declare_file(prefix + ctx.attr.name + extension) 22 android_constraint = ctx.attr._android_constraint[platform_common.ConstraintValueInfo] 23 24 # TODO(b/228461735): We need to dist the output for device target. 25 if ctx.target_platform_has_constraint(android_constraint) or not ctx.attr.stamp_build_number: 26 ctx.actions.symlink( 27 output = out_file, 28 target_file = ctx.files.src[0], 29 ) 30 return out_file 31 32 ctx.actions.run_shell( 33 inputs = ctx.files.src + [ctx.version_file], 34 outputs = [out_file], 35 command = """ 36 build_number=$(cat {file} | grep "BUILD_NUMBER" | cut -f2 -d' '); 37 {build_number_stamper} -i {input} -o {output} -s soong_build_number -v $build_number 38 """.format( 39 file = ctx.version_file.path, 40 input = ctx.files.src[0].path, 41 output = out_file.path, 42 build_number_stamper = ctx.executable._build_number_stamper.path, 43 ), 44 tools = [ctx.executable._build_number_stamper], 45 mnemonic = "StampBuildNumber", 46 ) 47 48 return out_file 49 50common_attrs = { 51 "stamp_build_number": attr.bool( 52 default = False, 53 doc = "Whether to stamp the build number", 54 ), 55 "_build_number_stamper": attr.label( 56 cfg = "exec", 57 doc = "The build number stamp tool.", 58 executable = True, 59 default = "//prebuilts/build-tools:linux-x86/bin/symbol_inject", 60 allow_single_file = True, 61 ), 62 "_android_constraint": attr.label( 63 default = Label("//build/bazel/platforms/os:android"), 64 ), 65} 66 67def _versioned_binary_impl(ctx): 68 common_providers = [ 69 ctx.attr.src[CcInfo], 70 ctx.attr.src[InstrumentedFilesInfo], 71 ctx.attr.src[DebugPackageInfo], 72 ctx.attr.src[OutputGroupInfo], 73 ] 74 75 out_file = stamp_build_number(ctx) 76 77 return [ 78 DefaultInfo( 79 files = depset([out_file]), 80 executable = out_file, 81 ), 82 ] + common_providers 83 84versioned_binary = rule( 85 implementation = _versioned_binary_impl, 86 attrs = dict( 87 common_attrs, 88 src = attr.label(mandatory = True, allow_single_file = True, providers = [CcInfo]), 89 ), 90) 91 92def _versioned_shared_library_impl(ctx): 93 out_file = stamp_build_number(ctx, "lib", ".so") 94 95 return [ 96 DefaultInfo(files = depset([out_file])), 97 ctx.attr.src[CcSharedLibraryInfo], 98 ctx.attr.src[OutputGroupInfo], 99 ] 100 101versioned_shared_library = rule( 102 implementation = _versioned_shared_library_impl, 103 attrs = dict( 104 common_attrs, 105 src = attr.label( 106 mandatory = True, 107 # TODO(b/217908237): reenable allow_single_file 108 # allow_single_file = True, 109 providers = [CcSharedLibraryInfo], 110 ), 111 ), 112) 113