blob: c8dce0f85941250fbed4335c7a01e198c8059110 [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"
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 Cross635c3b02016-05-18 15:37:25 -070066 var androidMkModulesList []Module
Colin Cross4f6e4e62016-01-11 12:55:55 -080067
Dan Willemsen218f6562015-07-08 18:13:11 -070068 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -070069 if amod, ok := module.(Module); 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
Colin Cross635c3b02016-05-18 15:37:25 -070093func translateAndroidMk(ctx blueprint.SingletonContext, mkFile string, mods []Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -070094 buf := &bytes.Buffer{}
95
Dan Willemsen97750522016-02-09 17:43:51 -080096 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -070097
98 for _, mod := range mods {
99 err := translateAndroidMkModule(ctx, buf, mod)
100 if err != nil {
101 os.Remove(mkFile)
102 return err
103 }
104 }
105
106 // Don't write to the file if it hasn't changed
107 if _, err := os.Stat(mkFile); !os.IsNotExist(err) {
108 if data, err := ioutil.ReadFile(mkFile); err == nil {
109 matches := buf.Len() == len(data)
110
111 if matches {
112 for i, value := range buf.Bytes() {
113 if value != data[i] {
114 matches = false
115 break
116 }
117 }
118 }
119
120 if matches {
121 return nil
122 }
123 }
124 }
125
126 return ioutil.WriteFile(mkFile, buf.Bytes(), 0666)
127}
128
129func translateAndroidMkModule(ctx blueprint.SingletonContext, w io.Writer, mod blueprint.Module) error {
Dan Willemsen97750522016-02-09 17:43:51 -0800130 name := ctx.ModuleName(mod)
131
132 provider, ok := mod.(AndroidMkDataProvider)
133 if !ok {
Dan Willemsen218f6562015-07-08 18:13:11 -0700134 return nil
135 }
136
Colin Cross635c3b02016-05-18 15:37:25 -0700137 amod := mod.(Module).base()
Dan Willemsen97750522016-02-09 17:43:51 -0800138 data, err := provider.AndroidMk()
139 if err != nil {
140 return err
Dan Willemsen218f6562015-07-08 18:13:11 -0700141 }
142
Dan Willemsen97750522016-02-09 17:43:51 -0800143 if !amod.Enabled() {
144 return err
Dan Willemsen218f6562015-07-08 18:13:11 -0700145 }
146
Colin Crossa2344662016-03-24 13:14:12 -0700147 if data.SubName != "" {
148 name += "_" + data.SubName
149 }
150
Dan Willemsen97750522016-02-09 17:43:51 -0800151 hostCross := false
152 if amod.Host() && amod.HostType() != CurrentHostType() {
153 hostCross = true
154 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700155
Dan Willemsen97750522016-02-09 17:43:51 -0800156 if data.Custom != nil {
Dan Willemsen218f6562015-07-08 18:13:11 -0700157 prefix := ""
Dan Willemsen97750522016-02-09 17:43:51 -0800158 if amod.Host() {
159 if hostCross {
160 prefix = "HOST_CROSS_"
161 } else {
162 prefix = "HOST_"
163 }
164 if amod.Arch().ArchType != ctx.Config().(Config).HostArches[amod.HostType()][0].ArchType {
165 prefix = "2ND_" + prefix
Dan Willemsen218f6562015-07-08 18:13:11 -0700166 }
167 } else {
Dan Willemsen97750522016-02-09 17:43:51 -0800168 prefix = "TARGET_"
169 if amod.Arch().ArchType != ctx.Config().(Config).DeviceArches[0].ArchType {
170 prefix = "2ND_" + prefix
Dan Willemsen218f6562015-07-08 18:13:11 -0700171 }
172 }
173
Dan Willemsen97750522016-02-09 17:43:51 -0800174 return data.Custom(w, name, prefix)
Dan Willemsen218f6562015-07-08 18:13:11 -0700175 }
176
Colin Crossca860ac2016-01-04 14:34:37 -0800177 if data.Disabled {
178 return nil
179 }
180
Dan Willemsen97750522016-02-09 17:43:51 -0800181 if !data.OutputFile.Valid() {
182 return err
183 }
184
185 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
Colin Cross26302132016-05-13 12:28:46 -0700186 fmt.Fprintln(w, "LOCAL_PATH :=", filepath.Dir(ctx.BlueprintFile(mod)))
Dan Willemsen97750522016-02-09 17:43:51 -0800187 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)
Colin Cross26302132016-05-13 12:28:46 -0700190 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", data.OutputFile.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800191
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}