2015-01-31 02:27:36 +01:00
|
|
|
// Copyright 2015 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-04-08 02:11:30 +02:00
|
|
|
package common
|
2015-01-31 02:27:36 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-04-02 23:37:16 +02:00
|
|
|
"path/filepath"
|
2015-01-31 02:27:36 +01:00
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
2015-04-08 02:11:30 +02:00
|
|
|
type Config interface {
|
|
|
|
CpPreserveSymlinksFlags() string
|
|
|
|
SrcDir() string
|
|
|
|
IntermediatesDir() string
|
|
|
|
Getenv(string) string
|
|
|
|
EnvDeps() map[string]string
|
|
|
|
DeviceOut() string
|
|
|
|
HostOut() string
|
|
|
|
PrebuiltOS() string
|
|
|
|
HostBinTool(string) (string, error)
|
|
|
|
HostJavaTool(string) (string, error)
|
|
|
|
}
|
2015-01-31 02:27:36 +01:00
|
|
|
|
|
|
|
// The configuration file name
|
|
|
|
const ConfigFileName = "soong.config"
|
|
|
|
|
|
|
|
// A FileConfigurableOptions contains options which can be configured by the
|
|
|
|
// config file. These will be included in the config struct.
|
|
|
|
type FileConfigurableOptions struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFileConfigurableOptions() FileConfigurableOptions {
|
|
|
|
f := FileConfigurableOptions{}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// A Config object represents the entire build configuration for Blue.
|
2015-04-08 02:11:30 +02:00
|
|
|
type config struct {
|
2015-01-31 02:27:36 +01:00
|
|
|
FileConfigurableOptions
|
|
|
|
|
2015-03-25 22:43:57 +01:00
|
|
|
srcDir string // the path of the root source directory
|
|
|
|
envDeps map[string]string
|
2015-01-31 02:27:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// loads configuration options from a JSON file in the cwd.
|
2015-04-08 02:11:30 +02:00
|
|
|
func loadFromConfigFile(config *config) error {
|
2015-01-31 02:27:36 +01:00
|
|
|
// Make a proxy config
|
|
|
|
var configProxy FileConfigurableOptions
|
|
|
|
|
|
|
|
// Try to open the file
|
|
|
|
configFileReader, err := os.Open(ConfigFileName)
|
|
|
|
defer configFileReader.Close()
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// Need to create a file, so that blueprint & ninja don't get in
|
|
|
|
// a dependency tracking loop.
|
|
|
|
// Make a file-configurable-options with defaults, write it out using
|
|
|
|
// a json writer.
|
|
|
|
configProxy = NewFileConfigurableOptions()
|
|
|
|
err = saveToConfigFile(configProxy)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Make a decoder for it
|
|
|
|
jsonDecoder := json.NewDecoder(configFileReader)
|
|
|
|
err = jsonDecoder.Decode(&configProxy)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("config file: %s did not parse correctly: "+err.Error(), ConfigFileName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the configurable options out of the config_proxy into the config,
|
|
|
|
// and we're done!
|
|
|
|
config.FileConfigurableOptions = configProxy
|
|
|
|
|
|
|
|
// No error
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveToConfigFile(config FileConfigurableOptions) error {
|
|
|
|
data, err := json.MarshalIndent(&config, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot marshal config data: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
configFileWriter, err := os.Create(ConfigFileName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot create empty config file %s: %s\n", ConfigFileName, err.Error())
|
|
|
|
}
|
|
|
|
defer configFileWriter.Close()
|
|
|
|
|
|
|
|
_, err = configFileWriter.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("default config file: %s could not be written: %s", ConfigFileName, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new Config object. The srcDir argument specifies the path to
|
|
|
|
// the root source directory. It also loads the config file, if found.
|
2015-04-08 02:11:30 +02:00
|
|
|
func NewConfig(srcDir string) (Config, error) {
|
2015-01-31 02:27:36 +01:00
|
|
|
// Make a config with default options
|
2015-04-08 02:11:30 +02:00
|
|
|
config := &config{
|
2015-03-25 22:43:57 +01:00
|
|
|
srcDir: srcDir,
|
|
|
|
envDeps: make(map[string]string),
|
|
|
|
}
|
2015-01-31 02:27:36 +01:00
|
|
|
|
|
|
|
// Load any configurable options from the configuration file
|
|
|
|
err := loadFromConfigFile(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2015-04-08 02:11:30 +02:00
|
|
|
func (c *config) SrcDir() string {
|
2015-01-31 02:27:36 +01:00
|
|
|
return c.srcDir
|
|
|
|
}
|
|
|
|
|
2015-04-08 02:11:30 +02:00
|
|
|
func (c *config) IntermediatesDir() string {
|
2015-04-08 01:50:10 +02:00
|
|
|
return ".intermediates"
|
|
|
|
}
|
|
|
|
|
2015-01-31 02:27:36 +01:00
|
|
|
// HostGoOS returns the OS of the system that the Go toolchain is being run on.
|
2015-04-08 02:11:30 +02:00
|
|
|
func (c *config) HostGoOS() string {
|
2015-01-31 02:27:36 +01:00
|
|
|
return runtime.GOOS
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrebuiltOS returns the name of the host OS used in prebuilts directories
|
2015-04-08 02:11:30 +02:00
|
|
|
func (c *config) PrebuiltOS() string {
|
2015-01-31 02:27:36 +01:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "linux":
|
|
|
|
return "linux-x86"
|
|
|
|
case "darwin":
|
|
|
|
return "darwin-x86"
|
|
|
|
default:
|
|
|
|
panic("Unknown GOOS")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GoRoot returns the path to the root directory of the Go toolchain.
|
2015-04-08 02:11:30 +02:00
|
|
|
func (c *config) GoRoot() string {
|
2015-01-31 02:27:36 +01:00
|
|
|
return fmt.Sprintf("%s/prebuilts/go/%s", c.srcDir, c.PrebuiltOS())
|
|
|
|
}
|
|
|
|
|
2015-04-08 02:11:30 +02:00
|
|
|
func (c *config) CpPreserveSymlinksFlags() string {
|
2015-01-31 02:27:36 +01:00
|
|
|
switch c.HostGoOS() {
|
|
|
|
case "darwin":
|
|
|
|
return "-R"
|
|
|
|
case "linux":
|
|
|
|
return "-d"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
2015-03-25 22:43:57 +01:00
|
|
|
|
2015-04-08 02:11:30 +02:00
|
|
|
func (c *config) Getenv(key string) string {
|
2015-03-25 22:43:57 +01:00
|
|
|
var val string
|
|
|
|
var exists bool
|
|
|
|
if val, exists = c.envDeps[key]; !exists {
|
|
|
|
val = os.Getenv(key)
|
|
|
|
c.envDeps[key] = val
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2015-04-08 02:11:30 +02:00
|
|
|
func (c *config) EnvDeps() map[string]string {
|
2015-03-25 22:43:57 +01:00
|
|
|
return c.envDeps
|
|
|
|
}
|
2015-04-02 23:37:16 +02:00
|
|
|
|
|
|
|
// DeviceName returns the name of the current device target
|
|
|
|
// TODO: take an AndroidModuleContext to select the device name for multi-device builds
|
2015-04-08 02:11:30 +02:00
|
|
|
func (c *config) DeviceName() string {
|
2015-04-02 23:37:16 +02:00
|
|
|
return "unset"
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeviceOut returns the path to out directory for device targets
|
2015-04-08 02:11:30 +02:00
|
|
|
func (c *config) DeviceOut() string {
|
2015-04-02 23:37:16 +02:00
|
|
|
return filepath.Join("target/product", c.DeviceName())
|
|
|
|
}
|
|
|
|
|
|
|
|
// HostOut returns the path to out directory for host targets
|
2015-04-08 02:11:30 +02:00
|
|
|
func (c *config) HostOut() string {
|
2015-04-02 23:37:16 +02:00
|
|
|
return filepath.Join("host", c.PrebuiltOS())
|
|
|
|
}
|
|
|
|
|
|
|
|
// HostBin returns the path to bin directory for host targets
|
2015-04-08 02:11:30 +02:00
|
|
|
func (c *config) HostBin() string {
|
2015-04-02 23:37:16 +02:00
|
|
|
return filepath.Join(c.HostOut(), "bin")
|
|
|
|
}
|
|
|
|
|
|
|
|
// HostBinTool returns the path to a host tool in the bin directory for host targets
|
2015-04-08 02:11:30 +02:00
|
|
|
func (c *config) HostBinTool(tool string) (string, error) {
|
2015-04-02 23:37:16 +02:00
|
|
|
return filepath.Join(c.HostBin(), tool), nil
|
|
|
|
}
|
2015-04-04 01:54:17 +02:00
|
|
|
|
|
|
|
// HostJavaDir returns the path to framework directory for host targets
|
2015-04-08 02:11:30 +02:00
|
|
|
func (c *config) HostJavaDir() string {
|
2015-04-04 01:54:17 +02:00
|
|
|
return filepath.Join(c.HostOut(), "framework")
|
|
|
|
}
|
|
|
|
|
|
|
|
// HostJavaTool returns the path to a host tool in the frameworks directory for host targets
|
2015-04-08 02:11:30 +02:00
|
|
|
func (c *config) HostJavaTool(tool string) (string, error) {
|
2015-04-04 01:54:17 +02:00
|
|
|
return filepath.Join(c.HostJavaDir(), tool), nil
|
|
|
|
}
|