blob: 1d30dabd7b1c81780162d308d630b76dfec9854c [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 Willemsenb7adae82018-05-24 15:45:21 -070027 pctx.SourcePathVariable("lexCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/flex")
Dan Willemsene1240db2016-11-03 14:28:51 -070028
29 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 (
Colin Cross9d45bb72016-08-29 16:14:13 -070034 lex = pctx.AndroidStaticRule("lex",
Colin Cross581c1892015-04-07 16:50:10 -070035 blueprint.RuleParams{
36 Command: "$lexCmd -o$out $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080037 CommandDeps: []string{"$lexCmd"},
Colin Cross581c1892015-04-07 16:50:10 -070038 })
Dan Willemsene1240db2016-11-03 14:28:51 -070039
Inseob Kim21f26902018-09-06 00:55:20 +090040 sysprop = pctx.AndroidStaticRule("sysprop",
41 blueprint.RuleParams{
Inseob Kim5cefbd22019-06-08 20:36:59 +090042 Command: "$syspropCmd --header-dir=$headerOutDir --public-header-dir=$publicOutDir " +
Inseob Kimc0907f12019-02-08 21:00:45 +090043 "--source-dir=$srcOutDir --include-name=$includeName $in",
Inseob Kim21f26902018-09-06 00:55:20 +090044 CommandDeps: []string{"$syspropCmd"},
45 },
Inseob Kim5cefbd22019-06-08 20:36:59 +090046 "headerOutDir", "publicOutDir", "srcOutDir", "includeName")
Inseob Kim21f26902018-09-06 00:55:20 +090047
Dan Willemsen4f1c3d42017-09-09 01:15:26 -070048 windmc = pctx.AndroidStaticRule("windmc",
49 blueprint.RuleParams{
50 Command: "$windmcCmd -r$$(dirname $out) -h$$(dirname $out) $in",
51 CommandDeps: []string{"$windmcCmd"},
52 },
53 "windmcCmd")
Colin Cross581c1892015-04-07 16:50:10 -070054)
55
Dan Willemsen4e0aa232019-04-10 22:59:54 -070056type YaccProperties struct {
57 // list of module-specific flags that will be used for .y and .yy compiles
58 Flags []string
Colin Cross581c1892015-04-07 16:50:10 -070059
Dan Willemsen4e0aa232019-04-10 22:59:54 -070060 // whether the yacc files will produce a location.hh file
61 Gen_location_hh *bool
Colin Cross581c1892015-04-07 16:50:10 -070062
Dan Willemsen4e0aa232019-04-10 22:59:54 -070063 // whether the yacc files will product a position.hh file
64 Gen_position_hh *bool
65}
66
67func genYacc(ctx android.ModuleContext, rule *android.RuleBuilder, yaccFile android.Path,
68 outFile android.ModuleGenPath, props *YaccProperties) (headerFiles android.Paths) {
69
70 outDir := android.PathForModuleGen(ctx, "yacc")
71 headerFile := android.GenPathWithExt(ctx, "yacc", yaccFile, "h")
72 ret := android.Paths{headerFile}
73
74 cmd := rule.Command()
75
76 // Fix up #line markers to not use the sbox temporary directory
77 sedCmd := "sed -i.bak 's#__SBOX_OUT_DIR__#" + outDir.String() + "#'"
78 rule.Command().Text(sedCmd).Input(outFile)
79 rule.Command().Text(sedCmd).Input(headerFile)
80
81 var flags []string
82 if props != nil {
83 flags = props.Flags
84
85 if Bool(props.Gen_location_hh) {
86 locationHeader := outFile.InSameDir(ctx, "location.hh")
87 ret = append(ret, locationHeader)
88 cmd.ImplicitOutput(locationHeader)
89 rule.Command().Text(sedCmd).Input(locationHeader)
90 }
91 if Bool(props.Gen_position_hh) {
92 positionHeader := outFile.InSameDir(ctx, "position.hh")
93 ret = append(ret, positionHeader)
94 cmd.ImplicitOutput(positionHeader)
95 rule.Command().Text(sedCmd).Input(positionHeader)
96 }
97 }
98
99 cmd.Text("BISON_PKGDATADIR=prebuilts/build-tools/common/bison").
100 Tool(ctx.Config().PrebuiltBuildTool(ctx, "bison")).
101 Flag("-d").
102 Flags(flags).
103 FlagWithOutput("--defines=", headerFile).
104 Flag("-o").Output(outFile).Input(yaccFile)
105
106 return ret
Colin Cross581c1892015-04-07 16:50:10 -0700107}
108
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700109func genAidl(ctx android.ModuleContext, rule *android.RuleBuilder, aidlFile android.Path,
110 outFile, depFile android.ModuleGenPath, aidlFlags string) android.Paths {
Dan Willemsene1240db2016-11-03 14:28:51 -0700111
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700112 aidlPackage := strings.TrimSuffix(aidlFile.Rel(), aidlFile.Base())
113 baseName := strings.TrimSuffix(aidlFile.Base(), aidlFile.Ext())
114 shortName := strings.TrimPrefix(baseName, "I")
115
116 outDir := android.PathForModuleGen(ctx, "aidl")
117 headerI := outDir.Join(ctx, aidlPackage, baseName+".h")
118 headerBn := outDir.Join(ctx, aidlPackage, "Bn"+shortName+".h")
119 headerBp := outDir.Join(ctx, aidlPackage, "Bp"+shortName+".h")
120
121 cmd := rule.Command()
122 cmd.Tool(ctx.Config().HostToolPath(ctx, "aidl-cpp")).
123 FlagWithDepFile("-d", depFile).
124 Flag("--ninja").
125 Flag(aidlFlags).
126 Input(aidlFile).
127 OutputDir().
128 Output(outFile).
129 ImplicitOutputs(android.WritablePaths{
130 headerI,
131 headerBn,
132 headerBp,
133 })
134
135 return android.Paths{
136 headerI,
137 headerBn,
138 headerBp,
139 }
Dan Willemsene1240db2016-11-03 14:28:51 -0700140}
141
Colin Cross635c3b02016-05-18 15:37:25 -0700142func genLex(ctx android.ModuleContext, lexFile android.Path, outFile android.ModuleGenPath) {
Colin Crossae887032017-10-23 17:16:14 -0700143 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700144 Rule: lex,
145 Description: "lex " + lexFile.Rel(),
146 Output: outFile,
147 Input: lexFile,
Colin Cross581c1892015-04-07 16:50:10 -0700148 })
Colin Cross581c1892015-04-07 16:50:10 -0700149}
150
Inseob Kim21f26902018-09-06 00:55:20 +0900151func genSysprop(ctx android.ModuleContext, syspropFile android.Path) (android.Path, android.Path) {
152 headerFile := android.PathForModuleGen(ctx, "sysprop", "include", syspropFile.Rel()+".h")
Inseob Kim5cefbd22019-06-08 20:36:59 +0900153 publicHeaderFile := android.PathForModuleGen(ctx, "sysprop/public", "include", syspropFile.Rel()+".h")
Inseob Kim21f26902018-09-06 00:55:20 +0900154 cppFile := android.PathForModuleGen(ctx, "sysprop", syspropFile.Rel()+".cpp")
155
156 ctx.Build(pctx, android.BuildParams{
157 Rule: sysprop,
158 Description: "sysprop " + syspropFile.Rel(),
159 Output: cppFile,
160 ImplicitOutput: headerFile,
161 Input: syspropFile,
162 Args: map[string]string{
163 "headerOutDir": filepath.Dir(headerFile.String()),
Inseob Kim5cefbd22019-06-08 20:36:59 +0900164 "publicOutDir": filepath.Dir(publicHeaderFile.String()),
Inseob Kim21f26902018-09-06 00:55:20 +0900165 "srcOutDir": filepath.Dir(cppFile.String()),
166 "includeName": syspropFile.Rel() + ".h",
167 },
168 })
169
170 return cppFile, headerFile
171}
172
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700173func genWinMsg(ctx android.ModuleContext, srcFile android.Path, flags builderFlags) (android.Path, android.Path) {
174 headerFile := android.GenPathWithExt(ctx, "windmc", srcFile, "h")
175 rcFile := android.GenPathWithExt(ctx, "windmc", srcFile, "rc")
176
177 windmcCmd := gccCmd(flags.toolchain, "windmc")
178
Colin Crossae887032017-10-23 17:16:14 -0700179 ctx.Build(pctx, android.BuildParams{
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700180 Rule: windmc,
181 Description: "windmc " + srcFile.Rel(),
182 Output: rcFile,
183 ImplicitOutput: headerFile,
184 Input: srcFile,
185 Args: map[string]string{
186 "windmcCmd": windmcCmd,
187 },
188 })
189
190 return rcFile, headerFile
191}
192
Colin Cross635c3b02016-05-18 15:37:25 -0700193func genSources(ctx android.ModuleContext, srcFiles android.Paths,
194 buildFlags builderFlags) (android.Paths, android.Paths) {
Colin Cross581c1892015-04-07 16:50:10 -0700195
Colin Cross635c3b02016-05-18 15:37:25 -0700196 var deps android.Paths
Colin Cross2a252be2017-05-01 17:37:24 -0700197 var rsFiles android.Paths
198
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700199 var aidlRule *android.RuleBuilder
200
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700201 var yaccRule_ *android.RuleBuilder
202 yaccRule := func() *android.RuleBuilder {
203 if yaccRule_ == nil {
204 yaccRule_ = android.NewRuleBuilder().Sbox(android.PathForModuleGen(ctx, "yacc"))
205 }
206 return yaccRule_
207 }
208
Colin Cross581c1892015-04-07 16:50:10 -0700209 for i, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700210 switch srcFile.Ext() {
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800211 case ".y":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700212 cFile := android.GenPathWithExt(ctx, "yacc", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800213 srcFiles[i] = cFile
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700214 deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cFile, buildFlags.yacc)...)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800215 case ".yy":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700216 cppFile := android.GenPathWithExt(ctx, "yacc", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700217 srcFiles[i] = cppFile
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700218 deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cppFile, buildFlags.yacc)...)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800219 case ".l":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700220 cFile := android.GenPathWithExt(ctx, "lex", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800221 srcFiles[i] = cFile
222 genLex(ctx, srcFile, cFile)
223 case ".ll":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700224 cppFile := android.GenPathWithExt(ctx, "lex", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700225 srcFiles[i] = cppFile
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800226 genLex(ctx, srcFile, cppFile)
Colin Cross0c461f12016-10-20 16:11:43 -0700227 case ".proto":
Dan Willemsen60e62f02018-11-16 21:05:32 -0800228 ccFile, headerFile := genProto(ctx, srcFile, buildFlags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700229 srcFiles[i] = ccFile
230 deps = append(deps, headerFile)
Dan Willemsene1240db2016-11-03 14:28:51 -0700231 case ".aidl":
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700232 if aidlRule == nil {
233 aidlRule = android.NewRuleBuilder().Sbox(android.PathForModuleGen(ctx, "aidl"))
234 }
Dan Willemsene1240db2016-11-03 14:28:51 -0700235 cppFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp")
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700236 depFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp.d")
Dan Willemsene1240db2016-11-03 14:28:51 -0700237 srcFiles[i] = cppFile
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700238 deps = append(deps, genAidl(ctx, aidlRule, srcFile, cppFile, depFile, buildFlags.aidlFlags)...)
Colin Cross2a252be2017-05-01 17:37:24 -0700239 case ".rs", ".fs":
240 cppFile := rsGeneratedCppFile(ctx, srcFile)
241 rsFiles = append(rsFiles, srcFiles[i])
242 srcFiles[i] = cppFile
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700243 case ".mc":
244 rcFile, headerFile := genWinMsg(ctx, srcFile, buildFlags)
245 srcFiles[i] = rcFile
246 deps = append(deps, headerFile)
Inseob Kim21f26902018-09-06 00:55:20 +0900247 case ".sysprop":
248 cppFile, headerFile := genSysprop(ctx, srcFile)
249 srcFiles[i] = cppFile
250 deps = append(deps, headerFile)
Colin Cross581c1892015-04-07 16:50:10 -0700251 }
252 }
253
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700254 if aidlRule != nil {
255 aidlRule.Build(pctx, ctx, "aidl", "gen aidl")
256 }
257
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700258 if yaccRule_ != nil {
259 yaccRule_.Build(pctx, ctx, "yacc", "gen yacc")
260 }
261
Colin Cross2a252be2017-05-01 17:37:24 -0700262 if len(rsFiles) > 0 {
263 deps = append(deps, rsGenerateCpp(ctx, rsFiles, buildFlags.rsFlags)...)
264 }
265
Colin Cross581c1892015-04-07 16:50:10 -0700266 return srcFiles, deps
267}