blob: e335de00885476ad79c249d8e7cad4a45ce0ef6c [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 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
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
Jingwen Chenc711fec2020-11-22 23:52:50 -050017// This is the primary location to write and read all configuration values and
18// product variables necessary for soong_build's operation.
19
Colin Cross3f40fa42015-01-30 17:27:36 -080020import (
Colin Cross3f40fa42015-01-30 17:27:36 -080021 "encoding/json"
Lukacs T. Berki720b3962021-03-17 13:34:30 +010022 "errors"
Colin Cross3f40fa42015-01-30 17:27:36 -080023 "fmt"
Colin Crossd8f20142016-11-03 09:43:26 -070024 "io/ioutil"
Colin Cross3f40fa42015-01-30 17:27:36 -080025 "os"
Colin Cross35cec122015-04-02 14:37:16 -070026 "path/filepath"
Colin Cross3f40fa42015-01-30 17:27:36 -080027 "runtime"
Inseob Kim60c32f02020-12-21 22:53:05 +090028 "strconv"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070029 "strings"
Colin Crossc1e86a32015-04-15 12:33:28 -070030 "sync"
Colin Cross6ff51382015-12-17 16:39:19 -080031
Colin Cross98be1bb2019-12-13 20:41:13 -080032 "github.com/google/blueprint"
Colin Crosse87040b2017-12-11 15:52:26 -080033 "github.com/google/blueprint/bootstrap"
Colin Cross98be1bb2019-12-13 20:41:13 -080034 "github.com/google/blueprint/pathtools"
Colin Cross6ff51382015-12-17 16:39:19 -080035 "github.com/google/blueprint/proptools"
Colin Cross9d34f352019-11-22 16:03:51 -080036
37 "android/soong/android/soongconfig"
Colin Cross77cdcfd2021-03-12 11:28:25 -080038 "android/soong/remoteexec"
Colin Cross3f40fa42015-01-30 17:27:36 -080039)
40
Jingwen Chenc711fec2020-11-22 23:52:50 -050041// Bool re-exports proptools.Bool for the android package.
Colin Cross6ff51382015-12-17 16:39:19 -080042var Bool = proptools.Bool
Jingwen Chenc711fec2020-11-22 23:52:50 -050043
44// String re-exports proptools.String for the android package.
Jack He8cc71432016-12-08 15:45:07 -080045var String = proptools.String
Jingwen Chenc711fec2020-11-22 23:52:50 -050046
47// StringDefault re-exports proptools.StringDefault for the android package.
Jeongik Cha219141c2020-08-06 23:00:37 +090048var StringDefault = proptools.StringDefault
Jiyong Park6a927c42020-01-21 02:03:43 +090049
Jingwen Chenc711fec2020-11-22 23:52:50 -050050// FutureApiLevelInt is a placeholder constant for unreleased API levels.
Dan Albert0b176c82020-07-23 16:43:25 -070051const FutureApiLevelInt = 10000
52
Jingwen Chenc711fec2020-11-22 23:52:50 -050053// FutureApiLevel represents unreleased API levels.
Dan Albert0b176c82020-07-23 16:43:25 -070054var FutureApiLevel = ApiLevel{
55 value: "current",
56 number: FutureApiLevelInt,
57 isPreview: true,
58}
Colin Cross6ff51382015-12-17 16:39:19 -080059
Jingwen Chenc4d91bc2020-11-24 22:59:26 -050060// The product variables file name, containing product config from Kati.
Dan Willemsen87b17d12015-07-14 00:39:06 -070061const productVariablesFileName = "soong.variables"
Colin Cross3f40fa42015-01-30 17:27:36 -080062
Colin Cross9272ade2016-08-17 15:24:12 -070063// A Config object represents the entire build configuration for Android.
Colin Crossc3c0a492015-04-10 15:43:55 -070064type Config struct {
65 *config
66}
67
Jingwen Chenc711fec2020-11-22 23:52:50 -050068// BuildDir returns the build output directory for the configuration.
Jeff Gastonefc1b412017-03-29 17:29:06 -070069func (c Config) BuildDir() string {
70 return c.buildDir
71}
72
Lukacs T. Berki89e9a162021-03-12 08:31:32 +010073func (c Config) NinjaBuildDir() string {
74 return c.buildDir
75}
76
Lukacs T. Berki5f6cb1d2021-03-17 15:03:14 +010077func (c Config) DebugCompilation() bool {
78 return false // Never compile Go code in the main build for debugging
79}
80
Lukacs T. Berki89e9a162021-03-12 08:31:32 +010081func (c Config) SrcDir() string {
82 return c.srcDir
83}
84
Jingwen Chenc711fec2020-11-22 23:52:50 -050085// A DeviceConfig object represents the configuration for a particular device
86// being built. For now there will only be one of these, but in the future there
87// may be multiple devices being built.
Colin Cross9272ade2016-08-17 15:24:12 -070088type DeviceConfig struct {
89 *deviceConfig
90}
91
Jingwen Chenc711fec2020-11-22 23:52:50 -050092// VendorConfig represents the configuration for vendor-specific behavior.
Colin Cross9d34f352019-11-22 16:03:51 -080093type VendorConfig soongconfig.SoongConfig
Dan Willemsen0fe78662018-03-26 12:41:18 -070094
Jingwen Chenc711fec2020-11-22 23:52:50 -050095// Definition of general build configuration for soong_build. Some of these
Jingwen Chenc4d91bc2020-11-24 22:59:26 -050096// product configuration values are read from Kati-generated soong.variables.
Colin Cross1332b002015-04-07 17:11:30 -070097type config struct {
Jingwen Chenc711fec2020-11-22 23:52:50 -050098 // Options configurable with soong.variables
Dan Willemsen45133ac2018-03-09 21:22:06 -080099 productVariables productVariables
Colin Cross3f40fa42015-01-30 17:27:36 -0800100
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700101 // Only available on configs created by TestConfig
102 TestProductVariables *productVariables
103
Jingwen Chenc711fec2020-11-22 23:52:50 -0500104 // A specialized context object for Bazel/Soong mixed builds and migration
105 // purposes.
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400106 BazelContext BazelContext
107
Dan Willemsen87b17d12015-07-14 00:39:06 -0700108 ProductVariablesFileName string
109
Jaewoong Jung642916f2020-10-09 17:25:15 -0700110 Targets map[OsType][]Target
111 BuildOSTarget Target // the Target for tools run on the build machine
112 BuildOSCommonTarget Target // the Target for common (java) tools run on the build machine
113 AndroidCommonTarget Target // the Target for common modules for the Android device
114 AndroidFirstDeviceTarget Target // the first Target for modules for the Android device
Dan Willemsen218f6562015-07-08 18:13:11 -0700115
Jingwen Chenc711fec2020-11-22 23:52:50 -0500116 // multilibConflicts for an ArchType is true if there is earlier configured
117 // device architecture with the same multilib value.
Colin Cross3b19f5d2019-09-17 14:45:31 -0700118 multilibConflicts map[ArchType]bool
119
Colin Cross9272ade2016-08-17 15:24:12 -0700120 deviceConfig *deviceConfig
121
Chris Parsons8f232a22020-06-23 17:37:05 -0400122 srcDir string // the path of the root source directory
123 buildDir string // the path of the build output directory
124 moduleListFile string // the path to the file which lists blueprint files to parse.
Colin Crossc1e86a32015-04-15 12:33:28 -0700125
Colin Cross6ccbc912017-10-10 23:07:38 -0700126 env map[string]string
Dan Willemsene7680ba2015-09-11 17:06:19 -0700127 envLock sync.Mutex
128 envDeps map[string]string
129 envFrozen bool
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800130
Jingwen Chencda22c92020-11-23 00:22:30 -0500131 // Changes behavior based on whether Kati runs after soong_build, or if soong_build
132 // runs standalone.
133 katiEnabled bool
Colin Cross1e7d3702016-08-24 15:25:47 -0700134
Colin Cross32616ed2017-09-05 21:56:44 -0700135 captureBuild bool // true for tests, saves build parameters for each module
136 ignoreEnvironment bool // true for tests, returns empty from all Getenv calls
Colin Crosscec81712017-07-13 14:43:27 -0700137
Colin Crosse87040b2017-12-11 15:52:26 -0800138 stopBefore bootstrap.StopBefore
139
Colin Cross98be1bb2019-12-13 20:41:13 -0800140 fs pathtools.FileSystem
141 mockBpList string
142
Colin Cross5e6a7972020-06-07 16:56:32 -0700143 // If testAllowNonExistentPaths is true then PathForSource and PathForModuleSrc won't error
144 // in tests when a path doesn't exist.
Pedro Loureiro5d190cc2021-02-15 15:41:33 +0000145 TestAllowNonExistentPaths bool
Colin Cross5e6a7972020-06-07 16:56:32 -0700146
Jingwen Chenc711fec2020-11-22 23:52:50 -0500147 // The list of files that when changed, must invalidate soong_build to
148 // regenerate build.ninja.
Colin Cross12129292020-10-29 18:23:58 -0700149 ninjaFileDepsSet sync.Map
150
Colin Cross9272ade2016-08-17 15:24:12 -0700151 OncePer
152}
153
154type deviceConfig struct {
Dan Willemsen00269f22017-07-06 16:59:48 -0700155 config *config
Colin Cross9272ade2016-08-17 15:24:12 -0700156 OncePer
Colin Cross3f40fa42015-01-30 17:27:36 -0800157}
158
Colin Cross485e5722015-08-27 13:28:01 -0700159type jsonConfigurable interface {
Colin Cross27385972015-09-18 10:57:10 -0700160 SetDefaultConfig()
Colin Cross485e5722015-08-27 13:28:01 -0700161}
Colin Cross3f40fa42015-01-30 17:27:36 -0800162
Colin Cross485e5722015-08-27 13:28:01 -0700163func loadConfig(config *config) error {
Colin Cross988414c2020-01-11 01:11:46 +0000164 return loadFromConfigFile(&config.productVariables, absolutePath(config.ProductVariablesFileName))
Colin Cross485e5722015-08-27 13:28:01 -0700165}
166
Jingwen Chenc711fec2020-11-22 23:52:50 -0500167// loadFromConfigFile loads and decodes configuration options from a JSON file
168// in the current working directory.
Colin Cross485e5722015-08-27 13:28:01 -0700169func loadFromConfigFile(configurable jsonConfigurable, filename string) error {
Colin Cross3f40fa42015-01-30 17:27:36 -0800170 // Try to open the file
Colin Cross485e5722015-08-27 13:28:01 -0700171 configFileReader, err := os.Open(filename)
Colin Cross3f40fa42015-01-30 17:27:36 -0800172 defer configFileReader.Close()
173 if os.IsNotExist(err) {
174 // Need to create a file, so that blueprint & ninja don't get in
175 // a dependency tracking loop.
176 // Make a file-configurable-options with defaults, write it out using
177 // a json writer.
Colin Cross27385972015-09-18 10:57:10 -0700178 configurable.SetDefaultConfig()
179 err = saveToConfigFile(configurable, filename)
Colin Cross3f40fa42015-01-30 17:27:36 -0800180 if err != nil {
181 return err
182 }
Colin Cross15cd21a2018-02-27 11:26:02 -0800183 } else if err != nil {
184 return fmt.Errorf("config file: could not open %s: %s", filename, err.Error())
Colin Cross3f40fa42015-01-30 17:27:36 -0800185 } else {
186 // Make a decoder for it
187 jsonDecoder := json.NewDecoder(configFileReader)
Colin Cross485e5722015-08-27 13:28:01 -0700188 err = jsonDecoder.Decode(configurable)
Colin Cross3f40fa42015-01-30 17:27:36 -0800189 if err != nil {
Colin Cross15cd21a2018-02-27 11:26:02 -0800190 return fmt.Errorf("config file: %s did not parse correctly: %s", filename, err.Error())
Colin Cross3f40fa42015-01-30 17:27:36 -0800191 }
192 }
193
Colin Cross3f40fa42015-01-30 17:27:36 -0800194 // No error
195 return nil
196}
197
Colin Crossd8f20142016-11-03 09:43:26 -0700198// atomically writes the config file in case two copies of soong_build are running simultaneously
199// (for example, docs generation and ninja manifest generation)
Colin Cross485e5722015-08-27 13:28:01 -0700200func saveToConfigFile(config jsonConfigurable, filename string) error {
Colin Cross3f40fa42015-01-30 17:27:36 -0800201 data, err := json.MarshalIndent(&config, "", " ")
202 if err != nil {
203 return fmt.Errorf("cannot marshal config data: %s", err.Error())
204 }
205
Colin Crossd8f20142016-11-03 09:43:26 -0700206 f, err := ioutil.TempFile(filepath.Dir(filename), "config")
Colin Cross3f40fa42015-01-30 17:27:36 -0800207 if err != nil {
Jingwen Chenc711fec2020-11-22 23:52:50 -0500208 return fmt.Errorf("cannot create empty config file %s: %s", filename, err.Error())
Colin Cross3f40fa42015-01-30 17:27:36 -0800209 }
Colin Crossd8f20142016-11-03 09:43:26 -0700210 defer os.Remove(f.Name())
211 defer f.Close()
Colin Cross3f40fa42015-01-30 17:27:36 -0800212
Colin Crossd8f20142016-11-03 09:43:26 -0700213 _, err = f.Write(data)
Colin Cross3f40fa42015-01-30 17:27:36 -0800214 if err != nil {
Colin Cross485e5722015-08-27 13:28:01 -0700215 return fmt.Errorf("default config file: %s could not be written: %s", filename, err.Error())
216 }
217
Colin Crossd8f20142016-11-03 09:43:26 -0700218 _, err = f.WriteString("\n")
Colin Cross485e5722015-08-27 13:28:01 -0700219 if err != nil {
220 return fmt.Errorf("default config file: %s could not be written: %s", filename, err.Error())
Colin Cross3f40fa42015-01-30 17:27:36 -0800221 }
222
Colin Crossd8f20142016-11-03 09:43:26 -0700223 f.Close()
224 os.Rename(f.Name(), filename)
225
Colin Cross3f40fa42015-01-30 17:27:36 -0800226 return nil
227}
228
Colin Cross988414c2020-01-11 01:11:46 +0000229// NullConfig returns a mostly empty Config for use by standalone tools like dexpreopt_gen that
230// use the android package.
231func NullConfig(buildDir string) Config {
232 return Config{
233 config: &config{
234 buildDir: buildDir,
235 fs: pathtools.OsFs,
236 },
237 }
238}
239
Jingwen Chenc711fec2020-11-22 23:52:50 -0500240// TestConfig returns a Config object for testing.
Colin Cross98be1bb2019-12-13 20:41:13 -0800241func TestConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
Colin Cross9c6241f2019-04-22 15:51:26 -0700242 envCopy := make(map[string]string)
243 for k, v := range env {
244 envCopy[k] = v
245 }
246
Jingwen Chen2838c812020-11-23 01:06:40 -0500247 // Copy the real PATH value to the test environment, it's needed by
248 // NonHermeticHostSystemTool() used in x86_darwin_host.go
Lukacs T. Berkideba7212021-03-04 10:50:10 +0100249 envCopy["PATH"] = os.Getenv("PATH")
Colin Cross9c6241f2019-04-22 15:51:26 -0700250
Dan Willemsen00269f22017-07-06 16:59:48 -0700251 config := &config{
Dan Willemsen45133ac2018-03-09 21:22:06 -0800252 productVariables: productVariables{
Dan Albert4f378d72020-07-23 17:32:15 -0700253 DeviceName: stringPtr("test_device"),
254 Platform_sdk_version: intPtr(30),
255 Platform_sdk_codename: stringPtr("S"),
256 Platform_version_active_codenames: []string{"S"},
257 DeviceSystemSdkVersions: []string{"14", "15"},
258 Platform_systemsdk_versions: []string{"29", "30"},
259 AAPTConfig: []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"},
260 AAPTPreferredConfig: stringPtr("xhdpi"),
261 AAPTCharacteristics: stringPtr("nosdcard"),
262 AAPTPrebuiltDPI: []string{"xhdpi", "xxhdpi"},
263 UncompressPrivAppDex: boolPtr(true),
Inseob Kim60c32f02020-12-21 22:53:05 +0900264 ShippingApiLevel: stringPtr("30"),
Dan Willemsen00269f22017-07-06 16:59:48 -0700265 },
266
Colin Cross6ccbc912017-10-10 23:07:38 -0700267 buildDir: buildDir,
268 captureBuild: true,
Colin Cross9c6241f2019-04-22 15:51:26 -0700269 env: envCopy,
Colin Cross5e6a7972020-06-07 16:56:32 -0700270
271 // Set testAllowNonExistentPaths so that test contexts don't need to specify every path
272 // passed to PathForSource or PathForModuleSrc.
Pedro Loureiro5d190cc2021-02-15 15:41:33 +0000273 TestAllowNonExistentPaths: true,
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400274
275 BazelContext: noopBazelContext{},
Dan Willemsen00269f22017-07-06 16:59:48 -0700276 }
277 config.deviceConfig = &deviceConfig{
278 config: config,
279 }
Dan Willemsen45133ac2018-03-09 21:22:06 -0800280 config.TestProductVariables = &config.productVariables
Dan Willemsen00269f22017-07-06 16:59:48 -0700281
Colin Cross98be1bb2019-12-13 20:41:13 -0800282 config.mockFileSystem(bp, fs)
283
Dan Willemsen00269f22017-07-06 16:59:48 -0700284 return Config{config}
Colin Crossce75d2c2016-10-06 16:12:58 -0700285}
286
Paul Duffinecdac8a2021-02-24 19:18:42 +0000287func fuchsiaTargets() map[OsType][]Target {
288 return map[OsType][]Target{
289 Fuchsia: {
Jiyong Park1613e552020-09-14 19:43:17 +0900290 {Fuchsia, Arch{ArchType: Arm64, ArchVariant: "", Abi: []string{"arm64-v8a"}}, NativeBridgeDisabled, "", "", false},
Doug Hornc32c6b02019-01-17 14:44:05 -0800291 },
Paul Duffinecdac8a2021-02-24 19:18:42 +0000292 BuildOs: {
Jiyong Park1613e552020-09-14 19:43:17 +0900293 {BuildOs, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
Doug Hornc32c6b02019-01-17 14:44:05 -0800294 },
295 }
Doug Hornc32c6b02019-01-17 14:44:05 -0800296}
297
Paul Duffinecdac8a2021-02-24 19:18:42 +0000298var PrepareForTestSetDeviceToFuchsia = FixtureModifyConfig(func(config Config) {
299 config.Targets = fuchsiaTargets()
300})
301
Paul Duffin35816122021-02-24 01:49:52 +0000302func modifyTestConfigToSupportArchMutator(testConfig Config) {
Colin Crossae4c6182017-09-15 17:33:55 -0700303 config := testConfig.config
304
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700305 config.Targets = map[OsType][]Target{
306 Android: []Target{
Jiyong Park1613e552020-09-14 19:43:17 +0900307 {Android, Arch{ArchType: Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridgeDisabled, "", "", false},
308 {Android, Arch{ArchType: Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridgeDisabled, "", "", false},
Colin Crossae4c6182017-09-15 17:33:55 -0700309 },
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700310 BuildOs: []Target{
Jiyong Park1613e552020-09-14 19:43:17 +0900311 {BuildOs, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
312 {BuildOs, Arch{ArchType: X86}, NativeBridgeDisabled, "", "", false},
Colin Crossae4c6182017-09-15 17:33:55 -0700313 },
314 }
315
Colin Cross0d99f7c2019-05-14 16:01:24 -0700316 if runtime.GOOS == "darwin" {
317 config.Targets[BuildOs] = config.Targets[BuildOs][:1]
318 }
319
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700320 config.BuildOSTarget = config.Targets[BuildOs][0]
321 config.BuildOSCommonTarget = getCommonTargets(config.Targets[BuildOs])[0]
322 config.AndroidCommonTarget = getCommonTargets(config.Targets[Android])[0]
Jaewoong Jung642916f2020-10-09 17:25:15 -0700323 config.AndroidFirstDeviceTarget = firstTarget(config.Targets[Android], "lib64", "lib32")[0]
Inseob Kim1f086e22019-05-09 13:29:15 +0900324 config.TestProductVariables.DeviceArch = proptools.StringPtr("arm64")
325 config.TestProductVariables.DeviceArchVariant = proptools.StringPtr("armv8-a")
326 config.TestProductVariables.DeviceSecondaryArch = proptools.StringPtr("arm")
327 config.TestProductVariables.DeviceSecondaryArchVariant = proptools.StringPtr("armv7-a-neon")
Paul Duffin35816122021-02-24 01:49:52 +0000328}
Colin Cross2a076922018-10-04 23:28:25 -0700329
Paul Duffin35816122021-02-24 01:49:52 +0000330// TestArchConfig returns a Config object suitable for using for tests that
331// need to run the arch mutator.
332func TestArchConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
333 testConfig := TestConfig(buildDir, env, bp, fs)
334 modifyTestConfigToSupportArchMutator(testConfig)
Colin Crossae4c6182017-09-15 17:33:55 -0700335 return testConfig
336}
337
Jingwen Chenc711fec2020-11-22 23:52:50 -0500338// ConfigForAdditionalRun is a config object which is "reset" for another
339// bootstrap run. Only per-run data is reset. Data which needs to persist across
340// multiple runs in the same program execution is carried over (such as Bazel
341// context or environment deps).
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400342func ConfigForAdditionalRun(c Config) (Config, error) {
343 newConfig, err := NewConfig(c.srcDir, c.buildDir, c.moduleListFile)
344 if err != nil {
345 return Config{}, err
346 }
347 newConfig.BazelContext = c.BazelContext
348 newConfig.envDeps = c.envDeps
349 return newConfig, nil
350}
351
Jingwen Chenc711fec2020-11-22 23:52:50 -0500352// NewConfig creates a new Config object. The srcDir argument specifies the path
353// to the root source directory. It also loads the config file, if found.
Chris Parsons8f232a22020-06-23 17:37:05 -0400354func NewConfig(srcDir, buildDir string, moduleListFile string) (Config, error) {
Jingwen Chenc711fec2020-11-22 23:52:50 -0500355 // Make a config with default options.
Colin Cross9272ade2016-08-17 15:24:12 -0700356 config := &config{
Colin Cross9272ade2016-08-17 15:24:12 -0700357 ProductVariablesFileName: filepath.Join(buildDir, productVariablesFileName),
Dan Willemsen87b17d12015-07-14 00:39:06 -0700358
Colin Cross6ccbc912017-10-10 23:07:38 -0700359 env: originalEnv,
360
Colin Cross3b19f5d2019-09-17 14:45:31 -0700361 srcDir: srcDir,
362 buildDir: buildDir,
363 multilibConflicts: make(map[ArchType]bool),
Colin Cross98be1bb2019-12-13 20:41:13 -0800364
Chris Parsons8f232a22020-06-23 17:37:05 -0400365 moduleListFile: moduleListFile,
366 fs: pathtools.NewOsFs(absSrcDir),
Colin Cross68f55102015-03-25 14:43:57 -0700367 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800368
Dan Willemsen00269f22017-07-06 16:59:48 -0700369 config.deviceConfig = &deviceConfig{
Colin Cross9272ade2016-08-17 15:24:12 -0700370 config: config,
371 }
372
Liz Kammer7941b302020-07-28 13:27:34 -0700373 // Soundness check of the build and source directories. This won't catch strange
374 // configurations with symlinks, but at least checks the obvious case.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700375 absBuildDir, err := filepath.Abs(buildDir)
376 if err != nil {
377 return Config{}, err
378 }
379
380 absSrcDir, err := filepath.Abs(srcDir)
381 if err != nil {
382 return Config{}, err
383 }
384
385 if strings.HasPrefix(absSrcDir, absBuildDir) {
386 return Config{}, fmt.Errorf("Build dir must not contain source directory")
387 }
388
Colin Cross3f40fa42015-01-30 17:27:36 -0800389 // Load any configurable options from the configuration file
Colin Cross9272ade2016-08-17 15:24:12 -0700390 err = loadConfig(config)
Colin Cross3f40fa42015-01-30 17:27:36 -0800391 if err != nil {
Colin Crossc3c0a492015-04-10 15:43:55 -0700392 return Config{}, err
Colin Cross3f40fa42015-01-30 17:27:36 -0800393 }
394
Jingwen Chencda22c92020-11-23 00:22:30 -0500395 KatiEnabledMarkerFile := filepath.Join(buildDir, ".soong.kati_enabled")
396 if _, err := os.Stat(absolutePath(KatiEnabledMarkerFile)); err == nil {
397 config.katiEnabled = true
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800398 }
399
Jingwen Chenc711fec2020-11-22 23:52:50 -0500400 // Sets up the map of target OSes to the finer grained compilation targets
401 // that are configured from the product variables.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700402 targets, err := decodeTargetProductVariables(config)
Dan Willemsen218f6562015-07-08 18:13:11 -0700403 if err != nil {
404 return Config{}, err
405 }
406
Paul Duffin1356d8c2020-02-25 19:26:33 +0000407 // Make the CommonOS OsType available for all products.
408 targets[CommonOS] = []Target{commonTargetMap[CommonOS.Name]}
409
Dan Albert4098deb2016-10-19 14:04:41 -0700410 var archConfig []archConfig
Jingwen Chenc4d91bc2020-11-24 22:59:26 -0500411 if config.NdkAbis() {
Dan Albert4098deb2016-10-19 14:04:41 -0700412 archConfig = getNdkAbisConfig()
Martin Stjernholmc1ecc432019-11-15 15:00:31 +0000413 } else if config.AmlAbis() {
414 archConfig = getAmlAbisConfig()
Dan Albert4098deb2016-10-19 14:04:41 -0700415 }
416
417 if archConfig != nil {
Dan Willemsen01a3c252019-01-11 19:02:16 -0800418 androidTargets, err := decodeArchSettings(Android, archConfig)
Dan Willemsen322acaf2016-01-12 23:07:05 -0800419 if err != nil {
420 return Config{}, err
421 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700422 targets[Android] = androidTargets
Dan Willemsen322acaf2016-01-12 23:07:05 -0800423 }
424
Colin Cross3b19f5d2019-09-17 14:45:31 -0700425 multilib := make(map[string]bool)
426 for _, target := range targets[Android] {
427 if seen := multilib[target.Arch.ArchType.Multilib]; seen {
428 config.multilibConflicts[target.Arch.ArchType] = true
429 }
430 multilib[target.Arch.ArchType.Multilib] = true
431 }
432
Jingwen Chenc711fec2020-11-22 23:52:50 -0500433 // Map of OS to compilation targets.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700434 config.Targets = targets
Jingwen Chenc711fec2020-11-22 23:52:50 -0500435
436 // Compilation targets for host tools.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700437 config.BuildOSTarget = config.Targets[BuildOs][0]
438 config.BuildOSCommonTarget = getCommonTargets(config.Targets[BuildOs])[0]
Jingwen Chenc711fec2020-11-22 23:52:50 -0500439
440 // Compilation targets for Android.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700441 if len(config.Targets[Android]) > 0 {
442 config.AndroidCommonTarget = getCommonTargets(config.Targets[Android])[0]
Jaewoong Jung642916f2020-10-09 17:25:15 -0700443 config.AndroidFirstDeviceTarget = firstTarget(config.Targets[Android], "lib64", "lib32")[0]
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700444 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700445
Colin Cross1a6acd42020-06-16 17:51:46 -0700446 if Bool(config.productVariables.GcovCoverage) && Bool(config.productVariables.ClangCoverage) {
447 return Config{}, fmt.Errorf("GcovCoverage and ClangCoverage cannot both be set")
448 }
449
450 config.productVariables.Native_coverage = proptools.BoolPtr(
451 Bool(config.productVariables.GcovCoverage) ||
452 Bool(config.productVariables.ClangCoverage))
453
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400454 config.BazelContext, err = NewBazelContext(config)
Colin Cross3f40fa42015-01-30 17:27:36 -0800455
Jingwen Chenc711fec2020-11-22 23:52:50 -0500456 return Config{config}, err
457}
Colin Cross988414c2020-01-11 01:11:46 +0000458
Colin Cross98be1bb2019-12-13 20:41:13 -0800459// mockFileSystem replaces all reads with accesses to the provided map of
460// filenames to contents stored as a byte slice.
461func (c *config) mockFileSystem(bp string, fs map[string][]byte) {
462 mockFS := map[string][]byte{}
463
464 if _, exists := mockFS["Android.bp"]; !exists {
465 mockFS["Android.bp"] = []byte(bp)
466 }
467
468 for k, v := range fs {
469 mockFS[k] = v
470 }
471
472 // no module list file specified; find every file named Blueprints or Android.bp
473 pathsToParse := []string{}
474 for candidate := range mockFS {
475 base := filepath.Base(candidate)
476 if base == "Blueprints" || base == "Android.bp" {
477 pathsToParse = append(pathsToParse, candidate)
478 }
479 }
480 if len(pathsToParse) < 1 {
481 panic(fmt.Sprintf("No Blueprint or Android.bp files found in mock filesystem: %v\n", mockFS))
482 }
483 mockFS[blueprint.MockModuleListFile] = []byte(strings.Join(pathsToParse, "\n"))
484
485 c.fs = pathtools.MockFs(mockFS)
486 c.mockBpList = blueprint.MockModuleListFile
487}
488
Colin Crosse87040b2017-12-11 15:52:26 -0800489func (c *config) StopBefore() bootstrap.StopBefore {
490 return c.stopBefore
Dan Willemsen218f6562015-07-08 18:13:11 -0700491}
492
Jingwen Chenc711fec2020-11-22 23:52:50 -0500493// SetStopBefore configures soong_build to exit earlier at a specific point.
Colin Crosse87040b2017-12-11 15:52:26 -0800494func (c *config) SetStopBefore(stopBefore bootstrap.StopBefore) {
495 c.stopBefore = stopBefore
496}
497
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100498func (c *config) SetAllowMissingDependencies() {
499 c.productVariables.Allow_missing_dependencies = proptools.BoolPtr(true)
500}
501
Colin Crosse87040b2017-12-11 15:52:26 -0800502var _ bootstrap.ConfigStopBefore = (*config)(nil)
503
Jingwen Chenc711fec2020-11-22 23:52:50 -0500504// BlueprintToolLocation returns the directory containing build system tools
505// from Blueprint, like soong_zip and merge_zips.
Dan Willemsenc2aa4a92016-05-26 15:13:03 -0700506func (c *config) BlueprintToolLocation() string {
507 return filepath.Join(c.buildDir, "host", c.PrebuiltOS(), "bin")
508}
509
Colin Crosse87040b2017-12-11 15:52:26 -0800510var _ bootstrap.ConfigBlueprintToolLocation = (*config)(nil)
511
Dan Willemsen60e62f02018-11-16 21:05:32 -0800512func (c *config) HostToolPath(ctx PathContext, tool string) Path {
513 return PathForOutput(ctx, "host", c.PrebuiltOS(), "bin", tool)
514}
515
Martin Stjernholm7260d062019-12-09 21:47:14 +0000516func (c *config) HostJNIToolPath(ctx PathContext, path string) Path {
517 ext := ".so"
518 if runtime.GOOS == "darwin" {
519 ext = ".dylib"
520 }
521 return PathForOutput(ctx, "host", c.PrebuiltOS(), "lib64", path+ext)
522}
523
524func (c *config) HostJavaToolPath(ctx PathContext, path string) Path {
525 return PathForOutput(ctx, "host", c.PrebuiltOS(), "framework", path)
526}
527
Jingwen Chenc711fec2020-11-22 23:52:50 -0500528// PrebuiltOS returns the name of the host OS used in prebuilts directories.
Colin Cross1332b002015-04-07 17:11:30 -0700529func (c *config) PrebuiltOS() string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800530 switch runtime.GOOS {
531 case "linux":
532 return "linux-x86"
533 case "darwin":
534 return "darwin-x86"
535 default:
536 panic("Unknown GOOS")
537 }
538}
539
540// GoRoot returns the path to the root directory of the Go toolchain.
Colin Cross1332b002015-04-07 17:11:30 -0700541func (c *config) GoRoot() string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800542 return fmt.Sprintf("%s/prebuilts/go/%s", c.srcDir, c.PrebuiltOS())
543}
544
Jingwen Chenc711fec2020-11-22 23:52:50 -0500545// PrebuiltBuildTool returns the path to a tool in the prebuilts directory containing
546// checked-in tools, like Kati, Ninja or Toybox, for the current host OS.
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700547func (c *config) PrebuiltBuildTool(ctx PathContext, tool string) Path {
548 return PathForSource(ctx, "prebuilts/build-tools", c.PrebuiltOS(), "bin", tool)
549}
550
Jingwen Chenc711fec2020-11-22 23:52:50 -0500551// CpPreserveSymlinksFlags returns the host-specific flag for the cp(1) command
552// to preserve symlinks.
Colin Cross1332b002015-04-07 17:11:30 -0700553func (c *config) CpPreserveSymlinksFlags() string {
Colin Cross485e5722015-08-27 13:28:01 -0700554 switch runtime.GOOS {
Colin Cross3f40fa42015-01-30 17:27:36 -0800555 case "darwin":
556 return "-R"
557 case "linux":
558 return "-d"
559 default:
560 return ""
561 }
562}
Colin Cross68f55102015-03-25 14:43:57 -0700563
Colin Cross1332b002015-04-07 17:11:30 -0700564func (c *config) Getenv(key string) string {
Colin Cross68f55102015-03-25 14:43:57 -0700565 var val string
566 var exists bool
Colin Crossc1e86a32015-04-15 12:33:28 -0700567 c.envLock.Lock()
Colin Crossc0d58b42017-02-06 15:40:41 -0800568 defer c.envLock.Unlock()
569 if c.envDeps == nil {
570 c.envDeps = make(map[string]string)
571 }
Colin Cross68f55102015-03-25 14:43:57 -0700572 if val, exists = c.envDeps[key]; !exists {
Dan Willemsene7680ba2015-09-11 17:06:19 -0700573 if c.envFrozen {
574 panic("Cannot access new environment variables after envdeps are frozen")
575 }
Colin Cross6ccbc912017-10-10 23:07:38 -0700576 val, _ = c.env[key]
Colin Cross68f55102015-03-25 14:43:57 -0700577 c.envDeps[key] = val
578 }
579 return val
580}
581
Colin Cross99d7c232016-11-23 16:52:04 -0800582func (c *config) GetenvWithDefault(key string, defaultValue string) string {
583 ret := c.Getenv(key)
584 if ret == "" {
585 return defaultValue
586 }
587 return ret
588}
589
590func (c *config) IsEnvTrue(key string) bool {
591 value := c.Getenv(key)
592 return value == "1" || value == "y" || value == "yes" || value == "on" || value == "true"
593}
594
595func (c *config) IsEnvFalse(key string) bool {
596 value := c.Getenv(key)
597 return value == "0" || value == "n" || value == "no" || value == "off" || value == "false"
598}
599
Jingwen Chenc711fec2020-11-22 23:52:50 -0500600// EnvDeps returns the environment variables this build depends on. The first
601// call to this function blocks future reads from the environment.
Colin Cross1332b002015-04-07 17:11:30 -0700602func (c *config) EnvDeps() map[string]string {
Dan Willemsene7680ba2015-09-11 17:06:19 -0700603 c.envLock.Lock()
Colin Crossc0d58b42017-02-06 15:40:41 -0800604 defer c.envLock.Unlock()
Dan Willemsene7680ba2015-09-11 17:06:19 -0700605 c.envFrozen = true
Colin Cross68f55102015-03-25 14:43:57 -0700606 return c.envDeps
607}
Colin Cross35cec122015-04-02 14:37:16 -0700608
Jingwen Chencda22c92020-11-23 00:22:30 -0500609func (c *config) KatiEnabled() bool {
610 return c.katiEnabled
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800611}
612
Nan Zhang581fd212018-01-10 16:06:12 -0800613func (c *config) BuildId() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800614 return String(c.productVariables.BuildId)
Nan Zhang581fd212018-01-10 16:06:12 -0800615}
616
Jingwen Chenc711fec2020-11-22 23:52:50 -0500617// BuildNumberFile returns the path to a text file containing metadata
618// representing the current build's number.
619//
620// Rules that want to reference the build number should read from this file
621// without depending on it. They will run whenever their other dependencies
622// require them to run and get the current build number. This ensures they don't
623// rebuild on every incremental build when the build number changes.
Colin Cross2a2e0db2020-02-21 16:55:46 -0800624func (c *config) BuildNumberFile(ctx PathContext) Path {
625 return PathForOutput(ctx, String(c.productVariables.BuildNumberFile))
Nan Zhang581fd212018-01-10 16:06:12 -0800626}
627
Jingwen Chenc711fec2020-11-22 23:52:50 -0500628// DeviceName returns the name of the current device target.
Colin Cross35cec122015-04-02 14:37:16 -0700629// TODO: take an AndroidModuleContext to select the device name for multi-device builds
Colin Cross1332b002015-04-07 17:11:30 -0700630func (c *config) DeviceName() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800631 return *c.productVariables.DeviceName
Colin Cross35cec122015-04-02 14:37:16 -0700632}
633
Anton Hansson53c88442019-03-18 15:53:16 +0000634func (c *config) DeviceResourceOverlays() []string {
635 return c.productVariables.DeviceResourceOverlays
636}
637
638func (c *config) ProductResourceOverlays() []string {
639 return c.productVariables.ProductResourceOverlays
Colin Cross30e076a2015-04-13 13:58:27 -0700640}
641
Colin Crossbfd347d2018-05-09 11:11:35 -0700642func (c *config) PlatformVersionName() string {
643 return String(c.productVariables.Platform_version_name)
644}
645
Dan Albert4f378d72020-07-23 17:32:15 -0700646func (c *config) PlatformSdkVersion() ApiLevel {
647 return uncheckedFinalApiLevel(*c.productVariables.Platform_sdk_version)
Colin Cross30e076a2015-04-13 13:58:27 -0700648}
649
Colin Crossd09b0b62018-04-18 11:06:47 -0700650func (c *config) PlatformSdkCodename() string {
651 return String(c.productVariables.Platform_sdk_codename)
652}
653
Colin Cross092c9da2019-04-02 22:56:43 -0700654func (c *config) PlatformSecurityPatch() string {
655 return String(c.productVariables.Platform_security_patch)
656}
657
658func (c *config) PlatformPreviewSdkVersion() string {
659 return String(c.productVariables.Platform_preview_sdk_version)
660}
661
662func (c *config) PlatformMinSupportedTargetSdkVersion() string {
663 return String(c.productVariables.Platform_min_supported_target_sdk_version)
664}
665
666func (c *config) PlatformBaseOS() string {
667 return String(c.productVariables.Platform_base_os)
668}
669
Dan Albert1a246272020-07-06 14:49:35 -0700670func (c *config) MinSupportedSdkVersion() ApiLevel {
671 return uncheckedFinalApiLevel(16)
672}
673
674func (c *config) FinalApiLevels() []ApiLevel {
675 var levels []ApiLevel
Dan Albert4f378d72020-07-23 17:32:15 -0700676 for i := 1; i <= c.PlatformSdkVersion().FinalOrFutureInt(); i++ {
Dan Albert1a246272020-07-06 14:49:35 -0700677 levels = append(levels, uncheckedFinalApiLevel(i))
678 }
679 return levels
680}
681
682func (c *config) PreviewApiLevels() []ApiLevel {
683 var levels []ApiLevel
684 for i, codename := range c.PlatformVersionActiveCodenames() {
685 levels = append(levels, ApiLevel{
686 value: codename,
687 number: i,
688 isPreview: true,
689 })
690 }
691 return levels
692}
693
694func (c *config) AllSupportedApiLevels() []ApiLevel {
695 var levels []ApiLevel
696 levels = append(levels, c.FinalApiLevels()...)
697 return append(levels, c.PreviewApiLevels()...)
Dan Albertf5415d72017-08-17 16:19:59 -0700698}
699
Jingwen Chenc711fec2020-11-22 23:52:50 -0500700// DefaultAppTargetSdk returns the API level that platform apps are targeting.
701// This converts a codename to the exact ApiLevel it represents.
Dan Albert4f378d72020-07-23 17:32:15 -0700702func (c *config) DefaultAppTargetSdk(ctx EarlyModuleContext) ApiLevel {
Colin Crossd09b0b62018-04-18 11:06:47 -0700703 if Bool(c.productVariables.Platform_sdk_final) {
704 return c.PlatformSdkVersion()
Colin Crossd09b0b62018-04-18 11:06:47 -0700705 }
Jingwen Chenc711fec2020-11-22 23:52:50 -0500706 codename := c.PlatformSdkCodename()
707 if codename == "" {
708 return NoneApiLevel
709 }
710 if codename == "REL" {
711 panic("Platform_sdk_codename should not be REL when Platform_sdk_final is true")
712 }
713 return ApiLevelOrPanic(ctx, codename)
Colin Crossd09b0b62018-04-18 11:06:47 -0700714}
715
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800716func (c *config) AppsDefaultVersionName() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800717 return String(c.productVariables.AppsDefaultVersionName)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800718}
719
Dan Albert31384de2017-07-28 12:39:46 -0700720// Codenames that are active in the current lunch target.
721func (c *config) PlatformVersionActiveCodenames() []string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800722 return c.productVariables.Platform_version_active_codenames
Dan Albert31384de2017-07-28 12:39:46 -0700723}
724
Colin Crossface4e42017-10-30 17:32:15 -0700725func (c *config) ProductAAPTConfig() []string {
Colin Crossa74ca042019-01-31 14:31:51 -0800726 return c.productVariables.AAPTConfig
Colin Cross30e076a2015-04-13 13:58:27 -0700727}
728
Colin Crossface4e42017-10-30 17:32:15 -0700729func (c *config) ProductAAPTPreferredConfig() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800730 return String(c.productVariables.AAPTPreferredConfig)
Colin Cross30e076a2015-04-13 13:58:27 -0700731}
732
Colin Crossface4e42017-10-30 17:32:15 -0700733func (c *config) ProductAAPTCharacteristics() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800734 return String(c.productVariables.AAPTCharacteristics)
Colin Crossface4e42017-10-30 17:32:15 -0700735}
736
737func (c *config) ProductAAPTPrebuiltDPI() []string {
Colin Crossa74ca042019-01-31 14:31:51 -0800738 return c.productVariables.AAPTPrebuiltDPI
Colin Cross30e076a2015-04-13 13:58:27 -0700739}
740
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700741func (c *config) DefaultAppCertificateDir(ctx PathContext) SourcePath {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800742 defaultCert := String(c.productVariables.DefaultAppCertificate)
Colin Cross61ae0b72017-12-01 17:16:02 -0800743 if defaultCert != "" {
744 return PathForSource(ctx, filepath.Dir(defaultCert))
Colin Cross61ae0b72017-12-01 17:16:02 -0800745 }
Jingwen Chenc711fec2020-11-22 23:52:50 -0500746 return PathForSource(ctx, "build/make/target/product/security")
Colin Cross30e076a2015-04-13 13:58:27 -0700747}
748
Colin Crosse1731a52017-12-14 11:22:55 -0800749func (c *config) DefaultAppCertificate(ctx PathContext) (pem, key SourcePath) {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800750 defaultCert := String(c.productVariables.DefaultAppCertificate)
Colin Cross61ae0b72017-12-01 17:16:02 -0800751 if defaultCert != "" {
Colin Crosse1731a52017-12-14 11:22:55 -0800752 return PathForSource(ctx, defaultCert+".x509.pem"), PathForSource(ctx, defaultCert+".pk8")
Colin Cross61ae0b72017-12-01 17:16:02 -0800753 }
Jingwen Chenc711fec2020-11-22 23:52:50 -0500754 defaultDir := c.DefaultAppCertificateDir(ctx)
755 return defaultDir.Join(ctx, "testkey.x509.pem"), defaultDir.Join(ctx, "testkey.pk8")
Colin Cross30e076a2015-04-13 13:58:27 -0700756}
Colin Cross6ff51382015-12-17 16:39:19 -0800757
Jiyong Park9335a262018-12-24 11:31:58 +0900758func (c *config) ApexKeyDir(ctx ModuleContext) SourcePath {
759 // TODO(b/121224311): define another variable such as TARGET_APEX_KEY_OVERRIDE
760 defaultCert := String(c.productVariables.DefaultAppCertificate)
Dan Willemsen412160e2019-04-09 21:36:26 -0700761 if defaultCert == "" || filepath.Dir(defaultCert) == "build/make/target/product/security" {
Jiyong Park9335a262018-12-24 11:31:58 +0900762 // When defaultCert is unset or is set to the testkeys path, use the APEX keys
763 // that is under the module dir
Colin Cross07e51612019-03-05 12:46:40 -0800764 return pathForModuleSrc(ctx)
Jiyong Park9335a262018-12-24 11:31:58 +0900765 }
Jingwen Chenc711fec2020-11-22 23:52:50 -0500766 // If not, APEX keys are under the specified directory
767 return PathForSource(ctx, filepath.Dir(defaultCert))
Jiyong Park9335a262018-12-24 11:31:58 +0900768}
769
Jingwen Chenc711fec2020-11-22 23:52:50 -0500770// AllowMissingDependencies configures Blueprint/Soong to not fail when modules
771// are configured to depend on non-existent modules. Note that this does not
772// affect missing input dependencies at the Ninja level.
Colin Cross6ff51382015-12-17 16:39:19 -0800773func (c *config) AllowMissingDependencies() bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800774 return Bool(c.productVariables.Allow_missing_dependencies)
Colin Cross6ff51382015-12-17 16:39:19 -0800775}
Dan Willemsen322acaf2016-01-12 23:07:05 -0800776
Jeongik Cha816a23a2020-07-08 01:09:23 +0900777// Returns true if a full platform source tree cannot be assumed.
Colin Crossfc3674a2017-09-18 17:41:52 -0700778func (c *config) UnbundledBuild() bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800779 return Bool(c.productVariables.Unbundled_build)
Colin Crossfc3674a2017-09-18 17:41:52 -0700780}
781
Martin Stjernholmfd9eb4b2020-06-17 01:13:15 +0100782// Returns true if building apps that aren't bundled with the platform.
783// UnbundledBuild() is always true when this is true.
784func (c *config) UnbundledBuildApps() bool {
785 return Bool(c.productVariables.Unbundled_build_apps)
786}
787
Jeongik Cha816a23a2020-07-08 01:09:23 +0900788// Returns true if building modules against prebuilt SDKs.
789func (c *config) AlwaysUsePrebuiltSdks() bool {
790 return Bool(c.productVariables.Always_use_prebuilt_sdks)
Colin Cross1f367bf2018-12-18 22:46:24 -0800791}
792
Paul Duffin9a89a2a2020-10-28 19:20:06 +0000793// Returns true if the boot jars check should be skipped.
794func (c *config) SkipBootJarsCheck() bool {
795 return Bool(c.productVariables.Skip_boot_jars_check)
796}
797
Doug Horn21b94272019-01-16 12:06:11 -0800798func (c *config) Fuchsia() bool {
799 return Bool(c.productVariables.Fuchsia)
800}
801
Colin Cross126a25c2017-10-31 13:55:34 -0700802func (c *config) MinimizeJavaDebugInfo() bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800803 return Bool(c.productVariables.MinimizeJavaDebugInfo) && !Bool(c.productVariables.Eng)
Colin Cross126a25c2017-10-31 13:55:34 -0700804}
805
Colin Crossed064c02018-09-05 16:28:13 -0700806func (c *config) Debuggable() bool {
807 return Bool(c.productVariables.Debuggable)
808}
809
Jaewoong Jung1d6eb682018-11-29 15:08:44 -0800810func (c *config) Eng() bool {
811 return Bool(c.productVariables.Eng)
812}
813
Jiyong Park8d52f862018-07-07 18:02:07 +0900814func (c *config) DevicePrimaryArchType() ArchType {
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700815 return c.Targets[Android][0].Arch.ArchType
Jiyong Park8d52f862018-07-07 18:02:07 +0900816}
817
Colin Cross16b23492016-01-06 14:41:07 -0800818func (c *config) SanitizeHost() []string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800819 return append([]string(nil), c.productVariables.SanitizeHost...)
Colin Cross16b23492016-01-06 14:41:07 -0800820}
821
822func (c *config) SanitizeDevice() []string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800823 return append([]string(nil), c.productVariables.SanitizeDevice...)
Colin Cross23ae82a2016-11-02 14:34:39 -0700824}
825
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700826func (c *config) SanitizeDeviceDiag() []string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800827 return append([]string(nil), c.productVariables.SanitizeDeviceDiag...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700828}
829
Colin Cross23ae82a2016-11-02 14:34:39 -0700830func (c *config) SanitizeDeviceArch() []string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800831 return append([]string(nil), c.productVariables.SanitizeDeviceArch...)
Colin Cross16b23492016-01-06 14:41:07 -0800832}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700833
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800834func (c *config) EnableCFI() bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800835 if c.productVariables.EnableCFI == nil {
Vishwath Mohanc32c3eb2017-01-24 14:20:54 -0800836 return true
Vishwath Mohanc32c3eb2017-01-24 14:20:54 -0800837 }
Jingwen Chenc711fec2020-11-22 23:52:50 -0500838 return *c.productVariables.EnableCFI
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800839}
840
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800841func (c *config) DisableScudo() bool {
842 return Bool(c.productVariables.DisableScudo)
843}
844
Colin Crossa1ad8d12016-06-01 17:09:44 -0700845func (c *config) Android64() bool {
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700846 for _, t := range c.Targets[Android] {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700847 if t.Arch.ArchType.Multilib == "lib64" {
848 return true
849 }
850 }
851
852 return false
853}
Colin Cross9272ade2016-08-17 15:24:12 -0700854
Colin Cross9d45bb72016-08-29 16:14:13 -0700855func (c *config) UseGoma() bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800856 return Bool(c.productVariables.UseGoma)
Colin Cross9d45bb72016-08-29 16:14:13 -0700857}
858
Ramy Medhatbbf25672019-07-17 12:30:04 +0000859func (c *config) UseRBE() bool {
860 return Bool(c.productVariables.UseRBE)
861}
862
Ramy Medhat8ea054a2020-01-27 14:19:44 -0500863func (c *config) UseRBEJAVAC() bool {
864 return Bool(c.productVariables.UseRBEJAVAC)
865}
866
867func (c *config) UseRBER8() bool {
868 return Bool(c.productVariables.UseRBER8)
869}
870
871func (c *config) UseRBED8() bool {
872 return Bool(c.productVariables.UseRBED8)
873}
874
Colin Cross8b8bec32019-11-15 13:18:43 -0800875func (c *config) UseRemoteBuild() bool {
876 return c.UseGoma() || c.UseRBE()
877}
878
Colin Cross66548102018-06-19 22:47:35 -0700879func (c *config) RunErrorProne() bool {
880 return c.IsEnvTrue("RUN_ERROR_PRONE")
881}
882
Jingwen Chenc711fec2020-11-22 23:52:50 -0500883// XrefCorpusName returns the Kythe cross-reference corpus name.
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800884func (c *config) XrefCorpusName() string {
885 return c.Getenv("XREF_CORPUS")
886}
887
Jingwen Chenc711fec2020-11-22 23:52:50 -0500888// XrefCuEncoding returns the compilation unit encoding to use for Kythe code
889// xrefs. Can be 'json' (default), 'proto' or 'all'.
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800890func (c *config) XrefCuEncoding() string {
891 if enc := c.Getenv("KYTHE_KZIP_ENCODING"); enc != "" {
892 return enc
893 }
894 return "json"
895}
896
Sasha Smundakb0addaf2021-02-16 10:39:40 -0800897// XrefCuJavaSourceMax returns the maximum number of the Java source files
898// in a single compilation unit
899const xrefJavaSourceFileMaxDefault = "1000"
900
901func (c Config) XrefCuJavaSourceMax() string {
902 v := c.Getenv("KYTHE_JAVA_SOURCE_BATCH_SIZE")
903 if v == "" {
904 return xrefJavaSourceFileMaxDefault
905 }
906 if _, err := strconv.ParseUint(v, 0, 0); err != nil {
907 fmt.Fprintf(os.Stderr,
908 "bad KYTHE_JAVA_SOURCE_BATCH_SIZE value: %s, will use %s",
909 err, xrefJavaSourceFileMaxDefault)
910 return xrefJavaSourceFileMaxDefault
911 }
912 return v
913
914}
915
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800916func (c *config) EmitXrefRules() bool {
917 return c.XrefCorpusName() != ""
918}
919
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700920func (c *config) ClangTidy() bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800921 return Bool(c.productVariables.ClangTidy)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700922}
923
924func (c *config) TidyChecks() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800925 if c.productVariables.TidyChecks == nil {
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700926 return ""
927 }
Dan Willemsen45133ac2018-03-09 21:22:06 -0800928 return *c.productVariables.TidyChecks
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700929}
930
Colin Cross0f4e0d62016-07-27 10:56:55 -0700931func (c *config) LibartImgHostBaseAddress() string {
932 return "0x60000000"
933}
934
935func (c *config) LibartImgDeviceBaseAddress() string {
Elliott Hughesda3a0712020-03-06 16:55:28 -0800936 return "0x70000000"
Colin Cross0f4e0d62016-07-27 10:56:55 -0700937}
938
Hiroshi Yamauchie2a10632016-12-19 13:44:41 -0800939func (c *config) ArtUseReadBarrier() bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800940 return Bool(c.productVariables.ArtUseReadBarrier)
Hiroshi Yamauchie2a10632016-12-19 13:44:41 -0800941}
942
Jingwen Chenc711fec2020-11-22 23:52:50 -0500943// Enforce Runtime Resource Overlays for a module. RROs supersede static RROs,
944// but some modules still depend on it.
945//
946// More info: https://source.android.com/devices/architecture/rros
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700947func (c *config) EnforceRROForModule(name string) bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800948 enforceList := c.productVariables.EnforceRROTargets
Jeongik Chacee5ba92021-02-19 12:11:51 +0900949
Roland Levillainf6cc2612020-07-09 16:58:14 +0100950 if len(enforceList) > 0 {
Yo Chiang4ebd06a2019-10-01 13:13:41 +0800951 if InList("*", enforceList) {
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700952 return true
953 }
Colin Crossa74ca042019-01-31 14:31:51 -0800954 return InList(name, enforceList)
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700955 }
956 return false
957}
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700958func (c *config) EnforceRROExcludedOverlay(path string) bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800959 excluded := c.productVariables.EnforceRROExcludedOverlays
Roland Levillainf6cc2612020-07-09 16:58:14 +0100960 if len(excluded) > 0 {
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800961 return HasAnyPrefix(path, excluded)
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700962 }
963 return false
964}
965
966func (c *config) ExportedNamespaces() []string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800967 return append([]string(nil), c.productVariables.NamespacesToExport...)
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700968}
969
970func (c *config) HostStaticBinaries() bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800971 return Bool(c.productVariables.HostStaticBinaries)
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700972}
973
Colin Cross5a0dcd52018-10-05 14:20:06 -0700974func (c *config) UncompressPrivAppDex() bool {
975 return Bool(c.productVariables.UncompressPrivAppDex)
976}
977
978func (c *config) ModulesLoadedByPrivilegedModules() []string {
979 return c.productVariables.ModulesLoadedByPrivilegedModules
980}
981
Jingwen Chenc711fec2020-11-22 23:52:50 -0500982// DexpreoptGlobalConfigPath returns the path to the dexpreopt.config file in
983// the output directory, if it was created during the product configuration
984// phase by Kati.
Jingwen Chenebb0b572020-11-02 00:24:57 -0500985func (c *config) DexpreoptGlobalConfigPath(ctx PathContext) OptionalPath {
Colin Cross988414c2020-01-11 01:11:46 +0000986 if c.productVariables.DexpreoptGlobalConfig == nil {
Jingwen Chenebb0b572020-11-02 00:24:57 -0500987 return OptionalPathForPath(nil)
988 }
989 return OptionalPathForPath(
990 pathForBuildToolDep(ctx, *c.productVariables.DexpreoptGlobalConfig))
991}
992
Jingwen Chenc711fec2020-11-22 23:52:50 -0500993// DexpreoptGlobalConfig returns the raw byte contents of the dexpreopt global
994// configuration. Since the configuration file was created by Kati during
995// product configuration (externally of soong_build), it's not tracked, so we
996// also manually add a Ninja file dependency on the configuration file to the
997// rule that creates the main build.ninja file. This ensures that build.ninja is
998// regenerated correctly if dexpreopt.config changes.
Jingwen Chenebb0b572020-11-02 00:24:57 -0500999func (c *config) DexpreoptGlobalConfig(ctx PathContext) ([]byte, error) {
1000 path := c.DexpreoptGlobalConfigPath(ctx)
1001 if !path.Valid() {
Colin Cross988414c2020-01-11 01:11:46 +00001002 return nil, nil
1003 }
Jingwen Chenebb0b572020-11-02 00:24:57 -05001004 ctx.AddNinjaFileDeps(path.String())
1005 return ioutil.ReadFile(absolutePath(path.String()))
Colin Cross43f08db2018-11-12 10:13:39 -08001006}
1007
Inseob Kim7b85eeb2021-03-23 20:52:24 +09001008func (c *deviceConfig) WithDexpreopt() bool {
1009 return c.config.productVariables.WithDexpreopt
1010}
1011
David Brazdil91b4e3e2019-01-23 21:04:05 +00001012func (c *config) FrameworksBaseDirExists(ctx PathContext) bool {
Colin Cross5a756a62021-03-16 16:34:46 -07001013 return ExistentPathForSource(ctx, "frameworks", "base", "Android.bp").Valid()
David Brazdil91b4e3e2019-01-23 21:04:05 +00001014}
1015
Inseob Kimae553032019-05-14 18:52:49 +09001016func (c *config) VndkSnapshotBuildArtifacts() bool {
1017 return Bool(c.productVariables.VndkSnapshotBuildArtifacts)
1018}
1019
Colin Cross3b19f5d2019-09-17 14:45:31 -07001020func (c *config) HasMultilibConflict(arch ArchType) bool {
1021 return c.multilibConflicts[arch]
1022}
1023
Bill Peckhambae47492021-01-08 09:34:44 -08001024func (c *config) PrebuiltHiddenApiDir(ctx PathContext) string {
1025 return String(c.productVariables.PrebuiltHiddenApiDir)
1026}
1027
Colin Cross9272ade2016-08-17 15:24:12 -07001028func (c *deviceConfig) Arches() []Arch {
1029 var arches []Arch
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001030 for _, target := range c.config.Targets[Android] {
Colin Cross9272ade2016-08-17 15:24:12 -07001031 arches = append(arches, target.Arch)
1032 }
1033 return arches
1034}
Dan Willemsend2ede872016-11-18 14:54:24 -08001035
Jayant Chowdhary34ce67d2018-03-08 11:00:50 -08001036func (c *deviceConfig) BinderBitness() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001037 is32BitBinder := c.config.productVariables.Binder32bit
Jayant Chowdhary34ce67d2018-03-08 11:00:50 -08001038 if is32BitBinder != nil && *is32BitBinder {
1039 return "32"
1040 }
1041 return "64"
1042}
1043
Dan Willemsen4353bc42016-12-05 17:16:02 -08001044func (c *deviceConfig) VendorPath() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001045 if c.config.productVariables.VendorPath != nil {
1046 return *c.config.productVariables.VendorPath
Dan Willemsen4353bc42016-12-05 17:16:02 -08001047 }
1048 return "vendor"
1049}
1050
Justin Yun71549282017-11-17 12:10:28 +09001051func (c *deviceConfig) VndkVersion() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001052 return String(c.config.productVariables.DeviceVndkVersion)
Justin Yun71549282017-11-17 12:10:28 +09001053}
1054
Jose Galmes6f843bc2020-12-11 13:36:29 -08001055func (c *deviceConfig) RecoverySnapshotVersion() string {
1056 return String(c.config.productVariables.RecoverySnapshotVersion)
1057}
1058
Jeongik Cha219141c2020-08-06 23:00:37 +09001059func (c *deviceConfig) CurrentApiLevelForVendorModules() string {
1060 return StringDefault(c.config.productVariables.DeviceCurrentApiLevelForVendorModules, "current")
1061}
1062
Justin Yun8fe12122017-12-07 17:18:15 +09001063func (c *deviceConfig) PlatformVndkVersion() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001064 return String(c.config.productVariables.Platform_vndk_version)
Justin Yun8fe12122017-12-07 17:18:15 +09001065}
1066
Justin Yun5f7f7e82019-11-18 19:52:14 +09001067func (c *deviceConfig) ProductVndkVersion() string {
1068 return String(c.config.productVariables.ProductVndkVersion)
1069}
1070
Justin Yun71549282017-11-17 12:10:28 +09001071func (c *deviceConfig) ExtraVndkVersions() []string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001072 return c.config.productVariables.ExtraVndkVersions
Dan Willemsend2ede872016-11-18 14:54:24 -08001073}
Jack He8cc71432016-12-08 15:45:07 -08001074
Vic Yangefd249e2018-11-12 20:19:56 -08001075func (c *deviceConfig) VndkUseCoreVariant() bool {
1076 return Bool(c.config.productVariables.VndkUseCoreVariant)
1077}
1078
Jiyong Park1a5d7b12018-01-15 15:05:10 +09001079func (c *deviceConfig) SystemSdkVersions() []string {
Colin Crossa74ca042019-01-31 14:31:51 -08001080 return c.config.productVariables.DeviceSystemSdkVersions
Jiyong Park1a5d7b12018-01-15 15:05:10 +09001081}
1082
1083func (c *deviceConfig) PlatformSystemSdkVersions() []string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001084 return c.config.productVariables.Platform_systemsdk_versions
Jiyong Park1a5d7b12018-01-15 15:05:10 +09001085}
1086
Jiyong Park2db76922017-11-08 16:03:48 +09001087func (c *deviceConfig) OdmPath() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001088 if c.config.productVariables.OdmPath != nil {
1089 return *c.config.productVariables.OdmPath
Jiyong Park2db76922017-11-08 16:03:48 +09001090 }
1091 return "odm"
1092}
1093
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +09001094func (c *deviceConfig) ProductPath() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001095 if c.config.productVariables.ProductPath != nil {
1096 return *c.config.productVariables.ProductPath
Jiyong Park2db76922017-11-08 16:03:48 +09001097 }
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +09001098 return "product"
Jiyong Park2db76922017-11-08 16:03:48 +09001099}
1100
Justin Yund5f6c822019-06-25 16:47:17 +09001101func (c *deviceConfig) SystemExtPath() string {
1102 if c.config.productVariables.SystemExtPath != nil {
1103 return *c.config.productVariables.SystemExtPath
Dario Frenifd05a742018-05-29 13:28:54 +01001104 }
Justin Yund5f6c822019-06-25 16:47:17 +09001105 return "system_ext"
Dario Frenifd05a742018-05-29 13:28:54 +01001106}
1107
Jack He8cc71432016-12-08 15:45:07 -08001108func (c *deviceConfig) BtConfigIncludeDir() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001109 return String(c.config.productVariables.BtConfigIncludeDir)
Jack He8cc71432016-12-08 15:45:07 -08001110}
Dan Willemsen581341d2017-02-09 16:16:31 -08001111
Jiyong Parkd773eb32017-07-03 13:18:12 +09001112func (c *deviceConfig) DeviceKernelHeaderDirs() []string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001113 return c.config.productVariables.DeviceKernelHeaders
Jiyong Parkd773eb32017-07-03 13:18:12 +09001114}
1115
Yi Kongceb5b762020-03-20 15:22:27 +08001116func (c *deviceConfig) SamplingPGO() bool {
1117 return Bool(c.config.productVariables.SamplingPGO)
1118}
1119
Roland Levillainada12702020-06-09 13:07:36 +01001120// JavaCoverageEnabledForPath returns whether Java code coverage is enabled for
1121// path. Coverage is enabled by default when the product variable
1122// JavaCoveragePaths is empty. If JavaCoveragePaths is not empty, coverage is
1123// enabled for any path which is part of this variable (and not part of the
1124// JavaCoverageExcludePaths product variable). Value "*" in JavaCoveragePaths
1125// represents any path.
1126func (c *deviceConfig) JavaCoverageEnabledForPath(path string) bool {
1127 coverage := false
Chris Gross2f748692020-06-24 20:36:59 +00001128 if len(c.config.productVariables.JavaCoveragePaths) == 0 ||
Roland Levillainada12702020-06-09 13:07:36 +01001129 InList("*", c.config.productVariables.JavaCoveragePaths) ||
1130 HasAnyPrefix(path, c.config.productVariables.JavaCoveragePaths) {
1131 coverage = true
1132 }
Roland Levillainf6cc2612020-07-09 16:58:14 +01001133 if coverage && len(c.config.productVariables.JavaCoverageExcludePaths) > 0 {
Roland Levillainada12702020-06-09 13:07:36 +01001134 if HasAnyPrefix(path, c.config.productVariables.JavaCoverageExcludePaths) {
1135 coverage = false
1136 }
1137 }
1138 return coverage
1139}
1140
Colin Cross1a6acd42020-06-16 17:51:46 -07001141// Returns true if gcov or clang coverage is enabled.
Dan Willemsen581341d2017-02-09 16:16:31 -08001142func (c *deviceConfig) NativeCoverageEnabled() bool {
Colin Cross1a6acd42020-06-16 17:51:46 -07001143 return Bool(c.config.productVariables.GcovCoverage) ||
1144 Bool(c.config.productVariables.ClangCoverage)
Dan Willemsen581341d2017-02-09 16:16:31 -08001145}
1146
Oliver Nguyen1382ab62019-12-06 15:22:41 -08001147func (c *deviceConfig) ClangCoverageEnabled() bool {
1148 return Bool(c.config.productVariables.ClangCoverage)
1149}
1150
Colin Cross1a6acd42020-06-16 17:51:46 -07001151func (c *deviceConfig) GcovCoverageEnabled() bool {
1152 return Bool(c.config.productVariables.GcovCoverage)
1153}
1154
Roland Levillain4f5297b2020-06-09 12:44:06 +01001155// NativeCoverageEnabledForPath returns whether (GCOV- or Clang-based) native
1156// code coverage is enabled for path. By default, coverage is not enabled for a
1157// given path unless it is part of the NativeCoveragePaths product variable (and
1158// not part of the NativeCoverageExcludePaths product variable). Value "*" in
1159// NativeCoveragePaths represents any path.
1160func (c *deviceConfig) NativeCoverageEnabledForPath(path string) bool {
Ryan Campbell469a18a2017-02-27 09:01:54 -08001161 coverage := false
Roland Levillainf6cc2612020-07-09 16:58:14 +01001162 if len(c.config.productVariables.NativeCoveragePaths) > 0 {
Roland Levillain4f5297b2020-06-09 12:44:06 +01001163 if InList("*", c.config.productVariables.NativeCoveragePaths) || HasAnyPrefix(path, c.config.productVariables.NativeCoveragePaths) {
Ivan Lozano5f595532017-07-13 14:46:05 -07001164 coverage = true
Dan Willemsen581341d2017-02-09 16:16:31 -08001165 }
1166 }
Roland Levillainf6cc2612020-07-09 16:58:14 +01001167 if coverage && len(c.config.productVariables.NativeCoverageExcludePaths) > 0 {
Roland Levillain4f5297b2020-06-09 12:44:06 +01001168 if HasAnyPrefix(path, c.config.productVariables.NativeCoverageExcludePaths) {
Ivan Lozano5f595532017-07-13 14:46:05 -07001169 coverage = false
Ryan Campbell469a18a2017-02-27 09:01:54 -08001170 }
1171 }
1172 return coverage
Dan Willemsen581341d2017-02-09 16:16:31 -08001173}
Ivan Lozano5f595532017-07-13 14:46:05 -07001174
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -08001175func (c *deviceConfig) PgoAdditionalProfileDirs() []string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001176 return c.config.productVariables.PgoAdditionalProfileDirs
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -08001177}
1178
Tri Vo35a51432018-03-25 20:00:00 -07001179func (c *deviceConfig) VendorSepolicyDirs() []string {
1180 return c.config.productVariables.BoardVendorSepolicyDirs
1181}
1182
1183func (c *deviceConfig) OdmSepolicyDirs() []string {
1184 return c.config.productVariables.BoardOdmSepolicyDirs
1185}
1186
Felixa20a8752020-05-17 18:28:35 +02001187func (c *deviceConfig) SystemExtPublicSepolicyDirs() []string {
1188 return c.config.productVariables.SystemExtPublicSepolicyDirs
Tri Vo35a51432018-03-25 20:00:00 -07001189}
1190
Felixa20a8752020-05-17 18:28:35 +02001191func (c *deviceConfig) SystemExtPrivateSepolicyDirs() []string {
1192 return c.config.productVariables.SystemExtPrivateSepolicyDirs
Tri Vo35a51432018-03-25 20:00:00 -07001193}
1194
Inseob Kim0866b002019-04-15 20:21:29 +09001195func (c *deviceConfig) SepolicyM4Defs() []string {
1196 return c.config.productVariables.BoardSepolicyM4Defs
1197}
1198
Jiyong Park7f67f482019-01-05 12:57:48 +09001199func (c *deviceConfig) OverrideManifestPackageNameFor(name string) (manifestName string, overridden bool) {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001200 return findOverrideValue(c.config.productVariables.ManifestPackageNameOverrides, name,
1201 "invalid override rule %q in PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES should be <module_name>:<manifest_name>")
1202}
1203
1204func (c *deviceConfig) OverrideCertificateFor(name string) (certificatePath string, overridden bool) {
Jaewoong Jungacb6db32019-02-28 16:22:30 +00001205 return findOverrideValue(c.config.productVariables.CertificateOverrides, name,
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001206 "invalid override rule %q in PRODUCT_CERTIFICATE_OVERRIDES should be <module_name>:<certificate_module_name>")
1207}
1208
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001209func (c *deviceConfig) OverridePackageNameFor(name string) string {
1210 newName, overridden := findOverrideValue(
1211 c.config.productVariables.PackageNameOverrides,
1212 name,
1213 "invalid override rule %q in PRODUCT_PACKAGE_NAME_OVERRIDES should be <module_name>:<package_name>")
1214 if overridden {
1215 return newName
1216 }
1217 return name
1218}
1219
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001220func findOverrideValue(overrides []string, name string, errorMsg string) (newValue string, overridden bool) {
Jiyong Park7f67f482019-01-05 12:57:48 +09001221 if overrides == nil || len(overrides) == 0 {
1222 return "", false
1223 }
1224 for _, o := range overrides {
1225 split := strings.Split(o, ":")
1226 if len(split) != 2 {
1227 // This shouldn't happen as this is first checked in make, but just in case.
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001228 panic(fmt.Errorf(errorMsg, o))
Jiyong Park7f67f482019-01-05 12:57:48 +09001229 }
1230 if matchPattern(split[0], name) {
1231 return substPattern(split[0], split[1], name), true
1232 }
1233 }
1234 return "", false
1235}
1236
Ivan Lozano5f595532017-07-13 14:46:05 -07001237func (c *config) IntegerOverflowDisabledForPath(path string) bool {
Roland Levillainf6cc2612020-07-09 16:58:14 +01001238 if len(c.productVariables.IntegerOverflowExcludePaths) == 0 {
Ivan Lozano5f595532017-07-13 14:46:05 -07001239 return false
1240 }
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001241 return HasAnyPrefix(path, c.productVariables.IntegerOverflowExcludePaths)
Ivan Lozano5f595532017-07-13 14:46:05 -07001242}
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -07001243
1244func (c *config) CFIDisabledForPath(path string) bool {
Roland Levillainf6cc2612020-07-09 16:58:14 +01001245 if len(c.productVariables.CFIExcludePaths) == 0 {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -07001246 return false
1247 }
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001248 return HasAnyPrefix(path, c.productVariables.CFIExcludePaths)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -07001249}
1250
1251func (c *config) CFIEnabledForPath(path string) bool {
Roland Levillainf6cc2612020-07-09 16:58:14 +01001252 if len(c.productVariables.CFIIncludePaths) == 0 {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -07001253 return false
1254 }
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001255 return HasAnyPrefix(path, c.productVariables.CFIIncludePaths)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -07001256}
Colin Crosse15ddaf2017-12-04 11:24:31 -08001257
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08001258func (c *config) MemtagHeapDisabledForPath(path string) bool {
1259 if len(c.productVariables.MemtagHeapExcludePaths) == 0 {
1260 return false
1261 }
1262 return HasAnyPrefix(path, c.productVariables.MemtagHeapExcludePaths)
1263}
1264
1265func (c *config) MemtagHeapAsyncEnabledForPath(path string) bool {
1266 if len(c.productVariables.MemtagHeapAsyncIncludePaths) == 0 {
1267 return false
1268 }
1269 return HasAnyPrefix(path, c.productVariables.MemtagHeapAsyncIncludePaths)
1270}
1271
1272func (c *config) MemtagHeapSyncEnabledForPath(path string) bool {
1273 if len(c.productVariables.MemtagHeapSyncIncludePaths) == 0 {
1274 return false
1275 }
1276 return HasAnyPrefix(path, c.productVariables.MemtagHeapSyncIncludePaths)
1277}
1278
Dan Willemsen0fe78662018-03-26 12:41:18 -07001279func (c *config) VendorConfig(name string) VendorConfig {
Colin Cross9d34f352019-11-22 16:03:51 -08001280 return soongconfig.Config(c.productVariables.VendorVars[name])
Dan Willemsen0fe78662018-03-26 12:41:18 -07001281}
1282
Colin Cross395f2cf2018-10-24 16:10:32 -07001283func (c *config) NdkAbis() bool {
1284 return Bool(c.productVariables.Ndk_abis)
1285}
1286
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001287func (c *config) AmlAbis() bool {
1288 return Bool(c.productVariables.Aml_abis)
1289}
1290
Dan Albert23d37e02018-11-28 08:30:10 -08001291func (c *config) ExcludeDraftNdkApis() bool {
1292 return Bool(c.productVariables.Exclude_draft_ndk_apis)
1293}
1294
Jiyong Park8fd61922018-11-08 02:50:25 +09001295func (c *config) FlattenApex() bool {
Roland Levillaina3863212019-08-12 19:56:16 +01001296 return Bool(c.productVariables.Flatten_apex)
Jiyong Park8fd61922018-11-08 02:50:25 +09001297}
1298
Jiyong Park4da07972021-01-05 21:01:11 +09001299func (c *config) ForceApexSymlinkOptimization() bool {
1300 return Bool(c.productVariables.ForceApexSymlinkOptimization)
1301}
1302
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00001303func (c *config) CompressedApex() bool {
1304 return Bool(c.productVariables.CompressedApex)
1305}
1306
Jeongik Chac9464142019-01-07 12:07:27 +09001307func (c *config) EnforceSystemCertificate() bool {
1308 return Bool(c.productVariables.EnforceSystemCertificate)
1309}
1310
Colin Cross440e0d02020-06-11 11:32:11 -07001311func (c *config) EnforceSystemCertificateAllowList() []string {
1312 return c.productVariables.EnforceSystemCertificateAllowList
Jeongik Chac9464142019-01-07 12:07:27 +09001313}
1314
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001315func (c *config) EnforceProductPartitionInterface() bool {
1316 return Bool(c.productVariables.EnforceProductPartitionInterface)
1317}
1318
JaeMan Parkff715562020-10-19 17:25:58 +09001319func (c *config) EnforceInterPartitionJavaSdkLibrary() bool {
1320 return Bool(c.productVariables.EnforceInterPartitionJavaSdkLibrary)
1321}
1322
1323func (c *config) InterPartitionJavaLibraryAllowList() []string {
1324 return c.productVariables.InterPartitionJavaLibraryAllowList
1325}
1326
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09001327func (c *config) InstallExtraFlattenedApexes() bool {
1328 return Bool(c.productVariables.InstallExtraFlattenedApexes)
1329}
1330
Colin Crossf24a22a2019-01-31 14:12:44 -08001331func (c *config) ProductHiddenAPIStubs() []string {
1332 return c.productVariables.ProductHiddenAPIStubs
Colin Cross8faf8fc2019-01-16 15:15:52 -08001333}
1334
Colin Crossf24a22a2019-01-31 14:12:44 -08001335func (c *config) ProductHiddenAPIStubsSystem() []string {
1336 return c.productVariables.ProductHiddenAPIStubsSystem
Colin Cross8faf8fc2019-01-16 15:15:52 -08001337}
1338
Colin Crossf24a22a2019-01-31 14:12:44 -08001339func (c *config) ProductHiddenAPIStubsTest() []string {
1340 return c.productVariables.ProductHiddenAPIStubsTest
Colin Cross8faf8fc2019-01-16 15:15:52 -08001341}
Dan Willemsen71c74602019-04-10 12:27:35 -07001342
Dan Willemsen54879d12019-04-18 10:08:46 -07001343func (c *deviceConfig) TargetFSConfigGen() []string {
Dan Willemsen71c74602019-04-10 12:27:35 -07001344 return c.config.productVariables.TargetFSConfigGen
1345}
Inseob Kim0866b002019-04-15 20:21:29 +09001346
1347func (c *config) ProductPublicSepolicyDirs() []string {
1348 return c.productVariables.ProductPublicSepolicyDirs
1349}
1350
1351func (c *config) ProductPrivateSepolicyDirs() []string {
1352 return c.productVariables.ProductPrivateSepolicyDirs
1353}
1354
Colin Cross50ddcc42019-05-16 12:28:22 -07001355func (c *config) MissingUsesLibraries() []string {
1356 return c.productVariables.MissingUsesLibraries
1357}
1358
Inseob Kim1f086e22019-05-09 13:29:15 +09001359func (c *deviceConfig) DeviceArch() string {
1360 return String(c.config.productVariables.DeviceArch)
1361}
1362
1363func (c *deviceConfig) DeviceArchVariant() string {
1364 return String(c.config.productVariables.DeviceArchVariant)
1365}
1366
1367func (c *deviceConfig) DeviceSecondaryArch() string {
1368 return String(c.config.productVariables.DeviceSecondaryArch)
1369}
1370
1371func (c *deviceConfig) DeviceSecondaryArchVariant() string {
1372 return String(c.config.productVariables.DeviceSecondaryArchVariant)
1373}
Yifan Hong82db7352020-01-21 16:12:26 -08001374
1375func (c *deviceConfig) BoardUsesRecoveryAsBoot() bool {
1376 return Bool(c.config.productVariables.BoardUsesRecoveryAsBoot)
1377}
Yifan Hong97365ee2020-07-29 09:51:57 -07001378
1379func (c *deviceConfig) BoardKernelBinaries() []string {
1380 return c.config.productVariables.BoardKernelBinaries
1381}
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001382
Yifan Hong42bef8d2020-08-05 14:36:09 -07001383func (c *deviceConfig) BoardKernelModuleInterfaceVersions() []string {
1384 return c.config.productVariables.BoardKernelModuleInterfaceVersions
1385}
1386
Yifan Hongdd8dacc2020-10-21 15:40:17 -07001387func (c *deviceConfig) BoardMoveRecoveryResourcesToVendorBoot() bool {
1388 return Bool(c.config.productVariables.BoardMoveRecoveryResourcesToVendorBoot)
1389}
1390
Inseob Kim16ebd5a2020-12-09 23:08:17 +09001391func (c *deviceConfig) PlatformSepolicyVersion() string {
1392 return String(c.config.productVariables.PlatformSepolicyVersion)
1393}
1394
1395func (c *deviceConfig) BoardSepolicyVers() string {
Inseob Kim0c4eec82021-03-22 22:33:40 +09001396 if ver := String(c.config.productVariables.BoardSepolicyVers); ver != "" {
1397 return ver
1398 }
1399 return c.PlatformSepolicyVersion()
Inseob Kim16ebd5a2020-12-09 23:08:17 +09001400}
1401
1402func (c *deviceConfig) BoardReqdMaskPolicy() []string {
1403 return c.config.productVariables.BoardReqdMaskPolicy
1404}
1405
Inseob Kim7cf14652021-01-06 23:06:52 +09001406func (c *deviceConfig) DirectedVendorSnapshot() bool {
1407 return c.config.productVariables.DirectedVendorSnapshot
1408}
1409
1410func (c *deviceConfig) VendorSnapshotModules() map[string]bool {
1411 return c.config.productVariables.VendorSnapshotModules
1412}
1413
Jose Galmes4c6895e2021-02-09 07:44:30 -08001414func (c *deviceConfig) DirectedRecoverySnapshot() bool {
1415 return c.config.productVariables.DirectedRecoverySnapshot
1416}
1417
1418func (c *deviceConfig) RecoverySnapshotModules() map[string]bool {
1419 return c.config.productVariables.RecoverySnapshotModules
1420}
1421
Justin DeMartino383bfb32021-02-24 10:49:43 -08001422func createDirsMap(previous map[string]bool, dirs []string) (map[string]bool, error) {
1423 var ret = make(map[string]bool)
1424 for _, dir := range dirs {
1425 clean := filepath.Clean(dir)
1426 if previous[clean] || ret[clean] {
1427 return nil, fmt.Errorf("Duplicate entry %s", dir)
1428 }
1429 ret[clean] = true
1430 }
1431 return ret, nil
1432}
1433
1434func (c *deviceConfig) createDirsMapOnce(onceKey OnceKey, previous map[string]bool, dirs []string) map[string]bool {
1435 dirMap := c.Once(onceKey, func() interface{} {
1436 ret, err := createDirsMap(previous, dirs)
1437 if err != nil {
1438 panic(fmt.Errorf("%s: %w", onceKey.key, err))
1439 }
1440 return ret
1441 })
1442 if dirMap == nil {
1443 return nil
1444 }
1445 return dirMap.(map[string]bool)
1446}
1447
1448var vendorSnapshotDirsExcludedKey = NewOnceKey("VendorSnapshotDirsExcludedMap")
1449
1450func (c *deviceConfig) VendorSnapshotDirsExcludedMap() map[string]bool {
1451 return c.createDirsMapOnce(vendorSnapshotDirsExcludedKey, nil,
1452 c.config.productVariables.VendorSnapshotDirsExcluded)
1453}
1454
1455var vendorSnapshotDirsIncludedKey = NewOnceKey("VendorSnapshotDirsIncludedMap")
1456
1457func (c *deviceConfig) VendorSnapshotDirsIncludedMap() map[string]bool {
1458 excludedMap := c.VendorSnapshotDirsExcludedMap()
1459 return c.createDirsMapOnce(vendorSnapshotDirsIncludedKey, excludedMap,
1460 c.config.productVariables.VendorSnapshotDirsIncluded)
1461}
1462
1463var recoverySnapshotDirsExcludedKey = NewOnceKey("RecoverySnapshotDirsExcludedMap")
1464
1465func (c *deviceConfig) RecoverySnapshotDirsExcludedMap() map[string]bool {
1466 return c.createDirsMapOnce(recoverySnapshotDirsExcludedKey, nil,
1467 c.config.productVariables.RecoverySnapshotDirsExcluded)
1468}
1469
1470var recoverySnapshotDirsIncludedKey = NewOnceKey("RecoverySnapshotDirsIncludedMap")
1471
1472func (c *deviceConfig) RecoverySnapshotDirsIncludedMap() map[string]bool {
1473 excludedMap := c.RecoverySnapshotDirsExcludedMap()
1474 return c.createDirsMapOnce(recoverySnapshotDirsIncludedKey, excludedMap,
1475 c.config.productVariables.RecoverySnapshotDirsIncluded)
1476}
1477
Inseob Kim60c32f02020-12-21 22:53:05 +09001478func (c *deviceConfig) ShippingApiLevel() ApiLevel {
1479 if c.config.productVariables.ShippingApiLevel == nil {
1480 return NoneApiLevel
1481 }
1482 apiLevel, _ := strconv.Atoi(*c.config.productVariables.ShippingApiLevel)
1483 return uncheckedFinalApiLevel(apiLevel)
1484}
1485
Inseob Kim67e5add192021-03-17 18:05:33 +09001486func (c *deviceConfig) BuildBrokenEnforceSyspropOwner() bool {
1487 return c.config.productVariables.BuildBrokenEnforceSyspropOwner
1488}
1489
1490func (c *deviceConfig) BuildBrokenTrebleSyspropNeverallow() bool {
1491 return c.config.productVariables.BuildBrokenTrebleSyspropNeverallow
1492}
1493
Inseob Kim0cac7b42021-02-03 18:16:46 +09001494func (c *deviceConfig) BuildBrokenVendorPropertyNamespace() bool {
1495 return c.config.productVariables.BuildBrokenVendorPropertyNamespace
1496}
1497
Inseob Kim67e5add192021-03-17 18:05:33 +09001498func (c *deviceConfig) RequiresInsecureExecmemForSwiftshader() bool {
1499 return c.config.productVariables.RequiresInsecureExecmemForSwiftshader
1500}
1501
1502func (c *config) SelinuxIgnoreNeverallows() bool {
1503 return c.productVariables.SelinuxIgnoreNeverallows
1504}
1505
1506func (c *deviceConfig) SepolicySplit() bool {
1507 return c.config.productVariables.SepolicySplit
1508}
1509
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001510// The ConfiguredJarList struct provides methods for handling a list of (apex, jar) pairs.
1511// Such lists are used in the build system for things like bootclasspath jars or system server jars.
1512// The apex part is either an apex name, or a special names "platform" or "system_ext". Jar is a
1513// module name. The pairs come from Make product variables as a list of colon-separated strings.
1514//
1515// Examples:
1516// - "com.android.art:core-oj"
1517// - "platform:framework"
1518// - "system_ext:foo"
1519//
1520type ConfiguredJarList struct {
Jingwen Chenc711fec2020-11-22 23:52:50 -05001521 // A list of apex components, which can be an apex name,
1522 // or special names like "platform" or "system_ext".
1523 apexes []string
1524
1525 // A list of jar module name components.
1526 jars []string
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001527}
1528
Jingwen Chenc711fec2020-11-22 23:52:50 -05001529// Len returns the length of the list of jars.
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001530func (l *ConfiguredJarList) Len() int {
1531 return len(l.jars)
1532}
1533
Jingwen Chenc711fec2020-11-22 23:52:50 -05001534// Jar returns the idx-th jar component of (apex, jar) pairs.
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001535func (l *ConfiguredJarList) Jar(idx int) string {
1536 return l.jars[idx]
1537}
1538
Jingwen Chenc711fec2020-11-22 23:52:50 -05001539// Apex returns the idx-th apex component of (apex, jar) pairs.
Paul Duffin9a89a2a2020-10-28 19:20:06 +00001540func (l *ConfiguredJarList) Apex(idx int) string {
1541 return l.apexes[idx]
1542}
1543
Jingwen Chenc711fec2020-11-22 23:52:50 -05001544// ContainsJar returns true if the (apex, jar) pairs contains a pair with the
1545// given jar module name.
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001546func (l *ConfiguredJarList) ContainsJar(jar string) bool {
1547 return InList(jar, l.jars)
1548}
1549
1550// If the list contains the given (apex, jar) pair.
1551func (l *ConfiguredJarList) containsApexJarPair(apex, jar string) bool {
1552 for i := 0; i < l.Len(); i++ {
Paul Duffin1e8c6072020-10-23 18:28:55 +01001553 if apex == l.apexes[i] && jar == l.jars[i] {
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001554 return true
1555 }
1556 }
1557 return false
1558}
1559
Jingwen Chenc711fec2020-11-22 23:52:50 -05001560// IndexOfJar returns the first pair with the given jar name on the list, or -1
1561// if not found.
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001562func (l *ConfiguredJarList) IndexOfJar(jar string) int {
1563 return IndexList(jar, l.jars)
1564}
1565
Paul Duffin7d584e92020-10-23 18:26:03 +01001566func copyAndAppend(list []string, item string) []string {
1567 // Create the result list to be 1 longer than the input.
1568 result := make([]string, len(list)+1)
1569
1570 // Copy the whole input list into the result.
1571 count := copy(result, list)
1572
1573 // Insert the extra item at the end.
1574 result[count] = item
1575
1576 return result
1577}
1578
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001579// Append an (apex, jar) pair to the list.
Paul Duffin7d584e92020-10-23 18:26:03 +01001580func (l *ConfiguredJarList) Append(apex string, jar string) ConfiguredJarList {
1581 // Create a copy of the backing arrays before appending to avoid sharing backing
1582 // arrays that are mutated across instances.
1583 apexes := copyAndAppend(l.apexes, apex)
1584 jars := copyAndAppend(l.jars, jar)
1585
1586 return ConfiguredJarList{apexes, jars}
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001587}
1588
Jingwen Chenc711fec2020-11-22 23:52:50 -05001589// RemoveList filters out a list of (apex, jar) pairs from the receiving list of pairs.
Paul Duffin7d584e92020-10-23 18:26:03 +01001590func (l *ConfiguredJarList) RemoveList(list ConfiguredJarList) ConfiguredJarList {
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001591 apexes := make([]string, 0, l.Len())
1592 jars := make([]string, 0, l.Len())
1593
1594 for i, jar := range l.jars {
Paul Duffin1e8c6072020-10-23 18:28:55 +01001595 apex := l.apexes[i]
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001596 if !list.containsApexJarPair(apex, jar) {
1597 apexes = append(apexes, apex)
1598 jars = append(jars, jar)
1599 }
1600 }
1601
Paul Duffin7d584e92020-10-23 18:26:03 +01001602 return ConfiguredJarList{apexes, jars}
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001603}
1604
Jingwen Chenc711fec2020-11-22 23:52:50 -05001605// CopyOfJars returns a copy of the list of strings containing jar module name
1606// components.
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001607func (l *ConfiguredJarList) CopyOfJars() []string {
1608 return CopyOf(l.jars)
1609}
1610
Jingwen Chenc711fec2020-11-22 23:52:50 -05001611// CopyOfApexJarPairs returns a copy of the list of strings with colon-separated
1612// (apex, jar) pairs.
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001613func (l *ConfiguredJarList) CopyOfApexJarPairs() []string {
1614 pairs := make([]string, 0, l.Len())
1615
1616 for i, jar := range l.jars {
Paul Duffin1e8c6072020-10-23 18:28:55 +01001617 apex := l.apexes[i]
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001618 pairs = append(pairs, apex+":"+jar)
1619 }
1620
1621 return pairs
1622}
1623
Jingwen Chenc711fec2020-11-22 23:52:50 -05001624// BuildPaths returns a list of build paths based on the given directory prefix.
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001625func (l *ConfiguredJarList) BuildPaths(ctx PathContext, dir OutputPath) WritablePaths {
1626 paths := make(WritablePaths, l.Len())
1627 for i, jar := range l.jars {
1628 paths[i] = dir.Join(ctx, ModuleStem(jar)+".jar")
1629 }
1630 return paths
1631}
1632
Jingwen Chenc711fec2020-11-22 23:52:50 -05001633// UnmarshalJSON converts JSON configuration from raw bytes into a
1634// ConfiguredJarList structure.
Paul Duffin69d1fb12020-10-23 21:14:20 +01001635func (l *ConfiguredJarList) UnmarshalJSON(b []byte) error {
1636 // Try and unmarshal into a []string each item of which contains a pair
1637 // <apex>:<jar>.
1638 var list []string
1639 err := json.Unmarshal(b, &list)
1640 if err != nil {
1641 // Did not work so return
1642 return err
1643 }
1644
1645 apexes, jars, err := splitListOfPairsIntoPairOfLists(list)
1646 if err != nil {
1647 return err
1648 }
1649 l.apexes = apexes
1650 l.jars = jars
1651 return nil
1652}
1653
Lukacs T. Berki720b3962021-03-17 13:34:30 +01001654func (l *ConfiguredJarList) MarshalJSON() ([]byte, error) {
1655 if len(l.apexes) != len(l.jars) {
1656 return nil, errors.New(fmt.Sprintf("Inconsistent ConfiguredJarList: apexes: %q, jars: %q", l.apexes, l.jars))
1657 }
1658
1659 list := make([]string, 0, len(l.apexes))
1660
1661 for i := 0; i < len(l.apexes); i++ {
1662 list = append(list, l.apexes[i]+":"+l.jars[i])
1663 }
1664
1665 return json.Marshal(list)
1666}
1667
Jingwen Chenc711fec2020-11-22 23:52:50 -05001668// ModuleStem hardcodes the stem of framework-minus-apex to return "framework".
1669//
1670// TODO(b/139391334): hard coded until we find a good way to query the stem of a
1671// module before any other mutators are run.
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001672func ModuleStem(module string) string {
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001673 if module == "framework-minus-apex" {
1674 return "framework"
1675 }
1676 return module
1677}
1678
Jingwen Chenc711fec2020-11-22 23:52:50 -05001679// DevicePaths computes the on-device paths for the list of (apex, jar) pairs,
1680// based on the operating system.
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001681func (l *ConfiguredJarList) DevicePaths(cfg Config, ostype OsType) []string {
1682 paths := make([]string, l.Len())
1683 for i, jar := range l.jars {
1684 apex := l.apexes[i]
1685 name := ModuleStem(jar) + ".jar"
1686
1687 var subdir string
1688 if apex == "platform" {
1689 subdir = "system/framework"
1690 } else if apex == "system_ext" {
1691 subdir = "system_ext/framework"
1692 } else {
1693 subdir = filepath.Join("apex", apex, "javalib")
1694 }
1695
1696 if ostype.Class == Host {
1697 paths[i] = filepath.Join(cfg.Getenv("OUT_DIR"), "host", cfg.PrebuiltOS(), subdir, name)
1698 } else {
1699 paths[i] = filepath.Join("/", subdir, name)
1700 }
1701 }
1702 return paths
1703}
1704
Paul Duffin7d584e92020-10-23 18:26:03 +01001705func (l *ConfiguredJarList) String() string {
1706 var pairs []string
1707 for i := 0; i < l.Len(); i++ {
1708 pairs = append(pairs, l.apexes[i]+":"+l.jars[i])
1709 }
1710 return strings.Join(pairs, ",")
1711}
1712
Paul Duffin01416602020-10-23 21:04:03 +01001713func splitListOfPairsIntoPairOfLists(list []string) ([]string, []string, error) {
1714 // Now we need to populate this list by splitting each item in the slice of
1715 // pairs and appending them to the appropriate list of apexes or jars.
1716 apexes := make([]string, len(list))
1717 jars := make([]string, len(list))
1718
1719 for i, apexjar := range list {
1720 apex, jar, err := splitConfiguredJarPair(apexjar)
1721 if err != nil {
1722 return nil, nil, err
1723 }
1724 apexes[i] = apex
1725 jars[i] = jar
1726 }
1727
1728 return apexes, jars, nil
1729}
1730
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001731// Expected format for apexJarValue = <apex name>:<jar name>
Paul Duffin01416602020-10-23 21:04:03 +01001732func splitConfiguredJarPair(str string) (string, string, error) {
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001733 pair := strings.SplitN(str, ":", 2)
1734 if len(pair) == 2 {
Paul Duffin9c3ac962021-02-03 14:11:27 +00001735 apex := pair[0]
1736 jar := pair[1]
1737 if apex == "" {
1738 return apex, jar, fmt.Errorf("invalid apex '%s' in <apex>:<jar> pair '%s', expected format: <apex>:<jar>", apex, str)
1739 }
1740 return apex, jar, nil
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001741 } else {
Paul Duffin01416602020-10-23 21:04:03 +01001742 return "error-apex", "error-jar", fmt.Errorf("malformed (apex, jar) pair: '%s', expected format: <apex>:<jar>", str)
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001743 }
1744}
1745
Paul Duffin9c3ac962021-02-03 14:11:27 +00001746// CreateTestConfiguredJarList is a function to create ConfiguredJarList for tests.
Paul Duffine10dfa42020-10-23 21:23:44 +01001747func CreateTestConfiguredJarList(list []string) ConfiguredJarList {
Paul Duffin9c3ac962021-02-03 14:11:27 +00001748 // Create the ConfiguredJarList in as similar way as it is created at runtime by marshalling to
1749 // a json list of strings and then unmarshalling into a ConfiguredJarList instance.
1750 b, err := json.Marshal(list)
Paul Duffin01416602020-10-23 21:04:03 +01001751 if err != nil {
Paul Duffine10dfa42020-10-23 21:23:44 +01001752 panic(err)
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001753 }
1754
Paul Duffin9c3ac962021-02-03 14:11:27 +00001755 var jarList ConfiguredJarList
1756 err = json.Unmarshal(b, &jarList)
1757 if err != nil {
1758 panic(err)
1759 }
1760
1761 return jarList
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001762}
1763
Jingwen Chenc711fec2020-11-22 23:52:50 -05001764// EmptyConfiguredJarList returns an empty jar list.
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001765func EmptyConfiguredJarList() ConfiguredJarList {
1766 return ConfiguredJarList{}
1767}
1768
1769var earlyBootJarsKey = NewOnceKey("earlyBootJars")
1770
1771func (c *config) BootJars() []string {
1772 return c.Once(earlyBootJarsKey, func() interface{} {
Paul Duffin69d1fb12020-10-23 21:14:20 +01001773 list := c.productVariables.BootJars.CopyOfJars()
Jingwen Chenc711fec2020-11-22 23:52:50 -05001774 return append(list, c.productVariables.UpdatableBootJars.CopyOfJars()...)
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001775 }).([]string)
1776}
Paul Duffin9a89a2a2020-10-28 19:20:06 +00001777
1778func (c *config) NonUpdatableBootJars() ConfiguredJarList {
1779 return c.productVariables.BootJars
1780}
1781
1782func (c *config) UpdatableBootJars() ConfiguredJarList {
1783 return c.productVariables.UpdatableBootJars
1784}
Colin Cross77cdcfd2021-03-12 11:28:25 -08001785
1786func (c *config) RBEWrapper() string {
1787 return c.GetenvWithDefault("RBE_WRAPPER", remoteexec.DefaultWrapperPath)
1788}