blob: 93142b9fe029f3f031f950daf94ce0c08f216d77 [file] [log] [blame]
Colin Cross0c461f12016-10-20 16:11:43 -07001// Copyright 2016 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 cc
16
17import (
Dan Willemsenab9f4262018-02-14 13:58:34 -080018 "github.com/google/blueprint/pathtools"
Liz Kammer12615db2021-09-28 09:19:17 -040019 "github.com/google/blueprint/proptools"
Colin Cross0c461f12016-10-20 16:11:43 -070020
21 "android/soong/android"
Andrea Marchini49ce87e2024-07-22 10:34:00 +000022
23 "strings"
Liz Kammer12615db2021-09-28 09:19:17 -040024)
25
26const (
27 protoTypeDefault = "lite"
Colin Cross0c461f12016-10-20 16:11:43 -070028)
29
Colin Cross6af17aa2017-09-20 12:59:05 -070030// genProto creates a rule to convert a .proto file to generated .pb.cc and .pb.h files and returns
31// the paths to the generated files.
Colin Cross19878da2019-03-28 14:45:07 -070032func genProto(ctx android.ModuleContext, protoFile android.Path, flags builderFlags) (cc, header android.WritablePath) {
33 var ccFile, headerFile android.ModuleGenPath
Dan Willemsen60e62f02018-11-16 21:05:32 -080034
35 srcSuffix := ".cc"
36 if flags.protoC {
37 srcSuffix = ".c"
38 }
Colin Cross6af17aa2017-09-20 12:59:05 -070039
Andrea Marchini49ce87e2024-07-22 10:34:00 +000040 srcInfix := "pb"
41 for _, value := range flags.proto.Flags {
42 if strings.HasPrefix(value, "--plugin=") && strings.HasSuffix(value, "protoc-gen-grpc-cpp-plugin") {
43 srcInfix = "grpc.pb"
44 break
45 }
46 }
47
Colin Cross19878da2019-03-28 14:45:07 -070048 if flags.proto.CanonicalPathFromRoot {
Andrea Marchini49ce87e2024-07-22 10:34:00 +000049 ccFile = android.GenPathWithExt(ctx, "proto", protoFile, srcInfix+srcSuffix)
50 headerFile = android.GenPathWithExt(ctx, "proto", protoFile, srcInfix+".h")
Dan Willemsenab9f4262018-02-14 13:58:34 -080051 } else {
52 rel := protoFile.Rel()
Andrea Marchini49ce87e2024-07-22 10:34:00 +000053 ccFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, srcInfix+srcSuffix))
54 headerFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, srcInfix+".h"))
Dan Willemsenab9f4262018-02-14 13:58:34 -080055 }
Colin Cross6af17aa2017-09-20 12:59:05 -070056
Colin Crossfe17f6f2019-03-28 19:30:56 -070057 protoDeps := flags.proto.Deps
Dan Willemsen60e62f02018-11-16 21:05:32 -080058 if flags.protoOptionsFile {
59 optionsFile := pathtools.ReplaceExtension(protoFile.String(), "options")
Colin Cross19878da2019-03-28 14:45:07 -070060 optionsPath := android.PathForSource(ctx, optionsFile)
61 protoDeps = append(android.Paths{optionsPath}, protoDeps...)
Dan Willemsen60e62f02018-11-16 21:05:32 -080062 }
63
Colin Cross19878da2019-03-28 14:45:07 -070064 outDir := flags.proto.Dir
65 depFile := ccFile.ReplaceExtension(ctx, "d")
66 outputs := android.WritablePaths{ccFile, headerFile}
67
Colin Crossf1a035e2020-11-16 17:32:30 -080068 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross19878da2019-03-28 14:45:07 -070069
Colin Crossf1a035e2020-11-16 17:32:30 -080070 android.ProtoRule(rule, protoFile, flags.proto, protoDeps, outDir, depFile, outputs)
Colin Cross19878da2019-03-28 14:45:07 -070071
Colin Crossf1a035e2020-11-16 17:32:30 -080072 rule.Build("protoc_"+protoFile.Rel(), "protoc "+protoFile.Rel())
Colin Cross6af17aa2017-09-20 12:59:05 -070073
74 return ccFile, headerFile
75}
76
Colin Crossfe17f6f2019-03-28 19:30:56 -070077func protoDeps(ctx DepsContext, deps Deps, p *android.ProtoProperties, static bool) Deps {
Colin Cross0c461f12016-10-20 16:11:43 -070078 var lib string
Colin Cross0c461f12016-10-20 16:11:43 -070079
Colin Crossfe17f6f2019-03-28 19:30:56 -070080 if String(p.Proto.Plugin) == "" {
Liz Kammer12615db2021-09-28 09:19:17 -040081 switch proptools.StringDefault(p.Proto.Type, protoTypeDefault) {
Colin Crossfe17f6f2019-03-28 19:30:56 -070082 case "full":
83 if ctx.useSdk() {
84 lib = "libprotobuf-cpp-full-ndk"
85 static = true
86 } else {
87 lib = "libprotobuf-cpp-full"
88 }
Liz Kammer12615db2021-09-28 09:19:17 -040089 case "lite":
Colin Crossfe17f6f2019-03-28 19:30:56 -070090 if ctx.useSdk() {
91 lib = "libprotobuf-cpp-lite-ndk"
92 static = true
93 } else {
94 lib = "libprotobuf-cpp-lite"
95 }
96 case "nanopb-c":
97 lib = "libprotobuf-c-nano"
Colin Cross0c461f12016-10-20 16:11:43 -070098 static = true
Colin Crossfe17f6f2019-03-28 19:30:56 -070099 case "nanopb-c-enable_malloc":
100 lib = "libprotobuf-c-nano-enable_malloc"
Colin Cross0c461f12016-10-20 16:11:43 -0700101 static = true
Colin Crossfe17f6f2019-03-28 19:30:56 -0700102 case "nanopb-c-16bit":
103 lib = "libprotobuf-c-nano-16bit"
104 static = true
105 case "nanopb-c-enable_malloc-16bit":
106 lib = "libprotobuf-c-nano-enable_malloc-16bit"
107 static = true
108 case "nanopb-c-32bit":
109 lib = "libprotobuf-c-nano-32bit"
110 static = true
111 case "nanopb-c-enable_malloc-32bit":
112 lib = "libprotobuf-c-nano-enable_malloc-32bit"
113 static = true
114 default:
115 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
116 String(p.Proto.Type))
Colin Cross0c461f12016-10-20 16:11:43 -0700117 }
Colin Cross0c461f12016-10-20 16:11:43 -0700118
Colin Crossfe17f6f2019-03-28 19:30:56 -0700119 if static {
120 deps.StaticLibs = append(deps.StaticLibs, lib)
121 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, lib)
122 } else {
123 deps.SharedLibs = append(deps.SharedLibs, lib)
124 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, lib)
125 }
Colin Cross0c461f12016-10-20 16:11:43 -0700126 }
127
128 return deps
129}
130
Colin Cross38f794e2017-09-07 10:53:07 -0700131func protoFlags(ctx ModuleContext, flags Flags, p *android.ProtoProperties) Flags {
Colin Cross4af21ed2019-11-04 09:37:55 -0800132 flags.Local.CFlags = append(flags.Local.CFlags, "-DGOOGLE_PROTOBUF_NO_RTTI")
Dan Willemsenab9f4262018-02-14 13:58:34 -0800133
Colin Cross19878da2019-03-28 14:45:07 -0700134 flags.proto = android.GetProtoFlags(ctx, p)
135 if flags.proto.CanonicalPathFromRoot {
Colin Cross4af21ed2019-11-04 09:37:55 -0800136 flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-I"+flags.proto.SubDir.String())
Dan Willemsenab9f4262018-02-14 13:58:34 -0800137 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800138 flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-I"+flags.proto.Dir.String())
Colin Cross5ff51b52017-05-02 13:34:32 -0700139
Colin Crossfe17f6f2019-03-28 19:30:56 -0700140 if String(p.Proto.Plugin) == "" {
141 var plugin string
Dan Willemsen60e62f02018-11-16 21:05:32 -0800142
Colin Crossfe17f6f2019-03-28 19:30:56 -0700143 switch String(p.Proto.Type) {
144 case "nanopb-c", "nanopb-c-enable_malloc", "nanopb-c-16bit", "nanopb-c-enable_malloc-16bit", "nanopb-c-32bit", "nanopb-c-enable_malloc-32bit":
145 flags.protoC = true
146 flags.protoOptionsFile = true
147 flags.proto.OutTypeFlag = "--nanopb_out"
Ulf Adams82fd89b2020-11-25 22:37:22 +0100148 // Disable nanopb timestamps to support remote caching.
149 flags.proto.OutParams = append(flags.proto.OutParams, "-T")
Colin Crossfe17f6f2019-03-28 19:30:56 -0700150 plugin = "protoc-gen-nanopb"
151 case "full":
152 flags.proto.OutTypeFlag = "--cpp_out"
153 case "lite":
154 flags.proto.OutTypeFlag = "--cpp_out"
155 flags.proto.OutParams = append(flags.proto.OutParams, "lite")
156 case "":
157 // TODO(b/119714316): this should be equivalent to "lite" in
158 // order to match protoDeps, but some modules are depending on
159 // this behavior
160 flags.proto.OutTypeFlag = "--cpp_out"
161 default:
162 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
163 String(p.Proto.Type))
164 }
Dan Willemsen60e62f02018-11-16 21:05:32 -0800165
Colin Crossfe17f6f2019-03-28 19:30:56 -0700166 if plugin != "" {
167 path := ctx.Config().HostToolPath(ctx, plugin)
168 flags.proto.Deps = append(flags.proto.Deps, path)
169 flags.proto.Flags = append(flags.proto.Flags, "--plugin="+path.String())
170 }
Joe Onorato09e94ab2017-11-18 18:23:14 -0800171 }
172
Colin Cross0c461f12016-10-20 16:11:43 -0700173 return flags
174}