Merge changes I6f7d40b7,I25654032

* changes:
  add a library to report build numbers without causing rebuilds
  Rewrite symbol_inject to be testable
This commit is contained in:
Treehugger Robot
2018-03-03 03:16:48 +00:00
committed by Gerrit Code Review
22 changed files with 3430 additions and 164 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(),
},
})
}