blob: 28c229039708dff3f3e07b0357a3854d1c5ba12c [file] [log] [blame]
Dan Willemsen218f6562015-07-08 18:13:11 -07001// 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
Dan Willemsen218f6562015-07-08 18:13:11 -070016
17import (
18 "bytes"
Dan Willemsen97750522016-02-09 17:43:51 -080019 "fmt"
Dan Willemsen218f6562015-07-08 18:13:11 -070020 "io"
21 "io/ioutil"
22 "os"
23 "path/filepath"
24 "sort"
Dan Willemsen0fda89f2016-06-01 15:25:32 -070025 "strings"
Dan Willemsen218f6562015-07-08 18:13:11 -070026
Dan Willemsen218f6562015-07-08 18:13:11 -070027 "github.com/google/blueprint"
Dan Willemsen174978c2016-05-11 00:27:49 -070028 "github.com/google/blueprint/proptools"
Dan Willemsen218f6562015-07-08 18:13:11 -070029)
30
31func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070032 RegisterSingletonType("androidmk", AndroidMkSingleton)
Dan Willemsen218f6562015-07-08 18:13:11 -070033}
34
35type AndroidMkDataProvider interface {
Dan Willemsen97750522016-02-09 17:43:51 -080036 AndroidMk() (AndroidMkData, error)
Dan Willemsen218f6562015-07-08 18:13:11 -070037}
38
39type AndroidMkData struct {
40 Class string
Colin Crossa2344662016-03-24 13:14:12 -070041 SubName string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070042 OutputFile OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -080043 Disabled bool
Dan Willemsen218f6562015-07-08 18:13:11 -070044
Dan Willemsen97750522016-02-09 17:43:51 -080045 Custom func(w io.Writer, name, prefix string) error
Dan Willemsen218f6562015-07-08 18:13:11 -070046
Colin Crossca860ac2016-01-04 14:34:37 -080047 Extra []func(w io.Writer, outputFile Path) error
Dan Willemsen218f6562015-07-08 18:13:11 -070048}
49
50func AndroidMkSingleton() blueprint.Singleton {
51 return &androidMkSingleton{}
52}
53
54type androidMkSingleton struct{}
55
56func (c *androidMkSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
Dan Willemsen174978c2016-05-11 00:27:49 -070057 config := ctx.Config().(Config)
58
59 if !config.EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -080060 return
61 }
62
Dan Willemsen174978c2016-05-11 00:27:49 -070063 ctx.SetNinjaBuildDir(pctx, filepath.Join(config.buildDir, ".."))
Dan Willemsen218f6562015-07-08 18:13:11 -070064
Colin Cross635c3b02016-05-18 15:37:25 -070065 var androidMkModulesList []Module
Colin Cross4f6e4e62016-01-11 12:55:55 -080066
Dan Willemsen218f6562015-07-08 18:13:11 -070067 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -070068 if amod, ok := module.(Module); ok {
Dan Willemsen218f6562015-07-08 18:13:11 -070069 androidMkModulesList = append(androidMkModulesList, amod)
70 }
Colin Cross4f6e4e62016-01-11 12:55:55 -080071 })
Dan Willemsen218f6562015-07-08 18:13:11 -070072
Colin Crossd779da42015-12-17 18:00:23 -080073 sort.Sort(AndroidModulesByName{androidMkModulesList, ctx})
74
Dan Willemsen174978c2016-05-11 00:27:49 -070075 transMk := PathForOutput(ctx, "Android"+proptools.String(config.ProductVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -070076 if ctx.Failed() {
77 return
78 }
Dan Willemsen218f6562015-07-08 18:13:11 -070079
Dan Willemsen34cc69e2015-09-23 15:26:20 -070080 err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -070081 if err != nil {
82 ctx.Errorf(err.Error())
83 }
84
85 ctx.Build(pctx, blueprint.BuildParams{
86 Rule: blueprint.Phony,
Dan Willemsen34cc69e2015-09-23 15:26:20 -070087 Outputs: []string{transMk.String()},
Dan Willemsen218f6562015-07-08 18:13:11 -070088 Optional: true,
89 })
90}
91
Colin Cross635c3b02016-05-18 15:37:25 -070092func translateAndroidMk(ctx blueprint.SingletonContext, mkFile string, mods []Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -070093 buf := &bytes.Buffer{}
94
Dan Willemsen97750522016-02-09 17:43:51 -080095 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -070096
Dan Willemsen70e17fa2016-07-25 16:00:20 -070097 type_stats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -070098 for _, mod := range mods {
99 err := translateAndroidMkModule(ctx, buf, mod)
100 if err != nil {
101 os.Remove(mkFile)
102 return err
103 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700104
105 if ctx.PrimaryModule(mod) == mod {
106 type_stats[ctx.ModuleType(mod)] += 1
107 }
108 }
109
110 keys := []string{}
111 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
112 for k := range type_stats {
113 keys = append(keys, k)
114 }
115 sort.Strings(keys)
116 for _, mod_type := range keys {
117 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
118 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, type_stats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700119 }
120
121 // Don't write to the file if it hasn't changed
122 if _, err := os.Stat(mkFile); !os.IsNotExist(err) {
123 if data, err := ioutil.ReadFile(mkFile); err == nil {
124 matches := buf.Len() == len(data)
125
126 if matches {
127 for i, value := range buf.Bytes() {
128 if value != data[i] {
129 matches = false
130 break
131 }
132 }
133 }
134
135 if matches {
136 return nil
137 }
138 }
139 }
140
141 return ioutil.WriteFile(mkFile, buf.Bytes(), 0666)
142}
143
144func translateAndroidMkModule(ctx blueprint.SingletonContext, w io.Writer, mod blueprint.Module) error {
Dan Willemsen97750522016-02-09 17:43:51 -0800145 name := ctx.ModuleName(mod)
146
147 provider, ok := mod.(AndroidMkDataProvider)
148 if !ok {
Dan Willemsen218f6562015-07-08 18:13:11 -0700149 return nil
150 }
151
Colin Cross635c3b02016-05-18 15:37:25 -0700152 amod := mod.(Module).base()
Dan Willemsen97750522016-02-09 17:43:51 -0800153 data, err := provider.AndroidMk()
154 if err != nil {
155 return err
Dan Willemsen218f6562015-07-08 18:13:11 -0700156 }
157
Dan Willemsen97750522016-02-09 17:43:51 -0800158 if !amod.Enabled() {
159 return err
Dan Willemsen218f6562015-07-08 18:13:11 -0700160 }
161
Colin Crossa2344662016-03-24 13:14:12 -0700162 if data.SubName != "" {
Dan Albert6a047692016-07-18 17:24:47 -0700163 name += data.SubName
Colin Crossa2344662016-03-24 13:14:12 -0700164 }
165
Dan Willemsen97750522016-02-09 17:43:51 -0800166 if data.Custom != nil {
Dan Willemsen218f6562015-07-08 18:13:11 -0700167 prefix := ""
Colin Crossa1ad8d12016-06-01 17:09:44 -0700168 switch amod.Os().Class {
169 case Host:
170 prefix = "HOST_"
171 case HostCross:
172 prefix = "HOST_CROSS_"
173 case Device:
Dan Willemsen97750522016-02-09 17:43:51 -0800174 prefix = "TARGET_"
Colin Crossa1ad8d12016-06-01 17:09:44 -0700175
176 }
177
178 config := ctx.Config().(Config)
179 if amod.Arch().ArchType != config.Targets[amod.Os().Class][0].Arch.ArchType {
180 prefix = "2ND_" + prefix
Dan Willemsen218f6562015-07-08 18:13:11 -0700181 }
182
Dan Willemsen97750522016-02-09 17:43:51 -0800183 return data.Custom(w, name, prefix)
Dan Willemsen218f6562015-07-08 18:13:11 -0700184 }
185
Colin Crossca860ac2016-01-04 14:34:37 -0800186 if data.Disabled {
187 return nil
188 }
189
Dan Willemsen97750522016-02-09 17:43:51 -0800190 if !data.OutputFile.Valid() {
191 return err
192 }
193
194 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
Colin Cross26302132016-05-13 12:28:46 -0700195 fmt.Fprintln(w, "LOCAL_PATH :=", filepath.Dir(ctx.BlueprintFile(mod)))
Dan Willemsen97750522016-02-09 17:43:51 -0800196 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
197 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", data.Class)
Colin Cross26302132016-05-13 12:28:46 -0700198 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", data.OutputFile.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800199
Chris Wolfe998306e2016-08-15 14:47:23 -0400200 if len(amod.commonProperties.Required) > 0 {
201 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES := "+strings.Join(amod.commonProperties.Required, " "))
202 }
203
Dan Willemsen97750522016-02-09 17:43:51 -0800204 archStr := amod.Arch().ArchType.String()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700205 host := false
206 switch amod.Os().Class {
207 case Host:
208 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
209 host = true
210 case HostCross:
211 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
212 host = true
213 case Device:
Dan Willemsen97750522016-02-09 17:43:51 -0800214 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700215
216 if len(amod.commonProperties.Logtags) > 0 {
217 fmt.Fprintln(w, "LOCAL_LOGTAGS_FILES := ", strings.Join(amod.commonProperties.Logtags, " "))
218 }
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700219 if len(amod.commonProperties.Init_rc) > 0 {
220 fmt.Fprintln(w, "LOCAL_INIT_RC := ", strings.Join(amod.commonProperties.Init_rc, " "))
221 }
Dan Willemsen97750522016-02-09 17:43:51 -0800222 }
223
Colin Crossa1ad8d12016-06-01 17:09:44 -0700224 if host {
225 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", amod.Os().String())
226 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
227 }
228
Colin Crossca860ac2016-01-04 14:34:37 -0800229 for _, extra := range data.Extra {
230 err = extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800231 if err != nil {
232 return err
233 }
234 }
235
236 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
237
238 return err
Dan Willemsen218f6562015-07-08 18:13:11 -0700239}