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