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