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