blob: ccc3d0ef9894561f31889211c0f37a369f6a2829 [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
78 sedCmd := "sed -i.bak 's#__SBOX_OUT_DIR__#" + outDir.String() + "#'"
79 rule.Command().Text(sedCmd).Input(outFile)
80 rule.Command().Text(sedCmd).Input(headerFile)
81
82 var flags []string
83 if props != nil {
84 flags = props.Flags
85
86 if Bool(props.Gen_location_hh) {
87 locationHeader := outFile.InSameDir(ctx, "location.hh")
88 ret = append(ret, locationHeader)
89 cmd.ImplicitOutput(locationHeader)
90 rule.Command().Text(sedCmd).Input(locationHeader)
91 }
92 if Bool(props.Gen_position_hh) {
93 positionHeader := outFile.InSameDir(ctx, "position.hh")
94 ret = append(ret, positionHeader)
95 cmd.ImplicitOutput(positionHeader)
96 rule.Command().Text(sedCmd).Input(positionHeader)
97 }
98 }
99
David Sudd18efd2020-07-24 17:19:23 +0000100 cmd.Text("BISON_PKGDATADIR=prebuilts/build-tools/common/bison").
101 FlagWithInput("M4=", ctx.Config().PrebuiltBuildTool(ctx, "m4")).
102 PrebuiltBuildTool(ctx, "bison").
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700103 Flag("-d").
104 Flags(flags).
105 FlagWithOutput("--defines=", headerFile).
106 Flag("-o").Output(outFile).Input(yaccFile)
107
108 return ret
Colin Cross581c1892015-04-07 16:50:10 -0700109}
110
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700111func genAidl(ctx android.ModuleContext, rule *android.RuleBuilder, aidlFile android.Path,
112 outFile, depFile android.ModuleGenPath, aidlFlags string) android.Paths {
Dan Willemsene1240db2016-11-03 14:28:51 -0700113
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700114 aidlPackage := strings.TrimSuffix(aidlFile.Rel(), aidlFile.Base())
115 baseName := strings.TrimSuffix(aidlFile.Base(), aidlFile.Ext())
Daniel Norman10b74352019-09-24 17:39:58 -0700116 shortName := baseName
117 // TODO(b/111362593): aidl_to_cpp_common.cpp uses heuristics to figure out if
118 // an interface name has a leading I. Those same heuristics have been
119 // moved here.
120 if len(baseName) >= 2 && baseName[0] == 'I' &&
121 strings.ToUpper(baseName)[1] == baseName[1] {
122 shortName = strings.TrimPrefix(baseName, "I")
123 }
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700124
125 outDir := android.PathForModuleGen(ctx, "aidl")
126 headerI := outDir.Join(ctx, aidlPackage, baseName+".h")
127 headerBn := outDir.Join(ctx, aidlPackage, "Bn"+shortName+".h")
128 headerBp := outDir.Join(ctx, aidlPackage, "Bp"+shortName+".h")
129
Jiyong Park29074592019-07-07 16:27:47 +0900130 baseDir := strings.TrimSuffix(aidlFile.String(), aidlFile.Rel())
131 if baseDir != "" {
132 aidlFlags += " -I" + baseDir
133 }
134
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700135 cmd := rule.Command()
Colin Crossee94d6a2019-07-08 17:08:34 -0700136 cmd.BuiltTool(ctx, "aidl-cpp").
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700137 FlagWithDepFile("-d", depFile).
138 Flag("--ninja").
139 Flag(aidlFlags).
140 Input(aidlFile).
141 OutputDir().
142 Output(outFile).
143 ImplicitOutputs(android.WritablePaths{
144 headerI,
145 headerBn,
146 headerBp,
147 })
148
149 return android.Paths{
150 headerI,
151 headerBn,
152 headerBp,
153 }
Dan Willemsene1240db2016-11-03 14:28:51 -0700154}
155
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200156type LexProperties struct {
157 // list of module-specific flags that will be used for .l and .ll compiles
158 Flags []string
159}
160
161func genLex(ctx android.ModuleContext, lexFile android.Path, outFile android.ModuleGenPath, props *LexProperties) {
162 var flags []string
163 if props != nil {
164 flags = props.Flags
165 }
166 flagsString := strings.Join(flags[:], " ")
David Sudd18efd2020-07-24 17:19:23 +0000167 ctx.Build(pctx, android.BuildParams{
168 Rule: lex,
169 Description: "lex " + lexFile.Rel(),
170 Output: outFile,
171 Input: lexFile,
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200172 Args: map[string]string{"flags": flagsString},
David Sudd18efd2020-07-24 17:19:23 +0000173 })
Colin Cross581c1892015-04-07 16:50:10 -0700174}
175
Inseob Kimed2641f2020-02-19 10:42:41 +0900176func genSysprop(ctx android.ModuleContext, syspropFile android.Path) (android.Path, android.Paths) {
Inseob Kim21f26902018-09-06 00:55:20 +0900177 headerFile := android.PathForModuleGen(ctx, "sysprop", "include", syspropFile.Rel()+".h")
Inseob Kim5cefbd22019-06-08 20:36:59 +0900178 publicHeaderFile := android.PathForModuleGen(ctx, "sysprop/public", "include", syspropFile.Rel()+".h")
Inseob Kim21f26902018-09-06 00:55:20 +0900179 cppFile := android.PathForModuleGen(ctx, "sysprop", syspropFile.Rel()+".cpp")
180
Inseob Kimed2641f2020-02-19 10:42:41 +0900181 headers := android.WritablePaths{headerFile, publicHeaderFile}
182
Inseob Kim21f26902018-09-06 00:55:20 +0900183 ctx.Build(pctx, android.BuildParams{
Inseob Kimed2641f2020-02-19 10:42:41 +0900184 Rule: sysprop,
185 Description: "sysprop " + syspropFile.Rel(),
186 Output: cppFile,
187 ImplicitOutputs: headers,
188 Input: syspropFile,
Inseob Kim21f26902018-09-06 00:55:20 +0900189 Args: map[string]string{
190 "headerOutDir": filepath.Dir(headerFile.String()),
Inseob Kim5cefbd22019-06-08 20:36:59 +0900191 "publicOutDir": filepath.Dir(publicHeaderFile.String()),
Inseob Kim21f26902018-09-06 00:55:20 +0900192 "srcOutDir": filepath.Dir(cppFile.String()),
193 "includeName": syspropFile.Rel() + ".h",
194 },
195 })
196
Inseob Kimed2641f2020-02-19 10:42:41 +0900197 return cppFile, headers.Paths()
Inseob Kim21f26902018-09-06 00:55:20 +0900198}
199
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700200func genWinMsg(ctx android.ModuleContext, srcFile android.Path, flags builderFlags) (android.Path, android.Path) {
201 headerFile := android.GenPathWithExt(ctx, "windmc", srcFile, "h")
202 rcFile := android.GenPathWithExt(ctx, "windmc", srcFile, "rc")
203
204 windmcCmd := gccCmd(flags.toolchain, "windmc")
205
Colin Crossae887032017-10-23 17:16:14 -0700206 ctx.Build(pctx, android.BuildParams{
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700207 Rule: windmc,
208 Description: "windmc " + srcFile.Rel(),
209 Output: rcFile,
210 ImplicitOutput: headerFile,
211 Input: srcFile,
212 Args: map[string]string{
213 "windmcCmd": windmcCmd,
214 },
215 })
216
217 return rcFile, headerFile
218}
219
David Sudd18efd2020-07-24 17:19:23 +0000220func genSources(ctx android.ModuleContext, srcFiles android.Paths,
221 buildFlags builderFlags) (android.Paths, android.Paths) {
Colin Cross581c1892015-04-07 16:50:10 -0700222
Colin Cross635c3b02016-05-18 15:37:25 -0700223 var deps android.Paths
Colin Cross2a252be2017-05-01 17:37:24 -0700224 var rsFiles android.Paths
225
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700226 var aidlRule *android.RuleBuilder
227
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700228 var yaccRule_ *android.RuleBuilder
229 yaccRule := func() *android.RuleBuilder {
230 if yaccRule_ == nil {
231 yaccRule_ = android.NewRuleBuilder().Sbox(android.PathForModuleGen(ctx, "yacc"))
232 }
233 return yaccRule_
234 }
235
Colin Cross581c1892015-04-07 16:50:10 -0700236 for i, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700237 switch srcFile.Ext() {
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800238 case ".y":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700239 cFile := android.GenPathWithExt(ctx, "yacc", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800240 srcFiles[i] = cFile
David Sudd18efd2020-07-24 17:19:23 +0000241 deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cFile, buildFlags.yacc)...)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800242 case ".yy":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700243 cppFile := android.GenPathWithExt(ctx, "yacc", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700244 srcFiles[i] = cppFile
David Sudd18efd2020-07-24 17:19:23 +0000245 deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cppFile, buildFlags.yacc)...)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800246 case ".l":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700247 cFile := android.GenPathWithExt(ctx, "lex", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800248 srcFiles[i] = cFile
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200249 genLex(ctx, srcFile, cFile, buildFlags.lex)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800250 case ".ll":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700251 cppFile := android.GenPathWithExt(ctx, "lex", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700252 srcFiles[i] = cppFile
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200253 genLex(ctx, srcFile, cppFile, buildFlags.lex)
Colin Cross0c461f12016-10-20 16:11:43 -0700254 case ".proto":
Dan Willemsen60e62f02018-11-16 21:05:32 -0800255 ccFile, headerFile := genProto(ctx, srcFile, buildFlags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700256 srcFiles[i] = ccFile
257 deps = append(deps, headerFile)
Dan Willemsene1240db2016-11-03 14:28:51 -0700258 case ".aidl":
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700259 if aidlRule == nil {
260 aidlRule = android.NewRuleBuilder().Sbox(android.PathForModuleGen(ctx, "aidl"))
261 }
Dan Willemsene1240db2016-11-03 14:28:51 -0700262 cppFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp")
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700263 depFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp.d")
Dan Willemsene1240db2016-11-03 14:28:51 -0700264 srcFiles[i] = cppFile
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700265 deps = append(deps, genAidl(ctx, aidlRule, srcFile, cppFile, depFile, buildFlags.aidlFlags)...)
Jeff Vander Stoepd6126272019-07-15 19:08:37 -0700266 case ".rscript", ".fs":
Colin Cross2a252be2017-05-01 17:37:24 -0700267 cppFile := rsGeneratedCppFile(ctx, srcFile)
268 rsFiles = append(rsFiles, srcFiles[i])
269 srcFiles[i] = cppFile
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700270 case ".mc":
271 rcFile, headerFile := genWinMsg(ctx, srcFile, buildFlags)
272 srcFiles[i] = rcFile
273 deps = append(deps, headerFile)
Inseob Kim21f26902018-09-06 00:55:20 +0900274 case ".sysprop":
Inseob Kimed2641f2020-02-19 10:42:41 +0900275 cppFile, headerFiles := genSysprop(ctx, srcFile)
Inseob Kim21f26902018-09-06 00:55:20 +0900276 srcFiles[i] = cppFile
Inseob Kimed2641f2020-02-19 10:42:41 +0900277 deps = append(deps, headerFiles...)
Colin Cross581c1892015-04-07 16:50:10 -0700278 }
279 }
280
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700281 if aidlRule != nil {
282 aidlRule.Build(pctx, ctx, "aidl", "gen aidl")
283 }
284
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700285 if yaccRule_ != nil {
286 yaccRule_.Build(pctx, ctx, "yacc", "gen yacc")
287 }
288
Colin Cross2a252be2017-05-01 17:37:24 -0700289 if len(rsFiles) > 0 {
290 deps = append(deps, rsGenerateCpp(ctx, rsFiles, buildFlags.rsFlags)...)
291 }
292
Colin Cross581c1892015-04-07 16:50:10 -0700293 return srcFiles, deps
294}