platform_build_soong/cc/library_stub_test.go
Spandan Das a3c8a175ca Assemble API surface headers before compiling rdeps
The current combined multi-tree build graph is not accurate. A vendor.c
file currently does not have a dependency on the assembled .h file in
multi-tree out. Previous successful builds of
`multitree_build vendor/vendorimage` were a happy
coincidence (presumably because the actions which copy the .h files from
a speciific inner tree to out/ are at the bottom of the build graph).

Fix this by exporting `cc_api_library.src` to be an order only
dependency of its rdeps (both compile and link). Making an .so file an
order only dependency of _compilation_ is probably confusing, but I
think it will be less confusing than the other alternative I could think
of (creating .timestamp files for each api_surface include dir and
creating phony targets to make them available from top-level to chdir'd
inner tree)

Test: go test ./cc
Test: orchestrator/prebuilts/build-tools/linux-x86/bin/nsjail --config
out/nsjail.cfg --
orchestrator/prebuilts/build-tools/linux-x86/bin/ninja -f
out/multitree.ninja -t path
vendor/out/soong/.intermediates/external/angle/angle_common/android_arm64_armv8-a_cortex-a53_static/obj/external/angle/src/common/android_util.o
out/api_surfaces/vendorapi/current/libc/libc_headers.contribution.androidos_include/features.h

(Path did not exist before this CL)

Change-Id: I74391100f2108993448004019eeba6456af0c12a
2022-10-26 21:10:44 +00:00

286 lines
8.4 KiB
Go

// Copyright 2021 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 cc
import (
_ "fmt"
_ "sort"
"testing"
"android/soong/android"
"github.com/google/blueprint"
)
func hasDirectDependency(t *testing.T, ctx *android.TestResult, from android.Module, to android.Module) bool {
t.Helper()
var found bool
ctx.VisitDirectDeps(from, func(dep blueprint.Module) {
if dep == to {
found = true
}
})
return found
}
func TestApiLibraryReplacesExistingModule(t *testing.T) {
bp := `
cc_library {
name: "libfoo",
shared_libs: ["libbar"],
}
cc_library {
name: "libbar",
}
cc_api_library {
name: "libbar",
src: "libbar.so",
}
api_imports {
name: "api_imports",
shared_libs: [
"libbar",
],
header_libs: [],
}
`
ctx := prepareForCcTest.RunTestWithBp(t, bp)
libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
libbar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
libbarApiImport := ctx.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
android.AssertBoolEquals(t, "original library should not be linked", false, hasDirectDependency(t, ctx, libfoo, libbar))
android.AssertBoolEquals(t, "Stub library from API surface should be linked", true, hasDirectDependency(t, ctx, libfoo, libbarApiImport))
}
func TestApiLibraryDoNotRequireOriginalModule(t *testing.T) {
bp := `
cc_library {
name: "libfoo",
shared_libs: ["libbar"],
}
cc_api_library {
name: "libbar",
src: "libbar.so",
}
api_imports {
name: "api_imports",
shared_libs: [
"libbar",
],
header_libs: [],
}
`
ctx := prepareForCcTest.RunTestWithBp(t, bp)
libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
libbarApiImport := ctx.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
android.AssertBoolEquals(t, "Stub library from API surface should be linked", true, hasDirectDependency(t, ctx, libfoo, libbarApiImport))
}
func TestApiLibraryShouldNotReplaceWithoutApiImport(t *testing.T) {
bp := `
cc_library {
name: "libfoo",
shared_libs: ["libbar"],
}
cc_library {
name: "libbar",
}
cc_api_library {
name: "libbar",
src: "libbar.so",
}
api_imports {
name: "api_imports",
shared_libs: [],
header_libs: [],
}
`
ctx := prepareForCcTest.RunTestWithBp(t, bp)
libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
libbar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
libbarApiImport := ctx.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
android.AssertBoolEquals(t, "original library should be linked", true, hasDirectDependency(t, ctx, libfoo, libbar))
android.AssertBoolEquals(t, "Stub library from API surface should not be linked", false, hasDirectDependency(t, ctx, libfoo, libbarApiImport))
}
func TestApiHeaderReplacesExistingModule(t *testing.T) {
bp := `
cc_library {
name: "libfoo",
header_libs: ["libfoo_headers"],
}
cc_api_library {
name: "libfoo",
header_libs: ["libfoo_headers"],
src: "libfoo.so",
}
cc_library_headers {
name: "libfoo_headers",
}
cc_api_headers {
name: "libfoo_headers",
}
api_imports {
name: "api_imports",
shared_libs: [
"libfoo",
],
header_libs: [
"libfoo_headers",
],
}
`
ctx := prepareForCcTest.RunTestWithBp(t, bp)
libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
libfooApiImport := ctx.ModuleForTests("libfoo.apiimport", "android_arm64_armv8-a_shared").Module()
libfooHeader := ctx.ModuleForTests("libfoo_headers", "android_arm64_armv8-a").Module()
libfooHeaderApiImport := ctx.ModuleForTests("libfoo_headers.apiimport", "android_arm64_armv8-a").Module()
android.AssertBoolEquals(t, "original header should not be used for original library", false, hasDirectDependency(t, ctx, libfoo, libfooHeader))
android.AssertBoolEquals(t, "Header from API surface should be used for original library", true, hasDirectDependency(t, ctx, libfoo, libfooHeaderApiImport))
android.AssertBoolEquals(t, "original header should not be used for library imported from API surface", false, hasDirectDependency(t, ctx, libfooApiImport, libfooHeader))
android.AssertBoolEquals(t, "Header from API surface should be used for library imported from API surface", true, hasDirectDependency(t, ctx, libfooApiImport, libfooHeaderApiImport))
}
func TestApiHeadersDoNotRequireOriginalModule(t *testing.T) {
bp := `
cc_library {
name: "libfoo",
header_libs: ["libfoo_headers"],
}
cc_api_headers {
name: "libfoo_headers",
}
api_imports {
name: "api_imports",
shared_libs: [
"libfoo",
],
header_libs: [
"libfoo_headers",
],
}
`
ctx := prepareForCcTest.RunTestWithBp(t, bp)
libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
libfooHeaderApiImport := ctx.ModuleForTests("libfoo_headers.apiimport", "android_arm64_armv8-a").Module()
android.AssertBoolEquals(t, "Header from API surface should be used for original library", true, hasDirectDependency(t, ctx, libfoo, libfooHeaderApiImport))
}
func TestApiHeadersShouldNotReplaceWithoutApiImport(t *testing.T) {
bp := `
cc_library {
name: "libfoo",
header_libs: ["libfoo_headers"],
}
cc_library_headers {
name: "libfoo_headers",
}
cc_api_headers {
name: "libfoo_headers",
}
api_imports {
name: "api_imports",
shared_libs: [
"libfoo",
],
header_libs: [],
}
`
ctx := prepareForCcTest.RunTestWithBp(t, bp)
libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
libfooHeader := ctx.ModuleForTests("libfoo_headers", "android_arm64_armv8-a").Module()
libfooHeaderApiImport := ctx.ModuleForTests("libfoo_headers.apiimport", "android_arm64_armv8-a").Module()
android.AssertBoolEquals(t, "original header should be used for original library", true, hasDirectDependency(t, ctx, libfoo, libfooHeader))
android.AssertBoolEquals(t, "Header from API surface should not be used for original library", false, hasDirectDependency(t, ctx, libfoo, libfooHeaderApiImport))
}
func TestExportDirFromStubLibrary(t *testing.T) {
bp := `
cc_library {
name: "libfoo",
export_include_dirs: ["source_include_dir"],
export_system_include_dirs: ["source_system_include_dir"],
vendor_available: true,
}
cc_api_library {
name: "libfoo",
export_include_dirs: ["stub_include_dir"],
export_system_include_dirs: ["stub_system_include_dir"],
vendor_available: true,
src: "libfoo.so",
}
api_imports {
name: "api_imports",
shared_libs: [
"libfoo",
],
header_libs: [],
}
// vendor binary
cc_binary {
name: "vendorbin",
vendor: true,
srcs: ["vendor.cc"],
shared_libs: ["libfoo"],
}
`
ctx := prepareForCcTest.RunTestWithBp(t, bp)
vendorCFlags := ctx.ModuleForTests("vendorbin", "android_vendor.29_arm64_armv8-a").Rule("cc").Args["cFlags"]
android.AssertStringDoesContain(t, "Vendor binary should compile using headers provided by stub", vendorCFlags, "-Istub_include_dir")
android.AssertStringDoesNotContain(t, "Vendor binary should not compile using headers of source", vendorCFlags, "-Isource_include_dir")
android.AssertStringDoesContain(t, "Vendor binary should compile using system headers provided by stub", vendorCFlags, "-isystem stub_system_include_dir")
android.AssertStringDoesNotContain(t, "Vendor binary should not compile using system headers of source", vendorCFlags, "-isystem source_system_include_dir")
vendorImplicits := ctx.ModuleForTests("vendorbin", "android_vendor.29_arm64_armv8-a").Rule("cc").OrderOnly.Strings()
// Building the stub.so file first assembles its .h files in multi-tree out.
// These header files are required for compiling the other API domain (vendor in this case)
android.AssertStringListContains(t, "Vendor binary compilation should have an implicit dep on the stub .so file", vendorImplicits, "libfoo.so")
}