blob: edae061cef645637c91abff886f644997af475b4 [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 Willemsene1240db2016-11-03 14:28:51 -070027 pctx.HostBinToolVariable("aidlCmd", "aidl-cpp")
Inseob Kim21f26902018-09-06 00:55:20 +090028 pctx.HostBinToolVariable("syspropCmd", "sysprop_cpp")
Colin Cross581c1892015-04-07 16:50:10 -070029}
30
31var (
Inseob Kim21f26902018-09-06 00:55:20 +090032 sysprop = pctx.AndroidStaticRule("sysprop",
33 blueprint.RuleParams{
Inseob Kim5cefbd22019-06-08 20:36:59 +090034 Command: "$syspropCmd --header-dir=$headerOutDir --public-header-dir=$publicOutDir " +
Inseob Kimc0907f12019-02-08 21:00:45 +090035 "--source-dir=$srcOutDir --include-name=$includeName $in",
Inseob Kim21f26902018-09-06 00:55:20 +090036 CommandDeps: []string{"$syspropCmd"},
37 },
Inseob Kim5cefbd22019-06-08 20:36:59 +090038 "headerOutDir", "publicOutDir", "srcOutDir", "includeName")
Inseob Kim21f26902018-09-06 00:55:20 +090039
Dan Willemsen4f1c3d42017-09-09 01:15:26 -070040 windmc = pctx.AndroidStaticRule("windmc",
41 blueprint.RuleParams{
42 Command: "$windmcCmd -r$$(dirname $out) -h$$(dirname $out) $in",
43 CommandDeps: []string{"$windmcCmd"},
44 },
45 "windmcCmd")
Colin Cross581c1892015-04-07 16:50:10 -070046)
47
Dan Willemsen4e0aa232019-04-10 22:59:54 -070048type YaccProperties struct {
49 // list of module-specific flags that will be used for .y and .yy compiles
50 Flags []string
Colin Cross581c1892015-04-07 16:50:10 -070051
Dan Willemsen4e0aa232019-04-10 22:59:54 -070052 // whether the yacc files will produce a location.hh file
53 Gen_location_hh *bool
Colin Cross581c1892015-04-07 16:50:10 -070054
Dan Willemsen4e0aa232019-04-10 22:59:54 -070055 // whether the yacc files will product a position.hh file
56 Gen_position_hh *bool
57}
58
59func genYacc(ctx android.ModuleContext, rule *android.RuleBuilder, yaccFile android.Path,
Dan Willemsend2e291a2020-07-16 17:49:05 -070060 outFile android.ModuleGenPath, props *YaccProperties,
61 tools map[string]android.Path) (headerFiles android.Paths) {
Dan Willemsen4e0aa232019-04-10 22:59:54 -070062
63 outDir := android.PathForModuleGen(ctx, "yacc")
64 headerFile := android.GenPathWithExt(ctx, "yacc", yaccFile, "h")
65 ret := android.Paths{headerFile}
66
67 cmd := rule.Command()
68
69 // Fix up #line markers to not use the sbox temporary directory
70 sedCmd := "sed -i.bak 's#__SBOX_OUT_DIR__#" + outDir.String() + "#'"
71 rule.Command().Text(sedCmd).Input(outFile)
72 rule.Command().Text(sedCmd).Input(headerFile)
73
74 var flags []string
75 if props != nil {
76 flags = props.Flags
77
78 if Bool(props.Gen_location_hh) {
79 locationHeader := outFile.InSameDir(ctx, "location.hh")
80 ret = append(ret, locationHeader)
81 cmd.ImplicitOutput(locationHeader)
82 rule.Command().Text(sedCmd).Input(locationHeader)
83 }
84 if Bool(props.Gen_position_hh) {
85 positionHeader := outFile.InSameDir(ctx, "position.hh")
86 ret = append(ret, positionHeader)
87 cmd.ImplicitOutput(positionHeader)
88 rule.Command().Text(sedCmd).Input(positionHeader)
89 }
90 }
91
Dan Willemsend2e291a2020-07-16 17:49:05 -070092 bison, ok := tools["bison"]
93 if !ok {
94 ctx.ModuleErrorf("Unable to find bison")
95 }
96 m4, ok := tools["m4"]
97 if !ok {
98 ctx.ModuleErrorf("Unable to find m4")
99 }
100
101 cmd.FlagWithInput("M4=", m4).
102 Tool(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
Dan Willemsend2e291a2020-07-16 17:49:05 -0700156func genLex(ctx android.ModuleContext, rule *android.RuleBuilder, lexFile android.Path,
157 outFile android.ModuleGenPath, tools map[string]android.Path) {
158
159 flex, ok := tools["flex"]
160 if !ok {
161 ctx.ModuleErrorf("Unable to find flex")
162 }
163 m4, ok := tools["m4"]
164 if !ok {
165 ctx.ModuleErrorf("Unable to find m4")
166 }
167
168 rule.Command().
169 FlagWithInput("M4=", m4).
170 Tool(flex).
171 FlagWithOutput("-o", outFile).
172 Input(lexFile)
Colin Cross581c1892015-04-07 16:50:10 -0700173}
174
Inseob Kimed2641f2020-02-19 10:42:41 +0900175func genSysprop(ctx android.ModuleContext, syspropFile android.Path) (android.Path, android.Paths) {
Inseob Kim21f26902018-09-06 00:55:20 +0900176 headerFile := android.PathForModuleGen(ctx, "sysprop", "include", syspropFile.Rel()+".h")
Inseob Kim5cefbd22019-06-08 20:36:59 +0900177 publicHeaderFile := android.PathForModuleGen(ctx, "sysprop/public", "include", syspropFile.Rel()+".h")
Inseob Kim21f26902018-09-06 00:55:20 +0900178 cppFile := android.PathForModuleGen(ctx, "sysprop", syspropFile.Rel()+".cpp")
179
Inseob Kimed2641f2020-02-19 10:42:41 +0900180 headers := android.WritablePaths{headerFile, publicHeaderFile}
181
Inseob Kim21f26902018-09-06 00:55:20 +0900182 ctx.Build(pctx, android.BuildParams{
Inseob Kimed2641f2020-02-19 10:42:41 +0900183 Rule: sysprop,
184 Description: "sysprop " + syspropFile.Rel(),
185 Output: cppFile,
186 ImplicitOutputs: headers,
187 Input: syspropFile,
Inseob Kim21f26902018-09-06 00:55:20 +0900188 Args: map[string]string{
189 "headerOutDir": filepath.Dir(headerFile.String()),
Inseob Kim5cefbd22019-06-08 20:36:59 +0900190 "publicOutDir": filepath.Dir(publicHeaderFile.String()),
Inseob Kim21f26902018-09-06 00:55:20 +0900191 "srcOutDir": filepath.Dir(cppFile.String()),
192 "includeName": syspropFile.Rel() + ".h",
193 },
194 })
195
Inseob Kimed2641f2020-02-19 10:42:41 +0900196 return cppFile, headers.Paths()
Inseob Kim21f26902018-09-06 00:55:20 +0900197}
198
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700199func genWinMsg(ctx android.ModuleContext, srcFile android.Path, flags builderFlags) (android.Path, android.Path) {
200 headerFile := android.GenPathWithExt(ctx, "windmc", srcFile, "h")
201 rcFile := android.GenPathWithExt(ctx, "windmc", srcFile, "rc")
202
203 windmcCmd := gccCmd(flags.toolchain, "windmc")
204
Colin Crossae887032017-10-23 17:16:14 -0700205 ctx.Build(pctx, android.BuildParams{
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700206 Rule: windmc,
207 Description: "windmc " + srcFile.Rel(),
208 Output: rcFile,
209 ImplicitOutput: headerFile,
210 Input: srcFile,
211 Args: map[string]string{
212 "windmcCmd": windmcCmd,
213 },
214 })
215
216 return rcFile, headerFile
217}
218
Dan Willemsend2e291a2020-07-16 17:49:05 -0700219func genSources(ctx android.ModuleContext, srcFiles android.Paths, buildFlags builderFlags,
220 tools map[string]android.Path) (android.Paths, android.Paths) {
Colin Cross581c1892015-04-07 16:50:10 -0700221
Colin Cross635c3b02016-05-18 15:37:25 -0700222 var deps android.Paths
Colin Cross2a252be2017-05-01 17:37:24 -0700223 var rsFiles android.Paths
224
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700225 var aidlRule *android.RuleBuilder
226
Dan Willemsend2e291a2020-07-16 17:49:05 -0700227 var lexRule_ *android.RuleBuilder
228 lexRule := func() *android.RuleBuilder {
229 if lexRule_ == nil {
230 lexRule_ = android.NewRuleBuilder().Sbox(android.PathForModuleGen(ctx, "lex"))
231 }
232 return lexRule_
233 }
234
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700235 var yaccRule_ *android.RuleBuilder
236 yaccRule := func() *android.RuleBuilder {
237 if yaccRule_ == nil {
238 yaccRule_ = android.NewRuleBuilder().Sbox(android.PathForModuleGen(ctx, "yacc"))
239 }
240 return yaccRule_
241 }
242
Colin Cross581c1892015-04-07 16:50:10 -0700243 for i, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700244 switch srcFile.Ext() {
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800245 case ".y":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700246 cFile := android.GenPathWithExt(ctx, "yacc", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800247 srcFiles[i] = cFile
Dan Willemsend2e291a2020-07-16 17:49:05 -0700248 deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cFile, buildFlags.yacc, tools)...)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800249 case ".yy":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700250 cppFile := android.GenPathWithExt(ctx, "yacc", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700251 srcFiles[i] = cppFile
Dan Willemsend2e291a2020-07-16 17:49:05 -0700252 deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cppFile, buildFlags.yacc, tools)...)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800253 case ".l":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700254 cFile := android.GenPathWithExt(ctx, "lex", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800255 srcFiles[i] = cFile
Dan Willemsend2e291a2020-07-16 17:49:05 -0700256 genLex(ctx, lexRule(), srcFile, cFile, tools)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800257 case ".ll":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700258 cppFile := android.GenPathWithExt(ctx, "lex", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700259 srcFiles[i] = cppFile
Dan Willemsend2e291a2020-07-16 17:49:05 -0700260 genLex(ctx, lexRule(), srcFile, cppFile, tools)
Colin Cross0c461f12016-10-20 16:11:43 -0700261 case ".proto":
Dan Willemsen60e62f02018-11-16 21:05:32 -0800262 ccFile, headerFile := genProto(ctx, srcFile, buildFlags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700263 srcFiles[i] = ccFile
264 deps = append(deps, headerFile)
Dan Willemsene1240db2016-11-03 14:28:51 -0700265 case ".aidl":
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700266 if aidlRule == nil {
267 aidlRule = android.NewRuleBuilder().Sbox(android.PathForModuleGen(ctx, "aidl"))
268 }
Dan Willemsene1240db2016-11-03 14:28:51 -0700269 cppFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp")
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700270 depFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp.d")
Dan Willemsene1240db2016-11-03 14:28:51 -0700271 srcFiles[i] = cppFile
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700272 deps = append(deps, genAidl(ctx, aidlRule, srcFile, cppFile, depFile, buildFlags.aidlFlags)...)
Jeff Vander Stoepd6126272019-07-15 19:08:37 -0700273 case ".rscript", ".fs":
Colin Cross2a252be2017-05-01 17:37:24 -0700274 cppFile := rsGeneratedCppFile(ctx, srcFile)
275 rsFiles = append(rsFiles, srcFiles[i])
276 srcFiles[i] = cppFile
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700277 case ".mc":
278 rcFile, headerFile := genWinMsg(ctx, srcFile, buildFlags)
279 srcFiles[i] = rcFile
280 deps = append(deps, headerFile)
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
Inseob Kimed2641f2020-02-19 10:42:41 +0900284 deps = append(deps, headerFiles...)
Colin Cross581c1892015-04-07 16:50:10 -0700285 }
286 }
287
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700288 if aidlRule != nil {
289 aidlRule.Build(pctx, ctx, "aidl", "gen aidl")
290 }
291
Dan Willemsend2e291a2020-07-16 17:49:05 -0700292 if lexRule_ != nil {
293 lexRule_.Build(pctx, ctx, "lex", "gen lex")
294 }
295
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700296 if yaccRule_ != nil {
297 yaccRule_.Build(pctx, ctx, "yacc", "gen yacc")
298 }
299
Colin Cross2a252be2017-05-01 17:37:24 -0700300 if len(rsFiles) > 0 {
301 deps = append(deps, rsGenerateCpp(ctx, rsFiles, buildFlags.rsFlags)...)
302 }
303
Colin Cross581c1892015-04-07 16:50:10 -0700304 return srcFiles, deps
305}