blob: ae047569645076f4511288549f6cf0f191b0fd8e [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// 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 Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Colin Cross3f40fa42015-01-30 17:27:36 -080018 "encoding/json"
19 "fmt"
20 "os"
Colin Cross35cec122015-04-02 14:37:16 -070021 "path/filepath"
Colin Cross3f40fa42015-01-30 17:27:36 -080022 "runtime"
Dan Willemsen5951c8a2016-07-19 19:08:14 -070023 "strconv"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070024 "strings"
Colin Crossc1e86a32015-04-15 12:33:28 -070025 "sync"
Colin Cross6ff51382015-12-17 16:39:19 -080026
27 "github.com/google/blueprint/proptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080028)
29
Colin Cross6ff51382015-12-17 16:39:19 -080030var Bool = proptools.Bool
31
Colin Cross3f40fa42015-01-30 17:27:36 -080032// The configuration file name
Dan Willemsen87b17d12015-07-14 00:39:06 -070033const configFileName = "soong.config"
34const productVariablesFileName = "soong.variables"
Colin Cross3f40fa42015-01-30 17:27:36 -080035
36// A FileConfigurableOptions contains options which can be configured by the
37// config file. These will be included in the config struct.
38type FileConfigurableOptions struct {
Dan Willemsen322acaf2016-01-12 23:07:05 -080039 Mega_device *bool `json:",omitempty"`
Colin Cross3f40fa42015-01-30 17:27:36 -080040}
41
Colin Cross27385972015-09-18 10:57:10 -070042func (f *FileConfigurableOptions) SetDefaultConfig() {
43 *f = FileConfigurableOptions{}
Colin Cross3f40fa42015-01-30 17:27:36 -080044}
45
Colin Crossc3c0a492015-04-10 15:43:55 -070046type Config struct {
47 *config
48}
49
Dan Willemsen218f6562015-07-08 18:13:11 -070050// A config object represents the entire build configuration for Android.
Colin Cross1332b002015-04-07 17:11:30 -070051type config struct {
Colin Cross3f40fa42015-01-30 17:27:36 -080052 FileConfigurableOptions
Colin Cross485e5722015-08-27 13:28:01 -070053 ProductVariables productVariables
Colin Cross3f40fa42015-01-30 17:27:36 -080054
Dan Willemsen87b17d12015-07-14 00:39:06 -070055 ConfigFileName string
56 ProductVariablesFileName string
57
Colin Crossa1ad8d12016-06-01 17:09:44 -070058 Targets map[OsClass][]Target
59 BuildOsVariant string
Dan Willemsen218f6562015-07-08 18:13:11 -070060
Dan Willemsen87b17d12015-07-14 00:39:06 -070061 srcDir string // the path of the root source directory
62 buildDir string // the path of the build output directory
Colin Crossc1e86a32015-04-15 12:33:28 -070063
Dan Willemsene7680ba2015-09-11 17:06:19 -070064 envLock sync.Mutex
65 envDeps map[string]string
66 envFrozen bool
Dan Willemsen5ba07e82015-12-11 13:51:06 -080067
68 inMake bool
Colin Cross3f40fa42015-01-30 17:27:36 -080069}
70
Colin Cross485e5722015-08-27 13:28:01 -070071type jsonConfigurable interface {
Colin Cross27385972015-09-18 10:57:10 -070072 SetDefaultConfig()
Colin Cross485e5722015-08-27 13:28:01 -070073}
Colin Cross3f40fa42015-01-30 17:27:36 -080074
Colin Cross485e5722015-08-27 13:28:01 -070075func loadConfig(config *config) error {
Dan Willemsen87b17d12015-07-14 00:39:06 -070076 err := loadFromConfigFile(&config.FileConfigurableOptions, config.ConfigFileName)
Colin Cross485e5722015-08-27 13:28:01 -070077 if err != nil {
78 return err
79 }
80
Dan Willemsen87b17d12015-07-14 00:39:06 -070081 return loadFromConfigFile(&config.ProductVariables, config.ProductVariablesFileName)
Colin Cross485e5722015-08-27 13:28:01 -070082}
83
84// loads configuration options from a JSON file in the cwd.
85func loadFromConfigFile(configurable jsonConfigurable, filename string) error {
Colin Cross3f40fa42015-01-30 17:27:36 -080086 // Try to open the file
Colin Cross485e5722015-08-27 13:28:01 -070087 configFileReader, err := os.Open(filename)
Colin Cross3f40fa42015-01-30 17:27:36 -080088 defer configFileReader.Close()
89 if os.IsNotExist(err) {
90 // Need to create a file, so that blueprint & ninja don't get in
91 // a dependency tracking loop.
92 // Make a file-configurable-options with defaults, write it out using
93 // a json writer.
Colin Cross27385972015-09-18 10:57:10 -070094 configurable.SetDefaultConfig()
95 err = saveToConfigFile(configurable, filename)
Colin Cross3f40fa42015-01-30 17:27:36 -080096 if err != nil {
97 return err
98 }
99 } else {
100 // Make a decoder for it
101 jsonDecoder := json.NewDecoder(configFileReader)
Colin Cross485e5722015-08-27 13:28:01 -0700102 err = jsonDecoder.Decode(configurable)
Colin Cross3f40fa42015-01-30 17:27:36 -0800103 if err != nil {
Colin Cross485e5722015-08-27 13:28:01 -0700104 return fmt.Errorf("config file: %s did not parse correctly: "+err.Error(), filename)
Colin Cross3f40fa42015-01-30 17:27:36 -0800105 }
106 }
107
Colin Cross3f40fa42015-01-30 17:27:36 -0800108 // No error
109 return nil
110}
111
Colin Cross485e5722015-08-27 13:28:01 -0700112func saveToConfigFile(config jsonConfigurable, filename string) error {
Colin Cross3f40fa42015-01-30 17:27:36 -0800113 data, err := json.MarshalIndent(&config, "", " ")
114 if err != nil {
115 return fmt.Errorf("cannot marshal config data: %s", err.Error())
116 }
117
Colin Cross485e5722015-08-27 13:28:01 -0700118 configFileWriter, err := os.Create(filename)
Colin Cross3f40fa42015-01-30 17:27:36 -0800119 if err != nil {
Colin Cross485e5722015-08-27 13:28:01 -0700120 return fmt.Errorf("cannot create empty config file %s: %s\n", filename, err.Error())
Colin Cross3f40fa42015-01-30 17:27:36 -0800121 }
122 defer configFileWriter.Close()
123
124 _, err = configFileWriter.Write(data)
125 if err != nil {
Colin Cross485e5722015-08-27 13:28:01 -0700126 return fmt.Errorf("default config file: %s could not be written: %s", filename, err.Error())
127 }
128
129 _, err = configFileWriter.WriteString("\n")
130 if err != nil {
131 return fmt.Errorf("default config file: %s could not be written: %s", filename, err.Error())
Colin Cross3f40fa42015-01-30 17:27:36 -0800132 }
133
134 return nil
135}
136
137// New creates a new Config object. The srcDir argument specifies the path to
138// the root source directory. It also loads the config file, if found.
Dan Willemsen87b17d12015-07-14 00:39:06 -0700139func NewConfig(srcDir, buildDir string) (Config, error) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800140 // Make a config with default options
Colin Crossc3c0a492015-04-10 15:43:55 -0700141 config := Config{
142 config: &config{
Dan Willemsen87b17d12015-07-14 00:39:06 -0700143 ConfigFileName: filepath.Join(buildDir, configFileName),
144 ProductVariablesFileName: filepath.Join(buildDir, productVariablesFileName),
145
146 srcDir: srcDir,
147 buildDir: buildDir,
148 envDeps: make(map[string]string),
Colin Crossc3c0a492015-04-10 15:43:55 -0700149 },
Colin Cross68f55102015-03-25 14:43:57 -0700150 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800151
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700152 // Sanity check the build and source directories. This won't catch strange
153 // configurations with symlinks, but at least checks the obvious cases.
154 absBuildDir, err := filepath.Abs(buildDir)
155 if err != nil {
156 return Config{}, err
157 }
158
159 absSrcDir, err := filepath.Abs(srcDir)
160 if err != nil {
161 return Config{}, err
162 }
163
164 if strings.HasPrefix(absSrcDir, absBuildDir) {
165 return Config{}, fmt.Errorf("Build dir must not contain source directory")
166 }
167
Colin Cross3f40fa42015-01-30 17:27:36 -0800168 // Load any configurable options from the configuration file
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700169 err = loadConfig(config.config)
Colin Cross3f40fa42015-01-30 17:27:36 -0800170 if err != nil {
Colin Crossc3c0a492015-04-10 15:43:55 -0700171 return Config{}, err
Colin Cross3f40fa42015-01-30 17:27:36 -0800172 }
173
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800174 inMakeFile := filepath.Join(buildDir, ".soong.in_make")
175 if _, err := os.Stat(inMakeFile); err == nil {
176 config.inMake = true
177 }
178
Colin Crossa1ad8d12016-06-01 17:09:44 -0700179 targets, err := decodeTargetProductVariables(config)
Dan Willemsen218f6562015-07-08 18:13:11 -0700180 if err != nil {
181 return Config{}, err
182 }
183
Dan Willemsen322acaf2016-01-12 23:07:05 -0800184 if Bool(config.Mega_device) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700185 deviceTargets, err := decodeMegaDevice()
Dan Willemsen322acaf2016-01-12 23:07:05 -0800186 if err != nil {
187 return Config{}, err
188 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700189 targets[Device] = deviceTargets
Dan Willemsen322acaf2016-01-12 23:07:05 -0800190 }
191
Colin Crossa1ad8d12016-06-01 17:09:44 -0700192 config.Targets = targets
193 config.BuildOsVariant = targets[Host][0].String()
Dan Willemsen218f6562015-07-08 18:13:11 -0700194
Colin Cross3f40fa42015-01-30 17:27:36 -0800195 return config, nil
196}
197
Dan Willemsen218f6562015-07-08 18:13:11 -0700198func (c *config) RemoveAbandonedFiles() bool {
199 return false
200}
201
Dan Willemsenc2aa4a92016-05-26 15:13:03 -0700202func (c *config) BlueprintToolLocation() string {
203 return filepath.Join(c.buildDir, "host", c.PrebuiltOS(), "bin")
204}
205
Colin Cross3f40fa42015-01-30 17:27:36 -0800206// PrebuiltOS returns the name of the host OS used in prebuilts directories
Colin Cross1332b002015-04-07 17:11:30 -0700207func (c *config) PrebuiltOS() string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800208 switch runtime.GOOS {
209 case "linux":
210 return "linux-x86"
211 case "darwin":
212 return "darwin-x86"
213 default:
214 panic("Unknown GOOS")
215 }
216}
217
218// GoRoot returns the path to the root directory of the Go toolchain.
Colin Cross1332b002015-04-07 17:11:30 -0700219func (c *config) GoRoot() string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800220 return fmt.Sprintf("%s/prebuilts/go/%s", c.srcDir, c.PrebuiltOS())
221}
222
Colin Cross1332b002015-04-07 17:11:30 -0700223func (c *config) CpPreserveSymlinksFlags() string {
Colin Cross485e5722015-08-27 13:28:01 -0700224 switch runtime.GOOS {
Colin Cross3f40fa42015-01-30 17:27:36 -0800225 case "darwin":
226 return "-R"
227 case "linux":
228 return "-d"
229 default:
230 return ""
231 }
232}
Colin Cross68f55102015-03-25 14:43:57 -0700233
Colin Cross1332b002015-04-07 17:11:30 -0700234func (c *config) Getenv(key string) string {
Colin Cross68f55102015-03-25 14:43:57 -0700235 var val string
236 var exists bool
Colin Crossc1e86a32015-04-15 12:33:28 -0700237 c.envLock.Lock()
Colin Cross68f55102015-03-25 14:43:57 -0700238 if val, exists = c.envDeps[key]; !exists {
Dan Willemsene7680ba2015-09-11 17:06:19 -0700239 if c.envFrozen {
240 panic("Cannot access new environment variables after envdeps are frozen")
241 }
Colin Cross68f55102015-03-25 14:43:57 -0700242 val = os.Getenv(key)
243 c.envDeps[key] = val
244 }
Colin Crossc1e86a32015-04-15 12:33:28 -0700245 c.envLock.Unlock()
Colin Cross68f55102015-03-25 14:43:57 -0700246 return val
247}
248
Colin Cross1332b002015-04-07 17:11:30 -0700249func (c *config) EnvDeps() map[string]string {
Dan Willemsene7680ba2015-09-11 17:06:19 -0700250 c.envLock.Lock()
251 c.envFrozen = true
252 c.envLock.Unlock()
Colin Cross68f55102015-03-25 14:43:57 -0700253 return c.envDeps
254}
Colin Cross35cec122015-04-02 14:37:16 -0700255
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800256func (c *config) EmbeddedInMake() bool {
257 return c.inMake
258}
259
Colin Cross35cec122015-04-02 14:37:16 -0700260// DeviceName returns the name of the current device target
261// TODO: take an AndroidModuleContext to select the device name for multi-device builds
Colin Cross1332b002015-04-07 17:11:30 -0700262func (c *config) DeviceName() string {
Dan Willemsen1d31ec32015-09-22 16:56:09 -0700263 return *c.ProductVariables.DeviceName
Colin Cross35cec122015-04-02 14:37:16 -0700264}
265
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700266func (c *config) DeviceUsesClang() bool {
267 if c.ProductVariables.DeviceUsesClang != nil {
268 return *c.ProductVariables.DeviceUsesClang
269 }
Dan Willemsen0084d782016-03-01 14:54:24 -0800270 return true
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700271}
272
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700273func (c *config) ResourceOverlays() []SourcePath {
Colin Cross30e076a2015-04-13 13:58:27 -0700274 return nil
275}
276
277func (c *config) PlatformVersion() string {
278 return "M"
279}
280
281func (c *config) PlatformSdkVersion() string {
Dan Willemsen5951c8a2016-07-19 19:08:14 -0700282 return strconv.Itoa(*c.ProductVariables.Platform_sdk_version)
Colin Cross30e076a2015-04-13 13:58:27 -0700283}
284
285func (c *config) BuildNumber() string {
286 return "000000"
287}
288
289func (c *config) ProductAaptConfig() []string {
290 return []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"}
291}
292
293func (c *config) ProductAaptPreferredConfig() string {
294 return "xhdpi"
295}
296
297func (c *config) ProductAaptCharacteristics() string {
298 return "nosdcard"
299}
300
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700301func (c *config) DefaultAppCertificateDir(ctx PathContext) SourcePath {
302 return PathForSource(ctx, "build/target/product/security")
Colin Cross30e076a2015-04-13 13:58:27 -0700303}
304
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700305func (c *config) DefaultAppCertificate(ctx PathContext) SourcePath {
306 return c.DefaultAppCertificateDir(ctx).Join(ctx, "testkey")
Colin Cross30e076a2015-04-13 13:58:27 -0700307}
Colin Cross6ff51382015-12-17 16:39:19 -0800308
309func (c *config) AllowMissingDependencies() bool {
Dan Willemsenb5038162016-03-16 12:35:33 -0700310 return Bool(c.ProductVariables.Allow_missing_dependencies)
Colin Cross6ff51382015-12-17 16:39:19 -0800311}
Dan Willemsen322acaf2016-01-12 23:07:05 -0800312
Dan Willemsen7f730fd2016-01-14 11:22:23 -0800313func (c *config) SkipDeviceInstall() bool {
Dan Willemsen322acaf2016-01-12 23:07:05 -0800314 return c.EmbeddedInMake() || Bool(c.Mega_device)
315}
Colin Cross16b23492016-01-06 14:41:07 -0800316
317func (c *config) SanitizeHost() []string {
318 if c.ProductVariables.SanitizeHost == nil {
319 return nil
320 }
Colin Crosscc85e682016-07-06 14:24:16 -0700321 return append([]string(nil), *c.ProductVariables.SanitizeHost...)
Colin Cross16b23492016-01-06 14:41:07 -0800322}
323
324func (c *config) SanitizeDevice() []string {
325 if c.ProductVariables.SanitizeDevice == nil {
326 return nil
327 }
Colin Crosscc85e682016-07-06 14:24:16 -0700328 return append([]string(nil), *c.ProductVariables.SanitizeDevice...)
Colin Cross16b23492016-01-06 14:41:07 -0800329}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700330
331func (c *config) Android64() bool {
332 for _, t := range c.Targets[Device] {
333 if t.Arch.ArchType.Multilib == "lib64" {
334 return true
335 }
336 }
337
338 return false
339}