blob: 035f40edaac39e3cd3b61182c87f7281c9555ee4 [file] [log] [blame]
Colin Cross581c1892015-04-07 16:50:10 -07001// 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
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
21import (
Colin Cross581c1892015-04-07 16:50:10 -070022 "github.com/google/blueprint"
Colin Cross581c1892015-04-07 16:50:10 -070023
24 "android/soong/common"
25)
26
27func init() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -070028 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 Cross581c1892015-04-07 16:50:10 -070031}
32
33var (
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 Willemsenc94a7682015-11-17 15:27:28 -080038 CommandDeps: []string{"$yaccCmd"},
Colin Cross581c1892015-04-07 16:50:10 -070039 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 Willemsenc94a7682015-11-17 15:27:28 -080046 CommandDeps: []string{"$lexCmd"},
Colin Cross581c1892015-04-07 16:50:10 -070047 Description: "lex $out",
48 })
49)
50
Dan Willemsen34cc69e2015-09-23 15:26:20 -070051func 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 Cross581c1892015-04-07 16:50:10 -070055
Dan Willemsen34cc69e2015-09-23 15:26:20 -070056 ctx.ModuleBuild(pctx, common.ModuleBuildParams{
Dan Willemsenc94a7682015-11-17 15:27:28 -080057 Rule: yacc,
Dan Willemsen34cc69e2015-09-23 15:26:20 -070058 Outputs: common.WritablePaths{cppFile, headerFile},
59 Input: yaccFile,
Colin Cross581c1892015-04-07 16:50:10 -070060 Args: map[string]string{
61 "yaccFlags": yaccFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -070062 "cppFile": cppFile.String(),
63 "hppFile": hppFile.String(),
64 "hFile": headerFile.String(),
Colin Cross581c1892015-04-07 16:50:10 -070065 },
66 })
67
68 return cppFile, headerFile
69}
70
Dan Willemsen34cc69e2015-09-23 15:26:20 -070071func genLex(ctx common.AndroidModuleContext, lexFile common.Path) (cppFile common.ModuleGenPath) {
72 cppFile = common.GenPathWithExt(ctx, lexFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -070073
Dan Willemsen34cc69e2015-09-23 15:26:20 -070074 ctx.ModuleBuild(pctx, common.ModuleBuildParams{
75 Rule: lex,
76 Output: cppFile,
77 Input: lexFile,
Colin Cross581c1892015-04-07 16:50:10 -070078 })
79
80 return cppFile
81}
82
Dan Willemsen34cc69e2015-09-23 15:26:20 -070083func genSources(ctx common.AndroidModuleContext, srcFiles common.Paths,
84 buildFlags builderFlags) (common.Paths, common.Paths) {
Colin Cross581c1892015-04-07 16:50:10 -070085
Dan Willemsen34cc69e2015-09-23 15:26:20 -070086 var deps common.Paths
Colin Cross581c1892015-04-07 16:50:10 -070087
88 for i, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -070089 switch srcFile.Ext() {
Colin Cross581c1892015-04-07 16:50:10 -070090 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}