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