add a library to report build numbers without causing rebuilds
Allow native modules to specify use_version_lib, which will make
an android::build::GetBuildNumber() function available. For host
builds, the function will return the build number at the time that
the module was linked. For device modules it will return the
value of the ro.build.version.incremental property.
Bug: 71719137
Test: build_version_test
Test: m build_version_test && touch build/make/core/Makefile build/soong/cc/libbuildversion/tests/build_version_test.cpp && m build_version_test shows different build numbers for binary and library tests.
Change-Id: I6f7d40b7574bb8206866c4e39bad9c710c796e32
diff --git a/cc/binary.go b/cc/binary.go
index 630a68d..7794eab 100644
--- a/cc/binary.go
+++ b/cc/binary.go
@@ -323,6 +323,12 @@
flagsToBuilderFlags(flags), afterPrefixSymbols)
}
+ if Bool(binary.baseLinker.Properties.Use_version_lib) && ctx.Host() {
+ versionedOutputFile := outputFile
+ outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
+ binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
+ }
+
linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
linkerDeps = append(linkerDeps, objs.tidyFiles...)
diff --git a/cc/libbuildversion/Android.bp b/cc/libbuildversion/Android.bp
new file mode 100644
index 0000000..fd563e6
--- /dev/null
+++ b/cc/libbuildversion/Android.bp
@@ -0,0 +1,12 @@
+cc_library_static {
+ name: "libbuildversion",
+ host_supported: true,
+ srcs: ["libbuildversion.cpp"],
+ export_include_dirs: ["include"],
+ cflags: ["-fvisibility=hidden"],
+ target: {
+ windows: {
+ enabled: true,
+ },
+ },
+}
diff --git a/cc/libbuildversion/include/build/version.h b/cc/libbuildversion/include/build/version.h
new file mode 100644
index 0000000..0cc6082
--- /dev/null
+++ b/cc/libbuildversion/include/build/version.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#ifndef BUILD_VERSION_H
+#define BUILD_VERSION_H
+
+#include <string>
+
+namespace android {
+namespace build {
+
+std::string GetBuildNumber();
+
+} // namespace build
+} // namespace android
+
+#endif // BUILD_VERSION_H
diff --git a/cc/libbuildversion/libbuildversion.cpp b/cc/libbuildversion/libbuildversion.cpp
new file mode 100644
index 0000000..d80d587
--- /dev/null
+++ b/cc/libbuildversion/libbuildversion.cpp
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#include <build/version.h>
+
+#ifdef __ANDROID__
+#include <sys/system_properties.h>
+#endif
+
+namespace android {
+namespace build {
+
+#ifdef __ANDROID__
+
+std::string GetBuildNumber() {
+ const prop_info* pi = __system_property_find("ro.build.version.incremental");
+ if (pi == nullptr) return "";
+
+ std::string property_value;
+ __system_property_read_callback(pi,
+ [](void* cookie, const char*, const char* value, unsigned) {
+ auto property_value = reinterpret_cast<std::string*>(cookie);
+ *property_value = value;
+ },
+ &property_value);
+
+ return property_value;
+}
+
+#else
+
+extern "C" {
+ char soong_build_number[128] = "SOONG BUILD NUMBER PLACEHOLDER";
+}
+
+std::string GetBuildNumber() {
+ return soong_build_number;
+}
+
+#endif
+} // namespace build
+} // namespace android
diff --git a/cc/libbuildversion/tests/Android.bp b/cc/libbuildversion/tests/Android.bp
new file mode 100644
index 0000000..a18bc6c
--- /dev/null
+++ b/cc/libbuildversion/tests/Android.bp
@@ -0,0 +1,35 @@
+cc_defaults {
+ name: "build_version_test_defaults",
+ use_version_lib: true,
+ host_supported: true,
+ target: {
+ windows: {
+ enabled: true,
+ },
+ },
+}
+
+cc_test {
+ name: "build_version_test",
+ defaults: ["build_version_test_defaults"],
+ srcs: ["build_version_test.cpp"],
+ target: {
+ android: {
+ shared_libs: ["libbuild_version_test"],
+ },
+ not_windows: {
+ shared_libs: ["libbuild_version_test"],
+ },
+ },
+}
+
+cc_library_shared {
+ name: "libbuild_version_test",
+ defaults: ["build_version_test_defaults"],
+ srcs: ["build_version_test_lib.cpp"],
+ target: {
+ windows: {
+ enabled: false,
+ },
+ },
+}
diff --git a/cc/libbuildversion/tests/build_version_test.cpp b/cc/libbuildversion/tests/build_version_test.cpp
new file mode 100644
index 0000000..9a92065
--- /dev/null
+++ b/cc/libbuildversion/tests/build_version_test.cpp
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#include <stdio.h>
+
+#include <gtest/gtest.h>
+
+#include <build/version.h>
+
+#include "build_version_test_lib.h"
+
+TEST(BuildNumber, binary) {
+ printf("binary version: %s\n", android::build::GetBuildNumber().c_str());
+ EXPECT_NE(android::build::GetBuildNumber(), "SOONG BUILD NUMBER PLACEHOLDER");
+}
+
+ // symbol_inject doesn't support dlls
+#ifndef __WIN32__
+TEST(BuildNumber, library) {
+ printf("shared library version: %s\n", LibGetBuildNumber().c_str());
+ EXPECT_NE(LibGetBuildNumber(), "SOONG BUILD NUMBER PLACEHOLDER");
+}
+#endif
diff --git a/cc/libbuildversion/tests/build_version_test_lib.cpp b/cc/libbuildversion/tests/build_version_test_lib.cpp
new file mode 100644
index 0000000..2876a63
--- /dev/null
+++ b/cc/libbuildversion/tests/build_version_test_lib.cpp
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#include <build/version.h>
+
+#include "build_version_test_lib.h"
+
+std::string LibGetBuildNumber() {
+ return android::build::GetBuildNumber();
+}
diff --git a/cc/libbuildversion/tests/build_version_test_lib.h b/cc/libbuildversion/tests/build_version_test_lib.h
new file mode 100644
index 0000000..ca975af
--- /dev/null
+++ b/cc/libbuildversion/tests/build_version_test_lib.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#ifndef BUILD_VERSION_TEST_LIB_H_
+#define BUILD_VERSION_TEST_LIB_H_
+
+#include <string>
+
+std::string __attribute__((visibility("default"))) LibGetBuildNumber();
+
+#endif // BUILD_VERSION_TEST_LIB_H_
diff --git a/cc/library.go b/cc/library.go
index d3717dd..6fe9ca4 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -492,10 +492,16 @@
library.objects = deps.WholeStaticLibObjs.Copy()
library.objects = library.objects.Append(objs)
- outputFile := android.PathForModuleOut(ctx,
- ctx.ModuleName()+library.MutatedProperties.VariantName+staticLibraryExtension)
+ fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension
+ outputFile := android.PathForModuleOut(ctx, fileName)
builderFlags := flagsToBuilderFlags(flags)
+ if Bool(library.baseLinker.Properties.Use_version_lib) && ctx.Host() {
+ versionedOutputFile := outputFile
+ outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
+ library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
+ }
+
TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
@@ -585,6 +591,12 @@
library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
}
+ if Bool(library.baseLinker.Properties.Use_version_lib) && ctx.Host() {
+ versionedOutputFile := outputFile
+ outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
+ library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
+ }
+
sharedLibs := deps.SharedLibs
sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
diff --git a/cc/linker.go b/cc/linker.go
index fae5542..bcedc8d 100644
--- a/cc/linker.go
+++ b/cc/linker.go
@@ -18,6 +18,7 @@
"android/soong/android"
"fmt"
+ "github.com/google/blueprint"
"github.com/google/blueprint/proptools"
)
@@ -95,6 +96,9 @@
Exclude_static_libs []string
}
}
+
+ // make android::build:GetBuildNumber() available containing the build ID.
+ Use_version_lib *bool `android:"arch_variant"`
}
func NewBaseLinker() *baseLinker {
@@ -136,6 +140,10 @@
deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
deps.ReexportGeneratedHeaders = append(deps.ReexportGeneratedHeaders, linker.Properties.Export_generated_headers...)
+ if Bool(linker.Properties.Use_version_lib) {
+ deps.WholeStaticLibs = append(deps.WholeStaticLibs, "libbuildversion")
+ }
+
if ctx.useVndk() {
deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Vendor.Exclude_shared_libs)
deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Vendor.Exclude_shared_libs)
@@ -278,3 +286,31 @@
flags Flags, deps PathDeps, objs Objects) android.Path {
panic(fmt.Errorf("baseLinker doesn't know how to link"))
}
+
+// Injecting version symbols
+// Some host modules want a version number, but we don't want to rebuild it every time. Optionally add a step
+// after linking that injects a constant placeholder with the current version number.
+
+func init() {
+ pctx.HostBinToolVariable("symbolInjectCmd", "symbol_inject")
+}
+
+var injectVersionSymbol = pctx.AndroidStaticRule("injectVersionSymbol",
+ blueprint.RuleParams{
+ Command: "$symbolInjectCmd -i $in -o $out -s soong_build_number " +
+ "-from 'SOONG BUILD NUMBER PLACEHOLDER' -v $buildNumberFromFile",
+ CommandDeps: []string{"$symbolInjectCmd"},
+ },
+ "buildNumberFromFile")
+
+func (linker *baseLinker) injectVersionSymbol(ctx ModuleContext, in android.Path, out android.WritablePath) {
+ ctx.Build(pctx, android.BuildParams{
+ Rule: injectVersionSymbol,
+ Description: "inject version symbol",
+ Input: in,
+ Output: out,
+ Args: map[string]string{
+ "buildNumberFromFile": ctx.Config().BuildNumberFromFile(),
+ },
+ })
+}
diff --git a/cmd/symbol_inject/Android.bp b/cmd/symbol_inject/Android.bp
index 2d3c69b..a2ea12b 100644
--- a/cmd/symbol_inject/Android.bp
+++ b/cmd/symbol_inject/Android.bp
@@ -27,5 +27,6 @@
"macho_test.go",
"pe_symboldata_test.go",
"pe_test.go",
+ "symbol_inject_test.go",
],
}
diff --git a/cmd/symbol_inject/symbol_inject_test.go b/cmd/symbol_inject/symbol_inject_test.go
new file mode 100644
index 0000000..dbee39a
--- /dev/null
+++ b/cmd/symbol_inject/symbol_inject_test.go
@@ -0,0 +1,67 @@
+// Copyright 2018 Google Inc. All rights reserved.
+//
+// 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.
+
+package main
+
+import (
+ "bytes"
+ "strconv"
+ "testing"
+)
+
+func TestCopyAndInject(t *testing.T) {
+ s := "abcdefghijklmnopqrstuvwxyz"
+ testCases := []struct {
+ offset, size uint64
+ value string
+ expected string
+ }{
+ {
+ offset: 0,
+ size: 1,
+ value: "A",
+ expected: "Abcdefghijklmnopqrstuvwxyz",
+ },
+ {
+ offset: 1,
+ size: 1,
+ value: "B",
+ expected: "aBcdefghijklmnopqrstuvwxyz",
+ },
+ {
+ offset: 1,
+ size: 1,
+ value: "BCD",
+ expected: "aBcdefghijklmnopqrstuvwxyz",
+ },
+ {
+ offset: 25,
+ size: 1,
+ value: "Z",
+ expected: "abcdefghijklmnopqrstuvwxyZ",
+ },
+ }
+
+ for i, testCase := range testCases {
+ t.Run(strconv.Itoa(i), func(t *testing.T) {
+ in := bytes.NewReader([]byte(s))
+ out := &bytes.Buffer{}
+ copyAndInject(in, out, testCase.offset, testCase.size, testCase.value)
+
+ if out.String() != testCase.expected {
+ t.Errorf("expected %s, got %s", testCase.expected, out.String())
+ }
+ })
+ }
+}