Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1 | // 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 Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 16 | |
| 17 | import ( |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 18 | "encoding/json" |
| 19 | "fmt" |
Colin Cross | d8f2014 | 2016-11-03 09:43:26 -0700 | [diff] [blame] | 20 | "io/ioutil" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 21 | "os" |
Colin Cross | 35cec12 | 2015-04-02 14:37:16 -0700 | [diff] [blame] | 22 | "path/filepath" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 23 | "runtime" |
Dan Willemsen | 5951c8a | 2016-07-19 19:08:14 -0700 | [diff] [blame] | 24 | "strconv" |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 25 | "strings" |
Colin Cross | c1e86a3 | 2015-04-15 12:33:28 -0700 | [diff] [blame] | 26 | "sync" |
Colin Cross | 6ff5138 | 2015-12-17 16:39:19 -0800 | [diff] [blame] | 27 | |
| 28 | "github.com/google/blueprint/proptools" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 29 | ) |
| 30 | |
Colin Cross | 6ff5138 | 2015-12-17 16:39:19 -0800 | [diff] [blame] | 31 | var Bool = proptools.Bool |
Jack He | 8cc7143 | 2016-12-08 15:45:07 -0800 | [diff] [blame] | 32 | var String = proptools.String |
Colin Cross | 6ff5138 | 2015-12-17 16:39:19 -0800 | [diff] [blame] | 33 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 34 | // The configuration file name |
Dan Willemsen | 87b17d1 | 2015-07-14 00:39:06 -0700 | [diff] [blame] | 35 | const configFileName = "soong.config" |
| 36 | const productVariablesFileName = "soong.variables" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 37 | |
| 38 | // A FileConfigurableOptions contains options which can be configured by the |
| 39 | // config file. These will be included in the config struct. |
| 40 | type FileConfigurableOptions struct { |
Dan Willemsen | 322acaf | 2016-01-12 23:07:05 -0800 | [diff] [blame] | 41 | Mega_device *bool `json:",omitempty"` |
Dan Albert | 4098deb | 2016-10-19 14:04:41 -0700 | [diff] [blame] | 42 | Ndk_abis *bool `json:",omitempty"` |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 43 | Host_bionic *bool `json:",omitempty"` |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Colin Cross | 2738597 | 2015-09-18 10:57:10 -0700 | [diff] [blame] | 46 | func (f *FileConfigurableOptions) SetDefaultConfig() { |
| 47 | *f = FileConfigurableOptions{} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 50 | // A Config object represents the entire build configuration for Android. |
Colin Cross | c3c0a49 | 2015-04-10 15:43:55 -0700 | [diff] [blame] | 51 | type Config struct { |
| 52 | *config |
| 53 | } |
| 54 | |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 55 | func (c Config) BuildDir() string { |
| 56 | return c.buildDir |
| 57 | } |
| 58 | |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 59 | // A DeviceConfig object represents the configuration for a particular device being built. For |
| 60 | // now there will only be one of these, but in the future there may be multiple devices being |
| 61 | // built |
| 62 | type DeviceConfig struct { |
| 63 | *deviceConfig |
| 64 | } |
| 65 | |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 66 | type config struct { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 67 | FileConfigurableOptions |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 68 | ProductVariables productVariables |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 69 | |
Dan Willemsen | 87b17d1 | 2015-07-14 00:39:06 -0700 | [diff] [blame] | 70 | ConfigFileName string |
| 71 | ProductVariablesFileName string |
| 72 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 73 | Targets map[OsClass][]Target |
| 74 | BuildOsVariant string |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 75 | |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 76 | deviceConfig *deviceConfig |
| 77 | |
Dan Willemsen | 87b17d1 | 2015-07-14 00:39:06 -0700 | [diff] [blame] | 78 | srcDir string // the path of the root source directory |
| 79 | buildDir string // the path of the build output directory |
Colin Cross | c1e86a3 | 2015-04-15 12:33:28 -0700 | [diff] [blame] | 80 | |
Dan Willemsen | e7680ba | 2015-09-11 17:06:19 -0700 | [diff] [blame] | 81 | envLock sync.Mutex |
| 82 | envDeps map[string]string |
| 83 | envFrozen bool |
Dan Willemsen | 5ba07e8 | 2015-12-11 13:51:06 -0800 | [diff] [blame] | 84 | |
| 85 | inMake bool |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 86 | |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 87 | OncePer |
| 88 | } |
| 89 | |
| 90 | type deviceConfig struct { |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame^] | 91 | config *config |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 92 | OncePer |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 95 | type jsonConfigurable interface { |
Colin Cross | 2738597 | 2015-09-18 10:57:10 -0700 | [diff] [blame] | 96 | SetDefaultConfig() |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 97 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 98 | |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 99 | func loadConfig(config *config) error { |
Dan Willemsen | 87b17d1 | 2015-07-14 00:39:06 -0700 | [diff] [blame] | 100 | err := loadFromConfigFile(&config.FileConfigurableOptions, config.ConfigFileName) |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 101 | if err != nil { |
| 102 | return err |
| 103 | } |
| 104 | |
Dan Willemsen | 87b17d1 | 2015-07-14 00:39:06 -0700 | [diff] [blame] | 105 | return loadFromConfigFile(&config.ProductVariables, config.ProductVariablesFileName) |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // loads configuration options from a JSON file in the cwd. |
| 109 | func loadFromConfigFile(configurable jsonConfigurable, filename string) error { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 110 | // Try to open the file |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 111 | configFileReader, err := os.Open(filename) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 112 | defer configFileReader.Close() |
| 113 | if os.IsNotExist(err) { |
| 114 | // Need to create a file, so that blueprint & ninja don't get in |
| 115 | // a dependency tracking loop. |
| 116 | // Make a file-configurable-options with defaults, write it out using |
| 117 | // a json writer. |
Colin Cross | 2738597 | 2015-09-18 10:57:10 -0700 | [diff] [blame] | 118 | configurable.SetDefaultConfig() |
| 119 | err = saveToConfigFile(configurable, filename) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 120 | if err != nil { |
| 121 | return err |
| 122 | } |
| 123 | } else { |
| 124 | // Make a decoder for it |
| 125 | jsonDecoder := json.NewDecoder(configFileReader) |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 126 | err = jsonDecoder.Decode(configurable) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 127 | if err != nil { |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 128 | return fmt.Errorf("config file: %s did not parse correctly: "+err.Error(), filename) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 132 | // No error |
| 133 | return nil |
| 134 | } |
| 135 | |
Colin Cross | d8f2014 | 2016-11-03 09:43:26 -0700 | [diff] [blame] | 136 | // atomically writes the config file in case two copies of soong_build are running simultaneously |
| 137 | // (for example, docs generation and ninja manifest generation) |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 138 | func saveToConfigFile(config jsonConfigurable, filename string) error { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 139 | data, err := json.MarshalIndent(&config, "", " ") |
| 140 | if err != nil { |
| 141 | return fmt.Errorf("cannot marshal config data: %s", err.Error()) |
| 142 | } |
| 143 | |
Colin Cross | d8f2014 | 2016-11-03 09:43:26 -0700 | [diff] [blame] | 144 | f, err := ioutil.TempFile(filepath.Dir(filename), "config") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 145 | if err != nil { |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 146 | return fmt.Errorf("cannot create empty config file %s: %s\n", filename, err.Error()) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 147 | } |
Colin Cross | d8f2014 | 2016-11-03 09:43:26 -0700 | [diff] [blame] | 148 | defer os.Remove(f.Name()) |
| 149 | defer f.Close() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 150 | |
Colin Cross | d8f2014 | 2016-11-03 09:43:26 -0700 | [diff] [blame] | 151 | _, err = f.Write(data) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 152 | if err != nil { |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 153 | return fmt.Errorf("default config file: %s could not be written: %s", filename, err.Error()) |
| 154 | } |
| 155 | |
Colin Cross | d8f2014 | 2016-11-03 09:43:26 -0700 | [diff] [blame] | 156 | _, err = f.WriteString("\n") |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 157 | if err != nil { |
| 158 | return fmt.Errorf("default config file: %s could not be written: %s", filename, err.Error()) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Colin Cross | d8f2014 | 2016-11-03 09:43:26 -0700 | [diff] [blame] | 161 | f.Close() |
| 162 | os.Rename(f.Name(), filename) |
| 163 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 164 | return nil |
| 165 | } |
| 166 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 167 | // TestConfig returns a Config object suitable for using for tests |
Colin Cross | 0d614dd | 2016-10-14 15:38:43 -0700 | [diff] [blame] | 168 | func TestConfig(buildDir string) Config { |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame^] | 169 | config := &config{ |
| 170 | ProductVariables: productVariables{ |
| 171 | DeviceName: stringPtr("test_device"), |
| 172 | }, |
| 173 | |
Colin Cross | 0d614dd | 2016-10-14 15:38:43 -0700 | [diff] [blame] | 174 | buildDir: buildDir, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame^] | 175 | } |
| 176 | config.deviceConfig = &deviceConfig{ |
| 177 | config: config, |
| 178 | } |
| 179 | |
| 180 | return Config{config} |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 183 | // New creates a new Config object. The srcDir argument specifies the path to |
| 184 | // the root source directory. It also loads the config file, if found. |
Dan Willemsen | 87b17d1 | 2015-07-14 00:39:06 -0700 | [diff] [blame] | 185 | func NewConfig(srcDir, buildDir string) (Config, error) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 186 | // Make a config with default options |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 187 | config := &config{ |
| 188 | ConfigFileName: filepath.Join(buildDir, configFileName), |
| 189 | ProductVariablesFileName: filepath.Join(buildDir, productVariablesFileName), |
Dan Willemsen | 87b17d1 | 2015-07-14 00:39:06 -0700 | [diff] [blame] | 190 | |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 191 | srcDir: srcDir, |
| 192 | buildDir: buildDir, |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 193 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 194 | |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame^] | 195 | config.deviceConfig = &deviceConfig{ |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 196 | config: config, |
| 197 | } |
| 198 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 199 | // Sanity check the build and source directories. This won't catch strange |
| 200 | // configurations with symlinks, but at least checks the obvious cases. |
| 201 | absBuildDir, err := filepath.Abs(buildDir) |
| 202 | if err != nil { |
| 203 | return Config{}, err |
| 204 | } |
| 205 | |
| 206 | absSrcDir, err := filepath.Abs(srcDir) |
| 207 | if err != nil { |
| 208 | return Config{}, err |
| 209 | } |
| 210 | |
| 211 | if strings.HasPrefix(absSrcDir, absBuildDir) { |
| 212 | return Config{}, fmt.Errorf("Build dir must not contain source directory") |
| 213 | } |
| 214 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 215 | // Load any configurable options from the configuration file |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 216 | err = loadConfig(config) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 217 | if err != nil { |
Colin Cross | c3c0a49 | 2015-04-10 15:43:55 -0700 | [diff] [blame] | 218 | return Config{}, err |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Dan Willemsen | 5ba07e8 | 2015-12-11 13:51:06 -0800 | [diff] [blame] | 221 | inMakeFile := filepath.Join(buildDir, ".soong.in_make") |
| 222 | if _, err := os.Stat(inMakeFile); err == nil { |
| 223 | config.inMake = true |
| 224 | } |
| 225 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 226 | targets, err := decodeTargetProductVariables(config) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 227 | if err != nil { |
| 228 | return Config{}, err |
| 229 | } |
| 230 | |
Dan Albert | 4098deb | 2016-10-19 14:04:41 -0700 | [diff] [blame] | 231 | var archConfig []archConfig |
Dan Willemsen | 322acaf | 2016-01-12 23:07:05 -0800 | [diff] [blame] | 232 | if Bool(config.Mega_device) { |
Dan Albert | 4098deb | 2016-10-19 14:04:41 -0700 | [diff] [blame] | 233 | archConfig = getMegaDeviceConfig() |
| 234 | } else if Bool(config.Ndk_abis) { |
| 235 | archConfig = getNdkAbisConfig() |
| 236 | } |
| 237 | |
| 238 | if archConfig != nil { |
| 239 | deviceTargets, err := decodeArchSettings(archConfig) |
Dan Willemsen | 322acaf | 2016-01-12 23:07:05 -0800 | [diff] [blame] | 240 | if err != nil { |
| 241 | return Config{}, err |
| 242 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 243 | targets[Device] = deviceTargets |
Dan Willemsen | 322acaf | 2016-01-12 23:07:05 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 246 | config.Targets = targets |
| 247 | config.BuildOsVariant = targets[Host][0].String() |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 248 | |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 249 | return Config{config}, nil |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 252 | func (c *config) RemoveAbandonedFiles() bool { |
| 253 | return false |
| 254 | } |
| 255 | |
Dan Willemsen | c2aa4a9 | 2016-05-26 15:13:03 -0700 | [diff] [blame] | 256 | func (c *config) BlueprintToolLocation() string { |
| 257 | return filepath.Join(c.buildDir, "host", c.PrebuiltOS(), "bin") |
| 258 | } |
| 259 | |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 260 | // HostSystemTool looks for non-hermetic tools from the system we're running on. |
| 261 | // Generally shouldn't be used, but useful to find the XCode SDK, etc. |
| 262 | func (c *config) HostSystemTool(name string) string { |
| 263 | for _, dir := range filepath.SplitList(c.Getenv("PATH")) { |
| 264 | path := filepath.Join(dir, name) |
| 265 | if s, err := os.Stat(path); err != nil { |
| 266 | continue |
| 267 | } else if m := s.Mode(); !s.IsDir() && m&0111 != 0 { |
| 268 | return path |
| 269 | } |
| 270 | } |
| 271 | return name |
| 272 | } |
| 273 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 274 | // PrebuiltOS returns the name of the host OS used in prebuilts directories |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 275 | func (c *config) PrebuiltOS() string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 276 | switch runtime.GOOS { |
| 277 | case "linux": |
| 278 | return "linux-x86" |
| 279 | case "darwin": |
| 280 | return "darwin-x86" |
| 281 | default: |
| 282 | panic("Unknown GOOS") |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // GoRoot returns the path to the root directory of the Go toolchain. |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 287 | func (c *config) GoRoot() string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 288 | return fmt.Sprintf("%s/prebuilts/go/%s", c.srcDir, c.PrebuiltOS()) |
| 289 | } |
| 290 | |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 291 | func (c *config) CpPreserveSymlinksFlags() string { |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 292 | switch runtime.GOOS { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 293 | case "darwin": |
| 294 | return "-R" |
| 295 | case "linux": |
| 296 | return "-d" |
| 297 | default: |
| 298 | return "" |
| 299 | } |
| 300 | } |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 301 | |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 302 | func (c *config) Getenv(key string) string { |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 303 | var val string |
| 304 | var exists bool |
Colin Cross | c1e86a3 | 2015-04-15 12:33:28 -0700 | [diff] [blame] | 305 | c.envLock.Lock() |
Colin Cross | c0d58b4 | 2017-02-06 15:40:41 -0800 | [diff] [blame] | 306 | defer c.envLock.Unlock() |
| 307 | if c.envDeps == nil { |
| 308 | c.envDeps = make(map[string]string) |
| 309 | } |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 310 | if val, exists = c.envDeps[key]; !exists { |
Dan Willemsen | e7680ba | 2015-09-11 17:06:19 -0700 | [diff] [blame] | 311 | if c.envFrozen { |
| 312 | panic("Cannot access new environment variables after envdeps are frozen") |
| 313 | } |
Dan Willemsen | 6606872 | 2017-05-08 21:15:59 +0000 | [diff] [blame] | 314 | val, _ = originalEnv[key] |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 315 | c.envDeps[key] = val |
| 316 | } |
| 317 | return val |
| 318 | } |
| 319 | |
Colin Cross | 99d7c23 | 2016-11-23 16:52:04 -0800 | [diff] [blame] | 320 | func (c *config) GetenvWithDefault(key string, defaultValue string) string { |
| 321 | ret := c.Getenv(key) |
| 322 | if ret == "" { |
| 323 | return defaultValue |
| 324 | } |
| 325 | return ret |
| 326 | } |
| 327 | |
| 328 | func (c *config) IsEnvTrue(key string) bool { |
| 329 | value := c.Getenv(key) |
| 330 | return value == "1" || value == "y" || value == "yes" || value == "on" || value == "true" |
| 331 | } |
| 332 | |
| 333 | func (c *config) IsEnvFalse(key string) bool { |
| 334 | value := c.Getenv(key) |
| 335 | return value == "0" || value == "n" || value == "no" || value == "off" || value == "false" |
| 336 | } |
| 337 | |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 338 | func (c *config) EnvDeps() map[string]string { |
Dan Willemsen | e7680ba | 2015-09-11 17:06:19 -0700 | [diff] [blame] | 339 | c.envLock.Lock() |
Colin Cross | c0d58b4 | 2017-02-06 15:40:41 -0800 | [diff] [blame] | 340 | defer c.envLock.Unlock() |
Dan Willemsen | e7680ba | 2015-09-11 17:06:19 -0700 | [diff] [blame] | 341 | c.envFrozen = true |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 342 | return c.envDeps |
| 343 | } |
Colin Cross | 35cec12 | 2015-04-02 14:37:16 -0700 | [diff] [blame] | 344 | |
Dan Willemsen | 5ba07e8 | 2015-12-11 13:51:06 -0800 | [diff] [blame] | 345 | func (c *config) EmbeddedInMake() bool { |
| 346 | return c.inMake |
| 347 | } |
| 348 | |
Colin Cross | 35cec12 | 2015-04-02 14:37:16 -0700 | [diff] [blame] | 349 | // DeviceName returns the name of the current device target |
| 350 | // TODO: take an AndroidModuleContext to select the device name for multi-device builds |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 351 | func (c *config) DeviceName() string { |
Dan Willemsen | 1d31ec3 | 2015-09-22 16:56:09 -0700 | [diff] [blame] | 352 | return *c.ProductVariables.DeviceName |
Colin Cross | 35cec12 | 2015-04-02 14:37:16 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Dan Willemsen | dd0e2c3 | 2015-10-20 14:29:35 -0700 | [diff] [blame] | 355 | func (c *config) DeviceUsesClang() bool { |
| 356 | if c.ProductVariables.DeviceUsesClang != nil { |
| 357 | return *c.ProductVariables.DeviceUsesClang |
| 358 | } |
Dan Willemsen | 0084d78 | 2016-03-01 14:54:24 -0800 | [diff] [blame] | 359 | return true |
Dan Willemsen | dd0e2c3 | 2015-10-20 14:29:35 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 362 | func (c *config) ResourceOverlays() []SourcePath { |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 363 | return nil |
| 364 | } |
| 365 | |
| 366 | func (c *config) PlatformVersion() string { |
| 367 | return "M" |
| 368 | } |
| 369 | |
Dan Albert | 073379e | 2016-11-10 10:46:36 -0800 | [diff] [blame] | 370 | func (c *config) PlatformSdkVersionInt() int { |
| 371 | return *c.ProductVariables.Platform_sdk_version |
| 372 | } |
| 373 | |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 374 | func (c *config) PlatformSdkVersion() string { |
Dan Albert | 073379e | 2016-11-10 10:46:36 -0800 | [diff] [blame] | 375 | return strconv.Itoa(c.PlatformSdkVersionInt()) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 378 | func (c *config) PlatformVersionAllCodenames() []string { |
| 379 | return c.ProductVariables.Platform_version_all_codenames |
| 380 | } |
| 381 | |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 382 | func (c *config) BuildNumber() string { |
| 383 | return "000000" |
| 384 | } |
| 385 | |
| 386 | func (c *config) ProductAaptConfig() []string { |
| 387 | return []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"} |
| 388 | } |
| 389 | |
| 390 | func (c *config) ProductAaptPreferredConfig() string { |
| 391 | return "xhdpi" |
| 392 | } |
| 393 | |
| 394 | func (c *config) ProductAaptCharacteristics() string { |
| 395 | return "nosdcard" |
| 396 | } |
| 397 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 398 | func (c *config) DefaultAppCertificateDir(ctx PathContext) SourcePath { |
| 399 | return PathForSource(ctx, "build/target/product/security") |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 402 | func (c *config) DefaultAppCertificate(ctx PathContext) SourcePath { |
| 403 | return c.DefaultAppCertificateDir(ctx).Join(ctx, "testkey") |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 404 | } |
Colin Cross | 6ff5138 | 2015-12-17 16:39:19 -0800 | [diff] [blame] | 405 | |
| 406 | func (c *config) AllowMissingDependencies() bool { |
Dan Willemsen | b503816 | 2016-03-16 12:35:33 -0700 | [diff] [blame] | 407 | return Bool(c.ProductVariables.Allow_missing_dependencies) |
Colin Cross | 6ff5138 | 2015-12-17 16:39:19 -0800 | [diff] [blame] | 408 | } |
Dan Willemsen | 322acaf | 2016-01-12 23:07:05 -0800 | [diff] [blame] | 409 | |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 410 | func (c *config) DevicePrefer32BitExecutables() bool { |
| 411 | return Bool(c.ProductVariables.DevicePrefer32BitExecutables) |
| 412 | } |
| 413 | |
Dan Willemsen | 7f730fd | 2016-01-14 11:22:23 -0800 | [diff] [blame] | 414 | func (c *config) SkipDeviceInstall() bool { |
Colin Cross | 893d816 | 2017-04-26 17:34:03 -0700 | [diff] [blame] | 415 | return c.EmbeddedInMake() |
| 416 | } |
| 417 | |
| 418 | func (c *config) SkipMegaDeviceInstall(path string) bool { |
| 419 | return Bool(c.Mega_device) && |
| 420 | strings.HasPrefix(path, filepath.Join(c.buildDir, "target", "product")) |
Dan Willemsen | 322acaf | 2016-01-12 23:07:05 -0800 | [diff] [blame] | 421 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 422 | |
| 423 | func (c *config) SanitizeHost() []string { |
Colin Cross | 23ae82a | 2016-11-02 14:34:39 -0700 | [diff] [blame] | 424 | return append([]string(nil), c.ProductVariables.SanitizeHost...) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | func (c *config) SanitizeDevice() []string { |
Colin Cross | 23ae82a | 2016-11-02 14:34:39 -0700 | [diff] [blame] | 428 | return append([]string(nil), c.ProductVariables.SanitizeDevice...) |
| 429 | } |
| 430 | |
| 431 | func (c *config) SanitizeDeviceArch() []string { |
| 432 | return append([]string(nil), c.ProductVariables.SanitizeDeviceArch...) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 433 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 434 | |
Vishwath Mohan | 1b017a7 | 2017-01-19 13:54:55 -0800 | [diff] [blame] | 435 | func (c *config) EnableCFI() bool { |
Vishwath Mohan | c32c3eb | 2017-01-24 14:20:54 -0800 | [diff] [blame] | 436 | if c.ProductVariables.EnableCFI == nil { |
| 437 | return true |
| 438 | } else { |
| 439 | return *c.ProductVariables.EnableCFI |
| 440 | } |
Vishwath Mohan | 1b017a7 | 2017-01-19 13:54:55 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 443 | func (c *config) Android64() bool { |
| 444 | for _, t := range c.Targets[Device] { |
| 445 | if t.Arch.ArchType.Multilib == "lib64" { |
| 446 | return true |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | return false |
| 451 | } |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 452 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 453 | func (c *config) UseGoma() bool { |
| 454 | return Bool(c.ProductVariables.UseGoma) |
| 455 | } |
| 456 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 457 | func (c *config) ClangTidy() bool { |
| 458 | return Bool(c.ProductVariables.ClangTidy) |
| 459 | } |
| 460 | |
| 461 | func (c *config) TidyChecks() string { |
| 462 | if c.ProductVariables.TidyChecks == nil { |
| 463 | return "" |
| 464 | } |
| 465 | return *c.ProductVariables.TidyChecks |
| 466 | } |
| 467 | |
Colin Cross | 0f4e0d6 | 2016-07-27 10:56:55 -0700 | [diff] [blame] | 468 | func (c *config) LibartImgHostBaseAddress() string { |
| 469 | return "0x60000000" |
| 470 | } |
| 471 | |
| 472 | func (c *config) LibartImgDeviceBaseAddress() string { |
Colin Cross | 20e1365 | 2017-06-22 15:34:51 -0700 | [diff] [blame] | 473 | archType := Common |
| 474 | if len(c.Targets[Device]) > 0 { |
| 475 | archType = c.Targets[Device][0].Arch.ArchType |
| 476 | } |
| 477 | switch archType { |
Colin Cross | 0f4e0d6 | 2016-07-27 10:56:55 -0700 | [diff] [blame] | 478 | default: |
| 479 | return "0x70000000" |
| 480 | case Mips, Mips64: |
Chris Larsen | ae7f3e2 | 2017-05-30 16:36:58 -0700 | [diff] [blame] | 481 | return "0x5C000000" |
Colin Cross | 0f4e0d6 | 2016-07-27 10:56:55 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
Hiroshi Yamauchi | e2a1063 | 2016-12-19 13:44:41 -0800 | [diff] [blame] | 485 | func (c *config) ArtUseReadBarrier() bool { |
| 486 | return Bool(c.ProductVariables.ArtUseReadBarrier) |
| 487 | } |
| 488 | |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 489 | func (c *deviceConfig) Arches() []Arch { |
| 490 | var arches []Arch |
| 491 | for _, target := range c.config.Targets[Device] { |
| 492 | arches = append(arches, target.Arch) |
| 493 | } |
| 494 | return arches |
| 495 | } |
Dan Willemsen | d2ede87 | 2016-11-18 14:54:24 -0800 | [diff] [blame] | 496 | |
Dan Willemsen | 4353bc4 | 2016-12-05 17:16:02 -0800 | [diff] [blame] | 497 | func (c *deviceConfig) VendorPath() string { |
| 498 | if c.config.ProductVariables.VendorPath != nil { |
| 499 | return *c.config.ProductVariables.VendorPath |
| 500 | } |
| 501 | return "vendor" |
| 502 | } |
| 503 | |
Dan Willemsen | 11b2614 | 2017-03-19 18:30:37 -0700 | [diff] [blame] | 504 | func (c *deviceConfig) CompileVndk() bool { |
Dan Willemsen | d2ede87 | 2016-11-18 14:54:24 -0800 | [diff] [blame] | 505 | if c.config.ProductVariables.DeviceVndkVersion == nil { |
Dan Willemsen | 11b2614 | 2017-03-19 18:30:37 -0700 | [diff] [blame] | 506 | return false |
Dan Willemsen | d2ede87 | 2016-11-18 14:54:24 -0800 | [diff] [blame] | 507 | } |
Dan Willemsen | 11b2614 | 2017-03-19 18:30:37 -0700 | [diff] [blame] | 508 | return *c.config.ProductVariables.DeviceVndkVersion == "current" |
Dan Willemsen | d2ede87 | 2016-11-18 14:54:24 -0800 | [diff] [blame] | 509 | } |
Jack He | 8cc7143 | 2016-12-08 15:45:07 -0800 | [diff] [blame] | 510 | |
| 511 | func (c *deviceConfig) BtConfigIncludeDir() string { |
| 512 | return String(c.config.ProductVariables.BtConfigIncludeDir) |
| 513 | } |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 514 | |
| 515 | func (c *deviceConfig) NativeCoverageEnabled() bool { |
| 516 | return Bool(c.config.ProductVariables.NativeCoverage) |
| 517 | } |
| 518 | |
| 519 | func (c *deviceConfig) CoverageEnabledForPath(path string) bool { |
Ryan Campbell | 469a18a | 2017-02-27 09:01:54 -0800 | [diff] [blame] | 520 | coverage := false |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 521 | if c.config.ProductVariables.CoveragePaths != nil { |
| 522 | for _, prefix := range *c.config.ProductVariables.CoveragePaths { |
| 523 | if strings.HasPrefix(path, prefix) { |
Ryan Campbell | 469a18a | 2017-02-27 09:01:54 -0800 | [diff] [blame] | 524 | coverage = true |
| 525 | break |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | } |
Ryan Campbell | 469a18a | 2017-02-27 09:01:54 -0800 | [diff] [blame] | 529 | if coverage && c.config.ProductVariables.CoverageExcludePaths != nil { |
| 530 | for _, prefix := range *c.config.ProductVariables.CoverageExcludePaths { |
| 531 | if strings.HasPrefix(path, prefix) { |
| 532 | coverage = false |
| 533 | break |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | return coverage |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 538 | } |