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