xref: /aosp_15_r20/build/soong/android/test_config.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1// Copyright 2022 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package android
16
17import (
18	"encoding/json"
19	"os"
20	"path/filepath"
21	"runtime"
22
23	"github.com/google/blueprint/proptools"
24)
25
26// TestConfig returns a Config object for testing.
27func TestConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
28	envCopy := make(map[string]string)
29	for k, v := range env {
30		envCopy[k] = v
31	}
32
33	// Copy the real PATH value to the test environment, it's needed by
34	// NonHermeticHostSystemTool() used in x86_darwin_host.go
35	envCopy["PATH"] = os.Getenv("PATH")
36
37	config := &config{
38		productVariables: ProductVariables{
39			DeviceName:                          stringPtr("test_device"),
40			DeviceProduct:                       stringPtr("test_product"),
41			Platform_sdk_version:                intPtr(30),
42			Platform_sdk_version_or_codename:    stringPtr("S"),
43			Platform_sdk_codename:               stringPtr("S"),
44			Platform_base_sdk_extension_version: intPtr(1),
45			Platform_version_active_codenames:   []string{"S", "Tiramisu"},
46			DeviceSystemSdkVersions:             []string{"29", "30", "S"},
47			Platform_systemsdk_versions:         []string{"29", "30", "S", "Tiramisu"},
48			VendorApiLevel:                      stringPtr("202404"),
49			AAPTConfig:                          []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"},
50			AAPTPreferredConfig:                 stringPtr("xhdpi"),
51			AAPTCharacteristics:                 stringPtr("nosdcard"),
52			AAPTPrebuiltDPI:                     []string{"xhdpi", "xxhdpi"},
53			UncompressPrivAppDex:                boolPtr(true),
54			Shipping_api_level:                  stringPtr("30"),
55		},
56
57		outDir:       buildDir,
58		soongOutDir:  filepath.Join(buildDir, "soong"),
59		captureBuild: true,
60		env:          envCopy,
61
62		// Set testAllowNonExistentPaths so that test contexts don't need to specify every path
63		// passed to PathForSource or PathForModuleSrc.
64		TestAllowNonExistentPaths: true,
65
66		BuildMode: AnalysisNoBazel,
67	}
68	config.deviceConfig = &deviceConfig{
69		config: config,
70	}
71	config.TestProductVariables = &config.productVariables
72
73	config.mockFileSystem(bp, fs)
74
75	determineBuildOS(config)
76
77	return Config{config}
78}
79
80func modifyTestConfigToSupportArchMutator(testConfig Config) {
81	config := testConfig.config
82
83	config.Targets = map[OsType][]Target{
84		Android: []Target{
85			{Android, Arch{ArchType: Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridgeDisabled, "", "", false},
86			{Android, Arch{ArchType: Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridgeDisabled, "", "", false},
87		},
88		config.BuildOS: []Target{
89			{config.BuildOS, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
90			{config.BuildOS, Arch{ArchType: X86}, NativeBridgeDisabled, "", "", false},
91		},
92	}
93
94	// Make the CommonOS OsType available for all products.
95	config.Targets[CommonOS] = []Target{commonTargetMap[CommonOS.Name]}
96
97	if runtime.GOOS == "darwin" {
98		config.Targets[config.BuildOS] = config.Targets[config.BuildOS][:1]
99	}
100
101	config.BuildOSTarget = config.Targets[config.BuildOS][0]
102	config.BuildOSCommonTarget = getCommonTargets(config.Targets[config.BuildOS])[0]
103	config.AndroidCommonTarget = getCommonTargets(config.Targets[Android])[0]
104	config.AndroidFirstDeviceTarget = FirstTarget(config.Targets[Android], "lib64", "lib32")[0]
105	config.TestProductVariables.DeviceArch = proptools.StringPtr("arm64")
106	config.TestProductVariables.DeviceArchVariant = proptools.StringPtr("armv8-a")
107	config.TestProductVariables.DeviceSecondaryArch = proptools.StringPtr("arm")
108	config.TestProductVariables.DeviceSecondaryArchVariant = proptools.StringPtr("armv7-a-neon")
109}
110
111// ModifyTestConfigForMusl takes a Config returned by TestConfig and changes the host targets from glibc to musl.
112func ModifyTestConfigForMusl(config Config) {
113	delete(config.Targets, config.BuildOS)
114	config.productVariables.HostMusl = boolPtr(true)
115	determineBuildOS(config.config)
116	config.Targets[config.BuildOS] = []Target{
117		{config.BuildOS, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
118		{config.BuildOS, Arch{ArchType: X86}, NativeBridgeDisabled, "", "", false},
119	}
120
121	config.BuildOSTarget = config.Targets[config.BuildOS][0]
122	config.BuildOSCommonTarget = getCommonTargets(config.Targets[config.BuildOS])[0]
123}
124
125func modifyTestConfigForMuslArm64HostCross(config Config) {
126	config.Targets[LinuxMusl] = append(config.Targets[LinuxMusl],
127		Target{config.BuildOS, Arch{ArchType: Arm64}, NativeBridgeDisabled, "", "", true})
128}
129
130// TestArchConfig returns a Config object suitable for using for tests that
131// need to run the arch mutator.
132func TestArchConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
133	testConfig := TestConfig(buildDir, env, bp, fs)
134	modifyTestConfigToSupportArchMutator(testConfig)
135	return testConfig
136}
137
138// CreateTestConfiguredJarList is a function to create ConfiguredJarList for tests.
139func CreateTestConfiguredJarList(list []string) ConfiguredJarList {
140	// Create the ConfiguredJarList in as similar way as it is created at runtime by marshalling to
141	// a json list of strings and then unmarshalling into a ConfiguredJarList instance.
142	b, err := json.Marshal(list)
143	if err != nil {
144		panic(err)
145	}
146
147	var jarList ConfiguredJarList
148	err = json.Unmarshal(b, &jarList)
149	if err != nil {
150		panic(err)
151	}
152
153	return jarList
154}
155