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", |
Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame^] | 26 | defaults: ["cc_defaults_flags"], |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 27 | wrapper_src: "src/any.h", |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 28 | crate_name: "bindgen", |
| 29 | stem: "libbindgen", |
| 30 | source_stem: "bindings", |
Stephen Crane | 12e2cb7 | 2020-08-04 12:26:10 -0700 | [diff] [blame] | 31 | bindgen_flags: ["--bindgen-flag.*"], |
| 32 | cflags: ["--clang-flag()"], |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 33 | shared_libs: ["libfoo_shared"], |
| 34 | static_libs: ["libfoo_static"], |
| 35 | } |
| 36 | cc_library_shared { |
| 37 | name: "libfoo_shared", |
| 38 | export_include_dirs: ["shared_include"], |
| 39 | } |
| 40 | cc_library_static { |
| 41 | name: "libfoo_static", |
| 42 | export_include_dirs: ["static_include"], |
| 43 | } |
Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame^] | 44 | cc_defaults { |
| 45 | name: "cc_defaults_flags", |
| 46 | cflags: ["--default-flag"], |
| 47 | } |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 48 | `) |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 49 | libbindgen := ctx.ModuleForTests("libbindgen", "android_arm64_armv8-a_source").Output("bindings.rs") |
Stephen Crane | 12e2cb7 | 2020-08-04 12:26:10 -0700 | [diff] [blame] | 50 | // Ensure that the flags are present and escaped |
| 51 | if !strings.Contains(libbindgen.Args["flags"], "'--bindgen-flag.*'") { |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 52 | t.Errorf("missing bindgen flags in rust_bindgen rule: flags %#v", libbindgen.Args["flags"]) |
| 53 | } |
Stephen Crane | 12e2cb7 | 2020-08-04 12:26:10 -0700 | [diff] [blame] | 54 | if !strings.Contains(libbindgen.Args["cflags"], "'--clang-flag()'") { |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 55 | t.Errorf("missing clang cflags in rust_bindgen rule: cflags %#v", libbindgen.Args["cflags"]) |
| 56 | } |
| 57 | if !strings.Contains(libbindgen.Args["cflags"], "-Ishared_include") { |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 58 | 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] | 59 | } |
| 60 | if !strings.Contains(libbindgen.Args["cflags"], "-Istatic_include") { |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 61 | 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] | 62 | } |
Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame^] | 63 | if !strings.Contains(libbindgen.Args["cflags"], "--default-flag") { |
| 64 | t.Errorf("rust_bindgen missing cflags defined in cc_defaults: cflags %#v", libbindgen.Args["cflags"]) |
| 65 | } |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 66 | } |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 67 | |
| 68 | func TestRustBindgenCustomBindgen(t *testing.T) { |
| 69 | ctx := testRust(t, ` |
| 70 | rust_bindgen { |
| 71 | name: "libbindgen", |
| 72 | wrapper_src: "src/any.h", |
| 73 | crate_name: "bindgen", |
| 74 | stem: "libbindgen", |
| 75 | source_stem: "bindings", |
| 76 | custom_bindgen: "my_bindgen" |
| 77 | } |
| 78 | rust_binary_host { |
| 79 | name: "my_bindgen", |
| 80 | srcs: ["foo.rs"], |
| 81 | } |
| 82 | `) |
| 83 | |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 84 | libbindgen := ctx.ModuleForTests("libbindgen", "android_arm64_armv8-a_source").Output("bindings.rs") |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 85 | |
| 86 | // The rule description should contain the custom binary name rather than bindgen, so checking the description |
| 87 | // should be sufficient. |
| 88 | if !strings.Contains(libbindgen.Description, "my_bindgen") { |
| 89 | t.Errorf("Custom bindgen binary %s not used for libbindgen: rule description %#v", "my_bindgen", |
| 90 | libbindgen.Description) |
| 91 | } |
| 92 | } |
Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 93 | |
| 94 | func TestRustBindgenStdVersions(t *testing.T) { |
| 95 | testRustError(t, "c_std and cpp_std cannot both be defined at the same time.", ` |
| 96 | rust_bindgen { |
| 97 | name: "libbindgen", |
| 98 | wrapper_src: "src/any.h", |
| 99 | crate_name: "bindgen", |
| 100 | stem: "libbindgen", |
| 101 | source_stem: "bindings", |
| 102 | c_std: "somevalue", |
| 103 | cpp_std: "somevalue", |
| 104 | } |
| 105 | `) |
| 106 | |
| 107 | ctx := testRust(t, ` |
| 108 | rust_bindgen { |
| 109 | name: "libbindgen_cstd", |
| 110 | wrapper_src: "src/any.h", |
| 111 | crate_name: "bindgen", |
| 112 | stem: "libbindgen", |
| 113 | source_stem: "bindings", |
| 114 | c_std: "foo" |
| 115 | } |
| 116 | rust_bindgen { |
| 117 | name: "libbindgen_cppstd", |
| 118 | wrapper_src: "src/any.h", |
| 119 | crate_name: "bindgen", |
| 120 | stem: "libbindgen", |
| 121 | source_stem: "bindings", |
| 122 | cpp_std: "foo" |
| 123 | } |
| 124 | `) |
| 125 | |
Thiébaud Weksteen | e0510d7 | 2020-09-25 15:50:09 +0200 | [diff] [blame] | 126 | libbindgen_cstd := ctx.ModuleForTests("libbindgen_cstd", "android_arm64_armv8-a_source").Output("bindings.rs") |
| 127 | libbindgen_cppstd := ctx.ModuleForTests("libbindgen_cppstd", "android_arm64_armv8-a_source").Output("bindings.rs") |
Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 128 | |
| 129 | if !strings.Contains(libbindgen_cstd.Args["cflags"], "-std=foo") { |
| 130 | t.Errorf("c_std value not passed in to rust_bindgen as a clang flag") |
| 131 | } |
| 132 | |
| 133 | if !strings.Contains(libbindgen_cppstd.Args["cflags"], "-std=foo") { |
| 134 | t.Errorf("cpp_std value not passed in to rust_bindgen as a clang flag") |
| 135 | } |
| 136 | } |