blob: 9cccf13be0e8ad28132759412b889f27a8b7c09e [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"],
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 Lozanobc9e4212020-09-25 16:08:34 -040044 cc_defaults {
45 name: "cc_defaults_flags",
46 cflags: ["--default-flag"],
47 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -040048 `)
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020049 libbindgen := ctx.ModuleForTests("libbindgen", "android_arm64_armv8-a_source").Output("bindings.rs")
Stephen Crane12e2cb72020-08-04 12:26:10 -070050 // Ensure that the flags are present and escaped
51 if !strings.Contains(libbindgen.Args["flags"], "'--bindgen-flag.*'") {
Ivan Lozano4fef93c2020-07-08 08:39:44 -040052 t.Errorf("missing bindgen flags in rust_bindgen rule: flags %#v", libbindgen.Args["flags"])
53 }
Stephen Crane12e2cb72020-08-04 12:26:10 -070054 if !strings.Contains(libbindgen.Args["cflags"], "'--clang-flag()'") {
Ivan Lozano4fef93c2020-07-08 08:39:44 -040055 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 Lozano45901ed2020-07-24 16:05:01 -040058 t.Errorf("missing shared_libs exported includes in rust_bindgen rule: cflags %#v", libbindgen.Args["cflags"])
Ivan Lozano4fef93c2020-07-08 08:39:44 -040059 }
60 if !strings.Contains(libbindgen.Args["cflags"], "-Istatic_include") {
Ivan Lozano45901ed2020-07-24 16:05:01 -040061 t.Errorf("missing static_libs exported includes in rust_bindgen rule: cflags %#v", libbindgen.Args["cflags"])
Ivan Lozano4fef93c2020-07-08 08:39:44 -040062 }
Ivan Lozanobc9e4212020-09-25 16:08:34 -040063 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 Lozano4fef93c2020-07-08 08:39:44 -040066}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040067
68func 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 Weksteen295c72b2020-09-23 18:10:17 +020084 libbindgen := ctx.ModuleForTests("libbindgen", "android_arm64_armv8-a_source").Output("bindings.rs")
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040085
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 Lozano3d947522020-09-23 13:26:25 -040093
94func 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 Weksteene0510d72020-09-25 15:50:09 +0200126 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 Lozano3d947522020-09-23 13:26:25 -0400128
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}