blob: 232a599b799150012c06faf4bcaba9fcd58c69bc [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
27 "android/soong"
28
29 "github.com/google/blueprint"
Dan Willemsen174978c2016-05-11 00:27:49 -070030 "github.com/google/blueprint/proptools"
Dan Willemsen218f6562015-07-08 18:13:11 -070031)
32
33func init() {
34 soong.RegisterSingletonType("androidmk", AndroidMkSingleton)
35}
36
37type AndroidMkDataProvider interface {
Dan Willemsen97750522016-02-09 17:43:51 -080038 AndroidMk() (AndroidMkData, error)
Dan Willemsen218f6562015-07-08 18:13:11 -070039}
40
41type AndroidMkData struct {
42 Class string
Colin Crossa2344662016-03-24 13:14:12 -070043 SubName string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070044 OutputFile OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -080045 Disabled bool
Dan Willemsen218f6562015-07-08 18:13:11 -070046
Dan Willemsen97750522016-02-09 17:43:51 -080047 Custom func(w io.Writer, name, prefix string) error
Dan Willemsen218f6562015-07-08 18:13:11 -070048
Colin Crossca860ac2016-01-04 14:34:37 -080049 Extra []func(w io.Writer, outputFile Path) error
Dan Willemsen218f6562015-07-08 18:13:11 -070050}
51
52func AndroidMkSingleton() blueprint.Singleton {
53 return &androidMkSingleton{}
54}
55
56type androidMkSingleton struct{}
57
58func (c *androidMkSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
Dan Willemsen174978c2016-05-11 00:27:49 -070059 config := ctx.Config().(Config)
60
61 if !config.EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -080062 return
63 }
64
Dan Willemsen174978c2016-05-11 00:27:49 -070065 ctx.SetNinjaBuildDir(pctx, filepath.Join(config.buildDir, ".."))
Dan Willemsen218f6562015-07-08 18:13:11 -070066
Colin Cross635c3b02016-05-18 15:37:25 -070067 var androidMkModulesList []Module
Colin Cross4f6e4e62016-01-11 12:55:55 -080068
Dan Willemsen218f6562015-07-08 18:13:11 -070069 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -070070 if amod, ok := module.(Module); ok {
Dan Willemsen218f6562015-07-08 18:13:11 -070071 androidMkModulesList = append(androidMkModulesList, amod)
72 }
Colin Cross4f6e4e62016-01-11 12:55:55 -080073 })
Dan Willemsen218f6562015-07-08 18:13:11 -070074
Colin Crossd779da42015-12-17 18:00:23 -080075 sort.Sort(AndroidModulesByName{androidMkModulesList, ctx})
76
Dan Willemsen174978c2016-05-11 00:27:49 -070077 transMk := PathForOutput(ctx, "Android"+proptools.String(config.ProductVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -070078 if ctx.Failed() {
79 return
80 }
Dan Willemsen218f6562015-07-08 18:13:11 -070081
Dan Willemsen34cc69e2015-09-23 15:26:20 -070082 err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -070083 if err != nil {
84 ctx.Errorf(err.Error())
85 }
86
87 ctx.Build(pctx, blueprint.BuildParams{
88 Rule: blueprint.Phony,
Dan Willemsen34cc69e2015-09-23 15:26:20 -070089 Outputs: []string{transMk.String()},
Dan Willemsen218f6562015-07-08 18:13:11 -070090 Optional: true,
91 })
92}
93
Colin Cross635c3b02016-05-18 15:37:25 -070094func translateAndroidMk(ctx blueprint.SingletonContext, mkFile string, mods []Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -070095 buf := &bytes.Buffer{}
96
Dan Willemsen97750522016-02-09 17:43:51 -080097 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -070098
Dan Willemsen70e17fa2016-07-25 16:00:20 -070099 type_stats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -0700100 for _, mod := range mods {
101 err := translateAndroidMkModule(ctx, buf, mod)
102 if err != nil {
103 os.Remove(mkFile)
104 return err
105 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700106
107 if ctx.PrimaryModule(mod) == mod {
108 type_stats[ctx.ModuleType(mod)] += 1
109 }
110 }
111
112 keys := []string{}
113 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
114 for k := range type_stats {
115 keys = append(keys, k)
116 }
117 sort.Strings(keys)
118 for _, mod_type := range keys {
119 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
120 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, type_stats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700121 }
122
123 // Don't write to the file if it hasn't changed
124 if _, err := os.Stat(mkFile); !os.IsNotExist(err) {
125 if data, err := ioutil.ReadFile(mkFile); err == nil {
126 matches := buf.Len() == len(data)
127
128 if matches {
129 for i, value := range buf.Bytes() {
130 if value != data[i] {
131 matches = false
132 break
133 }
134 }
135 }
136
137 if matches {
138 return nil
139 }
140 }
141 }
142
143 return ioutil.WriteFile(mkFile, buf.Bytes(), 0666)
144}
145
146func translateAndroidMkModule(ctx blueprint.SingletonContext, w io.Writer, mod blueprint.Module) error {
Dan Willemsen97750522016-02-09 17:43:51 -0800147 name := ctx.ModuleName(mod)
148
149 provider, ok := mod.(AndroidMkDataProvider)
150 if !ok {
Dan Willemsen218f6562015-07-08 18:13:11 -0700151 return nil
152 }
153
Colin Cross635c3b02016-05-18 15:37:25 -0700154 amod := mod.(Module).base()
Dan Willemsen97750522016-02-09 17:43:51 -0800155 data, err := provider.AndroidMk()
156 if err != nil {
157 return err
Dan Willemsen218f6562015-07-08 18:13:11 -0700158 }
159
Dan Willemsen97750522016-02-09 17:43:51 -0800160 if !amod.Enabled() {
161 return err
Dan Willemsen218f6562015-07-08 18:13:11 -0700162 }
163
Colin Crossa2344662016-03-24 13:14:12 -0700164 if data.SubName != "" {
Dan Albert6a047692016-07-18 17:24:47 -0700165 name += data.SubName
Colin Crossa2344662016-03-24 13:14:12 -0700166 }
167
Dan Willemsen97750522016-02-09 17:43:51 -0800168 if data.Custom != nil {
Dan Willemsen218f6562015-07-08 18:13:11 -0700169 prefix := ""
Colin Crossa1ad8d12016-06-01 17:09:44 -0700170 switch amod.Os().Class {
171 case Host:
172 prefix = "HOST_"
173 case HostCross:
174 prefix = "HOST_CROSS_"
175 case Device:
Dan Willemsen97750522016-02-09 17:43:51 -0800176 prefix = "TARGET_"
Colin Crossa1ad8d12016-06-01 17:09:44 -0700177
178 }
179
180 config := ctx.Config().(Config)
181 if amod.Arch().ArchType != config.Targets[amod.Os().Class][0].Arch.ArchType {
182 prefix = "2ND_" + prefix
Dan Willemsen218f6562015-07-08 18:13:11 -0700183 }
184
Dan Willemsen97750522016-02-09 17:43:51 -0800185 return data.Custom(w, name, prefix)
Dan Willemsen218f6562015-07-08 18:13:11 -0700186 }
187
Colin Crossca860ac2016-01-04 14:34:37 -0800188 if data.Disabled {
189 return nil
190 }
191
Dan Willemsen97750522016-02-09 17:43:51 -0800192 if !data.OutputFile.Valid() {
193 return err
194 }
195
196 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
Colin Cross26302132016-05-13 12:28:46 -0700197 fmt.Fprintln(w, "LOCAL_PATH :=", filepath.Dir(ctx.BlueprintFile(mod)))
Dan Willemsen97750522016-02-09 17:43:51 -0800198 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
199 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", data.Class)
200 fmt.Fprintln(w, "LOCAL_MULTILIB :=", amod.commonProperties.Compile_multilib)
Colin Cross26302132016-05-13 12:28:46 -0700201 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", data.OutputFile.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800202
203 archStr := amod.Arch().ArchType.String()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700204 host := false
205 switch amod.Os().Class {
206 case Host:
207 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
208 host = true
209 case HostCross:
210 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
211 host = true
212 case Device:
Dan Willemsen97750522016-02-09 17:43:51 -0800213 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700214
215 if len(amod.commonProperties.Logtags) > 0 {
216 fmt.Fprintln(w, "LOCAL_LOGTAGS_FILES := ", strings.Join(amod.commonProperties.Logtags, " "))
217 }
Dan Willemsen97750522016-02-09 17:43:51 -0800218 }
219
Colin Crossa1ad8d12016-06-01 17:09:44 -0700220 if host {
221 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", amod.Os().String())
222 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
223 }
224
Colin Crossca860ac2016-01-04 14:34:37 -0800225 for _, extra := range data.Extra {
226 err = extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800227 if err != nil {
228 return err
229 }
230 }
231
232 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
233
234 return err
Dan Willemsen218f6562015-07-08 18:13:11 -0700235}