Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 1 | // 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 18 | "strings" |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 19 | "testing" |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 20 | |
| 21 | "android/soong/android" |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | func TestRustProtobuf(t *testing.T) { |
| 25 | ctx := testRust(t, ` |
| 26 | rust_protobuf { |
| 27 | name: "librust_proto", |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 28 | protos: ["buf.proto", "proto.proto"], |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 29 | crate_name: "rust_proto", |
| 30 | source_stem: "buf", |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 31 | 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 Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 41 | } |
| 42 | `) |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 43 | // Check that libprotobuf is added as a dependency. |
| 44 | librust_proto := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_dylib").Module().(*Module) |
Jeff Vander Stoep | 91c0466 | 2023-03-22 15:09:00 +0100 | [diff] [blame] | 45 | if !android.InList("libprotobuf_deprecated", librust_proto.Properties.AndroidMkDylibs) { |
| 46 | t.Errorf("libprotobuf_deprecated dependency missing for rust_protobuf (dependency missing from AndroidMkDylibs)") |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 47 | } |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 48 | |
| 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 Stoep | 91c0466 | 2023-03-22 15:09:00 +0100 | [diff] [blame] | 52 | if w := "protoc-gen-rust-deprecated"; !strings.Contains(cmd, w) { |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 53 | t.Errorf("expected %q in %q", w, cmd) |
| 54 | } |
| 55 | |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 56 | // 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 Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 63 | |
| 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 Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 70 | } |
| 71 | |
Jeff Vander Stoep | c1490ec | 2023-04-24 11:28:25 +0200 | [diff] [blame] | 72 | func 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 Lozano | d106efe | 2023-09-21 23:30:26 -0400 | [diff] [blame^] | 121 | func TestRustProtobufInclude(t *testing.T) { |
| 122 | ctx := testRust(t, ` |
| 123 | rust_protobuf { |
| 124 | name: "librust_proto", |
| 125 | protos: ["proto.proto"], |
| 126 | crate_name: "rust_proto", |
| 127 | source_stem: "proto", |
| 128 | use_protobuf3: true, |
| 129 | rustlibs: ["librust_exported_proto", "libfoo"], |
| 130 | } |
| 131 | rust_protobuf { |
| 132 | name: "librust_exported_proto", |
| 133 | protos: ["proto.proto"], |
| 134 | crate_name: "rust_exported_proto", |
| 135 | source_stem: "exported_proto", |
| 136 | use_protobuf3: true, |
| 137 | exported_include_dirs: ["proto"] |
| 138 | } |
| 139 | rust_library { |
| 140 | name: "libfoo", |
| 141 | crate_name: "foo", |
| 142 | srcs: ["foo.rs"], |
| 143 | } |
| 144 | `) |
| 145 | // Check that librust_exported_proto is added as additional crate to generate source. |
| 146 | librust_proto := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_source").Module().(*Module).sourceProvider.(*protobufDecorator) |
| 147 | if !android.InList("rust_exported_proto", librust_proto.additionalCrates) { |
| 148 | t.Errorf("librust_proto should have librust_exported_proto included as an additional crate for generated source, instead got: %#v", librust_proto.additionalCrates) |
| 149 | } |
| 150 | |
| 151 | // Make sure the default crates aren't being included. |
| 152 | if android.InList("std", librust_proto.additionalCrates) { |
| 153 | t.Errorf("librust_proto should not have included libstd as an additional crate for generated source, instead got: %#v", librust_proto.additionalCrates) |
| 154 | } |
| 155 | if android.InList("protobuf", librust_proto.additionalCrates) { |
| 156 | t.Errorf("librust_proto should not have included libprotobuf as an additional crate for generated source, instead got: %#v", librust_proto.additionalCrates) |
| 157 | } |
| 158 | |
| 159 | // And make sure that non-protobuf crates aren't getting included either. |
| 160 | if android.InList("foo", librust_proto.additionalCrates) { |
| 161 | t.Errorf("librust_proto should not have included libfoo as an additional crate for generated source, instead got: %#v", librust_proto.additionalCrates) |
| 162 | } |
| 163 | |
| 164 | // Check librust_proto args includes -Iproto |
| 165 | librust_proto_rule := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_source").Output("proto.rs") |
| 166 | cmd := librust_proto_rule.RuleParams.Command |
| 167 | if w := "-Iproto"; !strings.Contains(cmd, w) { |
| 168 | t.Errorf("expected %q in %q", w, cmd) |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 173 | func TestRustGrpc(t *testing.T) { |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 174 | ctx := testRust(t, ` |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 175 | rust_protobuf { |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 176 | name: "librust_grpcio", |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 177 | protos: ["buf.proto"], |
| 178 | grpc_protos: ["foo.proto", "proto.proto"], |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 179 | crate_name: "rust_grpcio", |
| 180 | source_stem: "buf", |
| 181 | } |
| 182 | `) |
| 183 | |
| 184 | // Check that libprotobuf is added as a dependency. |
| 185 | librust_grpcio_module := ctx.ModuleForTests("librust_grpcio", "android_arm64_armv8-a_dylib").Module().(*Module) |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 186 | |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 187 | // Check that libgrpcio is added as a dependency. |
| 188 | if !android.InList("libgrpcio", librust_grpcio_module.Properties.AndroidMkDylibs) { |
| 189 | t.Errorf("libgrpcio dependency missing for rust_grpcio (dependency missing from AndroidMkDylibs)") |
| 190 | } |
| 191 | |
| 192 | // Check that libfutures is added as a dependency. |
| 193 | if !android.InList("libfutures", librust_grpcio_module.Properties.AndroidMkDylibs) { |
| 194 | t.Errorf("libfutures dependency missing for rust_grpcio (dependency missing from AndroidMkDylibs)") |
| 195 | } |
| 196 | |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 197 | // Make sure the correct plugin is being used. |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 198 | librust_grpcio_out := ctx.ModuleForTests("librust_grpcio", "android_arm64_armv8-a_source").Output("foo_grpc.rs") |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 199 | cmd := librust_grpcio_out.RuleParams.Command |
| 200 | if w := "protoc-gen-grpc"; !strings.Contains(cmd, w) { |
| 201 | t.Errorf("expected %q in %q", w, cmd) |
| 202 | } |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 203 | |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 204 | // Check that we're including the exported directory from libprotobuf-cpp-full |
Paul Duffin | 2c4ca8d | 2021-03-07 19:18:38 +0000 | [diff] [blame] | 205 | if w := "-I" + rustDefaultsDir + "libprotobuf-cpp-full-includes"; !strings.Contains(cmd, w) { |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 206 | t.Errorf("expected %q in %q", w, cmd) |
| 207 | } |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 208 | |
| 209 | // Check proto.rs, the second protobuf, is listed as an output |
| 210 | librust_grpcio_outputs := ctx.ModuleForTests("librust_grpcio", "android_arm64_armv8-a_source").AllOutputs() |
| 211 | if android.InList("proto_grpc.rs", librust_grpcio_outputs) { |
| 212 | t.Errorf("rust_protobuf is not producing multiple outputs; expected 'proto_grpc.rs' in list, got: %#v ", |
| 213 | librust_grpcio_outputs) |
| 214 | } |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 215 | } |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 216 | |
| 217 | func TestRustProtoErrors(t *testing.T) { |
| 218 | testRustError(t, "A proto can only be added once to either grpc_protos or protos.*", ` |
| 219 | rust_protobuf { |
| 220 | name: "librust_grpcio", |
| 221 | protos: ["buf.proto"], |
| 222 | grpc_protos: ["buf.proto"], |
| 223 | crate_name: "rust_grpcio", |
| 224 | source_stem: "buf", |
| 225 | } |
| 226 | `) |
| 227 | |
| 228 | testRustError(t, "proto filenames must be unique across 'protos' and 'grpc_protos'.*", ` |
| 229 | rust_protobuf { |
| 230 | name: "librust_grpcio", |
| 231 | protos: ["buf.proto"], |
| 232 | grpc_protos: ["proto/buf.proto"], |
| 233 | crate_name: "rust_grpcio", |
| 234 | source_stem: "buf", |
| 235 | } |
| 236 | `) |
| 237 | } |