blob: ae761d0362d18b7f0cc1225ba92165ca15604dc1 [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"
19
Colin Cross581c1892015-04-07 16:50:10 -070020 "github.com/google/blueprint"
Colin Cross581c1892015-04-07 16:50:10 -070021
Colin Cross635c3b02016-05-18 15:37:25 -070022 "android/soong/android"
Colin Cross581c1892015-04-07 16:50:10 -070023)
24
25func init() {
Dan Willemsenb7adae82018-05-24 15:45:21 -070026 pctx.SourcePathVariable("lexCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/flex")
Dan Willemsene1240db2016-11-03 14:28:51 -070027
28 pctx.HostBinToolVariable("aidlCmd", "aidl-cpp")
Inseob Kim21f26902018-09-06 00:55:20 +090029 pctx.HostBinToolVariable("syspropCmd", "sysprop_cpp")
Colin Cross581c1892015-04-07 16:50:10 -070030}
31
32var (
Colin Cross9d45bb72016-08-29 16:14:13 -070033 lex = pctx.AndroidStaticRule("lex",
Colin Cross581c1892015-04-07 16:50:10 -070034 blueprint.RuleParams{
35 Command: "$lexCmd -o$out $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080036 CommandDeps: []string{"$lexCmd"},
Colin Cross581c1892015-04-07 16:50:10 -070037 })
Dan Willemsene1240db2016-11-03 14:28:51 -070038
39 aidl = pctx.AndroidStaticRule("aidl",
40 blueprint.RuleParams{
Steven Morelandb468bca2018-07-06 11:36:32 -070041 Command: "$aidlCmd -d${out}.d --ninja $aidlFlags $in $outDir $out",
Dan Willemsene1240db2016-11-03 14:28:51 -070042 CommandDeps: []string{"$aidlCmd"},
43 Depfile: "${out}.d",
44 Deps: blueprint.DepsGCC,
Dan Willemsene1240db2016-11-03 14:28:51 -070045 },
46 "aidlFlags", "outDir")
Dan Willemsen4f1c3d42017-09-09 01:15:26 -070047
Inseob Kim21f26902018-09-06 00:55:20 +090048 sysprop = pctx.AndroidStaticRule("sysprop",
49 blueprint.RuleParams{
Inseob Kimc0907f12019-02-08 21:00:45 +090050 Command: "$syspropCmd --header-dir=$headerOutDir --system-header-dir=$systemOutDir " +
51 "--source-dir=$srcOutDir --include-name=$includeName $in",
Inseob Kim21f26902018-09-06 00:55:20 +090052 CommandDeps: []string{"$syspropCmd"},
53 },
Inseob Kimc0907f12019-02-08 21:00:45 +090054 "headerOutDir", "systemOutDir", "srcOutDir", "includeName")
Inseob Kim21f26902018-09-06 00:55:20 +090055
Dan Willemsen4f1c3d42017-09-09 01:15:26 -070056 windmc = pctx.AndroidStaticRule("windmc",
57 blueprint.RuleParams{
58 Command: "$windmcCmd -r$$(dirname $out) -h$$(dirname $out) $in",
59 CommandDeps: []string{"$windmcCmd"},
60 },
61 "windmcCmd")
Colin Cross581c1892015-04-07 16:50:10 -070062)
63
Dan Willemsen4e0aa232019-04-10 22:59:54 -070064type YaccProperties struct {
65 // list of module-specific flags that will be used for .y and .yy compiles
66 Flags []string
Colin Cross581c1892015-04-07 16:50:10 -070067
Dan Willemsen4e0aa232019-04-10 22:59:54 -070068 // whether the yacc files will produce a location.hh file
69 Gen_location_hh *bool
Colin Cross581c1892015-04-07 16:50:10 -070070
Dan Willemsen4e0aa232019-04-10 22:59:54 -070071 // whether the yacc files will product a position.hh file
72 Gen_position_hh *bool
73}
74
75func genYacc(ctx android.ModuleContext, rule *android.RuleBuilder, yaccFile android.Path,
76 outFile android.ModuleGenPath, props *YaccProperties) (headerFiles android.Paths) {
77
78 outDir := android.PathForModuleGen(ctx, "yacc")
79 headerFile := android.GenPathWithExt(ctx, "yacc", yaccFile, "h")
80 ret := android.Paths{headerFile}
81
82 cmd := rule.Command()
83
84 // Fix up #line markers to not use the sbox temporary directory
85 sedCmd := "sed -i.bak 's#__SBOX_OUT_DIR__#" + outDir.String() + "#'"
86 rule.Command().Text(sedCmd).Input(outFile)
87 rule.Command().Text(sedCmd).Input(headerFile)
88
89 var flags []string
90 if props != nil {
91 flags = props.Flags
92
93 if Bool(props.Gen_location_hh) {
94 locationHeader := outFile.InSameDir(ctx, "location.hh")
95 ret = append(ret, locationHeader)
96 cmd.ImplicitOutput(locationHeader)
97 rule.Command().Text(sedCmd).Input(locationHeader)
98 }
99 if Bool(props.Gen_position_hh) {
100 positionHeader := outFile.InSameDir(ctx, "position.hh")
101 ret = append(ret, positionHeader)
102 cmd.ImplicitOutput(positionHeader)
103 rule.Command().Text(sedCmd).Input(positionHeader)
104 }
105 }
106
107 cmd.Text("BISON_PKGDATADIR=prebuilts/build-tools/common/bison").
108 Tool(ctx.Config().PrebuiltBuildTool(ctx, "bison")).
109 Flag("-d").
110 Flags(flags).
111 FlagWithOutput("--defines=", headerFile).
112 Flag("-o").Output(outFile).Input(yaccFile)
113
114 return ret
Colin Cross581c1892015-04-07 16:50:10 -0700115}
116
Dan Willemsene1240db2016-11-03 14:28:51 -0700117func genAidl(ctx android.ModuleContext, aidlFile android.Path, outFile android.ModuleGenPath, aidlFlags string) android.Paths {
Colin Crossae887032017-10-23 17:16:14 -0700118 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700119 Rule: aidl,
120 Description: "aidl " + aidlFile.Rel(),
121 Output: outFile,
122 Input: aidlFile,
Dan Willemsene1240db2016-11-03 14:28:51 -0700123 Args: map[string]string{
124 "aidlFlags": aidlFlags,
125 "outDir": android.PathForModuleGen(ctx, "aidl").String(),
126 },
127 })
128
129 // TODO: This should return the generated headers, not the source file.
130 return android.Paths{outFile}
131}
132
Colin Cross635c3b02016-05-18 15:37:25 -0700133func genLex(ctx android.ModuleContext, lexFile android.Path, outFile android.ModuleGenPath) {
Colin Crossae887032017-10-23 17:16:14 -0700134 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700135 Rule: lex,
136 Description: "lex " + lexFile.Rel(),
137 Output: outFile,
138 Input: lexFile,
Colin Cross581c1892015-04-07 16:50:10 -0700139 })
Colin Cross581c1892015-04-07 16:50:10 -0700140}
141
Inseob Kim21f26902018-09-06 00:55:20 +0900142func genSysprop(ctx android.ModuleContext, syspropFile android.Path) (android.Path, android.Path) {
143 headerFile := android.PathForModuleGen(ctx, "sysprop", "include", syspropFile.Rel()+".h")
Inseob Kimc0907f12019-02-08 21:00:45 +0900144 systemHeaderFile := android.PathForModuleGen(ctx, "sysprop/system", "include", syspropFile.Rel()+".h")
Inseob Kim21f26902018-09-06 00:55:20 +0900145 cppFile := android.PathForModuleGen(ctx, "sysprop", syspropFile.Rel()+".cpp")
146
147 ctx.Build(pctx, android.BuildParams{
148 Rule: sysprop,
149 Description: "sysprop " + syspropFile.Rel(),
150 Output: cppFile,
151 ImplicitOutput: headerFile,
152 Input: syspropFile,
153 Args: map[string]string{
154 "headerOutDir": filepath.Dir(headerFile.String()),
Inseob Kimc0907f12019-02-08 21:00:45 +0900155 "systemOutDir": filepath.Dir(systemHeaderFile.String()),
Inseob Kim21f26902018-09-06 00:55:20 +0900156 "srcOutDir": filepath.Dir(cppFile.String()),
157 "includeName": syspropFile.Rel() + ".h",
158 },
159 })
160
161 return cppFile, headerFile
162}
163
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700164func genWinMsg(ctx android.ModuleContext, srcFile android.Path, flags builderFlags) (android.Path, android.Path) {
165 headerFile := android.GenPathWithExt(ctx, "windmc", srcFile, "h")
166 rcFile := android.GenPathWithExt(ctx, "windmc", srcFile, "rc")
167
168 windmcCmd := gccCmd(flags.toolchain, "windmc")
169
Colin Crossae887032017-10-23 17:16:14 -0700170 ctx.Build(pctx, android.BuildParams{
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700171 Rule: windmc,
172 Description: "windmc " + srcFile.Rel(),
173 Output: rcFile,
174 ImplicitOutput: headerFile,
175 Input: srcFile,
176 Args: map[string]string{
177 "windmcCmd": windmcCmd,
178 },
179 })
180
181 return rcFile, headerFile
182}
183
Colin Cross635c3b02016-05-18 15:37:25 -0700184func genSources(ctx android.ModuleContext, srcFiles android.Paths,
185 buildFlags builderFlags) (android.Paths, android.Paths) {
Colin Cross581c1892015-04-07 16:50:10 -0700186
Colin Cross635c3b02016-05-18 15:37:25 -0700187 var deps android.Paths
Colin Cross2a252be2017-05-01 17:37:24 -0700188 var rsFiles android.Paths
189
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700190 var yaccRule_ *android.RuleBuilder
191 yaccRule := func() *android.RuleBuilder {
192 if yaccRule_ == nil {
193 yaccRule_ = android.NewRuleBuilder().Sbox(android.PathForModuleGen(ctx, "yacc"))
194 }
195 return yaccRule_
196 }
197
Colin Cross581c1892015-04-07 16:50:10 -0700198 for i, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700199 switch srcFile.Ext() {
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800200 case ".y":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700201 cFile := android.GenPathWithExt(ctx, "yacc", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800202 srcFiles[i] = cFile
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700203 deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cFile, buildFlags.yacc)...)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800204 case ".yy":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700205 cppFile := android.GenPathWithExt(ctx, "yacc", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700206 srcFiles[i] = cppFile
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700207 deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cppFile, buildFlags.yacc)...)
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800208 case ".l":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700209 cFile := android.GenPathWithExt(ctx, "lex", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800210 srcFiles[i] = cFile
211 genLex(ctx, srcFile, cFile)
212 case ".ll":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700213 cppFile := android.GenPathWithExt(ctx, "lex", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700214 srcFiles[i] = cppFile
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800215 genLex(ctx, srcFile, cppFile)
Colin Cross0c461f12016-10-20 16:11:43 -0700216 case ".proto":
Dan Willemsen60e62f02018-11-16 21:05:32 -0800217 ccFile, headerFile := genProto(ctx, srcFile, buildFlags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700218 srcFiles[i] = ccFile
219 deps = append(deps, headerFile)
Dan Willemsene1240db2016-11-03 14:28:51 -0700220 case ".aidl":
221 cppFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp")
222 srcFiles[i] = cppFile
223 deps = append(deps, genAidl(ctx, srcFile, cppFile, buildFlags.aidlFlags)...)
Colin Cross2a252be2017-05-01 17:37:24 -0700224 case ".rs", ".fs":
225 cppFile := rsGeneratedCppFile(ctx, srcFile)
226 rsFiles = append(rsFiles, srcFiles[i])
227 srcFiles[i] = cppFile
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700228 case ".mc":
229 rcFile, headerFile := genWinMsg(ctx, srcFile, buildFlags)
230 srcFiles[i] = rcFile
231 deps = append(deps, headerFile)
Inseob Kim21f26902018-09-06 00:55:20 +0900232 case ".sysprop":
233 cppFile, headerFile := genSysprop(ctx, srcFile)
234 srcFiles[i] = cppFile
235 deps = append(deps, headerFile)
Colin Cross581c1892015-04-07 16:50:10 -0700236 }
237 }
238
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700239 if yaccRule_ != nil {
240 yaccRule_.Build(pctx, ctx, "yacc", "gen yacc")
241 }
242
Colin Cross2a252be2017-05-01 17:37:24 -0700243 if len(rsFiles) > 0 {
244 deps = append(deps, rsGenerateCpp(ctx, rsFiles, buildFlags.rsFlags)...)
245 }
246
Colin Cross581c1892015-04-07 16:50:10 -0700247 return srcFiles, deps
248}