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	"path/filepath"
19	"strconv"
20
21	"android/soong/android"
22)
23
24const (
25	protoTypeDefault = "lite"
26)
27
28func genProto(ctx android.ModuleContext, protoFiles android.Paths, flags android.ProtoFlags) android.Paths {
29	// Shard proto files into groups of 100 to avoid having to recompile all of them if one changes and to avoid
30	// hitting command line length limits.
31	shards := android.ShardPaths(protoFiles, 50)
32
33	srcJarFiles := make(android.Paths, 0, len(shards))
34
35	for i, shard := range shards {
36		srcJarFile := android.PathForModuleGen(ctx, "proto", "proto"+strconv.Itoa(i)+".srcjar")
37		srcJarFiles = append(srcJarFiles, srcJarFile)
38
39		outDir := srcJarFile.ReplaceExtension(ctx, "tmp")
40
41		rule := android.NewRuleBuilder(pctx, ctx)
42
43		rule.Command().Text("rm -rf").Flag(outDir.String())
44		rule.Command().Text("mkdir -p").Flag(outDir.String())
45
46		for _, protoFile := range shard {
47			depFile := srcJarFile.InSameDir(ctx, protoFile.String()+".d")
48			rule.Command().Text("mkdir -p").Flag(filepath.Dir(depFile.String()))
49			android.ProtoRule(rule, protoFile, flags, flags.Deps, outDir, depFile, nil)
50		}
51
52		// Proto generated java files have an unknown package name in the path, so package the entire output directory
53		// into a srcjar.
54		rule.Command().
55			BuiltTool("soong_zip").
56			Flag("-srcjar").
57			Flag("-write_if_changed").
58			FlagWithOutput("-o ", srcJarFile).
59			FlagWithArg("-C ", outDir.String()).
60			FlagWithArg("-D ", outDir.String())
61
62		rule.Command().Text("rm -rf").Flag(outDir.String())
63
64		rule.Restat()
65
66		ruleName := "protoc"
67		ruleDesc := "protoc"
68		if len(shards) > 1 {
69			ruleName += "_" + strconv.Itoa(i)
70			ruleDesc += " " + strconv.Itoa(i)
71		}
72
73		rule.Build(ruleName, ruleDesc)
74	}
75
76	return srcJarFiles
77}
78
79func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) {
80	const unspecifiedProtobufPluginType = ""
81	if String(p.Proto.Plugin) == "" {
82		switch String(p.Proto.Type) {
83		case "stream": // does not require additional dependencies
84		case "micro":
85			ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-micro")
86		case "nano":
87			ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-nano")
88		case "lite", unspecifiedProtobufPluginType:
89			ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-lite")
90		case "full":
91			if ctx.Host() {
92				ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-full")
93			} else {
94				ctx.PropertyErrorf("proto.type", "full java protos only supported on the host")
95			}
96		default:
97			ctx.PropertyErrorf("proto.type", "unknown proto type %q",
98				String(p.Proto.Type))
99		}
100	}
101}
102
103func protoFlags(ctx android.ModuleContext, j *CommonProperties, p *android.ProtoProperties,
104	flags javaBuilderFlags) javaBuilderFlags {
105
106	flags.proto = android.GetProtoFlags(ctx, p)
107
108	if String(p.Proto.Plugin) == "" {
109		var typeToPlugin string
110		switch String(p.Proto.Type) {
111		case "stream":
112			flags.proto.OutTypeFlag = "--javastream_out"
113			typeToPlugin = "javastream"
114		case "micro":
115			flags.proto.OutTypeFlag = "--javamicro_out"
116			typeToPlugin = "javamicro"
117		case "nano":
118			flags.proto.OutTypeFlag = "--javanano_out"
119			typeToPlugin = "javanano"
120		case "lite", "":
121			flags.proto.OutTypeFlag = "--java_out"
122			flags.proto.OutParams = append(flags.proto.OutParams, "lite")
123		case "full":
124			flags.proto.OutTypeFlag = "--java_out"
125		default:
126			ctx.PropertyErrorf("proto.type", "unknown proto type %q",
127				String(p.Proto.Type))
128		}
129
130		if typeToPlugin != "" {
131			hostTool := ctx.Config().HostToolPath(ctx, "protoc-gen-"+typeToPlugin)
132			flags.proto.Deps = append(flags.proto.Deps, hostTool)
133			flags.proto.Flags = append(flags.proto.Flags, "--plugin=protoc-gen-"+typeToPlugin+"="+hostTool.String())
134		}
135	}
136
137	flags.proto.OutParams = append(flags.proto.OutParams, j.Proto.Output_params...)
138
139	return flags
140}
141