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