2015-07-09 09:13:11 +08: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 06:37:25 +08:00
|
|
|
package android
|
2015-07-09 09:13:11 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-02-10 09:43:51 +08:00
|
|
|
"fmt"
|
2015-07-09 09:13:11 +08:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
2016-06-02 06:25:32 +08:00
|
|
|
"strings"
|
2015-07-09 09:13:11 +08:00
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
2018-09-29 01:19:18 +08:00
|
|
|
"github.com/google/blueprint/bootstrap"
|
2015-07-09 09:13:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2016-10-13 05:28:16 +08:00
|
|
|
RegisterSingletonType("androidmk", AndroidMkSingleton)
|
2015-07-09 09:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type AndroidMkDataProvider interface {
|
2017-08-11 08:00:19 +08:00
|
|
|
AndroidMk() AndroidMkData
|
2016-10-07 07:12:58 +08:00
|
|
|
BaseModuleName() string
|
2015-07-09 09:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type AndroidMkData struct {
|
|
|
|
Class string
|
2016-03-25 04:14:12 +08:00
|
|
|
SubName string
|
2018-11-20 01:33:29 +08:00
|
|
|
DistFile OptionalPath
|
2015-09-24 06:26:20 +08:00
|
|
|
OutputFile OptionalPath
|
2016-01-05 06:34:37 +08:00
|
|
|
Disabled bool
|
2017-09-08 04:20:25 +08:00
|
|
|
Include string
|
2017-10-10 05:59:32 +08:00
|
|
|
Required []string
|
2015-07-09 09:13:11 +08:00
|
|
|
|
2017-08-11 08:07:28 +08:00
|
|
|
Custom func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData)
|
2015-07-09 09:13:11 +08:00
|
|
|
|
2017-08-11 07:32:23 +08:00
|
|
|
Extra []AndroidMkExtraFunc
|
2017-08-11 08:07:28 +08:00
|
|
|
|
|
|
|
preamble bytes.Buffer
|
2015-07-09 09:13:11 +08:00
|
|
|
}
|
|
|
|
|
2017-08-11 07:32:23 +08:00
|
|
|
type AndroidMkExtraFunc func(w io.Writer, outputFile Path)
|
|
|
|
|
2017-11-29 09:34:01 +08:00
|
|
|
func AndroidMkSingleton() Singleton {
|
2015-07-09 09:13:11 +08:00
|
|
|
return &androidMkSingleton{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type androidMkSingleton struct{}
|
|
|
|
|
2017-11-29 09:34:01 +08:00
|
|
|
func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
|
2017-11-29 16:27:14 +08:00
|
|
|
if !ctx.Config().EmbeddedInMake() {
|
2015-12-12 05:51:06 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-29 01:19:18 +08:00
|
|
|
var androidMkModulesList []blueprint.Module
|
2015-07-09 09:13:11 +08:00
|
|
|
|
2018-09-29 01:19:18 +08:00
|
|
|
ctx.VisitAllModulesBlueprint(func(module blueprint.Module) {
|
2017-11-29 09:34:01 +08:00
|
|
|
androidMkModulesList = append(androidMkModulesList, module)
|
2016-01-12 04:55:55 +08:00
|
|
|
})
|
2015-07-09 09:13:11 +08:00
|
|
|
|
2019-01-15 04:47:35 +08:00
|
|
|
sort.SliceStable(androidMkModulesList, func(i, j int) bool {
|
|
|
|
return ctx.ModuleName(androidMkModulesList[i]) < ctx.ModuleName(androidMkModulesList[j])
|
|
|
|
})
|
2015-12-18 10:00:23 +08:00
|
|
|
|
2018-03-10 13:22:06 +08:00
|
|
|
transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
|
2015-09-24 06:26:20 +08:00
|
|
|
if ctx.Failed() {
|
|
|
|
return
|
|
|
|
}
|
2015-07-09 09:13:11 +08:00
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList)
|
2015-07-09 09:13:11 +08:00
|
|
|
if err != nil {
|
|
|
|
ctx.Errorf(err.Error())
|
|
|
|
}
|
|
|
|
|
2017-11-29 09:34:01 +08:00
|
|
|
ctx.Build(pctx, BuildParams{
|
|
|
|
Rule: blueprint.Phony,
|
|
|
|
Output: transMk,
|
2015-07-09 09:13:11 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-29 01:19:18 +08:00
|
|
|
func translateAndroidMk(ctx SingletonContext, mkFile string, mods []blueprint.Module) error {
|
2015-07-09 09:13:11 +08:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
|
2016-02-10 09:43:51 +08:00
|
|
|
fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
|
2015-07-09 09:13:11 +08:00
|
|
|
|
2016-07-26 07:00:20 +08:00
|
|
|
type_stats := make(map[string]int)
|
2015-07-09 09:13:11 +08:00
|
|
|
for _, mod := range mods {
|
|
|
|
err := translateAndroidMkModule(ctx, buf, mod)
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(mkFile)
|
|
|
|
return err
|
|
|
|
}
|
2016-07-26 07:00:20 +08:00
|
|
|
|
2018-09-29 01:19:18 +08:00
|
|
|
if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod {
|
|
|
|
type_stats[ctx.ModuleType(amod)] += 1
|
2016-07-26 07:00:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 09:13:11 +08: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)
|
|
|
|
}
|
|
|
|
|
2017-11-29 09:34:01 +08:00
|
|
|
func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error {
|
2018-09-06 07:23:54 +08:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
|
|
|
|
r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-09-29 01:19:18 +08:00
|
|
|
switch x := mod.(type) {
|
|
|
|
case AndroidMkDataProvider:
|
|
|
|
return translateAndroidModule(ctx, w, mod, x)
|
|
|
|
case bootstrap.GoBinaryTool:
|
|
|
|
return translateGoBinaryModule(ctx, w, mod, x)
|
|
|
|
default:
|
2016-02-10 09:43:51 +08:00
|
|
|
return nil
|
2015-07-09 09:13:11 +08:00
|
|
|
}
|
2018-09-29 01:19:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
|
|
|
|
goBinary bootstrap.GoBinaryTool) error {
|
|
|
|
|
|
|
|
name := ctx.ModuleName(mod)
|
|
|
|
fmt.Fprintln(w, ".PHONY:", name)
|
|
|
|
fmt.Fprintln(w, name+":", goBinary.InstallPath())
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
|
|
|
|
provider AndroidMkDataProvider) error {
|
2015-07-09 09:13:11 +08:00
|
|
|
|
2016-10-07 07:12:58 +08:00
|
|
|
name := provider.BaseModuleName()
|
2016-05-19 06:37:25 +08:00
|
|
|
amod := mod.(Module).base()
|
2015-07-09 09:13:11 +08:00
|
|
|
|
2016-02-10 09:43:51 +08:00
|
|
|
if !amod.Enabled() {
|
2016-10-07 07:12:58 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if amod.commonProperties.SkipInstall {
|
|
|
|
return nil
|
2016-02-10 09:43:51 +08:00
|
|
|
}
|
2015-12-16 10:07:15 +08:00
|
|
|
|
2017-11-30 08:47:17 +08:00
|
|
|
if !amod.commonProperties.NamespaceExportedToMake {
|
|
|
|
// TODO(jeffrygaston) do we want to validate that there are no modules being
|
|
|
|
// exported to Kati that depend on this module?
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-11 07:59:47 +08:00
|
|
|
data := provider.AndroidMk()
|
2016-11-24 07:41:09 +08:00
|
|
|
|
2017-09-08 04:20:25 +08:00
|
|
|
if data.Include == "" {
|
|
|
|
data.Include = "$(BUILD_PREBUILT)"
|
|
|
|
}
|
|
|
|
|
2017-12-20 01:17:32 +08:00
|
|
|
data.Required = append(data.Required, amod.commonProperties.Required...)
|
2017-10-10 05:59:32 +08:00
|
|
|
|
2016-06-14 08:19:03 +08:00
|
|
|
// Make does not understand LinuxBionic
|
|
|
|
if amod.Os() == LinuxBionic {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-11 08:07:28 +08:00
|
|
|
prefix := ""
|
|
|
|
if amod.ArchSpecific() {
|
|
|
|
switch amod.Os().Class {
|
|
|
|
case Host:
|
|
|
|
prefix = "HOST_"
|
|
|
|
case HostCross:
|
|
|
|
prefix = "HOST_CROSS_"
|
|
|
|
case Device:
|
|
|
|
prefix = "TARGET_"
|
2016-03-25 04:14:12 +08:00
|
|
|
|
2015-07-09 09:13:11 +08:00
|
|
|
}
|
|
|
|
|
2018-10-11 08:02:29 +08:00
|
|
|
if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
|
2017-08-11 08:07:28 +08:00
|
|
|
prefix = "2ND_" + prefix
|
|
|
|
}
|
2016-02-10 09:43:51 +08:00
|
|
|
}
|
2015-07-09 09:13:11 +08:00
|
|
|
|
2018-11-20 01:33:29 +08:00
|
|
|
if len(amod.commonProperties.Dist.Targets) > 0 {
|
|
|
|
distFile := data.DistFile
|
|
|
|
if !distFile.Valid() {
|
|
|
|
distFile = data.OutputFile
|
|
|
|
}
|
|
|
|
if distFile.Valid() {
|
|
|
|
dest := filepath.Base(distFile.String())
|
|
|
|
|
|
|
|
if amod.commonProperties.Dist.Dest != nil {
|
|
|
|
var err error
|
|
|
|
dest, err = validateSafePath(*amod.commonProperties.Dist.Dest)
|
|
|
|
if err != nil {
|
|
|
|
// This was checked in ModuleBase.GenerateBuildActions
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if amod.commonProperties.Dist.Suffix != nil {
|
|
|
|
ext := filepath.Ext(dest)
|
|
|
|
suffix := *amod.commonProperties.Dist.Suffix
|
|
|
|
dest = strings.TrimSuffix(dest, ext) + suffix + ext
|
|
|
|
}
|
|
|
|
|
|
|
|
if amod.commonProperties.Dist.Dir != nil {
|
|
|
|
var err error
|
|
|
|
dest, err = validateSafePath(*amod.commonProperties.Dist.Dir, dest)
|
|
|
|
if err != nil {
|
|
|
|
// This was checked in ModuleBase.GenerateBuildActions
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
goals := strings.Join(amod.commonProperties.Dist.Targets, " ")
|
|
|
|
fmt.Fprintln(&data.preamble, ".PHONY:", goals)
|
|
|
|
fmt.Fprintf(&data.preamble, "$(call dist-for-goals,%s,%s:%s)\n",
|
|
|
|
goals, distFile.String(), dest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 08:07:28 +08:00
|
|
|
fmt.Fprintln(&data.preamble, "\ninclude $(CLEAR_VARS)")
|
|
|
|
fmt.Fprintln(&data.preamble, "LOCAL_PATH :=", filepath.Dir(ctx.BlueprintFile(mod)))
|
|
|
|
fmt.Fprintln(&data.preamble, "LOCAL_MODULE :=", name+data.SubName)
|
|
|
|
fmt.Fprintln(&data.preamble, "LOCAL_MODULE_CLASS :=", data.Class)
|
|
|
|
fmt.Fprintln(&data.preamble, "LOCAL_PREBUILT_MODULE_FILE :=", data.OutputFile.String())
|
2015-07-09 09:13:11 +08:00
|
|
|
|
2017-10-10 05:59:32 +08:00
|
|
|
if len(data.Required) > 0 {
|
|
|
|
fmt.Fprintln(&data.preamble, "LOCAL_REQUIRED_MODULES := "+strings.Join(data.Required, " "))
|
2016-08-16 02:47:23 +08:00
|
|
|
}
|
|
|
|
|
2016-02-10 09:43:51 +08:00
|
|
|
archStr := amod.Arch().ArchType.String()
|
2016-06-02 08:09:44 +08:00
|
|
|
host := false
|
|
|
|
switch amod.Os().Class {
|
|
|
|
case Host:
|
2017-02-28 02:12:13 +08:00
|
|
|
// Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
|
|
|
|
if archStr != "common" {
|
2017-08-11 08:07:28 +08:00
|
|
|
fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_ARCH :=", archStr)
|
2017-02-28 02:12:13 +08:00
|
|
|
}
|
2016-06-02 08:09:44 +08:00
|
|
|
host = true
|
|
|
|
case HostCross:
|
2017-02-28 02:12:13 +08:00
|
|
|
// Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
|
|
|
|
if archStr != "common" {
|
2017-08-11 08:07:28 +08:00
|
|
|
fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
|
2017-02-28 02:12:13 +08:00
|
|
|
}
|
2016-06-02 08:09:44 +08:00
|
|
|
host = true
|
|
|
|
case Device:
|
2017-02-28 02:12:13 +08:00
|
|
|
// Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
|
|
|
|
if archStr != "common" {
|
2017-08-11 08:07:28 +08:00
|
|
|
fmt.Fprintln(&data.preamble, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
|
2017-02-28 02:12:13 +08:00
|
|
|
}
|
2016-06-02 06:25:32 +08:00
|
|
|
|
2016-07-26 11:27:39 +08:00
|
|
|
if len(amod.commonProperties.Init_rc) > 0 {
|
2017-08-11 08:07:28 +08:00
|
|
|
fmt.Fprintln(&data.preamble, "LOCAL_INIT_RC := ", strings.Join(amod.commonProperties.Init_rc, " "))
|
2016-07-26 11:27:39 +08:00
|
|
|
}
|
2018-04-05 06:42:19 +08:00
|
|
|
if len(amod.commonProperties.Vintf_fragments) > 0 {
|
|
|
|
fmt.Fprintln(&data.preamble, "LOCAL_VINTF_FRAGMENTS := ", strings.Join(amod.commonProperties.Vintf_fragments, " "))
|
|
|
|
}
|
2017-11-02 01:38:29 +08:00
|
|
|
if Bool(amod.commonProperties.Proprietary) {
|
2017-08-11 08:07:28 +08:00
|
|
|
fmt.Fprintln(&data.preamble, "LOCAL_PROPRIETARY_MODULE := true")
|
2016-12-06 08:47:50 +08:00
|
|
|
}
|
2017-11-08 15:03:48 +08:00
|
|
|
if Bool(amod.commonProperties.Vendor) || Bool(amod.commonProperties.Soc_specific) {
|
2017-08-11 08:07:28 +08:00
|
|
|
fmt.Fprintln(&data.preamble, "LOCAL_VENDOR_MODULE := true")
|
2017-11-08 15:03:48 +08:00
|
|
|
}
|
|
|
|
if Bool(amod.commonProperties.Device_specific) {
|
|
|
|
fmt.Fprintln(&data.preamble, "LOCAL_ODM_MODULE := true")
|
|
|
|
}
|
|
|
|
if Bool(amod.commonProperties.Product_specific) {
|
2018-01-10 18:00:15 +08:00
|
|
|
fmt.Fprintln(&data.preamble, "LOCAL_PRODUCT_MODULE := true")
|
2018-05-29 20:28:54 +08:00
|
|
|
}
|
2018-08-17 07:57:57 +08:00
|
|
|
if Bool(amod.commonProperties.Product_services_specific) {
|
2018-05-29 20:28:54 +08:00
|
|
|
fmt.Fprintln(&data.preamble, "LOCAL_PRODUCT_SERVICES_MODULE := true")
|
2017-04-07 03:49:58 +08:00
|
|
|
}
|
2017-07-19 10:42:09 +08:00
|
|
|
if amod.commonProperties.Owner != nil {
|
2017-08-11 08:07:28 +08:00
|
|
|
fmt.Fprintln(&data.preamble, "LOCAL_MODULE_OWNER :=", *amod.commonProperties.Owner)
|
2017-03-21 04:23:34 +08:00
|
|
|
}
|
2018-11-16 09:19:56 +08:00
|
|
|
}
|
|
|
|
|
2018-11-17 05:26:43 +08:00
|
|
|
if amod.noticeFile != nil {
|
|
|
|
fmt.Fprintln(&data.preamble, "LOCAL_NOTICE_FILE :=", amod.noticeFile.String())
|
2016-02-10 09:43:51 +08:00
|
|
|
}
|
2015-07-09 09:13:11 +08:00
|
|
|
|
2016-06-02 08:09:44 +08:00
|
|
|
if host {
|
2017-09-23 03:28:24 +08:00
|
|
|
makeOs := amod.Os().String()
|
|
|
|
if amod.Os() == Linux || amod.Os() == LinuxBionic {
|
|
|
|
makeOs = "linux"
|
|
|
|
}
|
|
|
|
fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_OS :=", makeOs)
|
2017-08-11 08:07:28 +08:00
|
|
|
fmt.Fprintln(&data.preamble, "LOCAL_IS_HOST_MODULE := true")
|
2016-06-02 08:09:44 +08:00
|
|
|
}
|
|
|
|
|
2017-08-11 08:07:28 +08:00
|
|
|
blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
|
|
|
|
|
|
|
|
if data.Custom != nil {
|
|
|
|
data.Custom(w, name, prefix, blueprintDir, data)
|
|
|
|
} else {
|
|
|
|
WriteAndroidMkData(w, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
|
|
|
|
if data.Disabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !data.OutputFile.Valid() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(data.preamble.Bytes())
|
|
|
|
|
2016-01-05 06:34:37 +08:00
|
|
|
for _, extra := range data.Extra {
|
2017-08-11 07:32:23 +08:00
|
|
|
extra(w, data.OutputFile.Path())
|
2015-07-09 09:13:11 +08:00
|
|
|
}
|
|
|
|
|
2017-09-08 04:20:25 +08:00
|
|
|
fmt.Fprintln(w, "include "+data.Include)
|
2015-07-09 09:13:11 +08:00
|
|
|
}
|