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
This commit is contained in:
Colin Cross 2018-02-15 14:12:26 -08:00
parent 8673b5b959
commit 86803cfe6e
12 changed files with 339 additions and 2 deletions

View File

@ -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...)

View File

@ -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,
},
},
}

View File

@ -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

View File

@ -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

View File

@ -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,
},
},
}

View File

@ -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

View File

@ -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();
}

View File

@ -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_

View File

@ -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...)

View File

@ -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(),
},
})
}

View File

@ -27,5 +27,6 @@ blueprint_go_binary {
"macho_test.go",
"pe_symboldata_test.go",
"pe_test.go",
"symbol_inject_test.go",
],
}

View File

@ -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())
}
})
}
}