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 | "io/ioutil" |
| 19 | "os" |
Ivan Lozano | c008361 | 2019-09-03 13:49:39 -0700 | [diff] [blame] | 20 | "runtime" |
Ivan Lozano | b9040d6 | 2019-09-24 13:23:50 -0700 | [diff] [blame] | 21 | "strings" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 22 | "testing" |
| 23 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 24 | "github.com/google/blueprint/proptools" |
| 25 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 26 | "android/soong/android" |
Colin Cross | f28329d | 2020-02-15 11:00:10 -0800 | [diff] [blame] | 27 | "android/soong/cc" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | var ( |
| 31 | buildDir string |
| 32 | ) |
| 33 | |
| 34 | func setUp() { |
| 35 | var err error |
| 36 | buildDir, err = ioutil.TempDir("", "soong_rust_test") |
| 37 | if err != nil { |
| 38 | panic(err) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func tearDown() { |
| 43 | os.RemoveAll(buildDir) |
| 44 | } |
| 45 | |
| 46 | func TestMain(m *testing.M) { |
| 47 | run := func() int { |
| 48 | setUp() |
| 49 | defer tearDown() |
| 50 | |
| 51 | return m.Run() |
| 52 | } |
| 53 | |
| 54 | os.Exit(run()) |
| 55 | } |
| 56 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 57 | func testConfig(bp string) android.Config { |
| 58 | bp = bp + GatherRequiredDepsForTest() |
| 59 | |
| 60 | fs := map[string][]byte{ |
| 61 | "foo.rs": nil, |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 62 | "foo.c": nil, |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 63 | "src/bar.rs": nil, |
| 64 | "liby.so": nil, |
| 65 | "libz.so": nil, |
| 66 | } |
| 67 | |
Colin Cross | f28329d | 2020-02-15 11:00:10 -0800 | [diff] [blame] | 68 | cc.GatherRequiredFilesForTest(fs) |
| 69 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 70 | return android.TestArchConfig(buildDir, nil, bp, fs) |
| 71 | } |
| 72 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 73 | func testRust(t *testing.T, bp string) *android.TestContext { |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 74 | return testRustContext(t, bp, false) |
| 75 | } |
| 76 | |
| 77 | func testRustCov(t *testing.T, bp string) *android.TestContext { |
| 78 | return testRustContext(t, bp, true) |
| 79 | } |
| 80 | |
| 81 | func testRustContext(t *testing.T, bp string, coverage bool) *android.TestContext { |
Ivan Lozano | c008361 | 2019-09-03 13:49:39 -0700 | [diff] [blame] | 82 | // TODO (b/140435149) |
| 83 | if runtime.GOOS != "linux" { |
| 84 | t.Skip("Only the Linux toolchain is supported for Rust") |
| 85 | } |
| 86 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 87 | t.Helper() |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 88 | config := testConfig(bp) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 89 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 90 | if coverage { |
Colin Cross | 1a6acd4 | 2020-06-16 17:51:46 -0700 | [diff] [blame] | 91 | config.TestProductVariables.GcovCoverage = proptools.BoolPtr(true) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 92 | config.TestProductVariables.Native_coverage = proptools.BoolPtr(true) |
Roland Levillain | 4f5297b | 2020-06-09 12:44:06 +0100 | [diff] [blame] | 93 | config.TestProductVariables.NativeCoveragePaths = []string{"*"} |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 96 | ctx := CreateTestContext() |
| 97 | ctx.Register(config) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 98 | |
| 99 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 100 | android.FailIfErrored(t, errs) |
| 101 | _, errs = ctx.PrepareBuildActions(config) |
| 102 | android.FailIfErrored(t, errs) |
| 103 | |
| 104 | return ctx |
| 105 | } |
| 106 | |
| 107 | func testRustError(t *testing.T, pattern string, bp string) { |
Ivan Lozano | c008361 | 2019-09-03 13:49:39 -0700 | [diff] [blame] | 108 | // TODO (b/140435149) |
| 109 | if runtime.GOOS != "linux" { |
| 110 | t.Skip("Only the Linux toolchain is supported for Rust") |
| 111 | } |
| 112 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 113 | t.Helper() |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 114 | config := testConfig(bp) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 115 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 116 | ctx := CreateTestContext() |
| 117 | ctx.Register(config) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 118 | |
| 119 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 120 | if len(errs) > 0 { |
| 121 | android.FailIfNoMatchingErrors(t, pattern, errs) |
| 122 | return |
| 123 | } |
| 124 | |
| 125 | _, errs = ctx.PrepareBuildActions(config) |
| 126 | if len(errs) > 0 { |
| 127 | android.FailIfNoMatchingErrors(t, pattern, errs) |
| 128 | return |
| 129 | } |
| 130 | |
| 131 | t.Fatalf("missing expected error %q (0 errors are returned)", pattern) |
| 132 | } |
| 133 | |
| 134 | // Test that we can extract the lib name from a lib path. |
| 135 | func TestLibNameFromFilePath(t *testing.T) { |
Ivan Lozano | d648c43 | 2020-02-06 12:05:10 -0500 | [diff] [blame] | 136 | libBarPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/libbar.so.so") |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 137 | libLibPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/liblib.dylib.so") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 138 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 139 | libBarName := libNameFromFilePath(libBarPath) |
| 140 | libLibName := libNameFromFilePath(libLibPath) |
| 141 | |
Ivan Lozano | d648c43 | 2020-02-06 12:05:10 -0500 | [diff] [blame] | 142 | expectedResult := "bar.so" |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 143 | if libBarName != expectedResult { |
| 144 | t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libBarName) |
| 145 | } |
| 146 | |
| 147 | expectedResult = "lib.dylib" |
| 148 | if libLibName != expectedResult { |
| 149 | t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libLibPath) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | // Test that we can extract the link path from a lib path. |
| 154 | func TestLinkPathFromFilePath(t *testing.T) { |
| 155 | barPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/libbar.so") |
| 156 | libName := linkPathFromFilePath(barPath) |
| 157 | expectedResult := "out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/" |
| 158 | |
| 159 | if libName != expectedResult { |
| 160 | t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libName) |
| 161 | } |
| 162 | } |
| 163 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 164 | // Test to make sure dependencies are being picked up correctly. |
| 165 | func TestDepsTracking(t *testing.T) { |
| 166 | ctx := testRust(t, ` |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame^] | 167 | rust_ffi_host_static { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 168 | name: "libstatic", |
| 169 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 170 | crate_name: "static", |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 171 | } |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame^] | 172 | rust_ffi_host_shared { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 173 | name: "libshared", |
| 174 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 175 | crate_name: "shared", |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 176 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 177 | rust_library_host_dylib { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 178 | name: "libdylib", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 179 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 180 | crate_name: "dylib", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 181 | } |
| 182 | rust_library_host_rlib { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 183 | name: "librlib", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 184 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 185 | crate_name: "rlib", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 186 | } |
| 187 | rust_proc_macro { |
| 188 | name: "libpm", |
| 189 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 190 | crate_name: "pm", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 191 | } |
| 192 | rust_binary_host { |
| 193 | name: "fizz-buzz", |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 194 | dylibs: ["libdylib"], |
| 195 | rlibs: ["librlib"], |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 196 | proc_macros: ["libpm"], |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 197 | static_libs: ["libstatic"], |
| 198 | shared_libs: ["libshared"], |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 199 | srcs: ["foo.rs"], |
| 200 | } |
| 201 | `) |
| 202 | module := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module) |
| 203 | |
| 204 | // Since dependencies are added to AndroidMk* properties, we can check these to see if they've been picked up. |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 205 | if !android.InList("libdylib", module.Properties.AndroidMkDylibs) { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 206 | t.Errorf("Dylib dependency not detected (dependency missing from AndroidMkDylibs)") |
| 207 | } |
| 208 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 209 | if !android.InList("librlib", module.Properties.AndroidMkRlibs) { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 210 | t.Errorf("Rlib dependency not detected (dependency missing from AndroidMkRlibs)") |
| 211 | } |
| 212 | |
| 213 | if !android.InList("libpm", module.Properties.AndroidMkProcMacroLibs) { |
| 214 | t.Errorf("Proc_macro dependency not detected (dependency missing from AndroidMkProcMacroLibs)") |
| 215 | } |
| 216 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 217 | if !android.InList("libshared", module.Properties.AndroidMkSharedLibs) { |
| 218 | t.Errorf("Shared library dependency not detected (dependency missing from AndroidMkSharedLibs)") |
| 219 | } |
| 220 | |
| 221 | if !android.InList("libstatic", module.Properties.AndroidMkStaticLibs) { |
| 222 | t.Errorf("Static library dependency not detected (dependency missing from AndroidMkStaticLibs)") |
| 223 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 224 | } |
Ivan Lozano | b9040d6 | 2019-09-24 13:23:50 -0700 | [diff] [blame] | 225 | |
| 226 | // Test to make sure proc_macros use host variants when building device modules. |
| 227 | func TestProcMacroDeviceDeps(t *testing.T) { |
| 228 | ctx := testRust(t, ` |
| 229 | rust_library_host_rlib { |
| 230 | name: "libbar", |
| 231 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 232 | crate_name: "bar", |
Ivan Lozano | b9040d6 | 2019-09-24 13:23:50 -0700 | [diff] [blame] | 233 | } |
| 234 | rust_proc_macro { |
| 235 | name: "libpm", |
| 236 | rlibs: ["libbar"], |
| 237 | srcs: ["foo.rs"], |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 238 | crate_name: "pm", |
Ivan Lozano | b9040d6 | 2019-09-24 13:23:50 -0700 | [diff] [blame] | 239 | } |
| 240 | rust_binary { |
| 241 | name: "fizz-buzz", |
| 242 | proc_macros: ["libpm"], |
| 243 | srcs: ["foo.rs"], |
| 244 | } |
| 245 | `) |
| 246 | rustc := ctx.ModuleForTests("libpm", "linux_glibc_x86_64").Rule("rustc") |
| 247 | |
| 248 | if !strings.Contains(rustc.Args["libFlags"], "libbar/linux_glibc_x86_64") { |
| 249 | t.Errorf("Proc_macro is not using host variant of dependent modules.") |
| 250 | } |
| 251 | } |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 252 | |
| 253 | // Test that no_stdlibs suppresses dependencies on rust standard libraries |
| 254 | func TestNoStdlibs(t *testing.T) { |
| 255 | ctx := testRust(t, ` |
| 256 | rust_binary { |
| 257 | name: "fizz-buzz", |
| 258 | srcs: ["foo.rs"], |
Ivan Lozano | 9d1df10 | 2020-04-28 10:10:23 -0400 | [diff] [blame] | 259 | no_stdlibs: true, |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 260 | }`) |
Colin Cross | 7113d20 | 2019-11-20 16:39:12 -0800 | [diff] [blame] | 261 | module := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Module().(*Module) |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 262 | |
| 263 | if android.InList("libstd", module.Properties.AndroidMkDylibs) { |
| 264 | t.Errorf("no_stdlibs did not suppress dependency on libstd") |
| 265 | } |
| 266 | } |
Ivan Lozano | 9d1df10 | 2020-04-28 10:10:23 -0400 | [diff] [blame] | 267 | |
| 268 | // Test that libraries provide both 32-bit and 64-bit variants. |
| 269 | func TestMultilib(t *testing.T) { |
| 270 | ctx := testRust(t, ` |
| 271 | rust_library_rlib { |
| 272 | name: "libfoo", |
| 273 | srcs: ["foo.rs"], |
| 274 | crate_name: "foo", |
| 275 | }`) |
| 276 | |
| 277 | _ = ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib") |
| 278 | _ = ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_rlib") |
| 279 | } |