• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
26func initTestConfig(buildDir string, env map[string]string) *config {
27	envCopy := make(map[string]string)
28	for k, v := range env {
29		envCopy[k] = v
30	}
31
32	// Copy the real PATH value to the test environment, it's needed by
33	// NonHermeticHostSystemTool() used in x86_darwin_host.go
34	envCopy["PATH"] = os.Getenv("PATH")
35
36	config := &config{
37		productVariables: ProductVariables{
38			DeviceName:                          stringPtr("test_device"),
39			DeviceProduct:                       stringPtr("test_product"),
40			Platform_sdk_version:                intPtr(30),
41			Platform_sdk_version_or_codename:    stringPtr("S"),
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{"29", "30", "S"},
46			Platform_systemsdk_versions:         []string{"29", "30", "S", "Tiramisu"},
47			VendorApiLevel:                      stringPtr("202404"),
48			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),
53			Shipping_api_level:                  stringPtr("30"),
54		},
55
56		outDir:       buildDir,
57		soongOutDir:  filepath.Join(buildDir, "soong"),
58		captureBuild: true,
59		env:          envCopy,
60		OncePer:      &OncePer{},
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	config.deviceNameToInstall = config.TestProductVariables.DeviceName
73
74	determineBuildOS(config)
75
76	return config
77}
78
79// TestConfig returns a Config object for testing.
80func TestConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
81	config := initTestConfig(buildDir, env)
82
83	config.mockFileSystem(bp, fs)
84
85	config.genericConfig = initTestConfig(buildDir, env)
86	overrideGenericConfig(config)
87
88	return Config{config}
89}
90
91func modifyTestConfigToSupportArchMutator(testConfig Config) {
92	config := testConfig.config
93
94	config.Targets = map[OsType][]Target{
95		Android: []Target{
96			{Android, Arch{ArchType: Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridgeDisabled, "", "", false},
97			{Android, Arch{ArchType: Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridgeDisabled, "", "", false},
98		},
99		config.BuildOS: []Target{
100			{config.BuildOS, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
101			{config.BuildOS, Arch{ArchType: X86}, NativeBridgeDisabled, "", "", false},
102		},
103	}
104
105	// Make the CommonOS OsType available for all products.
106	config.Targets[CommonOS] = []Target{commonTargetMap[CommonOS.Name]}
107
108	if runtime.GOOS == "darwin" {
109		config.Targets[config.BuildOS] = config.Targets[config.BuildOS][:1]
110	}
111
112	config.BuildOSTarget = config.Targets[config.BuildOS][0]
113	config.BuildOSCommonTarget = getCommonTargets(config.Targets[config.BuildOS])[0]
114	config.AndroidCommonTarget = getCommonTargets(config.Targets[Android])[0]
115	config.AndroidFirstDeviceTarget = FirstTarget(config.Targets[Android], "lib64", "lib32")[0]
116	config.TestProductVariables.DeviceArch = proptools.StringPtr("arm64")
117	config.TestProductVariables.DeviceArchVariant = proptools.StringPtr("armv8-a")
118	config.TestProductVariables.DeviceSecondaryArch = proptools.StringPtr("arm")
119	config.TestProductVariables.DeviceSecondaryArchVariant = proptools.StringPtr("armv7-a-neon")
120}
121
122// ModifyTestConfigForMusl takes a Config returned by TestConfig and changes the host targets from glibc to musl.
123func ModifyTestConfigForMusl(config Config) {
124	delete(config.Targets, config.BuildOS)
125	config.productVariables.HostMusl = boolPtr(true)
126	determineBuildOS(config.config)
127	config.Targets[config.BuildOS] = []Target{
128		{config.BuildOS, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
129		{config.BuildOS, Arch{ArchType: X86}, NativeBridgeDisabled, "", "", false},
130	}
131
132	config.BuildOSTarget = config.Targets[config.BuildOS][0]
133	config.BuildOSCommonTarget = getCommonTargets(config.Targets[config.BuildOS])[0]
134}
135
136func modifyTestConfigForMuslArm64HostCross(config Config) {
137	config.Targets[LinuxMusl] = append(config.Targets[LinuxMusl],
138		Target{config.BuildOS, Arch{ArchType: Arm64}, NativeBridgeDisabled, "", "", true})
139}
140
141// TestArchConfig returns a Config object suitable for using for tests that
142// need to run the arch mutator.
143func TestArchConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
144	testConfig := TestConfig(buildDir, env, bp, fs)
145	modifyTestConfigToSupportArchMutator(testConfig)
146	return testConfig
147}
148
149// CreateTestConfiguredJarList is a function to create ConfiguredJarList for tests.
150func CreateTestConfiguredJarList(list []string) ConfiguredJarList {
151	// Create the ConfiguredJarList in as similar way as it is created at runtime by marshalling to
152	// a json list of strings and then unmarshalling into a ConfiguredJarList instance.
153	b, err := json.Marshal(list)
154	if err != nil {
155		panic(err)
156	}
157
158	var jarList ConfiguredJarList
159	err = json.Unmarshal(b, &jarList)
160	if err != nil {
161		panic(err)
162	}
163
164	return jarList
165}
166