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