blob: be50d7574b67a66ec46bd24f88ebe11f08606097 [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",
Dan Willemsenc94a7682015-11-17 15:27:28 -080041 CommandDeps: []string{"$yaccCmd"},
Colin Cross581c1892015-04-07 16:50:10 -070042 Description: "yacc $out",
43 },
44 "yaccFlags", "cppFile", "hppFile", "hFile")
45
46 lex = pctx.StaticRule("lex",
47 blueprint.RuleParams{
48 Command: "$lexCmd -o$out $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080049 CommandDeps: []string{"$lexCmd"},
Colin Cross581c1892015-04-07 16:50:10 -070050 Description: "lex $out",
51 })
52)
53
54func genYacc(ctx common.AndroidModuleContext, yaccFile, yaccFlags string) (cppFile, headerFile string) {
Colin Cross6e18ca42015-07-14 18:55:36 -070055 cppFile = common.SrcDirRelPath(ctx, yaccFile)
Colin Cross581c1892015-04-07 16:50:10 -070056 cppFile = filepath.Join(common.ModuleGenDir(ctx), cppFile)
57 cppFile = pathtools.ReplaceExtension(cppFile, "cpp")
58 hppFile := pathtools.ReplaceExtension(cppFile, "hpp")
59 headerFile = pathtools.ReplaceExtension(cppFile, "h")
60
61 ctx.Build(pctx, blueprint.BuildParams{
Dan Willemsenc94a7682015-11-17 15:27:28 -080062 Rule: yacc,
63 Outputs: []string{cppFile, headerFile},
64 Inputs: []string{yaccFile},
Colin Cross581c1892015-04-07 16:50:10 -070065 Args: map[string]string{
66 "yaccFlags": yaccFlags,
67 "cppFile": cppFile,
68 "hppFile": hppFile,
69 "hFile": headerFile,
70 },
71 })
72
73 return cppFile, headerFile
74}
75
76func genLex(ctx common.AndroidModuleContext, lexFile string) (cppFile string) {
Colin Cross6e18ca42015-07-14 18:55:36 -070077 cppFile = common.SrcDirRelPath(ctx, lexFile)
Colin Cross581c1892015-04-07 16:50:10 -070078 cppFile = filepath.Join(common.ModuleGenDir(ctx), cppFile)
79 cppFile = pathtools.ReplaceExtension(cppFile, "cpp")
80
81 ctx.Build(pctx, blueprint.BuildParams{
Dan Willemsenc94a7682015-11-17 15:27:28 -080082 Rule: lex,
83 Outputs: []string{cppFile},
84 Inputs: []string{lexFile},
Colin Cross581c1892015-04-07 16:50:10 -070085 })
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}