blob: 49380e05bb31bb46e20721f4876b29fb0009072e [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"
29)
30
31func init() {
32 soong.RegisterSingletonType("androidmk", AndroidMkSingleton)
33}
34
35type AndroidMkDataProvider interface {
Dan Willemsen97750522016-02-09 17:43:51 -080036 AndroidMk() (AndroidMkData, error)
Dan Willemsen218f6562015-07-08 18:13:11 -070037}
38
39type AndroidMkData struct {
40 Class string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070041 OutputFile OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -080042 Disabled bool
Dan Willemsen218f6562015-07-08 18:13:11 -070043
Dan Willemsen97750522016-02-09 17:43:51 -080044 Custom func(w io.Writer, name, prefix string) error
Dan Willemsen218f6562015-07-08 18:13:11 -070045
Colin Crossca860ac2016-01-04 14:34:37 -080046 Extra []func(w io.Writer, outputFile Path) error
Dan Willemsen218f6562015-07-08 18:13:11 -070047}
48
49func AndroidMkSingleton() blueprint.Singleton {
50 return &androidMkSingleton{}
51}
52
53type androidMkSingleton struct{}
54
55func (c *androidMkSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
Dan Willemsen5ba07e82015-12-11 13:51:06 -080056 if !ctx.Config().(Config).EmbeddedInMake() {
57 return
58 }
59
Dan Willemsen34cc69e2015-09-23 15:26:20 -070060 ctx.SetNinjaBuildDir(pctx, filepath.Join(ctx.Config().(Config).buildDir, ".."))
Dan Willemsen218f6562015-07-08 18:13:11 -070061
Colin Cross4f6e4e62016-01-11 12:55:55 -080062 var androidMkModulesList []AndroidModule
63
Dan Willemsen218f6562015-07-08 18:13:11 -070064 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross4f6e4e62016-01-11 12:55:55 -080065 if amod, ok := module.(AndroidModule); ok {
Dan Willemsen218f6562015-07-08 18:13:11 -070066 androidMkModulesList = append(androidMkModulesList, amod)
67 }
Colin Cross4f6e4e62016-01-11 12:55:55 -080068 })
Dan Willemsen218f6562015-07-08 18:13:11 -070069
Colin Crossd779da42015-12-17 18:00:23 -080070 sort.Sort(AndroidModulesByName{androidMkModulesList, ctx})
71
Dan Willemsen34cc69e2015-09-23 15:26:20 -070072 transMk := PathForOutput(ctx, "Android.mk")
73 if ctx.Failed() {
74 return
75 }
Dan Willemsen218f6562015-07-08 18:13:11 -070076
Dan Willemsen34cc69e2015-09-23 15:26:20 -070077 err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -070078 if err != nil {
79 ctx.Errorf(err.Error())
80 }
81
82 ctx.Build(pctx, blueprint.BuildParams{
83 Rule: blueprint.Phony,
Dan Willemsen34cc69e2015-09-23 15:26:20 -070084 Outputs: []string{transMk.String()},
Dan Willemsen218f6562015-07-08 18:13:11 -070085 Optional: true,
86 })
87}
88
89func translateAndroidMk(ctx blueprint.SingletonContext, mkFile string, mods []AndroidModule) error {
90 buf := &bytes.Buffer{}
91
Dan Willemsen97750522016-02-09 17:43:51 -080092 fmt.Fprintln(buf, "LOCAL_PATH := $(TOP)")
93 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -070094
95 for _, mod := range mods {
96 err := translateAndroidMkModule(ctx, buf, mod)
97 if err != nil {
98 os.Remove(mkFile)
99 return err
100 }
101 }
102
103 // Don't write to the file if it hasn't changed
104 if _, err := os.Stat(mkFile); !os.IsNotExist(err) {
105 if data, err := ioutil.ReadFile(mkFile); err == nil {
106 matches := buf.Len() == len(data)
107
108 if matches {
109 for i, value := range buf.Bytes() {
110 if value != data[i] {
111 matches = false
112 break
113 }
114 }
115 }
116
117 if matches {
118 return nil
119 }
120 }
121 }
122
123 return ioutil.WriteFile(mkFile, buf.Bytes(), 0666)
124}
125
126func translateAndroidMkModule(ctx blueprint.SingletonContext, w io.Writer, mod blueprint.Module) error {
Dan Willemsen97750522016-02-09 17:43:51 -0800127 name := ctx.ModuleName(mod)
128
129 provider, ok := mod.(AndroidMkDataProvider)
130 if !ok {
Dan Willemsen218f6562015-07-08 18:13:11 -0700131 return nil
132 }
133
Dan Willemsen97750522016-02-09 17:43:51 -0800134 amod := mod.(AndroidModule).base()
135 data, err := provider.AndroidMk()
136 if err != nil {
137 return err
Dan Willemsen218f6562015-07-08 18:13:11 -0700138 }
139
Dan Willemsen97750522016-02-09 17:43:51 -0800140 if !amod.Enabled() {
141 return err
Dan Willemsen218f6562015-07-08 18:13:11 -0700142 }
143
Dan Willemsen97750522016-02-09 17:43:51 -0800144 hostCross := false
145 if amod.Host() && amod.HostType() != CurrentHostType() {
146 hostCross = true
147 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700148
Dan Willemsen97750522016-02-09 17:43:51 -0800149 if data.Custom != nil {
Dan Willemsen218f6562015-07-08 18:13:11 -0700150 prefix := ""
Dan Willemsen97750522016-02-09 17:43:51 -0800151 if amod.Host() {
152 if hostCross {
153 prefix = "HOST_CROSS_"
154 } else {
155 prefix = "HOST_"
156 }
157 if amod.Arch().ArchType != ctx.Config().(Config).HostArches[amod.HostType()][0].ArchType {
158 prefix = "2ND_" + prefix
Dan Willemsen218f6562015-07-08 18:13:11 -0700159 }
160 } else {
Dan Willemsen97750522016-02-09 17:43:51 -0800161 prefix = "TARGET_"
162 if amod.Arch().ArchType != ctx.Config().(Config).DeviceArches[0].ArchType {
163 prefix = "2ND_" + prefix
Dan Willemsen218f6562015-07-08 18:13:11 -0700164 }
165 }
166
Dan Willemsen97750522016-02-09 17:43:51 -0800167 return data.Custom(w, name, prefix)
Dan Willemsen218f6562015-07-08 18:13:11 -0700168 }
169
Colin Crossca860ac2016-01-04 14:34:37 -0800170 if data.Disabled {
171 return nil
172 }
173
Dan Willemsen97750522016-02-09 17:43:51 -0800174 if !data.OutputFile.Valid() {
175 return err
176 }
177
178 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
179 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
180 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", data.Class)
181 fmt.Fprintln(w, "LOCAL_MULTILIB :=", amod.commonProperties.Compile_multilib)
182 fmt.Fprintln(w, "LOCAL_SRC_FILES :=", data.OutputFile.String())
183
184 archStr := amod.Arch().ArchType.String()
185 if amod.Host() {
186 if hostCross {
187 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
188 } else {
189 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
Dan Willemsen97750522016-02-09 17:43:51 -0800190 }
191 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", amod.HostType().String())
192 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
193 } else {
194 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
195 }
196
Colin Crossca860ac2016-01-04 14:34:37 -0800197 for _, extra := range data.Extra {
198 err = extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800199 if err != nil {
200 return err
201 }
202 }
203
204 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
205
206 return err
Dan Willemsen218f6562015-07-08 18:13:11 -0700207}