blob: 8d2951db41d5b2d2ad3abba29f64be07ea3ccb86 [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
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
Colin Cross635c3b02016-05-18 15:37:25 -0700138 amod := mod.(Module).base()
Dan Willemsen97750522016-02-09 17:43:51 -0800139 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 if data.Custom != nil {
Dan Willemsen218f6562015-07-08 18:13:11 -0700153 prefix := ""
Colin Crossa1ad8d12016-06-01 17:09:44 -0700154 switch amod.Os().Class {
155 case Host:
156 prefix = "HOST_"
157 case HostCross:
158 prefix = "HOST_CROSS_"
159 case Device:
Dan Willemsen97750522016-02-09 17:43:51 -0800160 prefix = "TARGET_"
Colin Crossa1ad8d12016-06-01 17:09:44 -0700161
162 }
163
164 config := ctx.Config().(Config)
165 if amod.Arch().ArchType != config.Targets[amod.Os().Class][0].Arch.ArchType {
166 prefix = "2ND_" + prefix
Dan Willemsen218f6562015-07-08 18:13:11 -0700167 }
168
Dan Willemsen97750522016-02-09 17:43:51 -0800169 return data.Custom(w, name, prefix)
Dan Willemsen218f6562015-07-08 18:13:11 -0700170 }
171
Colin Crossca860ac2016-01-04 14:34:37 -0800172 if data.Disabled {
173 return nil
174 }
175
Dan Willemsen97750522016-02-09 17:43:51 -0800176 if !data.OutputFile.Valid() {
177 return err
178 }
179
180 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
Colin Cross26302132016-05-13 12:28:46 -0700181 fmt.Fprintln(w, "LOCAL_PATH :=", filepath.Dir(ctx.BlueprintFile(mod)))
Dan Willemsen97750522016-02-09 17:43:51 -0800182 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
183 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", data.Class)
184 fmt.Fprintln(w, "LOCAL_MULTILIB :=", amod.commonProperties.Compile_multilib)
Colin Cross26302132016-05-13 12:28:46 -0700185 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", data.OutputFile.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800186
187 archStr := amod.Arch().ArchType.String()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700188 host := false
189 switch amod.Os().Class {
190 case Host:
191 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
192 host = true
193 case HostCross:
194 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
195 host = true
196 case Device:
Dan Willemsen97750522016-02-09 17:43:51 -0800197 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700198
199 if len(amod.commonProperties.Logtags) > 0 {
200 fmt.Fprintln(w, "LOCAL_LOGTAGS_FILES := ", strings.Join(amod.commonProperties.Logtags, " "))
201 }
Dan Willemsen97750522016-02-09 17:43:51 -0800202 }
203
Colin Crossa1ad8d12016-06-01 17:09:44 -0700204 if host {
205 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", amod.Os().String())
206 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
207 }
208
Colin Crossca860ac2016-01-04 14:34:37 -0800209 for _, extra := range data.Extra {
210 err = extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800211 if err != nil {
212 return err
213 }
214 }
215
216 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
217
218 return err
Dan Willemsen218f6562015-07-08 18:13:11 -0700219}