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