blob: eb04e7257963f1b85db1244e01c7584782e609d5 [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 "io/ioutil"
19 "os"
Ivan Lozanoc0083612019-09-03 13:49:39 -070020 "runtime"
Ivan Lozanob9040d62019-09-24 13:23:50 -070021 "strings"
Ivan Lozanoffee3342019-08-27 12:03:00 -070022 "testing"
23
24 "android/soong/android"
25)
26
27var (
28 buildDir string
29)
30
31func setUp() {
32 var err error
33 buildDir, err = ioutil.TempDir("", "soong_rust_test")
34 if err != nil {
35 panic(err)
36 }
37}
38
39func tearDown() {
40 os.RemoveAll(buildDir)
41}
42
43func TestMain(m *testing.M) {
44 run := func() int {
45 setUp()
46 defer tearDown()
47
48 return m.Run()
49 }
50
51 os.Exit(run())
52}
53
54func testRust(t *testing.T, bp string) *android.TestContext {
Ivan Lozanoc0083612019-09-03 13:49:39 -070055 // TODO (b/140435149)
56 if runtime.GOOS != "linux" {
57 t.Skip("Only the Linux toolchain is supported for Rust")
58 }
59
Ivan Lozanoffee3342019-08-27 12:03:00 -070060 t.Helper()
61 config := android.TestArchConfig(buildDir, nil)
62
63 t.Helper()
64 ctx := CreateTestContext(bp)
65 ctx.Register()
66
67 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
68 android.FailIfErrored(t, errs)
69 _, errs = ctx.PrepareBuildActions(config)
70 android.FailIfErrored(t, errs)
71
72 return ctx
73}
74
75func testRustError(t *testing.T, pattern string, bp string) {
Ivan Lozanoc0083612019-09-03 13:49:39 -070076 // TODO (b/140435149)
77 if runtime.GOOS != "linux" {
78 t.Skip("Only the Linux toolchain is supported for Rust")
79 }
80
Ivan Lozanoffee3342019-08-27 12:03:00 -070081 t.Helper()
82 config := android.TestArchConfig(buildDir, nil)
83
84 ctx := CreateTestContext(bp)
85 ctx.Register()
86
87 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
88 if len(errs) > 0 {
89 android.FailIfNoMatchingErrors(t, pattern, errs)
90 return
91 }
92
93 _, errs = ctx.PrepareBuildActions(config)
94 if len(errs) > 0 {
95 android.FailIfNoMatchingErrors(t, pattern, errs)
96 return
97 }
98
99 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
100}
101
102// Test that we can extract the lib name from a lib path.
103func TestLibNameFromFilePath(t *testing.T) {
Ivan Lozano52767be2019-10-18 14:49:46 -0700104 libBarPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/libbar.so")
105 libLibPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/liblib.dylib.so")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700106
Ivan Lozano52767be2019-10-18 14:49:46 -0700107 libBarName := libNameFromFilePath(libBarPath)
108 libLibName := libNameFromFilePath(libLibPath)
109
110 expectedResult := "bar"
111 if libBarName != expectedResult {
112 t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libBarName)
113 }
114
115 expectedResult = "lib.dylib"
116 if libLibName != expectedResult {
117 t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libLibPath)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700118 }
119}
120
121// Test that we can extract the link path from a lib path.
122func TestLinkPathFromFilePath(t *testing.T) {
123 barPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/libbar.so")
124 libName := linkPathFromFilePath(barPath)
125 expectedResult := "out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/"
126
127 if libName != expectedResult {
128 t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libName)
129 }
130}
131
132// Test default crate names from module names are generated correctly.
133func TestDefaultCrateName(t *testing.T) {
134 ctx := testRust(t, `
135 rust_library_host_dylib {
136 name: "fizz-buzz",
137 srcs: ["foo.rs"],
138 }`)
139 module := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64_dylib").Module().(*Module)
140 crateName := module.CrateName()
141 expectedResult := "fizz_buzz"
142
143 if crateName != expectedResult {
144 t.Errorf("CrateName() returned the wrong default crate name; expected '%#v', got '%#v'", expectedResult, crateName)
145 }
146}
147
148// Test to make sure dependencies are being picked up correctly.
149func TestDepsTracking(t *testing.T) {
150 ctx := testRust(t, `
Ivan Lozano52767be2019-10-18 14:49:46 -0700151 rust_library_host_static {
152 name: "libstatic",
153 srcs: ["foo.rs"],
154 }
155 rust_library_host_shared {
156 name: "libshared",
157 srcs: ["foo.rs"],
158 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700159 rust_library_host_dylib {
Ivan Lozano52767be2019-10-18 14:49:46 -0700160 name: "libdylib",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700161 srcs: ["foo.rs"],
162 }
163 rust_library_host_rlib {
Ivan Lozano52767be2019-10-18 14:49:46 -0700164 name: "librlib",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700165 srcs: ["foo.rs"],
166 }
167 rust_proc_macro {
168 name: "libpm",
169 srcs: ["foo.rs"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700170 }
171 rust_binary_host {
172 name: "fizz-buzz",
Ivan Lozano52767be2019-10-18 14:49:46 -0700173 dylibs: ["libdylib"],
174 rlibs: ["librlib"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700175 proc_macros: ["libpm"],
Ivan Lozano52767be2019-10-18 14:49:46 -0700176 static_libs: ["libstatic"],
177 shared_libs: ["libshared"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700178 srcs: ["foo.rs"],
179 }
180 `)
181 module := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module)
182
183 // Since dependencies are added to AndroidMk* properties, we can check these to see if they've been picked up.
Ivan Lozano52767be2019-10-18 14:49:46 -0700184 if !android.InList("libdylib", module.Properties.AndroidMkDylibs) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700185 t.Errorf("Dylib dependency not detected (dependency missing from AndroidMkDylibs)")
186 }
187
Ivan Lozano52767be2019-10-18 14:49:46 -0700188 if !android.InList("librlib", module.Properties.AndroidMkRlibs) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700189 t.Errorf("Rlib dependency not detected (dependency missing from AndroidMkRlibs)")
190 }
191
192 if !android.InList("libpm", module.Properties.AndroidMkProcMacroLibs) {
193 t.Errorf("Proc_macro dependency not detected (dependency missing from AndroidMkProcMacroLibs)")
194 }
195
Ivan Lozano52767be2019-10-18 14:49:46 -0700196 if !android.InList("libshared", module.Properties.AndroidMkSharedLibs) {
197 t.Errorf("Shared library dependency not detected (dependency missing from AndroidMkSharedLibs)")
198 }
199
200 if !android.InList("libstatic", module.Properties.AndroidMkStaticLibs) {
201 t.Errorf("Static library dependency not detected (dependency missing from AndroidMkStaticLibs)")
202 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700203}
Ivan Lozanob9040d62019-09-24 13:23:50 -0700204
205// Test to make sure proc_macros use host variants when building device modules.
206func TestProcMacroDeviceDeps(t *testing.T) {
207 ctx := testRust(t, `
208 rust_library_host_rlib {
209 name: "libbar",
210 srcs: ["foo.rs"],
211 }
212 rust_proc_macro {
213 name: "libpm",
214 rlibs: ["libbar"],
215 srcs: ["foo.rs"],
216 }
217 rust_binary {
218 name: "fizz-buzz",
219 proc_macros: ["libpm"],
220 srcs: ["foo.rs"],
221 }
222 `)
223 rustc := ctx.ModuleForTests("libpm", "linux_glibc_x86_64").Rule("rustc")
224
225 if !strings.Contains(rustc.Args["libFlags"], "libbar/linux_glibc_x86_64") {
226 t.Errorf("Proc_macro is not using host variant of dependent modules.")
227 }
228}