blob: 9d2f6c00ae8a511a8bd0e3e0566c97eca0b929ba [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",
32 }`)
33
Ivan Lozano52767be2019-10-18 14:49:46 -070034 // Test all variants are being built.
Ivan Lozanoffee3342019-08-27 12:03:00 -070035 libfooRlib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_rlib").Output("libfoo.rlib")
Ivan Lozano52767be2019-10-18 14:49:46 -070036 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Output("libfoo.dylib.so")
37 libfooStatic := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_static").Output("libfoo.a")
38 libfooShared := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_shared").Output("libfoo.so")
39
40 rlibCrateType := "rlib"
41 dylibCrateType := "dylib"
42 sharedCrateType := "cdylib"
43 staticCrateType := "static"
Ivan Lozanoffee3342019-08-27 12:03:00 -070044
45 // Test crate type for rlib is correct.
Ivan Lozano52767be2019-10-18 14:49:46 -070046 if !strings.Contains(libfooRlib.Args["rustcFlags"], "crate-type="+rlibCrateType) {
47 t.Errorf("missing crate-type for static variant, expecting %#v, rustcFlags: %#v", rlibCrateType, libfooRlib.Args["rustcFlags"])
Ivan Lozanoffee3342019-08-27 12:03:00 -070048 }
49
50 // Test crate type for dylib is correct.
Ivan Lozano52767be2019-10-18 14:49:46 -070051 if !strings.Contains(libfooDylib.Args["rustcFlags"], "crate-type="+dylibCrateType) {
52 t.Errorf("missing crate-type for static variant, expecting %#v, rustcFlags: %#v", dylibCrateType, libfooDylib.Args["rustcFlags"])
Ivan Lozanoffee3342019-08-27 12:03:00 -070053 }
Ivan Lozano52767be2019-10-18 14:49:46 -070054
55 // Test crate type for C static libraries is correct.
56 if !strings.Contains(libfooStatic.Args["rustcFlags"], "crate-type="+staticCrateType) {
57 t.Errorf("missing crate-type for static variant, expecting %#v, rustcFlags: %#v", staticCrateType, libfooStatic.Args["rustcFlags"])
58 }
59
60 // Test crate type for C shared libraries is correct.
61 if !strings.Contains(libfooShared.Args["rustcFlags"], "crate-type="+sharedCrateType) {
62 t.Errorf("missing crate-type for shared variant, expecting %#v, got rustcFlags: %#v", sharedCrateType, libfooShared.Args["rustcFlags"])
63 }
64
Ivan Lozanoffee3342019-08-27 12:03:00 -070065}
66
67// Test that dylibs are not statically linking the standard library.
68func TestDylibPreferDynamic(t *testing.T) {
69 ctx := testRust(t, `
70 rust_library_host_dylib {
71 name: "libfoo",
72 srcs: ["foo.rs"],
73 crate_name: "foo",
74 }`)
75
Ivan Lozano52767be2019-10-18 14:49:46 -070076 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Output("libfoo.dylib.so")
Ivan Lozanoffee3342019-08-27 12:03:00 -070077
78 if !strings.Contains(libfooDylib.Args["rustcFlags"], "prefer-dynamic") {
79 t.Errorf("missing prefer-dynamic flag for libfoo dylib, rustcFlags: %#v", libfooDylib.Args["rustcFlags"])
80 }
81}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070082
83func TestValidateLibraryStem(t *testing.T) {
84 testRustError(t, "crate_name must be defined.", `
85 rust_library_host {
86 name: "libfoo",
87 srcs: ["foo.rs"],
88 }`)
89
90 testRustError(t, "library crate_names must be alphanumeric with underscores allowed", `
91 rust_library_host {
92 name: "libfoo-bar",
93 srcs: ["foo.rs"],
94 crate_name: "foo-bar"
95 }`)
96
97 testRustError(t, "Invalid name or stem property; library filenames must start with lib<crate_name>", `
98 rust_library_host {
99 name: "foobar",
100 srcs: ["foo.rs"],
101 crate_name: "foo_bar"
102 }`)
103 testRustError(t, "Invalid name or stem property; library filenames must start with lib<crate_name>", `
104 rust_library_host {
105 name: "foobar",
106 stem: "libfoo",
107 srcs: ["foo.rs"],
108 crate_name: "foo_bar"
109 }`)
110 testRustError(t, "Invalid name or stem property; library filenames must start with lib<crate_name>", `
111 rust_library_host {
112 name: "foobar",
113 stem: "foo_bar",
114 srcs: ["foo.rs"],
115 crate_name: "foo_bar"
116 }`)
117
118}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400119
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400120func TestSharedLibrary(t *testing.T) {
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400121 ctx := testRust(t, `
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400122 rust_library {
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400123 name: "libfoo",
124 srcs: ["foo.rs"],
125 crate_name: "foo",
126 }`)
127
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400128 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared")
129
130 libfooOutput := libfoo.Output("libfoo.so")
131 if !strings.Contains(libfooOutput.Args["linkFlags"], "-Wl,-soname=libfoo.so") {
132 t.Errorf("missing expected -Wl,-soname linker flag for libfoo shared lib, linkFlags: %#v",
133 libfooOutput.Args["linkFlags"])
134 }
135
136 if !android.InList("libstd", libfoo.Module().(*Module).Properties.AndroidMkDylibs) {
137 t.Errorf("Non-static libstd dylib expected to be a dependency of Rust shared libraries. Dylib deps are: %#v",
138 libfoo.Module().(*Module).Properties.AndroidMkDylibs)
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400139 }
140}