blob: 7a22abdde44240ad1d1bbb7645748fe4e383cae9 [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 Willemsen76ef5ab2016-09-13 16:55:40 -070028 pctx.SourcePathVariable("lexCmd", "prebuilts/misc/${config.HostPrebuiltTag}/flex/flex-2.5.39")
29 pctx.SourcePathVariable("yaccCmd", "prebuilts/misc/${config.HostPrebuiltTag}/bison/bison")
Dan Willemsen34cc69e2015-09-23 15:26:20 -070030 pctx.SourcePathVariable("yaccDataDir", "external/bison/data")
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")
Colin Cross581c1892015-04-07 16:50:10 -070057)
58
Colin Cross635c3b02016-05-18 15:37:25 -070059func genYacc(ctx android.ModuleContext, yaccFile android.Path, outFile android.ModuleGenPath, yaccFlags string) (headerFile android.ModuleGenPath) {
Dan Willemsen21ec4902016-11-02 20:43:13 -070060 headerFile = android.GenPathWithExt(ctx, "yacc", yaccFile, "h")
Colin Cross581c1892015-04-07 16:50:10 -070061
Colin Cross635c3b02016-05-18 15:37:25 -070062 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -070063 Rule: yacc,
Colin Cross67a5c132017-05-09 13:45:28 -070064 Description: "yacc " + yaccFile.Rel(),
Dan Willemsen9f3c5742016-11-03 14:28:31 -070065 Output: outFile,
66 ImplicitOutput: headerFile,
67 Input: yaccFile,
Colin Cross581c1892015-04-07 16:50:10 -070068 Args: map[string]string{
69 "yaccFlags": yaccFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -070070 "hFile": headerFile.String(),
Colin Cross581c1892015-04-07 16:50:10 -070071 },
72 })
73
Dan Willemsenf0c73e02016-03-01 15:15:26 -080074 return headerFile
Colin Cross581c1892015-04-07 16:50:10 -070075}
76
Dan Willemsene1240db2016-11-03 14:28:51 -070077func genAidl(ctx android.ModuleContext, aidlFile android.Path, outFile android.ModuleGenPath, aidlFlags string) android.Paths {
78
79 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -070080 Rule: aidl,
81 Description: "aidl " + aidlFile.Rel(),
82 Output: outFile,
83 Input: aidlFile,
Dan Willemsene1240db2016-11-03 14:28:51 -070084 Args: map[string]string{
85 "aidlFlags": aidlFlags,
86 "outDir": android.PathForModuleGen(ctx, "aidl").String(),
87 },
88 })
89
90 // TODO: This should return the generated headers, not the source file.
91 return android.Paths{outFile}
92}
93
Colin Cross635c3b02016-05-18 15:37:25 -070094func genLex(ctx android.ModuleContext, lexFile android.Path, outFile android.ModuleGenPath) {
95 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -070096 Rule: lex,
97 Description: "lex " + lexFile.Rel(),
98 Output: outFile,
99 Input: lexFile,
Colin Cross581c1892015-04-07 16:50:10 -0700100 })
Colin Cross581c1892015-04-07 16:50:10 -0700101}
102
Colin Cross635c3b02016-05-18 15:37:25 -0700103func genSources(ctx android.ModuleContext, srcFiles android.Paths,
104 buildFlags builderFlags) (android.Paths, android.Paths) {
Colin Cross581c1892015-04-07 16:50:10 -0700105
Colin Cross635c3b02016-05-18 15:37:25 -0700106 var deps android.Paths
Colin Cross581c1892015-04-07 16:50:10 -0700107
Colin Cross2a252be2017-05-01 17:37:24 -0700108 var rsFiles android.Paths
109
Colin Cross581c1892015-04-07 16:50:10 -0700110 for i, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700111 switch srcFile.Ext() {
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800112 case ".y":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700113 cFile := android.GenPathWithExt(ctx, "yacc", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800114 srcFiles[i] = cFile
115 deps = append(deps, genYacc(ctx, srcFile, cFile, buildFlags.yaccFlags))
116 case ".yy":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700117 cppFile := android.GenPathWithExt(ctx, "yacc", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700118 srcFiles[i] = cppFile
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800119 deps = append(deps, genYacc(ctx, srcFile, cppFile, buildFlags.yaccFlags))
120 case ".l":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700121 cFile := android.GenPathWithExt(ctx, "lex", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800122 srcFiles[i] = cFile
123 genLex(ctx, srcFile, cFile)
124 case ".ll":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700125 cppFile := android.GenPathWithExt(ctx, "lex", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700126 srcFiles[i] = cppFile
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800127 genLex(ctx, srcFile, cppFile)
Colin Cross0c461f12016-10-20 16:11:43 -0700128 case ".proto":
129 cppFile, headerFile := genProto(ctx, srcFile, buildFlags.protoFlags)
130 srcFiles[i] = cppFile
131 deps = append(deps, headerFile)
Dan Willemsene1240db2016-11-03 14:28:51 -0700132 case ".aidl":
133 cppFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp")
134 srcFiles[i] = cppFile
135 deps = append(deps, genAidl(ctx, srcFile, cppFile, buildFlags.aidlFlags)...)
Colin Cross2a252be2017-05-01 17:37:24 -0700136 case ".rs", ".fs":
137 cppFile := rsGeneratedCppFile(ctx, srcFile)
138 rsFiles = append(rsFiles, srcFiles[i])
139 srcFiles[i] = cppFile
Colin Cross581c1892015-04-07 16:50:10 -0700140 }
141 }
142
Colin Cross2a252be2017-05-01 17:37:24 -0700143 if len(rsFiles) > 0 {
144 deps = append(deps, rsGenerateCpp(ctx, rsFiles, buildFlags.rsFlags)...)
145 }
146
Colin Cross581c1892015-04-07 16:50:10 -0700147 return srcFiles, deps
148}