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