blob: 359f28b83c75457f97a268567e5cf7b31fd9b41f [file] [log] [blame]
Ivan Lozano4fef93c2020-07-08 08:39:44 -04001// 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 (
18 "strings"
19 "testing"
20)
21
22func TestRustBindgen(t *testing.T) {
23 ctx := testRust(t, `
24 rust_bindgen {
25 name: "libbindgen",
26 wrapper_src: "src/any.h",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040027 crate_name: "bindgen",
28 stem: "libbindgen",
29 source_stem: "bindings",
Stephen Crane12e2cb72020-08-04 12:26:10 -070030 bindgen_flags: ["--bindgen-flag.*"],
31 cflags: ["--clang-flag()"],
Ivan Lozano4fef93c2020-07-08 08:39:44 -040032 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 Lozano4fef93c2020-07-08 08:39:44 -040043 `)
44 libbindgen := ctx.ModuleForTests("libbindgen", "android_arm64_armv8-a").Output("bindings.rs")
Stephen Crane12e2cb72020-08-04 12:26:10 -070045 // Ensure that the flags are present and escaped
46 if !strings.Contains(libbindgen.Args["flags"], "'--bindgen-flag.*'") {
Ivan Lozano4fef93c2020-07-08 08:39:44 -040047 t.Errorf("missing bindgen flags in rust_bindgen rule: flags %#v", libbindgen.Args["flags"])
48 }
Stephen Crane12e2cb72020-08-04 12:26:10 -070049 if !strings.Contains(libbindgen.Args["cflags"], "'--clang-flag()'") {
Ivan Lozano4fef93c2020-07-08 08:39:44 -040050 t.Errorf("missing clang cflags in rust_bindgen rule: cflags %#v", libbindgen.Args["cflags"])
51 }
52 if !strings.Contains(libbindgen.Args["cflags"], "-Ishared_include") {
Ivan Lozano45901ed2020-07-24 16:05:01 -040053 t.Errorf("missing shared_libs exported includes in rust_bindgen rule: cflags %#v", libbindgen.Args["cflags"])
Ivan Lozano4fef93c2020-07-08 08:39:44 -040054 }
55 if !strings.Contains(libbindgen.Args["cflags"], "-Istatic_include") {
Ivan Lozano45901ed2020-07-24 16:05:01 -040056 t.Errorf("missing static_libs exported includes in rust_bindgen rule: cflags %#v", libbindgen.Args["cflags"])
Ivan Lozano4fef93c2020-07-08 08:39:44 -040057 }
58}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040059
60func TestRustBindgenCustomBindgen(t *testing.T) {
61 ctx := testRust(t, `
62 rust_bindgen {
63 name: "libbindgen",
64 wrapper_src: "src/any.h",
65 crate_name: "bindgen",
66 stem: "libbindgen",
67 source_stem: "bindings",
68 custom_bindgen: "my_bindgen"
69 }
70 rust_binary_host {
71 name: "my_bindgen",
72 srcs: ["foo.rs"],
73 }
74 `)
75
76 libbindgen := ctx.ModuleForTests("libbindgen", "android_arm64_armv8-a").Output("bindings.rs")
77
78 // The rule description should contain the custom binary name rather than bindgen, so checking the description
79 // should be sufficient.
80 if !strings.Contains(libbindgen.Description, "my_bindgen") {
81 t.Errorf("Custom bindgen binary %s not used for libbindgen: rule description %#v", "my_bindgen",
82 libbindgen.Description)
83 }
84}
Ivan Lozano3d947522020-09-23 13:26:25 -040085
86func TestRustBindgenStdVersions(t *testing.T) {
87 testRustError(t, "c_std and cpp_std cannot both be defined at the same time.", `
88 rust_bindgen {
89 name: "libbindgen",
90 wrapper_src: "src/any.h",
91 crate_name: "bindgen",
92 stem: "libbindgen",
93 source_stem: "bindings",
94 c_std: "somevalue",
95 cpp_std: "somevalue",
96 }
97 `)
98
99 ctx := testRust(t, `
100 rust_bindgen {
101 name: "libbindgen_cstd",
102 wrapper_src: "src/any.h",
103 crate_name: "bindgen",
104 stem: "libbindgen",
105 source_stem: "bindings",
106 c_std: "foo"
107 }
108 rust_bindgen {
109 name: "libbindgen_cppstd",
110 wrapper_src: "src/any.h",
111 crate_name: "bindgen",
112 stem: "libbindgen",
113 source_stem: "bindings",
114 cpp_std: "foo"
115 }
116 `)
117
118 libbindgen_cstd := ctx.ModuleForTests("libbindgen_cstd", "android_arm64_armv8-a").Output("bindings.rs")
119 libbindgen_cppstd := ctx.ModuleForTests("libbindgen_cppstd", "android_arm64_armv8-a").Output("bindings.rs")
120
121 if !strings.Contains(libbindgen_cstd.Args["cflags"], "-std=foo") {
122 t.Errorf("c_std value not passed in to rust_bindgen as a clang flag")
123 }
124
125 if !strings.Contains(libbindgen_cppstd.Args["cflags"], "-std=foo") {
126 t.Errorf("cpp_std value not passed in to rust_bindgen as a clang flag")
127 }
128}