blob: 134d6d9fce08d8e9b850d1563defba383bb3ac56 [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
Colin Cross3d680512020-11-13 16:23:53 -080078 // android.SboxPathForOutput(outDir, outDir) returns the sbox placeholder for the out
79 // directory itself, without any filename appended.
80 // TODO(ccross): make this cmd.PathForOutput(outDir) instead.
81 sboxOutDir := android.SboxPathForOutput(outDir, outDir)
82 sedCmd := "sed -i.bak 's#" + sboxOutDir + "#" + outDir.String() + "#'"
Dan Willemsen4e0aa232019-04-10 22:59:54 -070083 rule.Command().Text(sedCmd).Input(outFile)
84 rule.Command().Text(sedCmd).Input(headerFile)
85
86 var flags []string
87 if props != nil {
88 flags = props.Flags
89
90 if Bool(props.Gen_location_hh) {
91 locationHeader := outFile.InSameDir(ctx, "location.hh")
92 ret = append(ret, locationHeader)
93 cmd.ImplicitOutput(locationHeader)
94 rule.Command().Text(sedCmd).Input(locationHeader)
95 }
96 if Bool(props.Gen_position_hh) {
97 positionHeader := outFile.InSameDir(ctx, "position.hh")
98 ret = append(ret, positionHeader)
99 cmd.ImplicitOutput(positionHeader)
100 rule.Command().Text(sedCmd).Input(positionHeader)
101 }
102 }
103
David Sudd18efd2020-07-24 17:19:23 +0000104 cmd.Text("BISON_PKGDATADIR=prebuilts/build-tools/common/bison").
105 FlagWithInput("M4=", ctx.Config().PrebuiltBuildTool(ctx, "m4")).
106 PrebuiltBuildTool(ctx, "bison").
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700107 Flag("-d").
108 Flags(flags).
109 FlagWithOutput("--defines=", headerFile).
110 Flag("-o").Output(outFile).Input(yaccFile)
111
112 return ret
Colin Cross581c1892015-04-07 16:50:10 -0700113}
114
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700115func genAidl(ctx android.ModuleContext, rule *android.RuleBuilder, aidlFile android.Path,
116 outFile, depFile android.ModuleGenPath, aidlFlags string) android.Paths {
Dan Willemsene1240db2016-11-03 14:28:51 -0700117
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700118 aidlPackage := strings.TrimSuffix(aidlFile.Rel(), aidlFile.Base())
119 baseName := strings.TrimSuffix(aidlFile.Base(), aidlFile.Ext())
Daniel Norman10b74352019-09-24 17:39:58 -0700120 shortName := baseName
121 // TODO(b/111362593): aidl_to_cpp_common.cpp uses heuristics to figure out if
122 // an interface name has a leading I. Those same heuristics have been
123 // moved here.
124 if len(baseName) >= 2 && baseName[0] == 'I' &&
125 strings.ToUpper(baseName)[1] == baseName[1] {
126 shortName = strings.TrimPrefix(baseName, "I")
127 }
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700128
129 outDir := android.PathForModuleGen(ctx, "aidl")
130 headerI := outDir.Join(ctx, aidlPackage, baseName+".h")
131 headerBn := outDir.Join(ctx, aidlPackage, "Bn"+shortName+".h")
132 headerBp := outDir.Join(ctx, aidlPackage, "Bp"+shortName+".h")
133
Jiyong Park29074592019-07-07 16:27:47 +0900134 baseDir := strings.TrimSuffix(aidlFile.String(), aidlFile.Rel())
135 if baseDir != "" {
136 aidlFlags += " -I" + baseDir
137 }
138
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700139 cmd := rule.Command()
Colin Crossee94d6a2019-07-08 17:08:34 -0700140 cmd.BuiltTool(ctx, "aidl-cpp").
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700141 FlagWithDepFile("-d", depFile).
142 Flag("--ninja").
143 Flag(aidlFlags).
144 Input(aidlFile).
145 OutputDir().
146 Output(outFile).
147 ImplicitOutputs(android.WritablePaths{
148 headerI,
149 headerBn,
150 headerBp,
151 })
152
153 return android.Paths{
154 headerI,
155 headerBn,
156 headerBp,
157 }
Dan Willemsene1240db2016-11-03 14:28:51 -0700158}
159
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200160type LexProperties struct {
161 // list of module-specific flags that will be used for .l and .ll compiles
162 Flags []string
163}
164
165func genLex(ctx android.ModuleContext, lexFile android.Path, outFile android.ModuleGenPath, props *LexProperties) {
166 var flags []string
167 if props != nil {
168 flags = props.Flags
169 }
170 flagsString := strings.Join(flags[:], " ")
David Sudd18efd2020-07-24 17:19:23 +0000171 ctx.Build(pctx, android.BuildParams{
172 Rule: lex,
173 Description: "lex " + lexFile.Rel(),
174 Output: outFile,
175 Input: lexFile,
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200176 Args: map[string]string{"flags": flagsString},
David Sudd18efd2020-07-24 17:19:23 +0000177 })
Colin Cross581c1892015-04-07 16:50:10 -0700178}
179
Inseob Kimed2641f2020-02-19 10:42:41 +0900180func genSysprop(ctx android.ModuleContext, syspropFile android.Path) (android.Path, android.Paths) {
Inseob Kim21f26902018-09-06 00:55:20 +0900181 headerFile := android.PathForModuleGen(ctx, "sysprop", "include", syspropFile.Rel()+".h")
Inseob Kim5cefbd22019-06-08 20:36:59 +0900182 publicHeaderFile := android.PathForModuleGen(ctx, "sysprop/public", "include", syspropFile.Rel()+".h")
Inseob Kim21f26902018-09-06 00:55:20 +0900183 cppFile := android.PathForModuleGen(ctx, "sysprop", syspropFile.Rel()+".cpp")
184
Inseob Kimed2641f2020-02-19 10:42:41 +0900185 headers := android.WritablePaths{headerFile, publicHeaderFile}
186
Inseob Kim21f26902018-09-06 00:55:20 +0900187 ctx.Build(pctx, android.BuildParams{
Inseob Kimed2641f2020-02-19 10:42:41 +0900188 Rule: sysprop,
189 Description: "sysprop " + syspropFile.Rel(),
190 Output: cppFile,
191 ImplicitOutputs: headers,
192 Input: syspropFile,
Inseob Kim21f26902018-09-06 00:55:20 +0900193 Args: map[string]string{
194 "headerOutDir": filepath.Dir(headerFile.String()),
Inseob Kim5cefbd22019-06-08 20:36:59 +0900195 "publicOutDir": filepath.Dir(publicHeaderFile.String()),
Inseob Kim21f26902018-09-06 00:55:20 +0900196 "srcOutDir": filepath.Dir(cppFile.String()),
197 "includeName": syspropFile.Rel() + ".h",
198 },
199 })
200
Inseob Kimed2641f2020-02-19 10:42:41 +0900201 return cppFile, headers.Paths()
Inseob Kim21f26902018-09-06 00:55:20 +0900202}
203
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700204func genWinMsg(ctx android.ModuleContext, srcFile android.Path, flags builderFlags) (android.Path, android.Path) {
205 headerFile := android.GenPathWithExt(ctx, "windmc", srcFile, "h")
206 rcFile := android.GenPathWithExt(ctx, "windmc", srcFile, "rc")
207
208 windmcCmd := gccCmd(flags.toolchain, "windmc")
209
Colin Crossae887032017-10-23 17:16:14 -0700210 ctx.Build(pctx, android.BuildParams{
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700211 Rule: windmc,
212 Description: "windmc " + srcFile.Rel(),
213 Output: rcFile,
214 ImplicitOutput: headerFile,
215 Input: srcFile,
216 Args: map[string]string{
217 "windmcCmd": windmcCmd,
218 },
219 })
220
221 return rcFile, headerFile
222}
223
David Sudd18efd2020-07-24 17:19:23 +0000224func genSources(ctx android.ModuleContext, srcFiles android.Paths,
225 buildFlags builderFlags) (android.Paths, android.Paths) {
Colin Cross581c1892015-04-07 16:50:10 -0700226
Colin Cross635c3b02016-05-18 15:37:25 -0700227 var deps android.Paths
Colin Cross2a252be2017-05-01 17:37:24 -0700228 var rsFiles android.Paths
229
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700230 var aidlRule *android.RuleBuilder
231
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700232 var yaccRule_ *android.RuleBuilder
233 yaccRule := func() *android.RuleBuilder {
234 if yaccRule_ == nil {
235 yaccRule_ = android.NewRuleBuilder().Sbox(android.PathForModuleGen(ctx, "yacc"))
236 }
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
261 deps = append(deps, headerFile)
Dan Willemsene1240db2016-11-03 14:28:51 -0700262 case ".aidl":
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700263 if aidlRule == nil {
264 aidlRule = android.NewRuleBuilder().Sbox(android.PathForModuleGen(ctx, "aidl"))
265 }
Dan Willemsene1240db2016-11-03 14:28:51 -0700266 cppFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp")
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700267 depFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp.d")
Dan Willemsene1240db2016-11-03 14:28:51 -0700268 srcFiles[i] = cppFile
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700269 deps = append(deps, genAidl(ctx, aidlRule, srcFile, cppFile, depFile, buildFlags.aidlFlags)...)
Jeff Vander Stoepd6126272019-07-15 19:08:37 -0700270 case ".rscript", ".fs":
Colin Cross2a252be2017-05-01 17:37:24 -0700271 cppFile := rsGeneratedCppFile(ctx, srcFile)
272 rsFiles = append(rsFiles, srcFiles[i])
273 srcFiles[i] = cppFile
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700274 case ".mc":
275 rcFile, headerFile := genWinMsg(ctx, srcFile, buildFlags)
276 srcFiles[i] = rcFile
277 deps = append(deps, headerFile)
Inseob Kim21f26902018-09-06 00:55:20 +0900278 case ".sysprop":
Inseob Kimed2641f2020-02-19 10:42:41 +0900279 cppFile, headerFiles := genSysprop(ctx, srcFile)
Inseob Kim21f26902018-09-06 00:55:20 +0900280 srcFiles[i] = cppFile
Inseob Kimed2641f2020-02-19 10:42:41 +0900281 deps = append(deps, headerFiles...)
Colin Cross581c1892015-04-07 16:50:10 -0700282 }
283 }
284
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700285 if aidlRule != nil {
286 aidlRule.Build(pctx, ctx, "aidl", "gen aidl")
287 }
288
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700289 if yaccRule_ != nil {
290 yaccRule_.Build(pctx, ctx, "yacc", "gen yacc")
291 }
292
Colin Cross2a252be2017-05-01 17:37:24 -0700293 if len(rsFiles) > 0 {
294 deps = append(deps, rsGenerateCpp(ctx, rsFiles, buildFlags.rsFlags)...)
295 }
296
Colin Cross581c1892015-04-07 16:50:10 -0700297 return srcFiles, deps
298}