blob: c3088f44ce242dabd8283cf10525cc5bda6eb953 [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 Willemsen6f46a382018-01-08 17:26:13 -080027 pctx.SourcePathVariable("yaccCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/bison")
28 pctx.SourcePathVariable("yaccDataDir", "prebuilts/build-tools/common/bison")
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 yacc = pctx.AndroidStaticRule("yacc",
Colin Cross581c1892015-04-07 16:50:10 -070036 blueprint.RuleParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -070037 Command: "BISON_PKGDATADIR=$yaccDataDir $yaccCmd -d $yaccFlags --defines=$hFile -o $out $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080038 CommandDeps: []string{"$yaccCmd"},
Colin Cross581c1892015-04-07 16:50:10 -070039 },
Dan Willemsen9f3c5742016-11-03 14:28:31 -070040 "yaccFlags", "hFile")
Colin Cross581c1892015-04-07 16:50:10 -070041
Colin Cross9d45bb72016-08-29 16:14:13 -070042 lex = pctx.AndroidStaticRule("lex",
Colin Cross581c1892015-04-07 16:50:10 -070043 blueprint.RuleParams{
44 Command: "$lexCmd -o$out $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080045 CommandDeps: []string{"$lexCmd"},
Colin Cross581c1892015-04-07 16:50:10 -070046 })
Dan Willemsene1240db2016-11-03 14:28:51 -070047
48 aidl = pctx.AndroidStaticRule("aidl",
49 blueprint.RuleParams{
Steven Morelandb468bca2018-07-06 11:36:32 -070050 Command: "$aidlCmd -d${out}.d --ninja $aidlFlags $in $outDir $out",
Dan Willemsene1240db2016-11-03 14:28:51 -070051 CommandDeps: []string{"$aidlCmd"},
52 Depfile: "${out}.d",
53 Deps: blueprint.DepsGCC,
Dan Willemsene1240db2016-11-03 14:28:51 -070054 },
55 "aidlFlags", "outDir")
Dan Willemsen4f1c3d42017-09-09 01:15:26 -070056
Inseob Kim21f26902018-09-06 00:55:20 +090057 sysprop = pctx.AndroidStaticRule("sysprop",
58 blueprint.RuleParams{
59 Command: "$syspropCmd --header-output-dir=$headerOutDir --source-output-dir=$srcOutDir --include-name=$includeName $in",
60 CommandDeps: []string{"$syspropCmd"},
61 },
62 "headerOutDir", "srcOutDir", "includeName")
63
Dan Willemsen4f1c3d42017-09-09 01:15:26 -070064 windmc = pctx.AndroidStaticRule("windmc",
65 blueprint.RuleParams{
66 Command: "$windmcCmd -r$$(dirname $out) -h$$(dirname $out) $in",
67 CommandDeps: []string{"$windmcCmd"},
68 },
69 "windmcCmd")
Colin Cross581c1892015-04-07 16:50:10 -070070)
71
Colin Cross635c3b02016-05-18 15:37:25 -070072func genYacc(ctx android.ModuleContext, yaccFile android.Path, outFile android.ModuleGenPath, yaccFlags string) (headerFile android.ModuleGenPath) {
Dan Willemsen21ec4902016-11-02 20:43:13 -070073 headerFile = android.GenPathWithExt(ctx, "yacc", yaccFile, "h")
Colin Cross581c1892015-04-07 16:50:10 -070074
Colin Crossae887032017-10-23 17:16:14 -070075 ctx.Build(pctx, android.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -070076 Rule: yacc,
Colin Cross67a5c132017-05-09 13:45:28 -070077 Description: "yacc " + yaccFile.Rel(),
Dan Willemsen9f3c5742016-11-03 14:28:31 -070078 Output: outFile,
79 ImplicitOutput: headerFile,
80 Input: yaccFile,
Colin Cross581c1892015-04-07 16:50:10 -070081 Args: map[string]string{
82 "yaccFlags": yaccFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -070083 "hFile": headerFile.String(),
Colin Cross581c1892015-04-07 16:50:10 -070084 },
85 })
86
Dan Willemsenf0c73e02016-03-01 15:15:26 -080087 return headerFile
Colin Cross581c1892015-04-07 16:50:10 -070088}
89
Dan Willemsene1240db2016-11-03 14:28:51 -070090func genAidl(ctx android.ModuleContext, aidlFile android.Path, outFile android.ModuleGenPath, aidlFlags string) android.Paths {
Colin Crossae887032017-10-23 17:16:14 -070091 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -070092 Rule: aidl,
93 Description: "aidl " + aidlFile.Rel(),
94 Output: outFile,
95 Input: aidlFile,
Dan Willemsene1240db2016-11-03 14:28:51 -070096 Args: map[string]string{
97 "aidlFlags": aidlFlags,
98 "outDir": android.PathForModuleGen(ctx, "aidl").String(),
99 },
100 })
101
102 // TODO: This should return the generated headers, not the source file.
103 return android.Paths{outFile}
104}
105
Colin Cross635c3b02016-05-18 15:37:25 -0700106func genLex(ctx android.ModuleContext, lexFile android.Path, outFile android.ModuleGenPath) {
Colin Crossae887032017-10-23 17:16:14 -0700107 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700108 Rule: lex,
109 Description: "lex " + lexFile.Rel(),
110 Output: outFile,
111 Input: lexFile,
Colin Cross581c1892015-04-07 16:50:10 -0700112 })
Colin Cross581c1892015-04-07 16:50:10 -0700113}
114
Inseob Kim21f26902018-09-06 00:55:20 +0900115func genSysprop(ctx android.ModuleContext, syspropFile android.Path) (android.Path, android.Path) {
116 headerFile := android.PathForModuleGen(ctx, "sysprop", "include", syspropFile.Rel()+".h")
117 cppFile := android.PathForModuleGen(ctx, "sysprop", syspropFile.Rel()+".cpp")
118
119 ctx.Build(pctx, android.BuildParams{
120 Rule: sysprop,
121 Description: "sysprop " + syspropFile.Rel(),
122 Output: cppFile,
123 ImplicitOutput: headerFile,
124 Input: syspropFile,
125 Args: map[string]string{
126 "headerOutDir": filepath.Dir(headerFile.String()),
127 "srcOutDir": filepath.Dir(cppFile.String()),
128 "includeName": syspropFile.Rel() + ".h",
129 },
130 })
131
132 return cppFile, headerFile
133}
134
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700135func genWinMsg(ctx android.ModuleContext, srcFile android.Path, flags builderFlags) (android.Path, android.Path) {
136 headerFile := android.GenPathWithExt(ctx, "windmc", srcFile, "h")
137 rcFile := android.GenPathWithExt(ctx, "windmc", srcFile, "rc")
138
139 windmcCmd := gccCmd(flags.toolchain, "windmc")
140
Colin Crossae887032017-10-23 17:16:14 -0700141 ctx.Build(pctx, android.BuildParams{
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700142 Rule: windmc,
143 Description: "windmc " + srcFile.Rel(),
144 Output: rcFile,
145 ImplicitOutput: headerFile,
146 Input: srcFile,
147 Args: map[string]string{
148 "windmcCmd": windmcCmd,
149 },
150 })
151
152 return rcFile, headerFile
153}
154
Colin Cross635c3b02016-05-18 15:37:25 -0700155func genSources(ctx android.ModuleContext, srcFiles android.Paths,
156 buildFlags builderFlags) (android.Paths, android.Paths) {
Colin Cross581c1892015-04-07 16:50:10 -0700157
Colin Cross635c3b02016-05-18 15:37:25 -0700158 var deps android.Paths
Colin Cross581c1892015-04-07 16:50:10 -0700159
Colin Cross2a252be2017-05-01 17:37:24 -0700160 var rsFiles android.Paths
161
Colin Cross581c1892015-04-07 16:50:10 -0700162 for i, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700163 switch srcFile.Ext() {
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800164 case ".y":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700165 cFile := android.GenPathWithExt(ctx, "yacc", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800166 srcFiles[i] = cFile
167 deps = append(deps, genYacc(ctx, srcFile, cFile, buildFlags.yaccFlags))
168 case ".yy":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700169 cppFile := android.GenPathWithExt(ctx, "yacc", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700170 srcFiles[i] = cppFile
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800171 deps = append(deps, genYacc(ctx, srcFile, cppFile, buildFlags.yaccFlags))
172 case ".l":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700173 cFile := android.GenPathWithExt(ctx, "lex", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800174 srcFiles[i] = cFile
175 genLex(ctx, srcFile, cFile)
176 case ".ll":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700177 cppFile := android.GenPathWithExt(ctx, "lex", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700178 srcFiles[i] = cppFile
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800179 genLex(ctx, srcFile, cppFile)
Colin Cross0c461f12016-10-20 16:11:43 -0700180 case ".proto":
Dan Willemsen60e62f02018-11-16 21:05:32 -0800181 ccFile, headerFile := genProto(ctx, srcFile, buildFlags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700182 srcFiles[i] = ccFile
183 deps = append(deps, headerFile)
Dan Willemsene1240db2016-11-03 14:28:51 -0700184 case ".aidl":
185 cppFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp")
186 srcFiles[i] = cppFile
187 deps = append(deps, genAidl(ctx, srcFile, cppFile, buildFlags.aidlFlags)...)
Colin Cross2a252be2017-05-01 17:37:24 -0700188 case ".rs", ".fs":
189 cppFile := rsGeneratedCppFile(ctx, srcFile)
190 rsFiles = append(rsFiles, srcFiles[i])
191 srcFiles[i] = cppFile
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700192 case ".mc":
193 rcFile, headerFile := genWinMsg(ctx, srcFile, buildFlags)
194 srcFiles[i] = rcFile
195 deps = append(deps, headerFile)
Inseob Kim21f26902018-09-06 00:55:20 +0900196 case ".sysprop":
197 cppFile, headerFile := genSysprop(ctx, srcFile)
198 srcFiles[i] = cppFile
199 deps = append(deps, headerFile)
Colin Cross581c1892015-04-07 16:50:10 -0700200 }
201 }
202
Colin Cross2a252be2017-05-01 17:37:24 -0700203 if len(rsFiles) > 0 {
204 deps = append(deps, rsGenerateCpp(ctx, rsFiles, buildFlags.rsFlags)...)
205 }
206
Colin Cross581c1892015-04-07 16:50:10 -0700207 return srcFiles, deps
208}