blob: c344cf64229d413bcaa0df9f9c1661f1b602e01e [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
Colin Cross635c3b02016-05-18 15:37:25 -070024 "android/soong/android"
Colin Cross581c1892015-04-07 16:50:10 -070025)
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 (
Colin Cross9d45bb72016-08-29 16:14:13 -070034 yacc = pctx.AndroidStaticRule("yacc",
Colin Cross581c1892015-04-07 16:50:10 -070035 blueprint.RuleParams{
Dan Willemsenf0c73e02016-03-01 15:15:26 -080036 Command: "BISON_PKGDATADIR=$yaccDataDir $yaccCmd -d $yaccFlags --defines=$hFile -o $cFile $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080037 CommandDeps: []string{"$yaccCmd"},
Colin Cross581c1892015-04-07 16:50:10 -070038 Description: "yacc $out",
39 },
Dan Willemsenf0c73e02016-03-01 15:15:26 -080040 "yaccFlags", "cFile", "hFile")
Colin Cross581c1892015-04-07 16:50:10 -070041
Colin Cross9d45bb72016-08-29 16:14:13 -070042 lex = pctx.AndroidStaticRule("lex",
Colin Cross581c1892015-04-07 16:50:10 -070043 blueprint.RuleParams{
44 Command: "$lexCmd -o$out $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080045 CommandDeps: []string{"$lexCmd"},
Colin Cross581c1892015-04-07 16:50:10 -070046 Description: "lex $out",
47 })
48)
49
Colin Cross635c3b02016-05-18 15:37:25 -070050func genYacc(ctx android.ModuleContext, yaccFile android.Path, outFile android.ModuleGenPath, yaccFlags string) (headerFile android.ModuleGenPath) {
51 headerFile = android.GenPathWithExt(ctx, yaccFile, "h")
Colin Cross581c1892015-04-07 16:50:10 -070052
Colin Cross635c3b02016-05-18 15:37:25 -070053 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsenc94a7682015-11-17 15:27:28 -080054 Rule: yacc,
Colin Cross635c3b02016-05-18 15:37:25 -070055 Outputs: android.WritablePaths{outFile, headerFile},
Dan Willemsen34cc69e2015-09-23 15:26:20 -070056 Input: yaccFile,
Colin Cross581c1892015-04-07 16:50:10 -070057 Args: map[string]string{
58 "yaccFlags": yaccFlags,
Dan Willemsenf0c73e02016-03-01 15:15:26 -080059 "cFile": outFile.String(),
Dan Willemsen34cc69e2015-09-23 15:26:20 -070060 "hFile": headerFile.String(),
Colin Cross581c1892015-04-07 16:50:10 -070061 },
62 })
63
Dan Willemsenf0c73e02016-03-01 15:15:26 -080064 return headerFile
Colin Cross581c1892015-04-07 16:50:10 -070065}
66
Colin Cross635c3b02016-05-18 15:37:25 -070067func genLex(ctx android.ModuleContext, lexFile android.Path, outFile android.ModuleGenPath) {
68 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen34cc69e2015-09-23 15:26:20 -070069 Rule: lex,
Dan Willemsenf0c73e02016-03-01 15:15:26 -080070 Output: outFile,
Dan Willemsen34cc69e2015-09-23 15:26:20 -070071 Input: lexFile,
Colin Cross581c1892015-04-07 16:50:10 -070072 })
Colin Cross581c1892015-04-07 16:50:10 -070073}
74
Colin Cross635c3b02016-05-18 15:37:25 -070075func genSources(ctx android.ModuleContext, srcFiles android.Paths,
76 buildFlags builderFlags) (android.Paths, android.Paths) {
Colin Cross581c1892015-04-07 16:50:10 -070077
Colin Cross635c3b02016-05-18 15:37:25 -070078 var deps android.Paths
Colin Cross581c1892015-04-07 16:50:10 -070079
80 for i, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -070081 switch srcFile.Ext() {
Dan Willemsenf0c73e02016-03-01 15:15:26 -080082 case ".y":
Colin Cross635c3b02016-05-18 15:37:25 -070083 cFile := android.GenPathWithExt(ctx, srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -080084 srcFiles[i] = cFile
85 deps = append(deps, genYacc(ctx, srcFile, cFile, buildFlags.yaccFlags))
86 case ".yy":
Colin Cross635c3b02016-05-18 15:37:25 -070087 cppFile := android.GenPathWithExt(ctx, srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -070088 srcFiles[i] = cppFile
Dan Willemsenf0c73e02016-03-01 15:15:26 -080089 deps = append(deps, genYacc(ctx, srcFile, cppFile, buildFlags.yaccFlags))
90 case ".l":
Colin Cross635c3b02016-05-18 15:37:25 -070091 cFile := android.GenPathWithExt(ctx, srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -080092 srcFiles[i] = cFile
93 genLex(ctx, srcFile, cFile)
94 case ".ll":
Colin Cross635c3b02016-05-18 15:37:25 -070095 cppFile := android.GenPathWithExt(ctx, srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -070096 srcFiles[i] = cppFile
Dan Willemsenf0c73e02016-03-01 15:15:26 -080097 genLex(ctx, srcFile, cppFile)
Colin Cross581c1892015-04-07 16:50:10 -070098 }
99 }
100
101 return srcFiles, deps
102}