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