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