blob: 29a2bb20695e4662dee05e2ab18a2a95d1fabb44 [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
17// This file generates the final rules for compiling all C/C++. All properties related to
18// compiling should have been translated into builderFlags or another argument to the Transform*
19// functions.
20
21import (
Inseob Kim21f26902018-09-06 00:55:20 +090022 "path/filepath"
23
Colin Cross581c1892015-04-07 16:50:10 -070024 "github.com/google/blueprint"
Colin Cross581c1892015-04-07 16:50:10 -070025
Colin Cross635c3b02016-05-18 15:37:25 -070026 "android/soong/android"
Colin Cross581c1892015-04-07 16:50:10 -070027)
28
29func init() {
Dan Willemsenb7adae82018-05-24 15:45:21 -070030 pctx.SourcePathVariable("lexCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/flex")
Dan Willemsen6f46a382018-01-08 17:26:13 -080031 pctx.SourcePathVariable("yaccCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/bison")
32 pctx.SourcePathVariable("yaccDataDir", "prebuilts/build-tools/common/bison")
Dan Willemsene1240db2016-11-03 14:28:51 -070033
34 pctx.HostBinToolVariable("aidlCmd", "aidl-cpp")
Inseob Kim21f26902018-09-06 00:55:20 +090035 pctx.HostBinToolVariable("syspropCmd", "sysprop_cpp")
Colin Cross581c1892015-04-07 16:50:10 -070036}
37
38var (
Colin Cross9d45bb72016-08-29 16:14:13 -070039 yacc = pctx.AndroidStaticRule("yacc",
Colin Cross581c1892015-04-07 16:50:10 -070040 blueprint.RuleParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -070041 Command: "BISON_PKGDATADIR=$yaccDataDir $yaccCmd -d $yaccFlags --defines=$hFile -o $out $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080042 CommandDeps: []string{"$yaccCmd"},
Colin Cross581c1892015-04-07 16:50:10 -070043 },
Dan Willemsen9f3c5742016-11-03 14:28:31 -070044 "yaccFlags", "hFile")
Colin Cross581c1892015-04-07 16:50:10 -070045
Colin Cross9d45bb72016-08-29 16:14:13 -070046 lex = pctx.AndroidStaticRule("lex",
Colin Cross581c1892015-04-07 16:50:10 -070047 blueprint.RuleParams{
48 Command: "$lexCmd -o$out $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080049 CommandDeps: []string{"$lexCmd"},
Colin Cross581c1892015-04-07 16:50:10 -070050 })
Dan Willemsene1240db2016-11-03 14:28:51 -070051
52 aidl = pctx.AndroidStaticRule("aidl",
53 blueprint.RuleParams{
Steven Morelandb468bca2018-07-06 11:36:32 -070054 Command: "$aidlCmd -d${out}.d --ninja $aidlFlags $in $outDir $out",
Dan Willemsene1240db2016-11-03 14:28:51 -070055 CommandDeps: []string{"$aidlCmd"},
56 Depfile: "${out}.d",
57 Deps: blueprint.DepsGCC,
Dan Willemsene1240db2016-11-03 14:28:51 -070058 },
59 "aidlFlags", "outDir")
Dan Willemsen4f1c3d42017-09-09 01:15:26 -070060
Inseob Kim21f26902018-09-06 00:55:20 +090061 sysprop = pctx.AndroidStaticRule("sysprop",
62 blueprint.RuleParams{
63 Command: "$syspropCmd --header-output-dir=$headerOutDir --source-output-dir=$srcOutDir --include-name=$includeName $in",
64 CommandDeps: []string{"$syspropCmd"},
65 },
66 "headerOutDir", "srcOutDir", "includeName")
67
Dan Willemsen4f1c3d42017-09-09 01:15:26 -070068 windmc = pctx.AndroidStaticRule("windmc",
69 blueprint.RuleParams{
70 Command: "$windmcCmd -r$$(dirname $out) -h$$(dirname $out) $in",
71 CommandDeps: []string{"$windmcCmd"},
72 },
73 "windmcCmd")
Colin Cross581c1892015-04-07 16:50:10 -070074)
75
Colin Cross635c3b02016-05-18 15:37:25 -070076func genYacc(ctx android.ModuleContext, yaccFile android.Path, outFile android.ModuleGenPath, yaccFlags string) (headerFile android.ModuleGenPath) {
Dan Willemsen21ec4902016-11-02 20:43:13 -070077 headerFile = android.GenPathWithExt(ctx, "yacc", yaccFile, "h")
Colin Cross581c1892015-04-07 16:50:10 -070078
Colin Crossae887032017-10-23 17:16:14 -070079 ctx.Build(pctx, android.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -070080 Rule: yacc,
Colin Cross67a5c132017-05-09 13:45:28 -070081 Description: "yacc " + yaccFile.Rel(),
Dan Willemsen9f3c5742016-11-03 14:28:31 -070082 Output: outFile,
83 ImplicitOutput: headerFile,
84 Input: yaccFile,
Colin Cross581c1892015-04-07 16:50:10 -070085 Args: map[string]string{
86 "yaccFlags": yaccFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -070087 "hFile": headerFile.String(),
Colin Cross581c1892015-04-07 16:50:10 -070088 },
89 })
90
Dan Willemsenf0c73e02016-03-01 15:15:26 -080091 return headerFile
Colin Cross581c1892015-04-07 16:50:10 -070092}
93
Dan Willemsene1240db2016-11-03 14:28:51 -070094func genAidl(ctx android.ModuleContext, aidlFile android.Path, outFile android.ModuleGenPath, aidlFlags string) android.Paths {
Colin Crossae887032017-10-23 17:16:14 -070095 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -070096 Rule: aidl,
97 Description: "aidl " + aidlFile.Rel(),
98 Output: outFile,
99 Input: aidlFile,
Dan Willemsene1240db2016-11-03 14:28:51 -0700100 Args: map[string]string{
101 "aidlFlags": aidlFlags,
102 "outDir": android.PathForModuleGen(ctx, "aidl").String(),
103 },
104 })
105
106 // TODO: This should return the generated headers, not the source file.
107 return android.Paths{outFile}
108}
109
Colin Cross635c3b02016-05-18 15:37:25 -0700110func genLex(ctx android.ModuleContext, lexFile android.Path, outFile android.ModuleGenPath) {
Colin Crossae887032017-10-23 17:16:14 -0700111 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700112 Rule: lex,
113 Description: "lex " + lexFile.Rel(),
114 Output: outFile,
115 Input: lexFile,
Colin Cross581c1892015-04-07 16:50:10 -0700116 })
Colin Cross581c1892015-04-07 16:50:10 -0700117}
118
Inseob Kim21f26902018-09-06 00:55:20 +0900119func genSysprop(ctx android.ModuleContext, syspropFile android.Path) (android.Path, android.Path) {
120 headerFile := android.PathForModuleGen(ctx, "sysprop", "include", syspropFile.Rel()+".h")
121 cppFile := android.PathForModuleGen(ctx, "sysprop", syspropFile.Rel()+".cpp")
122
123 ctx.Build(pctx, android.BuildParams{
124 Rule: sysprop,
125 Description: "sysprop " + syspropFile.Rel(),
126 Output: cppFile,
127 ImplicitOutput: headerFile,
128 Input: syspropFile,
129 Args: map[string]string{
130 "headerOutDir": filepath.Dir(headerFile.String()),
131 "srcOutDir": filepath.Dir(cppFile.String()),
132 "includeName": syspropFile.Rel() + ".h",
133 },
134 })
135
136 return cppFile, headerFile
137}
138
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700139func genWinMsg(ctx android.ModuleContext, srcFile android.Path, flags builderFlags) (android.Path, android.Path) {
140 headerFile := android.GenPathWithExt(ctx, "windmc", srcFile, "h")
141 rcFile := android.GenPathWithExt(ctx, "windmc", srcFile, "rc")
142
143 windmcCmd := gccCmd(flags.toolchain, "windmc")
144
Colin Crossae887032017-10-23 17:16:14 -0700145 ctx.Build(pctx, android.BuildParams{
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700146 Rule: windmc,
147 Description: "windmc " + srcFile.Rel(),
148 Output: rcFile,
149 ImplicitOutput: headerFile,
150 Input: srcFile,
151 Args: map[string]string{
152 "windmcCmd": windmcCmd,
153 },
154 })
155
156 return rcFile, headerFile
157}
158
Colin Cross635c3b02016-05-18 15:37:25 -0700159func genSources(ctx android.ModuleContext, srcFiles android.Paths,
160 buildFlags builderFlags) (android.Paths, android.Paths) {
Colin Cross581c1892015-04-07 16:50:10 -0700161
Colin Cross635c3b02016-05-18 15:37:25 -0700162 var deps android.Paths
Colin Cross581c1892015-04-07 16:50:10 -0700163
Colin Cross2a252be2017-05-01 17:37:24 -0700164 var rsFiles android.Paths
165
Colin Cross581c1892015-04-07 16:50:10 -0700166 for i, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700167 switch srcFile.Ext() {
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800168 case ".y":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700169 cFile := android.GenPathWithExt(ctx, "yacc", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800170 srcFiles[i] = cFile
171 deps = append(deps, genYacc(ctx, srcFile, cFile, buildFlags.yaccFlags))
172 case ".yy":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700173 cppFile := android.GenPathWithExt(ctx, "yacc", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700174 srcFiles[i] = cppFile
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800175 deps = append(deps, genYacc(ctx, srcFile, cppFile, buildFlags.yaccFlags))
176 case ".l":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700177 cFile := android.GenPathWithExt(ctx, "lex", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800178 srcFiles[i] = cFile
179 genLex(ctx, srcFile, cFile)
180 case ".ll":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700181 cppFile := android.GenPathWithExt(ctx, "lex", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700182 srcFiles[i] = cppFile
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800183 genLex(ctx, srcFile, cppFile)
Colin Cross0c461f12016-10-20 16:11:43 -0700184 case ".proto":
Joe Onorato09e94ab2017-11-18 18:23:14 -0800185 ccFile, headerFile := genProto(ctx, srcFile, buildFlags.protoFlags,
Dan Willemsenab9f4262018-02-14 13:58:34 -0800186 buildFlags.protoOutParams, buildFlags.protoRoot)
Colin Cross6af17aa2017-09-20 12:59:05 -0700187 srcFiles[i] = ccFile
188 deps = append(deps, headerFile)
Dan Willemsene1240db2016-11-03 14:28:51 -0700189 case ".aidl":
190 cppFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp")
191 srcFiles[i] = cppFile
192 deps = append(deps, genAidl(ctx, srcFile, cppFile, buildFlags.aidlFlags)...)
Colin Cross2a252be2017-05-01 17:37:24 -0700193 case ".rs", ".fs":
194 cppFile := rsGeneratedCppFile(ctx, srcFile)
195 rsFiles = append(rsFiles, srcFiles[i])
196 srcFiles[i] = cppFile
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700197 case ".mc":
198 rcFile, headerFile := genWinMsg(ctx, srcFile, buildFlags)
199 srcFiles[i] = rcFile
200 deps = append(deps, headerFile)
Inseob Kim21f26902018-09-06 00:55:20 +0900201 case ".sysprop":
202 cppFile, headerFile := genSysprop(ctx, srcFile)
203 srcFiles[i] = cppFile
204 deps = append(deps, headerFile)
Colin Cross581c1892015-04-07 16:50:10 -0700205 }
206 }
207
Colin Cross2a252be2017-05-01 17:37:24 -0700208 if len(rsFiles) > 0 {
209 deps = append(deps, rsGenerateCpp(ctx, rsFiles, buildFlags.rsFlags)...)
210 }
211
Colin Cross581c1892015-04-07 16:50:10 -0700212 return srcFiles, deps
213}