aboutsummaryrefslogtreecommitdiffstats
path: root/cc
diff options
context:
space:
mode:
authorColin Cross <ccross@android.com>2015-04-07 16:50:10 -0700
committerColin Cross <ccross@android.com>2015-04-08 15:17:45 -0700
commit581c18998830a44c20b8932935a2f32442bd8e80 (patch)
treedf97714ae7d8fbb63ab4f36cf96ed3f7cdad58bd /cc
parent65bf4f231c9407a0f53c7c2b9237d0b219032231 (diff)
downloadbuild_soong-581c18998830a44c20b8932935a2f32442bd8e80.tar.gz
build_soong-581c18998830a44c20b8932935a2f32442bd8e80.tar.bz2
build_soong-581c18998830a44c20b8932935a2f32442bd8e80.zip
Add yacc and lex support
Add support for yacc (.y or .yy) and lex (.l or .ll) files. Also tweak locations of .o files for normal and generated sources to makes sure they don't collide. Change-Id: I03172cddbdc022525bf392a81d72050406b8cdb3
Diffstat (limited to 'cc')
-rw-r--r--cc/builder.go24
-rw-r--r--cc/cc.go31
-rw-r--r--cc/gen.go109
-rw-r--r--cc/util.go1
4 files changed, 149 insertions, 16 deletions
diff --git a/cc/builder.go b/cc/builder.go
index cb2bf9ac..8fc428cd 100644
--- a/cc/builder.go
+++ b/cc/builder.go
@@ -99,6 +99,7 @@ type builderFlags struct {
ldFlags string
ldLibs string
incFlags string
+ yaccFlags string
nocrt bool
toolchain Toolchain
clang bool
@@ -106,7 +107,10 @@ type builderFlags struct {
// Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files
func TransformSourceToObj(ctx common.AndroidModuleContext, subdir string, srcFiles []string,
- flags builderFlags) (objFiles []string) {
+ flags builderFlags, deps []string) (objFiles []string) {
+
+ srcRoot := ctx.Config().(Config).SrcDir()
+ intermediatesRoot := ctx.Config().(Config).IntermediatesDir()
objFiles = make([]string, len(srcFiles))
objDir := common.ModuleObjDir(ctx)
@@ -119,8 +123,18 @@ func TransformSourceToObj(ctx common.AndroidModuleContext, subdir string, srcFil
asflags := flags.globalFlags + " " + flags.asFlags
for i, srcFile := range srcFiles {
- objFile := strings.TrimPrefix(srcFile, common.ModuleSrcDir(ctx))
- objFile = filepath.Join(objDir, objFile)
+ var objFile string
+ if strings.HasPrefix(srcFile, srcRoot) {
+ objFile = strings.TrimPrefix(srcFile, srcRoot)
+ objFile = filepath.Join(objDir, objFile)
+ } else if strings.HasPrefix(srcFile, intermediatesRoot) {
+ objFile = strings.TrimPrefix(srcFile, intermediatesRoot)
+ objFile = filepath.Join(objDir, "gen", objFile)
+ } else {
+ ctx.ModuleErrorf("source file %q is not in source directory %q", srcFile, srcRoot)
+ continue
+ }
+
objFile = pathtools.ReplaceExtension(objFile, "o")
objFiles[i] = objFile
@@ -158,11 +172,13 @@ func TransformSourceToObj(ctx common.AndroidModuleContext, subdir string, srcFil
ccCmd = gccCmd(flags.toolchain, ccCmd)
}
+ deps = append([]string{ccCmd}, deps...)
+
ctx.Build(pctx, blueprint.BuildParams{
Rule: cc,
Outputs: []string{objFile},
Inputs: []string{srcFile},
- Implicits: []string{ccCmd},
+ Implicits: deps,
Args: map[string]string{
"cFlags": moduleCflags,
"incFlags": flags.incFlags,
diff --git a/cc/cc.go b/cc/cc.go
index e9a183ef..b0dd7920 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -32,6 +32,7 @@ import (
type Config interface {
SrcDir() string
+ IntermediatesDir() string
PrebuiltOS() string
}
@@ -126,6 +127,9 @@ type ccProperties struct {
// asflags: list of module-specific flags that will be used for .S compiles
Asflags []string `android:"arch_variant"`
+ // yaccflags: list of module-specific flags that will be used for .y and .yy compiles
+ Yaccflags []string
+
// ldflags: list of module-specific flags that will be used for all link steps
Ldflags []string `android:"arch_variant"`
@@ -257,6 +261,7 @@ type CCFlags struct {
CFlags []string
ConlyFlags []string
CppFlags []string
+ YaccFlags []string
LdFlags []string
LdLibs []string
IncludeDirs []string
@@ -311,12 +316,12 @@ func (c *ccBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
flags.IncludeDirs = append(flags.IncludeDirs, deps.IncludeDirs...)
- objFiles := c.compileObjs(ctx, flags, deps)
+ objFiles := c.compileObjs(ctx, flags)
if ctx.Failed() {
return
}
- generatedObjFiles := c.compileGeneratedObjs(ctx, flags, deps)
+ generatedObjFiles := c.compileGeneratedObjs(ctx, flags)
if ctx.Failed() {
return
}
@@ -388,6 +393,7 @@ func (c *ccBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolcha
ConlyFlags: c.properties.Conlyflags,
LdFlags: c.properties.Ldflags,
AsFlags: c.properties.Asflags,
+ YaccFlags: c.properties.Yaccflags,
Nocrt: c.properties.Nocrt,
Toolchain: toolchain,
Clang: c.properties.Clang,
@@ -521,28 +527,29 @@ func (c *ccBase) Flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
// Compile a list of source files into objects a specified subdirectory
func (c *ccBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
- deps CCDeps, subdir string, srcFiles []string) []string {
+ subdir string, srcFiles []string) []string {
+
+ buildFlags := ccFlagsToBuilderFlags(flags)
srcFiles = pathtools.PrefixPaths(srcFiles, common.ModuleSrcDir(ctx))
srcFiles = common.ExpandGlobs(ctx, srcFiles)
+ srcFiles, deps := genSources(ctx, srcFiles, buildFlags)
- return TransformSourceToObj(ctx, subdir, srcFiles, ccFlagsToBuilderFlags(flags))
+ return TransformSourceToObj(ctx, subdir, srcFiles, buildFlags, deps)
}
// Compile files listed in c.properties.Srcs into objects
-func (c *ccBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags,
- deps CCDeps) []string {
+func (c *ccBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) []string {
if c.properties.SkipCompileObjs {
return nil
}
- return c.customCompileObjs(ctx, flags, deps, "", c.properties.Srcs)
+ return c.customCompileObjs(ctx, flags, "", c.properties.Srcs)
}
// Compile generated source files from dependencies
-func (c *ccBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags,
- deps CCDeps) []string {
+func (c *ccBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) []string {
var srcs []string
if c.properties.SkipCompileObjs {
@@ -559,7 +566,7 @@ func (c *ccBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCF
return nil
}
- return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags))
+ return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil)
}
func (c *ccBase) outputFile() string {
@@ -1013,7 +1020,7 @@ func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
staticFlags := flags
staticFlags.CFlags = append(staticFlags.CFlags, c.LibraryProperties.Static.Cflags...)
- objFilesStatic := c.customCompileObjs(ctx, staticFlags, deps, common.DeviceStaticLibrary,
+ objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
c.LibraryProperties.Static.Srcs)
objFiles = append(objFiles, objFilesStatic...)
@@ -1036,7 +1043,7 @@ func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
sharedFlags := flags
sharedFlags.CFlags = append(sharedFlags.CFlags, c.LibraryProperties.Shared.Cflags...)
- objFilesShared := c.customCompileObjs(ctx, sharedFlags, deps, common.DeviceSharedLibrary,
+ objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
c.LibraryProperties.Shared.Srcs)
objFiles = append(objFiles, objFilesShared...)
diff --git a/cc/gen.go b/cc/gen.go
new file mode 100644
index 00000000..bd91d647
--- /dev/null
+++ b/cc/gen.go
@@ -0,0 +1,109 @@
+// Copyright 2015 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package cc
+
+// This file generates the final rules for compiling all C/C++. All properties related to
+// compiling should have been translated into builderFlags or another argument to the Transform*
+// functions.
+
+import (
+ "path/filepath"
+ "strings"
+
+ "github.com/google/blueprint"
+ "github.com/google/blueprint/pathtools"
+
+ "android/soong/common"
+)
+
+func init() {
+ pctx.StaticVariable("lexCmd", "${SrcDir}/prebuilts/misc/${HostPrebuiltTag}/flex/flex-2.5.39")
+ pctx.StaticVariable("yaccCmd", "${SrcDir}/prebuilts/misc/${HostPrebuiltTag}/bison/bison")
+ pctx.StaticVariable("yaccDataDir", "${SrcDir}/external/bison/data")
+}
+
+var (
+ yacc = pctx.StaticRule("yacc",
+ blueprint.RuleParams{
+ Command: "BISON_PKGDATADIR=$yaccDataDir $yaccCmd -d $yaccFlags -o $cppFile $in && " +
+ "cp -f $hppFile $hFile",
+ Description: "yacc $out",
+ },
+ "yaccFlags", "cppFile", "hppFile", "hFile")
+
+ lex = pctx.StaticRule("lex",
+ blueprint.RuleParams{
+ Command: "$lexCmd -o$out $in",
+ Description: "lex $out",
+ })
+)
+
+func genYacc(ctx common.AndroidModuleContext, yaccFile, yaccFlags string) (cppFile, headerFile string) {
+ cppFile = strings.TrimPrefix(yaccFile, common.ModuleSrcDir(ctx))
+ cppFile = filepath.Join(common.ModuleGenDir(ctx), cppFile)
+ cppFile = pathtools.ReplaceExtension(cppFile, "cpp")
+ hppFile := pathtools.ReplaceExtension(cppFile, "hpp")
+ headerFile = pathtools.ReplaceExtension(cppFile, "h")
+
+ ctx.Build(pctx, blueprint.BuildParams{
+ Rule: yacc,
+ Outputs: []string{cppFile, headerFile},
+ Inputs: []string{yaccFile},
+ Implicits: []string{"$yaccCmd"},
+ Args: map[string]string{
+ "yaccFlags": yaccFlags,
+ "cppFile": cppFile,
+ "hppFile": hppFile,
+ "hFile": headerFile,
+ },
+ })
+
+ return cppFile, headerFile
+}
+
+func genLex(ctx common.AndroidModuleContext, lexFile string) (cppFile string) {
+ cppFile = strings.TrimPrefix(lexFile, common.ModuleSrcDir(ctx))
+ cppFile = filepath.Join(common.ModuleGenDir(ctx), cppFile)
+ cppFile = pathtools.ReplaceExtension(cppFile, "cpp")
+
+ ctx.Build(pctx, blueprint.BuildParams{
+ Rule: lex,
+ Outputs: []string{cppFile},
+ Inputs: []string{lexFile},
+ Implicits: []string{"$lexCmd"},
+ })
+
+ return cppFile
+}
+
+func genSources(ctx common.AndroidModuleContext, srcFiles []string,
+ buildFlags builderFlags) ([]string, []string) {
+
+ var deps []string
+
+ for i, srcFile := range srcFiles {
+ switch filepath.Ext(srcFile) {
+ case ".y", ".yy":
+ cppFile, headerFile := genYacc(ctx, srcFile, buildFlags.yaccFlags)
+ srcFiles[i] = cppFile
+ deps = append(deps, headerFile)
+ case ".l", ".ll":
+ cppFile := genLex(ctx, srcFile)
+ srcFiles[i] = cppFile
+ }
+ }
+
+ return srcFiles, deps
+}
diff --git a/cc/util.go b/cc/util.go
index db14c748..7440c256 100644
--- a/cc/util.go
+++ b/cc/util.go
@@ -98,6 +98,7 @@ func ccFlagsToBuilderFlags(in CCFlags) builderFlags {
cFlags: strings.Join(in.CFlags, " "),
conlyFlags: strings.Join(in.ConlyFlags, " "),
cppFlags: strings.Join(in.CppFlags, " "),
+ yaccFlags: strings.Join(in.YaccFlags, " "),
ldFlags: strings.Join(in.LdFlags, " "),
ldLibs: strings.Join(in.LdLibs, " "),
incFlags: includeDirsToFlags(in.IncludeDirs),