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:
@@ -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...)
|
||||
|
12
cc/libbuildversion/Android.bp
Normal file
12
cc/libbuildversion/Android.bp
Normal 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,
|
||||
},
|
||||
},
|
||||
}
|
30
cc/libbuildversion/include/build/version.h
Normal file
30
cc/libbuildversion/include/build/version.h
Normal 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
|
55
cc/libbuildversion/libbuildversion.cpp
Normal file
55
cc/libbuildversion/libbuildversion.cpp
Normal 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
|
35
cc/libbuildversion/tests/Android.bp
Normal file
35
cc/libbuildversion/tests/Android.bp
Normal 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,
|
||||
},
|
||||
},
|
||||
}
|
36
cc/libbuildversion/tests/build_version_test.cpp
Normal file
36
cc/libbuildversion/tests/build_version_test.cpp
Normal 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
|
23
cc/libbuildversion/tests/build_version_test_lib.cpp
Normal file
23
cc/libbuildversion/tests/build_version_test_lib.cpp
Normal 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();
|
||||
}
|
24
cc/libbuildversion/tests/build_version_test_lib.h
Normal file
24
cc/libbuildversion/tests/build_version_test_lib.h
Normal 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_
|
@@ -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...)
|
||||
|
||||
|
36
cc/linker.go
36
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(),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user