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 | |
| 17 | // This file generates the final rules for compiling all C/C++. All properties related to |
| 18 | // compiling should have been translated into builderFlags or another argument to the Transform* |
| 19 | // functions. |
| 20 | |
| 21 | import ( |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint" |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 23 | |
| 24 | "android/soong/common" |
| 25 | ) |
| 26 | |
| 27 | func init() { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 28 | pctx.SourcePathVariable("lexCmd", "prebuilts/misc/${HostPrebuiltTag}/flex/flex-2.5.39") |
| 29 | pctx.SourcePathVariable("yaccCmd", "prebuilts/misc/${HostPrebuiltTag}/bison/bison") |
| 30 | pctx.SourcePathVariable("yaccDataDir", "external/bison/data") |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | var ( |
| 34 | yacc = pctx.StaticRule("yacc", |
| 35 | blueprint.RuleParams{ |
| 36 | Command: "BISON_PKGDATADIR=$yaccDataDir $yaccCmd -d $yaccFlags -o $cppFile $in && " + |
| 37 | "cp -f $hppFile $hFile", |
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 | Description: "yacc $out", |
| 40 | }, |
| 41 | "yaccFlags", "cppFile", "hppFile", "hFile") |
| 42 | |
| 43 | lex = pctx.StaticRule("lex", |
| 44 | blueprint.RuleParams{ |
| 45 | Command: "$lexCmd -o$out $in", |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 46 | CommandDeps: []string{"$lexCmd"}, |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 47 | Description: "lex $out", |
| 48 | }) |
| 49 | ) |
| 50 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 51 | func genYacc(ctx common.AndroidModuleContext, yaccFile common.Path, yaccFlags string) (cppFile, headerFile common.ModuleGenPath) { |
| 52 | cppFile = common.GenPathWithExt(ctx, yaccFile, "cpp") |
| 53 | hppFile := common.GenPathWithExt(ctx, yaccFile, "hpp") |
| 54 | headerFile = common.GenPathWithExt(ctx, yaccFile, "h") |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 55 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 56 | ctx.ModuleBuild(pctx, common.ModuleBuildParams{ |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 57 | Rule: yacc, |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 58 | Outputs: common.WritablePaths{cppFile, headerFile}, |
| 59 | Input: yaccFile, |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 60 | Args: map[string]string{ |
| 61 | "yaccFlags": yaccFlags, |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 62 | "cppFile": cppFile.String(), |
| 63 | "hppFile": hppFile.String(), |
| 64 | "hFile": headerFile.String(), |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 65 | }, |
| 66 | }) |
| 67 | |
| 68 | return cppFile, headerFile |
| 69 | } |
| 70 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 71 | func genLex(ctx common.AndroidModuleContext, lexFile common.Path) (cppFile common.ModuleGenPath) { |
| 72 | cppFile = common.GenPathWithExt(ctx, lexFile, "cpp") |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 73 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 74 | ctx.ModuleBuild(pctx, common.ModuleBuildParams{ |
| 75 | Rule: lex, |
| 76 | Output: cppFile, |
| 77 | Input: lexFile, |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 78 | }) |
| 79 | |
| 80 | return cppFile |
| 81 | } |
| 82 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 83 | func genSources(ctx common.AndroidModuleContext, srcFiles common.Paths, |
| 84 | buildFlags builderFlags) (common.Paths, common.Paths) { |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 85 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 86 | var deps common.Paths |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 87 | |
| 88 | for i, srcFile := range srcFiles { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 89 | switch srcFile.Ext() { |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 90 | case ".y", ".yy": |
| 91 | cppFile, headerFile := genYacc(ctx, srcFile, buildFlags.yaccFlags) |
| 92 | srcFiles[i] = cppFile |
| 93 | deps = append(deps, headerFile) |
| 94 | case ".l", ".ll": |
| 95 | cppFile := genLex(ctx, srcFile) |
| 96 | srcFiles[i] = cppFile |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return srcFiles, deps |
| 101 | } |