blob: 483ec911a3b7689d8c3c882c01f06a025a1731fc [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 Cross9272ade2016-08-17 15:24:12 -070046// A Config object represents the entire build configuration for Android.
Colin Crossc3c0a492015-04-10 15:43:55 -070047type Config struct {
48 *config
49}
50
Colin Cross9272ade2016-08-17 15:24:12 -070051// A DeviceConfig object represents the configuration for a particular device being built. For
52// now there will only be one of these, but in the future there may be multiple devices being
53// built
54type DeviceConfig struct {
55 *deviceConfig
56}
57
Colin Cross1332b002015-04-07 17:11:30 -070058type config struct {
Colin Cross3f40fa42015-01-30 17:27:36 -080059 FileConfigurableOptions
Colin Cross485e5722015-08-27 13:28:01 -070060 ProductVariables productVariables
Colin Cross3f40fa42015-01-30 17:27:36 -080061
Dan Willemsen87b17d12015-07-14 00:39:06 -070062 ConfigFileName string
63 ProductVariablesFileName string
64
Colin Crossa1ad8d12016-06-01 17:09:44 -070065 Targets map[OsClass][]Target
66 BuildOsVariant string
Dan Willemsen218f6562015-07-08 18:13:11 -070067
Colin Cross9272ade2016-08-17 15:24:12 -070068 deviceConfig *deviceConfig
69
Dan Willemsen87b17d12015-07-14 00:39:06 -070070 srcDir string // the path of the root source directory
71 buildDir string // the path of the build output directory
Colin Crossc1e86a32015-04-15 12:33:28 -070072
Dan Willemsene7680ba2015-09-11 17:06:19 -070073 envLock sync.Mutex
74 envDeps map[string]string
75 envFrozen bool
Dan Willemsen5ba07e82015-12-11 13:51:06 -080076
77 inMake bool
Colin Cross1e7d3702016-08-24 15:25:47 -070078
Colin Cross9272ade2016-08-17 15:24:12 -070079 OncePer
80}
81
82type deviceConfig struct {
83 config *config
84 targets []Arch
85 OncePer
Colin Cross3f40fa42015-01-30 17:27:36 -080086}
87
Colin Cross485e5722015-08-27 13:28:01 -070088type jsonConfigurable interface {
Colin Cross27385972015-09-18 10:57:10 -070089 SetDefaultConfig()
Colin Cross485e5722015-08-27 13:28:01 -070090}
Colin Cross3f40fa42015-01-30 17:27:36 -080091
Colin Cross485e5722015-08-27 13:28:01 -070092func loadConfig(config *config) error {
Dan Willemsen87b17d12015-07-14 00:39:06 -070093 err := loadFromConfigFile(&config.FileConfigurableOptions, config.ConfigFileName)
Colin Cross485e5722015-08-27 13:28:01 -070094 if err != nil {
95 return err
96 }
97
Dan Willemsen87b17d12015-07-14 00:39:06 -070098 return loadFromConfigFile(&config.ProductVariables, config.ProductVariablesFileName)
Colin Cross485e5722015-08-27 13:28:01 -070099}
100
101// loads configuration options from a JSON file in the cwd.
102func loadFromConfigFile(configurable jsonConfigurable, filename string) error {
Colin Cross3f40fa42015-01-30 17:27:36 -0800103 // Try to open the file
Colin Cross485e5722015-08-27 13:28:01 -0700104 configFileReader, err := os.Open(filename)
Colin Cross3f40fa42015-01-30 17:27:36 -0800105 defer configFileReader.Close()
106 if os.IsNotExist(err) {
107 // Need to create a file, so that blueprint & ninja don't get in
108 // a dependency tracking loop.
109 // Make a file-configurable-options with defaults, write it out using
110 // a json writer.
Colin Cross27385972015-09-18 10:57:10 -0700111 configurable.SetDefaultConfig()
112 err = saveToConfigFile(configurable, filename)
Colin Cross3f40fa42015-01-30 17:27:36 -0800113 if err != nil {
114 return err
115 }
116 } else {
117 // Make a decoder for it
118 jsonDecoder := json.NewDecoder(configFileReader)
Colin Cross485e5722015-08-27 13:28:01 -0700119 err = jsonDecoder.Decode(configurable)
Colin Cross3f40fa42015-01-30 17:27:36 -0800120 if err != nil {
Colin Cross485e5722015-08-27 13:28:01 -0700121 return fmt.Errorf("config file: %s did not parse correctly: "+err.Error(), filename)
Colin Cross3f40fa42015-01-30 17:27:36 -0800122 }
123 }
124
Colin Cross3f40fa42015-01-30 17:27:36 -0800125 // No error
126 return nil
127}
128
Colin Cross485e5722015-08-27 13:28:01 -0700129func saveToConfigFile(config jsonConfigurable, filename string) error {
Colin Cross3f40fa42015-01-30 17:27:36 -0800130 data, err := json.MarshalIndent(&config, "", " ")
131 if err != nil {
132 return fmt.Errorf("cannot marshal config data: %s", err.Error())
133 }
134
Colin Cross485e5722015-08-27 13:28:01 -0700135 configFileWriter, err := os.Create(filename)
Colin Cross3f40fa42015-01-30 17:27:36 -0800136 if err != nil {
Colin Cross485e5722015-08-27 13:28:01 -0700137 return fmt.Errorf("cannot create empty config file %s: %s\n", filename, err.Error())
Colin Cross3f40fa42015-01-30 17:27:36 -0800138 }
139 defer configFileWriter.Close()
140
141 _, err = configFileWriter.Write(data)
142 if err != nil {
Colin Cross485e5722015-08-27 13:28:01 -0700143 return fmt.Errorf("default config file: %s could not be written: %s", filename, err.Error())
144 }
145
146 _, err = configFileWriter.WriteString("\n")
147 if err != nil {
148 return fmt.Errorf("default config file: %s could not be written: %s", filename, err.Error())
Colin Cross3f40fa42015-01-30 17:27:36 -0800149 }
150
151 return nil
152}
153
Colin Crossce75d2c2016-10-06 16:12:58 -0700154// TestConfig returns a Config object suitable for using for tests
Colin Cross0d614dd2016-10-14 15:38:43 -0700155func TestConfig(buildDir string) Config {
156 return Config{&config{
157 buildDir: buildDir,
158 }}
Colin Crossce75d2c2016-10-06 16:12:58 -0700159}
160
Colin Cross3f40fa42015-01-30 17:27:36 -0800161// New creates a new Config object. The srcDir argument specifies the path to
162// the root source directory. It also loads the config file, if found.
Dan Willemsen87b17d12015-07-14 00:39:06 -0700163func NewConfig(srcDir, buildDir string) (Config, error) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800164 // Make a config with default options
Colin Cross9272ade2016-08-17 15:24:12 -0700165 config := &config{
166 ConfigFileName: filepath.Join(buildDir, configFileName),
167 ProductVariablesFileName: filepath.Join(buildDir, productVariablesFileName),
Dan Willemsen87b17d12015-07-14 00:39:06 -0700168
Colin Cross9272ade2016-08-17 15:24:12 -0700169 srcDir: srcDir,
170 buildDir: buildDir,
171 envDeps: make(map[string]string),
172
173 deviceConfig: &deviceConfig{},
Colin Cross68f55102015-03-25 14:43:57 -0700174 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800175
Colin Cross9272ade2016-08-17 15:24:12 -0700176 deviceConfig := &deviceConfig{
177 config: config,
178 }
179
180 config.deviceConfig = deviceConfig
181
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700182 // Sanity check the build and source directories. This won't catch strange
183 // configurations with symlinks, but at least checks the obvious cases.
184 absBuildDir, err := filepath.Abs(buildDir)
185 if err != nil {
186 return Config{}, err
187 }
188
189 absSrcDir, err := filepath.Abs(srcDir)
190 if err != nil {
191 return Config{}, err
192 }
193
194 if strings.HasPrefix(absSrcDir, absBuildDir) {
195 return Config{}, fmt.Errorf("Build dir must not contain source directory")
196 }
197
Colin Cross3f40fa42015-01-30 17:27:36 -0800198 // Load any configurable options from the configuration file
Colin Cross9272ade2016-08-17 15:24:12 -0700199 err = loadConfig(config)
Colin Cross3f40fa42015-01-30 17:27:36 -0800200 if err != nil {
Colin Crossc3c0a492015-04-10 15:43:55 -0700201 return Config{}, err
Colin Cross3f40fa42015-01-30 17:27:36 -0800202 }
203
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800204 inMakeFile := filepath.Join(buildDir, ".soong.in_make")
205 if _, err := os.Stat(inMakeFile); err == nil {
206 config.inMake = true
207 }
208
Colin Crossa1ad8d12016-06-01 17:09:44 -0700209 targets, err := decodeTargetProductVariables(config)
Dan Willemsen218f6562015-07-08 18:13:11 -0700210 if err != nil {
211 return Config{}, err
212 }
213
Dan Willemsen322acaf2016-01-12 23:07:05 -0800214 if Bool(config.Mega_device) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700215 deviceTargets, err := decodeMegaDevice()
Dan Willemsen322acaf2016-01-12 23:07:05 -0800216 if err != nil {
217 return Config{}, err
218 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700219 targets[Device] = deviceTargets
Dan Willemsen322acaf2016-01-12 23:07:05 -0800220 }
221
Colin Crossa1ad8d12016-06-01 17:09:44 -0700222 config.Targets = targets
223 config.BuildOsVariant = targets[Host][0].String()
Dan Willemsen218f6562015-07-08 18:13:11 -0700224
Colin Cross9272ade2016-08-17 15:24:12 -0700225 return Config{config}, nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800226}
227
Dan Willemsen218f6562015-07-08 18:13:11 -0700228func (c *config) RemoveAbandonedFiles() bool {
229 return false
230}
231
Dan Willemsenc2aa4a92016-05-26 15:13:03 -0700232func (c *config) BlueprintToolLocation() string {
233 return filepath.Join(c.buildDir, "host", c.PrebuiltOS(), "bin")
234}
235
Colin Cross3f40fa42015-01-30 17:27:36 -0800236// PrebuiltOS returns the name of the host OS used in prebuilts directories
Colin Cross1332b002015-04-07 17:11:30 -0700237func (c *config) PrebuiltOS() string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800238 switch runtime.GOOS {
239 case "linux":
240 return "linux-x86"
241 case "darwin":
242 return "darwin-x86"
243 default:
244 panic("Unknown GOOS")
245 }
246}
247
248// GoRoot returns the path to the root directory of the Go toolchain.
Colin Cross1332b002015-04-07 17:11:30 -0700249func (c *config) GoRoot() string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800250 return fmt.Sprintf("%s/prebuilts/go/%s", c.srcDir, c.PrebuiltOS())
251}
252
Colin Cross1332b002015-04-07 17:11:30 -0700253func (c *config) CpPreserveSymlinksFlags() string {
Colin Cross485e5722015-08-27 13:28:01 -0700254 switch runtime.GOOS {
Colin Cross3f40fa42015-01-30 17:27:36 -0800255 case "darwin":
256 return "-R"
257 case "linux":
258 return "-d"
259 default:
260 return ""
261 }
262}
Colin Cross68f55102015-03-25 14:43:57 -0700263
Colin Cross1332b002015-04-07 17:11:30 -0700264func (c *config) Getenv(key string) string {
Colin Cross68f55102015-03-25 14:43:57 -0700265 var val string
266 var exists bool
Colin Crossc1e86a32015-04-15 12:33:28 -0700267 c.envLock.Lock()
Colin Cross68f55102015-03-25 14:43:57 -0700268 if val, exists = c.envDeps[key]; !exists {
Dan Willemsene7680ba2015-09-11 17:06:19 -0700269 if c.envFrozen {
270 panic("Cannot access new environment variables after envdeps are frozen")
271 }
Colin Cross68f55102015-03-25 14:43:57 -0700272 val = os.Getenv(key)
273 c.envDeps[key] = val
274 }
Colin Crossc1e86a32015-04-15 12:33:28 -0700275 c.envLock.Unlock()
Colin Cross68f55102015-03-25 14:43:57 -0700276 return val
277}
278
Colin Cross1332b002015-04-07 17:11:30 -0700279func (c *config) EnvDeps() map[string]string {
Dan Willemsene7680ba2015-09-11 17:06:19 -0700280 c.envLock.Lock()
281 c.envFrozen = true
282 c.envLock.Unlock()
Colin Cross68f55102015-03-25 14:43:57 -0700283 return c.envDeps
284}
Colin Cross35cec122015-04-02 14:37:16 -0700285
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800286func (c *config) EmbeddedInMake() bool {
287 return c.inMake
288}
289
Colin Cross35cec122015-04-02 14:37:16 -0700290// DeviceName returns the name of the current device target
291// TODO: take an AndroidModuleContext to select the device name for multi-device builds
Colin Cross1332b002015-04-07 17:11:30 -0700292func (c *config) DeviceName() string {
Dan Willemsen1d31ec32015-09-22 16:56:09 -0700293 return *c.ProductVariables.DeviceName
Colin Cross35cec122015-04-02 14:37:16 -0700294}
295
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700296func (c *config) DeviceUsesClang() bool {
297 if c.ProductVariables.DeviceUsesClang != nil {
298 return *c.ProductVariables.DeviceUsesClang
299 }
Dan Willemsen0084d782016-03-01 14:54:24 -0800300 return true
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700301}
302
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700303func (c *config) ResourceOverlays() []SourcePath {
Colin Cross30e076a2015-04-13 13:58:27 -0700304 return nil
305}
306
307func (c *config) PlatformVersion() string {
308 return "M"
309}
310
311func (c *config) PlatformSdkVersion() string {
Dan Willemsen5951c8a2016-07-19 19:08:14 -0700312 return strconv.Itoa(*c.ProductVariables.Platform_sdk_version)
Colin Cross30e076a2015-04-13 13:58:27 -0700313}
314
315func (c *config) BuildNumber() string {
316 return "000000"
317}
318
319func (c *config) ProductAaptConfig() []string {
320 return []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"}
321}
322
323func (c *config) ProductAaptPreferredConfig() string {
324 return "xhdpi"
325}
326
327func (c *config) ProductAaptCharacteristics() string {
328 return "nosdcard"
329}
330
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700331func (c *config) DefaultAppCertificateDir(ctx PathContext) SourcePath {
332 return PathForSource(ctx, "build/target/product/security")
Colin Cross30e076a2015-04-13 13:58:27 -0700333}
334
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700335func (c *config) DefaultAppCertificate(ctx PathContext) SourcePath {
336 return c.DefaultAppCertificateDir(ctx).Join(ctx, "testkey")
Colin Cross30e076a2015-04-13 13:58:27 -0700337}
Colin Cross6ff51382015-12-17 16:39:19 -0800338
339func (c *config) AllowMissingDependencies() bool {
Dan Willemsenb5038162016-03-16 12:35:33 -0700340 return Bool(c.ProductVariables.Allow_missing_dependencies)
Colin Cross6ff51382015-12-17 16:39:19 -0800341}
Dan Willemsen322acaf2016-01-12 23:07:05 -0800342
Colin Cross1e7d3702016-08-24 15:25:47 -0700343func (c *config) DevicePrefer32BitExecutables() bool {
344 return Bool(c.ProductVariables.DevicePrefer32BitExecutables)
345}
346
Dan Willemsen7f730fd2016-01-14 11:22:23 -0800347func (c *config) SkipDeviceInstall() bool {
Dan Willemsen322acaf2016-01-12 23:07:05 -0800348 return c.EmbeddedInMake() || Bool(c.Mega_device)
349}
Colin Cross16b23492016-01-06 14:41:07 -0800350
351func (c *config) SanitizeHost() []string {
352 if c.ProductVariables.SanitizeHost == nil {
353 return nil
354 }
Colin Crosscc85e682016-07-06 14:24:16 -0700355 return append([]string(nil), *c.ProductVariables.SanitizeHost...)
Colin Cross16b23492016-01-06 14:41:07 -0800356}
357
358func (c *config) SanitizeDevice() []string {
359 if c.ProductVariables.SanitizeDevice == nil {
360 return nil
361 }
Colin Crosscc85e682016-07-06 14:24:16 -0700362 return append([]string(nil), *c.ProductVariables.SanitizeDevice...)
Colin Cross16b23492016-01-06 14:41:07 -0800363}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700364
365func (c *config) Android64() bool {
366 for _, t := range c.Targets[Device] {
367 if t.Arch.ArchType.Multilib == "lib64" {
368 return true
369 }
370 }
371
372 return false
373}
Colin Cross9272ade2016-08-17 15:24:12 -0700374
Colin Cross9d45bb72016-08-29 16:14:13 -0700375func (c *config) UseGoma() bool {
376 return Bool(c.ProductVariables.UseGoma)
377}
378
Colin Cross0f4e0d62016-07-27 10:56:55 -0700379func (c *config) LibartImgHostBaseAddress() string {
380 return "0x60000000"
381}
382
383func (c *config) LibartImgDeviceBaseAddress() string {
384 switch c.Targets[Device][0].Arch.ArchType {
385 default:
386 return "0x70000000"
387 case Mips, Mips64:
388 return "0x30000000"
389 }
390}
391
Colin Cross9272ade2016-08-17 15:24:12 -0700392func (c *deviceConfig) Arches() []Arch {
393 var arches []Arch
394 for _, target := range c.config.Targets[Device] {
395 arches = append(arches, target.Arch)
396 }
397 return arches
398}