blob: 66bcd20fb283c14c7dbc4e9313a40fe69468bf0c [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"
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
Ivan Lozano52767be2019-10-18 14:49:46 -070032 // Test all variants are being built.
Ivan Lozanoffee3342019-08-27 12:03:00 -070033 libfooRlib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_rlib").Output("libfoo.rlib")
Ivan Lozano52767be2019-10-18 14:49:46 -070034 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 Lozanoffee3342019-08-27 12:03:00 -070042
43 // Test crate type for rlib is correct.
Ivan Lozano52767be2019-10-18 14:49:46 -070044 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 Lozanoffee3342019-08-27 12:03:00 -070046 }
47
48 // Test crate type for dylib is correct.
Ivan Lozano52767be2019-10-18 14:49:46 -070049 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 Lozanoffee3342019-08-27 12:03:00 -070051 }
Ivan Lozano52767be2019-10-18 14:49:46 -070052
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 Lozanoffee3342019-08-27 12:03:00 -070063}
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
Ivan Lozano52767be2019-10-18 14:49:46 -070074 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Output("libfoo.dylib.so")
Ivan Lozanoffee3342019-08-27 12:03:00 -070075
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}