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