1// Copyright 2020 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	"android/soong/android"
19	"sort"
20	"testing"
21)
22
23func TestSourceProviderCollision(t *testing.T) {
24	testRustError(t, "multiple source providers generate the same filename output: bindings.rs", `
25		rust_binary {
26			name: "source_collider",
27			srcs: [
28				"foo.rs",
29				":libbindings1",
30				":libbindings2",
31			],
32		}
33		rust_bindgen {
34			name: "libbindings1",
35			source_stem: "bindings",
36			crate_name: "bindings1",
37			wrapper_src: "src/any.h",
38		}
39		rust_bindgen {
40			name: "libbindings2",
41			source_stem: "bindings",
42			crate_name: "bindings2",
43			wrapper_src: "src/any.h",
44		}
45	`)
46}
47
48func TestCompilationOutputFiles(t *testing.T) {
49
50	// Note: Rustdoc output is produced for the PrimaryModule, so if the variant
51	// order changes, then it may be produced for a different variant.
52	ctx := testRust(t, `
53		rust_library {
54			name: "libfizz_buzz",
55			crate_name:"fizz_buzz",
56			srcs: ["lib.rs"],
57		}
58		rust_binary {
59			name: "fizz_buzz",
60			crate_name:"fizz_buzz",
61			srcs: ["lib.rs"],
62		}
63		rust_ffi {
64			name: "librust_ffi",
65			crate_name: "rust_ffi",
66			srcs: ["lib.rs"],
67		}
68	`)
69	testcases := []struct {
70		testName      string
71		moduleName    string
72		variant       string
73		expectedFiles []string
74	}{
75		{
76			testName:   "dylib",
77			moduleName: "libfizz_buzz",
78			variant:    "android_arm64_armv8-a_dylib",
79			expectedFiles: []string{
80				"out/soong/.intermediates/libfizz_buzz/android_arm64_armv8-a_dylib/libfizz_buzz.dylib.so",
81				"out/soong/.intermediates/libfizz_buzz/android_arm64_armv8-a_dylib/libfizz_buzz.dylib.so.clippy",
82				"out/soong/.intermediates/libfizz_buzz/android_arm64_armv8-a_dylib/unstripped/libfizz_buzz.dylib.so",
83				"out/soong/target/product/test_device/system/lib64/libfizz_buzz.dylib.so",
84				"out/soong/.intermediates/libfizz_buzz/android_arm64_armv8-a_dylib/meta_lic",
85			},
86		},
87		{
88			testName:   "rlib dylib-std",
89			moduleName: "libfizz_buzz",
90			variant:    "android_arm64_armv8-a_rlib_dylib-std",
91			expectedFiles: []string{
92				"out/soong/.intermediates/libfizz_buzz/android_arm64_armv8-a_rlib_dylib-std/libfizz_buzz.rlib",
93				"out/soong/.intermediates/libfizz_buzz/android_arm64_armv8-a_rlib_dylib-std/libfizz_buzz.rlib.clippy",
94				"out/soong/.intermediates/libfizz_buzz/android_arm64_armv8-a_rlib_dylib-std/meta_lic",
95			},
96		},
97		{
98			testName:   "rlib rlib-std",
99			moduleName: "libfizz_buzz",
100			variant:    "android_arm64_armv8-a_rlib_rlib-std",
101			expectedFiles: []string{
102				"out/soong/.intermediates/libfizz_buzz/android_arm64_armv8-a_rlib_rlib-std/libfizz_buzz.rlib",
103				"out/soong/.intermediates/libfizz_buzz/android_arm64_armv8-a_rlib_rlib-std/libfizz_buzz.rlib.clippy",
104				"out/soong/.intermediates/libfizz_buzz/android_arm64_armv8-a_rlib_rlib-std/meta_lic",
105				"out/soong/.intermediates/libfizz_buzz/android_arm64_armv8-a_rlib_rlib-std/rustdoc.timestamp",
106			},
107		},
108		{
109			testName:   "rust_binary",
110			moduleName: "fizz_buzz",
111			variant:    "android_arm64_armv8-a",
112			expectedFiles: []string{
113				"out/soong/.intermediates/fizz_buzz/android_arm64_armv8-a/fizz_buzz",
114				"out/soong/.intermediates/fizz_buzz/android_arm64_armv8-a/fizz_buzz.clippy",
115				"out/soong/.intermediates/fizz_buzz/android_arm64_armv8-a/unstripped/fizz_buzz",
116				"out/soong/target/product/test_device/system/bin/fizz_buzz",
117				"out/soong/.intermediates/fizz_buzz/android_arm64_armv8-a/meta_lic",
118			},
119		},
120		{
121			testName:   "rust_ffi static",
122			moduleName: "librust_ffi",
123			variant:    "android_arm64_armv8-a_static",
124			expectedFiles: []string{
125				"out/soong/.intermediates/librust_ffi/android_arm64_armv8-a_static/librust_ffi.a",
126				"out/soong/.intermediates/librust_ffi/android_arm64_armv8-a_static/librust_ffi.a.clippy",
127				"out/soong/.intermediates/librust_ffi/android_arm64_armv8-a_static/meta_lic",
128				"out/soong/.intermediates/librust_ffi/android_arm64_armv8-a_static/rustdoc.timestamp",
129			},
130		},
131		{
132			testName:   "rust_ffi rlib",
133			moduleName: "librust_ffi",
134			variant:    "android_arm64_armv8-a_rlib_rlib-std",
135			expectedFiles: []string{
136				"out/soong/.intermediates/librust_ffi/android_arm64_armv8-a_rlib_rlib-std/librust_ffi.rlib",
137				"out/soong/.intermediates/librust_ffi/android_arm64_armv8-a_rlib_rlib-std/librust_ffi.rlib.clippy",
138				"out/soong/.intermediates/librust_ffi/android_arm64_armv8-a_rlib_rlib-std/meta_lic",
139			},
140		},
141		{
142			testName:   "rust_ffi shared",
143			moduleName: "librust_ffi",
144			variant:    "android_arm64_armv8-a_shared",
145			expectedFiles: []string{
146				"out/soong/.intermediates/librust_ffi/android_arm64_armv8-a_shared/librust_ffi.so",
147				"out/soong/.intermediates/librust_ffi/android_arm64_armv8-a_shared/librust_ffi.so.clippy",
148				"out/soong/.intermediates/librust_ffi/android_arm64_armv8-a_shared/unstripped/librust_ffi.so",
149				"out/soong/.intermediates/librust_ffi/android_arm64_armv8-a_shared/unstripped/librust_ffi.so.toc",
150				"out/soong/.intermediates/librust_ffi/android_arm64_armv8-a_shared/meta_lic",
151				"out/soong/target/product/test_device/system/lib64/librust_ffi.so",
152			},
153		},
154	}
155	for _, tc := range testcases {
156		t.Run(tc.testName, func(t *testing.T) {
157			modOutputs := ctx.ModuleForTests(tc.moduleName, tc.variant).AllOutputs()
158			sort.Strings(tc.expectedFiles)
159			sort.Strings(modOutputs)
160			android.AssertStringPathsRelativeToTopEquals(
161				t,
162				"incorrect outputs from rust module",
163				ctx.Config(),
164				tc.expectedFiles,
165				modOutputs,
166			)
167		})
168	}
169}
170