Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
| 18 | "strings" |
| 19 | "testing" |
Ivan Lozano | 7e741cc | 2020-06-19 12:32:30 -0400 | [diff] [blame^] | 20 | |
| 21 | "android/soong/android" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | // Test that variants are being generated correctly, and that crate-types are correct. |
| 25 | func 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 Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 34 | // Test all variants are being built. |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 35 | libfooRlib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_rlib").Output("libfoo.rlib") |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 36 | 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 Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 44 | |
| 45 | // Test crate type for rlib is correct. |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 46 | 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 Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // Test crate type for dylib is correct. |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 51 | 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 Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 53 | } |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 54 | |
| 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 Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // Test that dylibs are not statically linking the standard library. |
| 68 | func 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 Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 76 | libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Output("libfoo.dylib.so") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 77 | |
| 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 Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 82 | |
| 83 | func 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 Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 119 | |
Ivan Lozano | 7e741cc | 2020-06-19 12:32:30 -0400 | [diff] [blame^] | 120 | func TestSharedLibrary(t *testing.T) { |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 121 | ctx := testRust(t, ` |
Ivan Lozano | 7e741cc | 2020-06-19 12:32:30 -0400 | [diff] [blame^] | 122 | rust_library { |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 123 | name: "libfoo", |
| 124 | srcs: ["foo.rs"], |
| 125 | crate_name: "foo", |
| 126 | }`) |
| 127 | |
Ivan Lozano | 7e741cc | 2020-06-19 12:32:30 -0400 | [diff] [blame^] | 128 | 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 Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 139 | } |
| 140 | } |