project_json_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. package rust
  15. import (
  16. "encoding/json"
  17. "io/ioutil"
  18. "path/filepath"
  19. "sort"
  20. "strings"
  21. "testing"
  22. "android/soong/android"
  23. )
  24. // testProjectJson run the generation of rust-project.json. It returns the raw
  25. // content of the generated file.
  26. func testProjectJson(t *testing.T, bp string) []byte {
  27. result := android.GroupFixturePreparers(
  28. prepareForRustTest,
  29. android.FixtureMergeEnv(map[string]string{"SOONG_GEN_RUST_PROJECT": "1"}),
  30. ).RunTestWithBp(t, bp)
  31. // The JSON file is generated via WriteFileToOutputDir. Therefore, it
  32. // won't appear in the Output of the TestingSingleton. Manually verify
  33. // it exists.
  34. content, err := ioutil.ReadFile(filepath.Join(result.Config.SoongOutDir(), rustProjectJsonFileName))
  35. if err != nil {
  36. t.Errorf("rust-project.json has not been generated")
  37. }
  38. return content
  39. }
  40. // validateJsonCrates validates that content follows the basic structure of
  41. // rust-project.json. It returns the crates attribute if the validation
  42. // succeeded.
  43. // It uses an empty interface instead of relying on a defined structure to
  44. // avoid a strong dependency on our implementation.
  45. func validateJsonCrates(t *testing.T, rawContent []byte) []interface{} {
  46. var content interface{}
  47. err := json.Unmarshal(rawContent, &content)
  48. if err != nil {
  49. t.Errorf("Unable to parse the rust-project.json as JSON: %v", err)
  50. }
  51. root, ok := content.(map[string]interface{})
  52. if !ok {
  53. t.Errorf("Unexpected JSON format: %v", content)
  54. }
  55. if _, ok = root["crates"]; !ok {
  56. t.Errorf("No crates attribute in rust-project.json: %v", root)
  57. }
  58. crates, ok := root["crates"].([]interface{})
  59. if !ok {
  60. t.Errorf("Unexpected crates format: %v", root["crates"])
  61. }
  62. return crates
  63. }
  64. // validateCrate ensures that a crate can be parsed as a map.
  65. func validateCrate(t *testing.T, crate interface{}) map[string]interface{} {
  66. c, ok := crate.(map[string]interface{})
  67. if !ok {
  68. t.Fatalf("Unexpected type for crate: %v", c)
  69. }
  70. return c
  71. }
  72. // validateDependencies parses the dependencies for a crate. It returns a list
  73. // of the dependencies name.
  74. func validateDependencies(t *testing.T, crate map[string]interface{}) []string {
  75. var dependencies []string
  76. deps, ok := crate["deps"].([]interface{})
  77. if !ok {
  78. t.Errorf("Unexpected format for deps: %v", crate["deps"])
  79. }
  80. for _, dep := range deps {
  81. d, ok := dep.(map[string]interface{})
  82. if !ok {
  83. t.Errorf("Unexpected format for dependency: %v", dep)
  84. }
  85. name, ok := d["name"].(string)
  86. if !ok {
  87. t.Errorf("Dependency is missing the name key: %v", d)
  88. }
  89. dependencies = append(dependencies, name)
  90. }
  91. return dependencies
  92. }
  93. func TestProjectJsonDep(t *testing.T) {
  94. bp := `
  95. rust_library {
  96. name: "liba",
  97. srcs: ["a/src/lib.rs"],
  98. crate_name: "a"
  99. }
  100. rust_library {
  101. name: "libb",
  102. srcs: ["b/src/lib.rs"],
  103. crate_name: "b",
  104. rlibs: ["liba"],
  105. }
  106. `
  107. jsonContent := testProjectJson(t, bp)
  108. validateJsonCrates(t, jsonContent)
  109. }
  110. func TestProjectJsonProcMacroDep(t *testing.T) {
  111. bp := `
  112. rust_proc_macro {
  113. name: "libproc_macro",
  114. srcs: ["a/src/lib.rs"],
  115. crate_name: "proc_macro"
  116. }
  117. rust_library {
  118. name: "librust",
  119. srcs: ["b/src/lib.rs"],
  120. crate_name: "rust",
  121. proc_macros: ["libproc_macro"],
  122. }
  123. `
  124. jsonContent := testProjectJson(t, bp)
  125. crates := validateJsonCrates(t, jsonContent)
  126. libproc_macro_count := 0
  127. librust_count := 0
  128. for _, c := range crates {
  129. crate := validateCrate(t, c)
  130. procMacro, ok := crate["is_proc_macro"].(bool)
  131. if !ok {
  132. t.Fatalf("Unexpected type for is_proc_macro: %v", crate["is_proc_macro"])
  133. }
  134. name, ok := crate["display_name"].(string)
  135. if !ok {
  136. t.Fatalf("Unexpected type for display_name: %v", crate["display_name"])
  137. }
  138. switch name {
  139. case "libproc_macro":
  140. libproc_macro_count += 1
  141. if !procMacro {
  142. t.Fatalf("'libproc_macro' is marked with is_proc_macro=false")
  143. }
  144. case "librust":
  145. librust_count += 1
  146. if procMacro {
  147. t.Fatalf("'librust' is not a proc macro crate, but is marked with is_proc_macro=true")
  148. }
  149. default:
  150. break
  151. }
  152. }
  153. if libproc_macro_count != 1 || librust_count != 1 {
  154. t.Fatalf("Unexpected crate counts: libproc_macro_count: %v, librust_count: %v",
  155. libproc_macro_count, librust_count)
  156. }
  157. }
  158. func TestProjectJsonFeature(t *testing.T) {
  159. bp := `
  160. rust_library {
  161. name: "liba",
  162. srcs: ["a/src/lib.rs"],
  163. crate_name: "a",
  164. features: ["f1", "f2"]
  165. }
  166. `
  167. jsonContent := testProjectJson(t, bp)
  168. crates := validateJsonCrates(t, jsonContent)
  169. for _, c := range crates {
  170. crate := validateCrate(t, c)
  171. cfgs, ok := crate["cfg"].([]interface{})
  172. if !ok {
  173. t.Fatalf("Unexpected type for cfgs: %v", crate)
  174. }
  175. expectedCfgs := []string{"feature=\"f1\"", "feature=\"f2\""}
  176. foundCfgs := []string{}
  177. for _, cfg := range cfgs {
  178. cfg, ok := cfg.(string)
  179. if !ok {
  180. t.Fatalf("Unexpected type for cfg: %v", cfg)
  181. }
  182. foundCfgs = append(foundCfgs, cfg)
  183. }
  184. sort.Strings(foundCfgs)
  185. for i, foundCfg := range foundCfgs {
  186. if foundCfg != expectedCfgs[i] {
  187. t.Errorf("Incorrect features: got %v; want %v", foundCfg, expectedCfgs[i])
  188. }
  189. }
  190. }
  191. }
  192. func TestProjectJsonBinary(t *testing.T) {
  193. bp := `
  194. rust_binary {
  195. name: "libz",
  196. srcs: ["z/src/lib.rs"],
  197. crate_name: "z"
  198. }
  199. `
  200. jsonContent := testProjectJson(t, bp)
  201. crates := validateJsonCrates(t, jsonContent)
  202. for _, c := range crates {
  203. crate := validateCrate(t, c)
  204. rootModule, ok := crate["root_module"].(string)
  205. if !ok {
  206. t.Fatalf("Unexpected type for root_module: %v", crate["root_module"])
  207. }
  208. if rootModule == "z/src/lib.rs" {
  209. return
  210. }
  211. }
  212. t.Errorf("Entry for binary %q not found: %s", "a", jsonContent)
  213. }
  214. func TestProjectJsonBindGen(t *testing.T) {
  215. buildOS := android.TestConfig(t.TempDir(), nil, "", nil).BuildOS
  216. bp := `
  217. rust_library {
  218. name: "libd",
  219. srcs: ["d/src/lib.rs"],
  220. rlibs: ["libbindings1"],
  221. crate_name: "d"
  222. }
  223. rust_bindgen {
  224. name: "libbindings1",
  225. crate_name: "bindings1",
  226. source_stem: "bindings1",
  227. host_supported: true,
  228. wrapper_src: "src/any.h",
  229. }
  230. rust_library_host {
  231. name: "libe",
  232. srcs: ["e/src/lib.rs"],
  233. rustlibs: ["libbindings2"],
  234. crate_name: "e"
  235. }
  236. rust_bindgen_host {
  237. name: "libbindings2",
  238. crate_name: "bindings2",
  239. source_stem: "bindings2",
  240. wrapper_src: "src/any.h",
  241. }
  242. `
  243. jsonContent := testProjectJson(t, bp)
  244. crates := validateJsonCrates(t, jsonContent)
  245. for _, c := range crates {
  246. crate := validateCrate(t, c)
  247. rootModule, ok := crate["root_module"].(string)
  248. if !ok {
  249. t.Fatalf("Unexpected type for root_module: %v", crate["root_module"])
  250. }
  251. if strings.Contains(rootModule, "libbindings1") && !strings.Contains(rootModule, "android_arm64") {
  252. t.Errorf("The source path for libbindings1 does not contain android_arm64, got %v", rootModule)
  253. }
  254. if strings.Contains(rootModule, "libbindings2") && !strings.Contains(rootModule, buildOS.String()) {
  255. t.Errorf("The source path for libbindings2 does not contain the BuildOs, got %v; want %v",
  256. rootModule, buildOS.String())
  257. }
  258. // Check that libbindings1 does not depend on itself.
  259. if strings.Contains(rootModule, "libbindings1") {
  260. for _, depName := range validateDependencies(t, crate) {
  261. if depName == "bindings1" {
  262. t.Errorf("libbindings1 depends on itself")
  263. }
  264. }
  265. }
  266. if strings.Contains(rootModule, "d/src/lib.rs") {
  267. // Check that libd depends on libbindings1
  268. found := false
  269. for _, depName := range validateDependencies(t, crate) {
  270. if depName == "bindings1" {
  271. found = true
  272. break
  273. }
  274. }
  275. if !found {
  276. t.Errorf("libd does not depend on libbindings1: %v", crate)
  277. }
  278. // Check that OUT_DIR is populated.
  279. env, ok := crate["env"].(map[string]interface{})
  280. if !ok {
  281. t.Errorf("libd does not have its environment variables set: %v", crate)
  282. }
  283. if _, ok = env["OUT_DIR"]; !ok {
  284. t.Errorf("libd does not have its OUT_DIR set: %v", env)
  285. }
  286. }
  287. }
  288. }
  289. func TestProjectJsonMultiVersion(t *testing.T) {
  290. bp := `
  291. rust_library {
  292. name: "liba1",
  293. srcs: ["a1/src/lib.rs"],
  294. crate_name: "a"
  295. }
  296. rust_library {
  297. name: "liba2",
  298. srcs: ["a2/src/lib.rs"],
  299. crate_name: "a",
  300. }
  301. rust_library {
  302. name: "libb",
  303. srcs: ["b/src/lib.rs"],
  304. crate_name: "b",
  305. rustlibs: ["liba1", "liba2"],
  306. }
  307. `
  308. jsonContent := testProjectJson(t, bp)
  309. crates := validateJsonCrates(t, jsonContent)
  310. for _, c := range crates {
  311. crate := validateCrate(t, c)
  312. rootModule, ok := crate["root_module"].(string)
  313. if !ok {
  314. t.Fatalf("Unexpected type for root_module: %v", crate["root_module"])
  315. }
  316. // Make sure that b has 2 different dependencies.
  317. if rootModule == "b/src/lib.rs" {
  318. aCount := 0
  319. deps := validateDependencies(t, crate)
  320. for _, depName := range deps {
  321. if depName == "a" {
  322. aCount++
  323. }
  324. }
  325. if aCount != 2 {
  326. t.Errorf("Unexpected number of liba dependencies want %v, got %v: %v", 2, aCount, deps)
  327. }
  328. return
  329. }
  330. }
  331. t.Errorf("libb crate has not been found: %v", crates)
  332. }