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 | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 27 | |
| 28 | pctx.HostBinToolVariable("aidlCmd", "aidl-cpp") |
Inseob Kim | 21f2690 | 2018-09-06 00:55:20 +0900 | [diff] [blame] | 29 | pctx.HostBinToolVariable("syspropCmd", "sysprop_cpp") |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | var ( |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 33 | lex = pctx.AndroidStaticRule("lex", |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 34 | blueprint.RuleParams{ |
| 35 | Command: "$lexCmd -o$out $in", |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 36 | CommandDeps: []string{"$lexCmd"}, |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 37 | }) |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 38 | |
| 39 | aidl = pctx.AndroidStaticRule("aidl", |
| 40 | blueprint.RuleParams{ |
Steven Moreland | b468bca | 2018-07-06 11:36:32 -0700 | [diff] [blame] | 41 | Command: "$aidlCmd -d${out}.d --ninja $aidlFlags $in $outDir $out", |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 42 | CommandDeps: []string{"$aidlCmd"}, |
| 43 | Depfile: "${out}.d", |
| 44 | Deps: blueprint.DepsGCC, |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 45 | }, |
| 46 | "aidlFlags", "outDir") |
Dan Willemsen | 4f1c3d4 | 2017-09-09 01:15:26 -0700 | [diff] [blame] | 47 | |
Inseob Kim | 21f2690 | 2018-09-06 00:55:20 +0900 | [diff] [blame] | 48 | sysprop = pctx.AndroidStaticRule("sysprop", |
| 49 | blueprint.RuleParams{ |
Inseob Kim | c0907f1 | 2019-02-08 21:00:45 +0900 | [diff] [blame] | 50 | Command: "$syspropCmd --header-dir=$headerOutDir --system-header-dir=$systemOutDir " + |
| 51 | "--source-dir=$srcOutDir --include-name=$includeName $in", |
Inseob Kim | 21f2690 | 2018-09-06 00:55:20 +0900 | [diff] [blame] | 52 | CommandDeps: []string{"$syspropCmd"}, |
| 53 | }, |
Inseob Kim | c0907f1 | 2019-02-08 21:00:45 +0900 | [diff] [blame] | 54 | "headerOutDir", "systemOutDir", "srcOutDir", "includeName") |
Inseob Kim | 21f2690 | 2018-09-06 00:55:20 +0900 | [diff] [blame] | 55 | |
Dan Willemsen | 4f1c3d4 | 2017-09-09 01:15:26 -0700 | [diff] [blame] | 56 | windmc = pctx.AndroidStaticRule("windmc", |
| 57 | blueprint.RuleParams{ |
| 58 | Command: "$windmcCmd -r$$(dirname $out) -h$$(dirname $out) $in", |
| 59 | CommandDeps: []string{"$windmcCmd"}, |
| 60 | }, |
| 61 | "windmcCmd") |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 62 | ) |
| 63 | |
Dan Willemsen | 4e0aa23 | 2019-04-10 22:59:54 -0700 | [diff] [blame^] | 64 | type YaccProperties struct { |
| 65 | // list of module-specific flags that will be used for .y and .yy compiles |
| 66 | Flags []string |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 67 | |
Dan Willemsen | 4e0aa23 | 2019-04-10 22:59:54 -0700 | [diff] [blame^] | 68 | // whether the yacc files will produce a location.hh file |
| 69 | Gen_location_hh *bool |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 70 | |
Dan Willemsen | 4e0aa23 | 2019-04-10 22:59:54 -0700 | [diff] [blame^] | 71 | // whether the yacc files will product a position.hh file |
| 72 | Gen_position_hh *bool |
| 73 | } |
| 74 | |
| 75 | func genYacc(ctx android.ModuleContext, rule *android.RuleBuilder, yaccFile android.Path, |
| 76 | outFile android.ModuleGenPath, props *YaccProperties) (headerFiles android.Paths) { |
| 77 | |
| 78 | outDir := android.PathForModuleGen(ctx, "yacc") |
| 79 | headerFile := android.GenPathWithExt(ctx, "yacc", yaccFile, "h") |
| 80 | ret := android.Paths{headerFile} |
| 81 | |
| 82 | cmd := rule.Command() |
| 83 | |
| 84 | // Fix up #line markers to not use the sbox temporary directory |
| 85 | sedCmd := "sed -i.bak 's#__SBOX_OUT_DIR__#" + outDir.String() + "#'" |
| 86 | rule.Command().Text(sedCmd).Input(outFile) |
| 87 | rule.Command().Text(sedCmd).Input(headerFile) |
| 88 | |
| 89 | var flags []string |
| 90 | if props != nil { |
| 91 | flags = props.Flags |
| 92 | |
| 93 | if Bool(props.Gen_location_hh) { |
| 94 | locationHeader := outFile.InSameDir(ctx, "location.hh") |
| 95 | ret = append(ret, locationHeader) |
| 96 | cmd.ImplicitOutput(locationHeader) |
| 97 | rule.Command().Text(sedCmd).Input(locationHeader) |
| 98 | } |
| 99 | if Bool(props.Gen_position_hh) { |
| 100 | positionHeader := outFile.InSameDir(ctx, "position.hh") |
| 101 | ret = append(ret, positionHeader) |
| 102 | cmd.ImplicitOutput(positionHeader) |
| 103 | rule.Command().Text(sedCmd).Input(positionHeader) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | cmd.Text("BISON_PKGDATADIR=prebuilts/build-tools/common/bison"). |
| 108 | Tool(ctx.Config().PrebuiltBuildTool(ctx, "bison")). |
| 109 | Flag("-d"). |
| 110 | Flags(flags). |
| 111 | FlagWithOutput("--defines=", headerFile). |
| 112 | Flag("-o").Output(outFile).Input(yaccFile) |
| 113 | |
| 114 | return ret |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 117 | 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] | 118 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 119 | Rule: aidl, |
| 120 | Description: "aidl " + aidlFile.Rel(), |
| 121 | Output: outFile, |
| 122 | Input: aidlFile, |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 123 | Args: map[string]string{ |
| 124 | "aidlFlags": aidlFlags, |
| 125 | "outDir": android.PathForModuleGen(ctx, "aidl").String(), |
| 126 | }, |
| 127 | }) |
| 128 | |
| 129 | // TODO: This should return the generated headers, not the source file. |
| 130 | return android.Paths{outFile} |
| 131 | } |
| 132 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 133 | func genLex(ctx android.ModuleContext, lexFile android.Path, outFile android.ModuleGenPath) { |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 134 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 135 | Rule: lex, |
| 136 | Description: "lex " + lexFile.Rel(), |
| 137 | Output: outFile, |
| 138 | Input: lexFile, |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 139 | }) |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Inseob Kim | 21f2690 | 2018-09-06 00:55:20 +0900 | [diff] [blame] | 142 | func genSysprop(ctx android.ModuleContext, syspropFile android.Path) (android.Path, android.Path) { |
| 143 | headerFile := android.PathForModuleGen(ctx, "sysprop", "include", syspropFile.Rel()+".h") |
Inseob Kim | c0907f1 | 2019-02-08 21:00:45 +0900 | [diff] [blame] | 144 | systemHeaderFile := android.PathForModuleGen(ctx, "sysprop/system", "include", syspropFile.Rel()+".h") |
Inseob Kim | 21f2690 | 2018-09-06 00:55:20 +0900 | [diff] [blame] | 145 | cppFile := android.PathForModuleGen(ctx, "sysprop", syspropFile.Rel()+".cpp") |
| 146 | |
| 147 | ctx.Build(pctx, android.BuildParams{ |
| 148 | Rule: sysprop, |
| 149 | Description: "sysprop " + syspropFile.Rel(), |
| 150 | Output: cppFile, |
| 151 | ImplicitOutput: headerFile, |
| 152 | Input: syspropFile, |
| 153 | Args: map[string]string{ |
| 154 | "headerOutDir": filepath.Dir(headerFile.String()), |
Inseob Kim | c0907f1 | 2019-02-08 21:00:45 +0900 | [diff] [blame] | 155 | "systemOutDir": filepath.Dir(systemHeaderFile.String()), |
Inseob Kim | 21f2690 | 2018-09-06 00:55:20 +0900 | [diff] [blame] | 156 | "srcOutDir": filepath.Dir(cppFile.String()), |
| 157 | "includeName": syspropFile.Rel() + ".h", |
| 158 | }, |
| 159 | }) |
| 160 | |
| 161 | return cppFile, headerFile |
| 162 | } |
| 163 | |
Dan Willemsen | 4f1c3d4 | 2017-09-09 01:15:26 -0700 | [diff] [blame] | 164 | func genWinMsg(ctx android.ModuleContext, srcFile android.Path, flags builderFlags) (android.Path, android.Path) { |
| 165 | headerFile := android.GenPathWithExt(ctx, "windmc", srcFile, "h") |
| 166 | rcFile := android.GenPathWithExt(ctx, "windmc", srcFile, "rc") |
| 167 | |
| 168 | windmcCmd := gccCmd(flags.toolchain, "windmc") |
| 169 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 170 | ctx.Build(pctx, android.BuildParams{ |
Dan Willemsen | 4f1c3d4 | 2017-09-09 01:15:26 -0700 | [diff] [blame] | 171 | Rule: windmc, |
| 172 | Description: "windmc " + srcFile.Rel(), |
| 173 | Output: rcFile, |
| 174 | ImplicitOutput: headerFile, |
| 175 | Input: srcFile, |
| 176 | Args: map[string]string{ |
| 177 | "windmcCmd": windmcCmd, |
| 178 | }, |
| 179 | }) |
| 180 | |
| 181 | return rcFile, headerFile |
| 182 | } |
| 183 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 184 | func genSources(ctx android.ModuleContext, srcFiles android.Paths, |
| 185 | buildFlags builderFlags) (android.Paths, android.Paths) { |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 186 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 187 | var deps android.Paths |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 188 | var rsFiles android.Paths |
| 189 | |
Dan Willemsen | 4e0aa23 | 2019-04-10 22:59:54 -0700 | [diff] [blame^] | 190 | var yaccRule_ *android.RuleBuilder |
| 191 | yaccRule := func() *android.RuleBuilder { |
| 192 | if yaccRule_ == nil { |
| 193 | yaccRule_ = android.NewRuleBuilder().Sbox(android.PathForModuleGen(ctx, "yacc")) |
| 194 | } |
| 195 | return yaccRule_ |
| 196 | } |
| 197 | |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 198 | for i, srcFile := range srcFiles { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 199 | switch srcFile.Ext() { |
Dan Willemsen | f0c73e0 | 2016-03-01 15:15:26 -0800 | [diff] [blame] | 200 | case ".y": |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 201 | cFile := android.GenPathWithExt(ctx, "yacc", srcFile, "c") |
Dan Willemsen | f0c73e0 | 2016-03-01 15:15:26 -0800 | [diff] [blame] | 202 | srcFiles[i] = cFile |
Dan Willemsen | 4e0aa23 | 2019-04-10 22:59:54 -0700 | [diff] [blame^] | 203 | deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cFile, buildFlags.yacc)...) |
Dan Willemsen | f0c73e0 | 2016-03-01 15:15:26 -0800 | [diff] [blame] | 204 | case ".yy": |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 205 | cppFile := android.GenPathWithExt(ctx, "yacc", srcFile, "cpp") |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 206 | srcFiles[i] = cppFile |
Dan Willemsen | 4e0aa23 | 2019-04-10 22:59:54 -0700 | [diff] [blame^] | 207 | deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cppFile, buildFlags.yacc)...) |
Dan Willemsen | f0c73e0 | 2016-03-01 15:15:26 -0800 | [diff] [blame] | 208 | case ".l": |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 209 | cFile := android.GenPathWithExt(ctx, "lex", srcFile, "c") |
Dan Willemsen | f0c73e0 | 2016-03-01 15:15:26 -0800 | [diff] [blame] | 210 | srcFiles[i] = cFile |
| 211 | genLex(ctx, srcFile, cFile) |
| 212 | case ".ll": |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 213 | cppFile := android.GenPathWithExt(ctx, "lex", srcFile, "cpp") |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 214 | srcFiles[i] = cppFile |
Dan Willemsen | f0c73e0 | 2016-03-01 15:15:26 -0800 | [diff] [blame] | 215 | genLex(ctx, srcFile, cppFile) |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 216 | case ".proto": |
Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 217 | ccFile, headerFile := genProto(ctx, srcFile, buildFlags) |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 218 | srcFiles[i] = ccFile |
| 219 | deps = append(deps, headerFile) |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 220 | case ".aidl": |
| 221 | cppFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp") |
| 222 | srcFiles[i] = cppFile |
| 223 | deps = append(deps, genAidl(ctx, srcFile, cppFile, buildFlags.aidlFlags)...) |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 224 | case ".rs", ".fs": |
| 225 | cppFile := rsGeneratedCppFile(ctx, srcFile) |
| 226 | rsFiles = append(rsFiles, srcFiles[i]) |
| 227 | srcFiles[i] = cppFile |
Dan Willemsen | 4f1c3d4 | 2017-09-09 01:15:26 -0700 | [diff] [blame] | 228 | case ".mc": |
| 229 | rcFile, headerFile := genWinMsg(ctx, srcFile, buildFlags) |
| 230 | srcFiles[i] = rcFile |
| 231 | deps = append(deps, headerFile) |
Inseob Kim | 21f2690 | 2018-09-06 00:55:20 +0900 | [diff] [blame] | 232 | case ".sysprop": |
| 233 | cppFile, headerFile := genSysprop(ctx, srcFile) |
| 234 | srcFiles[i] = cppFile |
| 235 | deps = append(deps, headerFile) |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
Dan Willemsen | 4e0aa23 | 2019-04-10 22:59:54 -0700 | [diff] [blame^] | 239 | if yaccRule_ != nil { |
| 240 | yaccRule_.Build(pctx, ctx, "yacc", "gen yacc") |
| 241 | } |
| 242 | |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 243 | if len(rsFiles) > 0 { |
| 244 | deps = append(deps, rsGenerateCpp(ctx, rsFiles, buildFlags.rsFlags)...) |
| 245 | } |
| 246 | |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 247 | return srcFiles, deps |
| 248 | } |