blob: 8f6236349608efad82f6425e0a9b4fc7762d6a0c [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")
Colin Cross581c1892015-04-07 16:50:10 -070048)
49
Dan Willemsen4e0aa232019-04-10 22:59:54 -070050type YaccProperties struct {
51 // list of module-specific flags that will be used for .y and .yy compiles
52 Flags []string
Colin Cross581c1892015-04-07 16:50:10 -070053
Dan Willemsen4e0aa232019-04-10 22:59:54 -070054 // whether the yacc files will produce a location.hh file
55 Gen_location_hh *bool
Colin Cross581c1892015-04-07 16:50:10 -070056
Dan Willemsen4e0aa232019-04-10 22:59:54 -070057 // whether the yacc files will product a position.hh file
58 Gen_position_hh *bool
59}
60
61func genYacc(ctx android.ModuleContext, rule *android.RuleBuilder, yaccFile android.Path,
David Sudd18efd2020-07-24 17:19:23 +000062 outFile android.ModuleGenPath, props *YaccProperties) (headerFiles android.Paths) {
Dan Willemsen4e0aa232019-04-10 22:59:54 -070063
64 outDir := android.PathForModuleGen(ctx, "yacc")
65 headerFile := android.GenPathWithExt(ctx, "yacc", yaccFile, "h")
66 ret := android.Paths{headerFile}
67
68 cmd := rule.Command()
69
70 // Fix up #line markers to not use the sbox temporary directory
Colin Crossf1a035e2020-11-16 17:32:30 -080071 // android.sboxPathForOutput(outDir, outDir) returns the sbox placeholder for the out
Colin Cross3d680512020-11-13 16:23:53 -080072 // directory itself, without any filename appended.
Colin Crossf1a035e2020-11-16 17:32:30 -080073 sboxOutDir := cmd.PathForOutput(outDir)
Colin Cross3d680512020-11-13 16:23:53 -080074 sedCmd := "sed -i.bak 's#" + sboxOutDir + "#" + outDir.String() + "#'"
Dan Willemsen4e0aa232019-04-10 22:59:54 -070075 rule.Command().Text(sedCmd).Input(outFile)
76 rule.Command().Text(sedCmd).Input(headerFile)
77
78 var flags []string
79 if props != nil {
80 flags = props.Flags
81
82 if Bool(props.Gen_location_hh) {
83 locationHeader := outFile.InSameDir(ctx, "location.hh")
84 ret = append(ret, locationHeader)
85 cmd.ImplicitOutput(locationHeader)
86 rule.Command().Text(sedCmd).Input(locationHeader)
87 }
88 if Bool(props.Gen_position_hh) {
89 positionHeader := outFile.InSameDir(ctx, "position.hh")
90 ret = append(ret, positionHeader)
91 cmd.ImplicitOutput(positionHeader)
92 rule.Command().Text(sedCmd).Input(positionHeader)
93 }
94 }
95
David Sudd18efd2020-07-24 17:19:23 +000096 cmd.Text("BISON_PKGDATADIR=prebuilts/build-tools/common/bison").
97 FlagWithInput("M4=", ctx.Config().PrebuiltBuildTool(ctx, "m4")).
98 PrebuiltBuildTool(ctx, "bison").
Dan Willemsen4e0aa232019-04-10 22:59:54 -070099 Flag("-d").
100 Flags(flags).
101 FlagWithOutput("--defines=", headerFile).
102 Flag("-o").Output(outFile).Input(yaccFile)
103
104 return ret
Colin Cross581c1892015-04-07 16:50:10 -0700105}
106
Jiyong Parkc7e592c2021-04-07 21:49:34 +0900107func 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 -0700108 aidlPackage := strings.TrimSuffix(aidlFile.Rel(), aidlFile.Base())
109 baseName := strings.TrimSuffix(aidlFile.Base(), aidlFile.Ext())
Daniel Norman10b74352019-09-24 17:39:58 -0700110 shortName := baseName
111 // TODO(b/111362593): aidl_to_cpp_common.cpp uses heuristics to figure out if
112 // an interface name has a leading I. Those same heuristics have been
113 // moved here.
114 if len(baseName) >= 2 && baseName[0] == 'I' &&
115 strings.ToUpper(baseName)[1] == baseName[1] {
116 shortName = strings.TrimPrefix(baseName, "I")
117 }
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700118
119 outDir := android.PathForModuleGen(ctx, "aidl")
Jiyong Parkc7e592c2021-04-07 21:49:34 +0900120 cppFile = outDir.Join(ctx, aidlPackage, baseName+".cpp")
121 depFile := outDir.Join(ctx, aidlPackage, baseName+".cpp.d")
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700122 headerI := outDir.Join(ctx, aidlPackage, baseName+".h")
123 headerBn := outDir.Join(ctx, aidlPackage, "Bn"+shortName+".h")
124 headerBp := outDir.Join(ctx, aidlPackage, "Bp"+shortName+".h")
125
Jiyong Park29074592019-07-07 16:27:47 +0900126 baseDir := strings.TrimSuffix(aidlFile.String(), aidlFile.Rel())
127 if baseDir != "" {
128 aidlFlags += " -I" + baseDir
129 }
130
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700131 cmd := rule.Command()
Colin Crossf1a035e2020-11-16 17:32:30 -0800132 cmd.BuiltTool("aidl-cpp").
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700133 FlagWithDepFile("-d", depFile).
134 Flag("--ninja").
135 Flag(aidlFlags).
136 Input(aidlFile).
137 OutputDir().
Jiyong Parkc7e592c2021-04-07 21:49:34 +0900138 Output(cppFile).
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700139 ImplicitOutputs(android.WritablePaths{
140 headerI,
141 headerBn,
142 headerBp,
143 })
144
Jiyong Parkc7e592c2021-04-07 21:49:34 +0900145 return cppFile, android.Paths{
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700146 headerI,
147 headerBn,
148 headerBp,
149 }
Dan Willemsene1240db2016-11-03 14:28:51 -0700150}
151
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200152type LexProperties struct {
153 // list of module-specific flags that will be used for .l and .ll compiles
154 Flags []string
155}
156
157func genLex(ctx android.ModuleContext, lexFile android.Path, outFile android.ModuleGenPath, props *LexProperties) {
158 var flags []string
159 if props != nil {
160 flags = props.Flags
161 }
162 flagsString := strings.Join(flags[:], " ")
David Sudd18efd2020-07-24 17:19:23 +0000163 ctx.Build(pctx, android.BuildParams{
164 Rule: lex,
165 Description: "lex " + lexFile.Rel(),
166 Output: outFile,
167 Input: lexFile,
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200168 Args: map[string]string{"flags": flagsString},
David Sudd18efd2020-07-24 17:19:23 +0000169 })
Colin Cross581c1892015-04-07 16:50:10 -0700170}
171
Inseob Kimed2641f2020-02-19 10:42:41 +0900172func genSysprop(ctx android.ModuleContext, syspropFile android.Path) (android.Path, android.Paths) {
Inseob Kim21f26902018-09-06 00:55:20 +0900173 headerFile := android.PathForModuleGen(ctx, "sysprop", "include", syspropFile.Rel()+".h")
Inseob Kim5cefbd22019-06-08 20:36:59 +0900174 publicHeaderFile := android.PathForModuleGen(ctx, "sysprop/public", "include", syspropFile.Rel()+".h")
Inseob Kim21f26902018-09-06 00:55:20 +0900175 cppFile := android.PathForModuleGen(ctx, "sysprop", syspropFile.Rel()+".cpp")
176
Inseob Kimed2641f2020-02-19 10:42:41 +0900177 headers := android.WritablePaths{headerFile, publicHeaderFile}
178
Inseob Kim21f26902018-09-06 00:55:20 +0900179 ctx.Build(pctx, android.BuildParams{
Inseob Kimed2641f2020-02-19 10:42:41 +0900180 Rule: sysprop,
181 Description: "sysprop " + syspropFile.Rel(),
182 Output: cppFile,
183 ImplicitOutputs: headers,
184 Input: syspropFile,
Inseob Kim21f26902018-09-06 00:55:20 +0900185 Args: map[string]string{
186 "headerOutDir": filepath.Dir(headerFile.String()),
Inseob Kim5cefbd22019-06-08 20:36:59 +0900187 "publicOutDir": filepath.Dir(publicHeaderFile.String()),
Inseob Kim21f26902018-09-06 00:55:20 +0900188 "srcOutDir": filepath.Dir(cppFile.String()),
189 "includeName": syspropFile.Rel() + ".h",
190 },
191 })
192
Inseob Kimed2641f2020-02-19 10:42:41 +0900193 return cppFile, headers.Paths()
Inseob Kim21f26902018-09-06 00:55:20 +0900194}
195
Paul Duffin33056e82021-02-19 13:49:08 +0000196// Used to communicate information from the genSources method back to the library code that uses
197// it.
198type generatedSourceInfo struct {
199 // The headers created from .proto files
200 protoHeaders android.Paths
201
202 // The files that can be used as order only dependencies in order to ensure that the proto header
203 // files are up to date.
204 protoOrderOnlyDeps android.Paths
205
206 // The headers created from .aidl files
207 aidlHeaders android.Paths
208
209 // The files that can be used as order only dependencies in order to ensure that the aidl header
210 // files are up to date.
211 aidlOrderOnlyDeps android.Paths
212
213 // The headers created from .sysprop files
214 syspropHeaders android.Paths
215
216 // The files that can be used as order only dependencies in order to ensure that the sysprop
217 // header files are up to date.
218 syspropOrderOnlyDeps android.Paths
219}
220
David Sudd18efd2020-07-24 17:19:23 +0000221func genSources(ctx android.ModuleContext, srcFiles android.Paths,
Paul Duffin33056e82021-02-19 13:49:08 +0000222 buildFlags builderFlags) (android.Paths, android.Paths, generatedSourceInfo) {
223
224 var info generatedSourceInfo
Colin Cross581c1892015-04-07 16:50:10 -0700225
Colin Cross635c3b02016-05-18 15:37:25 -0700226 var deps android.Paths
Colin Cross2a252be2017-05-01 17:37:24 -0700227 var rsFiles android.Paths
228
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700229 var aidlRule *android.RuleBuilder
230
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700231 var yaccRule_ *android.RuleBuilder
232 yaccRule := func() *android.RuleBuilder {
233 if yaccRule_ == nil {
Colin Crossf1a035e2020-11-16 17:32:30 -0800234 yaccRule_ = android.NewRuleBuilder(pctx, ctx).Sbox(android.PathForModuleGen(ctx, "yacc"),
Colin Crosse16ce362020-11-12 08:29:30 -0800235 android.PathForModuleGen(ctx, "yacc.sbox.textproto"))
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700236 }
237 return yaccRule_
238 }
239
Colin Cross581c1892015-04-07 16:50:10 -0700240 for i, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700241 switch srcFile.Ext() {
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800242 case ".y":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700243 cFile := android.GenPathWithExt(ctx, "yacc", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800244 srcFiles[i] = cFile
David Sudd18efd2020-07-24 17:19:23 +0000245 deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cFile, buildFlags.yacc)...)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800246 case ".yy":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700247 cppFile := android.GenPathWithExt(ctx, "yacc", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700248 srcFiles[i] = cppFile
David Sudd18efd2020-07-24 17:19:23 +0000249 deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cppFile, buildFlags.yacc)...)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800250 case ".l":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700251 cFile := android.GenPathWithExt(ctx, "lex", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800252 srcFiles[i] = cFile
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200253 genLex(ctx, srcFile, cFile, buildFlags.lex)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800254 case ".ll":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700255 cppFile := android.GenPathWithExt(ctx, "lex", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700256 srcFiles[i] = cppFile
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200257 genLex(ctx, srcFile, cppFile, buildFlags.lex)
Colin Cross0c461f12016-10-20 16:11:43 -0700258 case ".proto":
Dan Willemsen60e62f02018-11-16 21:05:32 -0800259 ccFile, headerFile := genProto(ctx, srcFile, buildFlags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700260 srcFiles[i] = ccFile
Paul Duffin33056e82021-02-19 13:49:08 +0000261 info.protoHeaders = append(info.protoHeaders, headerFile)
262 // Use the generated header as an order only dep to ensure that it is up to date when needed.
263 info.protoOrderOnlyDeps = append(info.protoOrderOnlyDeps, headerFile)
Dan Willemsene1240db2016-11-03 14:28:51 -0700264 case ".aidl":
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700265 if aidlRule == nil {
Colin Crossf1a035e2020-11-16 17:32:30 -0800266 aidlRule = android.NewRuleBuilder(pctx, ctx).Sbox(android.PathForModuleGen(ctx, "aidl"),
Colin Crosse16ce362020-11-12 08:29:30 -0800267 android.PathForModuleGen(ctx, "aidl.sbox.textproto"))
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700268 }
Jiyong Parkc7e592c2021-04-07 21:49:34 +0900269 cppFile, aidlHeaders := genAidl(ctx, aidlRule, srcFile, buildFlags.aidlFlags)
Dan Willemsene1240db2016-11-03 14:28:51 -0700270 srcFiles[i] = cppFile
Jiyong Parkc7e592c2021-04-07 21:49:34 +0900271
Paul Duffin33056e82021-02-19 13:49:08 +0000272 info.aidlHeaders = append(info.aidlHeaders, aidlHeaders...)
273 // Use the generated headers as order only deps to ensure that they are up to date when
274 // needed.
275 // TODO: Reduce the size of the ninja file by using one order only dep for the whole rule
276 info.aidlOrderOnlyDeps = append(info.aidlOrderOnlyDeps, aidlHeaders...)
Jeff Vander Stoepd6126272019-07-15 19:08:37 -0700277 case ".rscript", ".fs":
Colin Cross2a252be2017-05-01 17:37:24 -0700278 cppFile := rsGeneratedCppFile(ctx, srcFile)
279 rsFiles = append(rsFiles, srcFiles[i])
280 srcFiles[i] = cppFile
Inseob Kim21f26902018-09-06 00:55:20 +0900281 case ".sysprop":
Inseob Kimed2641f2020-02-19 10:42:41 +0900282 cppFile, headerFiles := genSysprop(ctx, srcFile)
Inseob Kim21f26902018-09-06 00:55:20 +0900283 srcFiles[i] = cppFile
Paul Duffin33056e82021-02-19 13:49:08 +0000284 info.syspropHeaders = append(info.syspropHeaders, headerFiles...)
285 // Use the generated headers as order only deps to ensure that they are up to date when
286 // needed.
287 info.syspropOrderOnlyDeps = append(info.syspropOrderOnlyDeps, headerFiles...)
Colin Cross581c1892015-04-07 16:50:10 -0700288 }
289 }
290
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700291 if aidlRule != nil {
Colin Crossf1a035e2020-11-16 17:32:30 -0800292 aidlRule.Build("aidl", "gen aidl")
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700293 }
294
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700295 if yaccRule_ != nil {
Colin Crossf1a035e2020-11-16 17:32:30 -0800296 yaccRule_.Build("yacc", "gen yacc")
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700297 }
298
Paul Duffin33056e82021-02-19 13:49:08 +0000299 deps = append(deps, info.protoOrderOnlyDeps...)
300 deps = append(deps, info.aidlOrderOnlyDeps...)
301 deps = append(deps, info.syspropOrderOnlyDeps...)
302
Colin Cross2a252be2017-05-01 17:37:24 -0700303 if len(rsFiles) > 0 {
304 deps = append(deps, rsGenerateCpp(ctx, rsFiles, buildFlags.rsFlags)...)
305 }
306
Paul Duffin33056e82021-02-19 13:49:08 +0000307 return srcFiles, deps, info
Colin Cross581c1892015-04-07 16:50:10 -0700308}