platform_build_soong/java/proto.go
Dan Willemsen ab9f4268c0 Add proto.canonical_path_from_root
Historically, we've always passed '-I .' as the first argument to
protoc, essentially treating all proto file package names as their full
path in the android source tree. This would make sense in a monorepo
world, but it makes less sense when we're pulling in external projects
with established package names.

So keep the same default (for now), but allow individual builds to opt
into using local paths as the default names with
'canonical_path_from_root: false'. A cleanup effort and/or large scale
change in the future could change the default to false.

As part of this, run protoc once per input proto file, since the flags
may need to change per-file. We'll also need this in order to specify
--dependency_out in the future.

Bug: 70704330
Test: aosp/master build-aosp_arm.ninja is identical
Test: aosp/master soong/build.ninja has expected changes
Test: m
Test: Build protobuf test
Change-Id: I9d6de9fd630326bbcced1c62a4a7e9546429b0ce
2018-02-22 16:48:35 -08:00

118 lines
3.5 KiB
Go

// Copyright 2017 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 java
import (
"strings"
"github.com/google/blueprint"
"github.com/google/blueprint/proptools"
"android/soong/android"
)
func init() {
pctx.HostBinToolVariable("protocCmd", "aprotoc")
}
var (
proto = pctx.AndroidStaticRule("protoc",
blueprint.RuleParams{
Command: `rm -rf $out.tmp && mkdir -p $out.tmp && ` +
`$protocCmd $protoOut=$protoOutParams:$out.tmp -I $protoBase $protoFlags $in && ` +
`${config.SoongZipCmd} -jar -o $out -C $out.tmp -D $out.tmp && rm -rf $out.tmp`,
CommandDeps: []string{
"$protocCmd",
"${config.SoongZipCmd}",
},
}, "protoBase", "protoFlags", "protoOut", "protoOutParams")
)
func genProto(ctx android.ModuleContext, protoFile android.Path, flags javaBuilderFlags) android.Path {
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{
Rule: proto,
Description: "protoc " + protoFile.Rel(),
Output: srcJarFile,
Input: protoFile,
Args: map[string]string{
"protoBase": protoBase,
"protoOut": flags.protoOutTypeFlag,
"protoOutParams": flags.protoOutParams,
"protoFlags": strings.Join(flags.protoFlags, " "),
},
})
return srcJarFile
}
func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) {
switch proptools.String(p.Proto.Type) {
case "micro":
ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-micro")
case "nano":
ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-nano")
case "lite", "":
ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-lite")
case "full":
if ctx.Host() {
ctx.AddDependency(ctx.Module(), staticLibTag, "libprotobuf-java-full")
} else {
ctx.PropertyErrorf("proto.type", "full java protos only supported on the host")
}
default:
ctx.PropertyErrorf("proto.type", "unknown proto type %q",
proptools.String(p.Proto.Type))
}
}
func protoFlags(ctx android.ModuleContext, j *CompilerProperties, p *android.ProtoProperties,
flags javaBuilderFlags) javaBuilderFlags {
switch proptools.String(p.Proto.Type) {
case "micro":
flags.protoOutTypeFlag = "--javamicro_out"
case "nano":
flags.protoOutTypeFlag = "--javanano_out"
case "lite":
flags.protoOutTypeFlag = "--java_out"
flags.protoOutParams = "lite"
case "full", "":
flags.protoOutTypeFlag = "--java_out"
default:
ctx.PropertyErrorf("proto.type", "unknown proto type %q",
proptools.String(p.Proto.Type))
}
if len(j.Proto.Output_params) > 0 {
if flags.protoOutParams != "" {
flags.protoOutParams += ","
}
flags.protoOutParams += strings.Join(j.Proto.Output_params, ",")
}
flags.protoFlags = android.ProtoFlags(ctx, p)
flags.protoRoot = android.ProtoCanonicalPathFromRoot(ctx, p)
return flags
}