blob: f2510387f5af140bfb8aba0ec9077cbb6080c62a [file] [log] [blame]
Liz Kammer7ec40cc2022-07-29 10:44:23 -04001// 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{
Cole Faustf8231dd2023-04-21 17:37:11 -070038 productVariables: ProductVariables{
Liz Kammer7ec40cc2022-07-29 10:44:23 -040039 DeviceName: stringPtr("test_device"),
40 DeviceProduct: stringPtr("test_product"),
41 Platform_sdk_version: intPtr(30),
Jiyong Park7416d672024-01-04 23:20:42 +000042 Platform_sdk_version_or_codename: stringPtr("S"),
Liz Kammer7ec40cc2022-07-29 10:44:23 -040043 Platform_sdk_codename: stringPtr("S"),
44 Platform_base_sdk_extension_version: intPtr(1),
45 Platform_version_active_codenames: []string{"S", "Tiramisu"},
Jiyong Park7416d672024-01-04 23:20:42 +000046 DeviceSystemSdkVersions: []string{"29", "30", "S"},
47 Platform_systemsdk_versions: []string{"29", "30", "S", "Tiramisu"},
Liz Kammer7ec40cc2022-07-29 10:44:23 -040048 AAPTConfig: []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"},
49 AAPTPreferredConfig: stringPtr("xhdpi"),
50 AAPTCharacteristics: stringPtr("nosdcard"),
51 AAPTPrebuiltDPI: []string{"xhdpi", "xxhdpi"},
52 UncompressPrivAppDex: boolPtr(true),
Steven Morelanda48df2b2024-06-03 22:29:38 +000053 Shipping_api_level: stringPtr("30"),
Liz Kammer7ec40cc2022-07-29 10:44:23 -040054 },
55
56 outDir: buildDir,
57 soongOutDir: filepath.Join(buildDir, "soong"),
58 captureBuild: true,
59 env: envCopy,
60
61 // Set testAllowNonExistentPaths so that test contexts don't need to specify every path
62 // passed to PathForSource or PathForModuleSrc.
63 TestAllowNonExistentPaths: true,
64
Colin Crossb63d7b32023-12-07 16:54:51 -080065 BuildMode: AnalysisNoBazel,
Liz Kammer7ec40cc2022-07-29 10:44:23 -040066 }
67 config.deviceConfig = &deviceConfig{
68 config: config,
69 }
70 config.TestProductVariables = &config.productVariables
71
72 config.mockFileSystem(bp, fs)
73
74 determineBuildOS(config)
75
76 return Config{config}
77}
78
79func modifyTestConfigToSupportArchMutator(testConfig Config) {
80 config := testConfig.config
81
82 config.Targets = map[OsType][]Target{
83 Android: []Target{
84 {Android, Arch{ArchType: Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridgeDisabled, "", "", false},
85 {Android, Arch{ArchType: Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridgeDisabled, "", "", false},
86 },
87 config.BuildOS: []Target{
88 {config.BuildOS, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
89 {config.BuildOS, Arch{ArchType: X86}, NativeBridgeDisabled, "", "", false},
90 },
91 }
92
Paul Duffinde13a3a2022-10-05 15:47:25 +010093 // Make the CommonOS OsType available for all products.
94 config.Targets[CommonOS] = []Target{commonTargetMap[CommonOS.Name]}
95
Liz Kammer7ec40cc2022-07-29 10:44:23 -040096 if runtime.GOOS == "darwin" {
97 config.Targets[config.BuildOS] = config.Targets[config.BuildOS][:1]
98 }
99
100 config.BuildOSTarget = config.Targets[config.BuildOS][0]
101 config.BuildOSCommonTarget = getCommonTargets(config.Targets[config.BuildOS])[0]
102 config.AndroidCommonTarget = getCommonTargets(config.Targets[Android])[0]
103 config.AndroidFirstDeviceTarget = FirstTarget(config.Targets[Android], "lib64", "lib32")[0]
104 config.TestProductVariables.DeviceArch = proptools.StringPtr("arm64")
105 config.TestProductVariables.DeviceArchVariant = proptools.StringPtr("armv8-a")
106 config.TestProductVariables.DeviceSecondaryArch = proptools.StringPtr("arm")
107 config.TestProductVariables.DeviceSecondaryArchVariant = proptools.StringPtr("armv7-a-neon")
108}
109
Colin Cross5dc62c92023-02-15 12:20:19 -0800110// ModifyTestConfigForMusl takes a Config returned by TestConfig and changes the host targets from glibc to musl.
111func ModifyTestConfigForMusl(config Config) {
Liz Kammer7ec40cc2022-07-29 10:44:23 -0400112 delete(config.Targets, config.BuildOS)
113 config.productVariables.HostMusl = boolPtr(true)
114 determineBuildOS(config.config)
115 config.Targets[config.BuildOS] = []Target{
116 {config.BuildOS, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
117 {config.BuildOS, Arch{ArchType: X86}, NativeBridgeDisabled, "", "", false},
118 }
119
120 config.BuildOSTarget = config.Targets[config.BuildOS][0]
121 config.BuildOSCommonTarget = getCommonTargets(config.Targets[config.BuildOS])[0]
122}
123
Colin Crossc0f0eb82022-07-19 14:41:11 -0700124func modifyTestConfigForMuslArm64HostCross(config Config) {
125 config.Targets[LinuxMusl] = append(config.Targets[LinuxMusl],
126 Target{config.BuildOS, Arch{ArchType: Arm64}, NativeBridgeDisabled, "", "", true})
127}
128
Liz Kammer7ec40cc2022-07-29 10:44:23 -0400129// TestArchConfig returns a Config object suitable for using for tests that
130// need to run the arch mutator.
131func TestArchConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
132 testConfig := TestConfig(buildDir, env, bp, fs)
133 modifyTestConfigToSupportArchMutator(testConfig)
134 return testConfig
135}
136
137// CreateTestConfiguredJarList is a function to create ConfiguredJarList for tests.
138func CreateTestConfiguredJarList(list []string) ConfiguredJarList {
139 // Create the ConfiguredJarList in as similar way as it is created at runtime by marshalling to
140 // a json list of strings and then unmarshalling into a ConfiguredJarList instance.
141 b, err := json.Marshal(list)
142 if err != nil {
143 panic(err)
144 }
145
146 var jarList ConfiguredJarList
147 err = json.Unmarshal(b, &jarList)
148 if err != nil {
149 panic(err)
150 }
151
152 return jarList
153}