blob: b723f3f1a9b0cf467e4b2a812dae80a085503088 [file] [log] [blame]
Treehugger Robot588aae72020-08-21 10:01:58 +00001// Copyright 2020 The Android Open Source Project
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 rust
16
17import (
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050018 "strings"
Treehugger Robot588aae72020-08-21 10:01:58 +000019 "testing"
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050020
21 "android/soong/android"
Treehugger Robot588aae72020-08-21 10:01:58 +000022)
23
24func TestRustProtobuf(t *testing.T) {
25 ctx := testRust(t, `
26 rust_protobuf {
27 name: "librust_proto",
Ivan Lozano57f434e2020-10-28 09:32:10 -040028 protos: ["buf.proto", "proto.proto"],
Treehugger Robot588aae72020-08-21 10:01:58 +000029 crate_name: "rust_proto",
30 source_stem: "buf",
Zach Johnson3df4e632020-11-06 11:56:27 -080031 shared_libs: ["libfoo_shared"],
32 static_libs: ["libfoo_static"],
33 }
34 cc_library_shared {
35 name: "libfoo_shared",
36 export_include_dirs: ["shared_include"],
37 }
38 cc_library_static {
39 name: "libfoo_static",
40 export_include_dirs: ["static_include"],
Treehugger Robot588aae72020-08-21 10:01:58 +000041 }
42 `)
Treehugger Robot588aae72020-08-21 10:01:58 +000043 // Check that libprotobuf is added as a dependency.
44 librust_proto := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_dylib").Module().(*Module)
Jeff Vander Stoep91c04662023-03-22 15:09:00 +010045 if !android.InList("libprotobuf_deprecated", librust_proto.Properties.AndroidMkDylibs) {
46 t.Errorf("libprotobuf_deprecated dependency missing for rust_protobuf (dependency missing from AndroidMkDylibs)")
Treehugger Robot588aae72020-08-21 10:01:58 +000047 }
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050048
49 // Make sure the correct plugin is being used.
50 librust_proto_out := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_source").Output("buf.rs")
51 cmd := librust_proto_out.RuleParams.Command
Jeff Vander Stoep91c04662023-03-22 15:09:00 +010052 if w := "protoc-gen-rust-deprecated"; !strings.Contains(cmd, w) {
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050053 t.Errorf("expected %q in %q", w, cmd)
54 }
55
Zach Johnson3df4e632020-11-06 11:56:27 -080056 // Check exported include directories
57 if w := "-Ishared_include"; !strings.Contains(cmd, w) {
58 t.Errorf("expected %q in %q", w, cmd)
59 }
60 if w := "-Istatic_include"; !strings.Contains(cmd, w) {
61 t.Errorf("expected %q in %q", w, cmd)
62 }
Ivan Lozano57f434e2020-10-28 09:32:10 -040063
64 // Check proto.rs, the second protobuf, is listed as an output
65 librust_proto_outputs := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_source").AllOutputs()
66 if android.InList("proto.rs", librust_proto_outputs) {
67 t.Errorf("rust_protobuf is not producing multiple outputs; expected 'proto.rs' in list, got: %#v ",
68 librust_proto_outputs)
69 }
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050070}
71
Jeff Vander Stoepc1490ec2023-04-24 11:28:25 +020072func TestRustProtobuf3(t *testing.T) {
73 ctx := testRust(t, `
74 rust_protobuf {
75 name: "librust_proto",
76 protos: ["buf.proto", "proto.proto"],
77 crate_name: "rust_proto",
78 source_stem: "buf",
79 use_protobuf3: true,
80 shared_libs: ["libfoo_shared"],
81 static_libs: ["libfoo_static"],
82 }
83 cc_library_shared {
84 name: "libfoo_shared",
85 export_include_dirs: ["shared_include"],
86 }
87 cc_library_static {
88 name: "libfoo_static",
89 export_include_dirs: ["static_include"],
90 }
91 `)
92 // Check that libprotobuf is added as a dependency.
93 librust_proto := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_dylib").Module().(*Module)
94 if !android.InList("libprotobuf", librust_proto.Properties.AndroidMkDylibs) {
95 t.Errorf("libprotobuf dependency missing for rust_protobuf (dependency missing from AndroidMkDylibs)")
96 }
97
98 // Make sure the correct plugin is being used.
99 librust_proto_out := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_source").Output("buf.rs")
100 cmd := librust_proto_out.RuleParams.Command
101 if w := "protoc-gen-rust"; !strings.Contains(cmd, w) {
102 t.Errorf("expected %q in %q", w, cmd)
103 }
104
105 // Check exported include directories
106 if w := "-Ishared_include"; !strings.Contains(cmd, w) {
107 t.Errorf("expected %q in %q", w, cmd)
108 }
109 if w := "-Istatic_include"; !strings.Contains(cmd, w) {
110 t.Errorf("expected %q in %q", w, cmd)
111 }
112
113 // Check proto.rs, the second protobuf, is listed as an output
114 librust_proto_outputs := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_source").AllOutputs()
115 if android.InList("proto.rs", librust_proto_outputs) {
116 t.Errorf("rust_protobuf is not producing multiple outputs; expected 'proto.rs' in list, got: %#v ",
117 librust_proto_outputs)
118 }
119}
120
Ivan Lozano6eff9002020-12-11 15:25:59 -0500121func TestRustGrpc(t *testing.T) {
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500122 ctx := testRust(t, `
Ivan Lozano6eff9002020-12-11 15:25:59 -0500123 rust_protobuf {
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500124 name: "librust_grpcio",
Ivan Lozano6eff9002020-12-11 15:25:59 -0500125 protos: ["buf.proto"],
126 grpc_protos: ["foo.proto", "proto.proto"],
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500127 crate_name: "rust_grpcio",
128 source_stem: "buf",
129 }
130 `)
131
132 // Check that libprotobuf is added as a dependency.
133 librust_grpcio_module := ctx.ModuleForTests("librust_grpcio", "android_arm64_armv8-a_dylib").Module().(*Module)
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500134
Zach Johnson3df4e632020-11-06 11:56:27 -0800135 // Check that libgrpcio is added as a dependency.
136 if !android.InList("libgrpcio", librust_grpcio_module.Properties.AndroidMkDylibs) {
137 t.Errorf("libgrpcio dependency missing for rust_grpcio (dependency missing from AndroidMkDylibs)")
138 }
139
140 // Check that libfutures is added as a dependency.
141 if !android.InList("libfutures", librust_grpcio_module.Properties.AndroidMkDylibs) {
142 t.Errorf("libfutures dependency missing for rust_grpcio (dependency missing from AndroidMkDylibs)")
143 }
144
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500145 // Make sure the correct plugin is being used.
Ivan Lozano6eff9002020-12-11 15:25:59 -0500146 librust_grpcio_out := ctx.ModuleForTests("librust_grpcio", "android_arm64_armv8-a_source").Output("foo_grpc.rs")
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500147 cmd := librust_grpcio_out.RuleParams.Command
148 if w := "protoc-gen-grpc"; !strings.Contains(cmd, w) {
149 t.Errorf("expected %q in %q", w, cmd)
150 }
Zach Johnson3df4e632020-11-06 11:56:27 -0800151
Zach Johnson3df4e632020-11-06 11:56:27 -0800152 // Check that we're including the exported directory from libprotobuf-cpp-full
Paul Duffin2c4ca8d2021-03-07 19:18:38 +0000153 if w := "-I" + rustDefaultsDir + "libprotobuf-cpp-full-includes"; !strings.Contains(cmd, w) {
Zach Johnson3df4e632020-11-06 11:56:27 -0800154 t.Errorf("expected %q in %q", w, cmd)
155 }
Ivan Lozano57f434e2020-10-28 09:32:10 -0400156
157 // Check proto.rs, the second protobuf, is listed as an output
158 librust_grpcio_outputs := ctx.ModuleForTests("librust_grpcio", "android_arm64_armv8-a_source").AllOutputs()
159 if android.InList("proto_grpc.rs", librust_grpcio_outputs) {
160 t.Errorf("rust_protobuf is not producing multiple outputs; expected 'proto_grpc.rs' in list, got: %#v ",
161 librust_grpcio_outputs)
162 }
Treehugger Robot588aae72020-08-21 10:01:58 +0000163}
Ivan Lozano6eff9002020-12-11 15:25:59 -0500164
165func TestRustProtoErrors(t *testing.T) {
166 testRustError(t, "A proto can only be added once to either grpc_protos or protos.*", `
167 rust_protobuf {
168 name: "librust_grpcio",
169 protos: ["buf.proto"],
170 grpc_protos: ["buf.proto"],
171 crate_name: "rust_grpcio",
172 source_stem: "buf",
173 }
174 `)
175
176 testRustError(t, "proto filenames must be unique across 'protos' and 'grpc_protos'.*", `
177 rust_protobuf {
178 name: "librust_grpcio",
179 protos: ["buf.proto"],
180 grpc_protos: ["proto/buf.proto"],
181 crate_name: "rust_grpcio",
182 source_stem: "buf",
183 }
184 `)
185}