aboutsummaryrefslogtreecommitdiffstats
path: root/cc
diff options
context:
space:
mode:
authorColin Cross <ccross@android.com>2018-02-15 14:12:26 -0800
committerColin Cross <ccross@android.com>2018-03-02 16:55:51 -0800
commit86803cfe6e814d3699e842cfc41244cd9a6e0c30 (patch)
treeddb07422dff2e413f4761eaa6dbc22be5ea966e9 /cc
parent8673b5b959def87c0f3f5922b1eea430b1215b62 (diff)
downloadbuild_soong-86803cfe6e814d3699e842cfc41244cd9a6e0c30.tar.gz
build_soong-86803cfe6e814d3699e842cfc41244cd9a6e0c30.tar.bz2
build_soong-86803cfe6e814d3699e842cfc41244cd9a6e0c30.zip
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
Diffstat (limited to 'cc')
-rw-r--r--cc/binary.go6
-rw-r--r--cc/libbuildversion/Android.bp12
-rw-r--r--cc/libbuildversion/include/build/version.h30
-rw-r--r--cc/libbuildversion/libbuildversion.cpp55
-rw-r--r--cc/libbuildversion/tests/Android.bp35
-rw-r--r--cc/libbuildversion/tests/build_version_test.cpp36
-rw-r--r--cc/libbuildversion/tests/build_version_test_lib.cpp23
-rw-r--r--cc/libbuildversion/tests/build_version_test_lib.h24
-rw-r--r--cc/library.go16
-rw-r--r--cc/linker.go36
10 files changed, 271 insertions, 2 deletions
diff --git a/cc/binary.go b/cc/binary.go
index 630a68df..7794eab6 100644
--- a/cc/binary.go
+++ b/cc/binary.go
@@ -323,6 +323,12 @@ func (binary *binaryDecorator) link(ctx ModuleContext,
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 00000000..fd563e6d
--- /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 00000000..0cc60820
--- /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 00000000..d80d587c
--- /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 00000000..a18bc6cc
--- /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 00000000..9a920654
--- /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 00000000..2876a63c
--- /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 00000000..ca975afe
--- /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 d3717dd9..6fe9ca4e 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -492,10 +492,16 @@ func (library *libraryDecorator) linkStatic(ctx ModuleContext,
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 @@ func (library *libraryDecorator) linkShared(ctx ModuleContext,
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 fae55425..bcedc8da 100644
--- a/cc/linker.go
+++ b/cc/linker.go
@@ -18,6 +18,7 @@ import (
"android/soong/android"
"fmt"
+ "github.com/google/blueprint"
"github.com/google/blueprint/proptools"
)
@@ -95,6 +96,9 @@ type BaseLinkerProperties struct {
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 @@ func (linker *baseLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
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 @@ func (linker *baseLinker) link(ctx ModuleContext,
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(),
+ },
+ })
+}