Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package 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 | |
| 21 | import ( |
Inseob Kim | 21f2690 | 2018-09-06 00:55:20 +0900 | [diff] [blame^] | 22 | "path/filepath" |
| 23 | |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 24 | "github.com/google/blueprint" |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 25 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 26 | "android/soong/android" |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | func init() { |
Dan Willemsen | b7adae8 | 2018-05-24 15:45:21 -0700 | [diff] [blame] | 30 | pctx.SourcePathVariable("lexCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/flex") |
Dan Willemsen | 6f46a38 | 2018-01-08 17:26:13 -0800 | [diff] [blame] | 31 | pctx.SourcePathVariable("yaccCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/bison") |
| 32 | pctx.SourcePathVariable("yaccDataDir", "prebuilts/build-tools/common/bison") |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 33 | |
| 34 | pctx.HostBinToolVariable("aidlCmd", "aidl-cpp") |
Inseob Kim | 21f2690 | 2018-09-06 00:55:20 +0900 | [diff] [blame^] | 35 | pctx.HostBinToolVariable("syspropCmd", "sysprop_cpp") |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | var ( |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 39 | yacc = pctx.AndroidStaticRule("yacc", |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 40 | blueprint.RuleParams{ |
Dan Willemsen | 9f3c574 | 2016-11-03 14:28:31 -0700 | [diff] [blame] | 41 | Command: "BISON_PKGDATADIR=$yaccDataDir $yaccCmd -d $yaccFlags --defines=$hFile -o $out $in", |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 42 | CommandDeps: []string{"$yaccCmd"}, |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 43 | }, |
Dan Willemsen | 9f3c574 | 2016-11-03 14:28:31 -0700 | [diff] [blame] | 44 | "yaccFlags", "hFile") |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 45 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 46 | lex = pctx.AndroidStaticRule("lex", |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 47 | blueprint.RuleParams{ |
| 48 | Command: "$lexCmd -o$out $in", |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 49 | CommandDeps: []string{"$lexCmd"}, |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 50 | }) |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 51 | |
| 52 | aidl = pctx.AndroidStaticRule("aidl", |
| 53 | blueprint.RuleParams{ |
Steven Moreland | b468bca | 2018-07-06 11:36:32 -0700 | [diff] [blame] | 54 | Command: "$aidlCmd -d${out}.d --ninja $aidlFlags $in $outDir $out", |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 55 | CommandDeps: []string{"$aidlCmd"}, |
| 56 | Depfile: "${out}.d", |
| 57 | Deps: blueprint.DepsGCC, |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 58 | }, |
| 59 | "aidlFlags", "outDir") |
Dan Willemsen | 4f1c3d4 | 2017-09-09 01:15:26 -0700 | [diff] [blame] | 60 | |
Inseob Kim | 21f2690 | 2018-09-06 00:55:20 +0900 | [diff] [blame^] | 61 | 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 Willemsen | 4f1c3d4 | 2017-09-09 01:15:26 -0700 | [diff] [blame] | 68 | 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 Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 74 | ) |
| 75 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 76 | func genYacc(ctx android.ModuleContext, yaccFile android.Path, outFile android.ModuleGenPath, yaccFlags string) (headerFile android.ModuleGenPath) { |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 77 | headerFile = android.GenPathWithExt(ctx, "yacc", yaccFile, "h") |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 78 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 79 | ctx.Build(pctx, android.BuildParams{ |
Dan Willemsen | 9f3c574 | 2016-11-03 14:28:31 -0700 | [diff] [blame] | 80 | Rule: yacc, |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 81 | Description: "yacc " + yaccFile.Rel(), |
Dan Willemsen | 9f3c574 | 2016-11-03 14:28:31 -0700 | [diff] [blame] | 82 | Output: outFile, |
| 83 | ImplicitOutput: headerFile, |
| 84 | Input: yaccFile, |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 85 | Args: map[string]string{ |
| 86 | "yaccFlags": yaccFlags, |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 87 | "hFile": headerFile.String(), |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 88 | }, |
| 89 | }) |
| 90 | |
Dan Willemsen | f0c73e0 | 2016-03-01 15:15:26 -0800 | [diff] [blame] | 91 | return headerFile |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 94 | func genAidl(ctx android.ModuleContext, aidlFile android.Path, outFile android.ModuleGenPath, aidlFlags string) android.Paths { |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 95 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 96 | Rule: aidl, |
| 97 | Description: "aidl " + aidlFile.Rel(), |
| 98 | Output: outFile, |
| 99 | Input: aidlFile, |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 100 | 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 Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 110 | func genLex(ctx android.ModuleContext, lexFile android.Path, outFile android.ModuleGenPath) { |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 111 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 112 | Rule: lex, |
| 113 | Description: "lex " + lexFile.Rel(), |
| 114 | Output: outFile, |
| 115 | Input: lexFile, |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 116 | }) |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Inseob Kim | 21f2690 | 2018-09-06 00:55:20 +0900 | [diff] [blame^] | 119 | func 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 Willemsen | 4f1c3d4 | 2017-09-09 01:15:26 -0700 | [diff] [blame] | 139 | func 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 Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 145 | ctx.Build(pctx, android.BuildParams{ |
Dan Willemsen | 4f1c3d4 | 2017-09-09 01:15:26 -0700 | [diff] [blame] | 146 | 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 Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 159 | func genSources(ctx android.ModuleContext, srcFiles android.Paths, |
| 160 | buildFlags builderFlags) (android.Paths, android.Paths) { |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 161 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 162 | var deps android.Paths |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 163 | |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 164 | var rsFiles android.Paths |
| 165 | |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 166 | for i, srcFile := range srcFiles { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 167 | switch srcFile.Ext() { |
Dan Willemsen | f0c73e0 | 2016-03-01 15:15:26 -0800 | [diff] [blame] | 168 | case ".y": |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 169 | cFile := android.GenPathWithExt(ctx, "yacc", srcFile, "c") |
Dan Willemsen | f0c73e0 | 2016-03-01 15:15:26 -0800 | [diff] [blame] | 170 | srcFiles[i] = cFile |
| 171 | deps = append(deps, genYacc(ctx, srcFile, cFile, buildFlags.yaccFlags)) |
| 172 | case ".yy": |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 173 | cppFile := android.GenPathWithExt(ctx, "yacc", srcFile, "cpp") |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 174 | srcFiles[i] = cppFile |
Dan Willemsen | f0c73e0 | 2016-03-01 15:15:26 -0800 | [diff] [blame] | 175 | deps = append(deps, genYacc(ctx, srcFile, cppFile, buildFlags.yaccFlags)) |
| 176 | case ".l": |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 177 | cFile := android.GenPathWithExt(ctx, "lex", srcFile, "c") |
Dan Willemsen | f0c73e0 | 2016-03-01 15:15:26 -0800 | [diff] [blame] | 178 | srcFiles[i] = cFile |
| 179 | genLex(ctx, srcFile, cFile) |
| 180 | case ".ll": |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 181 | cppFile := android.GenPathWithExt(ctx, "lex", srcFile, "cpp") |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 182 | srcFiles[i] = cppFile |
Dan Willemsen | f0c73e0 | 2016-03-01 15:15:26 -0800 | [diff] [blame] | 183 | genLex(ctx, srcFile, cppFile) |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 184 | case ".proto": |
Joe Onorato | 09e94ab | 2017-11-18 18:23:14 -0800 | [diff] [blame] | 185 | ccFile, headerFile := genProto(ctx, srcFile, buildFlags.protoFlags, |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 186 | buildFlags.protoOutParams, buildFlags.protoRoot) |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 187 | srcFiles[i] = ccFile |
| 188 | deps = append(deps, headerFile) |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 189 | 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 Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 193 | case ".rs", ".fs": |
| 194 | cppFile := rsGeneratedCppFile(ctx, srcFile) |
| 195 | rsFiles = append(rsFiles, srcFiles[i]) |
| 196 | srcFiles[i] = cppFile |
Dan Willemsen | 4f1c3d4 | 2017-09-09 01:15:26 -0700 | [diff] [blame] | 197 | case ".mc": |
| 198 | rcFile, headerFile := genWinMsg(ctx, srcFile, buildFlags) |
| 199 | srcFiles[i] = rcFile |
| 200 | deps = append(deps, headerFile) |
Inseob Kim | 21f2690 | 2018-09-06 00:55:20 +0900 | [diff] [blame^] | 201 | case ".sysprop": |
| 202 | cppFile, headerFile := genSysprop(ctx, srcFile) |
| 203 | srcFiles[i] = cppFile |
| 204 | deps = append(deps, headerFile) |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 208 | if len(rsFiles) > 0 { |
| 209 | deps = append(deps, rsGenerateCpp(ctx, rsFiles, buildFlags.rsFlags)...) |
| 210 | } |
| 211 | |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 212 | return srcFiles, deps |
| 213 | } |