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" |
ThiƩbaud Weksteen | 9e8451e | 2020-08-13 12:55:59 +0200 | [diff] [blame] | 20 | |
| 21 | "android/soong/android" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | // Test that feature flags are being correctly generated. |
| 25 | func TestFeaturesToFlags(t *testing.T) { |
| 26 | ctx := testRust(t, ` |
| 27 | rust_library_host_dylib { |
| 28 | name: "libfoo", |
| 29 | srcs: ["foo.rs"], |
| 30 | crate_name: "foo", |
| 31 | features: [ |
| 32 | "fizz", |
| 33 | "buzz" |
| 34 | ], |
| 35 | }`) |
| 36 | |
| 37 | libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Rule("rustc") |
| 38 | |
| 39 | if !strings.Contains(libfooDylib.Args["rustcFlags"], "cfg 'feature=\"fizz\"'") || |
| 40 | !strings.Contains(libfooDylib.Args["rustcFlags"], "cfg 'feature=\"buzz\"'") { |
| 41 | t.Fatalf("missing fizz and buzz feature flags for libfoo dylib, rustcFlags: %#v", libfooDylib.Args["rustcFlags"]) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // Test that we reject multiple source files. |
| 46 | func TestEnforceSingleSourceFile(t *testing.T) { |
| 47 | |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 48 | 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] | 49 | |
| 50 | // Test libraries |
| 51 | testRustError(t, singleSrcError, ` |
| 52 | rust_library_host { |
| 53 | name: "foo-bar-library", |
| 54 | srcs: ["foo.rs", "src/bar.rs"], |
| 55 | }`) |
| 56 | |
| 57 | // Test binaries |
| 58 | testRustError(t, singleSrcError, ` |
| 59 | rust_binary_host { |
| 60 | name: "foo-bar-binary", |
| 61 | srcs: ["foo.rs", "src/bar.rs"], |
| 62 | }`) |
| 63 | |
| 64 | // Test proc_macros |
| 65 | testRustError(t, singleSrcError, ` |
| 66 | rust_proc_macro { |
| 67 | name: "foo-bar-proc-macro", |
| 68 | srcs: ["foo.rs", "src/bar.rs"], |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 69 | }`) |
| 70 | |
| 71 | // Test prebuilts |
| 72 | testRustError(t, singleSrcError, ` |
| 73 | rust_prebuilt_dylib { |
| 74 | name: "foo-bar-prebuilt", |
| 75 | srcs: ["liby.so", "libz.so"], |
| 76 | host_supported: true, |
| 77 | }`) |
| 78 | } |
Ivan Lozano | f900f4b | 2020-04-28 13:58:45 -0400 | [diff] [blame] | 79 | |
| 80 | func TestInstallDir(t *testing.T) { |
| 81 | ctx := testRust(t, ` |
| 82 | rust_library_dylib { |
| 83 | name: "libfoo", |
| 84 | srcs: ["foo.rs"], |
| 85 | crate_name: "foo", |
| 86 | } |
| 87 | rust_binary { |
| 88 | name: "fizzbuzz", |
| 89 | srcs: ["foo.rs"], |
| 90 | }`) |
| 91 | |
| 92 | install_path_lib64 := ctx.ModuleForTests("libfoo", |
| 93 | "android_arm64_armv8-a_dylib").Module().(*Module).compiler.(*libraryDecorator).path.String() |
| 94 | install_path_lib32 := ctx.ModuleForTests("libfoo", |
| 95 | "android_arm_armv7-a-neon_dylib").Module().(*Module).compiler.(*libraryDecorator).path.String() |
| 96 | install_path_bin := ctx.ModuleForTests("fizzbuzz", |
| 97 | "android_arm64_armv8-a").Module().(*Module).compiler.(*binaryDecorator).path.String() |
| 98 | |
| 99 | if !strings.HasSuffix(install_path_lib64, "system/lib64/libfoo.dylib.so") { |
| 100 | t.Fatalf("unexpected install path for 64-bit library: %#v", install_path_lib64) |
| 101 | } |
| 102 | if !strings.HasSuffix(install_path_lib32, "system/lib/libfoo.dylib.so") { |
| 103 | t.Fatalf("unexpected install path for 32-bit library: %#v", install_path_lib32) |
| 104 | } |
| 105 | if !strings.HasSuffix(install_path_bin, "system/bin/fizzbuzz") { |
| 106 | t.Fatalf("unexpected install path for binary: %#v", install_path_bin) |
| 107 | } |
| 108 | } |
ThiƩbaud Weksteen | 9e8451e | 2020-08-13 12:55:59 +0200 | [diff] [blame] | 109 | |
| 110 | func TestLints(t *testing.T) { |
| 111 | |
| 112 | bp := ` |
| 113 | // foo uses the default value of lints |
| 114 | rust_library { |
| 115 | name: "libfoo", |
| 116 | srcs: ["foo.rs"], |
| 117 | crate_name: "foo", |
| 118 | } |
| 119 | // bar forces the use of the "android" lint set |
| 120 | rust_library { |
| 121 | name: "libbar", |
| 122 | srcs: ["foo.rs"], |
| 123 | crate_name: "bar", |
| 124 | lints: "android", |
| 125 | } |
| 126 | // foobar explicitly disable all lints |
| 127 | rust_library { |
| 128 | name: "libfoobar", |
| 129 | srcs: ["foo.rs"], |
| 130 | crate_name: "foobar", |
| 131 | lints: "none", |
| 132 | }` |
| 133 | |
| 134 | bp = bp + GatherRequiredDepsForTest() |
| 135 | |
| 136 | fs := map[string][]byte{ |
| 137 | // Reuse the same blueprint file for subdirectories. |
| 138 | "external/Android.bp": []byte(bp), |
| 139 | "hardware/Android.bp": []byte(bp), |
| 140 | } |
| 141 | |
| 142 | var lintTests = []struct { |
| 143 | modulePath string |
| 144 | fooFlags string |
| 145 | }{ |
| 146 | {"", "${config.RustDefaultLints}"}, |
| 147 | {"external/", "${config.RustAllowAllLints}"}, |
| 148 | {"hardware/", "${config.RustVendorLints}"}, |
| 149 | } |
| 150 | |
| 151 | for _, tc := range lintTests { |
| 152 | t.Run("path="+tc.modulePath, func(t *testing.T) { |
| 153 | |
| 154 | config := android.TestArchConfig(buildDir, nil, bp, fs) |
| 155 | ctx := CreateTestContext() |
| 156 | ctx.Register(config) |
| 157 | _, errs := ctx.ParseFileList(".", []string{tc.modulePath + "Android.bp"}) |
| 158 | android.FailIfErrored(t, errs) |
| 159 | _, errs = ctx.PrepareBuildActions(config) |
| 160 | android.FailIfErrored(t, errs) |
| 161 | |
| 162 | r := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").MaybeRule("rustc") |
| 163 | if !strings.Contains(r.Args["rustcFlags"], tc.fooFlags) { |
| 164 | t.Errorf("Incorrect flags for libfoo: %q, want %q", r.Args["rustcFlags"], tc.fooFlags) |
| 165 | } |
| 166 | |
| 167 | r = ctx.ModuleForTests("libbar", "android_arm64_armv8-a_dylib").MaybeRule("rustc") |
| 168 | if !strings.Contains(r.Args["rustcFlags"], "${config.RustDefaultLints}") { |
| 169 | t.Errorf("Incorrect flags for libbar: %q, want %q", r.Args["rustcFlags"], "${config.RustDefaultLints}") |
| 170 | } |
| 171 | |
| 172 | r = ctx.ModuleForTests("libfoobar", "android_arm64_armv8-a_dylib").MaybeRule("rustc") |
| 173 | if !strings.Contains(r.Args["rustcFlags"], "${config.RustAllowAllLints}") { |
| 174 | t.Errorf("Incorrect flags for libfoobar: %q, want %q", r.Args["rustcFlags"], "${config.RustAllowAllLints}") |
| 175 | } |
| 176 | |
| 177 | }) |
| 178 | } |
| 179 | } |
Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 180 | |
| 181 | // Test that devices are linking the stdlib dynamically |
| 182 | func TestStdDeviceLinkage(t *testing.T) { |
| 183 | ctx := testRust(t, ` |
| 184 | rust_binary { |
| 185 | name: "fizz", |
| 186 | srcs: ["foo.rs"], |
| 187 | } |
| 188 | rust_library { |
| 189 | name: "libfoo", |
| 190 | srcs: ["foo.rs"], |
| 191 | crate_name: "foo", |
| 192 | }`) |
| 193 | fizz := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Module().(*Module) |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 194 | fooRlib := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std").Module().(*Module) |
Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 195 | fooDylib := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module) |
| 196 | |
| 197 | if !android.InList("libstd", fizz.Properties.AndroidMkDylibs) { |
| 198 | t.Errorf("libstd is not linked dynamically for device binaries") |
| 199 | } |
| 200 | if !android.InList("libstd", fooRlib.Properties.AndroidMkDylibs) { |
| 201 | t.Errorf("libstd is not linked dynamically for rlibs") |
| 202 | } |
| 203 | if !android.InList("libstd", fooDylib.Properties.AndroidMkDylibs) { |
| 204 | t.Errorf("libstd is not linked dynamically for dylibs") |
| 205 | } |
| 206 | } |