blob: 1d5cc488ec3b8cc6632924176729535da7db0d3d [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 Willemsenc69d7152019-06-19 10:54:50 -070028 pctx.SourcePathVariable("m4Cmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/m4")
Dan Willemsene1240db2016-11-03 14:28:51 -070029
30 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 (
Colin Cross9d45bb72016-08-29 16:14:13 -070035 lex = pctx.AndroidStaticRule("lex",
Colin Cross581c1892015-04-07 16:50:10 -070036 blueprint.RuleParams{
Dan Willemsenc69d7152019-06-19 10:54:50 -070037 Command: "M4=$m4Cmd $lexCmd -o$out $in",
38 CommandDeps: []string{"$lexCmd", "$m4Cmd"},
Colin Cross581c1892015-04-07 16:50:10 -070039 })
Dan Willemsene1240db2016-11-03 14:28:51 -070040
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,
69 outFile android.ModuleGenPath, props *YaccProperties) (headerFiles android.Paths) {
70
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
100 cmd.Text("BISON_PKGDATADIR=prebuilts/build-tools/common/bison").
101 Tool(ctx.Config().PrebuiltBuildTool(ctx, "bison")).
102 Flag("-d").
103 Flags(flags).
104 FlagWithOutput("--defines=", headerFile).
105 Flag("-o").Output(outFile).Input(yaccFile)
106
107 return ret
Colin Cross581c1892015-04-07 16:50:10 -0700108}
109
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700110func genAidl(ctx android.ModuleContext, rule *android.RuleBuilder, aidlFile android.Path,
111 outFile, depFile android.ModuleGenPath, aidlFlags string) android.Paths {
Dan Willemsene1240db2016-11-03 14:28:51 -0700112
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700113 aidlPackage := strings.TrimSuffix(aidlFile.Rel(), aidlFile.Base())
114 baseName := strings.TrimSuffix(aidlFile.Base(), aidlFile.Ext())
115 shortName := strings.TrimPrefix(baseName, "I")
116
117 outDir := android.PathForModuleGen(ctx, "aidl")
118 headerI := outDir.Join(ctx, aidlPackage, baseName+".h")
119 headerBn := outDir.Join(ctx, aidlPackage, "Bn"+shortName+".h")
120 headerBp := outDir.Join(ctx, aidlPackage, "Bp"+shortName+".h")
121
122 cmd := rule.Command()
123 cmd.Tool(ctx.Config().HostToolPath(ctx, "aidl-cpp")).
124 FlagWithDepFile("-d", depFile).
125 Flag("--ninja").
126 Flag(aidlFlags).
127 Input(aidlFile).
128 OutputDir().
129 Output(outFile).
130 ImplicitOutputs(android.WritablePaths{
131 headerI,
132 headerBn,
133 headerBp,
134 })
135
136 return android.Paths{
137 headerI,
138 headerBn,
139 headerBp,
140 }
Dan Willemsene1240db2016-11-03 14:28:51 -0700141}
142
Colin Cross635c3b02016-05-18 15:37:25 -0700143func genLex(ctx android.ModuleContext, lexFile android.Path, outFile android.ModuleGenPath) {
Colin Crossae887032017-10-23 17:16:14 -0700144 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700145 Rule: lex,
146 Description: "lex " + lexFile.Rel(),
147 Output: outFile,
148 Input: lexFile,
Colin Cross581c1892015-04-07 16:50:10 -0700149 })
Colin Cross581c1892015-04-07 16:50:10 -0700150}
151
Inseob Kim21f26902018-09-06 00:55:20 +0900152func genSysprop(ctx android.ModuleContext, syspropFile android.Path) (android.Path, android.Path) {
153 headerFile := android.PathForModuleGen(ctx, "sysprop", "include", syspropFile.Rel()+".h")
Inseob Kim5cefbd22019-06-08 20:36:59 +0900154 publicHeaderFile := android.PathForModuleGen(ctx, "sysprop/public", "include", syspropFile.Rel()+".h")
Inseob Kim21f26902018-09-06 00:55:20 +0900155 cppFile := android.PathForModuleGen(ctx, "sysprop", syspropFile.Rel()+".cpp")
156
157 ctx.Build(pctx, android.BuildParams{
158 Rule: sysprop,
159 Description: "sysprop " + syspropFile.Rel(),
160 Output: cppFile,
161 ImplicitOutput: headerFile,
162 Input: syspropFile,
163 Args: map[string]string{
164 "headerOutDir": filepath.Dir(headerFile.String()),
Inseob Kim5cefbd22019-06-08 20:36:59 +0900165 "publicOutDir": filepath.Dir(publicHeaderFile.String()),
Inseob Kim21f26902018-09-06 00:55:20 +0900166 "srcOutDir": filepath.Dir(cppFile.String()),
167 "includeName": syspropFile.Rel() + ".h",
168 },
169 })
170
171 return cppFile, headerFile
172}
173
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700174func genWinMsg(ctx android.ModuleContext, srcFile android.Path, flags builderFlags) (android.Path, android.Path) {
175 headerFile := android.GenPathWithExt(ctx, "windmc", srcFile, "h")
176 rcFile := android.GenPathWithExt(ctx, "windmc", srcFile, "rc")
177
178 windmcCmd := gccCmd(flags.toolchain, "windmc")
179
Colin Crossae887032017-10-23 17:16:14 -0700180 ctx.Build(pctx, android.BuildParams{
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700181 Rule: windmc,
182 Description: "windmc " + srcFile.Rel(),
183 Output: rcFile,
184 ImplicitOutput: headerFile,
185 Input: srcFile,
186 Args: map[string]string{
187 "windmcCmd": windmcCmd,
188 },
189 })
190
191 return rcFile, headerFile
192}
193
Colin Cross635c3b02016-05-18 15:37:25 -0700194func genSources(ctx android.ModuleContext, srcFiles android.Paths,
195 buildFlags builderFlags) (android.Paths, android.Paths) {
Colin Cross581c1892015-04-07 16:50:10 -0700196
Colin Cross635c3b02016-05-18 15:37:25 -0700197 var deps android.Paths
Colin Cross2a252be2017-05-01 17:37:24 -0700198 var rsFiles android.Paths
199
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700200 var aidlRule *android.RuleBuilder
201
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700202 var yaccRule_ *android.RuleBuilder
203 yaccRule := func() *android.RuleBuilder {
204 if yaccRule_ == nil {
205 yaccRule_ = android.NewRuleBuilder().Sbox(android.PathForModuleGen(ctx, "yacc"))
206 }
207 return yaccRule_
208 }
209
Colin Cross581c1892015-04-07 16:50:10 -0700210 for i, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700211 switch srcFile.Ext() {
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800212 case ".y":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700213 cFile := android.GenPathWithExt(ctx, "yacc", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800214 srcFiles[i] = cFile
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700215 deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cFile, buildFlags.yacc)...)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800216 case ".yy":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700217 cppFile := android.GenPathWithExt(ctx, "yacc", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700218 srcFiles[i] = cppFile
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700219 deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cppFile, buildFlags.yacc)...)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800220 case ".l":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700221 cFile := android.GenPathWithExt(ctx, "lex", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800222 srcFiles[i] = cFile
223 genLex(ctx, srcFile, cFile)
224 case ".ll":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700225 cppFile := android.GenPathWithExt(ctx, "lex", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700226 srcFiles[i] = cppFile
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800227 genLex(ctx, srcFile, cppFile)
Colin Cross0c461f12016-10-20 16:11:43 -0700228 case ".proto":
Dan Willemsen60e62f02018-11-16 21:05:32 -0800229 ccFile, headerFile := genProto(ctx, srcFile, buildFlags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700230 srcFiles[i] = ccFile
231 deps = append(deps, headerFile)
Dan Willemsene1240db2016-11-03 14:28:51 -0700232 case ".aidl":
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700233 if aidlRule == nil {
234 aidlRule = android.NewRuleBuilder().Sbox(android.PathForModuleGen(ctx, "aidl"))
235 }
Dan Willemsene1240db2016-11-03 14:28:51 -0700236 cppFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp")
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700237 depFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp.d")
Dan Willemsene1240db2016-11-03 14:28:51 -0700238 srcFiles[i] = cppFile
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700239 deps = append(deps, genAidl(ctx, aidlRule, srcFile, cppFile, depFile, buildFlags.aidlFlags)...)
Jeff Vander Stoepd6126272019-07-15 19:08:37 -0700240 case ".rscript", ".fs":
Colin Cross2a252be2017-05-01 17:37:24 -0700241 cppFile := rsGeneratedCppFile(ctx, srcFile)
242 rsFiles = append(rsFiles, srcFiles[i])
243 srcFiles[i] = cppFile
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700244 case ".mc":
245 rcFile, headerFile := genWinMsg(ctx, srcFile, buildFlags)
246 srcFiles[i] = rcFile
247 deps = append(deps, headerFile)
Inseob Kim21f26902018-09-06 00:55:20 +0900248 case ".sysprop":
249 cppFile, headerFile := genSysprop(ctx, srcFile)
250 srcFiles[i] = cppFile
251 deps = append(deps, headerFile)
Colin Cross581c1892015-04-07 16:50:10 -0700252 }
253 }
254
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700255 if aidlRule != nil {
256 aidlRule.Build(pctx, ctx, "aidl", "gen aidl")
257 }
258
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700259 if yaccRule_ != nil {
260 yaccRule_.Build(pctx, ctx, "yacc", "gen yacc")
261 }
262
Colin Cross2a252be2017-05-01 17:37:24 -0700263 if len(rsFiles) > 0 {
264 deps = append(deps, rsGenerateCpp(ctx, rsFiles, buildFlags.rsFlags)...)
265 }
266
Colin Cross581c1892015-04-07 16:50:10 -0700267 return srcFiles, deps
268}