blob: 6a2ede0698164ad76722f00653ce7e00f06fb305 [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"
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"
25
26 "android/soong"
27
28 "github.com/google/blueprint"
Dan Willemsen174978c2016-05-11 00:27:49 -070029 "github.com/google/blueprint/proptools"
Dan Willemsen218f6562015-07-08 18:13:11 -070030)
31
32func init() {
33 soong.RegisterSingletonType("androidmk", AndroidMkSingleton)
34}
35
36type AndroidMkDataProvider interface {
Dan Willemsen97750522016-02-09 17:43:51 -080037 AndroidMk() (AndroidMkData, error)
Dan Willemsen218f6562015-07-08 18:13:11 -070038}
39
40type AndroidMkData struct {
41 Class string
Colin Crossa2344662016-03-24 13:14:12 -070042 SubName string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070043 OutputFile OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -080044 Disabled bool
Dan Willemsen218f6562015-07-08 18:13:11 -070045
Dan Willemsen97750522016-02-09 17:43:51 -080046 Custom func(w io.Writer, name, prefix string) error
Dan Willemsen218f6562015-07-08 18:13:11 -070047
Colin Crossca860ac2016-01-04 14:34:37 -080048 Extra []func(w io.Writer, outputFile Path) error
Dan Willemsen218f6562015-07-08 18:13:11 -070049}
50
51func AndroidMkSingleton() blueprint.Singleton {
52 return &androidMkSingleton{}
53}
54
55type androidMkSingleton struct{}
56
57func (c *androidMkSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
Dan Willemsen174978c2016-05-11 00:27:49 -070058 config := ctx.Config().(Config)
59
60 if !config.EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -080061 return
62 }
63
Dan Willemsen174978c2016-05-11 00:27:49 -070064 ctx.SetNinjaBuildDir(pctx, filepath.Join(config.buildDir, ".."))
Dan Willemsen218f6562015-07-08 18:13:11 -070065
Colin Cross4f6e4e62016-01-11 12:55:55 -080066 var androidMkModulesList []AndroidModule
67
Dan Willemsen218f6562015-07-08 18:13:11 -070068 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross4f6e4e62016-01-11 12:55:55 -080069 if amod, ok := module.(AndroidModule); ok {
Dan Willemsen218f6562015-07-08 18:13:11 -070070 androidMkModulesList = append(androidMkModulesList, amod)
71 }
Colin Cross4f6e4e62016-01-11 12:55:55 -080072 })
Dan Willemsen218f6562015-07-08 18:13:11 -070073
Colin Crossd779da42015-12-17 18:00:23 -080074 sort.Sort(AndroidModulesByName{androidMkModulesList, ctx})
75
Dan Willemsen174978c2016-05-11 00:27:49 -070076 transMk := PathForOutput(ctx, "Android"+proptools.String(config.ProductVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -070077 if ctx.Failed() {
78 return
79 }
Dan Willemsen218f6562015-07-08 18:13:11 -070080
Dan Willemsen34cc69e2015-09-23 15:26:20 -070081 err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -070082 if err != nil {
83 ctx.Errorf(err.Error())
84 }
85
86 ctx.Build(pctx, blueprint.BuildParams{
87 Rule: blueprint.Phony,
Dan Willemsen34cc69e2015-09-23 15:26:20 -070088 Outputs: []string{transMk.String()},
Dan Willemsen218f6562015-07-08 18:13:11 -070089 Optional: true,
90 })
91}
92
93func translateAndroidMk(ctx blueprint.SingletonContext, mkFile string, mods []AndroidModule) error {
94 buf := &bytes.Buffer{}
95
Dan Willemsen97750522016-02-09 17:43:51 -080096 fmt.Fprintln(buf, "LOCAL_PATH := $(TOP)")
97 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -070098
99 for _, mod := range mods {
100 err := translateAndroidMkModule(ctx, buf, mod)
101 if err != nil {
102 os.Remove(mkFile)
103 return err
104 }
105 }
106
107 // Don't write to the file if it hasn't changed
108 if _, err := os.Stat(mkFile); !os.IsNotExist(err) {
109 if data, err := ioutil.ReadFile(mkFile); err == nil {
110 matches := buf.Len() == len(data)
111
112 if matches {
113 for i, value := range buf.Bytes() {
114 if value != data[i] {
115 matches = false
116 break
117 }
118 }
119 }
120
121 if matches {
122 return nil
123 }
124 }
125 }
126
127 return ioutil.WriteFile(mkFile, buf.Bytes(), 0666)
128}
129
130func translateAndroidMkModule(ctx blueprint.SingletonContext, w io.Writer, mod blueprint.Module) error {
Dan Willemsen97750522016-02-09 17:43:51 -0800131 name := ctx.ModuleName(mod)
132
133 provider, ok := mod.(AndroidMkDataProvider)
134 if !ok {
Dan Willemsen218f6562015-07-08 18:13:11 -0700135 return nil
136 }
137
Dan Willemsen97750522016-02-09 17:43:51 -0800138 amod := mod.(AndroidModule).base()
139 data, err := provider.AndroidMk()
140 if err != nil {
141 return err
Dan Willemsen218f6562015-07-08 18:13:11 -0700142 }
143
Dan Willemsen97750522016-02-09 17:43:51 -0800144 if !amod.Enabled() {
145 return err
Dan Willemsen218f6562015-07-08 18:13:11 -0700146 }
147
Colin Crossa2344662016-03-24 13:14:12 -0700148 if data.SubName != "" {
149 name += "_" + data.SubName
150 }
151
Dan Willemsen97750522016-02-09 17:43:51 -0800152 hostCross := false
153 if amod.Host() && amod.HostType() != CurrentHostType() {
154 hostCross = true
155 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700156
Dan Willemsen97750522016-02-09 17:43:51 -0800157 if data.Custom != nil {
Dan Willemsen218f6562015-07-08 18:13:11 -0700158 prefix := ""
Dan Willemsen97750522016-02-09 17:43:51 -0800159 if amod.Host() {
160 if hostCross {
161 prefix = "HOST_CROSS_"
162 } else {
163 prefix = "HOST_"
164 }
165 if amod.Arch().ArchType != ctx.Config().(Config).HostArches[amod.HostType()][0].ArchType {
166 prefix = "2ND_" + prefix
Dan Willemsen218f6562015-07-08 18:13:11 -0700167 }
168 } else {
Dan Willemsen97750522016-02-09 17:43:51 -0800169 prefix = "TARGET_"
170 if amod.Arch().ArchType != ctx.Config().(Config).DeviceArches[0].ArchType {
171 prefix = "2ND_" + prefix
Dan Willemsen218f6562015-07-08 18:13:11 -0700172 }
173 }
174
Dan Willemsen97750522016-02-09 17:43:51 -0800175 return data.Custom(w, name, prefix)
Dan Willemsen218f6562015-07-08 18:13:11 -0700176 }
177
Colin Crossca860ac2016-01-04 14:34:37 -0800178 if data.Disabled {
179 return nil
180 }
181
Dan Willemsen97750522016-02-09 17:43:51 -0800182 if !data.OutputFile.Valid() {
183 return err
184 }
185
186 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
187 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
188 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", data.Class)
189 fmt.Fprintln(w, "LOCAL_MULTILIB :=", amod.commonProperties.Compile_multilib)
190 fmt.Fprintln(w, "LOCAL_SRC_FILES :=", data.OutputFile.String())
191
192 archStr := amod.Arch().ArchType.String()
193 if amod.Host() {
194 if hostCross {
195 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
196 } else {
197 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
Dan Willemsen97750522016-02-09 17:43:51 -0800198 }
199 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", amod.HostType().String())
200 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
201 } else {
202 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
203 }
204
Colin Crossca860ac2016-01-04 14:34:37 -0800205 for _, extra := range data.Extra {
206 err = extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800207 if err != nil {
208 return err
209 }
210 }
211
212 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
213
214 return err
Dan Willemsen218f6562015-07-08 18:13:11 -0700215}