aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYu Liu <yudiliu@google.com>2022-04-18 20:34:21 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2022-04-18 20:34:21 +0000
commitaec5a768b2c7d539c7eacbec29367012a267724a (patch)
tree4fd13312f4fb977032229e304f58120f450c9daf
parent7277462ac9c2db7155d2e9d74d002481a4c0db86 (diff)
parentd845dac6086093c1f163f57cb09de24bb107554d (diff)
downloadbazel-aec5a768b2c7d539c7eacbec29367012a267724a.tar.gz
Merge "Support build number stamping." am: bc077c22ca am: 0e2506f4b0 am: 7d0647e342 am: d845dac608
Original change: https://android-review.googlesource.com/c/platform/build/bazel/+/2037047 Change-Id: Ib60ff98dd1ff38aaf5454c1d2df1d9be210bb5fb Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r--examples/soong_config_variables/Android.bp1
-rw-r--r--linux.bazelrc5
-rw-r--r--rules/apex/cc.bzl13
-rw-r--r--rules/cc/cc_binary.bzl18
-rw-r--r--rules/cc/cc_library_shared.bzl22
-rw-r--r--rules/cc/cc_library_static.bzl5
-rw-r--r--rules/cc/versioned_cc_common.bzl116
-rwxr-xr-xscripts/gen_build_number.sh33
8 files changed, 192 insertions, 21 deletions
diff --git a/examples/soong_config_variables/Android.bp b/examples/soong_config_variables/Android.bp
index 8994f28e..e863e820 100644
--- a/examples/soong_config_variables/Android.bp
+++ b/examples/soong_config_variables/Android.bp
@@ -97,4 +97,5 @@ cc_binary {
srcs: [
"main.cpp",
],
+ use_version_lib: false,
}
diff --git a/linux.bazelrc b/linux.bazelrc
index a7575066..4fb75ab9 100644
--- a/linux.bazelrc
+++ b/linux.bazelrc
@@ -4,4 +4,7 @@ build --host_platform //build/bazel/platforms:linux_x86_64
# Workaround JVM segfault issue as suggested at
# https://github.com/bazelbuild/bazel/issues/3236#issuecomment-310656266
-build --sandbox_tmpfs_path=/tmp/ \ No newline at end of file
+build --sandbox_tmpfs_path=/tmp/
+
+# Create a build number that will be injected later.
+build --workspace_status_command=build/bazel/scripts/gen_build_number.sh \ No newline at end of file
diff --git a/rules/apex/cc.bzl b/rules/apex/cc.bzl
index 38532b18..2ef4bda0 100644
--- a/rules/apex/cc.bzl
+++ b/rules/apex/cc.bzl
@@ -46,6 +46,7 @@ def has_cc_stubs(target, ctx):
apex_name = ctx.attr._apex_name[BuildSettingInfo].value
available_versions = []
+
# Check that the shared library has stubs built for (at least) the
# min_sdk_version of the APEX
for stub_info in target[CcStubLibrariesInfo].infos:
@@ -54,8 +55,8 @@ def has_cc_stubs(target, ctx):
if stub_version <= min_sdk_version:
return True
- fail("cannot find a stub lib version for min_sdk_level %s (%s apex)\navailable versions: %s (%s)"
- % (min_sdk_version, apex_name, available_versions, target.label))
+ fail("cannot find a stub lib version for min_sdk_level %s (%s apex)\navailable versions: %s (%s)" %
+ (min_sdk_version, apex_name, available_versions, target.label))
# Check if this target is specified as a direct dependency of the APEX,
# as opposed to a transitive dependency, as the transitivity impacts
@@ -89,6 +90,7 @@ def _apex_cc_aspect_impl(target, ctx):
# Transitive deps containing shared libraries to be propagated the apex.
transitive_deps = []
+ rules_propagate_src = ["_bssl_hash_injection", "stripped_shared_library", "versioned_shared_library"]
# Exclude the stripped and unstripped so files
if ctx.rule.kind == "_cc_library_shared_proxy":
@@ -101,10 +103,7 @@ def _apex_cc_aspect_impl(target, ctx):
# Propagate along the dynamic_deps edge
for dep in ctx.rule.attr.dynamic_deps:
transitive_deps.append(dep)
- elif ctx.rule.kind == "_bssl_hash_injection" and hasattr(ctx.rule.attr, "src"):
- # Propagate along the src edge
- transitive_deps.append(ctx.rule.attr.src)
- elif ctx.rule.kind == "stripped_shared_library" and hasattr(ctx.rule.attr, "src"):
+ elif ctx.rule.kind in rules_propagate_src and hasattr(ctx.rule.attr, "src"):
# Propagate along the src edge
transitive_deps.append(ctx.rule.attr.src)
@@ -114,7 +113,7 @@ def _apex_cc_aspect_impl(target, ctx):
transitive_shared_libs = depset(
shared_object_files,
transitive = [dep[ApexCcInfo].transitive_shared_libs for dep in transitive_deps],
- )
+ ),
),
]
diff --git a/rules/cc/cc_binary.bzl b/rules/cc/cc_binary.bzl
index 6476a8ff..8ba78590 100644
--- a/rules/cc/cc_binary.bzl
+++ b/rules/cc/cc_binary.bzl
@@ -17,12 +17,14 @@ limitations under the License.
load(
":cc_library_common.bzl",
"add_lists_defaulting_to_none",
+ "parse_sdk_version",
"system_dynamic_deps_defaults",
"system_static_deps_defaults",
- "parse_sdk_version")
+)
load(":cc_library_static.bzl", "cc_library_static")
load(":stl.bzl", "shared_stl_deps", "static_binary_stl_deps")
load(":stripped_cc_common.bzl", "stripped_binary")
+load(":versioned_cc_common.bzl", "versioned_binary")
def cc_binary(
name,
@@ -53,6 +55,7 @@ def cc_binary(
target_compatible_with = [],
sdk_version = "",
min_sdk_version = "",
+ use_version_lib = False,
**kwargs):
"Bazel macro to correspond with the cc_binary Soong module."
@@ -73,7 +76,7 @@ def cc_binary(
if min_sdk_version:
toolchain_features += [
"sdk_version_" + parse_sdk_version(min_sdk_version),
- "-sdk_version_default"
+ "-sdk_version_default",
]
system_dynamic_deps = []
@@ -119,6 +122,7 @@ def cc_binary(
stl = stl,
system_dynamic_deps = system_dynamic_deps,
target_compatible_with = target_compatible_with,
+ use_version_lib = use_version_lib,
)
binary_dynamic_deps = add_lists_defaulting_to_none(
@@ -138,8 +142,16 @@ def cc_binary(
**kwargs
)
+ versioned_name = name + "_versioned"
+ versioned_binary(
+ name = versioned_name,
+ src = unstripped_name,
+ stamp_build_number = use_version_lib,
+ )
+
stripped_binary(
name = name,
- src = unstripped_name,
+ src = versioned_name,
target_compatible_with = target_compatible_with,
)
+
diff --git a/rules/cc/cc_library_shared.bzl b/rules/cc/cc_library_shared.bzl
index 7d2f7224..b4367e50 100644
--- a/rules/cc/cc_library_shared.bzl
+++ b/rules/cc/cc_library_shared.bzl
@@ -18,13 +18,15 @@ load(
":cc_library_common.bzl",
"add_lists_defaulting_to_none",
"disable_crt_link",
+ "parse_sdk_version",
"system_dynamic_deps_defaults",
- "parse_sdk_version")
+)
load(":cc_library_static.bzl", "cc_library_static")
-load(":cc_stub_library.bzl", "cc_stub_gen", "CcStubInfo")
+load(":cc_stub_library.bzl", "CcStubInfo", "cc_stub_gen")
load(":generate_toc.bzl", "shared_library_toc", _CcTocInfo = "CcTocInfo")
load(":stl.bzl", "shared_stl_deps")
load(":stripped_cc_common.bzl", "stripped_shared_library")
+load(":versioned_cc_common.bzl", "versioned_shared_library")
load("@rules_cc//examples:experimental_cc_shared_library.bzl", "cc_shared_library", _CcSharedLibraryInfo = "CcSharedLibraryInfo")
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cpp_toolchain")
@@ -80,10 +82,6 @@ def cc_library_shared(
**kwargs):
"Bazel macro to correspond with the cc_library_shared Soong module."
- if use_version_lib:
- libbuildversionLabel = "//build/soong/cc/libbuildversion:libbuildversion"
- whole_archive_deps = whole_archive_deps + [libbuildversionLabel]
-
shared_root_name = name + "_root"
unstripped_name = name + "_unstripped"
stripped_name = name + "_stripped"
@@ -100,7 +98,7 @@ def cc_library_shared(
if min_sdk_version:
features = features + [
"sdk_version_" + parse_sdk_version(min_sdk_version),
- "-sdk_version_default"
+ "-sdk_version_default",
]
# The static library at the root of the shared library.
@@ -165,6 +163,7 @@ def cc_library_shared(
if len(soname) == 0:
soname = name + ".so"
soname_flag = "-Wl,-soname," + soname
+
cc_shared_library(
name = unstripped_name,
user_link_flags = linkopts + [soname_flag],
@@ -188,9 +187,16 @@ def cc_library_shared(
inject_bssl_hash = inject_bssl_hash,
)
+ versioned_name = name + "_versioned"
+ versioned_shared_library(
+ name = versioned_name,
+ src = hashed_name,
+ stamp_build_number = use_version_lib,
+ )
+
stripped_shared_library(
name = stripped_name,
- src = hashed_name,
+ src = versioned_name,
target_compatible_with = target_compatible_with,
**strip
)
diff --git a/rules/cc/cc_library_static.bzl b/rules/cc/cc_library_static.bzl
index 526db156..f994490c 100644
--- a/rules/cc/cc_library_static.bzl
+++ b/rules/cc/cc_library_static.bzl
@@ -18,8 +18,9 @@ load(
":cc_library_common.bzl",
"create_ccinfo_for_includes",
"is_external_directory",
+ "parse_sdk_version",
"system_dynamic_deps_defaults",
- "parse_sdk_version")
+)
load(":stl.bzl", "static_stl_deps")
load("@bazel_skylib//lib:collections.bzl", "collections")
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cpp_toolchain")
@@ -101,7 +102,7 @@ def cc_library_static(
if min_sdk_version:
toolchain_features += [
"sdk_version_" + parse_sdk_version(min_sdk_version),
- "-sdk_version_default"
+ "-sdk_version_default",
]
if system_dynamic_deps == None:
diff --git a/rules/cc/versioned_cc_common.bzl b/rules/cc/versioned_cc_common.bzl
new file mode 100644
index 00000000..f91d151d
--- /dev/null
+++ b/rules/cc/versioned_cc_common.bzl
@@ -0,0 +1,116 @@
+"""
+Copyright (C) 2022 The Android Open Source Project
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+"""
+
+"""A macro to handle build number stamping."""
+
+load(":stripped_cc_common.bzl", "StrippedCcBinaryInfo")
+load("@rules_cc//examples:experimental_cc_shared_library.bzl", "CcSharedLibraryInfo")
+
+def stamp_build_number(ctx, prefix = "", extension = ""):
+ if len(ctx.files.src) != 1:
+ fail("Expected only one input file for build number stamping")
+
+ out_file = ctx.actions.declare_file(prefix + ctx.attr.name + extension)
+ android_constraint = ctx.attr._android_constraint[platform_common.ConstraintValueInfo]
+
+ # TODO(b/228461735): We need to dist the output for device target.
+ if ctx.target_platform_has_constraint(android_constraint) or not ctx.attr.stamp_build_number:
+ ctx.actions.symlink(
+ output = out_file,
+ target_file = ctx.files.src[0],
+ )
+ return out_file
+
+ ctx.actions.run_shell(
+ inputs = ctx.files.src + [ctx.version_file],
+ outputs = [out_file],
+ command = """
+ build_number=$(cat {file} | grep "BUILD_NUMBER" | cut -f2 -d' ');
+ {build_number_stamper} -i {input} -o {output} -s soong_build_number -v $build_number
+ """.format(
+ file = ctx.version_file.path,
+ input = ctx.files.src[0].path,
+ output = out_file.path,
+ build_number_stamper = ctx.executable._build_number_stamper.path,
+ ),
+ tools = [ctx.executable._build_number_stamper],
+ mnemonic = "StampBuildNumber",
+ )
+
+ return out_file
+
+common_attrs = {
+ "stamp_build_number": attr.bool(
+ default = False,
+ doc = "Whether to stamp the build number",
+ ),
+ "_build_number_stamper": attr.label(
+ cfg = "exec",
+ doc = "The build number stamp tool.",
+ executable = True,
+ default = "//prebuilts/build-tools:linux-x86/bin/symbol_inject",
+ allow_single_file = True,
+ ),
+ "_android_constraint": attr.label(
+ default = Label("//build/bazel/platforms/os:android"),
+ ),
+}
+
+def _versioned_binary_impl(ctx):
+ common_providers = [
+ ctx.attr.src[CcInfo],
+ ctx.attr.src[InstrumentedFilesInfo],
+ ctx.attr.src[DebugPackageInfo],
+ ctx.attr.src[OutputGroupInfo],
+ ]
+
+ out_file = stamp_build_number(ctx)
+
+ return [
+ DefaultInfo(
+ files = depset([out_file]),
+ executable = out_file,
+ ),
+ ] + common_providers
+
+versioned_binary = rule(
+ implementation = _versioned_binary_impl,
+ attrs = dict(
+ common_attrs,
+ src = attr.label(mandatory = True, allow_single_file = True, providers = [CcInfo]),
+ ),
+)
+
+def _versioned_shared_library_impl(ctx):
+ out_file = stamp_build_number(ctx, "lib", ".so")
+
+ return [
+ DefaultInfo(files = depset([out_file])),
+ ctx.attr.src[CcSharedLibraryInfo],
+ ]
+
+versioned_shared_library = rule(
+ implementation = _versioned_shared_library_impl,
+ attrs = dict(
+ common_attrs,
+ src = attr.label(
+ mandatory = True,
+ # TODO(b/217908237): reenable allow_single_file
+ # allow_single_file = True,
+ providers = [CcSharedLibraryInfo],
+ ),
+ ),
+)
diff --git a/scripts/gen_build_number.sh b/scripts/gen_build_number.sh
new file mode 100755
index 00000000..80085acd
--- /dev/null
+++ b/scripts/gen_build_number.sh
@@ -0,0 +1,33 @@
+#!/bin/bash -e
+# Copyright (C) 2022 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Script used as --workspace_status_command.
+# Must execute at the root of workspace.
+# https://docs.bazel.build/versions/main/command-line-reference.html#flag--workspace_status_command
+
+if [[ ! -f "WORKSPACE" ]]; then
+ echo "ERROR: gen_build_number.sh must be executed at the root of Bazel workspace." >&2
+ exit 1
+fi
+
+# TODO(b/228463719): figure out how to get the path properly.
+BUILD_NUMBER_FILE=out/soong/build_number.txt
+if [[ -f ${BUILD_NUMBER_FILE} ]]; then
+ BUILD_NUMBER=$(cat ${BUILD_NUMBER_FILE})
+else
+ BUILD_NUMBER=eng.${USER:0:6}.$(date '+%Y%m%d.%H%M%S')
+fi
+
+echo "BUILD_NUMBER ${BUILD_NUMBER}" \ No newline at end of file