2016-10-07 01:12:58 +02:00
// Copyright 2016 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 android
import (
2019-05-29 23:40:35 +02:00
"fmt"
2016-10-07 01:12:58 +02:00
"testing"
"github.com/google/blueprint"
)
2021-07-20 18:47:41 +02:00
func TestPrebuilts ( t * testing . T ) {
buildOS := TestArchConfig ( t . TempDir ( ) , nil , "" , nil ) . BuildOS
var prebuiltsTests = [ ] struct {
name string
replaceBp bool // modules is added to default bp boilerplate if false.
modules string
prebuilt [ ] OsType
preparer FixturePreparer
} {
{
name : "no prebuilt" ,
modules : `
source {
name : "bar" ,
} ` ,
prebuilt : nil ,
} ,
{
name : "no source prebuilt not preferred" ,
modules : `
prebuilt {
name : "bar" ,
prefer : false ,
srcs : [ "prebuilt_file" ] ,
} ` ,
prebuilt : [ ] OsType { Android , buildOS } ,
} ,
{
name : "no source prebuilt preferred" ,
modules : `
prebuilt {
name : "bar" ,
prefer : true ,
srcs : [ "prebuilt_file" ] ,
} ` ,
prebuilt : [ ] OsType { Android , buildOS } ,
} ,
{
name : "prebuilt not preferred" ,
modules : `
source {
name : "bar" ,
}
2020-03-05 18:34:13 +01:00
2021-07-20 18:47:41 +02:00
prebuilt {
name : "bar" ,
prefer : false ,
srcs : [ "prebuilt_file" ] ,
} ` ,
prebuilt : nil ,
} ,
{
name : "prebuilt preferred" ,
modules : `
source {
name : "bar" ,
}
2020-03-05 18:34:13 +01:00
2021-07-20 18:47:41 +02:00
prebuilt {
name : "bar" ,
prefer : true ,
srcs : [ "prebuilt_file" ] ,
} ` ,
prebuilt : [ ] OsType { Android , buildOS } ,
} ,
{
name : "prebuilt no file not preferred" ,
modules : `
source {
name : "bar" ,
}
2020-03-05 18:34:13 +01:00
2021-07-20 18:47:41 +02:00
prebuilt {
name : "bar" ,
prefer : false ,
} ` ,
prebuilt : nil ,
} ,
{
name : "prebuilt no file preferred" ,
modules : `
source {
name : "bar" ,
}
2020-03-05 18:34:13 +01:00
2021-07-20 18:47:41 +02:00
prebuilt {
name : "bar" ,
prefer : true ,
} ` ,
prebuilt : nil ,
} ,
{
name : "prebuilt file from filegroup preferred" ,
modules : `
filegroup {
name : "fg" ,
srcs : [ "prebuilt_file" ] ,
}
prebuilt {
name : "bar" ,
prefer : true ,
srcs : [ ":fg" ] ,
} ` ,
prebuilt : [ ] OsType { Android , buildOS } ,
} ,
{
name : "prebuilt module for device only" ,
modules : `
source {
name : "bar" ,
}
2020-03-05 18:34:13 +01:00
2021-07-20 18:47:41 +02:00
prebuilt {
name : "bar" ,
host_supported : false ,
prefer : true ,
srcs : [ "prebuilt_file" ] ,
} ` ,
prebuilt : [ ] OsType { Android } ,
} ,
{
name : "prebuilt file for host only" ,
modules : `
source {
name : "bar" ,
}
2020-03-05 18:34:13 +01:00
2021-07-20 18:47:41 +02:00
prebuilt {
name : "bar" ,
prefer : true ,
target : {
host : {
srcs : [ "prebuilt_file" ] ,
} ,
2020-03-05 18:34:13 +01:00
} ,
2021-07-20 18:47:41 +02:00
} ` ,
prebuilt : [ ] OsType { buildOS } ,
} ,
{
name : "prebuilt override not preferred" ,
modules : `
source {
name : "baz" ,
}
2020-06-11 19:37:51 +02:00
2021-07-20 18:47:41 +02:00
override_source {
name : "bar" ,
base : "baz" ,
}
2020-06-11 19:37:51 +02:00
2021-07-20 18:47:41 +02:00
prebuilt {
name : "bar" ,
prefer : false ,
srcs : [ "prebuilt_file" ] ,
} ` ,
prebuilt : nil ,
} ,
{
name : "prebuilt override preferred" ,
modules : `
source {
name : "baz" ,
}
2020-06-11 19:37:51 +02:00
2021-07-20 18:47:41 +02:00
override_source {
name : "bar" ,
base : "baz" ,
}
2020-06-11 19:37:51 +02:00
2021-07-20 18:47:41 +02:00
prebuilt {
name : "bar" ,
prefer : true ,
srcs : [ "prebuilt_file" ] ,
} ` ,
prebuilt : [ ] OsType { Android , buildOS } ,
} ,
{
name : "prebuilt including default-disabled OS" ,
replaceBp : true ,
modules : `
source {
name : "foo" ,
deps : [ ":bar" ] ,
target : {
windows : {
enabled : true ,
} ,
2020-07-07 03:20:40 +02:00
} ,
2021-07-20 18:47:41 +02:00
}
2020-07-07 03:20:40 +02:00
2021-07-20 18:47:41 +02:00
source {
name : "bar" ,
target : {
windows : {
enabled : true ,
} ,
2020-07-07 03:20:40 +02:00
} ,
2021-07-20 18:47:41 +02:00
}
2020-07-07 03:20:40 +02:00
2021-07-20 18:47:41 +02:00
prebuilt {
name : "bar" ,
prefer : true ,
srcs : [ "prebuilt_file" ] ,
target : {
windows : {
enabled : true ,
} ,
2020-07-07 03:20:40 +02:00
} ,
2021-07-20 18:47:41 +02:00
} ` ,
prebuilt : [ ] OsType { Android , buildOS , Windows } ,
} ,
{
name : "fall back to source for default-disabled OS" ,
replaceBp : true ,
modules : `
source {
name : "foo" ,
deps : [ ":bar" ] ,
target : {
windows : {
enabled : true ,
} ,
2020-07-07 03:20:40 +02:00
} ,
2021-07-20 18:47:41 +02:00
}
2020-07-07 03:20:40 +02:00
2021-07-20 18:47:41 +02:00
source {
name : "bar" ,
target : {
windows : {
enabled : true ,
} ,
2020-07-07 03:20:40 +02:00
} ,
2021-07-20 18:47:41 +02:00
}
2020-07-07 03:20:40 +02:00
2021-07-20 18:47:41 +02:00
prebuilt {
name : "bar" ,
prefer : true ,
srcs : [ "prebuilt_file" ] ,
} ` ,
prebuilt : [ ] OsType { Android , buildOS } ,
} ,
{
name : "prebuilt properties customizable" ,
replaceBp : true ,
modules : `
source {
name : "foo" ,
deps : [ ":bar" ] ,
}
2021-06-24 19:03:06 +02:00
2021-07-20 18:47:41 +02:00
soong_config_module_type {
name : "prebuilt_with_config" ,
module_type : "prebuilt" ,
config_namespace : "any_namespace" ,
bool_variables : [ "bool_var" ] ,
properties : [ "prefer" ] ,
}
2021-06-24 19:03:06 +02:00
2021-07-20 18:47:41 +02:00
prebuilt_with_config {
name : "bar" ,
prefer : true ,
srcs : [ "prebuilt_file" ] ,
soong_config_variables : {
bool_var : {
prefer : false ,
conditions_default : {
prefer : true ,
} ,
2021-06-24 19:03:06 +02:00
} ,
} ,
2021-07-20 18:47:41 +02:00
} ` ,
prebuilt : [ ] OsType { Android , buildOS } ,
} ,
}
2016-10-07 01:12:58 +02:00
2021-02-24 02:49:52 +01:00
fs := MockFS {
2019-12-14 05:41:13 +01:00
"prebuilt_file" : nil ,
"source_file" : nil ,
}
2016-10-15 00:38:43 +02:00
2016-10-07 01:12:58 +02:00
for _ , test := range prebuiltsTests {
t . Run ( test . name , func ( t * testing . T ) {
2020-07-07 03:20:40 +02:00
bp := test . modules
if ! test . replaceBp {
bp = bp + `
source {
name : "foo" ,
deps : [ ":bar" ] ,
} `
}
2019-12-14 05:41:13 +01:00
2020-07-07 03:20:40 +02:00
// Add windows to the target list to test the logic when a variant is
// disabled by default.
if ! Windows . DefaultDisabled {
t . Errorf ( "windows is assumed to be disabled by default" )
}
2016-10-07 01:12:58 +02:00
2021-03-20 01:36:14 +01:00
result := GroupFixturePreparers (
2021-02-24 02:49:52 +01:00
PrepareForTestWithArchMutator ,
PrepareForTestWithPrebuilts ,
PrepareForTestWithOverrides ,
PrepareForTestWithFilegroup ,
// Add a Windows target to the configuration.
FixtureModifyConfig ( func ( config Config ) {
config . Targets [ Windows ] = [ ] Target {
{ Windows , Arch { ArchType : X86_64 } , NativeBridgeDisabled , "" , "" , true } ,
}
} ) ,
fs . AddToFixture ( ) ,
FixtureRegisterWithContext ( registerTestPrebuiltModules ) ,
2021-07-06 18:15:25 +02:00
OptionalFixturePreparer ( test . preparer ) ,
2021-02-24 02:49:52 +01:00
) . RunTestWithBp ( t , bp )
2016-10-07 01:12:58 +02:00
2021-02-24 02:49:52 +01:00
for _ , variant := range result . ModuleVariantsForTests ( "foo" ) {
foo := result . ModuleForTests ( "foo" , variant )
2020-07-07 03:20:40 +02:00
t . Run ( foo . Module ( ) . Target ( ) . Os . String ( ) , func ( t * testing . T ) {
2020-03-05 18:34:13 +01:00
var dependsOnSourceModule , dependsOnPrebuiltModule bool
2021-02-24 02:49:52 +01:00
result . VisitDirectDeps ( foo . Module ( ) , func ( m blueprint . Module ) {
2020-03-05 18:34:13 +01:00
if _ , ok := m . ( * sourceModule ) ; ok {
dependsOnSourceModule = true
}
if p , ok := m . ( * prebuiltModule ) ; ok {
dependsOnPrebuiltModule = true
if ! p . Prebuilt ( ) . properties . UsePrebuilt {
t . Errorf ( "dependency on prebuilt module not marked used" )
}
}
} )
2016-10-07 01:12:58 +02:00
2020-07-07 03:20:40 +02:00
moduleIsDisabled := ! foo . Module ( ) . Enabled ( )
2020-03-05 18:34:13 +01:00
deps := foo . Module ( ) . ( * sourceModule ) . deps
2020-07-07 03:20:40 +02:00
if moduleIsDisabled {
if len ( deps ) > 0 {
t . Errorf ( "disabled module got deps: %v" , deps )
}
} else {
if len ( deps ) != 1 {
t . Errorf ( "deps does not have single path, but is %v" , deps )
}
2020-03-05 18:34:13 +01:00
}
2020-07-07 03:20:40 +02:00
2020-03-05 18:34:13 +01:00
var usingSourceFile , usingPrebuiltFile bool
2020-07-07 03:20:40 +02:00
if len ( deps ) > 0 && deps [ 0 ] . String ( ) == "source_file" {
2020-03-05 18:34:13 +01:00
usingSourceFile = true
}
2020-07-07 03:20:40 +02:00
if len ( deps ) > 0 && deps [ 0 ] . String ( ) == "prebuilt_file" {
2020-03-05 18:34:13 +01:00
usingPrebuiltFile = true
2017-03-17 21:14:32 +01:00
}
2020-03-05 18:34:13 +01:00
prebuilt := false
for _ , os := range test . prebuilt {
2020-07-07 03:20:40 +02:00
if os == foo . Module ( ) . Target ( ) . Os {
2020-03-05 18:34:13 +01:00
prebuilt = true
}
}
2019-04-19 10:23:04 +02:00
2020-03-05 18:34:13 +01:00
if prebuilt {
2020-07-07 03:20:40 +02:00
if moduleIsDisabled {
t . Errorf ( "dependent module for prebuilt is disabled" )
}
2020-03-05 18:34:13 +01:00
if ! dependsOnPrebuiltModule {
t . Errorf ( "doesn't depend on prebuilt module" )
}
if ! usingPrebuiltFile {
t . Errorf ( "doesn't use prebuilt_file" )
}
2016-10-07 01:12:58 +02:00
2020-03-05 18:34:13 +01:00
if dependsOnSourceModule {
t . Errorf ( "depends on source module" )
}
if usingSourceFile {
t . Errorf ( "using source_file" )
}
2020-07-07 03:20:40 +02:00
} else if ! moduleIsDisabled {
2020-03-05 18:34:13 +01:00
if dependsOnPrebuiltModule {
t . Errorf ( "depends on prebuilt module" )
}
if usingPrebuiltFile {
t . Errorf ( "using prebuilt_file" )
}
2016-10-07 01:12:58 +02:00
2020-03-05 18:34:13 +01:00
if ! dependsOnSourceModule {
t . Errorf ( "doesn't depend on source module" )
}
if ! usingSourceFile {
t . Errorf ( "doesn't use source_file" )
}
}
} )
2016-10-07 01:12:58 +02:00
}
} )
}
}
2023-10-26 22:48:02 +02:00
func testPrebuiltErrorWithFixture ( t * testing . T , expectedError , bp string , fixture FixturePreparer ) {
2023-05-02 04:43:14 +02:00
t . Helper ( )
fs := MockFS {
"prebuilt_file" : nil ,
}
GroupFixturePreparers (
PrepareForTestWithArchMutator ,
PrepareForTestWithPrebuilts ,
PrepareForTestWithOverrides ,
fs . AddToFixture ( ) ,
FixtureRegisterWithContext ( registerTestPrebuiltModules ) ,
2023-10-26 22:48:02 +02:00
OptionalFixturePreparer ( fixture ) ,
2023-05-02 04:43:14 +02:00
) .
ExtendWithErrorHandler ( FixtureExpectsAtLeastOneErrorMatchingPattern ( expectedError ) ) .
RunTestWithBp ( t , bp )
2023-10-26 22:48:02 +02:00
}
func testPrebuiltError ( t * testing . T , expectedError , bp string ) {
testPrebuiltErrorWithFixture ( t , expectedError , bp , nil )
2023-05-02 04:43:14 +02:00
}
func TestPrebuiltShouldNotChangePartition ( t * testing . T ) {
testPrebuiltError ( t , ` partition is different ` , `
source {
name : "foo" ,
vendor : true ,
}
prebuilt {
name : "foo" ,
prefer : true ,
srcs : [ "prebuilt_file" ] ,
} ` )
}
func TestPrebuiltShouldNotChangePartition_WithOverride ( t * testing . T ) {
testPrebuiltError ( t , ` partition is different ` , `
source {
name : "foo" ,
vendor : true ,
}
override_source {
name : "bar" ,
base : "foo" ,
}
prebuilt {
name : "bar" ,
prefer : true ,
srcs : [ "prebuilt_file" ] ,
} ` )
}
2020-01-14 13:42:08 +01:00
func registerTestPrebuiltBuildComponents ( ctx RegistrationContext ) {
2021-02-24 02:49:52 +01:00
registerTestPrebuiltModules ( ctx )
2020-01-14 13:42:08 +01:00
RegisterPrebuiltMutators ( ctx )
2020-06-11 19:37:51 +02:00
ctx . PostDepsMutators ( RegisterOverridePostDepsMutators )
2020-01-14 13:42:08 +01:00
}
2021-02-27 12:59:02 +01:00
var prepareForTestWithFakePrebuiltModules = FixtureRegisterWithContext ( registerTestPrebuiltModules )
2021-02-24 02:49:52 +01:00
func registerTestPrebuiltModules ( ctx RegistrationContext ) {
ctx . RegisterModuleType ( "prebuilt" , newPrebuiltModule )
ctx . RegisterModuleType ( "source" , newSourceModule )
ctx . RegisterModuleType ( "override_source" , newOverrideSourceModule )
2021-11-02 17:43:57 +01:00
ctx . RegisterModuleType ( "soong_config_module_type" , SoongConfigModuleTypeFactory )
ctx . RegisterModuleType ( "soong_config_string_variable" , SoongConfigStringVariableDummyFactory )
ctx . RegisterModuleType ( "soong_config_bool_variable" , SoongConfigBoolVariableDummyFactory )
2023-10-26 22:48:02 +02:00
RegisterApexContributionsBuildComponents ( ctx )
2021-02-24 02:49:52 +01:00
}
2016-10-07 01:12:58 +02:00
type prebuiltModule struct {
ModuleBase
2017-08-02 20:05:49 +02:00
prebuilt Prebuilt
properties struct {
2020-03-05 18:34:13 +01:00
Srcs [ ] string ` android:"path,arch_variant" `
2017-08-02 20:05:49 +02:00
}
2019-04-19 10:23:04 +02:00
src Path
2016-10-07 01:12:58 +02:00
}
2017-06-24 00:06:31 +02:00
func newPrebuiltModule ( ) Module {
2016-10-07 01:12:58 +02:00
m := & prebuiltModule { }
2017-08-02 20:05:49 +02:00
m . AddProperties ( & m . properties )
InitPrebuiltModule ( m , & m . properties . Srcs )
2020-03-05 18:34:13 +01:00
InitAndroidArchModule ( m , HostAndDeviceDefault , MultilibCommon )
2017-06-24 00:06:31 +02:00
return m
2016-10-07 01:12:58 +02:00
}
func ( p * prebuiltModule ) Name ( ) string {
return p . prebuilt . Name ( p . ModuleBase . Name ( ) )
}
2019-04-19 10:23:04 +02:00
func ( p * prebuiltModule ) GenerateAndroidBuildActions ( ctx ModuleContext ) {
if len ( p . properties . Srcs ) >= 1 {
p . src = p . prebuilt . SingleSourcePath ( ctx )
}
2016-10-07 01:12:58 +02:00
}
func ( p * prebuiltModule ) Prebuilt ( ) * Prebuilt {
return & p . prebuilt
}
2019-05-29 23:40:35 +02:00
func ( p * prebuiltModule ) OutputFiles ( tag string ) ( Paths , error ) {
switch tag {
case "" :
return Paths { p . src } , nil
default :
return nil , fmt . Errorf ( "unsupported module reference tag %q" , tag )
}
2019-04-19 10:23:04 +02:00
}
2020-06-11 19:37:51 +02:00
type sourceModuleProperties struct {
Deps [ ] string ` android:"path,arch_variant" `
}
2016-10-07 01:12:58 +02:00
type sourceModule struct {
ModuleBase
2020-06-11 19:37:51 +02:00
OverridableModuleBase
properties sourceModuleProperties
2016-10-07 01:12:58 +02:00
dependsOnSourceModule , dependsOnPrebuiltModule bool
2019-04-19 10:23:04 +02:00
deps Paths
src Path
2016-10-07 01:12:58 +02:00
}
2017-06-24 00:06:31 +02:00
func newSourceModule ( ) Module {
2016-10-07 01:12:58 +02:00
m := & sourceModule { }
2017-06-24 00:06:31 +02:00
m . AddProperties ( & m . properties )
2020-03-05 18:34:13 +01:00
InitAndroidArchModule ( m , HostAndDeviceDefault , MultilibCommon )
2020-06-11 19:37:51 +02:00
InitOverridableModule ( m , nil )
2017-06-24 00:06:31 +02:00
return m
2016-10-07 01:12:58 +02:00
}
2020-06-11 19:37:51 +02:00
func ( s * sourceModule ) OverridablePropertiesDepsMutator ( ctx BottomUpMutatorContext ) {
2019-04-19 10:23:04 +02:00
// s.properties.Deps are annotated with android:path, so they are
// automatically added to the dependency by pathDeps mutator
2016-10-07 01:12:58 +02:00
}
func ( s * sourceModule ) GenerateAndroidBuildActions ( ctx ModuleContext ) {
2019-04-19 10:23:04 +02:00
s . deps = PathsForModuleSrc ( ctx , s . properties . Deps )
s . src = PathForModuleSrc ( ctx , "source_file" )
}
func ( s * sourceModule ) Srcs ( ) Paths {
return Paths { s . src }
2016-10-07 01:12:58 +02:00
}
2020-06-11 19:37:51 +02:00
type overrideSourceModule struct {
ModuleBase
OverrideModuleBase
}
func ( o * overrideSourceModule ) GenerateAndroidBuildActions ( _ ModuleContext ) {
}
func newOverrideSourceModule ( ) Module {
m := & overrideSourceModule { }
m . AddProperties ( & sourceModuleProperties { } )
InitAndroidArchModule ( m , HostAndDeviceDefault , MultilibCommon )
InitOverrideModule ( m )
return m
}
2023-10-26 22:48:02 +02:00
func TestPrebuiltErrorCannotListBothSourceAndPrebuiltInContributions ( t * testing . T ) {
selectMainlineModuleContritbutions := GroupFixturePreparers (
FixtureModifyProductVariables ( func ( variables FixtureProductVariables ) {
variables . BuildFlags = map [ string ] string {
"RELEASE_APEX_CONTRIBUTIONS_ADSERVICES" : "my_apex_contributions" ,
}
} ) ,
)
2024-01-03 19:57:03 +01:00
testPrebuiltErrorWithFixture ( t , ` Found duplicate variations of the same module in apex_contributions: foo and prebuilt_foo. Please remove one of these ` , `
2023-10-26 22:48:02 +02:00
source {
name : "foo" ,
}
prebuilt {
name : "foo" ,
srcs : [ "prebuilt_file" ] ,
}
apex_contributions {
name : "my_apex_contributions" ,
api_domain : "my_mainline_module" ,
contents : [
"foo" ,
"prebuilt_foo" ,
] ,
}
all_apex_contributions {
name : "all_apex_contributions" ,
}
` , selectMainlineModuleContritbutions )
}
2024-02-26 10:39:37 +01:00
// Test that apex_contributions of prebuilt modules are ignored in coverage builds
func TestSourceIsSelectedInCoverageBuilds ( t * testing . T ) {
prebuiltMainlineContributions := GroupFixturePreparers (
FixtureModifyProductVariables ( func ( variables FixtureProductVariables ) {
variables . BuildFlags = map [ string ] string {
"RELEASE_APEX_CONTRIBUTIONS_ADSERVICES" : "my_prebuilt_apex_contributions" ,
}
} ) ,
FixtureMergeEnv ( map [ string ] string {
"EMMA_INSTRUMENT_FRAMEWORK" : "true" ,
} ) ,
)
bp := `
source {
name : "foo" ,
}
prebuilt {
name : "foo" ,
srcs : [ "prebuilt_file" ] ,
}
apex_contributions {
name : "my_prebuilt_apex_contributions" ,
api_domain : "my_mainline_module" ,
contents : [
"prebuilt_foo" ,
] ,
}
all_apex_contributions {
name : "all_apex_contributions" ,
}
`
ctx := GroupFixturePreparers (
PrepareForTestWithArchMutator ,
PrepareForTestWithPrebuilts ,
FixtureRegisterWithContext ( registerTestPrebuiltModules ) ,
prebuiltMainlineContributions ) . RunTestWithBp ( t , bp )
source := ctx . ModuleForTests ( "foo" , "android_common" ) . Module ( )
AssertBoolEquals ( t , "Source should be preferred in coverage builds" , true , ! source . IsHideFromMake ( ) )
prebuilt := ctx . ModuleForTests ( "prebuilt_foo" , "android_common" ) . Module ( )
AssertBoolEquals ( t , "Prebuilt should not be preferred in coverage builds" , false , ! prebuilt . IsHideFromMake ( ) )
}