blob: f7b668122f6be9639a969ce3495f24addb949331 [file] [log] [blame]
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +02001// Copyright 2020 Google Inc. All rights reserved.
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 (
Thiébaud Weksteen83ee52f2020-08-05 09:29:23 +020018 "encoding/json"
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020019 "io/ioutil"
20 "path/filepath"
Thiébaud Weksteene8b0ee72021-03-25 09:26:07 +010021 "sort"
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +020022 "strings"
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020023 "testing"
24
25 "android/soong/android"
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020026)
27
Thiébaud Weksteen83ee52f2020-08-05 09:29:23 +020028// testProjectJson run the generation of rust-project.json. It returns the raw
29// content of the generated file.
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +020030func testProjectJson(t *testing.T, bp string) []byte {
Paul Duffin79abe572021-03-29 02:16:14 +010031 result := android.GroupFixturePreparers(
32 prepareForRustTest,
33 android.FixtureMergeEnv(map[string]string{"SOONG_GEN_RUST_PROJECT": "1"}),
34 ).RunTestWithBp(t, bp)
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020035
36 // The JSON file is generated via WriteFileToOutputDir. Therefore, it
37 // won't appear in the Output of the TestingSingleton. Manually verify
38 // it exists.
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +020039 content, err := ioutil.ReadFile(filepath.Join(result.Config.SoongOutDir(), rustProjectJsonFileName))
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020040 if err != nil {
41 t.Errorf("rust-project.json has not been generated")
42 }
Thiébaud Weksteen83ee52f2020-08-05 09:29:23 +020043 return content
44}
45
46// validateJsonCrates validates that content follows the basic structure of
47// rust-project.json. It returns the crates attribute if the validation
48// succeeded.
49// It uses an empty interface instead of relying on a defined structure to
50// avoid a strong dependency on our implementation.
51func validateJsonCrates(t *testing.T, rawContent []byte) []interface{} {
52 var content interface{}
53 err := json.Unmarshal(rawContent, &content)
54 if err != nil {
55 t.Errorf("Unable to parse the rust-project.json as JSON: %v", err)
56 }
57 root, ok := content.(map[string]interface{})
58 if !ok {
59 t.Errorf("Unexpected JSON format: %v", content)
60 }
61 if _, ok = root["crates"]; !ok {
62 t.Errorf("No crates attribute in rust-project.json: %v", root)
63 }
64 crates, ok := root["crates"].([]interface{})
65 if !ok {
66 t.Errorf("Unexpected crates format: %v", root["crates"])
67 }
68 return crates
69}
70
Thiébaud Weksteen064f6e92020-12-03 20:58:32 +010071// validateCrate ensures that a crate can be parsed as a map.
72func validateCrate(t *testing.T, crate interface{}) map[string]interface{} {
73 c, ok := crate.(map[string]interface{})
74 if !ok {
75 t.Fatalf("Unexpected type for crate: %v", c)
76 }
77 return c
78}
79
80// validateDependencies parses the dependencies for a crate. It returns a list
81// of the dependencies name.
82func validateDependencies(t *testing.T, crate map[string]interface{}) []string {
83 var dependencies []string
84 deps, ok := crate["deps"].([]interface{})
85 if !ok {
86 t.Errorf("Unexpected format for deps: %v", crate["deps"])
87 }
88 for _, dep := range deps {
89 d, ok := dep.(map[string]interface{})
90 if !ok {
91 t.Errorf("Unexpected format for dependency: %v", dep)
92 }
93 name, ok := d["name"].(string)
94 if !ok {
95 t.Errorf("Dependency is missing the name key: %v", d)
96 }
97 dependencies = append(dependencies, name)
98 }
99 return dependencies
100}
101
Thiébaud Weksteen83ee52f2020-08-05 09:29:23 +0200102func TestProjectJsonDep(t *testing.T) {
103 bp := `
104 rust_library {
105 name: "liba",
106 srcs: ["a/src/lib.rs"],
107 crate_name: "a"
108 }
109 rust_library {
110 name: "libb",
111 srcs: ["b/src/lib.rs"],
112 crate_name: "b",
113 rlibs: ["liba"],
114 }
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200115 `
116 jsonContent := testProjectJson(t, bp)
Thiébaud Weksteen83ee52f2020-08-05 09:29:23 +0200117 validateJsonCrates(t, jsonContent)
118}
119
Thiébaud Weksteene8b0ee72021-03-25 09:26:07 +0100120func TestProjectJsonFeature(t *testing.T) {
121 bp := `
122 rust_library {
123 name: "liba",
124 srcs: ["a/src/lib.rs"],
125 crate_name: "a",
126 features: ["f1", "f2"]
127 }
128 `
129 jsonContent := testProjectJson(t, bp)
130 crates := validateJsonCrates(t, jsonContent)
131 for _, c := range crates {
132 crate := validateCrate(t, c)
133 cfgs, ok := crate["cfg"].([]interface{})
134 if !ok {
135 t.Fatalf("Unexpected type for cfgs: %v", crate)
136 }
137 expectedCfgs := []string{"feature=\"f1\"", "feature=\"f2\""}
138 foundCfgs := []string{}
139 for _, cfg := range cfgs {
140 cfg, ok := cfg.(string)
141 if !ok {
142 t.Fatalf("Unexpected type for cfg: %v", cfg)
143 }
144 foundCfgs = append(foundCfgs, cfg)
145 }
146 sort.Strings(foundCfgs)
147 for i, foundCfg := range foundCfgs {
148 if foundCfg != expectedCfgs[i] {
149 t.Errorf("Incorrect features: got %v; want %v", foundCfg, expectedCfgs[i])
150 }
151 }
152 }
153}
154
Thiébaud Weksteen064f6e92020-12-03 20:58:32 +0100155func TestProjectJsonBinary(t *testing.T) {
156 bp := `
157 rust_binary {
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100158 name: "libz",
159 srcs: ["z/src/lib.rs"],
160 crate_name: "z"
Thiébaud Weksteen064f6e92020-12-03 20:58:32 +0100161 }
162 `
163 jsonContent := testProjectJson(t, bp)
164 crates := validateJsonCrates(t, jsonContent)
165 for _, c := range crates {
166 crate := validateCrate(t, c)
167 rootModule, ok := crate["root_module"].(string)
168 if !ok {
169 t.Fatalf("Unexpected type for root_module: %v", crate["root_module"])
170 }
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100171 if rootModule == "z/src/lib.rs" {
Thiébaud Weksteen064f6e92020-12-03 20:58:32 +0100172 return
173 }
174 }
175 t.Errorf("Entry for binary %q not found: %s", "a", jsonContent)
176}
177
Thiébaud Weksteen83ee52f2020-08-05 09:29:23 +0200178func TestProjectJsonBindGen(t *testing.T) {
Colin Cross0c66bc62021-07-20 09:47:41 -0700179 buildOS := android.TestConfig(t.TempDir(), nil, "", nil).BuildOS
180
Thiébaud Weksteen83ee52f2020-08-05 09:29:23 +0200181 bp := `
182 rust_library {
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100183 name: "libd",
184 srcs: ["d/src/lib.rs"],
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +0200185 rlibs: ["libbindings1"],
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100186 crate_name: "d"
Thiébaud Weksteen83ee52f2020-08-05 09:29:23 +0200187 }
188 rust_bindgen {
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +0200189 name: "libbindings1",
190 crate_name: "bindings1",
191 source_stem: "bindings1",
Thiébaud Weksteen83ee52f2020-08-05 09:29:23 +0200192 host_supported: true,
193 wrapper_src: "src/any.h",
194 }
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +0200195 rust_library_host {
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100196 name: "libe",
197 srcs: ["e/src/lib.rs"],
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +0200198 rustlibs: ["libbindings2"],
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100199 crate_name: "e"
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +0200200 }
201 rust_bindgen_host {
202 name: "libbindings2",
203 crate_name: "bindings2",
204 source_stem: "bindings2",
205 wrapper_src: "src/any.h",
206 }
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200207 `
208 jsonContent := testProjectJson(t, bp)
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +0200209 crates := validateJsonCrates(t, jsonContent)
210 for _, c := range crates {
Thiébaud Weksteen064f6e92020-12-03 20:58:32 +0100211 crate := validateCrate(t, c)
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +0200212 rootModule, ok := crate["root_module"].(string)
213 if !ok {
214 t.Fatalf("Unexpected type for root_module: %v", crate["root_module"])
215 }
216 if strings.Contains(rootModule, "libbindings1") && !strings.Contains(rootModule, "android_arm64") {
Thiébaud Weksteen4f6c7662020-09-30 13:33:41 +0200217 t.Errorf("The source path for libbindings1 does not contain android_arm64, got %v", rootModule)
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +0200218 }
Colin Cross0c66bc62021-07-20 09:47:41 -0700219 if strings.Contains(rootModule, "libbindings2") && !strings.Contains(rootModule, buildOS.String()) {
Thiébaud Weksteen4f6c7662020-09-30 13:33:41 +0200220 t.Errorf("The source path for libbindings2 does not contain the BuildOs, got %v; want %v",
Colin Cross0c66bc62021-07-20 09:47:41 -0700221 rootModule, buildOS.String())
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +0200222 }
Thiébaud Weksteen3c5905b2020-11-25 16:09:32 +0100223 // Check that libbindings1 does not depend on itself.
224 if strings.Contains(rootModule, "libbindings1") {
Thiébaud Weksteen064f6e92020-12-03 20:58:32 +0100225 for _, depName := range validateDependencies(t, crate) {
226 if depName == "bindings1" {
Thiébaud Weksteen3c5905b2020-11-25 16:09:32 +0100227 t.Errorf("libbindings1 depends on itself")
228 }
229 }
230 }
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100231 if strings.Contains(rootModule, "d/src/lib.rs") {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100232 // Check that libd depends on libbindings1
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100233 found := false
234 for _, depName := range validateDependencies(t, crate) {
235 if depName == "bindings1" {
236 found = true
237 break
238 }
239 }
240 if !found {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100241 t.Errorf("libd does not depend on libbindings1: %v", crate)
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100242 }
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100243 // Check that OUT_DIR is populated.
244 env, ok := crate["env"].(map[string]interface{})
245 if !ok {
246 t.Errorf("libd does not have its environment variables set: %v", crate)
247 }
248 if _, ok = env["OUT_DIR"]; !ok {
249 t.Errorf("libd does not have its OUT_DIR set: %v", env)
250 }
251
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100252 }
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +0200253 }
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200254}
Thiébaud Weksteen2f628ba2020-08-05 14:27:32 +0200255
256func TestProjectJsonMultiVersion(t *testing.T) {
257 bp := `
258 rust_library {
259 name: "liba1",
260 srcs: ["a1/src/lib.rs"],
261 crate_name: "a"
262 }
263 rust_library {
264 name: "liba2",
265 srcs: ["a2/src/lib.rs"],
266 crate_name: "a",
267 }
268 rust_library {
269 name: "libb",
270 srcs: ["b/src/lib.rs"],
271 crate_name: "b",
272 rustlibs: ["liba1", "liba2"],
273 }
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200274 `
275 jsonContent := testProjectJson(t, bp)
Thiébaud Weksteen2f628ba2020-08-05 14:27:32 +0200276 crates := validateJsonCrates(t, jsonContent)
Thiébaud Weksteen064f6e92020-12-03 20:58:32 +0100277 for _, c := range crates {
278 crate := validateCrate(t, c)
279 rootModule, ok := crate["root_module"].(string)
280 if !ok {
281 t.Fatalf("Unexpected type for root_module: %v", crate["root_module"])
282 }
283 // Make sure that b has 2 different dependencies.
284 if rootModule == "b/src/lib.rs" {
Thiébaud Weksteen2f628ba2020-08-05 14:27:32 +0200285 aCount := 0
Thiébaud Weksteen064f6e92020-12-03 20:58:32 +0100286 deps := validateDependencies(t, crate)
287 for _, depName := range deps {
288 if depName == "a" {
Thiébaud Weksteen2f628ba2020-08-05 14:27:32 +0200289 aCount++
290 }
291 }
292 if aCount != 2 {
293 t.Errorf("Unexpected number of liba dependencies want %v, got %v: %v", 2, aCount, deps)
294 }
295 return
296 }
297 }
298 t.Errorf("libb crate has not been found: %v", crates)
299}