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 | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 15 | package common |
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" |
| 20 | "os" |
Colin Cross | 35cec12 | 2015-04-02 14:37:16 -0700 | [diff] [blame] | 21 | "path/filepath" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 22 | "runtime" |
Colin Cross | c1e86a3 | 2015-04-15 12:33:28 -0700 | [diff] [blame] | 23 | "sync" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 24 | ) |
| 25 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 26 | // The configuration file name |
Dan Willemsen | 87b17d1 | 2015-07-14 00:39:06 -0700 | [diff] [blame] | 27 | const configFileName = "soong.config" |
| 28 | const productVariablesFileName = "soong.variables" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 29 | |
| 30 | // A FileConfigurableOptions contains options which can be configured by the |
| 31 | // config file. These will be included in the config struct. |
| 32 | type FileConfigurableOptions struct { |
| 33 | } |
| 34 | |
Colin Cross | 2738597 | 2015-09-18 10:57:10 -0700 | [diff] [blame] | 35 | func (f *FileConfigurableOptions) SetDefaultConfig() { |
| 36 | *f = FileConfigurableOptions{} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Colin Cross | c3c0a49 | 2015-04-10 15:43:55 -0700 | [diff] [blame] | 39 | type Config struct { |
| 40 | *config |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame^] | 41 | |
| 42 | dontCreateNinjaFile bool |
Colin Cross | c3c0a49 | 2015-04-10 15:43:55 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame^] | 45 | // A config object represents the entire build configuration for Android. |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 46 | type config struct { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 47 | FileConfigurableOptions |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 48 | ProductVariables productVariables |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 49 | |
Dan Willemsen | 87b17d1 | 2015-07-14 00:39:06 -0700 | [diff] [blame] | 50 | ConfigFileName string |
| 51 | ProductVariablesFileName string |
| 52 | |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame^] | 53 | DeviceArches []Arch |
| 54 | HostArches map[HostType][]Arch |
| 55 | |
Dan Willemsen | 87b17d1 | 2015-07-14 00:39:06 -0700 | [diff] [blame] | 56 | srcDir string // the path of the root source directory |
| 57 | buildDir string // the path of the build output directory |
Colin Cross | c1e86a3 | 2015-04-15 12:33:28 -0700 | [diff] [blame] | 58 | |
Dan Willemsen | e7680ba | 2015-09-11 17:06:19 -0700 | [diff] [blame] | 59 | envLock sync.Mutex |
| 60 | envDeps map[string]string |
| 61 | envFrozen bool |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 64 | type jsonConfigurable interface { |
Colin Cross | 2738597 | 2015-09-18 10:57:10 -0700 | [diff] [blame] | 65 | SetDefaultConfig() |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 66 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 67 | |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 68 | func loadConfig(config *config) error { |
Dan Willemsen | 87b17d1 | 2015-07-14 00:39:06 -0700 | [diff] [blame] | 69 | err := loadFromConfigFile(&config.FileConfigurableOptions, config.ConfigFileName) |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | |
Dan Willemsen | 87b17d1 | 2015-07-14 00:39:06 -0700 | [diff] [blame] | 74 | return loadFromConfigFile(&config.ProductVariables, config.ProductVariablesFileName) |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // loads configuration options from a JSON file in the cwd. |
| 78 | func loadFromConfigFile(configurable jsonConfigurable, filename string) error { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 79 | // Try to open the file |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 80 | configFileReader, err := os.Open(filename) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 81 | defer configFileReader.Close() |
| 82 | if os.IsNotExist(err) { |
| 83 | // Need to create a file, so that blueprint & ninja don't get in |
| 84 | // a dependency tracking loop. |
| 85 | // Make a file-configurable-options with defaults, write it out using |
| 86 | // a json writer. |
Colin Cross | 2738597 | 2015-09-18 10:57:10 -0700 | [diff] [blame] | 87 | configurable.SetDefaultConfig() |
| 88 | err = saveToConfigFile(configurable, filename) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 89 | if err != nil { |
| 90 | return err |
| 91 | } |
| 92 | } else { |
| 93 | // Make a decoder for it |
| 94 | jsonDecoder := json.NewDecoder(configFileReader) |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 95 | err = jsonDecoder.Decode(configurable) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 96 | if err != nil { |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 97 | 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] | 98 | } |
| 99 | } |
| 100 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 101 | // No error |
| 102 | return nil |
| 103 | } |
| 104 | |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 105 | func saveToConfigFile(config jsonConfigurable, filename string) error { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 106 | data, err := json.MarshalIndent(&config, "", " ") |
| 107 | if err != nil { |
| 108 | return fmt.Errorf("cannot marshal config data: %s", err.Error()) |
| 109 | } |
| 110 | |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 111 | configFileWriter, err := os.Create(filename) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 112 | if err != nil { |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 113 | 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] | 114 | } |
| 115 | defer configFileWriter.Close() |
| 116 | |
| 117 | _, err = configFileWriter.Write(data) |
| 118 | if err != nil { |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 119 | return fmt.Errorf("default config file: %s could not be written: %s", filename, err.Error()) |
| 120 | } |
| 121 | |
| 122 | _, err = configFileWriter.WriteString("\n") |
| 123 | if err != nil { |
| 124 | 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] | 125 | } |
| 126 | |
| 127 | return nil |
| 128 | } |
| 129 | |
| 130 | // New creates a new Config object. The srcDir argument specifies the path to |
| 131 | // the root source directory. It also loads the config file, if found. |
Dan Willemsen | 87b17d1 | 2015-07-14 00:39:06 -0700 | [diff] [blame] | 132 | func NewConfig(srcDir, buildDir string) (Config, error) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 133 | // Make a config with default options |
Colin Cross | c3c0a49 | 2015-04-10 15:43:55 -0700 | [diff] [blame] | 134 | config := Config{ |
| 135 | config: &config{ |
Dan Willemsen | 87b17d1 | 2015-07-14 00:39:06 -0700 | [diff] [blame] | 136 | ConfigFileName: filepath.Join(buildDir, configFileName), |
| 137 | ProductVariablesFileName: filepath.Join(buildDir, productVariablesFileName), |
| 138 | |
| 139 | srcDir: srcDir, |
| 140 | buildDir: buildDir, |
| 141 | envDeps: make(map[string]string), |
Colin Cross | c3c0a49 | 2015-04-10 15:43:55 -0700 | [diff] [blame] | 142 | }, |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 143 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 144 | |
| 145 | // Load any configurable options from the configuration file |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 146 | err := loadConfig(config.config) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 147 | if err != nil { |
Colin Cross | c3c0a49 | 2015-04-10 15:43:55 -0700 | [diff] [blame] | 148 | return Config{}, err |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame^] | 151 | hostArches, deviceArches, err := decodeArchProductVariables(config.ProductVariables) |
| 152 | if err != nil { |
| 153 | return Config{}, err |
| 154 | } |
| 155 | |
| 156 | config.HostArches = hostArches |
| 157 | config.DeviceArches = deviceArches |
| 158 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 159 | return config, nil |
| 160 | } |
| 161 | |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 162 | func (c *config) SrcDir() string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 163 | return c.srcDir |
| 164 | } |
| 165 | |
Dan Willemsen | 87b17d1 | 2015-07-14 00:39:06 -0700 | [diff] [blame] | 166 | func (c *config) BuildDir() string { |
| 167 | return c.buildDir |
| 168 | } |
| 169 | |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 170 | func (c *config) IntermediatesDir() string { |
Dan Willemsen | 87b17d1 | 2015-07-14 00:39:06 -0700 | [diff] [blame] | 171 | return filepath.Join(c.BuildDir(), ".intermediates") |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame^] | 174 | func (c *config) RemoveAbandonedFiles() bool { |
| 175 | return false |
| 176 | } |
| 177 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 178 | // PrebuiltOS returns the name of the host OS used in prebuilts directories |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 179 | func (c *config) PrebuiltOS() string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 180 | switch runtime.GOOS { |
| 181 | case "linux": |
| 182 | return "linux-x86" |
| 183 | case "darwin": |
| 184 | return "darwin-x86" |
| 185 | default: |
| 186 | panic("Unknown GOOS") |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // GoRoot returns the path to the root directory of the Go toolchain. |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 191 | func (c *config) GoRoot() string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 192 | return fmt.Sprintf("%s/prebuilts/go/%s", c.srcDir, c.PrebuiltOS()) |
| 193 | } |
| 194 | |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 195 | func (c *config) CpPreserveSymlinksFlags() string { |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 196 | switch runtime.GOOS { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 197 | case "darwin": |
| 198 | return "-R" |
| 199 | case "linux": |
| 200 | return "-d" |
| 201 | default: |
| 202 | return "" |
| 203 | } |
| 204 | } |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 205 | |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 206 | func (c *config) Getenv(key string) string { |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 207 | var val string |
| 208 | var exists bool |
Colin Cross | c1e86a3 | 2015-04-15 12:33:28 -0700 | [diff] [blame] | 209 | c.envLock.Lock() |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 210 | if val, exists = c.envDeps[key]; !exists { |
Dan Willemsen | e7680ba | 2015-09-11 17:06:19 -0700 | [diff] [blame] | 211 | if c.envFrozen { |
| 212 | panic("Cannot access new environment variables after envdeps are frozen") |
| 213 | } |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 214 | val = os.Getenv(key) |
| 215 | c.envDeps[key] = val |
| 216 | } |
Colin Cross | c1e86a3 | 2015-04-15 12:33:28 -0700 | [diff] [blame] | 217 | c.envLock.Unlock() |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 218 | return val |
| 219 | } |
| 220 | |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 221 | func (c *config) EnvDeps() map[string]string { |
Dan Willemsen | e7680ba | 2015-09-11 17:06:19 -0700 | [diff] [blame] | 222 | c.envLock.Lock() |
| 223 | c.envFrozen = true |
| 224 | c.envLock.Unlock() |
Colin Cross | 68f5510 | 2015-03-25 14:43:57 -0700 | [diff] [blame] | 225 | return c.envDeps |
| 226 | } |
Colin Cross | 35cec12 | 2015-04-02 14:37:16 -0700 | [diff] [blame] | 227 | |
| 228 | // DeviceName returns the name of the current device target |
| 229 | // 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] | 230 | func (c *config) DeviceName() string { |
Dan Willemsen | 1d31ec3 | 2015-09-22 16:56:09 -0700 | [diff] [blame] | 231 | return *c.ProductVariables.DeviceName |
Colin Cross | 35cec12 | 2015-04-02 14:37:16 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Dan Willemsen | dd0e2c3 | 2015-10-20 14:29:35 -0700 | [diff] [blame] | 234 | func (c *config) DeviceUsesClang() bool { |
| 235 | if c.ProductVariables.DeviceUsesClang != nil { |
| 236 | return *c.ProductVariables.DeviceUsesClang |
| 237 | } |
| 238 | return false |
| 239 | } |
| 240 | |
Colin Cross | 35cec12 | 2015-04-02 14:37:16 -0700 | [diff] [blame] | 241 | // DeviceOut returns the path to out directory for device targets |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 242 | func (c *config) DeviceOut() string { |
Dan Willemsen | 87b17d1 | 2015-07-14 00:39:06 -0700 | [diff] [blame] | 243 | return filepath.Join(c.BuildDir(), "target/product", c.DeviceName()) |
Colin Cross | 35cec12 | 2015-04-02 14:37:16 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | // HostOut returns the path to out directory for host targets |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 247 | func (c *config) HostOut() string { |
Dan Willemsen | 87b17d1 | 2015-07-14 00:39:06 -0700 | [diff] [blame] | 248 | return filepath.Join(c.BuildDir(), "host", c.PrebuiltOS()) |
Colin Cross | 35cec12 | 2015-04-02 14:37:16 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | // HostBin returns the path to bin directory for host targets |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 252 | func (c *config) HostBin() string { |
Colin Cross | 35cec12 | 2015-04-02 14:37:16 -0700 | [diff] [blame] | 253 | return filepath.Join(c.HostOut(), "bin") |
| 254 | } |
| 255 | |
| 256 | // HostBinTool returns the path to a host tool in the bin directory for host targets |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 257 | func (c *config) HostBinTool(tool string) (string, error) { |
Colin Cross | 35cec12 | 2015-04-02 14:37:16 -0700 | [diff] [blame] | 258 | return filepath.Join(c.HostBin(), tool), nil |
| 259 | } |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 260 | |
| 261 | // HostJavaDir returns the path to framework directory for host targets |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 262 | func (c *config) HostJavaDir() string { |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 263 | return filepath.Join(c.HostOut(), "framework") |
| 264 | } |
| 265 | |
| 266 | // HostJavaTool returns the path to a host tool in the frameworks directory for host targets |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 267 | func (c *config) HostJavaTool(tool string) (string, error) { |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 268 | return filepath.Join(c.HostJavaDir(), tool), nil |
| 269 | } |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 270 | |
| 271 | func (c *config) ResourceOverlays() []string { |
| 272 | return nil |
| 273 | } |
| 274 | |
| 275 | func (c *config) PlatformVersion() string { |
| 276 | return "M" |
| 277 | } |
| 278 | |
| 279 | func (c *config) PlatformSdkVersion() string { |
| 280 | return "22" |
| 281 | } |
| 282 | |
| 283 | func (c *config) BuildNumber() string { |
| 284 | return "000000" |
| 285 | } |
| 286 | |
| 287 | func (c *config) ProductAaptConfig() []string { |
| 288 | return []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"} |
| 289 | } |
| 290 | |
| 291 | func (c *config) ProductAaptPreferredConfig() string { |
| 292 | return "xhdpi" |
| 293 | } |
| 294 | |
| 295 | func (c *config) ProductAaptCharacteristics() string { |
| 296 | return "nosdcard" |
| 297 | } |
| 298 | |
| 299 | func (c *config) DefaultAppCertificateDir() string { |
| 300 | return filepath.Join(c.SrcDir(), "build/target/product/security") |
| 301 | } |
| 302 | |
| 303 | func (c *config) DefaultAppCertificate() string { |
| 304 | return filepath.Join(c.DefaultAppCertificateDir(), "testkey") |
| 305 | } |