blob: 6f9036bd9c8a131b1fdf2dfc4c76845aa7c33e15 [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() {
Dan Willemsen613564e2020-07-23 21:37:35 +000027 pctx.SourcePathVariable("m4Cmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/m4")
28
Dan Willemsene1240db2016-11-03 14:28:51 -070029 pctx.HostBinToolVariable("aidlCmd", "aidl-cpp")
Inseob Kim21f26902018-09-06 00:55:20 +090030 pctx.HostBinToolVariable("syspropCmd", "sysprop_cpp")
Colin Cross581c1892015-04-07 16:50:10 -070031}
32
33var (
Inseob Kim21f26902018-09-06 00:55:20 +090034 sysprop = pctx.AndroidStaticRule("sysprop",
35 blueprint.RuleParams{
Inseob Kim5cefbd22019-06-08 20:36:59 +090036 Command: "$syspropCmd --header-dir=$headerOutDir --public-header-dir=$publicOutDir " +
Inseob Kimc0907f12019-02-08 21:00:45 +090037 "--source-dir=$srcOutDir --include-name=$includeName $in",
Inseob Kim21f26902018-09-06 00:55:20 +090038 CommandDeps: []string{"$syspropCmd"},
39 },
Inseob Kim5cefbd22019-06-08 20:36:59 +090040 "headerOutDir", "publicOutDir", "srcOutDir", "includeName")
Inseob Kim21f26902018-09-06 00:55:20 +090041
Dan Willemsen4f1c3d42017-09-09 01:15:26 -070042 windmc = pctx.AndroidStaticRule("windmc",
43 blueprint.RuleParams{
44 Command: "$windmcCmd -r$$(dirname $out) -h$$(dirname $out) $in",
45 CommandDeps: []string{"$windmcCmd"},
46 },
47 "windmcCmd")
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,
Dan Willemsend2e291a2020-07-16 17:49:05 -070062 outFile android.ModuleGenPath, props *YaccProperties,
63 tools map[string]android.Path) (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
72 sedCmd := "sed -i.bak 's#__SBOX_OUT_DIR__#" + outDir.String() + "#'"
73 rule.Command().Text(sedCmd).Input(outFile)
74 rule.Command().Text(sedCmd).Input(headerFile)
75
76 var flags []string
77 if props != nil {
78 flags = props.Flags
79
80 if Bool(props.Gen_location_hh) {
81 locationHeader := outFile.InSameDir(ctx, "location.hh")
82 ret = append(ret, locationHeader)
83 cmd.ImplicitOutput(locationHeader)
84 rule.Command().Text(sedCmd).Input(locationHeader)
85 }
86 if Bool(props.Gen_position_hh) {
87 positionHeader := outFile.InSameDir(ctx, "position.hh")
88 ret = append(ret, positionHeader)
89 cmd.ImplicitOutput(positionHeader)
90 rule.Command().Text(sedCmd).Input(positionHeader)
91 }
92 }
93
Dan Willemsend2e291a2020-07-16 17:49:05 -070094 bison, ok := tools["bison"]
95 if !ok {
96 ctx.ModuleErrorf("Unable to find bison")
97 }
98 m4, ok := tools["m4"]
99 if !ok {
100 ctx.ModuleErrorf("Unable to find m4")
101 }
102
103 cmd.FlagWithInput("M4=", m4).
104 Tool(bison).
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700105 Flag("-d").
106 Flags(flags).
107 FlagWithOutput("--defines=", headerFile).
108 Flag("-o").Output(outFile).Input(yaccFile)
109
110 return ret
Colin Cross581c1892015-04-07 16:50:10 -0700111}
112
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700113func genAidl(ctx android.ModuleContext, rule *android.RuleBuilder, aidlFile android.Path,
114 outFile, depFile android.ModuleGenPath, aidlFlags string) android.Paths {
Dan Willemsene1240db2016-11-03 14:28:51 -0700115
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700116 aidlPackage := strings.TrimSuffix(aidlFile.Rel(), aidlFile.Base())
117 baseName := strings.TrimSuffix(aidlFile.Base(), aidlFile.Ext())
Daniel Norman10b74352019-09-24 17:39:58 -0700118 shortName := baseName
119 // TODO(b/111362593): aidl_to_cpp_common.cpp uses heuristics to figure out if
120 // an interface name has a leading I. Those same heuristics have been
121 // moved here.
122 if len(baseName) >= 2 && baseName[0] == 'I' &&
123 strings.ToUpper(baseName)[1] == baseName[1] {
124 shortName = strings.TrimPrefix(baseName, "I")
125 }
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700126
127 outDir := android.PathForModuleGen(ctx, "aidl")
128 headerI := outDir.Join(ctx, aidlPackage, baseName+".h")
129 headerBn := outDir.Join(ctx, aidlPackage, "Bn"+shortName+".h")
130 headerBp := outDir.Join(ctx, aidlPackage, "Bp"+shortName+".h")
131
Jiyong Park29074592019-07-07 16:27:47 +0900132 baseDir := strings.TrimSuffix(aidlFile.String(), aidlFile.Rel())
133 if baseDir != "" {
134 aidlFlags += " -I" + baseDir
135 }
136
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700137 cmd := rule.Command()
Colin Crossee94d6a2019-07-08 17:08:34 -0700138 cmd.BuiltTool(ctx, "aidl-cpp").
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700139 FlagWithDepFile("-d", depFile).
140 Flag("--ninja").
141 Flag(aidlFlags).
142 Input(aidlFile).
143 OutputDir().
144 Output(outFile).
145 ImplicitOutputs(android.WritablePaths{
146 headerI,
147 headerBn,
148 headerBp,
149 })
150
151 return android.Paths{
152 headerI,
153 headerBn,
154 headerBp,
155 }
Dan Willemsene1240db2016-11-03 14:28:51 -0700156}
157
Dan Willemsend2e291a2020-07-16 17:49:05 -0700158func genLex(ctx android.ModuleContext, rule *android.RuleBuilder, lexFile android.Path,
159 outFile android.ModuleGenPath, tools map[string]android.Path) {
160
161 flex, ok := tools["flex"]
162 if !ok {
163 ctx.ModuleErrorf("Unable to find flex")
164 }
165 m4, ok := tools["m4"]
166 if !ok {
167 ctx.ModuleErrorf("Unable to find m4")
168 }
169
170 rule.Command().
171 FlagWithInput("M4=", m4).
172 Tool(flex).
173 FlagWithOutput("-o", outFile).
174 Input(lexFile)
Colin Cross581c1892015-04-07 16:50:10 -0700175}
176
Inseob Kimed2641f2020-02-19 10:42:41 +0900177func genSysprop(ctx android.ModuleContext, syspropFile android.Path) (android.Path, android.Paths) {
Inseob Kim21f26902018-09-06 00:55:20 +0900178 headerFile := android.PathForModuleGen(ctx, "sysprop", "include", syspropFile.Rel()+".h")
Inseob Kim5cefbd22019-06-08 20:36:59 +0900179 publicHeaderFile := android.PathForModuleGen(ctx, "sysprop/public", "include", syspropFile.Rel()+".h")
Inseob Kim21f26902018-09-06 00:55:20 +0900180 cppFile := android.PathForModuleGen(ctx, "sysprop", syspropFile.Rel()+".cpp")
181
Inseob Kimed2641f2020-02-19 10:42:41 +0900182 headers := android.WritablePaths{headerFile, publicHeaderFile}
183
Inseob Kim21f26902018-09-06 00:55:20 +0900184 ctx.Build(pctx, android.BuildParams{
Inseob Kimed2641f2020-02-19 10:42:41 +0900185 Rule: sysprop,
186 Description: "sysprop " + syspropFile.Rel(),
187 Output: cppFile,
188 ImplicitOutputs: headers,
189 Input: syspropFile,
Inseob Kim21f26902018-09-06 00:55:20 +0900190 Args: map[string]string{
191 "headerOutDir": filepath.Dir(headerFile.String()),
Inseob Kim5cefbd22019-06-08 20:36:59 +0900192 "publicOutDir": filepath.Dir(publicHeaderFile.String()),
Inseob Kim21f26902018-09-06 00:55:20 +0900193 "srcOutDir": filepath.Dir(cppFile.String()),
194 "includeName": syspropFile.Rel() + ".h",
195 },
196 })
197
Inseob Kimed2641f2020-02-19 10:42:41 +0900198 return cppFile, headers.Paths()
Inseob Kim21f26902018-09-06 00:55:20 +0900199}
200
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700201func genWinMsg(ctx android.ModuleContext, srcFile android.Path, flags builderFlags) (android.Path, android.Path) {
202 headerFile := android.GenPathWithExt(ctx, "windmc", srcFile, "h")
203 rcFile := android.GenPathWithExt(ctx, "windmc", srcFile, "rc")
204
205 windmcCmd := gccCmd(flags.toolchain, "windmc")
206
Colin Crossae887032017-10-23 17:16:14 -0700207 ctx.Build(pctx, android.BuildParams{
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700208 Rule: windmc,
209 Description: "windmc " + srcFile.Rel(),
210 Output: rcFile,
211 ImplicitOutput: headerFile,
212 Input: srcFile,
213 Args: map[string]string{
214 "windmcCmd": windmcCmd,
215 },
216 })
217
218 return rcFile, headerFile
219}
220
Dan Willemsend2e291a2020-07-16 17:49:05 -0700221func genSources(ctx android.ModuleContext, srcFiles android.Paths, buildFlags builderFlags,
222 tools map[string]android.Path) (android.Paths, android.Paths) {
Colin Cross581c1892015-04-07 16:50:10 -0700223
Colin Cross635c3b02016-05-18 15:37:25 -0700224 var deps android.Paths
Colin Cross2a252be2017-05-01 17:37:24 -0700225 var rsFiles android.Paths
226
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700227 var aidlRule *android.RuleBuilder
228
Dan Willemsend2e291a2020-07-16 17:49:05 -0700229 var lexRule_ *android.RuleBuilder
230 lexRule := func() *android.RuleBuilder {
231 if lexRule_ == nil {
232 lexRule_ = android.NewRuleBuilder().Sbox(android.PathForModuleGen(ctx, "lex"))
233 }
234 return lexRule_
235 }
236
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700237 var yaccRule_ *android.RuleBuilder
238 yaccRule := func() *android.RuleBuilder {
239 if yaccRule_ == nil {
240 yaccRule_ = android.NewRuleBuilder().Sbox(android.PathForModuleGen(ctx, "yacc"))
241 }
242 return yaccRule_
243 }
244
Colin Cross581c1892015-04-07 16:50:10 -0700245 for i, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700246 switch srcFile.Ext() {
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800247 case ".y":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700248 cFile := android.GenPathWithExt(ctx, "yacc", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800249 srcFiles[i] = cFile
Dan Willemsend2e291a2020-07-16 17:49:05 -0700250 deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cFile, buildFlags.yacc, tools)...)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800251 case ".yy":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700252 cppFile := android.GenPathWithExt(ctx, "yacc", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700253 srcFiles[i] = cppFile
Dan Willemsend2e291a2020-07-16 17:49:05 -0700254 deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cppFile, buildFlags.yacc, tools)...)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800255 case ".l":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700256 cFile := android.GenPathWithExt(ctx, "lex", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800257 srcFiles[i] = cFile
Dan Willemsend2e291a2020-07-16 17:49:05 -0700258 genLex(ctx, lexRule(), srcFile, cFile, tools)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800259 case ".ll":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700260 cppFile := android.GenPathWithExt(ctx, "lex", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700261 srcFiles[i] = cppFile
Dan Willemsend2e291a2020-07-16 17:49:05 -0700262 genLex(ctx, lexRule(), srcFile, cppFile, tools)
Colin Cross0c461f12016-10-20 16:11:43 -0700263 case ".proto":
Dan Willemsen60e62f02018-11-16 21:05:32 -0800264 ccFile, headerFile := genProto(ctx, srcFile, buildFlags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700265 srcFiles[i] = ccFile
266 deps = append(deps, headerFile)
Dan Willemsene1240db2016-11-03 14:28:51 -0700267 case ".aidl":
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700268 if aidlRule == nil {
269 aidlRule = android.NewRuleBuilder().Sbox(android.PathForModuleGen(ctx, "aidl"))
270 }
Dan Willemsene1240db2016-11-03 14:28:51 -0700271 cppFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp")
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700272 depFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp.d")
Dan Willemsene1240db2016-11-03 14:28:51 -0700273 srcFiles[i] = cppFile
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700274 deps = append(deps, genAidl(ctx, aidlRule, srcFile, cppFile, depFile, buildFlags.aidlFlags)...)
Jeff Vander Stoepd6126272019-07-15 19:08:37 -0700275 case ".rscript", ".fs":
Colin Cross2a252be2017-05-01 17:37:24 -0700276 cppFile := rsGeneratedCppFile(ctx, srcFile)
277 rsFiles = append(rsFiles, srcFiles[i])
278 srcFiles[i] = cppFile
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700279 case ".mc":
280 rcFile, headerFile := genWinMsg(ctx, srcFile, buildFlags)
281 srcFiles[i] = rcFile
282 deps = append(deps, headerFile)
Inseob Kim21f26902018-09-06 00:55:20 +0900283 case ".sysprop":
Inseob Kimed2641f2020-02-19 10:42:41 +0900284 cppFile, headerFiles := genSysprop(ctx, srcFile)
Inseob Kim21f26902018-09-06 00:55:20 +0900285 srcFiles[i] = cppFile
Inseob Kimed2641f2020-02-19 10:42:41 +0900286 deps = append(deps, headerFiles...)
Colin Cross581c1892015-04-07 16:50:10 -0700287 }
288 }
289
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700290 if aidlRule != nil {
291 aidlRule.Build(pctx, ctx, "aidl", "gen aidl")
292 }
293
Dan Willemsend2e291a2020-07-16 17:49:05 -0700294 if lexRule_ != nil {
295 lexRule_.Build(pctx, ctx, "lex", "gen lex")
296 }
297
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700298 if yaccRule_ != nil {
299 yaccRule_.Build(pctx, ctx, "yacc", "gen yacc")
300 }
301
Colin Cross2a252be2017-05-01 17:37:24 -0700302 if len(rsFiles) > 0 {
303 deps = append(deps, rsGenerateCpp(ctx, rsFiles, buildFlags.rsFlags)...)
304 }
305
Colin Cross581c1892015-04-07 16:50:10 -0700306 return srcFiles, deps
307}