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 15package rust 16 17import ( 18 "strings" 19 "testing" 20) 21 22// Test that variants are being generated correctly, and that crate-types are correct. 23func 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 32 // Test all variants are being built. 33 libfooRlib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_rlib").Output("libfoo.rlib") 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" 42 43 // Test crate type for rlib is correct. 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"]) 46 } 47 48 // Test crate type for dylib is correct. 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"]) 51 } 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 63} 64 65// Test that dylibs are not statically linking the standard library. 66func 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 74 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Output("libfoo.dylib.so") 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} 80 81func 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} 117