config.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. package common
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "os"
  19. "path/filepath"
  20. "runtime"
  21. "sync"
  22. )
  23. // The configuration file name
  24. const configFileName = "soong.config"
  25. const productVariablesFileName = "soong.variables"
  26. // A FileConfigurableOptions contains options which can be configured by the
  27. // config file. These will be included in the config struct.
  28. type FileConfigurableOptions struct {
  29. }
  30. func (FileConfigurableOptions) DefaultConfig() jsonConfigurable {
  31. f := FileConfigurableOptions{}
  32. return f
  33. }
  34. type Config struct {
  35. *config
  36. }
  37. // A config object represents the entire build configuration for Blue.
  38. type config struct {
  39. FileConfigurableOptions
  40. ProductVariables productVariables
  41. ConfigFileName string
  42. ProductVariablesFileName string
  43. srcDir string // the path of the root source directory
  44. buildDir string // the path of the build output directory
  45. envLock sync.Mutex
  46. envDeps map[string]string
  47. envFrozen bool
  48. }
  49. type jsonConfigurable interface {
  50. DefaultConfig() jsonConfigurable
  51. }
  52. func loadConfig(config *config) error {
  53. err := loadFromConfigFile(&config.FileConfigurableOptions, config.ConfigFileName)
  54. if err != nil {
  55. return err
  56. }
  57. return loadFromConfigFile(&config.ProductVariables, config.ProductVariablesFileName)
  58. }
  59. // loads configuration options from a JSON file in the cwd.
  60. func loadFromConfigFile(configurable jsonConfigurable, filename string) error {
  61. // Try to open the file
  62. configFileReader, err := os.Open(filename)
  63. defer configFileReader.Close()
  64. if os.IsNotExist(err) {
  65. // Need to create a file, so that blueprint & ninja don't get in
  66. // a dependency tracking loop.
  67. // Make a file-configurable-options with defaults, write it out using
  68. // a json writer.
  69. err = saveToConfigFile(configurable.DefaultConfig(), filename)
  70. if err != nil {
  71. return err
  72. }
  73. } else {
  74. // Make a decoder for it
  75. jsonDecoder := json.NewDecoder(configFileReader)
  76. err = jsonDecoder.Decode(configurable)
  77. if err != nil {
  78. return fmt.Errorf("config file: %s did not parse correctly: "+err.Error(), filename)
  79. }
  80. }
  81. // No error
  82. return nil
  83. }
  84. func saveToConfigFile(config jsonConfigurable, filename string) error {
  85. data, err := json.MarshalIndent(&config, "", " ")
  86. if err != nil {
  87. return fmt.Errorf("cannot marshal config data: %s", err.Error())
  88. }
  89. configFileWriter, err := os.Create(filename)
  90. if err != nil {
  91. return fmt.Errorf("cannot create empty config file %s: %s\n", filename, err.Error())
  92. }
  93. defer configFileWriter.Close()
  94. _, err = configFileWriter.Write(data)
  95. if err != nil {
  96. return fmt.Errorf("default config file: %s could not be written: %s", filename, err.Error())
  97. }
  98. _, err = configFileWriter.WriteString("\n")
  99. if err != nil {
  100. return fmt.Errorf("default config file: %s could not be written: %s", filename, err.Error())
  101. }
  102. return nil
  103. }
  104. // New creates a new Config object. The srcDir argument specifies the path to
  105. // the root source directory. It also loads the config file, if found.
  106. func NewConfig(srcDir, buildDir string) (Config, error) {
  107. // Make a config with default options
  108. config := Config{
  109. config: &config{
  110. ConfigFileName: filepath.Join(buildDir, configFileName),
  111. ProductVariablesFileName: filepath.Join(buildDir, productVariablesFileName),
  112. srcDir: srcDir,
  113. buildDir: buildDir,
  114. envDeps: make(map[string]string),
  115. },
  116. }
  117. // Load any configurable options from the configuration file
  118. err := loadConfig(config.config)
  119. if err != nil {
  120. return Config{}, err
  121. }
  122. return config, nil
  123. }
  124. func (c *config) SrcDir() string {
  125. return c.srcDir
  126. }
  127. func (c *config) BuildDir() string {
  128. return c.buildDir
  129. }
  130. func (c *config) IntermediatesDir() string {
  131. return filepath.Join(c.BuildDir(), ".intermediates")
  132. }
  133. // PrebuiltOS returns the name of the host OS used in prebuilts directories
  134. func (c *config) PrebuiltOS() string {
  135. switch runtime.GOOS {
  136. case "linux":
  137. return "linux-x86"
  138. case "darwin":
  139. return "darwin-x86"
  140. default:
  141. panic("Unknown GOOS")
  142. }
  143. }
  144. // GoRoot returns the path to the root directory of the Go toolchain.
  145. func (c *config) GoRoot() string {
  146. return fmt.Sprintf("%s/prebuilts/go/%s", c.srcDir, c.PrebuiltOS())
  147. }
  148. func (c *config) CpPreserveSymlinksFlags() string {
  149. switch runtime.GOOS {
  150. case "darwin":
  151. return "-R"
  152. case "linux":
  153. return "-d"
  154. default:
  155. return ""
  156. }
  157. }
  158. func (c *config) Getenv(key string) string {
  159. var val string
  160. var exists bool
  161. c.envLock.Lock()
  162. if val, exists = c.envDeps[key]; !exists {
  163. if c.envFrozen {
  164. panic("Cannot access new environment variables after envdeps are frozen")
  165. }
  166. val = os.Getenv(key)
  167. c.envDeps[key] = val
  168. }
  169. c.envLock.Unlock()
  170. return val
  171. }
  172. func (c *config) EnvDeps() map[string]string {
  173. c.envLock.Lock()
  174. c.envFrozen = true
  175. c.envLock.Unlock()
  176. return c.envDeps
  177. }
  178. // DeviceName returns the name of the current device target
  179. // TODO: take an AndroidModuleContext to select the device name for multi-device builds
  180. func (c *config) DeviceName() string {
  181. return "unset"
  182. }
  183. // DeviceOut returns the path to out directory for device targets
  184. func (c *config) DeviceOut() string {
  185. return filepath.Join(c.BuildDir(), "target/product", c.DeviceName())
  186. }
  187. // HostOut returns the path to out directory for host targets
  188. func (c *config) HostOut() string {
  189. return filepath.Join(c.BuildDir(), "host", c.PrebuiltOS())
  190. }
  191. // HostBin returns the path to bin directory for host targets
  192. func (c *config) HostBin() string {
  193. return filepath.Join(c.HostOut(), "bin")
  194. }
  195. // HostBinTool returns the path to a host tool in the bin directory for host targets
  196. func (c *config) HostBinTool(tool string) (string, error) {
  197. return filepath.Join(c.HostBin(), tool), nil
  198. }
  199. // HostJavaDir returns the path to framework directory for host targets
  200. func (c *config) HostJavaDir() string {
  201. return filepath.Join(c.HostOut(), "framework")
  202. }
  203. // HostJavaTool returns the path to a host tool in the frameworks directory for host targets
  204. func (c *config) HostJavaTool(tool string) (string, error) {
  205. return filepath.Join(c.HostJavaDir(), tool), nil
  206. }
  207. func (c *config) ResourceOverlays() []string {
  208. return nil
  209. }
  210. func (c *config) PlatformVersion() string {
  211. return "M"
  212. }
  213. func (c *config) PlatformSdkVersion() string {
  214. return "22"
  215. }
  216. func (c *config) BuildNumber() string {
  217. return "000000"
  218. }
  219. func (c *config) ProductAaptConfig() []string {
  220. return []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"}
  221. }
  222. func (c *config) ProductAaptPreferredConfig() string {
  223. return "xhdpi"
  224. }
  225. func (c *config) ProductAaptCharacteristics() string {
  226. return "nosdcard"
  227. }
  228. func (c *config) DefaultAppCertificateDir() string {
  229. return filepath.Join(c.SrcDir(), "build/target/product/security")
  230. }
  231. func (c *config) DefaultAppCertificate() string {
  232. return filepath.Join(c.DefaultAppCertificateDir(), "testkey")
  233. }