blob: b8531964338520b5b9f10b72b46c80a3a181370c [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 feature flags are being correctly generated.
23func TestFeaturesToFlags(t *testing.T) {
24 ctx := testRust(t, `
25 rust_library_host_dylib {
26 name: "libfoo",
27 srcs: ["foo.rs"],
28 crate_name: "foo",
29 features: [
30 "fizz",
31 "buzz"
32 ],
33 }`)
34
35 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Rule("rustc")
36
37 if !strings.Contains(libfooDylib.Args["rustcFlags"], "cfg 'feature=\"fizz\"'") ||
38 !strings.Contains(libfooDylib.Args["rustcFlags"], "cfg 'feature=\"buzz\"'") {
39 t.Fatalf("missing fizz and buzz feature flags for libfoo dylib, rustcFlags: %#v", libfooDylib.Args["rustcFlags"])
40 }
41}
42
43// Test that we reject multiple source files.
44func TestEnforceSingleSourceFile(t *testing.T) {
45
Ivan Lozano43845682020-07-09 21:03:28 -040046 singleSrcError := "srcs can only contain one path for a rust file and source providers prefixed by \":\""
Ivan Lozanoffee3342019-08-27 12:03:00 -070047
48 // Test libraries
49 testRustError(t, singleSrcError, `
50 rust_library_host {
51 name: "foo-bar-library",
52 srcs: ["foo.rs", "src/bar.rs"],
53 }`)
54
55 // Test binaries
56 testRustError(t, singleSrcError, `
57 rust_binary_host {
58 name: "foo-bar-binary",
59 srcs: ["foo.rs", "src/bar.rs"],
60 }`)
61
62 // Test proc_macros
63 testRustError(t, singleSrcError, `
64 rust_proc_macro {
65 name: "foo-bar-proc-macro",
66 srcs: ["foo.rs", "src/bar.rs"],
Ivan Lozanoffee3342019-08-27 12:03:00 -070067 }`)
68
69 // Test prebuilts
70 testRustError(t, singleSrcError, `
71 rust_prebuilt_dylib {
72 name: "foo-bar-prebuilt",
73 srcs: ["liby.so", "libz.so"],
74 host_supported: true,
75 }`)
76}
Ivan Lozanof900f4b2020-04-28 13:58:45 -040077
78func TestInstallDir(t *testing.T) {
79 ctx := testRust(t, `
80 rust_library_dylib {
81 name: "libfoo",
82 srcs: ["foo.rs"],
83 crate_name: "foo",
84 }
85 rust_binary {
86 name: "fizzbuzz",
87 srcs: ["foo.rs"],
88 }`)
89
90 install_path_lib64 := ctx.ModuleForTests("libfoo",
91 "android_arm64_armv8-a_dylib").Module().(*Module).compiler.(*libraryDecorator).path.String()
92 install_path_lib32 := ctx.ModuleForTests("libfoo",
93 "android_arm_armv7-a-neon_dylib").Module().(*Module).compiler.(*libraryDecorator).path.String()
94 install_path_bin := ctx.ModuleForTests("fizzbuzz",
95 "android_arm64_armv8-a").Module().(*Module).compiler.(*binaryDecorator).path.String()
96
97 if !strings.HasSuffix(install_path_lib64, "system/lib64/libfoo.dylib.so") {
98 t.Fatalf("unexpected install path for 64-bit library: %#v", install_path_lib64)
99 }
100 if !strings.HasSuffix(install_path_lib32, "system/lib/libfoo.dylib.so") {
101 t.Fatalf("unexpected install path for 32-bit library: %#v", install_path_lib32)
102 }
103 if !strings.HasSuffix(install_path_bin, "system/bin/fizzbuzz") {
104 t.Fatalf("unexpected install path for binary: %#v", install_path_bin)
105 }
106}