blob: c794f5c380e03f14f65f71280aa6f37f7e70f19a [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 (
Colin Cross581c1892015-04-07 16:50:10 -070022 "github.com/google/blueprint"
Colin Cross581c1892015-04-07 16:50:10 -070023
Colin Cross635c3b02016-05-18 15:37:25 -070024 "android/soong/android"
Colin Cross581c1892015-04-07 16:50:10 -070025)
26
27func init() {
Dan Willemsenb7adae82018-05-24 15:45:21 -070028 pctx.SourcePathVariable("lexCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/flex")
Dan Willemsen6f46a382018-01-08 17:26:13 -080029 pctx.SourcePathVariable("yaccCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/bison")
30 pctx.SourcePathVariable("yaccDataDir", "prebuilts/build-tools/common/bison")
Dan Willemsene1240db2016-11-03 14:28:51 -070031
32 pctx.HostBinToolVariable("aidlCmd", "aidl-cpp")
Colin Cross581c1892015-04-07 16:50:10 -070033}
34
35var (
Colin Cross9d45bb72016-08-29 16:14:13 -070036 yacc = pctx.AndroidStaticRule("yacc",
Colin Cross581c1892015-04-07 16:50:10 -070037 blueprint.RuleParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -070038 Command: "BISON_PKGDATADIR=$yaccDataDir $yaccCmd -d $yaccFlags --defines=$hFile -o $out $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080039 CommandDeps: []string{"$yaccCmd"},
Colin Cross581c1892015-04-07 16:50:10 -070040 },
Dan Willemsen9f3c5742016-11-03 14:28:31 -070041 "yaccFlags", "hFile")
Colin Cross581c1892015-04-07 16:50:10 -070042
Colin Cross9d45bb72016-08-29 16:14:13 -070043 lex = pctx.AndroidStaticRule("lex",
Colin Cross581c1892015-04-07 16:50:10 -070044 blueprint.RuleParams{
45 Command: "$lexCmd -o$out $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080046 CommandDeps: []string{"$lexCmd"},
Colin Cross581c1892015-04-07 16:50:10 -070047 })
Dan Willemsene1240db2016-11-03 14:28:51 -070048
49 aidl = pctx.AndroidStaticRule("aidl",
50 blueprint.RuleParams{
51 Command: "$aidlCmd -d${out}.d -ninja $aidlFlags $in $outDir $out",
52 CommandDeps: []string{"$aidlCmd"},
53 Depfile: "${out}.d",
54 Deps: blueprint.DepsGCC,
Dan Willemsene1240db2016-11-03 14:28:51 -070055 },
56 "aidlFlags", "outDir")
Dan Willemsen4f1c3d42017-09-09 01:15:26 -070057
58 windmc = pctx.AndroidStaticRule("windmc",
59 blueprint.RuleParams{
60 Command: "$windmcCmd -r$$(dirname $out) -h$$(dirname $out) $in",
61 CommandDeps: []string{"$windmcCmd"},
62 },
63 "windmcCmd")
Colin Cross581c1892015-04-07 16:50:10 -070064)
65
Colin Cross635c3b02016-05-18 15:37:25 -070066func genYacc(ctx android.ModuleContext, yaccFile android.Path, outFile android.ModuleGenPath, yaccFlags string) (headerFile android.ModuleGenPath) {
Dan Willemsen21ec4902016-11-02 20:43:13 -070067 headerFile = android.GenPathWithExt(ctx, "yacc", yaccFile, "h")
Colin Cross581c1892015-04-07 16:50:10 -070068
Colin Crossae887032017-10-23 17:16:14 -070069 ctx.Build(pctx, android.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -070070 Rule: yacc,
Colin Cross67a5c132017-05-09 13:45:28 -070071 Description: "yacc " + yaccFile.Rel(),
Dan Willemsen9f3c5742016-11-03 14:28:31 -070072 Output: outFile,
73 ImplicitOutput: headerFile,
74 Input: yaccFile,
Colin Cross581c1892015-04-07 16:50:10 -070075 Args: map[string]string{
76 "yaccFlags": yaccFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -070077 "hFile": headerFile.String(),
Colin Cross581c1892015-04-07 16:50:10 -070078 },
79 })
80
Dan Willemsenf0c73e02016-03-01 15:15:26 -080081 return headerFile
Colin Cross581c1892015-04-07 16:50:10 -070082}
83
Dan Willemsene1240db2016-11-03 14:28:51 -070084func genAidl(ctx android.ModuleContext, aidlFile android.Path, outFile android.ModuleGenPath, aidlFlags string) android.Paths {
85
Colin Crossae887032017-10-23 17:16:14 -070086 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -070087 Rule: aidl,
88 Description: "aidl " + aidlFile.Rel(),
89 Output: outFile,
90 Input: aidlFile,
Dan Willemsene1240db2016-11-03 14:28:51 -070091 Args: map[string]string{
92 "aidlFlags": aidlFlags,
93 "outDir": android.PathForModuleGen(ctx, "aidl").String(),
94 },
95 })
96
97 // TODO: This should return the generated headers, not the source file.
98 return android.Paths{outFile}
99}
100
Colin Cross635c3b02016-05-18 15:37:25 -0700101func genLex(ctx android.ModuleContext, lexFile android.Path, outFile android.ModuleGenPath) {
Colin Crossae887032017-10-23 17:16:14 -0700102 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700103 Rule: lex,
104 Description: "lex " + lexFile.Rel(),
105 Output: outFile,
106 Input: lexFile,
Colin Cross581c1892015-04-07 16:50:10 -0700107 })
Colin Cross581c1892015-04-07 16:50:10 -0700108}
109
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700110func genWinMsg(ctx android.ModuleContext, srcFile android.Path, flags builderFlags) (android.Path, android.Path) {
111 headerFile := android.GenPathWithExt(ctx, "windmc", srcFile, "h")
112 rcFile := android.GenPathWithExt(ctx, "windmc", srcFile, "rc")
113
114 windmcCmd := gccCmd(flags.toolchain, "windmc")
115
Colin Crossae887032017-10-23 17:16:14 -0700116 ctx.Build(pctx, android.BuildParams{
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700117 Rule: windmc,
118 Description: "windmc " + srcFile.Rel(),
119 Output: rcFile,
120 ImplicitOutput: headerFile,
121 Input: srcFile,
122 Args: map[string]string{
123 "windmcCmd": windmcCmd,
124 },
125 })
126
127 return rcFile, headerFile
128}
129
Colin Cross635c3b02016-05-18 15:37:25 -0700130func genSources(ctx android.ModuleContext, srcFiles android.Paths,
131 buildFlags builderFlags) (android.Paths, android.Paths) {
Colin Cross581c1892015-04-07 16:50:10 -0700132
Colin Cross635c3b02016-05-18 15:37:25 -0700133 var deps android.Paths
Colin Cross581c1892015-04-07 16:50:10 -0700134
Colin Cross2a252be2017-05-01 17:37:24 -0700135 var rsFiles android.Paths
136
Colin Cross581c1892015-04-07 16:50:10 -0700137 for i, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700138 switch srcFile.Ext() {
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800139 case ".y":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700140 cFile := android.GenPathWithExt(ctx, "yacc", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800141 srcFiles[i] = cFile
142 deps = append(deps, genYacc(ctx, srcFile, cFile, buildFlags.yaccFlags))
143 case ".yy":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700144 cppFile := android.GenPathWithExt(ctx, "yacc", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700145 srcFiles[i] = cppFile
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800146 deps = append(deps, genYacc(ctx, srcFile, cppFile, buildFlags.yaccFlags))
147 case ".l":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700148 cFile := android.GenPathWithExt(ctx, "lex", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800149 srcFiles[i] = cFile
150 genLex(ctx, srcFile, cFile)
151 case ".ll":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700152 cppFile := android.GenPathWithExt(ctx, "lex", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700153 srcFiles[i] = cppFile
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800154 genLex(ctx, srcFile, cppFile)
Colin Cross0c461f12016-10-20 16:11:43 -0700155 case ".proto":
Joe Onorato09e94ab2017-11-18 18:23:14 -0800156 ccFile, headerFile := genProto(ctx, srcFile, buildFlags.protoFlags,
Dan Willemsenab9f4262018-02-14 13:58:34 -0800157 buildFlags.protoOutParams, buildFlags.protoRoot)
Colin Cross6af17aa2017-09-20 12:59:05 -0700158 srcFiles[i] = ccFile
159 deps = append(deps, headerFile)
Dan Willemsene1240db2016-11-03 14:28:51 -0700160 case ".aidl":
161 cppFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp")
162 srcFiles[i] = cppFile
163 deps = append(deps, genAidl(ctx, srcFile, cppFile, buildFlags.aidlFlags)...)
Colin Cross2a252be2017-05-01 17:37:24 -0700164 case ".rs", ".fs":
165 cppFile := rsGeneratedCppFile(ctx, srcFile)
166 rsFiles = append(rsFiles, srcFiles[i])
167 srcFiles[i] = cppFile
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700168 case ".mc":
169 rcFile, headerFile := genWinMsg(ctx, srcFile, buildFlags)
170 srcFiles[i] = rcFile
171 deps = append(deps, headerFile)
Colin Cross581c1892015-04-07 16:50:10 -0700172 }
173 }
174
Colin Cross2a252be2017-05-01 17:37:24 -0700175 if len(rsFiles) > 0 {
176 deps = append(deps, rsGenerateCpp(ctx, rsFiles, buildFlags.rsFlags)...)
177 }
178
Colin Cross581c1892015-04-07 16:50:10 -0700179 return srcFiles, deps
180}