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 = "//build/soong/symbol_inject/cmd:symbol_inject", 60 allow_single_file = True, 61 ), 62 "_android_constraint": attr.label( 63 default = Label("//build/bazel_common_rules/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 runfiles = ctx.attr.src[DefaultInfo].default_runfiles, 82 ), 83 ] + common_providers 84 85versioned_binary = rule( 86 implementation = _versioned_binary_impl, 87 attrs = dict( 88 common_attrs, 89 src = attr.label(mandatory = True, allow_single_file = True, providers = [CcInfo]), 90 ), 91) 92 93def _versioned_shared_library_impl(ctx): 94 out_file = stamp_build_number(ctx, "lib", ".so") 95 96 return [ 97 DefaultInfo(files = depset([out_file])), 98 ctx.attr.src[CcSharedLibraryInfo], 99 ctx.attr.src[OutputGroupInfo], 100 ] 101 102versioned_shared_library = rule( 103 implementation = _versioned_shared_library_impl, 104 attrs = dict( 105 common_attrs, 106 src = attr.label( 107 mandatory = True, 108 providers = [CcSharedLibraryInfo], 109 ), 110 ), 111) 112