blob: 08b49c97d673c6bb9e0878618b3f612f5b6c7b90 [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
Trevor Radcliffeef9c9002022-05-13 20:55:35 +000021 "android/soong/bazel"
Colin Cross581c1892015-04-07 16:50:10 -070022 "github.com/google/blueprint"
Colin Cross581c1892015-04-07 16:50:10 -070023
Colin Cross635c3b02016-05-18 15:37:25 -070024 "android/soong/android"
Colin Cross581c1892015-04-07 16:50:10 -070025)
26
27func init() {
David Sudd18efd2020-07-24 17:19:23 +000028 pctx.SourcePathVariable("lexCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/flex")
Dan Willemsen613564e2020-07-23 21:37:35 +000029 pctx.SourcePathVariable("m4Cmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/m4")
30
Dan Willemsene1240db2016-11-03 14:28:51 -070031 pctx.HostBinToolVariable("aidlCmd", "aidl-cpp")
Inseob Kim21f26902018-09-06 00:55:20 +090032 pctx.HostBinToolVariable("syspropCmd", "sysprop_cpp")
Colin Cross581c1892015-04-07 16:50:10 -070033}
34
35var (
David Sudd18efd2020-07-24 17:19:23 +000036 lex = pctx.AndroidStaticRule("lex",
37 blueprint.RuleParams{
Matthias Maennich22fd4d12020-07-15 10:58:56 +020038 Command: "M4=$m4Cmd $lexCmd $flags -o$out $in",
David Sudd18efd2020-07-24 17:19:23 +000039 CommandDeps: []string{"$lexCmd", "$m4Cmd"},
Matthias Maennich22fd4d12020-07-15 10:58:56 +020040 }, "flags")
David Sudd18efd2020-07-24 17:19:23 +000041
Inseob Kim21f26902018-09-06 00:55:20 +090042 sysprop = pctx.AndroidStaticRule("sysprop",
43 blueprint.RuleParams{
Inseob Kim5cefbd22019-06-08 20:36:59 +090044 Command: "$syspropCmd --header-dir=$headerOutDir --public-header-dir=$publicOutDir " +
Inseob Kimc0907f12019-02-08 21:00:45 +090045 "--source-dir=$srcOutDir --include-name=$includeName $in",
Inseob Kim21f26902018-09-06 00:55:20 +090046 CommandDeps: []string{"$syspropCmd"},
47 },
Inseob Kim5cefbd22019-06-08 20:36:59 +090048 "headerOutDir", "publicOutDir", "srcOutDir", "includeName")
Colin Cross581c1892015-04-07 16:50:10 -070049)
50
Dan Willemsen4e0aa232019-04-10 22:59:54 -070051type YaccProperties struct {
52 // list of module-specific flags that will be used for .y and .yy compiles
53 Flags []string
Colin Cross581c1892015-04-07 16:50:10 -070054
Dan Willemsen4e0aa232019-04-10 22:59:54 -070055 // whether the yacc files will produce a location.hh file
56 Gen_location_hh *bool
Colin Cross581c1892015-04-07 16:50:10 -070057
Dan Willemsen4e0aa232019-04-10 22:59:54 -070058 // whether the yacc files will product a position.hh file
59 Gen_position_hh *bool
60}
61
62func genYacc(ctx android.ModuleContext, rule *android.RuleBuilder, yaccFile android.Path,
David Sudd18efd2020-07-24 17:19:23 +000063 outFile android.ModuleGenPath, props *YaccProperties) (headerFiles android.Paths) {
Dan Willemsen4e0aa232019-04-10 22:59:54 -070064
65 outDir := android.PathForModuleGen(ctx, "yacc")
66 headerFile := android.GenPathWithExt(ctx, "yacc", yaccFile, "h")
67 ret := android.Paths{headerFile}
68
69 cmd := rule.Command()
70
71 // Fix up #line markers to not use the sbox temporary directory
Colin Crossf1a035e2020-11-16 17:32:30 -080072 // android.sboxPathForOutput(outDir, outDir) returns the sbox placeholder for the out
Colin Cross3d680512020-11-13 16:23:53 -080073 // directory itself, without any filename appended.
Colin Crossf1a035e2020-11-16 17:32:30 -080074 sboxOutDir := cmd.PathForOutput(outDir)
Colin Cross3d680512020-11-13 16:23:53 -080075 sedCmd := "sed -i.bak 's#" + sboxOutDir + "#" + outDir.String() + "#'"
Dan Willemsen4e0aa232019-04-10 22:59:54 -070076 rule.Command().Text(sedCmd).Input(outFile)
77 rule.Command().Text(sedCmd).Input(headerFile)
78
79 var flags []string
80 if props != nil {
81 flags = props.Flags
82
83 if Bool(props.Gen_location_hh) {
84 locationHeader := outFile.InSameDir(ctx, "location.hh")
85 ret = append(ret, locationHeader)
86 cmd.ImplicitOutput(locationHeader)
87 rule.Command().Text(sedCmd).Input(locationHeader)
88 }
89 if Bool(props.Gen_position_hh) {
90 positionHeader := outFile.InSameDir(ctx, "position.hh")
91 ret = append(ret, positionHeader)
92 cmd.ImplicitOutput(positionHeader)
93 rule.Command().Text(sedCmd).Input(positionHeader)
94 }
95 }
96
David Sudd18efd2020-07-24 17:19:23 +000097 cmd.Text("BISON_PKGDATADIR=prebuilts/build-tools/common/bison").
98 FlagWithInput("M4=", ctx.Config().PrebuiltBuildTool(ctx, "m4")).
99 PrebuiltBuildTool(ctx, "bison").
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700100 Flag("-d").
101 Flags(flags).
102 FlagWithOutput("--defines=", headerFile).
103 Flag("-o").Output(outFile).Input(yaccFile)
104
105 return ret
Colin Cross581c1892015-04-07 16:50:10 -0700106}
107
Jiyong Parkc7e592c2021-04-07 21:49:34 +0900108func 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 -0700109 aidlPackage := strings.TrimSuffix(aidlFile.Rel(), aidlFile.Base())
110 baseName := strings.TrimSuffix(aidlFile.Base(), aidlFile.Ext())
Daniel Norman10b74352019-09-24 17:39:58 -0700111 shortName := baseName
112 // TODO(b/111362593): aidl_to_cpp_common.cpp uses heuristics to figure out if
113 // an interface name has a leading I. Those same heuristics have been
114 // moved here.
115 if len(baseName) >= 2 && baseName[0] == 'I' &&
116 strings.ToUpper(baseName)[1] == baseName[1] {
117 shortName = strings.TrimPrefix(baseName, "I")
118 }
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700119
120 outDir := android.PathForModuleGen(ctx, "aidl")
Jiyong Parkc7e592c2021-04-07 21:49:34 +0900121 cppFile = outDir.Join(ctx, aidlPackage, baseName+".cpp")
122 depFile := outDir.Join(ctx, aidlPackage, baseName+".cpp.d")
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700123 headerI := outDir.Join(ctx, aidlPackage, baseName+".h")
124 headerBn := outDir.Join(ctx, aidlPackage, "Bn"+shortName+".h")
125 headerBp := outDir.Join(ctx, aidlPackage, "Bp"+shortName+".h")
126
Jiyong Park29074592019-07-07 16:27:47 +0900127 baseDir := strings.TrimSuffix(aidlFile.String(), aidlFile.Rel())
128 if baseDir != "" {
129 aidlFlags += " -I" + baseDir
130 }
131
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700132 cmd := rule.Command()
Colin Crossf1a035e2020-11-16 17:32:30 -0800133 cmd.BuiltTool("aidl-cpp").
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700134 FlagWithDepFile("-d", depFile).
135 Flag("--ninja").
136 Flag(aidlFlags).
137 Input(aidlFile).
138 OutputDir().
Jiyong Parkc7e592c2021-04-07 21:49:34 +0900139 Output(cppFile).
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700140 ImplicitOutputs(android.WritablePaths{
141 headerI,
142 headerBn,
143 headerBp,
144 })
145
Jiyong Parkc7e592c2021-04-07 21:49:34 +0900146 return cppFile, android.Paths{
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700147 headerI,
148 headerBn,
149 headerBp,
150 }
Dan Willemsene1240db2016-11-03 14:28:51 -0700151}
152
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200153type LexProperties struct {
154 // list of module-specific flags that will be used for .l and .ll compiles
155 Flags []string
156}
157
158func genLex(ctx android.ModuleContext, lexFile android.Path, outFile android.ModuleGenPath, props *LexProperties) {
159 var flags []string
160 if props != nil {
161 flags = props.Flags
162 }
163 flagsString := strings.Join(flags[:], " ")
David Sudd18efd2020-07-24 17:19:23 +0000164 ctx.Build(pctx, android.BuildParams{
165 Rule: lex,
166 Description: "lex " + lexFile.Rel(),
167 Output: outFile,
168 Input: lexFile,
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200169 Args: map[string]string{"flags": flagsString},
David Sudd18efd2020-07-24 17:19:23 +0000170 })
Colin Cross581c1892015-04-07 16:50:10 -0700171}
172
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000173type LexAttrs struct {
174 Srcs bazel.LabelListAttribute
175 Lexopts bazel.StringListAttribute
176}
177
178type LexNames struct {
179 cSrcName bazel.LabelAttribute
180 srcName bazel.LabelAttribute
181}
182
183func bp2BuildLex(ctx android.Bp2buildMutatorContext, moduleName string, ca compilerAttributes) LexNames {
184 names := LexNames{}
185 if !ca.lSrcs.IsEmpty() {
186 names.cSrcName = createLexTargetModule(ctx, moduleName+"_genlex_l", ca.lSrcs, ca.lexopts)
187 }
188 if !ca.llSrcs.IsEmpty() {
189 names.srcName = createLexTargetModule(ctx, moduleName+"_genlex_ll", ca.llSrcs, ca.lexopts)
190 }
191 return names
192}
193
194func createLexTargetModule(ctx android.Bp2buildMutatorContext, name string, srcs bazel.LabelListAttribute, opts bazel.StringListAttribute) bazel.LabelAttribute {
195 ctx.CreateBazelTargetModule(
196 bazel.BazelTargetModuleProperties{
197 Rule_class: "genlex",
198 Bzl_load_location: "//build/bazel/rules/cc:flex.bzl",
199 },
200 android.CommonAttributes{Name: name},
201 &LexAttrs{
202 Srcs: srcs,
203 Lexopts: opts,
204 })
205 return bazel.LabelAttribute{Value: &bazel.Label{Label: ":" + name}}
206}
207
Inseob Kimed2641f2020-02-19 10:42:41 +0900208func genSysprop(ctx android.ModuleContext, syspropFile android.Path) (android.Path, android.Paths) {
Inseob Kim21f26902018-09-06 00:55:20 +0900209 headerFile := android.PathForModuleGen(ctx, "sysprop", "include", syspropFile.Rel()+".h")
Inseob Kim5cefbd22019-06-08 20:36:59 +0900210 publicHeaderFile := android.PathForModuleGen(ctx, "sysprop/public", "include", syspropFile.Rel()+".h")
Inseob Kim21f26902018-09-06 00:55:20 +0900211 cppFile := android.PathForModuleGen(ctx, "sysprop", syspropFile.Rel()+".cpp")
212
Inseob Kimed2641f2020-02-19 10:42:41 +0900213 headers := android.WritablePaths{headerFile, publicHeaderFile}
214
Inseob Kim21f26902018-09-06 00:55:20 +0900215 ctx.Build(pctx, android.BuildParams{
Inseob Kimed2641f2020-02-19 10:42:41 +0900216 Rule: sysprop,
217 Description: "sysprop " + syspropFile.Rel(),
218 Output: cppFile,
219 ImplicitOutputs: headers,
220 Input: syspropFile,
Inseob Kim21f26902018-09-06 00:55:20 +0900221 Args: map[string]string{
222 "headerOutDir": filepath.Dir(headerFile.String()),
Inseob Kim5cefbd22019-06-08 20:36:59 +0900223 "publicOutDir": filepath.Dir(publicHeaderFile.String()),
Inseob Kim21f26902018-09-06 00:55:20 +0900224 "srcOutDir": filepath.Dir(cppFile.String()),
225 "includeName": syspropFile.Rel() + ".h",
226 },
227 })
228
Inseob Kimed2641f2020-02-19 10:42:41 +0900229 return cppFile, headers.Paths()
Inseob Kim21f26902018-09-06 00:55:20 +0900230}
231
Paul Duffin33056e82021-02-19 13:49:08 +0000232// Used to communicate information from the genSources method back to the library code that uses
233// it.
234type generatedSourceInfo struct {
235 // The headers created from .proto files
236 protoHeaders android.Paths
237
238 // The files that can be used as order only dependencies in order to ensure that the proto header
239 // files are up to date.
240 protoOrderOnlyDeps android.Paths
241
242 // The headers created from .aidl files
243 aidlHeaders android.Paths
244
245 // The files that can be used as order only dependencies in order to ensure that the aidl header
246 // files are up to date.
247 aidlOrderOnlyDeps android.Paths
248
249 // The headers created from .sysprop files
250 syspropHeaders android.Paths
251
252 // The files that can be used as order only dependencies in order to ensure that the sysprop
253 // header files are up to date.
254 syspropOrderOnlyDeps android.Paths
255}
256
David Sudd18efd2020-07-24 17:19:23 +0000257func genSources(ctx android.ModuleContext, srcFiles android.Paths,
Paul Duffin33056e82021-02-19 13:49:08 +0000258 buildFlags builderFlags) (android.Paths, android.Paths, generatedSourceInfo) {
259
260 var info generatedSourceInfo
Colin Cross581c1892015-04-07 16:50:10 -0700261
Colin Cross635c3b02016-05-18 15:37:25 -0700262 var deps android.Paths
Colin Cross2a252be2017-05-01 17:37:24 -0700263 var rsFiles android.Paths
264
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700265 var aidlRule *android.RuleBuilder
266
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700267 var yaccRule_ *android.RuleBuilder
268 yaccRule := func() *android.RuleBuilder {
269 if yaccRule_ == nil {
Colin Crossf1a035e2020-11-16 17:32:30 -0800270 yaccRule_ = android.NewRuleBuilder(pctx, ctx).Sbox(android.PathForModuleGen(ctx, "yacc"),
Colin Crosse16ce362020-11-12 08:29:30 -0800271 android.PathForModuleGen(ctx, "yacc.sbox.textproto"))
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700272 }
273 return yaccRule_
274 }
275
Colin Cross581c1892015-04-07 16:50:10 -0700276 for i, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700277 switch srcFile.Ext() {
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800278 case ".y":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700279 cFile := android.GenPathWithExt(ctx, "yacc", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800280 srcFiles[i] = cFile
David Sudd18efd2020-07-24 17:19:23 +0000281 deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cFile, buildFlags.yacc)...)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800282 case ".yy":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700283 cppFile := android.GenPathWithExt(ctx, "yacc", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700284 srcFiles[i] = cppFile
David Sudd18efd2020-07-24 17:19:23 +0000285 deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cppFile, buildFlags.yacc)...)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800286 case ".l":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700287 cFile := android.GenPathWithExt(ctx, "lex", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800288 srcFiles[i] = cFile
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200289 genLex(ctx, srcFile, cFile, buildFlags.lex)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800290 case ".ll":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700291 cppFile := android.GenPathWithExt(ctx, "lex", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700292 srcFiles[i] = cppFile
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200293 genLex(ctx, srcFile, cppFile, buildFlags.lex)
Colin Cross0c461f12016-10-20 16:11:43 -0700294 case ".proto":
Dan Willemsen60e62f02018-11-16 21:05:32 -0800295 ccFile, headerFile := genProto(ctx, srcFile, buildFlags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700296 srcFiles[i] = ccFile
Paul Duffin33056e82021-02-19 13:49:08 +0000297 info.protoHeaders = append(info.protoHeaders, headerFile)
298 // Use the generated header as an order only dep to ensure that it is up to date when needed.
299 info.protoOrderOnlyDeps = append(info.protoOrderOnlyDeps, headerFile)
Dan Willemsene1240db2016-11-03 14:28:51 -0700300 case ".aidl":
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700301 if aidlRule == nil {
Colin Crossf1a035e2020-11-16 17:32:30 -0800302 aidlRule = android.NewRuleBuilder(pctx, ctx).Sbox(android.PathForModuleGen(ctx, "aidl"),
Colin Crosse16ce362020-11-12 08:29:30 -0800303 android.PathForModuleGen(ctx, "aidl.sbox.textproto"))
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700304 }
Jiyong Parkc7e592c2021-04-07 21:49:34 +0900305 cppFile, aidlHeaders := genAidl(ctx, aidlRule, srcFile, buildFlags.aidlFlags)
Dan Willemsene1240db2016-11-03 14:28:51 -0700306 srcFiles[i] = cppFile
Jiyong Parkc7e592c2021-04-07 21:49:34 +0900307
Paul Duffin33056e82021-02-19 13:49:08 +0000308 info.aidlHeaders = append(info.aidlHeaders, aidlHeaders...)
309 // Use the generated headers as order only deps to ensure that they are up to date when
310 // needed.
311 // TODO: Reduce the size of the ninja file by using one order only dep for the whole rule
312 info.aidlOrderOnlyDeps = append(info.aidlOrderOnlyDeps, aidlHeaders...)
Jeff Vander Stoepd6126272019-07-15 19:08:37 -0700313 case ".rscript", ".fs":
Colin Cross2a252be2017-05-01 17:37:24 -0700314 cppFile := rsGeneratedCppFile(ctx, srcFile)
315 rsFiles = append(rsFiles, srcFiles[i])
316 srcFiles[i] = cppFile
Inseob Kim21f26902018-09-06 00:55:20 +0900317 case ".sysprop":
Inseob Kimed2641f2020-02-19 10:42:41 +0900318 cppFile, headerFiles := genSysprop(ctx, srcFile)
Inseob Kim21f26902018-09-06 00:55:20 +0900319 srcFiles[i] = cppFile
Paul Duffin33056e82021-02-19 13:49:08 +0000320 info.syspropHeaders = append(info.syspropHeaders, headerFiles...)
321 // Use the generated headers as order only deps to ensure that they are up to date when
322 // needed.
323 info.syspropOrderOnlyDeps = append(info.syspropOrderOnlyDeps, headerFiles...)
Colin Cross581c1892015-04-07 16:50:10 -0700324 }
325 }
326
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700327 if aidlRule != nil {
Colin Crossf1a035e2020-11-16 17:32:30 -0800328 aidlRule.Build("aidl", "gen aidl")
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700329 }
330
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700331 if yaccRule_ != nil {
Colin Crossf1a035e2020-11-16 17:32:30 -0800332 yaccRule_.Build("yacc", "gen yacc")
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700333 }
334
Paul Duffin33056e82021-02-19 13:49:08 +0000335 deps = append(deps, info.protoOrderOnlyDeps...)
336 deps = append(deps, info.aidlOrderOnlyDeps...)
337 deps = append(deps, info.syspropOrderOnlyDeps...)
338
Colin Cross2a252be2017-05-01 17:37:24 -0700339 if len(rsFiles) > 0 {
340 deps = append(deps, rsGenerateCpp(ctx, rsFiles, buildFlags.rsFlags)...)
341 }
342
Paul Duffin33056e82021-02-19 13:49:08 +0000343 return srcFiles, deps, info
Colin Cross581c1892015-04-07 16:50:10 -0700344}