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) |
| 45 | if !android.InList("libprotobuf", librust_proto.Properties.AndroidMkDylibs) { |
| 46 | t.Errorf("libprotobuf dependency missing for rust_protobuf (dependency missing from AndroidMkDylibs)") |
| 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 |
| 52 | if w := "protoc-gen-rust"; !strings.Contains(cmd, w) { |
| 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 | |
| 72 | func TestRustGrpcio(t *testing.T) { |
| 73 | ctx := testRust(t, ` |
| 74 | rust_grpcio { |
| 75 | name: "librust_grpcio", |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame^] | 76 | protos: ["buf.proto", "proto.proto"], |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 77 | crate_name: "rust_grpcio", |
| 78 | source_stem: "buf", |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 79 | shared_libs: ["libfoo_shared"], |
| 80 | static_libs: ["libfoo_static"], |
| 81 | } |
| 82 | cc_library_shared { |
| 83 | name: "libfoo_shared", |
| 84 | export_include_dirs: ["shared_include"], |
| 85 | } |
| 86 | cc_library_static { |
| 87 | name: "libfoo_static", |
| 88 | export_include_dirs: ["static_include"], |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 89 | } |
| 90 | `) |
| 91 | |
| 92 | // Check that libprotobuf is added as a dependency. |
| 93 | librust_grpcio_module := ctx.ModuleForTests("librust_grpcio", "android_arm64_armv8-a_dylib").Module().(*Module) |
| 94 | if !android.InList("libprotobuf", librust_grpcio_module.Properties.AndroidMkDylibs) { |
| 95 | t.Errorf("libprotobuf dependency missing for rust_grpcio (dependency missing from AndroidMkDylibs)") |
| 96 | } |
| 97 | |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 98 | // Check that libgrpcio is added as a dependency. |
| 99 | if !android.InList("libgrpcio", librust_grpcio_module.Properties.AndroidMkDylibs) { |
| 100 | t.Errorf("libgrpcio dependency missing for rust_grpcio (dependency missing from AndroidMkDylibs)") |
| 101 | } |
| 102 | |
| 103 | // Check that libfutures is added as a dependency. |
| 104 | if !android.InList("libfutures", librust_grpcio_module.Properties.AndroidMkDylibs) { |
| 105 | t.Errorf("libfutures dependency missing for rust_grpcio (dependency missing from AndroidMkDylibs)") |
| 106 | } |
| 107 | |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 108 | // Make sure the correct plugin is being used. |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 109 | librust_grpcio_out := ctx.ModuleForTests("librust_grpcio", "android_arm64_armv8-a_source").Output("buf_grpc.rs") |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 110 | cmd := librust_grpcio_out.RuleParams.Command |
| 111 | if w := "protoc-gen-grpc"; !strings.Contains(cmd, w) { |
| 112 | t.Errorf("expected %q in %q", w, cmd) |
| 113 | } |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 114 | |
| 115 | // Check exported include directories |
| 116 | if w := "-Ishared_include"; !strings.Contains(cmd, w) { |
| 117 | t.Errorf("expected %q in %q", w, cmd) |
| 118 | } |
| 119 | if w := "-Istatic_include"; !strings.Contains(cmd, w) { |
| 120 | t.Errorf("expected %q in %q", w, cmd) |
| 121 | } |
| 122 | |
| 123 | // Check that we're including the exported directory from libprotobuf-cpp-full |
| 124 | if w := "-Ilibprotobuf-cpp-full-includes"; !strings.Contains(cmd, w) { |
| 125 | t.Errorf("expected %q in %q", w, cmd) |
| 126 | } |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame^] | 127 | |
| 128 | // Check proto.rs, the second protobuf, is listed as an output |
| 129 | librust_grpcio_outputs := ctx.ModuleForTests("librust_grpcio", "android_arm64_armv8-a_source").AllOutputs() |
| 130 | if android.InList("proto_grpc.rs", librust_grpcio_outputs) { |
| 131 | t.Errorf("rust_protobuf is not producing multiple outputs; expected 'proto_grpc.rs' in list, got: %#v ", |
| 132 | librust_grpcio_outputs) |
| 133 | } |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 134 | } |