blob: 0ba0ff840b6f6ba7ae128e25df3cc6252650855a [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",
Ivan Lozanobc9e4212020-09-25 16:08:34 -040026 defaults: ["cc_defaults_flags"],
Ivan Lozano4fef93c2020-07-08 08:39:44 -040027 wrapper_src: "src/any.h",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040028 crate_name: "bindgen",
29 stem: "libbindgen",
30 source_stem: "bindings",
Stephen Crane12e2cb72020-08-04 12:26:10 -070031 bindgen_flags: ["--bindgen-flag.*"],
32 cflags: ["--clang-flag()"],
Ivan Lozano4fef93c2020-07-08 08:39:44 -040033 shared_libs: ["libfoo_shared"],
34 static_libs: ["libfoo_static"],
Ivan Lozano9b443832020-11-17 13:39:30 -050035 header_libs: ["libfoo_header"],
Ivan Lozano4fef93c2020-07-08 08:39:44 -040036 }
37 cc_library_shared {
38 name: "libfoo_shared",
39 export_include_dirs: ["shared_include"],
40 }
41 cc_library_static {
42 name: "libfoo_static",
43 export_include_dirs: ["static_include"],
44 }
Ivan Lozano9b443832020-11-17 13:39:30 -050045 cc_library_headers {
46 name: "libfoo_header",
47 export_include_dirs: ["header_include"],
48 }
Ivan Lozanobc9e4212020-09-25 16:08:34 -040049 cc_defaults {
50 name: "cc_defaults_flags",
51 cflags: ["--default-flag"],
52 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -040053 `)
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020054 libbindgen := ctx.ModuleForTests("libbindgen", "android_arm64_armv8-a_source").Output("bindings.rs")
Stephen Crane12e2cb72020-08-04 12:26:10 -070055 // Ensure that the flags are present and escaped
56 if !strings.Contains(libbindgen.Args["flags"], "'--bindgen-flag.*'") {
Ivan Lozano4fef93c2020-07-08 08:39:44 -040057 t.Errorf("missing bindgen flags in rust_bindgen rule: flags %#v", libbindgen.Args["flags"])
58 }
Stephen Crane12e2cb72020-08-04 12:26:10 -070059 if !strings.Contains(libbindgen.Args["cflags"], "'--clang-flag()'") {
Ivan Lozano4fef93c2020-07-08 08:39:44 -040060 t.Errorf("missing clang cflags in rust_bindgen rule: cflags %#v", libbindgen.Args["cflags"])
61 }
62 if !strings.Contains(libbindgen.Args["cflags"], "-Ishared_include") {
Ivan Lozano45901ed2020-07-24 16:05:01 -040063 t.Errorf("missing shared_libs exported includes in rust_bindgen rule: cflags %#v", libbindgen.Args["cflags"])
Ivan Lozano4fef93c2020-07-08 08:39:44 -040064 }
65 if !strings.Contains(libbindgen.Args["cflags"], "-Istatic_include") {
Ivan Lozano45901ed2020-07-24 16:05:01 -040066 t.Errorf("missing static_libs exported includes in rust_bindgen rule: cflags %#v", libbindgen.Args["cflags"])
Ivan Lozano4fef93c2020-07-08 08:39:44 -040067 }
Ivan Lozano9b443832020-11-17 13:39:30 -050068 if !strings.Contains(libbindgen.Args["cflags"], "-Iheader_include") {
69 t.Errorf("missing static_libs exported includes in rust_bindgen rule: cflags %#v", libbindgen.Args["cflags"])
70 }
Ivan Lozanobc9e4212020-09-25 16:08:34 -040071 if !strings.Contains(libbindgen.Args["cflags"], "--default-flag") {
72 t.Errorf("rust_bindgen missing cflags defined in cc_defaults: cflags %#v", libbindgen.Args["cflags"])
73 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -040074}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040075
76func TestRustBindgenCustomBindgen(t *testing.T) {
77 ctx := testRust(t, `
78 rust_bindgen {
79 name: "libbindgen",
80 wrapper_src: "src/any.h",
81 crate_name: "bindgen",
82 stem: "libbindgen",
83 source_stem: "bindings",
84 custom_bindgen: "my_bindgen"
85 }
86 rust_binary_host {
87 name: "my_bindgen",
88 srcs: ["foo.rs"],
89 }
90 `)
91
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020092 libbindgen := ctx.ModuleForTests("libbindgen", "android_arm64_armv8-a_source").Output("bindings.rs")
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040093
94 // The rule description should contain the custom binary name rather than bindgen, so checking the description
95 // should be sufficient.
96 if !strings.Contains(libbindgen.Description, "my_bindgen") {
97 t.Errorf("Custom bindgen binary %s not used for libbindgen: rule description %#v", "my_bindgen",
98 libbindgen.Description)
99 }
100}
Ivan Lozano3d947522020-09-23 13:26:25 -0400101
102func TestRustBindgenStdVersions(t *testing.T) {
103 testRustError(t, "c_std and cpp_std cannot both be defined at the same time.", `
104 rust_bindgen {
105 name: "libbindgen",
106 wrapper_src: "src/any.h",
107 crate_name: "bindgen",
108 stem: "libbindgen",
109 source_stem: "bindings",
110 c_std: "somevalue",
111 cpp_std: "somevalue",
112 }
113 `)
114
115 ctx := testRust(t, `
116 rust_bindgen {
117 name: "libbindgen_cstd",
Ivan Lozano829e1e92023-10-09 11:30:09 -0400118 wrapper_src: "src/any.hpp",
Ivan Lozano3d947522020-09-23 13:26:25 -0400119 crate_name: "bindgen",
120 stem: "libbindgen",
121 source_stem: "bindings",
122 c_std: "foo"
123 }
124 rust_bindgen {
125 name: "libbindgen_cppstd",
126 wrapper_src: "src/any.h",
127 crate_name: "bindgen",
128 stem: "libbindgen",
129 source_stem: "bindings",
130 cpp_std: "foo"
131 }
132 `)
133
Thiébaud Weksteene0510d72020-09-25 15:50:09 +0200134 libbindgen_cstd := ctx.ModuleForTests("libbindgen_cstd", "android_arm64_armv8-a_source").Output("bindings.rs")
135 libbindgen_cppstd := ctx.ModuleForTests("libbindgen_cppstd", "android_arm64_armv8-a_source").Output("bindings.rs")
Ivan Lozano3d947522020-09-23 13:26:25 -0400136
137 if !strings.Contains(libbindgen_cstd.Args["cflags"], "-std=foo") {
138 t.Errorf("c_std value not passed in to rust_bindgen as a clang flag")
139 }
140
141 if !strings.Contains(libbindgen_cppstd.Args["cflags"], "-std=foo") {
142 t.Errorf("cpp_std value not passed in to rust_bindgen as a clang flag")
143 }
Ivan Lozano829e1e92023-10-09 11:30:09 -0400144
145 // Make sure specifying cpp_std emits the '-x c++' flag
146 if !strings.Contains(libbindgen_cppstd.Args["cflags"], "-x c++") {
147 t.Errorf("Setting cpp_std should cause the '-x c++' flag to be emitted")
148 }
149
150 // Make sure specifying c_std omits the '-x c++' flag
151 if strings.Contains(libbindgen_cstd.Args["cflags"], "-x c++") {
152 t.Errorf("Setting c_std should not cause the '-x c++' flag to be emitted")
153 }
Ivan Lozano3d947522020-09-23 13:26:25 -0400154}
Ivan Lozano0a2a1152020-10-16 10:49:08 -0400155
156func TestBindgenDisallowedFlags(t *testing.T) {
157 // Make sure passing '-x c++' to cflags generates an error
158 testRustError(t, "cflags: -x c\\+\\+ should not be specified in cflags.*", `
159 rust_bindgen {
160 name: "libbad_flag",
161 wrapper_src: "src/any.h",
162 crate_name: "bindgen",
163 stem: "libbindgen",
164 source_stem: "bindings",
165 cflags: ["-x c++"]
166 }
167 `)
168
169 // Make sure passing '-std=' to cflags generates an error
170 testRustError(t, "cflags: -std should not be specified in cflags.*", `
171 rust_bindgen {
172 name: "libbad_flag",
173 wrapper_src: "src/any.h",
174 crate_name: "bindgen",
175 stem: "libbindgen",
176 source_stem: "bindings",
177 cflags: ["-std=foo"]
178 }
179 `)
180}
Andrei Homescu78a1f8d2023-07-08 00:35:15 +0000181
182func TestBindgenFlagFile(t *testing.T) {
183 ctx := testRust(t, `
184 rust_bindgen {
185 name: "libbindgen",
186 wrapper_src: "src/any.h",
187 crate_name: "bindgen",
188 stem: "libbindgen",
189 source_stem: "bindings",
190 bindgen_flag_files: [
191 "flag_file.txt",
192 ],
193 }
194 `)
195 libbindgen := ctx.ModuleForTests("libbindgen", "android_arm64_armv8-a_source").Output("bindings.rs")
196
197 if !strings.Contains(libbindgen.Args["flagfiles"], "/dev/null") {
198 t.Errorf("missing /dev/null in rust_bindgen rule: flags %#v", libbindgen.Args["flagfiles"])
199 }
200 if !strings.Contains(libbindgen.Args["flagfiles"], "flag_file.txt") {
201 t.Errorf("missing bindgen flags file in rust_bindgen rule: flags %#v", libbindgen.Args["flagfiles"])
202 }
203 // TODO: The best we can do right now is check $flagfiles. Once bindgen.go switches to RuleBuilder,
204 // we may be able to check libbinder.RuleParams.Command to see if it contains $(cat /dev/null flag_file.txt)
205}