blob: 3bfe679dcf39561eee475e77ed94ccbaea6ff161 [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 (
22 "path/filepath"
Colin Cross581c1892015-04-07 16:50:10 -070023
24 "github.com/google/blueprint"
25 "github.com/google/blueprint/pathtools"
26
27 "android/soong/common"
28)
29
30func init() {
31 pctx.StaticVariable("lexCmd", "${SrcDir}/prebuilts/misc/${HostPrebuiltTag}/flex/flex-2.5.39")
32 pctx.StaticVariable("yaccCmd", "${SrcDir}/prebuilts/misc/${HostPrebuiltTag}/bison/bison")
33 pctx.StaticVariable("yaccDataDir", "${SrcDir}/external/bison/data")
34}
35
36var (
37 yacc = pctx.StaticRule("yacc",
38 blueprint.RuleParams{
39 Command: "BISON_PKGDATADIR=$yaccDataDir $yaccCmd -d $yaccFlags -o $cppFile $in && " +
40 "cp -f $hppFile $hFile",
41 Description: "yacc $out",
42 },
43 "yaccFlags", "cppFile", "hppFile", "hFile")
44
45 lex = pctx.StaticRule("lex",
46 blueprint.RuleParams{
47 Command: "$lexCmd -o$out $in",
48 Description: "lex $out",
49 })
50)
51
52func genYacc(ctx common.AndroidModuleContext, yaccFile, yaccFlags string) (cppFile, headerFile string) {
Colin Cross6e18ca42015-07-14 18:55:36 -070053 cppFile = common.SrcDirRelPath(ctx, yaccFile)
Colin Cross581c1892015-04-07 16:50:10 -070054 cppFile = filepath.Join(common.ModuleGenDir(ctx), cppFile)
55 cppFile = pathtools.ReplaceExtension(cppFile, "cpp")
56 hppFile := pathtools.ReplaceExtension(cppFile, "hpp")
57 headerFile = pathtools.ReplaceExtension(cppFile, "h")
58
59 ctx.Build(pctx, blueprint.BuildParams{
60 Rule: yacc,
61 Outputs: []string{cppFile, headerFile},
62 Inputs: []string{yaccFile},
63 Implicits: []string{"$yaccCmd"},
64 Args: map[string]string{
65 "yaccFlags": yaccFlags,
66 "cppFile": cppFile,
67 "hppFile": hppFile,
68 "hFile": headerFile,
69 },
70 })
71
72 return cppFile, headerFile
73}
74
75func genLex(ctx common.AndroidModuleContext, lexFile string) (cppFile string) {
Colin Cross6e18ca42015-07-14 18:55:36 -070076 cppFile = common.SrcDirRelPath(ctx, lexFile)
Colin Cross581c1892015-04-07 16:50:10 -070077 cppFile = filepath.Join(common.ModuleGenDir(ctx), cppFile)
78 cppFile = pathtools.ReplaceExtension(cppFile, "cpp")
79
80 ctx.Build(pctx, blueprint.BuildParams{
81 Rule: lex,
82 Outputs: []string{cppFile},
83 Inputs: []string{lexFile},
84 Implicits: []string{"$lexCmd"},
85 })
86
87 return cppFile
88}
89
90func genSources(ctx common.AndroidModuleContext, srcFiles []string,
91 buildFlags builderFlags) ([]string, []string) {
92
93 var deps []string
94
95 for i, srcFile := range srcFiles {
96 switch filepath.Ext(srcFile) {
97 case ".y", ".yy":
98 cppFile, headerFile := genYacc(ctx, srcFile, buildFlags.yaccFlags)
99 srcFiles[i] = cppFile
100 deps = append(deps, headerFile)
101 case ".l", ".ll":
102 cppFile := genLex(ctx, srcFile)
103 srcFiles[i] = cppFile
104 }
105 }
106
107 return srcFiles, deps
108}