blob: 3a1a0e2cc1b1370d166a6d4a28df0db993ec6a58 [file] [log] [blame]
Colin Cross581c1892015-04-07 16:50:10 -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 cc
16
Colin Cross581c1892015-04-07 16:50:10 -070017import (
Inseob Kim21f26902018-09-06 00:55:20 +090018 "path/filepath"
Dan Willemsen1945a4b2019-06-04 17:10:41 -070019 "strings"
Inseob Kim21f26902018-09-06 00:55:20 +090020
Colin Cross581c1892015-04-07 16:50:10 -070021 "github.com/google/blueprint"
Colin Cross581c1892015-04-07 16:50:10 -070022
Colin Cross635c3b02016-05-18 15:37:25 -070023 "android/soong/android"
Colin Cross581c1892015-04-07 16:50:10 -070024)
25
26func init() {
David Sudd18efd2020-07-24 17:19:23 +000027 pctx.SourcePathVariable("lexCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/flex")
Dan Willemsen613564e2020-07-23 21:37:35 +000028 pctx.SourcePathVariable("m4Cmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/m4")
29
Dan Willemsene1240db2016-11-03 14:28:51 -070030 pctx.HostBinToolVariable("aidlCmd", "aidl-cpp")
Inseob Kim21f26902018-09-06 00:55:20 +090031 pctx.HostBinToolVariable("syspropCmd", "sysprop_cpp")
Colin Cross581c1892015-04-07 16:50:10 -070032}
33
34var (
David Sudd18efd2020-07-24 17:19:23 +000035 lex = pctx.AndroidStaticRule("lex",
36 blueprint.RuleParams{
Matthias Maennich22fd4d12020-07-15 10:58:56 +020037 Command: "M4=$m4Cmd $lexCmd $flags -o$out $in",
David Sudd18efd2020-07-24 17:19:23 +000038 CommandDeps: []string{"$lexCmd", "$m4Cmd"},
Matthias Maennich22fd4d12020-07-15 10:58:56 +020039 }, "flags")
David Sudd18efd2020-07-24 17:19:23 +000040
Inseob Kim21f26902018-09-06 00:55:20 +090041 sysprop = pctx.AndroidStaticRule("sysprop",
42 blueprint.RuleParams{
Inseob Kim5cefbd22019-06-08 20:36:59 +090043 Command: "$syspropCmd --header-dir=$headerOutDir --public-header-dir=$publicOutDir " +
Inseob Kimc0907f12019-02-08 21:00:45 +090044 "--source-dir=$srcOutDir --include-name=$includeName $in",
Inseob Kim21f26902018-09-06 00:55:20 +090045 CommandDeps: []string{"$syspropCmd"},
46 },
Inseob Kim5cefbd22019-06-08 20:36:59 +090047 "headerOutDir", "publicOutDir", "srcOutDir", "includeName")
Inseob Kim21f26902018-09-06 00:55:20 +090048
Dan Willemsen4f1c3d42017-09-09 01:15:26 -070049 windmc = pctx.AndroidStaticRule("windmc",
50 blueprint.RuleParams{
51 Command: "$windmcCmd -r$$(dirname $out) -h$$(dirname $out) $in",
52 CommandDeps: []string{"$windmcCmd"},
53 },
54 "windmcCmd")
Colin Cross581c1892015-04-07 16:50:10 -070055)
56
Dan Willemsen4e0aa232019-04-10 22:59:54 -070057type YaccProperties struct {
58 // list of module-specific flags that will be used for .y and .yy compiles
59 Flags []string
Colin Cross581c1892015-04-07 16:50:10 -070060
Dan Willemsen4e0aa232019-04-10 22:59:54 -070061 // whether the yacc files will produce a location.hh file
62 Gen_location_hh *bool
Colin Cross581c1892015-04-07 16:50:10 -070063
Dan Willemsen4e0aa232019-04-10 22:59:54 -070064 // whether the yacc files will product a position.hh file
65 Gen_position_hh *bool
66}
67
68func genYacc(ctx android.ModuleContext, rule *android.RuleBuilder, yaccFile android.Path,
David Sudd18efd2020-07-24 17:19:23 +000069 outFile android.ModuleGenPath, props *YaccProperties) (headerFiles android.Paths) {
Dan Willemsen4e0aa232019-04-10 22:59:54 -070070
71 outDir := android.PathForModuleGen(ctx, "yacc")
72 headerFile := android.GenPathWithExt(ctx, "yacc", yaccFile, "h")
73 ret := android.Paths{headerFile}
74
75 cmd := rule.Command()
76
77 // Fix up #line markers to not use the sbox temporary directory
Colin Crossf1a035e2020-11-16 17:32:30 -080078 // android.sboxPathForOutput(outDir, outDir) returns the sbox placeholder for the out
Colin Cross3d680512020-11-13 16:23:53 -080079 // directory itself, without any filename appended.
Colin Crossf1a035e2020-11-16 17:32:30 -080080 sboxOutDir := cmd.PathForOutput(outDir)
Colin Cross3d680512020-11-13 16:23:53 -080081 sedCmd := "sed -i.bak 's#" + sboxOutDir + "#" + outDir.String() + "#'"
Dan Willemsen4e0aa232019-04-10 22:59:54 -070082 rule.Command().Text(sedCmd).Input(outFile)
83 rule.Command().Text(sedCmd).Input(headerFile)
84
85 var flags []string
86 if props != nil {
87 flags = props.Flags
88
89 if Bool(props.Gen_location_hh) {
90 locationHeader := outFile.InSameDir(ctx, "location.hh")
91 ret = append(ret, locationHeader)
92 cmd.ImplicitOutput(locationHeader)
93 rule.Command().Text(sedCmd).Input(locationHeader)
94 }
95 if Bool(props.Gen_position_hh) {
96 positionHeader := outFile.InSameDir(ctx, "position.hh")
97 ret = append(ret, positionHeader)
98 cmd.ImplicitOutput(positionHeader)
99 rule.Command().Text(sedCmd).Input(positionHeader)
100 }
101 }
102
David Sudd18efd2020-07-24 17:19:23 +0000103 cmd.Text("BISON_PKGDATADIR=prebuilts/build-tools/common/bison").
104 FlagWithInput("M4=", ctx.Config().PrebuiltBuildTool(ctx, "m4")).
105 PrebuiltBuildTool(ctx, "bison").
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700106 Flag("-d").
107 Flags(flags).
108 FlagWithOutput("--defines=", headerFile).
109 Flag("-o").Output(outFile).Input(yaccFile)
110
111 return ret
Colin Cross581c1892015-04-07 16:50:10 -0700112}
113
Jiyong Parkc7e592c2021-04-07 21:49:34 +0900114func genAidl(ctx android.ModuleContext, rule *android.RuleBuilder, aidlFile android.Path, aidlFlags string) (cppFile android.OutputPath, headerFiles android.Paths) {
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700115 aidlPackage := strings.TrimSuffix(aidlFile.Rel(), aidlFile.Base())
116 baseName := strings.TrimSuffix(aidlFile.Base(), aidlFile.Ext())
Daniel Norman10b74352019-09-24 17:39:58 -0700117 shortName := baseName
118 // TODO(b/111362593): aidl_to_cpp_common.cpp uses heuristics to figure out if
119 // an interface name has a leading I. Those same heuristics have been
120 // moved here.
121 if len(baseName) >= 2 && baseName[0] == 'I' &&
122 strings.ToUpper(baseName)[1] == baseName[1] {
123 shortName = strings.TrimPrefix(baseName, "I")
124 }
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700125
126 outDir := android.PathForModuleGen(ctx, "aidl")
Jiyong Parkc7e592c2021-04-07 21:49:34 +0900127 cppFile = outDir.Join(ctx, aidlPackage, baseName+".cpp")
128 depFile := outDir.Join(ctx, aidlPackage, baseName+".cpp.d")
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700129 headerI := outDir.Join(ctx, aidlPackage, baseName+".h")
130 headerBn := outDir.Join(ctx, aidlPackage, "Bn"+shortName+".h")
131 headerBp := outDir.Join(ctx, aidlPackage, "Bp"+shortName+".h")
132
Jiyong Park29074592019-07-07 16:27:47 +0900133 baseDir := strings.TrimSuffix(aidlFile.String(), aidlFile.Rel())
134 if baseDir != "" {
135 aidlFlags += " -I" + baseDir
136 }
137
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700138 cmd := rule.Command()
Colin Crossf1a035e2020-11-16 17:32:30 -0800139 cmd.BuiltTool("aidl-cpp").
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700140 FlagWithDepFile("-d", depFile).
141 Flag("--ninja").
142 Flag(aidlFlags).
143 Input(aidlFile).
144 OutputDir().
Jiyong Parkc7e592c2021-04-07 21:49:34 +0900145 Output(cppFile).
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700146 ImplicitOutputs(android.WritablePaths{
147 headerI,
148 headerBn,
149 headerBp,
150 })
151
Jiyong Parkc7e592c2021-04-07 21:49:34 +0900152 return cppFile, android.Paths{
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700153 headerI,
154 headerBn,
155 headerBp,
156 }
Dan Willemsene1240db2016-11-03 14:28:51 -0700157}
158
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200159type LexProperties struct {
160 // list of module-specific flags that will be used for .l and .ll compiles
161 Flags []string
162}
163
164func genLex(ctx android.ModuleContext, lexFile android.Path, outFile android.ModuleGenPath, props *LexProperties) {
165 var flags []string
166 if props != nil {
167 flags = props.Flags
168 }
169 flagsString := strings.Join(flags[:], " ")
David Sudd18efd2020-07-24 17:19:23 +0000170 ctx.Build(pctx, android.BuildParams{
171 Rule: lex,
172 Description: "lex " + lexFile.Rel(),
173 Output: outFile,
174 Input: lexFile,
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200175 Args: map[string]string{"flags": flagsString},
David Sudd18efd2020-07-24 17:19:23 +0000176 })
Colin Cross581c1892015-04-07 16:50:10 -0700177}
178
Inseob Kimed2641f2020-02-19 10:42:41 +0900179func genSysprop(ctx android.ModuleContext, syspropFile android.Path) (android.Path, android.Paths) {
Inseob Kim21f26902018-09-06 00:55:20 +0900180 headerFile := android.PathForModuleGen(ctx, "sysprop", "include", syspropFile.Rel()+".h")
Inseob Kim5cefbd22019-06-08 20:36:59 +0900181 publicHeaderFile := android.PathForModuleGen(ctx, "sysprop/public", "include", syspropFile.Rel()+".h")
Inseob Kim21f26902018-09-06 00:55:20 +0900182 cppFile := android.PathForModuleGen(ctx, "sysprop", syspropFile.Rel()+".cpp")
183
Inseob Kimed2641f2020-02-19 10:42:41 +0900184 headers := android.WritablePaths{headerFile, publicHeaderFile}
185
Inseob Kim21f26902018-09-06 00:55:20 +0900186 ctx.Build(pctx, android.BuildParams{
Inseob Kimed2641f2020-02-19 10:42:41 +0900187 Rule: sysprop,
188 Description: "sysprop " + syspropFile.Rel(),
189 Output: cppFile,
190 ImplicitOutputs: headers,
191 Input: syspropFile,
Inseob Kim21f26902018-09-06 00:55:20 +0900192 Args: map[string]string{
193 "headerOutDir": filepath.Dir(headerFile.String()),
Inseob Kim5cefbd22019-06-08 20:36:59 +0900194 "publicOutDir": filepath.Dir(publicHeaderFile.String()),
Inseob Kim21f26902018-09-06 00:55:20 +0900195 "srcOutDir": filepath.Dir(cppFile.String()),
196 "includeName": syspropFile.Rel() + ".h",
197 },
198 })
199
Inseob Kimed2641f2020-02-19 10:42:41 +0900200 return cppFile, headers.Paths()
Inseob Kim21f26902018-09-06 00:55:20 +0900201}
202
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700203func genWinMsg(ctx android.ModuleContext, srcFile android.Path, flags builderFlags) (android.Path, android.Path) {
204 headerFile := android.GenPathWithExt(ctx, "windmc", srcFile, "h")
205 rcFile := android.GenPathWithExt(ctx, "windmc", srcFile, "rc")
206
Elliott Hughes5f314b12021-05-18 11:56:02 -0700207 windmcCmd := mingwCmd(flags.toolchain, "windmc")
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700208
Colin Crossae887032017-10-23 17:16:14 -0700209 ctx.Build(pctx, android.BuildParams{
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700210 Rule: windmc,
211 Description: "windmc " + srcFile.Rel(),
212 Output: rcFile,
213 ImplicitOutput: headerFile,
214 Input: srcFile,
215 Args: map[string]string{
216 "windmcCmd": windmcCmd,
217 },
218 })
219
220 return rcFile, headerFile
221}
222
Paul Duffin33056e82021-02-19 13:49:08 +0000223// Used to communicate information from the genSources method back to the library code that uses
224// it.
225type generatedSourceInfo struct {
226 // The headers created from .proto files
227 protoHeaders android.Paths
228
229 // The files that can be used as order only dependencies in order to ensure that the proto header
230 // files are up to date.
231 protoOrderOnlyDeps android.Paths
232
233 // The headers created from .aidl files
234 aidlHeaders android.Paths
235
236 // The files that can be used as order only dependencies in order to ensure that the aidl header
237 // files are up to date.
238 aidlOrderOnlyDeps android.Paths
239
240 // The headers created from .sysprop files
241 syspropHeaders android.Paths
242
243 // The files that can be used as order only dependencies in order to ensure that the sysprop
244 // header files are up to date.
245 syspropOrderOnlyDeps android.Paths
246}
247
David Sudd18efd2020-07-24 17:19:23 +0000248func genSources(ctx android.ModuleContext, srcFiles android.Paths,
Paul Duffin33056e82021-02-19 13:49:08 +0000249 buildFlags builderFlags) (android.Paths, android.Paths, generatedSourceInfo) {
250
251 var info generatedSourceInfo
Colin Cross581c1892015-04-07 16:50:10 -0700252
Colin Cross635c3b02016-05-18 15:37:25 -0700253 var deps android.Paths
Colin Cross2a252be2017-05-01 17:37:24 -0700254 var rsFiles android.Paths
255
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700256 var aidlRule *android.RuleBuilder
257
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700258 var yaccRule_ *android.RuleBuilder
259 yaccRule := func() *android.RuleBuilder {
260 if yaccRule_ == nil {
Colin Crossf1a035e2020-11-16 17:32:30 -0800261 yaccRule_ = android.NewRuleBuilder(pctx, ctx).Sbox(android.PathForModuleGen(ctx, "yacc"),
Colin Crosse16ce362020-11-12 08:29:30 -0800262 android.PathForModuleGen(ctx, "yacc.sbox.textproto"))
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700263 }
264 return yaccRule_
265 }
266
Colin Cross581c1892015-04-07 16:50:10 -0700267 for i, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700268 switch srcFile.Ext() {
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800269 case ".y":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700270 cFile := android.GenPathWithExt(ctx, "yacc", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800271 srcFiles[i] = cFile
David Sudd18efd2020-07-24 17:19:23 +0000272 deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cFile, buildFlags.yacc)...)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800273 case ".yy":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700274 cppFile := android.GenPathWithExt(ctx, "yacc", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700275 srcFiles[i] = cppFile
David Sudd18efd2020-07-24 17:19:23 +0000276 deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cppFile, buildFlags.yacc)...)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800277 case ".l":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700278 cFile := android.GenPathWithExt(ctx, "lex", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800279 srcFiles[i] = cFile
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200280 genLex(ctx, srcFile, cFile, buildFlags.lex)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800281 case ".ll":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700282 cppFile := android.GenPathWithExt(ctx, "lex", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700283 srcFiles[i] = cppFile
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200284 genLex(ctx, srcFile, cppFile, buildFlags.lex)
Colin Cross0c461f12016-10-20 16:11:43 -0700285 case ".proto":
Dan Willemsen60e62f02018-11-16 21:05:32 -0800286 ccFile, headerFile := genProto(ctx, srcFile, buildFlags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700287 srcFiles[i] = ccFile
Paul Duffin33056e82021-02-19 13:49:08 +0000288 info.protoHeaders = append(info.protoHeaders, headerFile)
289 // Use the generated header as an order only dep to ensure that it is up to date when needed.
290 info.protoOrderOnlyDeps = append(info.protoOrderOnlyDeps, headerFile)
Dan Willemsene1240db2016-11-03 14:28:51 -0700291 case ".aidl":
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700292 if aidlRule == nil {
Colin Crossf1a035e2020-11-16 17:32:30 -0800293 aidlRule = android.NewRuleBuilder(pctx, ctx).Sbox(android.PathForModuleGen(ctx, "aidl"),
Colin Crosse16ce362020-11-12 08:29:30 -0800294 android.PathForModuleGen(ctx, "aidl.sbox.textproto"))
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700295 }
Jiyong Parkc7e592c2021-04-07 21:49:34 +0900296 cppFile, aidlHeaders := genAidl(ctx, aidlRule, srcFile, buildFlags.aidlFlags)
Dan Willemsene1240db2016-11-03 14:28:51 -0700297 srcFiles[i] = cppFile
Jiyong Parkc7e592c2021-04-07 21:49:34 +0900298
Paul Duffin33056e82021-02-19 13:49:08 +0000299 info.aidlHeaders = append(info.aidlHeaders, aidlHeaders...)
300 // Use the generated headers as order only deps to ensure that they are up to date when
301 // needed.
302 // TODO: Reduce the size of the ninja file by using one order only dep for the whole rule
303 info.aidlOrderOnlyDeps = append(info.aidlOrderOnlyDeps, aidlHeaders...)
Jeff Vander Stoepd6126272019-07-15 19:08:37 -0700304 case ".rscript", ".fs":
Colin Cross2a252be2017-05-01 17:37:24 -0700305 cppFile := rsGeneratedCppFile(ctx, srcFile)
306 rsFiles = append(rsFiles, srcFiles[i])
307 srcFiles[i] = cppFile
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700308 case ".mc":
309 rcFile, headerFile := genWinMsg(ctx, srcFile, buildFlags)
310 srcFiles[i] = rcFile
311 deps = append(deps, headerFile)
Inseob Kim21f26902018-09-06 00:55:20 +0900312 case ".sysprop":
Inseob Kimed2641f2020-02-19 10:42:41 +0900313 cppFile, headerFiles := genSysprop(ctx, srcFile)
Inseob Kim21f26902018-09-06 00:55:20 +0900314 srcFiles[i] = cppFile
Paul Duffin33056e82021-02-19 13:49:08 +0000315 info.syspropHeaders = append(info.syspropHeaders, headerFiles...)
316 // Use the generated headers as order only deps to ensure that they are up to date when
317 // needed.
318 info.syspropOrderOnlyDeps = append(info.syspropOrderOnlyDeps, headerFiles...)
Colin Cross581c1892015-04-07 16:50:10 -0700319 }
320 }
321
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700322 if aidlRule != nil {
Colin Crossf1a035e2020-11-16 17:32:30 -0800323 aidlRule.Build("aidl", "gen aidl")
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700324 }
325
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700326 if yaccRule_ != nil {
Colin Crossf1a035e2020-11-16 17:32:30 -0800327 yaccRule_.Build("yacc", "gen yacc")
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700328 }
329
Paul Duffin33056e82021-02-19 13:49:08 +0000330 deps = append(deps, info.protoOrderOnlyDeps...)
331 deps = append(deps, info.aidlOrderOnlyDeps...)
332 deps = append(deps, info.syspropOrderOnlyDeps...)
333
Colin Cross2a252be2017-05-01 17:37:24 -0700334 if len(rsFiles) > 0 {
335 deps = append(deps, rsGenerateCpp(ctx, rsFiles, buildFlags.rsFlags)...)
336 }
337
Paul Duffin33056e82021-02-19 13:49:08 +0000338 return srcFiles, deps, info
Colin Cross581c1892015-04-07 16:50:10 -0700339}