Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [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 ( |
| 18 | "strings" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | func TestRustBindgen(t *testing.T) { |
| 23 | ctx := testRust(t, ` |
| 24 | rust_bindgen { |
| 25 | name: "libbindgen", |
| 26 | wrapper_src: "src/any.h", |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 27 | crate_name: "bindgen", |
| 28 | stem: "libbindgen", |
| 29 | source_stem: "bindings", |
| 30 | bindgen_flags: ["--bindgen-flag"], |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 31 | cflags: ["--clang-flag"], |
| 32 | shared_libs: ["libfoo_shared"], |
| 33 | static_libs: ["libfoo_static"], |
| 34 | } |
| 35 | cc_library_shared { |
| 36 | name: "libfoo_shared", |
| 37 | export_include_dirs: ["shared_include"], |
| 38 | } |
| 39 | cc_library_static { |
| 40 | name: "libfoo_static", |
| 41 | export_include_dirs: ["static_include"], |
| 42 | } |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 43 | `) |
| 44 | libbindgen := ctx.ModuleForTests("libbindgen", "android_arm64_armv8-a").Output("bindings.rs") |
| 45 | if !strings.Contains(libbindgen.Args["flags"], "--bindgen-flag") { |
| 46 | t.Errorf("missing bindgen flags in rust_bindgen rule: flags %#v", libbindgen.Args["flags"]) |
| 47 | } |
| 48 | if !strings.Contains(libbindgen.Args["cflags"], "--clang-flag") { |
| 49 | t.Errorf("missing clang cflags in rust_bindgen rule: cflags %#v", libbindgen.Args["cflags"]) |
| 50 | } |
| 51 | if !strings.Contains(libbindgen.Args["cflags"], "-Ishared_include") { |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 52 | t.Errorf("missing shared_libs exported includes in rust_bindgen rule: cflags %#v", libbindgen.Args["cflags"]) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 53 | } |
| 54 | if !strings.Contains(libbindgen.Args["cflags"], "-Istatic_include") { |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 55 | t.Errorf("missing static_libs exported includes in rust_bindgen rule: cflags %#v", libbindgen.Args["cflags"]) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 56 | } |
| 57 | } |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame^] | 58 | |
| 59 | func TestRustBindgenCustomBindgen(t *testing.T) { |
| 60 | ctx := testRust(t, ` |
| 61 | rust_bindgen { |
| 62 | name: "libbindgen", |
| 63 | wrapper_src: "src/any.h", |
| 64 | crate_name: "bindgen", |
| 65 | stem: "libbindgen", |
| 66 | source_stem: "bindings", |
| 67 | custom_bindgen: "my_bindgen" |
| 68 | } |
| 69 | rust_binary_host { |
| 70 | name: "my_bindgen", |
| 71 | srcs: ["foo.rs"], |
| 72 | } |
| 73 | `) |
| 74 | |
| 75 | libbindgen := ctx.ModuleForTests("libbindgen", "android_arm64_armv8-a").Output("bindings.rs") |
| 76 | |
| 77 | // The rule description should contain the custom binary name rather than bindgen, so checking the description |
| 78 | // should be sufficient. |
| 79 | if !strings.Contains(libbindgen.Description, "my_bindgen") { |
| 80 | t.Errorf("Custom bindgen binary %s not used for libbindgen: rule description %#v", "my_bindgen", |
| 81 | libbindgen.Description) |
| 82 | } |
| 83 | } |