blob: d7c82a099b76b1ba6e4b7884ae64ce1a2cdb0a25 [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"
22 "fmt"
Colin Crossd8f20142016-11-03 09:43:26 -070023 "io/ioutil"
Colin Cross3f40fa42015-01-30 17:27:36 -080024 "os"
Colin Cross35cec122015-04-02 14:37:16 -070025 "path/filepath"
Colin Cross3f40fa42015-01-30 17:27:36 -080026 "runtime"
Inseob Kim60c32f02020-12-21 22:53:05 +090027 "strconv"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070028 "strings"
Colin Crossc1e86a32015-04-15 12:33:28 -070029 "sync"
Colin Cross6ff51382015-12-17 16:39:19 -080030
Colin Cross98be1bb2019-12-13 20:41:13 -080031 "github.com/google/blueprint"
Colin Crosse87040b2017-12-11 15:52:26 -080032 "github.com/google/blueprint/bootstrap"
Colin Cross98be1bb2019-12-13 20:41:13 -080033 "github.com/google/blueprint/pathtools"
Colin Cross6ff51382015-12-17 16:39:19 -080034 "github.com/google/blueprint/proptools"
Colin Cross9d34f352019-11-22 16:03:51 -080035
36 "android/soong/android/soongconfig"
Colin Cross3f40fa42015-01-30 17:27:36 -080037)
38
Jingwen Chenc711fec2020-11-22 23:52:50 -050039// Bool re-exports proptools.Bool for the android package.
Colin Cross6ff51382015-12-17 16:39:19 -080040var Bool = proptools.Bool
Jingwen Chenc711fec2020-11-22 23:52:50 -050041
42// String re-exports proptools.String for the android package.
Jack He8cc71432016-12-08 15:45:07 -080043var String = proptools.String
Jingwen Chenc711fec2020-11-22 23:52:50 -050044
45// StringDefault re-exports proptools.StringDefault for the android package.
Jeongik Cha219141c2020-08-06 23:00:37 +090046var StringDefault = proptools.StringDefault
Jiyong Park6a927c42020-01-21 02:03:43 +090047
Jingwen Chenc711fec2020-11-22 23:52:50 -050048// FutureApiLevelInt is a placeholder constant for unreleased API levels.
Dan Albert0b176c82020-07-23 16:43:25 -070049const FutureApiLevelInt = 10000
50
Jingwen Chenc711fec2020-11-22 23:52:50 -050051// FutureApiLevel represents unreleased API levels.
Dan Albert0b176c82020-07-23 16:43:25 -070052var FutureApiLevel = ApiLevel{
53 value: "current",
54 number: FutureApiLevelInt,
55 isPreview: true,
56}
Colin Cross6ff51382015-12-17 16:39:19 -080057
Jingwen Chenc4d91bc2020-11-24 22:59:26 -050058// The product variables file name, containing product config from Kati.
Dan Willemsen87b17d12015-07-14 00:39:06 -070059const productVariablesFileName = "soong.variables"
Colin Cross3f40fa42015-01-30 17:27:36 -080060
Colin Cross9272ade2016-08-17 15:24:12 -070061// A Config object represents the entire build configuration for Android.
Colin Crossc3c0a492015-04-10 15:43:55 -070062type Config struct {
63 *config
64}
65
Jingwen Chenc711fec2020-11-22 23:52:50 -050066// BuildDir returns the build output directory for the configuration.
Jeff Gastonefc1b412017-03-29 17:29:06 -070067func (c Config) BuildDir() string {
68 return c.buildDir
69}
70
Lukacs T. Berki89e9a162021-03-12 08:31:32 +010071func (c Config) NinjaBuildDir() string {
72 return c.buildDir
73}
74
75func (c Config) SrcDir() string {
76 return c.srcDir
77}
78
Jingwen Chenc711fec2020-11-22 23:52:50 -050079// A DeviceConfig object represents the configuration for a particular device
80// being built. For now there will only be one of these, but in the future there
81// may be multiple devices being built.
Colin Cross9272ade2016-08-17 15:24:12 -070082type DeviceConfig struct {
83 *deviceConfig
84}
85
Jingwen Chenc711fec2020-11-22 23:52:50 -050086// VendorConfig represents the configuration for vendor-specific behavior.
Colin Cross9d34f352019-11-22 16:03:51 -080087type VendorConfig soongconfig.SoongConfig
Dan Willemsen0fe78662018-03-26 12:41:18 -070088
Jingwen Chenc711fec2020-11-22 23:52:50 -050089// Definition of general build configuration for soong_build. Some of these
Jingwen Chenc4d91bc2020-11-24 22:59:26 -050090// product configuration values are read from Kati-generated soong.variables.
Colin Cross1332b002015-04-07 17:11:30 -070091type config struct {
Jingwen Chenc711fec2020-11-22 23:52:50 -050092 // Options configurable with soong.variables
Dan Willemsen45133ac2018-03-09 21:22:06 -080093 productVariables productVariables
Colin Cross3f40fa42015-01-30 17:27:36 -080094
Dan Willemsen674dc7f2018-03-12 18:06:05 -070095 // Only available on configs created by TestConfig
96 TestProductVariables *productVariables
97
Jingwen Chenc711fec2020-11-22 23:52:50 -050098 // A specialized context object for Bazel/Soong mixed builds and migration
99 // purposes.
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400100 BazelContext BazelContext
101
Dan Willemsen87b17d12015-07-14 00:39:06 -0700102 ProductVariablesFileName string
103
Jaewoong Jung642916f2020-10-09 17:25:15 -0700104 Targets map[OsType][]Target
105 BuildOSTarget Target // the Target for tools run on the build machine
106 BuildOSCommonTarget Target // the Target for common (java) tools run on the build machine
107 AndroidCommonTarget Target // the Target for common modules for the Android device
108 AndroidFirstDeviceTarget Target // the first Target for modules for the Android device
Dan Willemsen218f6562015-07-08 18:13:11 -0700109
Jingwen Chenc711fec2020-11-22 23:52:50 -0500110 // multilibConflicts for an ArchType is true if there is earlier configured
111 // device architecture with the same multilib value.
Colin Cross3b19f5d2019-09-17 14:45:31 -0700112 multilibConflicts map[ArchType]bool
113
Colin Cross9272ade2016-08-17 15:24:12 -0700114 deviceConfig *deviceConfig
115
Chris Parsons8f232a22020-06-23 17:37:05 -0400116 srcDir string // the path of the root source directory
117 buildDir string // the path of the build output directory
118 moduleListFile string // the path to the file which lists blueprint files to parse.
Colin Crossc1e86a32015-04-15 12:33:28 -0700119
Colin Cross6ccbc912017-10-10 23:07:38 -0700120 env map[string]string
Dan Willemsene7680ba2015-09-11 17:06:19 -0700121 envLock sync.Mutex
122 envDeps map[string]string
123 envFrozen bool
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800124
Jingwen Chencda22c92020-11-23 00:22:30 -0500125 // Changes behavior based on whether Kati runs after soong_build, or if soong_build
126 // runs standalone.
127 katiEnabled bool
Colin Cross1e7d3702016-08-24 15:25:47 -0700128
Colin Cross32616ed2017-09-05 21:56:44 -0700129 captureBuild bool // true for tests, saves build parameters for each module
130 ignoreEnvironment bool // true for tests, returns empty from all Getenv calls
Colin Crosscec81712017-07-13 14:43:27 -0700131
Colin Crosse87040b2017-12-11 15:52:26 -0800132 stopBefore bootstrap.StopBefore
133
Colin Cross98be1bb2019-12-13 20:41:13 -0800134 fs pathtools.FileSystem
135 mockBpList string
136
Colin Cross5e6a7972020-06-07 16:56:32 -0700137 // If testAllowNonExistentPaths is true then PathForSource and PathForModuleSrc won't error
138 // in tests when a path doesn't exist.
Pedro Loureiro5d190cc2021-02-15 15:41:33 +0000139 TestAllowNonExistentPaths bool
Colin Cross5e6a7972020-06-07 16:56:32 -0700140
Jingwen Chenc711fec2020-11-22 23:52:50 -0500141 // The list of files that when changed, must invalidate soong_build to
142 // regenerate build.ninja.
Colin Cross12129292020-10-29 18:23:58 -0700143 ninjaFileDepsSet sync.Map
144
Colin Cross9272ade2016-08-17 15:24:12 -0700145 OncePer
146}
147
148type deviceConfig struct {
Dan Willemsen00269f22017-07-06 16:59:48 -0700149 config *config
Colin Cross9272ade2016-08-17 15:24:12 -0700150 OncePer
Colin Cross3f40fa42015-01-30 17:27:36 -0800151}
152
Colin Cross485e5722015-08-27 13:28:01 -0700153type jsonConfigurable interface {
Colin Cross27385972015-09-18 10:57:10 -0700154 SetDefaultConfig()
Colin Cross485e5722015-08-27 13:28:01 -0700155}
Colin Cross3f40fa42015-01-30 17:27:36 -0800156
Colin Cross485e5722015-08-27 13:28:01 -0700157func loadConfig(config *config) error {
Colin Cross988414c2020-01-11 01:11:46 +0000158 return loadFromConfigFile(&config.productVariables, absolutePath(config.ProductVariablesFileName))
Colin Cross485e5722015-08-27 13:28:01 -0700159}
160
Jingwen Chenc711fec2020-11-22 23:52:50 -0500161// loadFromConfigFile loads and decodes configuration options from a JSON file
162// in the current working directory.
Colin Cross485e5722015-08-27 13:28:01 -0700163func loadFromConfigFile(configurable jsonConfigurable, filename string) error {
Colin Cross3f40fa42015-01-30 17:27:36 -0800164 // Try to open the file
Colin Cross485e5722015-08-27 13:28:01 -0700165 configFileReader, err := os.Open(filename)
Colin Cross3f40fa42015-01-30 17:27:36 -0800166 defer configFileReader.Close()
167 if os.IsNotExist(err) {
168 // Need to create a file, so that blueprint & ninja don't get in
169 // a dependency tracking loop.
170 // Make a file-configurable-options with defaults, write it out using
171 // a json writer.
Colin Cross27385972015-09-18 10:57:10 -0700172 configurable.SetDefaultConfig()
173 err = saveToConfigFile(configurable, filename)
Colin Cross3f40fa42015-01-30 17:27:36 -0800174 if err != nil {
175 return err
176 }
Colin Cross15cd21a2018-02-27 11:26:02 -0800177 } else if err != nil {
178 return fmt.Errorf("config file: could not open %s: %s", filename, err.Error())
Colin Cross3f40fa42015-01-30 17:27:36 -0800179 } else {
180 // Make a decoder for it
181 jsonDecoder := json.NewDecoder(configFileReader)
Colin Cross485e5722015-08-27 13:28:01 -0700182 err = jsonDecoder.Decode(configurable)
Colin Cross3f40fa42015-01-30 17:27:36 -0800183 if err != nil {
Colin Cross15cd21a2018-02-27 11:26:02 -0800184 return fmt.Errorf("config file: %s did not parse correctly: %s", filename, err.Error())
Colin Cross3f40fa42015-01-30 17:27:36 -0800185 }
186 }
187
Colin Cross3f40fa42015-01-30 17:27:36 -0800188 // No error
189 return nil
190}
191
Colin Crossd8f20142016-11-03 09:43:26 -0700192// atomically writes the config file in case two copies of soong_build are running simultaneously
193// (for example, docs generation and ninja manifest generation)
Colin Cross485e5722015-08-27 13:28:01 -0700194func saveToConfigFile(config jsonConfigurable, filename string) error {
Colin Cross3f40fa42015-01-30 17:27:36 -0800195 data, err := json.MarshalIndent(&config, "", " ")
196 if err != nil {
197 return fmt.Errorf("cannot marshal config data: %s", err.Error())
198 }
199
Colin Crossd8f20142016-11-03 09:43:26 -0700200 f, err := ioutil.TempFile(filepath.Dir(filename), "config")
Colin Cross3f40fa42015-01-30 17:27:36 -0800201 if err != nil {
Jingwen Chenc711fec2020-11-22 23:52:50 -0500202 return fmt.Errorf("cannot create empty config file %s: %s", filename, err.Error())
Colin Cross3f40fa42015-01-30 17:27:36 -0800203 }
Colin Crossd8f20142016-11-03 09:43:26 -0700204 defer os.Remove(f.Name())
205 defer f.Close()
Colin Cross3f40fa42015-01-30 17:27:36 -0800206
Colin Crossd8f20142016-11-03 09:43:26 -0700207 _, err = f.Write(data)
Colin Cross3f40fa42015-01-30 17:27:36 -0800208 if err != nil {
Colin Cross485e5722015-08-27 13:28:01 -0700209 return fmt.Errorf("default config file: %s could not be written: %s", filename, err.Error())
210 }
211
Colin Crossd8f20142016-11-03 09:43:26 -0700212 _, err = f.WriteString("\n")
Colin Cross485e5722015-08-27 13:28:01 -0700213 if err != nil {
214 return fmt.Errorf("default config file: %s could not be written: %s", filename, err.Error())
Colin Cross3f40fa42015-01-30 17:27:36 -0800215 }
216
Colin Crossd8f20142016-11-03 09:43:26 -0700217 f.Close()
218 os.Rename(f.Name(), filename)
219
Colin Cross3f40fa42015-01-30 17:27:36 -0800220 return nil
221}
222
Colin Cross988414c2020-01-11 01:11:46 +0000223// NullConfig returns a mostly empty Config for use by standalone tools like dexpreopt_gen that
224// use the android package.
225func NullConfig(buildDir string) Config {
226 return Config{
227 config: &config{
228 buildDir: buildDir,
229 fs: pathtools.OsFs,
230 },
231 }
232}
233
Jingwen Chenc711fec2020-11-22 23:52:50 -0500234// TestConfig returns a Config object for testing.
Colin Cross98be1bb2019-12-13 20:41:13 -0800235func TestConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
Colin Cross9c6241f2019-04-22 15:51:26 -0700236 envCopy := make(map[string]string)
237 for k, v := range env {
238 envCopy[k] = v
239 }
240
Jingwen Chen2838c812020-11-23 01:06:40 -0500241 // Copy the real PATH value to the test environment, it's needed by
242 // NonHermeticHostSystemTool() used in x86_darwin_host.go
Lukacs T. Berkideba7212021-03-04 10:50:10 +0100243 envCopy["PATH"] = os.Getenv("PATH")
Colin Cross9c6241f2019-04-22 15:51:26 -0700244
Dan Willemsen00269f22017-07-06 16:59:48 -0700245 config := &config{
Dan Willemsen45133ac2018-03-09 21:22:06 -0800246 productVariables: productVariables{
Dan Albert4f378d72020-07-23 17:32:15 -0700247 DeviceName: stringPtr("test_device"),
248 Platform_sdk_version: intPtr(30),
249 Platform_sdk_codename: stringPtr("S"),
250 Platform_version_active_codenames: []string{"S"},
251 DeviceSystemSdkVersions: []string{"14", "15"},
252 Platform_systemsdk_versions: []string{"29", "30"},
253 AAPTConfig: []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"},
254 AAPTPreferredConfig: stringPtr("xhdpi"),
255 AAPTCharacteristics: stringPtr("nosdcard"),
256 AAPTPrebuiltDPI: []string{"xhdpi", "xxhdpi"},
257 UncompressPrivAppDex: boolPtr(true),
Inseob Kim60c32f02020-12-21 22:53:05 +0900258 ShippingApiLevel: stringPtr("30"),
Dan Willemsen00269f22017-07-06 16:59:48 -0700259 },
260
Colin Cross6ccbc912017-10-10 23:07:38 -0700261 buildDir: buildDir,
262 captureBuild: true,
Colin Cross9c6241f2019-04-22 15:51:26 -0700263 env: envCopy,
Colin Cross5e6a7972020-06-07 16:56:32 -0700264
265 // Set testAllowNonExistentPaths so that test contexts don't need to specify every path
266 // passed to PathForSource or PathForModuleSrc.
Pedro Loureiro5d190cc2021-02-15 15:41:33 +0000267 TestAllowNonExistentPaths: true,
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400268
269 BazelContext: noopBazelContext{},
Dan Willemsen00269f22017-07-06 16:59:48 -0700270 }
271 config.deviceConfig = &deviceConfig{
272 config: config,
273 }
Dan Willemsen45133ac2018-03-09 21:22:06 -0800274 config.TestProductVariables = &config.productVariables
Dan Willemsen00269f22017-07-06 16:59:48 -0700275
Colin Cross98be1bb2019-12-13 20:41:13 -0800276 config.mockFileSystem(bp, fs)
277
Dan Willemsen00269f22017-07-06 16:59:48 -0700278 return Config{config}
Colin Crossce75d2c2016-10-06 16:12:58 -0700279}
280
Paul Duffinecdac8a2021-02-24 19:18:42 +0000281func fuchsiaTargets() map[OsType][]Target {
282 return map[OsType][]Target{
283 Fuchsia: {
Jiyong Park1613e552020-09-14 19:43:17 +0900284 {Fuchsia, Arch{ArchType: Arm64, ArchVariant: "", Abi: []string{"arm64-v8a"}}, NativeBridgeDisabled, "", "", false},
Doug Hornc32c6b02019-01-17 14:44:05 -0800285 },
Paul Duffinecdac8a2021-02-24 19:18:42 +0000286 BuildOs: {
Jiyong Park1613e552020-09-14 19:43:17 +0900287 {BuildOs, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
Doug Hornc32c6b02019-01-17 14:44:05 -0800288 },
289 }
Doug Hornc32c6b02019-01-17 14:44:05 -0800290}
291
Paul Duffinecdac8a2021-02-24 19:18:42 +0000292var PrepareForTestSetDeviceToFuchsia = FixtureModifyConfig(func(config Config) {
293 config.Targets = fuchsiaTargets()
294})
295
Paul Duffin35816122021-02-24 01:49:52 +0000296func modifyTestConfigToSupportArchMutator(testConfig Config) {
Colin Crossae4c6182017-09-15 17:33:55 -0700297 config := testConfig.config
298
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700299 config.Targets = map[OsType][]Target{
300 Android: []Target{
Jiyong Park1613e552020-09-14 19:43:17 +0900301 {Android, Arch{ArchType: Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridgeDisabled, "", "", false},
302 {Android, Arch{ArchType: Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridgeDisabled, "", "", false},
Colin Crossae4c6182017-09-15 17:33:55 -0700303 },
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700304 BuildOs: []Target{
Jiyong Park1613e552020-09-14 19:43:17 +0900305 {BuildOs, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
306 {BuildOs, Arch{ArchType: X86}, NativeBridgeDisabled, "", "", false},
Colin Crossae4c6182017-09-15 17:33:55 -0700307 },
308 }
309
Colin Cross0d99f7c2019-05-14 16:01:24 -0700310 if runtime.GOOS == "darwin" {
311 config.Targets[BuildOs] = config.Targets[BuildOs][:1]
312 }
313
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700314 config.BuildOSTarget = config.Targets[BuildOs][0]
315 config.BuildOSCommonTarget = getCommonTargets(config.Targets[BuildOs])[0]
316 config.AndroidCommonTarget = getCommonTargets(config.Targets[Android])[0]
Jaewoong Jung642916f2020-10-09 17:25:15 -0700317 config.AndroidFirstDeviceTarget = firstTarget(config.Targets[Android], "lib64", "lib32")[0]
Inseob Kim1f086e22019-05-09 13:29:15 +0900318 config.TestProductVariables.DeviceArch = proptools.StringPtr("arm64")
319 config.TestProductVariables.DeviceArchVariant = proptools.StringPtr("armv8-a")
320 config.TestProductVariables.DeviceSecondaryArch = proptools.StringPtr("arm")
321 config.TestProductVariables.DeviceSecondaryArchVariant = proptools.StringPtr("armv7-a-neon")
Paul Duffin35816122021-02-24 01:49:52 +0000322}
Colin Cross2a076922018-10-04 23:28:25 -0700323
Paul Duffin35816122021-02-24 01:49:52 +0000324// TestArchConfig returns a Config object suitable for using for tests that
325// need to run the arch mutator.
326func TestArchConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
327 testConfig := TestConfig(buildDir, env, bp, fs)
328 modifyTestConfigToSupportArchMutator(testConfig)
Colin Crossae4c6182017-09-15 17:33:55 -0700329 return testConfig
330}
331
Jingwen Chenc711fec2020-11-22 23:52:50 -0500332// ConfigForAdditionalRun is a config object which is "reset" for another
333// bootstrap run. Only per-run data is reset. Data which needs to persist across
334// multiple runs in the same program execution is carried over (such as Bazel
335// context or environment deps).
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400336func ConfigForAdditionalRun(c Config) (Config, error) {
337 newConfig, err := NewConfig(c.srcDir, c.buildDir, c.moduleListFile)
338 if err != nil {
339 return Config{}, err
340 }
341 newConfig.BazelContext = c.BazelContext
342 newConfig.envDeps = c.envDeps
343 return newConfig, nil
344}
345
Jingwen Chenc711fec2020-11-22 23:52:50 -0500346// NewConfig creates a new Config object. The srcDir argument specifies the path
347// to the root source directory. It also loads the config file, if found.
Chris Parsons8f232a22020-06-23 17:37:05 -0400348func NewConfig(srcDir, buildDir string, moduleListFile string) (Config, error) {
Jingwen Chenc711fec2020-11-22 23:52:50 -0500349 // Make a config with default options.
Colin Cross9272ade2016-08-17 15:24:12 -0700350 config := &config{
Colin Cross9272ade2016-08-17 15:24:12 -0700351 ProductVariablesFileName: filepath.Join(buildDir, productVariablesFileName),
Dan Willemsen87b17d12015-07-14 00:39:06 -0700352
Colin Cross6ccbc912017-10-10 23:07:38 -0700353 env: originalEnv,
354
Colin Cross3b19f5d2019-09-17 14:45:31 -0700355 srcDir: srcDir,
356 buildDir: buildDir,
357 multilibConflicts: make(map[ArchType]bool),
Colin Cross98be1bb2019-12-13 20:41:13 -0800358
Chris Parsons8f232a22020-06-23 17:37:05 -0400359 moduleListFile: moduleListFile,
360 fs: pathtools.NewOsFs(absSrcDir),
Colin Cross68f55102015-03-25 14:43:57 -0700361 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800362
Dan Willemsen00269f22017-07-06 16:59:48 -0700363 config.deviceConfig = &deviceConfig{
Colin Cross9272ade2016-08-17 15:24:12 -0700364 config: config,
365 }
366
Liz Kammer7941b302020-07-28 13:27:34 -0700367 // Soundness check of the build and source directories. This won't catch strange
368 // configurations with symlinks, but at least checks the obvious case.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700369 absBuildDir, err := filepath.Abs(buildDir)
370 if err != nil {
371 return Config{}, err
372 }
373
374 absSrcDir, err := filepath.Abs(srcDir)
375 if err != nil {
376 return Config{}, err
377 }
378
379 if strings.HasPrefix(absSrcDir, absBuildDir) {
380 return Config{}, fmt.Errorf("Build dir must not contain source directory")
381 }
382
Colin Cross3f40fa42015-01-30 17:27:36 -0800383 // Load any configurable options from the configuration file
Colin Cross9272ade2016-08-17 15:24:12 -0700384 err = loadConfig(config)
Colin Cross3f40fa42015-01-30 17:27:36 -0800385 if err != nil {
Colin Crossc3c0a492015-04-10 15:43:55 -0700386 return Config{}, err
Colin Cross3f40fa42015-01-30 17:27:36 -0800387 }
388
Jingwen Chencda22c92020-11-23 00:22:30 -0500389 KatiEnabledMarkerFile := filepath.Join(buildDir, ".soong.kati_enabled")
390 if _, err := os.Stat(absolutePath(KatiEnabledMarkerFile)); err == nil {
391 config.katiEnabled = true
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800392 }
393
Jingwen Chenc711fec2020-11-22 23:52:50 -0500394 // Sets up the map of target OSes to the finer grained compilation targets
395 // that are configured from the product variables.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700396 targets, err := decodeTargetProductVariables(config)
Dan Willemsen218f6562015-07-08 18:13:11 -0700397 if err != nil {
398 return Config{}, err
399 }
400
Paul Duffin1356d8c2020-02-25 19:26:33 +0000401 // Make the CommonOS OsType available for all products.
402 targets[CommonOS] = []Target{commonTargetMap[CommonOS.Name]}
403
Dan Albert4098deb2016-10-19 14:04:41 -0700404 var archConfig []archConfig
Jingwen Chenc4d91bc2020-11-24 22:59:26 -0500405 if config.NdkAbis() {
Dan Albert4098deb2016-10-19 14:04:41 -0700406 archConfig = getNdkAbisConfig()
Martin Stjernholmc1ecc432019-11-15 15:00:31 +0000407 } else if config.AmlAbis() {
408 archConfig = getAmlAbisConfig()
Dan Albert4098deb2016-10-19 14:04:41 -0700409 }
410
411 if archConfig != nil {
Dan Willemsen01a3c252019-01-11 19:02:16 -0800412 androidTargets, err := decodeArchSettings(Android, archConfig)
Dan Willemsen322acaf2016-01-12 23:07:05 -0800413 if err != nil {
414 return Config{}, err
415 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700416 targets[Android] = androidTargets
Dan Willemsen322acaf2016-01-12 23:07:05 -0800417 }
418
Colin Cross3b19f5d2019-09-17 14:45:31 -0700419 multilib := make(map[string]bool)
420 for _, target := range targets[Android] {
421 if seen := multilib[target.Arch.ArchType.Multilib]; seen {
422 config.multilibConflicts[target.Arch.ArchType] = true
423 }
424 multilib[target.Arch.ArchType.Multilib] = true
425 }
426
Jingwen Chenc711fec2020-11-22 23:52:50 -0500427 // Map of OS to compilation targets.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700428 config.Targets = targets
Jingwen Chenc711fec2020-11-22 23:52:50 -0500429
430 // Compilation targets for host tools.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700431 config.BuildOSTarget = config.Targets[BuildOs][0]
432 config.BuildOSCommonTarget = getCommonTargets(config.Targets[BuildOs])[0]
Jingwen Chenc711fec2020-11-22 23:52:50 -0500433
434 // Compilation targets for Android.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700435 if len(config.Targets[Android]) > 0 {
436 config.AndroidCommonTarget = getCommonTargets(config.Targets[Android])[0]
Jaewoong Jung642916f2020-10-09 17:25:15 -0700437 config.AndroidFirstDeviceTarget = firstTarget(config.Targets[Android], "lib64", "lib32")[0]
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700438 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700439
Colin Cross1a6acd42020-06-16 17:51:46 -0700440 if Bool(config.productVariables.GcovCoverage) && Bool(config.productVariables.ClangCoverage) {
441 return Config{}, fmt.Errorf("GcovCoverage and ClangCoverage cannot both be set")
442 }
443
444 config.productVariables.Native_coverage = proptools.BoolPtr(
445 Bool(config.productVariables.GcovCoverage) ||
446 Bool(config.productVariables.ClangCoverage))
447
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400448 config.BazelContext, err = NewBazelContext(config)
Colin Cross3f40fa42015-01-30 17:27:36 -0800449
Jingwen Chenc711fec2020-11-22 23:52:50 -0500450 return Config{config}, err
451}
Colin Cross988414c2020-01-11 01:11:46 +0000452
Colin Cross98be1bb2019-12-13 20:41:13 -0800453// mockFileSystem replaces all reads with accesses to the provided map of
454// filenames to contents stored as a byte slice.
455func (c *config) mockFileSystem(bp string, fs map[string][]byte) {
456 mockFS := map[string][]byte{}
457
458 if _, exists := mockFS["Android.bp"]; !exists {
459 mockFS["Android.bp"] = []byte(bp)
460 }
461
462 for k, v := range fs {
463 mockFS[k] = v
464 }
465
466 // no module list file specified; find every file named Blueprints or Android.bp
467 pathsToParse := []string{}
468 for candidate := range mockFS {
469 base := filepath.Base(candidate)
470 if base == "Blueprints" || base == "Android.bp" {
471 pathsToParse = append(pathsToParse, candidate)
472 }
473 }
474 if len(pathsToParse) < 1 {
475 panic(fmt.Sprintf("No Blueprint or Android.bp files found in mock filesystem: %v\n", mockFS))
476 }
477 mockFS[blueprint.MockModuleListFile] = []byte(strings.Join(pathsToParse, "\n"))
478
479 c.fs = pathtools.MockFs(mockFS)
480 c.mockBpList = blueprint.MockModuleListFile
481}
482
Colin Crosse87040b2017-12-11 15:52:26 -0800483func (c *config) StopBefore() bootstrap.StopBefore {
484 return c.stopBefore
Dan Willemsen218f6562015-07-08 18:13:11 -0700485}
486
Jingwen Chenc711fec2020-11-22 23:52:50 -0500487// SetStopBefore configures soong_build to exit earlier at a specific point.
Colin Crosse87040b2017-12-11 15:52:26 -0800488func (c *config) SetStopBefore(stopBefore bootstrap.StopBefore) {
489 c.stopBefore = stopBefore
490}
491
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100492func (c *config) SetAllowMissingDependencies() {
493 c.productVariables.Allow_missing_dependencies = proptools.BoolPtr(true)
494}
495
Colin Crosse87040b2017-12-11 15:52:26 -0800496var _ bootstrap.ConfigStopBefore = (*config)(nil)
497
Jingwen Chenc711fec2020-11-22 23:52:50 -0500498// BlueprintToolLocation returns the directory containing build system tools
499// from Blueprint, like soong_zip and merge_zips.
Dan Willemsenc2aa4a92016-05-26 15:13:03 -0700500func (c *config) BlueprintToolLocation() string {
501 return filepath.Join(c.buildDir, "host", c.PrebuiltOS(), "bin")
502}
503
Colin Crosse87040b2017-12-11 15:52:26 -0800504var _ bootstrap.ConfigBlueprintToolLocation = (*config)(nil)
505
Dan Willemsen60e62f02018-11-16 21:05:32 -0800506func (c *config) HostToolPath(ctx PathContext, tool string) Path {
507 return PathForOutput(ctx, "host", c.PrebuiltOS(), "bin", tool)
508}
509
Martin Stjernholm7260d062019-12-09 21:47:14 +0000510func (c *config) HostJNIToolPath(ctx PathContext, path string) Path {
511 ext := ".so"
512 if runtime.GOOS == "darwin" {
513 ext = ".dylib"
514 }
515 return PathForOutput(ctx, "host", c.PrebuiltOS(), "lib64", path+ext)
516}
517
518func (c *config) HostJavaToolPath(ctx PathContext, path string) Path {
519 return PathForOutput(ctx, "host", c.PrebuiltOS(), "framework", path)
520}
521
Jingwen Chenc711fec2020-11-22 23:52:50 -0500522// PrebuiltOS returns the name of the host OS used in prebuilts directories.
Colin Cross1332b002015-04-07 17:11:30 -0700523func (c *config) PrebuiltOS() string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800524 switch runtime.GOOS {
525 case "linux":
526 return "linux-x86"
527 case "darwin":
528 return "darwin-x86"
529 default:
530 panic("Unknown GOOS")
531 }
532}
533
534// GoRoot returns the path to the root directory of the Go toolchain.
Colin Cross1332b002015-04-07 17:11:30 -0700535func (c *config) GoRoot() string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800536 return fmt.Sprintf("%s/prebuilts/go/%s", c.srcDir, c.PrebuiltOS())
537}
538
Jingwen Chenc711fec2020-11-22 23:52:50 -0500539// PrebuiltBuildTool returns the path to a tool in the prebuilts directory containing
540// checked-in tools, like Kati, Ninja or Toybox, for the current host OS.
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700541func (c *config) PrebuiltBuildTool(ctx PathContext, tool string) Path {
542 return PathForSource(ctx, "prebuilts/build-tools", c.PrebuiltOS(), "bin", tool)
543}
544
Jingwen Chenc711fec2020-11-22 23:52:50 -0500545// CpPreserveSymlinksFlags returns the host-specific flag for the cp(1) command
546// to preserve symlinks.
Colin Cross1332b002015-04-07 17:11:30 -0700547func (c *config) CpPreserveSymlinksFlags() string {
Colin Cross485e5722015-08-27 13:28:01 -0700548 switch runtime.GOOS {
Colin Cross3f40fa42015-01-30 17:27:36 -0800549 case "darwin":
550 return "-R"
551 case "linux":
552 return "-d"
553 default:
554 return ""
555 }
556}
Colin Cross68f55102015-03-25 14:43:57 -0700557
Colin Cross1332b002015-04-07 17:11:30 -0700558func (c *config) Getenv(key string) string {
Colin Cross68f55102015-03-25 14:43:57 -0700559 var val string
560 var exists bool
Colin Crossc1e86a32015-04-15 12:33:28 -0700561 c.envLock.Lock()
Colin Crossc0d58b42017-02-06 15:40:41 -0800562 defer c.envLock.Unlock()
563 if c.envDeps == nil {
564 c.envDeps = make(map[string]string)
565 }
Colin Cross68f55102015-03-25 14:43:57 -0700566 if val, exists = c.envDeps[key]; !exists {
Dan Willemsene7680ba2015-09-11 17:06:19 -0700567 if c.envFrozen {
568 panic("Cannot access new environment variables after envdeps are frozen")
569 }
Colin Cross6ccbc912017-10-10 23:07:38 -0700570 val, _ = c.env[key]
Colin Cross68f55102015-03-25 14:43:57 -0700571 c.envDeps[key] = val
572 }
573 return val
574}
575
Colin Cross99d7c232016-11-23 16:52:04 -0800576func (c *config) GetenvWithDefault(key string, defaultValue string) string {
577 ret := c.Getenv(key)
578 if ret == "" {
579 return defaultValue
580 }
581 return ret
582}
583
584func (c *config) IsEnvTrue(key string) bool {
585 value := c.Getenv(key)
586 return value == "1" || value == "y" || value == "yes" || value == "on" || value == "true"
587}
588
589func (c *config) IsEnvFalse(key string) bool {
590 value := c.Getenv(key)
591 return value == "0" || value == "n" || value == "no" || value == "off" || value == "false"
592}
593
Jingwen Chenc711fec2020-11-22 23:52:50 -0500594// EnvDeps returns the environment variables this build depends on. The first
595// call to this function blocks future reads from the environment.
Colin Cross1332b002015-04-07 17:11:30 -0700596func (c *config) EnvDeps() map[string]string {
Dan Willemsene7680ba2015-09-11 17:06:19 -0700597 c.envLock.Lock()
Colin Crossc0d58b42017-02-06 15:40:41 -0800598 defer c.envLock.Unlock()
Dan Willemsene7680ba2015-09-11 17:06:19 -0700599 c.envFrozen = true
Colin Cross68f55102015-03-25 14:43:57 -0700600 return c.envDeps
601}
Colin Cross35cec122015-04-02 14:37:16 -0700602
Jingwen Chencda22c92020-11-23 00:22:30 -0500603func (c *config) KatiEnabled() bool {
604 return c.katiEnabled
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800605}
606
Nan Zhang581fd212018-01-10 16:06:12 -0800607func (c *config) BuildId() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800608 return String(c.productVariables.BuildId)
Nan Zhang581fd212018-01-10 16:06:12 -0800609}
610
Jingwen Chenc711fec2020-11-22 23:52:50 -0500611// BuildNumberFile returns the path to a text file containing metadata
612// representing the current build's number.
613//
614// Rules that want to reference the build number should read from this file
615// without depending on it. They will run whenever their other dependencies
616// require them to run and get the current build number. This ensures they don't
617// rebuild on every incremental build when the build number changes.
Colin Cross2a2e0db2020-02-21 16:55:46 -0800618func (c *config) BuildNumberFile(ctx PathContext) Path {
619 return PathForOutput(ctx, String(c.productVariables.BuildNumberFile))
Nan Zhang581fd212018-01-10 16:06:12 -0800620}
621
Jingwen Chenc711fec2020-11-22 23:52:50 -0500622// DeviceName returns the name of the current device target.
Colin Cross35cec122015-04-02 14:37:16 -0700623// TODO: take an AndroidModuleContext to select the device name for multi-device builds
Colin Cross1332b002015-04-07 17:11:30 -0700624func (c *config) DeviceName() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800625 return *c.productVariables.DeviceName
Colin Cross35cec122015-04-02 14:37:16 -0700626}
627
Anton Hansson53c88442019-03-18 15:53:16 +0000628func (c *config) DeviceResourceOverlays() []string {
629 return c.productVariables.DeviceResourceOverlays
630}
631
632func (c *config) ProductResourceOverlays() []string {
633 return c.productVariables.ProductResourceOverlays
Colin Cross30e076a2015-04-13 13:58:27 -0700634}
635
Colin Crossbfd347d2018-05-09 11:11:35 -0700636func (c *config) PlatformVersionName() string {
637 return String(c.productVariables.Platform_version_name)
638}
639
Dan Albert4f378d72020-07-23 17:32:15 -0700640func (c *config) PlatformSdkVersion() ApiLevel {
641 return uncheckedFinalApiLevel(*c.productVariables.Platform_sdk_version)
Colin Cross30e076a2015-04-13 13:58:27 -0700642}
643
Colin Crossd09b0b62018-04-18 11:06:47 -0700644func (c *config) PlatformSdkCodename() string {
645 return String(c.productVariables.Platform_sdk_codename)
646}
647
Colin Cross092c9da2019-04-02 22:56:43 -0700648func (c *config) PlatformSecurityPatch() string {
649 return String(c.productVariables.Platform_security_patch)
650}
651
652func (c *config) PlatformPreviewSdkVersion() string {
653 return String(c.productVariables.Platform_preview_sdk_version)
654}
655
656func (c *config) PlatformMinSupportedTargetSdkVersion() string {
657 return String(c.productVariables.Platform_min_supported_target_sdk_version)
658}
659
660func (c *config) PlatformBaseOS() string {
661 return String(c.productVariables.Platform_base_os)
662}
663
Dan Albert1a246272020-07-06 14:49:35 -0700664func (c *config) MinSupportedSdkVersion() ApiLevel {
665 return uncheckedFinalApiLevel(16)
666}
667
668func (c *config) FinalApiLevels() []ApiLevel {
669 var levels []ApiLevel
Dan Albert4f378d72020-07-23 17:32:15 -0700670 for i := 1; i <= c.PlatformSdkVersion().FinalOrFutureInt(); i++ {
Dan Albert1a246272020-07-06 14:49:35 -0700671 levels = append(levels, uncheckedFinalApiLevel(i))
672 }
673 return levels
674}
675
676func (c *config) PreviewApiLevels() []ApiLevel {
677 var levels []ApiLevel
678 for i, codename := range c.PlatformVersionActiveCodenames() {
679 levels = append(levels, ApiLevel{
680 value: codename,
681 number: i,
682 isPreview: true,
683 })
684 }
685 return levels
686}
687
688func (c *config) AllSupportedApiLevels() []ApiLevel {
689 var levels []ApiLevel
690 levels = append(levels, c.FinalApiLevels()...)
691 return append(levels, c.PreviewApiLevels()...)
Dan Albertf5415d72017-08-17 16:19:59 -0700692}
693
Jingwen Chenc711fec2020-11-22 23:52:50 -0500694// DefaultAppTargetSdk returns the API level that platform apps are targeting.
695// This converts a codename to the exact ApiLevel it represents.
Dan Albert4f378d72020-07-23 17:32:15 -0700696func (c *config) DefaultAppTargetSdk(ctx EarlyModuleContext) ApiLevel {
Colin Crossd09b0b62018-04-18 11:06:47 -0700697 if Bool(c.productVariables.Platform_sdk_final) {
698 return c.PlatformSdkVersion()
Colin Crossd09b0b62018-04-18 11:06:47 -0700699 }
Jingwen Chenc711fec2020-11-22 23:52:50 -0500700 codename := c.PlatformSdkCodename()
701 if codename == "" {
702 return NoneApiLevel
703 }
704 if codename == "REL" {
705 panic("Platform_sdk_codename should not be REL when Platform_sdk_final is true")
706 }
707 return ApiLevelOrPanic(ctx, codename)
Colin Crossd09b0b62018-04-18 11:06:47 -0700708}
709
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800710func (c *config) AppsDefaultVersionName() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800711 return String(c.productVariables.AppsDefaultVersionName)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800712}
713
Dan Albert31384de2017-07-28 12:39:46 -0700714// Codenames that are active in the current lunch target.
715func (c *config) PlatformVersionActiveCodenames() []string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800716 return c.productVariables.Platform_version_active_codenames
Dan Albert31384de2017-07-28 12:39:46 -0700717}
718
Colin Crossface4e42017-10-30 17:32:15 -0700719func (c *config) ProductAAPTConfig() []string {
Colin Crossa74ca042019-01-31 14:31:51 -0800720 return c.productVariables.AAPTConfig
Colin Cross30e076a2015-04-13 13:58:27 -0700721}
722
Colin Crossface4e42017-10-30 17:32:15 -0700723func (c *config) ProductAAPTPreferredConfig() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800724 return String(c.productVariables.AAPTPreferredConfig)
Colin Cross30e076a2015-04-13 13:58:27 -0700725}
726
Colin Crossface4e42017-10-30 17:32:15 -0700727func (c *config) ProductAAPTCharacteristics() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800728 return String(c.productVariables.AAPTCharacteristics)
Colin Crossface4e42017-10-30 17:32:15 -0700729}
730
731func (c *config) ProductAAPTPrebuiltDPI() []string {
Colin Crossa74ca042019-01-31 14:31:51 -0800732 return c.productVariables.AAPTPrebuiltDPI
Colin Cross30e076a2015-04-13 13:58:27 -0700733}
734
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700735func (c *config) DefaultAppCertificateDir(ctx PathContext) SourcePath {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800736 defaultCert := String(c.productVariables.DefaultAppCertificate)
Colin Cross61ae0b72017-12-01 17:16:02 -0800737 if defaultCert != "" {
738 return PathForSource(ctx, filepath.Dir(defaultCert))
Colin Cross61ae0b72017-12-01 17:16:02 -0800739 }
Jingwen Chenc711fec2020-11-22 23:52:50 -0500740 return PathForSource(ctx, "build/make/target/product/security")
Colin Cross30e076a2015-04-13 13:58:27 -0700741}
742
Colin Crosse1731a52017-12-14 11:22:55 -0800743func (c *config) DefaultAppCertificate(ctx PathContext) (pem, key SourcePath) {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800744 defaultCert := String(c.productVariables.DefaultAppCertificate)
Colin Cross61ae0b72017-12-01 17:16:02 -0800745 if defaultCert != "" {
Colin Crosse1731a52017-12-14 11:22:55 -0800746 return PathForSource(ctx, defaultCert+".x509.pem"), PathForSource(ctx, defaultCert+".pk8")
Colin Cross61ae0b72017-12-01 17:16:02 -0800747 }
Jingwen Chenc711fec2020-11-22 23:52:50 -0500748 defaultDir := c.DefaultAppCertificateDir(ctx)
749 return defaultDir.Join(ctx, "testkey.x509.pem"), defaultDir.Join(ctx, "testkey.pk8")
Colin Cross30e076a2015-04-13 13:58:27 -0700750}
Colin Cross6ff51382015-12-17 16:39:19 -0800751
Jiyong Park9335a262018-12-24 11:31:58 +0900752func (c *config) ApexKeyDir(ctx ModuleContext) SourcePath {
753 // TODO(b/121224311): define another variable such as TARGET_APEX_KEY_OVERRIDE
754 defaultCert := String(c.productVariables.DefaultAppCertificate)
Dan Willemsen412160e2019-04-09 21:36:26 -0700755 if defaultCert == "" || filepath.Dir(defaultCert) == "build/make/target/product/security" {
Jiyong Park9335a262018-12-24 11:31:58 +0900756 // When defaultCert is unset or is set to the testkeys path, use the APEX keys
757 // that is under the module dir
Colin Cross07e51612019-03-05 12:46:40 -0800758 return pathForModuleSrc(ctx)
Jiyong Park9335a262018-12-24 11:31:58 +0900759 }
Jingwen Chenc711fec2020-11-22 23:52:50 -0500760 // If not, APEX keys are under the specified directory
761 return PathForSource(ctx, filepath.Dir(defaultCert))
Jiyong Park9335a262018-12-24 11:31:58 +0900762}
763
Jingwen Chenc711fec2020-11-22 23:52:50 -0500764// AllowMissingDependencies configures Blueprint/Soong to not fail when modules
765// are configured to depend on non-existent modules. Note that this does not
766// affect missing input dependencies at the Ninja level.
Colin Cross6ff51382015-12-17 16:39:19 -0800767func (c *config) AllowMissingDependencies() bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800768 return Bool(c.productVariables.Allow_missing_dependencies)
Colin Cross6ff51382015-12-17 16:39:19 -0800769}
Dan Willemsen322acaf2016-01-12 23:07:05 -0800770
Jeongik Cha816a23a2020-07-08 01:09:23 +0900771// Returns true if a full platform source tree cannot be assumed.
Colin Crossfc3674a2017-09-18 17:41:52 -0700772func (c *config) UnbundledBuild() bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800773 return Bool(c.productVariables.Unbundled_build)
Colin Crossfc3674a2017-09-18 17:41:52 -0700774}
775
Martin Stjernholmfd9eb4b2020-06-17 01:13:15 +0100776// Returns true if building apps that aren't bundled with the platform.
777// UnbundledBuild() is always true when this is true.
778func (c *config) UnbundledBuildApps() bool {
779 return Bool(c.productVariables.Unbundled_build_apps)
780}
781
Jeongik Cha816a23a2020-07-08 01:09:23 +0900782// Returns true if building modules against prebuilt SDKs.
783func (c *config) AlwaysUsePrebuiltSdks() bool {
784 return Bool(c.productVariables.Always_use_prebuilt_sdks)
Colin Cross1f367bf2018-12-18 22:46:24 -0800785}
786
Paul Duffin9a89a2a2020-10-28 19:20:06 +0000787// Returns true if the boot jars check should be skipped.
788func (c *config) SkipBootJarsCheck() bool {
789 return Bool(c.productVariables.Skip_boot_jars_check)
790}
791
Doug Horn21b94272019-01-16 12:06:11 -0800792func (c *config) Fuchsia() bool {
793 return Bool(c.productVariables.Fuchsia)
794}
795
Colin Cross126a25c2017-10-31 13:55:34 -0700796func (c *config) MinimizeJavaDebugInfo() bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800797 return Bool(c.productVariables.MinimizeJavaDebugInfo) && !Bool(c.productVariables.Eng)
Colin Cross126a25c2017-10-31 13:55:34 -0700798}
799
Colin Crossed064c02018-09-05 16:28:13 -0700800func (c *config) Debuggable() bool {
801 return Bool(c.productVariables.Debuggable)
802}
803
Jaewoong Jung1d6eb682018-11-29 15:08:44 -0800804func (c *config) Eng() bool {
805 return Bool(c.productVariables.Eng)
806}
807
Jiyong Park8d52f862018-07-07 18:02:07 +0900808func (c *config) DevicePrimaryArchType() ArchType {
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700809 return c.Targets[Android][0].Arch.ArchType
Jiyong Park8d52f862018-07-07 18:02:07 +0900810}
811
Colin Cross16b23492016-01-06 14:41:07 -0800812func (c *config) SanitizeHost() []string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800813 return append([]string(nil), c.productVariables.SanitizeHost...)
Colin Cross16b23492016-01-06 14:41:07 -0800814}
815
816func (c *config) SanitizeDevice() []string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800817 return append([]string(nil), c.productVariables.SanitizeDevice...)
Colin Cross23ae82a2016-11-02 14:34:39 -0700818}
819
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700820func (c *config) SanitizeDeviceDiag() []string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800821 return append([]string(nil), c.productVariables.SanitizeDeviceDiag...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700822}
823
Colin Cross23ae82a2016-11-02 14:34:39 -0700824func (c *config) SanitizeDeviceArch() []string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800825 return append([]string(nil), c.productVariables.SanitizeDeviceArch...)
Colin Cross16b23492016-01-06 14:41:07 -0800826}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700827
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800828func (c *config) EnableCFI() bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800829 if c.productVariables.EnableCFI == nil {
Vishwath Mohanc32c3eb2017-01-24 14:20:54 -0800830 return true
Vishwath Mohanc32c3eb2017-01-24 14:20:54 -0800831 }
Jingwen Chenc711fec2020-11-22 23:52:50 -0500832 return *c.productVariables.EnableCFI
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800833}
834
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800835func (c *config) DisableScudo() bool {
836 return Bool(c.productVariables.DisableScudo)
837}
838
Colin Crossa1ad8d12016-06-01 17:09:44 -0700839func (c *config) Android64() bool {
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700840 for _, t := range c.Targets[Android] {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700841 if t.Arch.ArchType.Multilib == "lib64" {
842 return true
843 }
844 }
845
846 return false
847}
Colin Cross9272ade2016-08-17 15:24:12 -0700848
Colin Cross9d45bb72016-08-29 16:14:13 -0700849func (c *config) UseGoma() bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800850 return Bool(c.productVariables.UseGoma)
Colin Cross9d45bb72016-08-29 16:14:13 -0700851}
852
Ramy Medhatbbf25672019-07-17 12:30:04 +0000853func (c *config) UseRBE() bool {
854 return Bool(c.productVariables.UseRBE)
855}
856
Ramy Medhat8ea054a2020-01-27 14:19:44 -0500857func (c *config) UseRBEJAVAC() bool {
858 return Bool(c.productVariables.UseRBEJAVAC)
859}
860
861func (c *config) UseRBER8() bool {
862 return Bool(c.productVariables.UseRBER8)
863}
864
865func (c *config) UseRBED8() bool {
866 return Bool(c.productVariables.UseRBED8)
867}
868
Colin Cross8b8bec32019-11-15 13:18:43 -0800869func (c *config) UseRemoteBuild() bool {
870 return c.UseGoma() || c.UseRBE()
871}
872
Colin Cross66548102018-06-19 22:47:35 -0700873func (c *config) RunErrorProne() bool {
874 return c.IsEnvTrue("RUN_ERROR_PRONE")
875}
876
Jingwen Chenc711fec2020-11-22 23:52:50 -0500877// XrefCorpusName returns the Kythe cross-reference corpus name.
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800878func (c *config) XrefCorpusName() string {
879 return c.Getenv("XREF_CORPUS")
880}
881
Jingwen Chenc711fec2020-11-22 23:52:50 -0500882// XrefCuEncoding returns the compilation unit encoding to use for Kythe code
883// xrefs. Can be 'json' (default), 'proto' or 'all'.
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800884func (c *config) XrefCuEncoding() string {
885 if enc := c.Getenv("KYTHE_KZIP_ENCODING"); enc != "" {
886 return enc
887 }
888 return "json"
889}
890
Sasha Smundakb0addaf2021-02-16 10:39:40 -0800891// XrefCuJavaSourceMax returns the maximum number of the Java source files
892// in a single compilation unit
893const xrefJavaSourceFileMaxDefault = "1000"
894
895func (c Config) XrefCuJavaSourceMax() string {
896 v := c.Getenv("KYTHE_JAVA_SOURCE_BATCH_SIZE")
897 if v == "" {
898 return xrefJavaSourceFileMaxDefault
899 }
900 if _, err := strconv.ParseUint(v, 0, 0); err != nil {
901 fmt.Fprintf(os.Stderr,
902 "bad KYTHE_JAVA_SOURCE_BATCH_SIZE value: %s, will use %s",
903 err, xrefJavaSourceFileMaxDefault)
904 return xrefJavaSourceFileMaxDefault
905 }
906 return v
907
908}
909
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800910func (c *config) EmitXrefRules() bool {
911 return c.XrefCorpusName() != ""
912}
913
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700914func (c *config) ClangTidy() bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800915 return Bool(c.productVariables.ClangTidy)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700916}
917
918func (c *config) TidyChecks() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800919 if c.productVariables.TidyChecks == nil {
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700920 return ""
921 }
Dan Willemsen45133ac2018-03-09 21:22:06 -0800922 return *c.productVariables.TidyChecks
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700923}
924
Colin Cross0f4e0d62016-07-27 10:56:55 -0700925func (c *config) LibartImgHostBaseAddress() string {
926 return "0x60000000"
927}
928
929func (c *config) LibartImgDeviceBaseAddress() string {
Elliott Hughesda3a0712020-03-06 16:55:28 -0800930 return "0x70000000"
Colin Cross0f4e0d62016-07-27 10:56:55 -0700931}
932
Hiroshi Yamauchie2a10632016-12-19 13:44:41 -0800933func (c *config) ArtUseReadBarrier() bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800934 return Bool(c.productVariables.ArtUseReadBarrier)
Hiroshi Yamauchie2a10632016-12-19 13:44:41 -0800935}
936
Jingwen Chenc711fec2020-11-22 23:52:50 -0500937// Enforce Runtime Resource Overlays for a module. RROs supersede static RROs,
938// but some modules still depend on it.
939//
940// More info: https://source.android.com/devices/architecture/rros
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700941func (c *config) EnforceRROForModule(name string) bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800942 enforceList := c.productVariables.EnforceRROTargets
Jeongik Chacee5ba92021-02-19 12:11:51 +0900943
Roland Levillainf6cc2612020-07-09 16:58:14 +0100944 if len(enforceList) > 0 {
Yo Chiang4ebd06a2019-10-01 13:13:41 +0800945 if InList("*", enforceList) {
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700946 return true
947 }
Colin Crossa74ca042019-01-31 14:31:51 -0800948 return InList(name, enforceList)
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700949 }
950 return false
951}
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700952func (c *config) EnforceRROExcludedOverlay(path string) bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800953 excluded := c.productVariables.EnforceRROExcludedOverlays
Roland Levillainf6cc2612020-07-09 16:58:14 +0100954 if len(excluded) > 0 {
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800955 return HasAnyPrefix(path, excluded)
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700956 }
957 return false
958}
959
960func (c *config) ExportedNamespaces() []string {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800961 return append([]string(nil), c.productVariables.NamespacesToExport...)
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700962}
963
964func (c *config) HostStaticBinaries() bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800965 return Bool(c.productVariables.HostStaticBinaries)
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700966}
967
Colin Cross5a0dcd52018-10-05 14:20:06 -0700968func (c *config) UncompressPrivAppDex() bool {
969 return Bool(c.productVariables.UncompressPrivAppDex)
970}
971
972func (c *config) ModulesLoadedByPrivilegedModules() []string {
973 return c.productVariables.ModulesLoadedByPrivilegedModules
974}
975
Jingwen Chenc711fec2020-11-22 23:52:50 -0500976// DexpreoptGlobalConfigPath returns the path to the dexpreopt.config file in
977// the output directory, if it was created during the product configuration
978// phase by Kati.
Jingwen Chenebb0b572020-11-02 00:24:57 -0500979func (c *config) DexpreoptGlobalConfigPath(ctx PathContext) OptionalPath {
Colin Cross988414c2020-01-11 01:11:46 +0000980 if c.productVariables.DexpreoptGlobalConfig == nil {
Jingwen Chenebb0b572020-11-02 00:24:57 -0500981 return OptionalPathForPath(nil)
982 }
983 return OptionalPathForPath(
984 pathForBuildToolDep(ctx, *c.productVariables.DexpreoptGlobalConfig))
985}
986
Jingwen Chenc711fec2020-11-22 23:52:50 -0500987// DexpreoptGlobalConfig returns the raw byte contents of the dexpreopt global
988// configuration. Since the configuration file was created by Kati during
989// product configuration (externally of soong_build), it's not tracked, so we
990// also manually add a Ninja file dependency on the configuration file to the
991// rule that creates the main build.ninja file. This ensures that build.ninja is
992// regenerated correctly if dexpreopt.config changes.
Jingwen Chenebb0b572020-11-02 00:24:57 -0500993func (c *config) DexpreoptGlobalConfig(ctx PathContext) ([]byte, error) {
994 path := c.DexpreoptGlobalConfigPath(ctx)
995 if !path.Valid() {
Colin Cross988414c2020-01-11 01:11:46 +0000996 return nil, nil
997 }
Jingwen Chenebb0b572020-11-02 00:24:57 -0500998 ctx.AddNinjaFileDeps(path.String())
999 return ioutil.ReadFile(absolutePath(path.String()))
Colin Cross43f08db2018-11-12 10:13:39 -08001000}
1001
David Brazdil91b4e3e2019-01-23 21:04:05 +00001002func (c *config) FrameworksBaseDirExists(ctx PathContext) bool {
1003 return ExistentPathForSource(ctx, "frameworks", "base").Valid()
1004}
1005
Inseob Kimae553032019-05-14 18:52:49 +09001006func (c *config) VndkSnapshotBuildArtifacts() bool {
1007 return Bool(c.productVariables.VndkSnapshotBuildArtifacts)
1008}
1009
Colin Cross3b19f5d2019-09-17 14:45:31 -07001010func (c *config) HasMultilibConflict(arch ArchType) bool {
1011 return c.multilibConflicts[arch]
1012}
1013
Bill Peckhambae47492021-01-08 09:34:44 -08001014func (c *config) PrebuiltHiddenApiDir(ctx PathContext) string {
1015 return String(c.productVariables.PrebuiltHiddenApiDir)
1016}
1017
Colin Cross9272ade2016-08-17 15:24:12 -07001018func (c *deviceConfig) Arches() []Arch {
1019 var arches []Arch
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001020 for _, target := range c.config.Targets[Android] {
Colin Cross9272ade2016-08-17 15:24:12 -07001021 arches = append(arches, target.Arch)
1022 }
1023 return arches
1024}
Dan Willemsend2ede872016-11-18 14:54:24 -08001025
Jayant Chowdhary34ce67d2018-03-08 11:00:50 -08001026func (c *deviceConfig) BinderBitness() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001027 is32BitBinder := c.config.productVariables.Binder32bit
Jayant Chowdhary34ce67d2018-03-08 11:00:50 -08001028 if is32BitBinder != nil && *is32BitBinder {
1029 return "32"
1030 }
1031 return "64"
1032}
1033
Dan Willemsen4353bc42016-12-05 17:16:02 -08001034func (c *deviceConfig) VendorPath() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001035 if c.config.productVariables.VendorPath != nil {
1036 return *c.config.productVariables.VendorPath
Dan Willemsen4353bc42016-12-05 17:16:02 -08001037 }
1038 return "vendor"
1039}
1040
Justin Yun71549282017-11-17 12:10:28 +09001041func (c *deviceConfig) VndkVersion() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001042 return String(c.config.productVariables.DeviceVndkVersion)
Justin Yun71549282017-11-17 12:10:28 +09001043}
1044
Jose Galmes6f843bc2020-12-11 13:36:29 -08001045func (c *deviceConfig) RecoverySnapshotVersion() string {
1046 return String(c.config.productVariables.RecoverySnapshotVersion)
1047}
1048
Jeongik Cha219141c2020-08-06 23:00:37 +09001049func (c *deviceConfig) CurrentApiLevelForVendorModules() string {
1050 return StringDefault(c.config.productVariables.DeviceCurrentApiLevelForVendorModules, "current")
1051}
1052
Justin Yun8fe12122017-12-07 17:18:15 +09001053func (c *deviceConfig) PlatformVndkVersion() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001054 return String(c.config.productVariables.Platform_vndk_version)
Justin Yun8fe12122017-12-07 17:18:15 +09001055}
1056
Justin Yun5f7f7e82019-11-18 19:52:14 +09001057func (c *deviceConfig) ProductVndkVersion() string {
1058 return String(c.config.productVariables.ProductVndkVersion)
1059}
1060
Justin Yun71549282017-11-17 12:10:28 +09001061func (c *deviceConfig) ExtraVndkVersions() []string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001062 return c.config.productVariables.ExtraVndkVersions
Dan Willemsend2ede872016-11-18 14:54:24 -08001063}
Jack He8cc71432016-12-08 15:45:07 -08001064
Vic Yangefd249e2018-11-12 20:19:56 -08001065func (c *deviceConfig) VndkUseCoreVariant() bool {
1066 return Bool(c.config.productVariables.VndkUseCoreVariant)
1067}
1068
Jiyong Park1a5d7b12018-01-15 15:05:10 +09001069func (c *deviceConfig) SystemSdkVersions() []string {
Colin Crossa74ca042019-01-31 14:31:51 -08001070 return c.config.productVariables.DeviceSystemSdkVersions
Jiyong Park1a5d7b12018-01-15 15:05:10 +09001071}
1072
1073func (c *deviceConfig) PlatformSystemSdkVersions() []string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001074 return c.config.productVariables.Platform_systemsdk_versions
Jiyong Park1a5d7b12018-01-15 15:05:10 +09001075}
1076
Jiyong Park2db76922017-11-08 16:03:48 +09001077func (c *deviceConfig) OdmPath() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001078 if c.config.productVariables.OdmPath != nil {
1079 return *c.config.productVariables.OdmPath
Jiyong Park2db76922017-11-08 16:03:48 +09001080 }
1081 return "odm"
1082}
1083
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +09001084func (c *deviceConfig) ProductPath() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001085 if c.config.productVariables.ProductPath != nil {
1086 return *c.config.productVariables.ProductPath
Jiyong Park2db76922017-11-08 16:03:48 +09001087 }
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +09001088 return "product"
Jiyong Park2db76922017-11-08 16:03:48 +09001089}
1090
Justin Yund5f6c822019-06-25 16:47:17 +09001091func (c *deviceConfig) SystemExtPath() string {
1092 if c.config.productVariables.SystemExtPath != nil {
1093 return *c.config.productVariables.SystemExtPath
Dario Frenifd05a742018-05-29 13:28:54 +01001094 }
Justin Yund5f6c822019-06-25 16:47:17 +09001095 return "system_ext"
Dario Frenifd05a742018-05-29 13:28:54 +01001096}
1097
Jack He8cc71432016-12-08 15:45:07 -08001098func (c *deviceConfig) BtConfigIncludeDir() string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001099 return String(c.config.productVariables.BtConfigIncludeDir)
Jack He8cc71432016-12-08 15:45:07 -08001100}
Dan Willemsen581341d2017-02-09 16:16:31 -08001101
Jiyong Parkd773eb32017-07-03 13:18:12 +09001102func (c *deviceConfig) DeviceKernelHeaderDirs() []string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001103 return c.config.productVariables.DeviceKernelHeaders
Jiyong Parkd773eb32017-07-03 13:18:12 +09001104}
1105
Yi Kongceb5b762020-03-20 15:22:27 +08001106func (c *deviceConfig) SamplingPGO() bool {
1107 return Bool(c.config.productVariables.SamplingPGO)
1108}
1109
Roland Levillainada12702020-06-09 13:07:36 +01001110// JavaCoverageEnabledForPath returns whether Java code coverage is enabled for
1111// path. Coverage is enabled by default when the product variable
1112// JavaCoveragePaths is empty. If JavaCoveragePaths is not empty, coverage is
1113// enabled for any path which is part of this variable (and not part of the
1114// JavaCoverageExcludePaths product variable). Value "*" in JavaCoveragePaths
1115// represents any path.
1116func (c *deviceConfig) JavaCoverageEnabledForPath(path string) bool {
1117 coverage := false
Chris Gross2f748692020-06-24 20:36:59 +00001118 if len(c.config.productVariables.JavaCoveragePaths) == 0 ||
Roland Levillainada12702020-06-09 13:07:36 +01001119 InList("*", c.config.productVariables.JavaCoveragePaths) ||
1120 HasAnyPrefix(path, c.config.productVariables.JavaCoveragePaths) {
1121 coverage = true
1122 }
Roland Levillainf6cc2612020-07-09 16:58:14 +01001123 if coverage && len(c.config.productVariables.JavaCoverageExcludePaths) > 0 {
Roland Levillainada12702020-06-09 13:07:36 +01001124 if HasAnyPrefix(path, c.config.productVariables.JavaCoverageExcludePaths) {
1125 coverage = false
1126 }
1127 }
1128 return coverage
1129}
1130
Colin Cross1a6acd42020-06-16 17:51:46 -07001131// Returns true if gcov or clang coverage is enabled.
Dan Willemsen581341d2017-02-09 16:16:31 -08001132func (c *deviceConfig) NativeCoverageEnabled() bool {
Colin Cross1a6acd42020-06-16 17:51:46 -07001133 return Bool(c.config.productVariables.GcovCoverage) ||
1134 Bool(c.config.productVariables.ClangCoverage)
Dan Willemsen581341d2017-02-09 16:16:31 -08001135}
1136
Oliver Nguyen1382ab62019-12-06 15:22:41 -08001137func (c *deviceConfig) ClangCoverageEnabled() bool {
1138 return Bool(c.config.productVariables.ClangCoverage)
1139}
1140
Colin Cross1a6acd42020-06-16 17:51:46 -07001141func (c *deviceConfig) GcovCoverageEnabled() bool {
1142 return Bool(c.config.productVariables.GcovCoverage)
1143}
1144
Roland Levillain4f5297b2020-06-09 12:44:06 +01001145// NativeCoverageEnabledForPath returns whether (GCOV- or Clang-based) native
1146// code coverage is enabled for path. By default, coverage is not enabled for a
1147// given path unless it is part of the NativeCoveragePaths product variable (and
1148// not part of the NativeCoverageExcludePaths product variable). Value "*" in
1149// NativeCoveragePaths represents any path.
1150func (c *deviceConfig) NativeCoverageEnabledForPath(path string) bool {
Ryan Campbell469a18a2017-02-27 09:01:54 -08001151 coverage := false
Roland Levillainf6cc2612020-07-09 16:58:14 +01001152 if len(c.config.productVariables.NativeCoveragePaths) > 0 {
Roland Levillain4f5297b2020-06-09 12:44:06 +01001153 if InList("*", c.config.productVariables.NativeCoveragePaths) || HasAnyPrefix(path, c.config.productVariables.NativeCoveragePaths) {
Ivan Lozano5f595532017-07-13 14:46:05 -07001154 coverage = true
Dan Willemsen581341d2017-02-09 16:16:31 -08001155 }
1156 }
Roland Levillainf6cc2612020-07-09 16:58:14 +01001157 if coverage && len(c.config.productVariables.NativeCoverageExcludePaths) > 0 {
Roland Levillain4f5297b2020-06-09 12:44:06 +01001158 if HasAnyPrefix(path, c.config.productVariables.NativeCoverageExcludePaths) {
Ivan Lozano5f595532017-07-13 14:46:05 -07001159 coverage = false
Ryan Campbell469a18a2017-02-27 09:01:54 -08001160 }
1161 }
1162 return coverage
Dan Willemsen581341d2017-02-09 16:16:31 -08001163}
Ivan Lozano5f595532017-07-13 14:46:05 -07001164
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -08001165func (c *deviceConfig) PgoAdditionalProfileDirs() []string {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001166 return c.config.productVariables.PgoAdditionalProfileDirs
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -08001167}
1168
Tri Vo35a51432018-03-25 20:00:00 -07001169func (c *deviceConfig) VendorSepolicyDirs() []string {
1170 return c.config.productVariables.BoardVendorSepolicyDirs
1171}
1172
1173func (c *deviceConfig) OdmSepolicyDirs() []string {
1174 return c.config.productVariables.BoardOdmSepolicyDirs
1175}
1176
Felixa20a8752020-05-17 18:28:35 +02001177func (c *deviceConfig) SystemExtPublicSepolicyDirs() []string {
1178 return c.config.productVariables.SystemExtPublicSepolicyDirs
Tri Vo35a51432018-03-25 20:00:00 -07001179}
1180
Felixa20a8752020-05-17 18:28:35 +02001181func (c *deviceConfig) SystemExtPrivateSepolicyDirs() []string {
1182 return c.config.productVariables.SystemExtPrivateSepolicyDirs
Tri Vo35a51432018-03-25 20:00:00 -07001183}
1184
Inseob Kim0866b002019-04-15 20:21:29 +09001185func (c *deviceConfig) SepolicyM4Defs() []string {
1186 return c.config.productVariables.BoardSepolicyM4Defs
1187}
1188
Jiyong Park7f67f482019-01-05 12:57:48 +09001189func (c *deviceConfig) OverrideManifestPackageNameFor(name string) (manifestName string, overridden bool) {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001190 return findOverrideValue(c.config.productVariables.ManifestPackageNameOverrides, name,
1191 "invalid override rule %q in PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES should be <module_name>:<manifest_name>")
1192}
1193
1194func (c *deviceConfig) OverrideCertificateFor(name string) (certificatePath string, overridden bool) {
Jaewoong Jungacb6db32019-02-28 16:22:30 +00001195 return findOverrideValue(c.config.productVariables.CertificateOverrides, name,
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001196 "invalid override rule %q in PRODUCT_CERTIFICATE_OVERRIDES should be <module_name>:<certificate_module_name>")
1197}
1198
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001199func (c *deviceConfig) OverridePackageNameFor(name string) string {
1200 newName, overridden := findOverrideValue(
1201 c.config.productVariables.PackageNameOverrides,
1202 name,
1203 "invalid override rule %q in PRODUCT_PACKAGE_NAME_OVERRIDES should be <module_name>:<package_name>")
1204 if overridden {
1205 return newName
1206 }
1207 return name
1208}
1209
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001210func findOverrideValue(overrides []string, name string, errorMsg string) (newValue string, overridden bool) {
Jiyong Park7f67f482019-01-05 12:57:48 +09001211 if overrides == nil || len(overrides) == 0 {
1212 return "", false
1213 }
1214 for _, o := range overrides {
1215 split := strings.Split(o, ":")
1216 if len(split) != 2 {
1217 // This shouldn't happen as this is first checked in make, but just in case.
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001218 panic(fmt.Errorf(errorMsg, o))
Jiyong Park7f67f482019-01-05 12:57:48 +09001219 }
1220 if matchPattern(split[0], name) {
1221 return substPattern(split[0], split[1], name), true
1222 }
1223 }
1224 return "", false
1225}
1226
Ivan Lozano5f595532017-07-13 14:46:05 -07001227func (c *config) IntegerOverflowDisabledForPath(path string) bool {
Roland Levillainf6cc2612020-07-09 16:58:14 +01001228 if len(c.productVariables.IntegerOverflowExcludePaths) == 0 {
Ivan Lozano5f595532017-07-13 14:46:05 -07001229 return false
1230 }
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001231 return HasAnyPrefix(path, c.productVariables.IntegerOverflowExcludePaths)
Ivan Lozano5f595532017-07-13 14:46:05 -07001232}
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -07001233
1234func (c *config) CFIDisabledForPath(path string) bool {
Roland Levillainf6cc2612020-07-09 16:58:14 +01001235 if len(c.productVariables.CFIExcludePaths) == 0 {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -07001236 return false
1237 }
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001238 return HasAnyPrefix(path, c.productVariables.CFIExcludePaths)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -07001239}
1240
1241func (c *config) CFIEnabledForPath(path string) bool {
Roland Levillainf6cc2612020-07-09 16:58:14 +01001242 if len(c.productVariables.CFIIncludePaths) == 0 {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -07001243 return false
1244 }
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001245 return HasAnyPrefix(path, c.productVariables.CFIIncludePaths)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -07001246}
Colin Crosse15ddaf2017-12-04 11:24:31 -08001247
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08001248func (c *config) MemtagHeapDisabledForPath(path string) bool {
1249 if len(c.productVariables.MemtagHeapExcludePaths) == 0 {
1250 return false
1251 }
1252 return HasAnyPrefix(path, c.productVariables.MemtagHeapExcludePaths)
1253}
1254
1255func (c *config) MemtagHeapAsyncEnabledForPath(path string) bool {
1256 if len(c.productVariables.MemtagHeapAsyncIncludePaths) == 0 {
1257 return false
1258 }
1259 return HasAnyPrefix(path, c.productVariables.MemtagHeapAsyncIncludePaths)
1260}
1261
1262func (c *config) MemtagHeapSyncEnabledForPath(path string) bool {
1263 if len(c.productVariables.MemtagHeapSyncIncludePaths) == 0 {
1264 return false
1265 }
1266 return HasAnyPrefix(path, c.productVariables.MemtagHeapSyncIncludePaths)
1267}
1268
Dan Willemsen0fe78662018-03-26 12:41:18 -07001269func (c *config) VendorConfig(name string) VendorConfig {
Colin Cross9d34f352019-11-22 16:03:51 -08001270 return soongconfig.Config(c.productVariables.VendorVars[name])
Dan Willemsen0fe78662018-03-26 12:41:18 -07001271}
1272
Colin Cross395f2cf2018-10-24 16:10:32 -07001273func (c *config) NdkAbis() bool {
1274 return Bool(c.productVariables.Ndk_abis)
1275}
1276
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001277func (c *config) AmlAbis() bool {
1278 return Bool(c.productVariables.Aml_abis)
1279}
1280
Dan Albert23d37e02018-11-28 08:30:10 -08001281func (c *config) ExcludeDraftNdkApis() bool {
1282 return Bool(c.productVariables.Exclude_draft_ndk_apis)
1283}
1284
Jiyong Park8fd61922018-11-08 02:50:25 +09001285func (c *config) FlattenApex() bool {
Roland Levillaina3863212019-08-12 19:56:16 +01001286 return Bool(c.productVariables.Flatten_apex)
Jiyong Park8fd61922018-11-08 02:50:25 +09001287}
1288
Jiyong Park4da07972021-01-05 21:01:11 +09001289func (c *config) ForceApexSymlinkOptimization() bool {
1290 return Bool(c.productVariables.ForceApexSymlinkOptimization)
1291}
1292
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00001293func (c *config) CompressedApex() bool {
1294 return Bool(c.productVariables.CompressedApex)
1295}
1296
Jeongik Chac9464142019-01-07 12:07:27 +09001297func (c *config) EnforceSystemCertificate() bool {
1298 return Bool(c.productVariables.EnforceSystemCertificate)
1299}
1300
Colin Cross440e0d02020-06-11 11:32:11 -07001301func (c *config) EnforceSystemCertificateAllowList() []string {
1302 return c.productVariables.EnforceSystemCertificateAllowList
Jeongik Chac9464142019-01-07 12:07:27 +09001303}
1304
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001305func (c *config) EnforceProductPartitionInterface() bool {
1306 return Bool(c.productVariables.EnforceProductPartitionInterface)
1307}
1308
JaeMan Parkff715562020-10-19 17:25:58 +09001309func (c *config) EnforceInterPartitionJavaSdkLibrary() bool {
1310 return Bool(c.productVariables.EnforceInterPartitionJavaSdkLibrary)
1311}
1312
1313func (c *config) InterPartitionJavaLibraryAllowList() []string {
1314 return c.productVariables.InterPartitionJavaLibraryAllowList
1315}
1316
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09001317func (c *config) InstallExtraFlattenedApexes() bool {
1318 return Bool(c.productVariables.InstallExtraFlattenedApexes)
1319}
1320
Colin Crossf24a22a2019-01-31 14:12:44 -08001321func (c *config) ProductHiddenAPIStubs() []string {
1322 return c.productVariables.ProductHiddenAPIStubs
Colin Cross8faf8fc2019-01-16 15:15:52 -08001323}
1324
Colin Crossf24a22a2019-01-31 14:12:44 -08001325func (c *config) ProductHiddenAPIStubsSystem() []string {
1326 return c.productVariables.ProductHiddenAPIStubsSystem
Colin Cross8faf8fc2019-01-16 15:15:52 -08001327}
1328
Colin Crossf24a22a2019-01-31 14:12:44 -08001329func (c *config) ProductHiddenAPIStubsTest() []string {
1330 return c.productVariables.ProductHiddenAPIStubsTest
Colin Cross8faf8fc2019-01-16 15:15:52 -08001331}
Dan Willemsen71c74602019-04-10 12:27:35 -07001332
Dan Willemsen54879d12019-04-18 10:08:46 -07001333func (c *deviceConfig) TargetFSConfigGen() []string {
Dan Willemsen71c74602019-04-10 12:27:35 -07001334 return c.config.productVariables.TargetFSConfigGen
1335}
Inseob Kim0866b002019-04-15 20:21:29 +09001336
1337func (c *config) ProductPublicSepolicyDirs() []string {
1338 return c.productVariables.ProductPublicSepolicyDirs
1339}
1340
1341func (c *config) ProductPrivateSepolicyDirs() []string {
1342 return c.productVariables.ProductPrivateSepolicyDirs
1343}
1344
Colin Cross50ddcc42019-05-16 12:28:22 -07001345func (c *config) MissingUsesLibraries() []string {
1346 return c.productVariables.MissingUsesLibraries
1347}
1348
Inseob Kim1f086e22019-05-09 13:29:15 +09001349func (c *deviceConfig) DeviceArch() string {
1350 return String(c.config.productVariables.DeviceArch)
1351}
1352
1353func (c *deviceConfig) DeviceArchVariant() string {
1354 return String(c.config.productVariables.DeviceArchVariant)
1355}
1356
1357func (c *deviceConfig) DeviceSecondaryArch() string {
1358 return String(c.config.productVariables.DeviceSecondaryArch)
1359}
1360
1361func (c *deviceConfig) DeviceSecondaryArchVariant() string {
1362 return String(c.config.productVariables.DeviceSecondaryArchVariant)
1363}
Yifan Hong82db7352020-01-21 16:12:26 -08001364
1365func (c *deviceConfig) BoardUsesRecoveryAsBoot() bool {
1366 return Bool(c.config.productVariables.BoardUsesRecoveryAsBoot)
1367}
Yifan Hong97365ee2020-07-29 09:51:57 -07001368
1369func (c *deviceConfig) BoardKernelBinaries() []string {
1370 return c.config.productVariables.BoardKernelBinaries
1371}
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001372
Yifan Hong42bef8d2020-08-05 14:36:09 -07001373func (c *deviceConfig) BoardKernelModuleInterfaceVersions() []string {
1374 return c.config.productVariables.BoardKernelModuleInterfaceVersions
1375}
1376
Yifan Hongdd8dacc2020-10-21 15:40:17 -07001377func (c *deviceConfig) BoardMoveRecoveryResourcesToVendorBoot() bool {
1378 return Bool(c.config.productVariables.BoardMoveRecoveryResourcesToVendorBoot)
1379}
1380
Inseob Kim16ebd5a2020-12-09 23:08:17 +09001381func (c *deviceConfig) PlatformSepolicyVersion() string {
1382 return String(c.config.productVariables.PlatformSepolicyVersion)
1383}
1384
1385func (c *deviceConfig) BoardSepolicyVers() string {
1386 return String(c.config.productVariables.BoardSepolicyVers)
1387}
1388
1389func (c *deviceConfig) BoardReqdMaskPolicy() []string {
1390 return c.config.productVariables.BoardReqdMaskPolicy
1391}
1392
Inseob Kim7cf14652021-01-06 23:06:52 +09001393func (c *deviceConfig) DirectedVendorSnapshot() bool {
1394 return c.config.productVariables.DirectedVendorSnapshot
1395}
1396
1397func (c *deviceConfig) VendorSnapshotModules() map[string]bool {
1398 return c.config.productVariables.VendorSnapshotModules
1399}
1400
Jose Galmes4c6895e2021-02-09 07:44:30 -08001401func (c *deviceConfig) DirectedRecoverySnapshot() bool {
1402 return c.config.productVariables.DirectedRecoverySnapshot
1403}
1404
1405func (c *deviceConfig) RecoverySnapshotModules() map[string]bool {
1406 return c.config.productVariables.RecoverySnapshotModules
1407}
1408
Justin DeMartino383bfb32021-02-24 10:49:43 -08001409func createDirsMap(previous map[string]bool, dirs []string) (map[string]bool, error) {
1410 var ret = make(map[string]bool)
1411 for _, dir := range dirs {
1412 clean := filepath.Clean(dir)
1413 if previous[clean] || ret[clean] {
1414 return nil, fmt.Errorf("Duplicate entry %s", dir)
1415 }
1416 ret[clean] = true
1417 }
1418 return ret, nil
1419}
1420
1421func (c *deviceConfig) createDirsMapOnce(onceKey OnceKey, previous map[string]bool, dirs []string) map[string]bool {
1422 dirMap := c.Once(onceKey, func() interface{} {
1423 ret, err := createDirsMap(previous, dirs)
1424 if err != nil {
1425 panic(fmt.Errorf("%s: %w", onceKey.key, err))
1426 }
1427 return ret
1428 })
1429 if dirMap == nil {
1430 return nil
1431 }
1432 return dirMap.(map[string]bool)
1433}
1434
1435var vendorSnapshotDirsExcludedKey = NewOnceKey("VendorSnapshotDirsExcludedMap")
1436
1437func (c *deviceConfig) VendorSnapshotDirsExcludedMap() map[string]bool {
1438 return c.createDirsMapOnce(vendorSnapshotDirsExcludedKey, nil,
1439 c.config.productVariables.VendorSnapshotDirsExcluded)
1440}
1441
1442var vendorSnapshotDirsIncludedKey = NewOnceKey("VendorSnapshotDirsIncludedMap")
1443
1444func (c *deviceConfig) VendorSnapshotDirsIncludedMap() map[string]bool {
1445 excludedMap := c.VendorSnapshotDirsExcludedMap()
1446 return c.createDirsMapOnce(vendorSnapshotDirsIncludedKey, excludedMap,
1447 c.config.productVariables.VendorSnapshotDirsIncluded)
1448}
1449
1450var recoverySnapshotDirsExcludedKey = NewOnceKey("RecoverySnapshotDirsExcludedMap")
1451
1452func (c *deviceConfig) RecoverySnapshotDirsExcludedMap() map[string]bool {
1453 return c.createDirsMapOnce(recoverySnapshotDirsExcludedKey, nil,
1454 c.config.productVariables.RecoverySnapshotDirsExcluded)
1455}
1456
1457var recoverySnapshotDirsIncludedKey = NewOnceKey("RecoverySnapshotDirsIncludedMap")
1458
1459func (c *deviceConfig) RecoverySnapshotDirsIncludedMap() map[string]bool {
1460 excludedMap := c.RecoverySnapshotDirsExcludedMap()
1461 return c.createDirsMapOnce(recoverySnapshotDirsIncludedKey, excludedMap,
1462 c.config.productVariables.RecoverySnapshotDirsIncluded)
1463}
1464
Inseob Kim60c32f02020-12-21 22:53:05 +09001465func (c *deviceConfig) ShippingApiLevel() ApiLevel {
1466 if c.config.productVariables.ShippingApiLevel == nil {
1467 return NoneApiLevel
1468 }
1469 apiLevel, _ := strconv.Atoi(*c.config.productVariables.ShippingApiLevel)
1470 return uncheckedFinalApiLevel(apiLevel)
1471}
1472
Inseob Kim0cac7b42021-02-03 18:16:46 +09001473func (c *deviceConfig) BuildBrokenVendorPropertyNamespace() bool {
1474 return c.config.productVariables.BuildBrokenVendorPropertyNamespace
1475}
1476
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001477// The ConfiguredJarList struct provides methods for handling a list of (apex, jar) pairs.
1478// Such lists are used in the build system for things like bootclasspath jars or system server jars.
1479// The apex part is either an apex name, or a special names "platform" or "system_ext". Jar is a
1480// module name. The pairs come from Make product variables as a list of colon-separated strings.
1481//
1482// Examples:
1483// - "com.android.art:core-oj"
1484// - "platform:framework"
1485// - "system_ext:foo"
1486//
1487type ConfiguredJarList struct {
Jingwen Chenc711fec2020-11-22 23:52:50 -05001488 // A list of apex components, which can be an apex name,
1489 // or special names like "platform" or "system_ext".
1490 apexes []string
1491
1492 // A list of jar module name components.
1493 jars []string
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001494}
1495
Jingwen Chenc711fec2020-11-22 23:52:50 -05001496// Len returns the length of the list of jars.
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001497func (l *ConfiguredJarList) Len() int {
1498 return len(l.jars)
1499}
1500
Jingwen Chenc711fec2020-11-22 23:52:50 -05001501// Jar returns the idx-th jar component of (apex, jar) pairs.
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001502func (l *ConfiguredJarList) Jar(idx int) string {
1503 return l.jars[idx]
1504}
1505
Jingwen Chenc711fec2020-11-22 23:52:50 -05001506// Apex returns the idx-th apex component of (apex, jar) pairs.
Paul Duffin9a89a2a2020-10-28 19:20:06 +00001507func (l *ConfiguredJarList) Apex(idx int) string {
1508 return l.apexes[idx]
1509}
1510
Jingwen Chenc711fec2020-11-22 23:52:50 -05001511// ContainsJar returns true if the (apex, jar) pairs contains a pair with the
1512// given jar module name.
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001513func (l *ConfiguredJarList) ContainsJar(jar string) bool {
1514 return InList(jar, l.jars)
1515}
1516
1517// If the list contains the given (apex, jar) pair.
1518func (l *ConfiguredJarList) containsApexJarPair(apex, jar string) bool {
1519 for i := 0; i < l.Len(); i++ {
Paul Duffin1e8c6072020-10-23 18:28:55 +01001520 if apex == l.apexes[i] && jar == l.jars[i] {
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001521 return true
1522 }
1523 }
1524 return false
1525}
1526
Jingwen Chenc711fec2020-11-22 23:52:50 -05001527// IndexOfJar returns the first pair with the given jar name on the list, or -1
1528// if not found.
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001529func (l *ConfiguredJarList) IndexOfJar(jar string) int {
1530 return IndexList(jar, l.jars)
1531}
1532
Paul Duffin7d584e92020-10-23 18:26:03 +01001533func copyAndAppend(list []string, item string) []string {
1534 // Create the result list to be 1 longer than the input.
1535 result := make([]string, len(list)+1)
1536
1537 // Copy the whole input list into the result.
1538 count := copy(result, list)
1539
1540 // Insert the extra item at the end.
1541 result[count] = item
1542
1543 return result
1544}
1545
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001546// Append an (apex, jar) pair to the list.
Paul Duffin7d584e92020-10-23 18:26:03 +01001547func (l *ConfiguredJarList) Append(apex string, jar string) ConfiguredJarList {
1548 // Create a copy of the backing arrays before appending to avoid sharing backing
1549 // arrays that are mutated across instances.
1550 apexes := copyAndAppend(l.apexes, apex)
1551 jars := copyAndAppend(l.jars, jar)
1552
1553 return ConfiguredJarList{apexes, jars}
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001554}
1555
Jingwen Chenc711fec2020-11-22 23:52:50 -05001556// RemoveList filters out a list of (apex, jar) pairs from the receiving list of pairs.
Paul Duffin7d584e92020-10-23 18:26:03 +01001557func (l *ConfiguredJarList) RemoveList(list ConfiguredJarList) ConfiguredJarList {
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001558 apexes := make([]string, 0, l.Len())
1559 jars := make([]string, 0, l.Len())
1560
1561 for i, jar := range l.jars {
Paul Duffin1e8c6072020-10-23 18:28:55 +01001562 apex := l.apexes[i]
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001563 if !list.containsApexJarPair(apex, jar) {
1564 apexes = append(apexes, apex)
1565 jars = append(jars, jar)
1566 }
1567 }
1568
Paul Duffin7d584e92020-10-23 18:26:03 +01001569 return ConfiguredJarList{apexes, jars}
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001570}
1571
Jingwen Chenc711fec2020-11-22 23:52:50 -05001572// CopyOfJars returns a copy of the list of strings containing jar module name
1573// components.
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001574func (l *ConfiguredJarList) CopyOfJars() []string {
1575 return CopyOf(l.jars)
1576}
1577
Jingwen Chenc711fec2020-11-22 23:52:50 -05001578// CopyOfApexJarPairs returns a copy of the list of strings with colon-separated
1579// (apex, jar) pairs.
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001580func (l *ConfiguredJarList) CopyOfApexJarPairs() []string {
1581 pairs := make([]string, 0, l.Len())
1582
1583 for i, jar := range l.jars {
Paul Duffin1e8c6072020-10-23 18:28:55 +01001584 apex := l.apexes[i]
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001585 pairs = append(pairs, apex+":"+jar)
1586 }
1587
1588 return pairs
1589}
1590
Jingwen Chenc711fec2020-11-22 23:52:50 -05001591// BuildPaths returns a list of build paths based on the given directory prefix.
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001592func (l *ConfiguredJarList) BuildPaths(ctx PathContext, dir OutputPath) WritablePaths {
1593 paths := make(WritablePaths, l.Len())
1594 for i, jar := range l.jars {
1595 paths[i] = dir.Join(ctx, ModuleStem(jar)+".jar")
1596 }
1597 return paths
1598}
1599
Jingwen Chenc711fec2020-11-22 23:52:50 -05001600// UnmarshalJSON converts JSON configuration from raw bytes into a
1601// ConfiguredJarList structure.
Paul Duffin69d1fb12020-10-23 21:14:20 +01001602func (l *ConfiguredJarList) UnmarshalJSON(b []byte) error {
1603 // Try and unmarshal into a []string each item of which contains a pair
1604 // <apex>:<jar>.
1605 var list []string
1606 err := json.Unmarshal(b, &list)
1607 if err != nil {
1608 // Did not work so return
1609 return err
1610 }
1611
1612 apexes, jars, err := splitListOfPairsIntoPairOfLists(list)
1613 if err != nil {
1614 return err
1615 }
1616 l.apexes = apexes
1617 l.jars = jars
1618 return nil
1619}
1620
Jingwen Chenc711fec2020-11-22 23:52:50 -05001621// ModuleStem hardcodes the stem of framework-minus-apex to return "framework".
1622//
1623// TODO(b/139391334): hard coded until we find a good way to query the stem of a
1624// module before any other mutators are run.
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001625func ModuleStem(module string) string {
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001626 if module == "framework-minus-apex" {
1627 return "framework"
1628 }
1629 return module
1630}
1631
Jingwen Chenc711fec2020-11-22 23:52:50 -05001632// DevicePaths computes the on-device paths for the list of (apex, jar) pairs,
1633// based on the operating system.
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001634func (l *ConfiguredJarList) DevicePaths(cfg Config, ostype OsType) []string {
1635 paths := make([]string, l.Len())
1636 for i, jar := range l.jars {
1637 apex := l.apexes[i]
1638 name := ModuleStem(jar) + ".jar"
1639
1640 var subdir string
1641 if apex == "platform" {
1642 subdir = "system/framework"
1643 } else if apex == "system_ext" {
1644 subdir = "system_ext/framework"
1645 } else {
1646 subdir = filepath.Join("apex", apex, "javalib")
1647 }
1648
1649 if ostype.Class == Host {
1650 paths[i] = filepath.Join(cfg.Getenv("OUT_DIR"), "host", cfg.PrebuiltOS(), subdir, name)
1651 } else {
1652 paths[i] = filepath.Join("/", subdir, name)
1653 }
1654 }
1655 return paths
1656}
1657
Paul Duffin7d584e92020-10-23 18:26:03 +01001658func (l *ConfiguredJarList) String() string {
1659 var pairs []string
1660 for i := 0; i < l.Len(); i++ {
1661 pairs = append(pairs, l.apexes[i]+":"+l.jars[i])
1662 }
1663 return strings.Join(pairs, ",")
1664}
1665
Paul Duffin01416602020-10-23 21:04:03 +01001666func splitListOfPairsIntoPairOfLists(list []string) ([]string, []string, error) {
1667 // Now we need to populate this list by splitting each item in the slice of
1668 // pairs and appending them to the appropriate list of apexes or jars.
1669 apexes := make([]string, len(list))
1670 jars := make([]string, len(list))
1671
1672 for i, apexjar := range list {
1673 apex, jar, err := splitConfiguredJarPair(apexjar)
1674 if err != nil {
1675 return nil, nil, err
1676 }
1677 apexes[i] = apex
1678 jars[i] = jar
1679 }
1680
1681 return apexes, jars, nil
1682}
1683
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001684// Expected format for apexJarValue = <apex name>:<jar name>
Paul Duffin01416602020-10-23 21:04:03 +01001685func splitConfiguredJarPair(str string) (string, string, error) {
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001686 pair := strings.SplitN(str, ":", 2)
1687 if len(pair) == 2 {
Paul Duffin9c3ac962021-02-03 14:11:27 +00001688 apex := pair[0]
1689 jar := pair[1]
1690 if apex == "" {
1691 return apex, jar, fmt.Errorf("invalid apex '%s' in <apex>:<jar> pair '%s', expected format: <apex>:<jar>", apex, str)
1692 }
1693 return apex, jar, nil
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001694 } else {
Paul Duffin01416602020-10-23 21:04:03 +01001695 return "error-apex", "error-jar", fmt.Errorf("malformed (apex, jar) pair: '%s', expected format: <apex>:<jar>", str)
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001696 }
1697}
1698
Paul Duffin9c3ac962021-02-03 14:11:27 +00001699// CreateTestConfiguredJarList is a function to create ConfiguredJarList for tests.
Paul Duffine10dfa42020-10-23 21:23:44 +01001700func CreateTestConfiguredJarList(list []string) ConfiguredJarList {
Paul Duffin9c3ac962021-02-03 14:11:27 +00001701 // Create the ConfiguredJarList in as similar way as it is created at runtime by marshalling to
1702 // a json list of strings and then unmarshalling into a ConfiguredJarList instance.
1703 b, err := json.Marshal(list)
Paul Duffin01416602020-10-23 21:04:03 +01001704 if err != nil {
Paul Duffine10dfa42020-10-23 21:23:44 +01001705 panic(err)
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001706 }
1707
Paul Duffin9c3ac962021-02-03 14:11:27 +00001708 var jarList ConfiguredJarList
1709 err = json.Unmarshal(b, &jarList)
1710 if err != nil {
1711 panic(err)
1712 }
1713
1714 return jarList
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001715}
1716
Jingwen Chenc711fec2020-11-22 23:52:50 -05001717// EmptyConfiguredJarList returns an empty jar list.
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001718func EmptyConfiguredJarList() ConfiguredJarList {
1719 return ConfiguredJarList{}
1720}
1721
1722var earlyBootJarsKey = NewOnceKey("earlyBootJars")
1723
1724func (c *config) BootJars() []string {
1725 return c.Once(earlyBootJarsKey, func() interface{} {
Paul Duffin69d1fb12020-10-23 21:14:20 +01001726 list := c.productVariables.BootJars.CopyOfJars()
Jingwen Chenc711fec2020-11-22 23:52:50 -05001727 return append(list, c.productVariables.UpdatableBootJars.CopyOfJars()...)
Ulya Trafimovich249386a2020-07-01 14:31:13 +01001728 }).([]string)
1729}
Paul Duffin9a89a2a2020-10-28 19:20:06 +00001730
1731func (c *config) NonUpdatableBootJars() ConfiguredJarList {
1732 return c.productVariables.BootJars
1733}
1734
1735func (c *config) UpdatableBootJars() ConfiguredJarList {
1736 return c.productVariables.UpdatableBootJars
1737}