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 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 24 | "android/soong/android" |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | func init() { |
Dan Willemsen | 76ef5ab | 2016-09-13 16:55:40 -0700 | [diff] [blame] | 28 | pctx.SourcePathVariable("lexCmd", "prebuilts/misc/${config.HostPrebuiltTag}/flex/flex-2.5.39") |
| 29 | pctx.SourcePathVariable("yaccCmd", "prebuilts/misc/${config.HostPrebuiltTag}/bison/bison") |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 30 | pctx.SourcePathVariable("yaccDataDir", "external/bison/data") |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 31 | |
| 32 | pctx.HostBinToolVariable("aidlCmd", "aidl-cpp") |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | var ( |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 36 | yacc = pctx.AndroidStaticRule("yacc", |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 37 | blueprint.RuleParams{ |
Dan Willemsen | 9f3c574 | 2016-11-03 14:28:31 -0700 | [diff] [blame] | 38 | Command: "BISON_PKGDATADIR=$yaccDataDir $yaccCmd -d $yaccFlags --defines=$hFile -o $out $in", |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 39 | CommandDeps: []string{"$yaccCmd"}, |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 40 | }, |
Dan Willemsen | 9f3c574 | 2016-11-03 14:28:31 -0700 | [diff] [blame] | 41 | "yaccFlags", "hFile") |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 42 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 43 | lex = pctx.AndroidStaticRule("lex", |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 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 | }) |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 48 | |
| 49 | aidl = pctx.AndroidStaticRule("aidl", |
| 50 | blueprint.RuleParams{ |
| 51 | Command: "$aidlCmd -d${out}.d -ninja $aidlFlags $in $outDir $out", |
| 52 | CommandDeps: []string{"$aidlCmd"}, |
| 53 | Depfile: "${out}.d", |
| 54 | Deps: blueprint.DepsGCC, |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 55 | }, |
| 56 | "aidlFlags", "outDir") |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 57 | ) |
| 58 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 59 | func genYacc(ctx android.ModuleContext, yaccFile android.Path, outFile android.ModuleGenPath, yaccFlags string) (headerFile android.ModuleGenPath) { |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 60 | headerFile = android.GenPathWithExt(ctx, "yacc", yaccFile, "h") |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 61 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 62 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Dan Willemsen | 9f3c574 | 2016-11-03 14:28:31 -0700 | [diff] [blame] | 63 | Rule: yacc, |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 64 | Description: "yacc " + yaccFile.Rel(), |
Dan Willemsen | 9f3c574 | 2016-11-03 14:28:31 -0700 | [diff] [blame] | 65 | Output: outFile, |
| 66 | ImplicitOutput: headerFile, |
| 67 | Input: yaccFile, |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 68 | Args: map[string]string{ |
| 69 | "yaccFlags": yaccFlags, |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 70 | "hFile": headerFile.String(), |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 71 | }, |
| 72 | }) |
| 73 | |
Dan Willemsen | f0c73e0 | 2016-03-01 15:15:26 -0800 | [diff] [blame] | 74 | return headerFile |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 77 | func genAidl(ctx android.ModuleContext, aidlFile android.Path, outFile android.ModuleGenPath, aidlFlags string) android.Paths { |
| 78 | |
| 79 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 80 | Rule: aidl, |
| 81 | Description: "aidl " + aidlFile.Rel(), |
| 82 | Output: outFile, |
| 83 | Input: aidlFile, |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 84 | Args: map[string]string{ |
| 85 | "aidlFlags": aidlFlags, |
| 86 | "outDir": android.PathForModuleGen(ctx, "aidl").String(), |
| 87 | }, |
| 88 | }) |
| 89 | |
| 90 | // TODO: This should return the generated headers, not the source file. |
| 91 | return android.Paths{outFile} |
| 92 | } |
| 93 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 94 | func genLex(ctx android.ModuleContext, lexFile android.Path, outFile android.ModuleGenPath) { |
| 95 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 96 | Rule: lex, |
| 97 | Description: "lex " + lexFile.Rel(), |
| 98 | Output: outFile, |
| 99 | Input: lexFile, |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 100 | }) |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 103 | func genSources(ctx android.ModuleContext, srcFiles android.Paths, |
| 104 | buildFlags builderFlags) (android.Paths, android.Paths) { |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 105 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 106 | var deps android.Paths |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 107 | |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 108 | var rsFiles android.Paths |
| 109 | |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 110 | for i, srcFile := range srcFiles { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 111 | switch srcFile.Ext() { |
Dan Willemsen | f0c73e0 | 2016-03-01 15:15:26 -0800 | [diff] [blame] | 112 | case ".y": |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 113 | cFile := android.GenPathWithExt(ctx, "yacc", srcFile, "c") |
Dan Willemsen | f0c73e0 | 2016-03-01 15:15:26 -0800 | [diff] [blame] | 114 | srcFiles[i] = cFile |
| 115 | deps = append(deps, genYacc(ctx, srcFile, cFile, buildFlags.yaccFlags)) |
| 116 | case ".yy": |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 117 | cppFile := android.GenPathWithExt(ctx, "yacc", srcFile, "cpp") |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 118 | srcFiles[i] = cppFile |
Dan Willemsen | f0c73e0 | 2016-03-01 15:15:26 -0800 | [diff] [blame] | 119 | deps = append(deps, genYacc(ctx, srcFile, cppFile, buildFlags.yaccFlags)) |
| 120 | case ".l": |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 121 | cFile := android.GenPathWithExt(ctx, "lex", srcFile, "c") |
Dan Willemsen | f0c73e0 | 2016-03-01 15:15:26 -0800 | [diff] [blame] | 122 | srcFiles[i] = cFile |
| 123 | genLex(ctx, srcFile, cFile) |
| 124 | case ".ll": |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 125 | cppFile := android.GenPathWithExt(ctx, "lex", srcFile, "cpp") |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 126 | srcFiles[i] = cppFile |
Dan Willemsen | f0c73e0 | 2016-03-01 15:15:26 -0800 | [diff] [blame] | 127 | genLex(ctx, srcFile, cppFile) |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 128 | case ".proto": |
| 129 | cppFile, headerFile := genProto(ctx, srcFile, buildFlags.protoFlags) |
| 130 | srcFiles[i] = cppFile |
| 131 | deps = append(deps, headerFile) |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 132 | case ".aidl": |
| 133 | cppFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp") |
| 134 | srcFiles[i] = cppFile |
| 135 | deps = append(deps, genAidl(ctx, srcFile, cppFile, buildFlags.aidlFlags)...) |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 136 | case ".rs", ".fs": |
| 137 | cppFile := rsGeneratedCppFile(ctx, srcFile) |
| 138 | rsFiles = append(rsFiles, srcFiles[i]) |
| 139 | srcFiles[i] = cppFile |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 143 | if len(rsFiles) > 0 { |
| 144 | deps = append(deps, rsGenerateCpp(ctx, rsFiles, buildFlags.rsFlags)...) |
| 145 | } |
| 146 | |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 147 | return srcFiles, deps |
| 148 | } |