blob: 86efb042e7adaed5172967f11c3504f131aa57aa [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
15package common
16
17import (
18 "bytes"
19 "io"
20 "io/ioutil"
21 "os"
22 "path/filepath"
23 "sort"
24
25 "android/soong"
26
27 "github.com/google/blueprint"
28)
29
30func init() {
31 soong.RegisterSingletonType("androidmk", AndroidMkSingleton)
32}
33
34type AndroidMkDataProvider interface {
35 AndroidMk() AndroidMkData
36}
37
38type AndroidMkData struct {
39 Class string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070040 OutputFile OptionalPath
Dan Willemsen218f6562015-07-08 18:13:11 -070041
42 Custom func(w io.Writer, name, prefix string)
43
Dan Willemsen34cc69e2015-09-23 15:26:20 -070044 Extra func(name, prefix string, outputFile Path, arch Arch) []string
Dan Willemsen218f6562015-07-08 18:13:11 -070045}
46
47func AndroidMkSingleton() blueprint.Singleton {
48 return &androidMkSingleton{}
49}
50
51type androidMkSingleton struct{}
52
53func (c *androidMkSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
Dan Willemsen5ba07e82015-12-11 13:51:06 -080054 if !ctx.Config().(Config).EmbeddedInMake() {
55 return
56 }
57
Dan Willemsen34cc69e2015-09-23 15:26:20 -070058 ctx.SetNinjaBuildDir(pctx, filepath.Join(ctx.Config().(Config).buildDir, ".."))
Dan Willemsen218f6562015-07-08 18:13:11 -070059
Colin Cross4f6e4e62016-01-11 12:55:55 -080060 var androidMkModulesList []AndroidModule
61
Dan Willemsen218f6562015-07-08 18:13:11 -070062 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross4f6e4e62016-01-11 12:55:55 -080063 if amod, ok := module.(AndroidModule); ok {
Dan Willemsen218f6562015-07-08 18:13:11 -070064 androidMkModulesList = append(androidMkModulesList, amod)
65 }
Colin Cross4f6e4e62016-01-11 12:55:55 -080066 })
Dan Willemsen218f6562015-07-08 18:13:11 -070067
Colin Crossd779da42015-12-17 18:00:23 -080068 sort.Sort(AndroidModulesByName{androidMkModulesList, ctx})
69
Dan Willemsen34cc69e2015-09-23 15:26:20 -070070 transMk := PathForOutput(ctx, "Android.mk")
71 if ctx.Failed() {
72 return
73 }
Dan Willemsen218f6562015-07-08 18:13:11 -070074
Dan Willemsen34cc69e2015-09-23 15:26:20 -070075 err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -070076 if err != nil {
77 ctx.Errorf(err.Error())
78 }
79
80 ctx.Build(pctx, blueprint.BuildParams{
81 Rule: blueprint.Phony,
Dan Willemsen34cc69e2015-09-23 15:26:20 -070082 Outputs: []string{transMk.String()},
Dan Willemsen218f6562015-07-08 18:13:11 -070083 Optional: true,
84 })
85}
86
87func translateAndroidMk(ctx blueprint.SingletonContext, mkFile string, mods []AndroidModule) error {
88 buf := &bytes.Buffer{}
89
90 io.WriteString(buf, "LOCAL_PATH := $(TOP)\n")
91 io.WriteString(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))\n")
92
93 for _, mod := range mods {
94 err := translateAndroidMkModule(ctx, buf, mod)
95 if err != nil {
96 os.Remove(mkFile)
97 return err
98 }
99 }
100
101 // Don't write to the file if it hasn't changed
102 if _, err := os.Stat(mkFile); !os.IsNotExist(err) {
103 if data, err := ioutil.ReadFile(mkFile); err == nil {
104 matches := buf.Len() == len(data)
105
106 if matches {
107 for i, value := range buf.Bytes() {
108 if value != data[i] {
109 matches = false
110 break
111 }
112 }
113 }
114
115 if matches {
116 return nil
117 }
118 }
119 }
120
121 return ioutil.WriteFile(mkFile, buf.Bytes(), 0666)
122}
123
124func translateAndroidMkModule(ctx blueprint.SingletonContext, w io.Writer, mod blueprint.Module) error {
125 if mod != ctx.PrimaryModule(mod) {
126 // These will be handled by the primary module
127 return nil
128 }
129
130 name := ctx.ModuleName(mod)
131
132 type hostClass struct {
133 host bool
134 class string
135 multilib string
136 }
137
138 type archSrc struct {
139 arch Arch
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700140 src Path
Dan Willemsen218f6562015-07-08 18:13:11 -0700141 extra []string
142 }
143
144 srcs := make(map[hostClass][]archSrc)
145 var modules []hostClass
146
147 ctx.VisitAllModuleVariants(mod, func(m blueprint.Module) {
148 provider, ok := m.(AndroidMkDataProvider)
149 if !ok {
150 return
151 }
152
153 amod := m.(AndroidModule).base()
154 data := provider.AndroidMk()
155
Colin Cross1ef47562015-12-15 18:07:15 -0800156 if !amod.Enabled() {
157 return
158 }
159
Dan Willemsen218f6562015-07-08 18:13:11 -0700160 arch := amod.commonProperties.CompileArch
161
162 prefix := ""
163 if amod.HostOrDevice() == Host {
164 if arch.ArchType != ctx.Config().(Config).HostArches[amod.HostType()][0].ArchType {
165 prefix = "2ND_"
166 }
167 } else {
168 if arch.ArchType != ctx.Config().(Config).DeviceArches[0].ArchType {
169 prefix = "2ND_"
170 }
171 }
172
173 if data.Custom != nil {
174 data.Custom(w, name, prefix)
175 return
176 }
177
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700178 if !data.OutputFile.Valid() {
179 return
180 }
181
Dan Willemsen218f6562015-07-08 18:13:11 -0700182 hC := hostClass{
183 host: amod.HostOrDevice() == Host,
184 class: data.Class,
185 multilib: amod.commonProperties.Compile_multilib,
186 }
187
188 src := archSrc{
189 arch: arch,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700190 src: data.OutputFile.Path(),
Dan Willemsen218f6562015-07-08 18:13:11 -0700191 }
192
193 if data.Extra != nil {
194 src.extra = data.Extra(name, prefix, src.src, arch)
195 }
196
197 if srcs[hC] == nil {
198 modules = append(modules, hC)
199 }
200 srcs[hC] = append(srcs[hC], src)
201 })
202
203 for _, hC := range modules {
204 archSrcs := srcs[hC]
205
206 io.WriteString(w, "\ninclude $(CLEAR_VARS)\n")
207 io.WriteString(w, "LOCAL_MODULE := "+name+"\n")
208 io.WriteString(w, "LOCAL_MODULE_CLASS := "+hC.class+"\n")
209 io.WriteString(w, "LOCAL_MULTILIB := "+hC.multilib+"\n")
210
211 printed := make(map[string]bool)
212 for _, src := range archSrcs {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700213 io.WriteString(w, "LOCAL_SRC_FILES_"+src.arch.ArchType.String()+" := "+src.src.String()+"\n")
Dan Willemsen218f6562015-07-08 18:13:11 -0700214
215 for _, extra := range src.extra {
216 if !printed[extra] {
217 printed[extra] = true
218 io.WriteString(w, extra+"\n")
219 }
220 }
221 }
222
223 if hC.host {
224 // TODO: this isn't true for every module
225 io.WriteString(w, "LOCAL_ACP_UNAVAILABLE := true\n")
226
227 io.WriteString(w, "LOCAL_IS_HOST_MODULE := true\n")
228 }
229 io.WriteString(w, "include $(BUILD_PREBUILT)\n")
230 }
231
232 return nil
233}