2015-07-09 03:13:11 +02: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.
|
|
|
|
|
2016-05-19 00:37:25 +02:00
|
|
|
package android
|
2015-07-09 03:13:11 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-02-10 02:43:51 +01:00
|
|
|
"fmt"
|
2015-07-09 03:13:11 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
2016-06-02 00:25:32 +02:00
|
|
|
"strings"
|
2015-07-09 03:13:11 +02:00
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
2016-05-11 09:27:49 +02:00
|
|
|
"github.com/google/blueprint/proptools"
|
2015-07-09 03:13:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2016-10-12 23:28:16 +02:00
|
|
|
RegisterSingletonType("androidmk", AndroidMkSingleton)
|
2015-07-09 03:13:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type AndroidMkDataProvider interface {
|
2016-02-10 02:43:51 +01:00
|
|
|
AndroidMk() (AndroidMkData, error)
|
2015-07-09 03:13:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type AndroidMkData struct {
|
|
|
|
Class string
|
2016-03-24 21:14:12 +01:00
|
|
|
SubName string
|
2015-09-24 00:26:20 +02:00
|
|
|
OutputFile OptionalPath
|
2016-01-04 23:34:37 +01:00
|
|
|
Disabled bool
|
2015-07-09 03:13:11 +02:00
|
|
|
|
2016-02-10 02:43:51 +01:00
|
|
|
Custom func(w io.Writer, name, prefix string) error
|
2015-07-09 03:13:11 +02:00
|
|
|
|
2016-01-04 23:34:37 +01:00
|
|
|
Extra []func(w io.Writer, outputFile Path) error
|
2015-07-09 03:13:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func AndroidMkSingleton() blueprint.Singleton {
|
|
|
|
return &androidMkSingleton{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type androidMkSingleton struct{}
|
|
|
|
|
|
|
|
func (c *androidMkSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
|
2016-05-11 09:27:49 +02:00
|
|
|
config := ctx.Config().(Config)
|
|
|
|
|
|
|
|
if !config.EmbeddedInMake() {
|
2015-12-11 22:51:06 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-11 09:27:49 +02:00
|
|
|
ctx.SetNinjaBuildDir(pctx, filepath.Join(config.buildDir, ".."))
|
2015-07-09 03:13:11 +02:00
|
|
|
|
2016-05-19 00:37:25 +02:00
|
|
|
var androidMkModulesList []Module
|
2015-07-09 03:13:11 +02:00
|
|
|
|
2016-01-11 21:55:55 +01:00
|
|
|
ctx.VisitAllModules(func(module blueprint.Module) {
|
2016-05-19 00:37:25 +02:00
|
|
|
if amod, ok := module.(Module); ok {
|
2015-07-09 03:13:11 +02:00
|
|
|
androidMkModulesList = append(androidMkModulesList, amod)
|
|
|
|
}
|
2016-01-11 21:55:55 +01:00
|
|
|
})
|
2015-07-09 03:13:11 +02:00
|
|
|
|
2015-12-18 03:00:23 +01:00
|
|
|
sort.Sort(AndroidModulesByName{androidMkModulesList, ctx})
|
|
|
|
|
2016-05-11 09:27:49 +02:00
|
|
|
transMk := PathForOutput(ctx, "Android"+proptools.String(config.ProductVariables.Make_suffix)+".mk")
|
2015-09-24 00:26:20 +02:00
|
|
|
if ctx.Failed() {
|
|
|
|
return
|
|
|
|
}
|
2015-07-09 03:13:11 +02:00
|
|
|
|
2015-09-24 00:26:20 +02:00
|
|
|
err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList)
|
2015-07-09 03:13:11 +02:00
|
|
|
if err != nil {
|
|
|
|
ctx.Errorf(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Build(pctx, blueprint.BuildParams{
|
|
|
|
Rule: blueprint.Phony,
|
2015-09-24 00:26:20 +02:00
|
|
|
Outputs: []string{transMk.String()},
|
2015-07-09 03:13:11 +02:00
|
|
|
Optional: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-19 00:37:25 +02:00
|
|
|
func translateAndroidMk(ctx blueprint.SingletonContext, mkFile string, mods []Module) error {
|
2015-07-09 03:13:11 +02:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
|
2016-02-10 02:43:51 +01:00
|
|
|
fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
|
2015-07-09 03:13:11 +02:00
|
|
|
|
2016-07-26 01:00:20 +02:00
|
|
|
type_stats := make(map[string]int)
|
2015-07-09 03:13:11 +02:00
|
|
|
for _, mod := range mods {
|
|
|
|
err := translateAndroidMkModule(ctx, buf, mod)
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(mkFile)
|
|
|
|
return err
|
|
|
|
}
|
2016-07-26 01:00:20 +02:00
|
|
|
|
|
|
|
if ctx.PrimaryModule(mod) == mod {
|
|
|
|
type_stats[ctx.ModuleType(mod)] += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := []string{}
|
|
|
|
fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
|
|
|
|
for k := range type_stats {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
for _, mod_type := range keys {
|
|
|
|
fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
|
|
|
|
fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, type_stats[mod_type])
|
2015-07-09 03:13:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't write to the file if it hasn't changed
|
|
|
|
if _, err := os.Stat(mkFile); !os.IsNotExist(err) {
|
|
|
|
if data, err := ioutil.ReadFile(mkFile); err == nil {
|
|
|
|
matches := buf.Len() == len(data)
|
|
|
|
|
|
|
|
if matches {
|
|
|
|
for i, value := range buf.Bytes() {
|
|
|
|
if value != data[i] {
|
|
|
|
matches = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if matches {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(mkFile, buf.Bytes(), 0666)
|
|
|
|
}
|
|
|
|
|
|
|
|
func translateAndroidMkModule(ctx blueprint.SingletonContext, w io.Writer, mod blueprint.Module) error {
|
|
|
|
name := ctx.ModuleName(mod)
|
|
|
|
|
2016-02-10 02:43:51 +01:00
|
|
|
provider, ok := mod.(AndroidMkDataProvider)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
2015-07-09 03:13:11 +02:00
|
|
|
}
|
|
|
|
|
2016-05-19 00:37:25 +02:00
|
|
|
amod := mod.(Module).base()
|
2016-02-10 02:43:51 +01:00
|
|
|
data, err := provider.AndroidMk()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-07-09 03:13:11 +02:00
|
|
|
}
|
|
|
|
|
2016-02-10 02:43:51 +01:00
|
|
|
if !amod.Enabled() {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-16 03:07:15 +01:00
|
|
|
|
2016-03-24 21:14:12 +01:00
|
|
|
if data.SubName != "" {
|
2016-07-19 02:24:47 +02:00
|
|
|
name += data.SubName
|
2016-03-24 21:14:12 +01:00
|
|
|
}
|
|
|
|
|
2016-02-10 02:43:51 +01:00
|
|
|
if data.Custom != nil {
|
2015-07-09 03:13:11 +02:00
|
|
|
prefix := ""
|
2016-06-02 02:09:44 +02:00
|
|
|
switch amod.Os().Class {
|
|
|
|
case Host:
|
|
|
|
prefix = "HOST_"
|
|
|
|
case HostCross:
|
|
|
|
prefix = "HOST_CROSS_"
|
|
|
|
case Device:
|
2016-02-10 02:43:51 +01:00
|
|
|
prefix = "TARGET_"
|
2016-06-02 02:09:44 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
config := ctx.Config().(Config)
|
|
|
|
if amod.Arch().ArchType != config.Targets[amod.Os().Class][0].Arch.ArchType {
|
|
|
|
prefix = "2ND_" + prefix
|
2015-07-09 03:13:11 +02:00
|
|
|
}
|
|
|
|
|
2016-02-10 02:43:51 +01:00
|
|
|
return data.Custom(w, name, prefix)
|
|
|
|
}
|
2015-07-09 03:13:11 +02:00
|
|
|
|
2016-01-04 23:34:37 +01:00
|
|
|
if data.Disabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-10 02:43:51 +01:00
|
|
|
if !data.OutputFile.Valid() {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-09 03:13:11 +02:00
|
|
|
|
2016-02-10 02:43:51 +01:00
|
|
|
fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
|
2016-05-13 21:28:46 +02:00
|
|
|
fmt.Fprintln(w, "LOCAL_PATH :=", filepath.Dir(ctx.BlueprintFile(mod)))
|
2016-02-10 02:43:51 +01:00
|
|
|
fmt.Fprintln(w, "LOCAL_MODULE :=", name)
|
|
|
|
fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", data.Class)
|
2016-05-13 21:28:46 +02:00
|
|
|
fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", data.OutputFile.String())
|
2015-07-09 03:13:11 +02:00
|
|
|
|
2016-08-15 20:47:23 +02:00
|
|
|
if len(amod.commonProperties.Required) > 0 {
|
|
|
|
fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES := "+strings.Join(amod.commonProperties.Required, " "))
|
|
|
|
}
|
|
|
|
|
2016-02-10 02:43:51 +01:00
|
|
|
archStr := amod.Arch().ArchType.String()
|
2016-06-02 02:09:44 +02:00
|
|
|
host := false
|
|
|
|
switch amod.Os().Class {
|
|
|
|
case Host:
|
|
|
|
fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
|
|
|
|
host = true
|
|
|
|
case HostCross:
|
|
|
|
fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
|
|
|
|
host = true
|
|
|
|
case Device:
|
2016-02-10 02:43:51 +01:00
|
|
|
fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
|
2016-06-02 00:25:32 +02:00
|
|
|
|
|
|
|
if len(amod.commonProperties.Logtags) > 0 {
|
|
|
|
fmt.Fprintln(w, "LOCAL_LOGTAGS_FILES := ", strings.Join(amod.commonProperties.Logtags, " "))
|
|
|
|
}
|
2016-07-26 05:27:39 +02:00
|
|
|
if len(amod.commonProperties.Init_rc) > 0 {
|
|
|
|
fmt.Fprintln(w, "LOCAL_INIT_RC := ", strings.Join(amod.commonProperties.Init_rc, " "))
|
|
|
|
}
|
2016-02-10 02:43:51 +01:00
|
|
|
}
|
2015-07-09 03:13:11 +02:00
|
|
|
|
2016-06-02 02:09:44 +02:00
|
|
|
if host {
|
|
|
|
fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", amod.Os().String())
|
|
|
|
fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
|
|
|
|
}
|
|
|
|
|
2016-01-04 23:34:37 +01:00
|
|
|
for _, extra := range data.Extra {
|
|
|
|
err = extra(w, data.OutputFile.Path())
|
2016-02-10 02:43:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-07-09 03:13:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-10 02:43:51 +01:00
|
|
|
fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
|
|
|
|
|
|
|
|
return err
|
2015-07-09 03:13:11 +02:00
|
|
|
}
|