Liz Kammer | 267f1f7 | 2023-09-01 01:17:21 -0400 | [diff] [blame] | 1 | // Copyright 2023 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 | |
Vinh Tran | bcb5f57 | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 15 | package bp2build |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/rust" |
| 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | func runRustLibraryTestCase(t *testing.T, tc Bp2buildTestCase) { |
| 24 | t.Helper() |
| 25 | RunBp2BuildTestCase(t, registerRustLibraryModuleTypes, tc) |
| 26 | } |
| 27 | |
| 28 | func registerRustLibraryModuleTypes(ctx android.RegistrationContext) { |
| 29 | ctx.RegisterModuleType("rust_library", rust.RustLibraryFactory) |
| 30 | ctx.RegisterModuleType("rust_library_host", rust.RustLibraryHostFactory) |
| 31 | } |
| 32 | |
Vinh Tran | 9b84678 | 2023-08-24 12:55:12 -0400 | [diff] [blame] | 33 | func TestLibProtobuf(t *testing.T) { |
| 34 | runRustLibraryTestCase(t, Bp2buildTestCase{ |
| 35 | Dir: "external/rust/crates/foo", |
| 36 | Blueprint: "", |
| 37 | Filesystem: map[string]string{ |
| 38 | "external/rust/crates/foo/src/lib.rs": "", |
| 39 | "external/rust/crates/foo/Android.bp": ` |
| 40 | rust_library_host { |
| 41 | name: "libprotobuf", |
| 42 | crate_name: "protobuf", |
| 43 | srcs: ["src/lib.rs"], |
| 44 | bazel_module: { bp2build_available: true }, |
| 45 | } |
| 46 | `, |
| 47 | }, |
| 48 | ExpectedBazelTargets: []string{ |
| 49 | // TODO(b/290790800): Remove the restriction when rust toolchain for android is implemented |
| 50 | makeBazelTargetHostOrDevice("rust_library", "libprotobuf", AttrNameToString{ |
| 51 | "crate_name": `"protobuf"`, |
| 52 | "srcs": `["src/lib.rs"]`, |
| 53 | "deps": `[":libprotobuf_build_script"]`, |
| 54 | }, android.HostSupported), |
| 55 | makeBazelTargetHostOrDevice("cargo_build_script", "libprotobuf_build_script", AttrNameToString{ |
| 56 | "srcs": `["build.rs"]`, |
| 57 | }, android.HostSupported), |
| 58 | }, |
| 59 | }, |
| 60 | ) |
| 61 | } |
| 62 | |
Vinh Tran | bcb5f57 | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 63 | func TestRustLibrary(t *testing.T) { |
| 64 | expectedAttrs := AttrNameToString{ |
| 65 | "crate_name": `"foo"`, |
| 66 | "srcs": `[ |
| 67 | "src/helper.rs", |
| 68 | "src/lib.rs", |
| 69 | ]`, |
| 70 | "crate_features": `["bah-enabled"]`, |
| 71 | "edition": `"2021"`, |
| 72 | "rustc_flags": `["--cfg=baz"]`, |
| 73 | } |
| 74 | |
| 75 | runRustLibraryTestCase(t, Bp2buildTestCase{ |
| 76 | Dir: "external/rust/crates/foo", |
| 77 | Blueprint: "", |
| 78 | Filesystem: map[string]string{ |
| 79 | "external/rust/crates/foo/src/lib.rs": "", |
| 80 | "external/rust/crates/foo/src/helper.rs": "", |
| 81 | "external/rust/crates/foo/Android.bp": ` |
| 82 | rust_library { |
| 83 | name: "libfoo", |
| 84 | crate_name: "foo", |
| 85 | host_supported: true, |
| 86 | srcs: ["src/lib.rs"], |
| 87 | edition: "2021", |
| 88 | features: ["bah-enabled"], |
| 89 | cfgs: ["baz"], |
| 90 | bazel_module: { bp2build_available: true }, |
| 91 | } |
| 92 | rust_library_host { |
| 93 | name: "libfoo_host", |
| 94 | crate_name: "foo", |
| 95 | srcs: ["src/lib.rs"], |
| 96 | edition: "2021", |
| 97 | features: ["bah-enabled"], |
| 98 | cfgs: ["baz"], |
| 99 | bazel_module: { bp2build_available: true }, |
| 100 | } |
| 101 | `, |
| 102 | }, |
| 103 | ExpectedBazelTargets: []string{ |
Vinh Tran | fb8c5a5 | 2023-10-12 15:28:14 -0400 | [diff] [blame] | 104 | MakeBazelTargetNoRestrictions("rust_library", "libfoo", expectedAttrs), |
Vinh Tran | bcb5f57 | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 105 | makeBazelTargetHostOrDevice("rust_library", "libfoo_host", expectedAttrs, android.HostSupported), |
| 106 | }, |
| 107 | }, |
| 108 | ) |
| 109 | } |