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 15package cc 16 17import ( 18 "path/filepath" 19 20 "github.com/google/blueprint" 21 22 "android/soong/android" 23) 24 25func init() { 26 pctx.SourcePathVariable("lexCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/flex") 27 pctx.SourcePathVariable("yaccCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/bison") 28 pctx.SourcePathVariable("yaccDataDir", "prebuilts/build-tools/common/bison") 29 30 pctx.HostBinToolVariable("aidlCmd", "aidl-cpp") 31 pctx.HostBinToolVariable("syspropCmd", "sysprop_cpp") 32} 33 34var ( 35 yacc = pctx.AndroidStaticRule("yacc", 36 blueprint.RuleParams{ 37 Command: "BISON_PKGDATADIR=$yaccDataDir $yaccCmd -d $yaccFlags --defines=$hFile -o $out $in", 38 CommandDeps: []string{"$yaccCmd"}, 39 }, 40 "yaccFlags", "hFile") 41 42 lex = pctx.AndroidStaticRule("lex", 43 blueprint.RuleParams{ 44 Command: "$lexCmd -o$out $in", 45 CommandDeps: []string{"$lexCmd"}, 46 }) 47 48 aidl = pctx.AndroidStaticRule("aidl", 49 blueprint.RuleParams{ 50 Command: "$aidlCmd -d${out}.d --ninja $aidlFlags $in $outDir $out", 51 CommandDeps: []string{"$aidlCmd"}, 52 Depfile: "${out}.d", 53 Deps: blueprint.DepsGCC, 54 }, 55 "aidlFlags", "outDir") 56 57 sysprop = pctx.AndroidStaticRule("sysprop", 58 blueprint.RuleParams{ 59 Command: "$syspropCmd --header-dir=$headerOutDir --system-header-dir=$systemOutDir " + 60 "--source-dir=$srcOutDir --include-name=$includeName $in", 61 CommandDeps: []string{"$syspropCmd"}, 62 }, 63 "headerOutDir", "systemOutDir", "srcOutDir", "includeName") 64 65 windmc = pctx.AndroidStaticRule("windmc", 66 blueprint.RuleParams{ 67 Command: "$windmcCmd -r$$(dirname $out) -h$$(dirname $out) $in", 68 CommandDeps: []string{"$windmcCmd"}, 69 }, 70 "windmcCmd") 71) 72 73func genYacc(ctx android.ModuleContext, yaccFile android.Path, outFile android.ModuleGenPath, yaccFlags string) (headerFile android.ModuleGenPath) { 74 headerFile = android.GenPathWithExt(ctx, "yacc", yaccFile, "h") 75 76 ctx.Build(pctx, android.BuildParams{ 77 Rule: yacc, 78 Description: "yacc " + yaccFile.Rel(), 79 Output: outFile, 80 ImplicitOutput: headerFile, 81 Input: yaccFile, 82 Args: map[string]string{ 83 "yaccFlags": yaccFlags, 84 "hFile": headerFile.String(), 85 }, 86 }) 87 88 return headerFile 89} 90 91func genAidl(ctx android.ModuleContext, aidlFile android.Path, outFile android.ModuleGenPath, aidlFlags string) android.Paths { 92 ctx.Build(pctx, android.BuildParams{ 93 Rule: aidl, 94 Description: "aidl " + aidlFile.Rel(), 95 Output: outFile, 96 Input: aidlFile, 97 Args: map[string]string{ 98 "aidlFlags": aidlFlags, 99 "outDir": android.PathForModuleGen(ctx, "aidl").String(), 100 }, 101 }) 102 103 // TODO: This should return the generated headers, not the source file. 104 return android.Paths{outFile} 105} 106 107func genLex(ctx android.ModuleContext, lexFile android.Path, outFile android.ModuleGenPath) { 108 ctx.Build(pctx, android.BuildParams{ 109 Rule: lex, 110 Description: "lex " + lexFile.Rel(), 111 Output: outFile, 112 Input: lexFile, 113 }) 114} 115 116func genSysprop(ctx android.ModuleContext, syspropFile android.Path) (android.Path, android.Path) { 117 headerFile := android.PathForModuleGen(ctx, "sysprop", "include", syspropFile.Rel()+".h") 118 systemHeaderFile := android.PathForModuleGen(ctx, "sysprop/system", "include", syspropFile.Rel()+".h") 119 cppFile := android.PathForModuleGen(ctx, "sysprop", syspropFile.Rel()+".cpp") 120 121 ctx.Build(pctx, android.BuildParams{ 122 Rule: sysprop, 123 Description: "sysprop " + syspropFile.Rel(), 124 Output: cppFile, 125 ImplicitOutput: headerFile, 126 Input: syspropFile, 127 Args: map[string]string{ 128 "headerOutDir": filepath.Dir(headerFile.String()), 129 "systemOutDir": filepath.Dir(systemHeaderFile.String()), 130 "srcOutDir": filepath.Dir(cppFile.String()), 131 "includeName": syspropFile.Rel() + ".h", 132 }, 133 }) 134 135 return cppFile, headerFile 136} 137 138func genWinMsg(ctx android.ModuleContext, srcFile android.Path, flags builderFlags) (android.Path, android.Path) { 139 headerFile := android.GenPathWithExt(ctx, "windmc", srcFile, "h") 140 rcFile := android.GenPathWithExt(ctx, "windmc", srcFile, "rc") 141 142 windmcCmd := gccCmd(flags.toolchain, "windmc") 143 144 ctx.Build(pctx, android.BuildParams{ 145 Rule: windmc, 146 Description: "windmc " + srcFile.Rel(), 147 Output: rcFile, 148 ImplicitOutput: headerFile, 149 Input: srcFile, 150 Args: map[string]string{ 151 "windmcCmd": windmcCmd, 152 }, 153 }) 154 155 return rcFile, headerFile 156} 157 158func genSources(ctx android.ModuleContext, srcFiles android.Paths, 159 buildFlags builderFlags) (android.Paths, android.Paths) { 160 161 var deps android.Paths 162 163 var rsFiles android.Paths 164 165 for i, srcFile := range srcFiles { 166 switch srcFile.Ext() { 167 case ".y": 168 cFile := android.GenPathWithExt(ctx, "yacc", srcFile, "c") 169 srcFiles[i] = cFile 170 deps = append(deps, genYacc(ctx, srcFile, cFile, buildFlags.yaccFlags)) 171 case ".yy": 172 cppFile := android.GenPathWithExt(ctx, "yacc", srcFile, "cpp") 173 srcFiles[i] = cppFile 174 deps = append(deps, genYacc(ctx, srcFile, cppFile, buildFlags.yaccFlags)) 175 case ".l": 176 cFile := android.GenPathWithExt(ctx, "lex", srcFile, "c") 177 srcFiles[i] = cFile 178 genLex(ctx, srcFile, cFile) 179 case ".ll": 180 cppFile := android.GenPathWithExt(ctx, "lex", srcFile, "cpp") 181 srcFiles[i] = cppFile 182 genLex(ctx, srcFile, cppFile) 183 case ".proto": 184 ccFile, headerFile := genProto(ctx, srcFile, buildFlags) 185 srcFiles[i] = ccFile 186 deps = append(deps, headerFile) 187 case ".aidl": 188 cppFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp") 189 srcFiles[i] = cppFile 190 deps = append(deps, genAidl(ctx, srcFile, cppFile, buildFlags.aidlFlags)...) 191 case ".rs", ".fs": 192 cppFile := rsGeneratedCppFile(ctx, srcFile) 193 rsFiles = append(rsFiles, srcFiles[i]) 194 srcFiles[i] = cppFile 195 case ".mc": 196 rcFile, headerFile := genWinMsg(ctx, srcFile, buildFlags) 197 srcFiles[i] = rcFile 198 deps = append(deps, headerFile) 199 case ".sysprop": 200 cppFile, headerFile := genSysprop(ctx, srcFile) 201 srcFiles[i] = cppFile 202 deps = append(deps, headerFile) 203 } 204 } 205 206 if len(rsFiles) > 0 { 207 deps = append(deps, rsGenerateCpp(ctx, rsFiles, buildFlags.rsFlags)...) 208 } 209 210 return srcFiles, deps 211} 212