blob: 8a91cf10f34ab09d62b52bc511df036361adb075 [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// Copyright 2019 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"
Ivan Lozano7e741cc2020-06-19 12:32:30 -040020
21 "android/soong/android"
Ivan Lozanoffee3342019-08-27 12:03:00 -070022)
23
24// Test that variants are being generated correctly, and that crate-types are correct.
25func TestLibraryVariants(t *testing.T) {
26
27 ctx := testRust(t, `
28 rust_library_host {
29 name: "libfoo",
30 srcs: ["foo.rs"],
31 crate_name: "foo",
Matthew Maurer2ae05132020-06-23 14:28:53 -070032 }
33 rust_ffi_host {
34 name: "libfoo.ffi",
35 srcs: ["foo.rs"],
36 crate_name: "foo"
37 }`)
Ivan Lozanoffee3342019-08-27 12:03:00 -070038
Ivan Lozano52767be2019-10-18 14:49:46 -070039 // Test all variants are being built.
Ivan Lozanoffee3342019-08-27 12:03:00 -070040 libfooRlib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_rlib").Output("libfoo.rlib")
Ivan Lozano52767be2019-10-18 14:49:46 -070041 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Output("libfoo.dylib.so")
Matthew Maurer2ae05132020-06-23 14:28:53 -070042 libfooStatic := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_static").Output("libfoo.ffi.a")
43 libfooShared := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_shared").Output("libfoo.ffi.so")
Ivan Lozano52767be2019-10-18 14:49:46 -070044
45 rlibCrateType := "rlib"
46 dylibCrateType := "dylib"
47 sharedCrateType := "cdylib"
48 staticCrateType := "static"
Ivan Lozanoffee3342019-08-27 12:03:00 -070049
50 // Test crate type for rlib is correct.
Ivan Lozano52767be2019-10-18 14:49:46 -070051 if !strings.Contains(libfooRlib.Args["rustcFlags"], "crate-type="+rlibCrateType) {
52 t.Errorf("missing crate-type for static variant, expecting %#v, rustcFlags: %#v", rlibCrateType, libfooRlib.Args["rustcFlags"])
Ivan Lozanoffee3342019-08-27 12:03:00 -070053 }
54
55 // Test crate type for dylib is correct.
Ivan Lozano52767be2019-10-18 14:49:46 -070056 if !strings.Contains(libfooDylib.Args["rustcFlags"], "crate-type="+dylibCrateType) {
57 t.Errorf("missing crate-type for static variant, expecting %#v, rustcFlags: %#v", dylibCrateType, libfooDylib.Args["rustcFlags"])
Ivan Lozanoffee3342019-08-27 12:03:00 -070058 }
Ivan Lozano52767be2019-10-18 14:49:46 -070059
60 // Test crate type for C static libraries is correct.
61 if !strings.Contains(libfooStatic.Args["rustcFlags"], "crate-type="+staticCrateType) {
62 t.Errorf("missing crate-type for static variant, expecting %#v, rustcFlags: %#v", staticCrateType, libfooStatic.Args["rustcFlags"])
63 }
64
65 // Test crate type for C shared libraries is correct.
66 if !strings.Contains(libfooShared.Args["rustcFlags"], "crate-type="+sharedCrateType) {
67 t.Errorf("missing crate-type for shared variant, expecting %#v, got rustcFlags: %#v", sharedCrateType, libfooShared.Args["rustcFlags"])
68 }
69
Ivan Lozanoffee3342019-08-27 12:03:00 -070070}
71
72// Test that dylibs are not statically linking the standard library.
73func TestDylibPreferDynamic(t *testing.T) {
74 ctx := testRust(t, `
75 rust_library_host_dylib {
76 name: "libfoo",
77 srcs: ["foo.rs"],
78 crate_name: "foo",
79 }`)
80
Ivan Lozano52767be2019-10-18 14:49:46 -070081 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Output("libfoo.dylib.so")
Ivan Lozanoffee3342019-08-27 12:03:00 -070082
83 if !strings.Contains(libfooDylib.Args["rustcFlags"], "prefer-dynamic") {
84 t.Errorf("missing prefer-dynamic flag for libfoo dylib, rustcFlags: %#v", libfooDylib.Args["rustcFlags"])
85 }
86}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070087
88func TestValidateLibraryStem(t *testing.T) {
89 testRustError(t, "crate_name must be defined.", `
90 rust_library_host {
91 name: "libfoo",
92 srcs: ["foo.rs"],
93 }`)
94
95 testRustError(t, "library crate_names must be alphanumeric with underscores allowed", `
96 rust_library_host {
97 name: "libfoo-bar",
98 srcs: ["foo.rs"],
99 crate_name: "foo-bar"
100 }`)
101
102 testRustError(t, "Invalid name or stem property; library filenames must start with lib<crate_name>", `
103 rust_library_host {
104 name: "foobar",
105 srcs: ["foo.rs"],
106 crate_name: "foo_bar"
107 }`)
108 testRustError(t, "Invalid name or stem property; library filenames must start with lib<crate_name>", `
109 rust_library_host {
110 name: "foobar",
111 stem: "libfoo",
112 srcs: ["foo.rs"],
113 crate_name: "foo_bar"
114 }`)
115 testRustError(t, "Invalid name or stem property; library filenames must start with lib<crate_name>", `
116 rust_library_host {
117 name: "foobar",
118 stem: "foo_bar",
119 srcs: ["foo.rs"],
120 crate_name: "foo_bar"
121 }`)
122
123}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400124
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400125func TestSharedLibrary(t *testing.T) {
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400126 ctx := testRust(t, `
Matthew Maurer2ae05132020-06-23 14:28:53 -0700127 rust_ffi_shared {
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400128 name: "libfoo",
129 srcs: ["foo.rs"],
130 crate_name: "foo",
131 }`)
132
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400133 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared")
134
135 libfooOutput := libfoo.Output("libfoo.so")
136 if !strings.Contains(libfooOutput.Args["linkFlags"], "-Wl,-soname=libfoo.so") {
137 t.Errorf("missing expected -Wl,-soname linker flag for libfoo shared lib, linkFlags: %#v",
138 libfooOutput.Args["linkFlags"])
139 }
140
141 if !android.InList("libstd", libfoo.Module().(*Module).Properties.AndroidMkDylibs) {
142 t.Errorf("Non-static libstd dylib expected to be a dependency of Rust shared libraries. Dylib deps are: %#v",
143 libfoo.Module().(*Module).Properties.AndroidMkDylibs)
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400144 }
145}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700146
147// Test that variants pull in the right type of rustlib autodep
148func TestAutoDeps(t *testing.T) {
149
150 ctx := testRust(t, `
151 rust_library_host {
152 name: "libbar",
153 srcs: ["bar.rs"],
154 crate_name: "bar",
155 }
156 rust_library_host {
157 name: "libfoo",
158 srcs: ["foo.rs"],
159 crate_name: "foo",
160 rustlibs: ["libbar"],
161 }
162 rust_ffi_host {
163 name: "libfoo.ffi",
164 srcs: ["foo.rs"],
165 crate_name: "foo",
166 rustlibs: ["libbar"],
167 }`)
168
169 libfooRlib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_rlib")
170 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib")
171 libfooStatic := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_static")
172 libfooShared := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_shared")
173
174 for _, static := range []android.TestingModule{libfooRlib, libfooStatic} {
175 if !android.InList("libbar", static.Module().(*Module).Properties.AndroidMkRlibs) {
176 t.Errorf("libbar not present as static dependency in static lib")
177 }
178 if android.InList("libbar", static.Module().(*Module).Properties.AndroidMkDylibs) {
179 t.Errorf("libbar present as dynamic dependency in static lib")
180 }
181 }
182
183 for _, dyn := range []android.TestingModule{libfooDylib, libfooShared} {
184 if !android.InList("libbar", dyn.Module().(*Module).Properties.AndroidMkDylibs) {
185 t.Errorf("libbar not present as dynamic dependency in dynamic lib")
186 }
187 if android.InList("libbar", dyn.Module().(*Module).Properties.AndroidMkRlibs) {
188 t.Errorf("libbar present as static dependency in dynamic lib")
189 }
190
191 }
192}