diff --git a/bp2build/Android.bp b/bp2build/Android.bp index a93fb38c8..14e32edd9 100644 --- a/bp2build/Android.bp +++ b/bp2build/Android.bp @@ -89,6 +89,7 @@ bootstrap_go_package { "python_library_conversion_test.go", "python_test_conversion_test.go", "rust_binary_conversion_test.go", + "rust_ffi_conversion_test.go", "rust_library_conversion_test.go", "rust_proc_macro_conversion_test.go", "rust_protobuf_conversion_test.go", diff --git a/bp2build/rust_ffi_conversion_test.go b/bp2build/rust_ffi_conversion_test.go new file mode 100644 index 000000000..97fe297fd --- /dev/null +++ b/bp2build/rust_ffi_conversion_test.go @@ -0,0 +1,78 @@ +// Copyright 2023 Google Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package bp2build + +import ( + "android/soong/android" + "android/soong/rust" + "testing" +) + +func runRustFfiTestCase(t *testing.T, tc Bp2buildTestCase) { + t.Helper() + RunBp2BuildTestCase(t, registerRustFfiModuleTypes, tc) +} + +func registerRustFfiModuleTypes(ctx android.RegistrationContext) { + ctx.RegisterModuleType("rust_ffi_static", rust.RustFFIStaticFactory) + ctx.RegisterModuleType("rust_library", rust.RustLibraryFactory) +} + +func TestRustFfiStatic(t *testing.T) { + runRustFfiTestCase(t, Bp2buildTestCase{ + Dir: "external/rust/crates/foo", + Blueprint: "", + Filesystem: map[string]string{ + "external/rust/crates/foo/src/lib.rs": "", + "external/rust/crates/foo/src/helper.rs": "", + "external/rust/crates/foo/Android.bp": ` +rust_ffi_static { + name: "libfoo", + crate_name: "foo", + host_supported: true, + srcs: ["src/lib.rs"], + edition: "2015", + include_dirs: [ + "include", + ], + rustlibs: ["libbar"], + bazel_module: { bp2build_available: true }, +} +`, + "external/rust/crates/bar/Android.bp": ` +rust_library { + name: "libbar", + crate_name: "bar", + host_supported: true, + srcs: ["src/lib.rs"], + bazel_module: { bp2build_available: true }, +} +`, + }, + ExpectedBazelTargets: []string{ + MakeBazelTargetNoRestrictions("rust_ffi_static", "libfoo", AttrNameToString{ + "crate_name": `"foo"`, + "deps": `["//external/rust/crates/bar:libbar"]`, + "srcs": `[ + "src/helper.rs", + "src/lib.rs", + ]`, + "edition": `"2015"`, + "export_includes": `["include"]`, + }), + }, + }, + ) +} diff --git a/rust/library.go b/rust/library.go index 2d5113b0e..18bf0a05b 100644 --- a/rust/library.go +++ b/rust/library.go @@ -801,6 +801,10 @@ func (l *libraryDecorator) collectHeadersForSnapshot(ctx android.ModuleContext, } type rustLibraryAttributes struct { + commonLibraryAttrs +} + +type commonLibraryAttrs struct { Srcs bazel.LabelListAttribute Compile_data bazel.LabelListAttribute Crate_name bazel.StringAttribute @@ -811,7 +815,7 @@ type rustLibraryAttributes struct { Proc_macro_deps bazel.LabelListAttribute } -func libraryBp2build(ctx android.Bp2buildMutatorContext, m *Module) { +func commonLibraryAttrsBp2build(ctx android.Bp2buildMutatorContext, m *Module) *commonLibraryAttrs { lib := m.compiler.(*libraryDecorator) srcs, compileData := srcsAndCompileDataAttrs(ctx, *lib.baseCompiler) @@ -835,7 +839,7 @@ func libraryBp2build(ctx android.Bp2buildMutatorContext, m *Module) { rustcFLags = append(rustcFLags, fmt.Sprintf("--cfg=%s", cfg)) } - attrs := &rustLibraryAttributes{ + return &commonLibraryAttrs{ Srcs: bazel.MakeLabelListAttribute( srcs, ), @@ -865,6 +869,9 @@ func libraryBp2build(ctx android.Bp2buildMutatorContext, m *Module) { }, } +} + +func libraryBp2build(ctx android.Bp2buildMutatorContext, m *Module) { ctx.CreateBazelTargetModule( bazel.BazelTargetModuleProperties{ Rule_class: "rust_library", @@ -873,7 +880,7 @@ func libraryBp2build(ctx android.Bp2buildMutatorContext, m *Module) { android.CommonAttributes{ Name: m.Name(), }, - attrs, + commonLibraryAttrsBp2build(ctx, m), ) } @@ -946,3 +953,30 @@ func cargoBuildScriptBp2build(ctx android.Bp2buildMutatorContext, m *Module) *st return &name } + +type ffiStaticAttributes struct { + commonLibraryAttrs + Export_includes bazel.StringListAttribute +} + +func ffiStaticBp2build(ctx android.Bp2buildMutatorContext, m *Module) { + lib := m.compiler.(*libraryDecorator) + + attrs := &ffiStaticAttributes{ + Export_includes: bazel.StringListAttribute{ + Value: lib.Properties.Include_dirs, + }, + commonLibraryAttrs: *commonLibraryAttrsBp2build(ctx, m), + } + + ctx.CreateBazelTargetModule( + bazel.BazelTargetModuleProperties{ + Rule_class: "rust_ffi_static", + Bzl_load_location: "//build/bazel/rules/rust:rust_ffi_static.bzl", + }, + android.CommonAttributes{ + Name: m.Name(), + }, + attrs, + ) +} diff --git a/rust/rust.go b/rust/rust.go index ba63613a2..19c5230b4 100644 --- a/rust/rust.go +++ b/rust/rust.go @@ -1891,6 +1891,8 @@ func (m *Module) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { binaryBp2build(ctx, m) } else if ctx.ModuleType() == "rust_protobuf_host" || ctx.ModuleType() == "rust_protobuf" { protoLibraryBp2build(ctx, m) + } else if ctx.ModuleType() == "rust_ffi_static" { + ffiStaticBp2build(ctx, m) } else { ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "") }