Merge "Add proto.canonical_path_from_root"

am: 7c695eb797

Change-Id: I072c8db0a9b8d4f351adc21d5eaf59b1e02f3f28
This commit is contained in:
Dan Willemsen 2018-02-23 20:49:18 +00:00 committed by android-build-merger
commit c530837d4c
10 changed files with 77 additions and 40 deletions

View file

@ -23,8 +23,7 @@ package android
// generate the source. // generate the source.
func ProtoFlags(ctx ModuleContext, p *ProtoProperties) []string { func ProtoFlags(ctx ModuleContext, p *ProtoProperties) []string {
// -I . must come first, it affects where protoc places the output files. protoFlags := []string{}
protoFlags := []string{"-I ."}
if len(p.Proto.Local_include_dirs) > 0 { if len(p.Proto.Local_include_dirs) > 0 {
localProtoIncludeDirs := PathsForModuleSrc(ctx, p.Proto.Local_include_dirs) localProtoIncludeDirs := PathsForModuleSrc(ctx, p.Proto.Local_include_dirs)
@ -38,6 +37,13 @@ func ProtoFlags(ctx ModuleContext, p *ProtoProperties) []string {
return protoFlags return protoFlags
} }
func ProtoCanonicalPathFromRoot(ctx ModuleContext, p *ProtoProperties) bool {
if p.Proto.Canonical_path_from_root == nil {
return true
}
return *p.Proto.Canonical_path_from_root
}
// ProtoDir returns the module's "gen/proto" directory // ProtoDir returns the module's "gen/proto" directory
func ProtoDir(ctx ModuleContext) ModuleGenPath { func ProtoDir(ctx ModuleContext) ModuleGenPath {
return PathForModuleGen(ctx, "proto") return PathForModuleGen(ctx, "proto")
@ -59,5 +65,14 @@ type ProtoProperties struct {
// list of directories relative to the bp file that will // list of directories relative to the bp file that will
// be added to the protoc include paths. // be added to the protoc include paths.
Local_include_dirs []string Local_include_dirs []string
// whether to identify the proto files from the root of the
// source tree (the original method in Android, useful for
// android-specific protos), or relative from where they were
// specified (useful for external/third party protos).
//
// This defaults to true today, but is expected to default to
// false in the future.
Canonical_path_from_root *bool
} `android:"arch_variant"` } `android:"arch_variant"`
} }

View file

@ -252,6 +252,7 @@ type builderFlags struct {
tidy bool tidy bool
coverage bool coverage bool
sAbiDump bool sAbiDump bool
protoRoot bool
systemIncludeFlags string systemIncludeFlags string

View file

@ -134,6 +134,7 @@ type Flags struct {
Tidy bool Tidy bool
Coverage bool Coverage bool
SAbiDump bool SAbiDump bool
ProtoRoot bool
RequiredInstructionSet string RequiredInstructionSet string
DynamicLinker string DynamicLinker string

View file

@ -154,7 +154,7 @@ func genSources(ctx android.ModuleContext, srcFiles android.Paths,
genLex(ctx, srcFile, cppFile) genLex(ctx, srcFile, cppFile)
case ".proto": case ".proto":
ccFile, headerFile := genProto(ctx, srcFile, buildFlags.protoFlags, ccFile, headerFile := genProto(ctx, srcFile, buildFlags.protoFlags,
buildFlags.protoOutParams) buildFlags.protoOutParams, buildFlags.protoRoot)
srcFiles[i] = ccFile srcFiles[i] = ccFile
deps = append(deps, headerFile) deps = append(deps, headerFile)
case ".aidl": case ".aidl":

View file

@ -690,12 +690,13 @@ func (library *libraryDecorator) link(ctx ModuleContext,
if Bool(library.Properties.Proto.Export_proto_headers) { if Bool(library.Properties.Proto.Export_proto_headers) {
if library.baseCompiler.hasSrcExt(".proto") { if library.baseCompiler.hasSrcExt(".proto") {
flags := []string{ includes := []string{}
"-I" + android.ProtoSubDir(ctx).String(), if flags.ProtoRoot {
"-I" + android.ProtoDir(ctx).String(), includes = append(includes, "-I"+android.ProtoSubDir(ctx).String())
} }
library.reexportFlags(flags) includes = append(includes, "-I"+android.ProtoDir(ctx).String())
library.reuseExportedFlags = append(library.reuseExportedFlags, flags...) library.reexportFlags(includes)
library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...) library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
} }

View file

@ -15,7 +15,10 @@
package cc package cc
import ( import (
"strings"
"github.com/google/blueprint" "github.com/google/blueprint"
"github.com/google/blueprint/pathtools"
"github.com/google/blueprint/proptools" "github.com/google/blueprint/proptools"
"android/soong/android" "android/soong/android"
@ -28,18 +31,27 @@ func init() {
var ( var (
proto = pctx.AndroidStaticRule("protoc", proto = pctx.AndroidStaticRule("protoc",
blueprint.RuleParams{ blueprint.RuleParams{
Command: "$protocCmd --cpp_out=$protoOutParams:$outDir $protoFlags $in", Command: "$protocCmd --cpp_out=$protoOutParams:$outDir -I $protoBase $protoFlags $in",
CommandDeps: []string{"$protocCmd"}, CommandDeps: []string{"$protocCmd"},
}, "protoFlags", "protoOutParams", "outDir") }, "protoFlags", "protoOutParams", "protoBase", "outDir")
) )
// genProto creates a rule to convert a .proto file to generated .pb.cc and .pb.h files and returns // genProto creates a rule to convert a .proto file to generated .pb.cc and .pb.h files and returns
// the paths to the generated files. // the paths to the generated files.
func genProto(ctx android.ModuleContext, protoFile android.Path, func genProto(ctx android.ModuleContext, protoFile android.Path,
protoFlags string, protoOutParams string) (ccFile, headerFile android.WritablePath) { protoFlags, protoOutParams string, root bool) (ccFile, headerFile android.WritablePath) {
ccFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.cc") var protoBase string
headerFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.h") if root {
protoBase = "."
ccFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.cc")
headerFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.h")
} else {
rel := protoFile.Rel()
protoBase = strings.TrimSuffix(protoFile.String(), rel)
ccFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, "pb.cc"))
headerFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, "pb.h"))
}
ctx.Build(pctx, android.BuildParams{ ctx.Build(pctx, android.BuildParams{
Rule: proto, Rule: proto,
@ -50,6 +62,7 @@ func genProto(ctx android.ModuleContext, protoFile android.Path,
"outDir": android.ProtoDir(ctx).String(), "outDir": android.ProtoDir(ctx).String(),
"protoFlags": protoFlags, "protoFlags": protoFlags,
"protoOutParams": protoOutParams, "protoOutParams": protoOutParams,
"protoBase": protoBase,
}, },
}) })
@ -92,10 +105,12 @@ func protoDeps(ctx BaseModuleContext, deps Deps, p *android.ProtoProperties, sta
func protoFlags(ctx ModuleContext, flags Flags, p *android.ProtoProperties) Flags { func protoFlags(ctx ModuleContext, flags Flags, p *android.ProtoProperties) Flags {
flags.CFlags = append(flags.CFlags, "-DGOOGLE_PROTOBUF_NO_RTTI") flags.CFlags = append(flags.CFlags, "-DGOOGLE_PROTOBUF_NO_RTTI")
flags.GlobalFlags = append(flags.GlobalFlags,
"-I"+android.ProtoSubDir(ctx).String(), flags.ProtoRoot = android.ProtoCanonicalPathFromRoot(ctx, p)
"-I"+android.ProtoDir(ctx).String(), if flags.ProtoRoot {
) flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.ProtoSubDir(ctx).String())
}
flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.ProtoDir(ctx).String())
flags.protoFlags = android.ProtoFlags(ctx, p) flags.protoFlags = android.ProtoFlags(ctx, p)

View file

@ -80,6 +80,7 @@ func flagsToBuilderFlags(in Flags) builderFlags {
coverage: in.Coverage, coverage: in.Coverage,
tidy: in.Tidy, tidy: in.Tidy,
sAbiDump: in.SAbiDump, sAbiDump: in.SAbiDump,
protoRoot: in.ProtoRoot,
systemIncludeFlags: strings.Join(in.SystemIncludeFlags, " "), systemIncludeFlags: strings.Join(in.SystemIncludeFlags, " "),

View file

@ -169,6 +169,7 @@ type javaBuilderFlags struct {
protoFlags []string protoFlags []string
protoOutTypeFlag string // The flag itself: --java_out protoOutTypeFlag string // The flag itself: --java_out
protoOutParams string // Parameters to that flag: --java_out=$protoOutParams:$outDir protoOutParams string // Parameters to that flag: --java_out=$protoOutParams:$outDir
protoRoot bool
} }
func TransformKotlinToClasses(ctx android.ModuleContext, outputFile android.WritablePath, func TransformKotlinToClasses(ctx android.ModuleContext, outputFile android.WritablePath,

View file

@ -85,7 +85,6 @@ func genLogtags(ctx android.ModuleContext, logtagsFile android.Path) android.Pat
func (j *Module) genSources(ctx android.ModuleContext, srcFiles android.Paths, func (j *Module) genSources(ctx android.ModuleContext, srcFiles android.Paths,
flags javaBuilderFlags) android.Paths { flags javaBuilderFlags) android.Paths {
var protoFiles android.Paths
outSrcFiles := make(android.Paths, 0, len(srcFiles)) outSrcFiles := make(android.Paths, 0, len(srcFiles))
for _, srcFile := range srcFiles { for _, srcFile := range srcFiles {
@ -98,20 +97,13 @@ func (j *Module) genSources(ctx android.ModuleContext, srcFiles android.Paths,
javaFile := genLogtags(ctx, srcFile) javaFile := genLogtags(ctx, srcFile)
outSrcFiles = append(outSrcFiles, javaFile) outSrcFiles = append(outSrcFiles, javaFile)
case ".proto": case ".proto":
protoFiles = append(protoFiles, srcFile) srcJarFile := genProto(ctx, srcFile, flags)
outSrcFiles = append(outSrcFiles, srcJarFile)
default: default:
outSrcFiles = append(outSrcFiles, srcFile) outSrcFiles = append(outSrcFiles, srcFile)
} }
} }
if len(protoFiles) > 0 {
protoSrcJar := android.PathForModuleGen(ctx, "proto.srcjar")
genProto(ctx, protoSrcJar, protoFiles,
flags.protoFlags, flags.protoOutTypeFlag, flags.protoOutParams)
outSrcFiles = append(outSrcFiles, protoSrcJar)
}
return outSrcFiles return outSrcFiles
} }

View file

@ -30,31 +30,40 @@ func init() {
var ( var (
proto = pctx.AndroidStaticRule("protoc", proto = pctx.AndroidStaticRule("protoc",
blueprint.RuleParams{ blueprint.RuleParams{
Command: `rm -rf $outDir && mkdir -p $outDir && ` + Command: `rm -rf $out.tmp && mkdir -p $out.tmp && ` +
`$protocCmd $protoOut=$protoOutParams:$outDir $protoFlags $in && ` + `$protocCmd $protoOut=$protoOutParams:$out.tmp -I $protoBase $protoFlags $in && ` +
`${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`, `${config.SoongZipCmd} -jar -o $out -C $out.tmp -D $out.tmp && rm -rf $out.tmp`,
CommandDeps: []string{ CommandDeps: []string{
"$protocCmd", "$protocCmd",
"${config.SoongZipCmd}", "${config.SoongZipCmd}",
}, },
}, "protoFlags", "protoOut", "protoOutParams", "outDir") }, "protoBase", "protoFlags", "protoOut", "protoOutParams")
) )
func genProto(ctx android.ModuleContext, outputSrcJar android.WritablePath, func genProto(ctx android.ModuleContext, protoFile android.Path, flags javaBuilderFlags) android.Path {
protoFiles android.Paths, protoFlags []string, protoOut, protoOutParams string) { srcJarFile := android.GenPathWithExt(ctx, "proto", protoFile, "srcjar")
var protoBase string
if flags.protoRoot {
protoBase = "."
} else {
protoBase = strings.TrimSuffix(protoFile.String(), protoFile.Rel())
}
ctx.Build(pctx, android.BuildParams{ ctx.Build(pctx, android.BuildParams{
Rule: proto, Rule: proto,
Description: "protoc " + protoFiles[0].Rel(), Description: "protoc " + protoFile.Rel(),
Output: outputSrcJar, Output: srcJarFile,
Inputs: protoFiles, Input: protoFile,
Args: map[string]string{ Args: map[string]string{
"outDir": android.ProtoDir(ctx).String(), "protoBase": protoBase,
"protoOut": protoOut, "protoOut": flags.protoOutTypeFlag,
"protoOutParams": protoOutParams, "protoOutParams": flags.protoOutParams,
"protoFlags": strings.Join(protoFlags, " "), "protoFlags": strings.Join(flags.protoFlags, " "),
}, },
}) })
return srcJarFile
} }
func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) { func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) {
@ -103,6 +112,7 @@ func protoFlags(ctx android.ModuleContext, j *CompilerProperties, p *android.Pro
} }
flags.protoFlags = android.ProtoFlags(ctx, p) flags.protoFlags = android.ProtoFlags(ctx, p)
flags.protoRoot = android.ProtoCanonicalPathFromRoot(ctx, p)
return flags return flags
} }