blob: 523b464c0f5e179351f37f7f4bcfd98dba68a55e [file] [log] [blame]
Yu Liueae7b362023-11-16 17:05:47 -08001package codegen
Vinh Tran457ddef2023-08-02 13:50:26 -04002
3import (
Vinh Tran457ddef2023-08-02 13:50:26 -04004 "fmt"
5 "testing"
Zi Wang275f6542023-11-09 14:59:31 -08006
7 "android/soong/android"
8 "android/soong/rust"
Vinh Tran457ddef2023-08-02 13:50:26 -04009)
10
11func TestRustAconfigLibrary(t *testing.T) {
12 result := android.GroupFixturePreparers(
13 PrepareForTestWithAconfigBuildComponents,
Kiyoung Kim1db4a742024-04-01 15:50:01 +090014 rust.PrepareForIntegrationTestWithRust,
Vinh Tran457ddef2023-08-02 13:50:26 -040015 android.PrepareForTestWithArchMutator,
16 android.PrepareForTestWithDefaults,
17 android.PrepareForTestWithPrebuilts,
18 ).
19 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
20 RunTestWithBp(t, fmt.Sprintf(`
21 rust_library {
22 name: "libflags_rust", // test mock
23 crate_name: "flags_rust",
24 srcs: ["lib.rs"],
25 }
Dennis Shenba6ed2d2023-11-08 21:02:53 +000026 rust_library {
27 name: "liblazy_static", // test mock
28 crate_name: "lazy_static",
29 srcs: ["src/lib.rs"],
30 }
Ted Bauer02d475c2024-03-27 20:56:26 +000031 rust_library {
32 name: "libaconfig_storage_read_api", // test mock
33 crate_name: "aconfig_storage_read_api",
34 srcs: ["lib.rs"],
35 }
Ted Bauer6ef40db2024-03-29 14:04:10 +000036 rust_library {
37 name: "liblogger", // test mock
38 crate_name: "logger",
39 srcs: ["lib.rs"],
40 }
41 rust_library {
42 name: "liblog_rust", // test mock
43 crate_name: "log_rust",
44 srcs: ["lib.rs"],
45 }
Vinh Tran457ddef2023-08-02 13:50:26 -040046 aconfig_declarations {
47 name: "my_aconfig_declarations",
48 package: "com.example.package",
Yu Liu315a53c2024-04-24 16:41:57 +000049 container: "com.android.foo",
Vinh Tran457ddef2023-08-02 13:50:26 -040050 srcs: ["foo.aconfig"],
51 }
52
53 rust_aconfig_library {
54 name: "libmy_rust_aconfig_library",
55 crate_name: "my_rust_aconfig_library",
56 aconfig_declarations: "my_aconfig_declarations",
57 }
58 `))
59
60 sourceVariant := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_source")
61 rule := sourceVariant.Rule("rust_aconfig_library")
62 android.AssertStringEquals(t, "rule must contain production mode", rule.Args["mode"], "production")
63
64 dylibVariant := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_dylib")
65 rlibRlibStdVariant := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_rlib_rlib-std")
66 rlibDylibStdVariant := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_rlib_dylib-std")
67
68 variants := []android.TestingModule{
69 dylibVariant,
70 rlibDylibStdVariant,
71 rlibRlibStdVariant,
72 }
73
74 for _, variant := range variants {
Colin Crossdf0ed702023-09-22 21:59:59 +000075 android.AssertStringEquals(
Vinh Tran457ddef2023-08-02 13:50:26 -040076 t,
77 "dylib variant builds from generated rust code",
78 "out/soong/.intermediates/libmy_rust_aconfig_library/android_arm64_armv8-a_source/gen/src/lib.rs",
Colin Crossdf0ed702023-09-22 21:59:59 +000079 variant.Rule("rustc").Inputs[0].RelativeToTop().String(),
Vinh Tran457ddef2023-08-02 13:50:26 -040080 )
81 }
82}
Zi Wang275f6542023-11-09 14:59:31 -080083
84var rustCodegenModeTestData = []struct {
85 setting, expected string
86}{
87 {"", "production"},
88 {"mode: `production`,", "production"},
89 {"mode: `test`,", "test"},
90 {"mode: `exported`,", "exported"},
Zhi Dou70e21242023-12-20 23:14:34 +000091 {"mode: `force-read-only`,", "force-read-only"},
Zi Wang275f6542023-11-09 14:59:31 -080092}
93
94func TestRustCodegenMode(t *testing.T) {
95 for _, testData := range rustCodegenModeTestData {
96 testRustCodegenModeHelper(t, testData.setting, testData.expected)
97 }
98}
99
100func testRustCodegenModeHelper(t *testing.T, bpMode string, ruleMode string) {
101 t.Helper()
102 result := android.GroupFixturePreparers(
103 PrepareForTestWithAconfigBuildComponents,
Kiyoung Kim1db4a742024-04-01 15:50:01 +0900104 rust.PrepareForIntegrationTestWithRust).
Zi Wang275f6542023-11-09 14:59:31 -0800105 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
106 RunTestWithBp(t, fmt.Sprintf(`
107 rust_library {
108 name: "libflags_rust", // test mock
109 crate_name: "flags_rust",
110 srcs: ["lib.rs"],
111 }
112 rust_library {
113 name: "liblazy_static", // test mock
114 crate_name: "lazy_static",
115 srcs: ["src/lib.rs"],
116 }
Ted Bauer02d475c2024-03-27 20:56:26 +0000117 rust_library {
118 name: "libaconfig_storage_read_api", // test mock
119 crate_name: "aconfig_storage_read_api",
120 srcs: ["lib.rs"],
121 }
Ted Bauer6ef40db2024-03-29 14:04:10 +0000122 rust_library {
123 name: "liblogger", // test mock
124 crate_name: "logger",
125 srcs: ["lib.rs"],
126 }
127 rust_library {
128 name: "liblog_rust", // test mock
129 crate_name: "log_rust",
130 srcs: ["lib.rs"],
131 }
Zi Wang275f6542023-11-09 14:59:31 -0800132 aconfig_declarations {
133 name: "my_aconfig_declarations",
134 package: "com.example.package",
Yu Liu315a53c2024-04-24 16:41:57 +0000135 container: "com.android.foo",
Zi Wang275f6542023-11-09 14:59:31 -0800136 srcs: ["foo.aconfig"],
137 }
138 rust_aconfig_library {
139 name: "libmy_rust_aconfig_library",
140 crate_name: "my_rust_aconfig_library",
141 aconfig_declarations: "my_aconfig_declarations",
142 %s
143 }
144 `, bpMode))
145
146 module := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_source")
147 rule := module.Rule("rust_aconfig_library")
148 android.AssertStringEquals(t, "rule must contain test mode", rule.Args["mode"], ruleMode)
149}
150
151var incorrectRustCodegenModeTestData = []struct {
152 setting, expectedErr string
153}{
154 {"mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode"},
Zi Wang275f6542023-11-09 14:59:31 -0800155}
156
157func TestIncorrectRustCodegenMode(t *testing.T) {
158 for _, testData := range incorrectRustCodegenModeTestData {
159 testIncorrectRustCodegenModeHelper(t, testData.setting, testData.expectedErr)
160 }
161}
162
163func testIncorrectRustCodegenModeHelper(t *testing.T, bpMode string, err string) {
164 t.Helper()
165 android.GroupFixturePreparers(
166 PrepareForTestWithAconfigBuildComponents,
Kiyoung Kim1db4a742024-04-01 15:50:01 +0900167 rust.PrepareForIntegrationTestWithRust).
Zi Wang275f6542023-11-09 14:59:31 -0800168 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(err)).
169 RunTestWithBp(t, fmt.Sprintf(`
170 rust_library {
171 name: "libflags_rust", // test mock
172 crate_name: "flags_rust",
173 srcs: ["lib.rs"],
174 }
175 rust_library {
176 name: "liblazy_static", // test mock
177 crate_name: "lazy_static",
178 srcs: ["src/lib.rs"],
179 }
Ted Bauer02d475c2024-03-27 20:56:26 +0000180 rust_library {
181 name: "libaconfig_storage_read_api", // test mock
182 crate_name: "aconfig_storage_read_api",
183 srcs: ["lib.rs"],
184 }
Ted Bauer6ef40db2024-03-29 14:04:10 +0000185 rust_library {
186 name: "liblogger", // test mock
187 crate_name: "logger",
188 srcs: ["lib.rs"],
189 }
190 rust_library {
191 name: "liblog_rust", // test mock
192 crate_name: "log_rust",
193 srcs: ["lib.rs"],
194 }
Zi Wang275f6542023-11-09 14:59:31 -0800195 aconfig_declarations {
196 name: "my_aconfig_declarations",
197 package: "com.example.package",
Yu Liu315a53c2024-04-24 16:41:57 +0000198 container: "com.android.foo",
Zi Wang275f6542023-11-09 14:59:31 -0800199 srcs: ["foo.aconfig"],
200 }
201 rust_aconfig_library {
202 name: "libmy_rust_aconfig_library",
203 crate_name: "my_rust_aconfig_library",
204 aconfig_declarations: "my_aconfig_declarations",
205 %s
206 }
207 `, bpMode))
208}