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 feature flags are being correctly generated. |
| 23 | func 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. |
| 44 | func TestEnforceSingleSourceFile(t *testing.T) { |
| 45 | |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame^] | 46 | singleSrcError := "srcs can only contain one path for a rust file and source providers prefixed by \":\"" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 47 | |
| 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 Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 67 | }`) |
| 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 Lozano | f900f4b | 2020-04-28 13:58:45 -0400 | [diff] [blame] | 77 | |
| 78 | func 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 | } |