Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 1 | // 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
Thiébaud Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 18 | "encoding/json" |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 19 | "io/ioutil" |
| 20 | "path/filepath" |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 21 | "strings" |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 22 | "testing" |
| 23 | |
| 24 | "android/soong/android" |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 25 | ) |
| 26 | |
Thiébaud Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 27 | // testProjectJson run the generation of rust-project.json. It returns the raw |
| 28 | // content of the generated file. |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 29 | func testProjectJson(t *testing.T, bp string) []byte { |
| 30 | tctx := newTestRustCtx(t, bp) |
| 31 | tctx.env = map[string]string{"SOONG_GEN_RUST_PROJECT": "1"} |
| 32 | tctx.generateConfig() |
| 33 | tctx.parse(t) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 34 | |
| 35 | // The JSON file is generated via WriteFileToOutputDir. Therefore, it |
| 36 | // won't appear in the Output of the TestingSingleton. Manually verify |
| 37 | // it exists. |
Thiébaud Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 38 | content, err := ioutil.ReadFile(filepath.Join(buildDir, rustProjectJsonFileName)) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 39 | if err != nil { |
| 40 | t.Errorf("rust-project.json has not been generated") |
| 41 | } |
Thiébaud Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 42 | return content |
| 43 | } |
| 44 | |
| 45 | // validateJsonCrates validates that content follows the basic structure of |
| 46 | // rust-project.json. It returns the crates attribute if the validation |
| 47 | // succeeded. |
| 48 | // It uses an empty interface instead of relying on a defined structure to |
| 49 | // avoid a strong dependency on our implementation. |
| 50 | func validateJsonCrates(t *testing.T, rawContent []byte) []interface{} { |
| 51 | var content interface{} |
| 52 | err := json.Unmarshal(rawContent, &content) |
| 53 | if err != nil { |
| 54 | t.Errorf("Unable to parse the rust-project.json as JSON: %v", err) |
| 55 | } |
| 56 | root, ok := content.(map[string]interface{}) |
| 57 | if !ok { |
| 58 | t.Errorf("Unexpected JSON format: %v", content) |
| 59 | } |
| 60 | if _, ok = root["crates"]; !ok { |
| 61 | t.Errorf("No crates attribute in rust-project.json: %v", root) |
| 62 | } |
| 63 | crates, ok := root["crates"].([]interface{}) |
| 64 | if !ok { |
| 65 | t.Errorf("Unexpected crates format: %v", root["crates"]) |
| 66 | } |
| 67 | return crates |
| 68 | } |
| 69 | |
Thiébaud Weksteen | 064f6e9 | 2020-12-03 20:58:32 +0100 | [diff] [blame] | 70 | // validateCrate ensures that a crate can be parsed as a map. |
| 71 | func validateCrate(t *testing.T, crate interface{}) map[string]interface{} { |
| 72 | c, ok := crate.(map[string]interface{}) |
| 73 | if !ok { |
| 74 | t.Fatalf("Unexpected type for crate: %v", c) |
| 75 | } |
| 76 | return c |
| 77 | } |
| 78 | |
| 79 | // validateDependencies parses the dependencies for a crate. It returns a list |
| 80 | // of the dependencies name. |
| 81 | func validateDependencies(t *testing.T, crate map[string]interface{}) []string { |
| 82 | var dependencies []string |
| 83 | deps, ok := crate["deps"].([]interface{}) |
| 84 | if !ok { |
| 85 | t.Errorf("Unexpected format for deps: %v", crate["deps"]) |
| 86 | } |
| 87 | for _, dep := range deps { |
| 88 | d, ok := dep.(map[string]interface{}) |
| 89 | if !ok { |
| 90 | t.Errorf("Unexpected format for dependency: %v", dep) |
| 91 | } |
| 92 | name, ok := d["name"].(string) |
| 93 | if !ok { |
| 94 | t.Errorf("Dependency is missing the name key: %v", d) |
| 95 | } |
| 96 | dependencies = append(dependencies, name) |
| 97 | } |
| 98 | return dependencies |
| 99 | } |
| 100 | |
Thiébaud Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 101 | func TestProjectJsonDep(t *testing.T) { |
| 102 | bp := ` |
| 103 | rust_library { |
| 104 | name: "liba", |
| 105 | srcs: ["a/src/lib.rs"], |
| 106 | crate_name: "a" |
| 107 | } |
| 108 | rust_library { |
| 109 | name: "libb", |
| 110 | srcs: ["b/src/lib.rs"], |
| 111 | crate_name: "b", |
| 112 | rlibs: ["liba"], |
| 113 | } |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 114 | ` |
| 115 | jsonContent := testProjectJson(t, bp) |
Thiébaud Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 116 | validateJsonCrates(t, jsonContent) |
| 117 | } |
| 118 | |
Thiébaud Weksteen | 064f6e9 | 2020-12-03 20:58:32 +0100 | [diff] [blame] | 119 | func TestProjectJsonBinary(t *testing.T) { |
| 120 | bp := ` |
| 121 | rust_binary { |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 122 | name: "libz", |
| 123 | srcs: ["z/src/lib.rs"], |
| 124 | crate_name: "z" |
Thiébaud Weksteen | 064f6e9 | 2020-12-03 20:58:32 +0100 | [diff] [blame] | 125 | } |
| 126 | ` |
| 127 | jsonContent := testProjectJson(t, bp) |
| 128 | crates := validateJsonCrates(t, jsonContent) |
| 129 | for _, c := range crates { |
| 130 | crate := validateCrate(t, c) |
| 131 | rootModule, ok := crate["root_module"].(string) |
| 132 | if !ok { |
| 133 | t.Fatalf("Unexpected type for root_module: %v", crate["root_module"]) |
| 134 | } |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 135 | if rootModule == "z/src/lib.rs" { |
Thiébaud Weksteen | 064f6e9 | 2020-12-03 20:58:32 +0100 | [diff] [blame] | 136 | return |
| 137 | } |
| 138 | } |
| 139 | t.Errorf("Entry for binary %q not found: %s", "a", jsonContent) |
| 140 | } |
| 141 | |
Thiébaud Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 142 | func TestProjectJsonBindGen(t *testing.T) { |
| 143 | bp := ` |
| 144 | rust_library { |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 145 | name: "libd", |
| 146 | srcs: ["d/src/lib.rs"], |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 147 | rlibs: ["libbindings1"], |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 148 | crate_name: "d" |
Thiébaud Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 149 | } |
| 150 | rust_bindgen { |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 151 | name: "libbindings1", |
| 152 | crate_name: "bindings1", |
| 153 | source_stem: "bindings1", |
Thiébaud Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 154 | host_supported: true, |
| 155 | wrapper_src: "src/any.h", |
| 156 | } |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 157 | rust_library_host { |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 158 | name: "libe", |
| 159 | srcs: ["e/src/lib.rs"], |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 160 | rustlibs: ["libbindings2"], |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 161 | crate_name: "e" |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 162 | } |
| 163 | rust_bindgen_host { |
| 164 | name: "libbindings2", |
| 165 | crate_name: "bindings2", |
| 166 | source_stem: "bindings2", |
| 167 | wrapper_src: "src/any.h", |
| 168 | } |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 169 | ` |
| 170 | jsonContent := testProjectJson(t, bp) |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 171 | crates := validateJsonCrates(t, jsonContent) |
| 172 | for _, c := range crates { |
Thiébaud Weksteen | 064f6e9 | 2020-12-03 20:58:32 +0100 | [diff] [blame] | 173 | crate := validateCrate(t, c) |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 174 | rootModule, ok := crate["root_module"].(string) |
| 175 | if !ok { |
| 176 | t.Fatalf("Unexpected type for root_module: %v", crate["root_module"]) |
| 177 | } |
| 178 | if strings.Contains(rootModule, "libbindings1") && !strings.Contains(rootModule, "android_arm64") { |
Thiébaud Weksteen | 4f6c766 | 2020-09-30 13:33:41 +0200 | [diff] [blame] | 179 | t.Errorf("The source path for libbindings1 does not contain android_arm64, got %v", rootModule) |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 180 | } |
Thiébaud Weksteen | 4f6c766 | 2020-09-30 13:33:41 +0200 | [diff] [blame] | 181 | if strings.Contains(rootModule, "libbindings2") && !strings.Contains(rootModule, android.BuildOs.String()) { |
| 182 | t.Errorf("The source path for libbindings2 does not contain the BuildOs, got %v; want %v", |
| 183 | rootModule, android.BuildOs.String()) |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 184 | } |
Thiébaud Weksteen | 3c5905b | 2020-11-25 16:09:32 +0100 | [diff] [blame] | 185 | // Check that libbindings1 does not depend on itself. |
| 186 | if strings.Contains(rootModule, "libbindings1") { |
Thiébaud Weksteen | 064f6e9 | 2020-12-03 20:58:32 +0100 | [diff] [blame] | 187 | for _, depName := range validateDependencies(t, crate) { |
| 188 | if depName == "bindings1" { |
Thiébaud Weksteen | 3c5905b | 2020-11-25 16:09:32 +0100 | [diff] [blame] | 189 | t.Errorf("libbindings1 depends on itself") |
| 190 | } |
| 191 | } |
| 192 | } |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 193 | // Check that liba depends on libbindings1 |
| 194 | if strings.Contains(rootModule, "d/src/lib.rs") { |
| 195 | found := false |
| 196 | for _, depName := range validateDependencies(t, crate) { |
| 197 | if depName == "bindings1" { |
| 198 | found = true |
| 199 | break |
| 200 | } |
| 201 | } |
| 202 | if !found { |
| 203 | t.Errorf("liba does not depend on libbindings1: %v", crate) |
| 204 | } |
| 205 | } |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 206 | } |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 207 | } |
Thiébaud Weksteen | 2f628ba | 2020-08-05 14:27:32 +0200 | [diff] [blame] | 208 | |
| 209 | func TestProjectJsonMultiVersion(t *testing.T) { |
| 210 | bp := ` |
| 211 | rust_library { |
| 212 | name: "liba1", |
| 213 | srcs: ["a1/src/lib.rs"], |
| 214 | crate_name: "a" |
| 215 | } |
| 216 | rust_library { |
| 217 | name: "liba2", |
| 218 | srcs: ["a2/src/lib.rs"], |
| 219 | crate_name: "a", |
| 220 | } |
| 221 | rust_library { |
| 222 | name: "libb", |
| 223 | srcs: ["b/src/lib.rs"], |
| 224 | crate_name: "b", |
| 225 | rustlibs: ["liba1", "liba2"], |
| 226 | } |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 227 | ` |
| 228 | jsonContent := testProjectJson(t, bp) |
Thiébaud Weksteen | 2f628ba | 2020-08-05 14:27:32 +0200 | [diff] [blame] | 229 | crates := validateJsonCrates(t, jsonContent) |
Thiébaud Weksteen | 064f6e9 | 2020-12-03 20:58:32 +0100 | [diff] [blame] | 230 | for _, c := range crates { |
| 231 | crate := validateCrate(t, c) |
| 232 | rootModule, ok := crate["root_module"].(string) |
| 233 | if !ok { |
| 234 | t.Fatalf("Unexpected type for root_module: %v", crate["root_module"]) |
| 235 | } |
| 236 | // Make sure that b has 2 different dependencies. |
| 237 | if rootModule == "b/src/lib.rs" { |
Thiébaud Weksteen | 2f628ba | 2020-08-05 14:27:32 +0200 | [diff] [blame] | 238 | aCount := 0 |
Thiébaud Weksteen | 064f6e9 | 2020-12-03 20:58:32 +0100 | [diff] [blame] | 239 | deps := validateDependencies(t, crate) |
| 240 | for _, depName := range deps { |
| 241 | if depName == "a" { |
Thiébaud Weksteen | 2f628ba | 2020-08-05 14:27:32 +0200 | [diff] [blame] | 242 | aCount++ |
| 243 | } |
| 244 | } |
| 245 | if aCount != 2 { |
| 246 | t.Errorf("Unexpected number of liba dependencies want %v, got %v: %v", 2, aCount, deps) |
| 247 | } |
| 248 | return |
| 249 | } |
| 250 | } |
| 251 | t.Errorf("libb crate has not been found: %v", crates) |
| 252 | } |