• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2017 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 java
16
17import (
18	"android/soong/android"
19)
20
21func genProto(ctx android.ModuleContext, protoFile android.Path, flags android.ProtoFlags) android.Path {
22	srcJarFile := android.GenPathWithExt(ctx, "proto", protoFile, "srcjar")
23
24	outDir := srcJarFile.ReplaceExtension(ctx, "tmp")
25	depFile := srcJarFile.ReplaceExtension(ctx, "srcjar.d")
26
27	rule := android.NewRuleBuilder()
28
29	rule.Command().Text("rm -rf").Flag(outDir.String())
30	rule.Command().Text("mkdir -p").Flag(outDir.String())
31
32	android.ProtoRule(ctx, rule, protoFile, flags, flags.Deps, outDir, depFile, nil)
33
34	// Proto generated java files have an unknown package name in the path, so package the entire output directory
35	// into a srcjar.
36	rule.Command().
37		Tool(ctx.Config().HostToolPath(ctx, "soong_zip")).
38		Flag("-jar").
39		FlagWithOutput("-o ", srcJarFile).
40		FlagWithArg("-C ", outDir.String()).
41		FlagWithArg("-D ", outDir.String())
42
43	rule.Command().Text("rm -rf").Flag(outDir.String())
44
45	rule.Build(pctx, ctx, "protoc_"+protoFile.Rel(), "protoc "+protoFile.Rel())
46
47	return srcJarFile
48}
49
50func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) {
51	if String(p.Proto.Plugin) == "" {
52		switch String(p.Proto.Type) {
53		case "micro":
54			ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-micro")
55		case "nano":
56			ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-nano")
57		case "lite", "":
58			ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-lite")
59		case "full":
60			if ctx.Host() {
61				ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-full")
62			} else {
63				ctx.PropertyErrorf("proto.type", "full java protos only supported on the host")
64			}
65		default:
66			ctx.PropertyErrorf("proto.type", "unknown proto type %q",
67				String(p.Proto.Type))
68		}
69	}
70}
71
72func protoFlags(ctx android.ModuleContext, j *CompilerProperties, p *android.ProtoProperties,
73	flags javaBuilderFlags) javaBuilderFlags {
74
75	flags.proto = android.GetProtoFlags(ctx, p)
76
77	if String(p.Proto.Plugin) == "" {
78		switch String(p.Proto.Type) {
79		case "micro":
80			flags.proto.OutTypeFlag = "--javamicro_out"
81		case "nano":
82			flags.proto.OutTypeFlag = "--javanano_out"
83		case "lite":
84			flags.proto.OutTypeFlag = "--java_out"
85			flags.proto.OutParams = append(flags.proto.OutParams, "lite")
86		case "full", "":
87			flags.proto.OutTypeFlag = "--java_out"
88		default:
89			ctx.PropertyErrorf("proto.type", "unknown proto type %q",
90				String(p.Proto.Type))
91		}
92	}
93
94	flags.proto.OutParams = append(flags.proto.OutParams, j.Proto.Output_params...)
95
96	return flags
97}
98