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" |
| 20 | ) |
| 21 | |
| 22 | // Test that variants are being generated correctly, and that crate-types are correct. |
| 23 | func TestLibraryVariants(t *testing.T) { |
| 24 | |
| 25 | ctx := testRust(t, ` |
| 26 | rust_library_host { |
| 27 | name: "libfoo", |
| 28 | srcs: ["foo.rs"], |
| 29 | crate_name: "foo", |
| 30 | }`) |
| 31 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 32 | // Test all variants are being built. |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 33 | libfooRlib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_rlib").Output("libfoo.rlib") |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 34 | libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Output("libfoo.dylib.so") |
| 35 | libfooStatic := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_static").Output("libfoo.a") |
| 36 | libfooShared := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_shared").Output("libfoo.so") |
| 37 | |
| 38 | rlibCrateType := "rlib" |
| 39 | dylibCrateType := "dylib" |
| 40 | sharedCrateType := "cdylib" |
| 41 | staticCrateType := "static" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 42 | |
| 43 | // Test crate type for rlib is correct. |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 44 | if !strings.Contains(libfooRlib.Args["rustcFlags"], "crate-type="+rlibCrateType) { |
| 45 | 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] | 46 | } |
| 47 | |
| 48 | // Test crate type for dylib is correct. |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 49 | if !strings.Contains(libfooDylib.Args["rustcFlags"], "crate-type="+dylibCrateType) { |
| 50 | 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] | 51 | } |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 52 | |
| 53 | // Test crate type for C static libraries is correct. |
| 54 | if !strings.Contains(libfooStatic.Args["rustcFlags"], "crate-type="+staticCrateType) { |
| 55 | t.Errorf("missing crate-type for static variant, expecting %#v, rustcFlags: %#v", staticCrateType, libfooStatic.Args["rustcFlags"]) |
| 56 | } |
| 57 | |
| 58 | // Test crate type for C shared libraries is correct. |
| 59 | if !strings.Contains(libfooShared.Args["rustcFlags"], "crate-type="+sharedCrateType) { |
| 60 | t.Errorf("missing crate-type for shared variant, expecting %#v, got rustcFlags: %#v", sharedCrateType, libfooShared.Args["rustcFlags"]) |
| 61 | } |
| 62 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Test that dylibs are not statically linking the standard library. |
| 66 | func TestDylibPreferDynamic(t *testing.T) { |
| 67 | ctx := testRust(t, ` |
| 68 | rust_library_host_dylib { |
| 69 | name: "libfoo", |
| 70 | srcs: ["foo.rs"], |
| 71 | crate_name: "foo", |
| 72 | }`) |
| 73 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 74 | 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] | 75 | |
| 76 | if !strings.Contains(libfooDylib.Args["rustcFlags"], "prefer-dynamic") { |
| 77 | t.Errorf("missing prefer-dynamic flag for libfoo dylib, rustcFlags: %#v", libfooDylib.Args["rustcFlags"]) |
| 78 | } |
| 79 | } |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 80 | |
| 81 | func TestValidateLibraryStem(t *testing.T) { |
| 82 | testRustError(t, "crate_name must be defined.", ` |
| 83 | rust_library_host { |
| 84 | name: "libfoo", |
| 85 | srcs: ["foo.rs"], |
| 86 | }`) |
| 87 | |
| 88 | testRustError(t, "library crate_names must be alphanumeric with underscores allowed", ` |
| 89 | rust_library_host { |
| 90 | name: "libfoo-bar", |
| 91 | srcs: ["foo.rs"], |
| 92 | crate_name: "foo-bar" |
| 93 | }`) |
| 94 | |
| 95 | testRustError(t, "Invalid name or stem property; library filenames must start with lib<crate_name>", ` |
| 96 | rust_library_host { |
| 97 | name: "foobar", |
| 98 | srcs: ["foo.rs"], |
| 99 | crate_name: "foo_bar" |
| 100 | }`) |
| 101 | testRustError(t, "Invalid name or stem property; library filenames must start with lib<crate_name>", ` |
| 102 | rust_library_host { |
| 103 | name: "foobar", |
| 104 | stem: "libfoo", |
| 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: "foo_bar", |
| 112 | srcs: ["foo.rs"], |
| 113 | crate_name: "foo_bar" |
| 114 | }`) |
| 115 | |
| 116 | } |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame^] | 117 | |
| 118 | func TestSharedLibraryFlags(t *testing.T) { |
| 119 | ctx := testRust(t, ` |
| 120 | rust_library_host { |
| 121 | name: "libfoo", |
| 122 | srcs: ["foo.rs"], |
| 123 | crate_name: "foo", |
| 124 | }`) |
| 125 | |
| 126 | libfooShared := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_shared").Output("libfoo.so") |
| 127 | if !strings.Contains(libfooShared.Args["linkFlags"], "-Wl,-soname=libfoo.so") { |
| 128 | t.Errorf("missing expected -Wl,-soname linker flag for libfoo shared lib, linkFlags: %#v", libfooShared.Args["linkFlags"]) |
| 129 | } |
| 130 | } |