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