2019-08-27 21:03:00 +02:00
|
|
|
// Copyright 2019 The Android Open Source Project
|
|
|
|
//
|
|
|
|
// 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 rust
|
|
|
|
|
|
|
|
import (
|
2020-09-23 18:10:17 +02:00
|
|
|
"fmt"
|
2019-11-01 03:38:29 +01:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2019-08-27 21:03:00 +02:00
|
|
|
"android/soong/android"
|
2023-08-24 17:10:01 +02:00
|
|
|
"android/soong/bazel"
|
2020-09-18 23:15:30 +02:00
|
|
|
"android/soong/cc"
|
2023-08-24 17:10:01 +02:00
|
|
|
|
|
|
|
"github.com/google/blueprint/proptools"
|
2019-08-27 21:03:00 +02:00
|
|
|
)
|
|
|
|
|
2020-09-08 18:46:52 +02:00
|
|
|
var (
|
2023-06-15 20:21:09 +02:00
|
|
|
RlibStdlibSuffix = ".rlib-std"
|
2020-09-08 18:46:52 +02:00
|
|
|
)
|
|
|
|
|
2019-08-27 21:03:00 +02:00
|
|
|
func init() {
|
|
|
|
android.RegisterModuleType("rust_library", RustLibraryFactory)
|
|
|
|
android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory)
|
|
|
|
android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory)
|
|
|
|
android.RegisterModuleType("rust_library_host", RustLibraryHostFactory)
|
|
|
|
android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory)
|
|
|
|
android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory)
|
2020-06-23 23:28:53 +02:00
|
|
|
android.RegisterModuleType("rust_ffi", RustFFIFactory)
|
|
|
|
android.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory)
|
|
|
|
android.RegisterModuleType("rust_ffi_static", RustFFIStaticFactory)
|
|
|
|
android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory)
|
|
|
|
android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory)
|
|
|
|
android.RegisterModuleType("rust_ffi_host_static", RustFFIStaticHostFactory)
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type VariantLibraryProperties struct {
|
2020-06-25 09:47:46 +02:00
|
|
|
Enabled *bool `android:"arch_variant"`
|
|
|
|
Srcs []string `android:"path,arch_variant"`
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type LibraryCompilerProperties struct {
|
2019-10-18 23:49:46 +02:00
|
|
|
Rlib VariantLibraryProperties `android:"arch_variant"`
|
|
|
|
Dylib VariantLibraryProperties `android:"arch_variant"`
|
|
|
|
Shared VariantLibraryProperties `android:"arch_variant"`
|
|
|
|
Static VariantLibraryProperties `android:"arch_variant"`
|
2019-08-27 21:03:00 +02:00
|
|
|
|
2019-10-18 23:49:46 +02:00
|
|
|
// path to include directories to pass to cc_* modules, only relevant for static/shared variants.
|
|
|
|
Include_dirs []string `android:"path,arch_variant"`
|
2020-09-08 18:46:52 +02:00
|
|
|
|
|
|
|
// Whether this library is part of the Rust toolchain sysroot.
|
|
|
|
Sysroot *bool
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type LibraryMutatedProperties struct {
|
|
|
|
// Build a dylib variant
|
|
|
|
BuildDylib bool `blueprint:"mutated"`
|
|
|
|
// Build an rlib variant
|
|
|
|
BuildRlib bool `blueprint:"mutated"`
|
2019-10-18 23:49:46 +02:00
|
|
|
// Build a shared library variant
|
|
|
|
BuildShared bool `blueprint:"mutated"`
|
|
|
|
// Build a static library variant
|
|
|
|
BuildStatic bool `blueprint:"mutated"`
|
2019-08-27 21:03:00 +02:00
|
|
|
|
|
|
|
// This variant is a dylib
|
|
|
|
VariantIsDylib bool `blueprint:"mutated"`
|
|
|
|
// This variant is an rlib
|
|
|
|
VariantIsRlib bool `blueprint:"mutated"`
|
2019-10-18 23:49:46 +02:00
|
|
|
// This variant is a shared library
|
|
|
|
VariantIsShared bool `blueprint:"mutated"`
|
|
|
|
// This variant is a static library
|
|
|
|
VariantIsStatic bool `blueprint:"mutated"`
|
2020-09-23 18:10:17 +02:00
|
|
|
// This variant is a source provider
|
|
|
|
VariantIsSource bool `blueprint:"mutated"`
|
2020-07-31 19:40:31 +02:00
|
|
|
|
|
|
|
// This variant is disabled and should not be compiled
|
|
|
|
// (used for SourceProvider variants that produce only source)
|
|
|
|
VariantIsDisabled bool `blueprint:"mutated"`
|
2020-09-08 18:46:52 +02:00
|
|
|
|
|
|
|
// Whether this library variant should be link libstd via rlibs
|
|
|
|
VariantIsStaticStd bool `blueprint:"mutated"`
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type libraryDecorator struct {
|
|
|
|
*baseCompiler
|
2020-06-25 18:34:12 +02:00
|
|
|
*flagExporter
|
2020-08-27 13:48:36 +02:00
|
|
|
stripper Stripper
|
2019-08-27 21:03:00 +02:00
|
|
|
|
2020-06-16 16:26:57 +02:00
|
|
|
Properties LibraryCompilerProperties
|
|
|
|
MutatedProperties LibraryMutatedProperties
|
|
|
|
includeDirs android.Paths
|
2020-07-31 19:40:31 +02:00
|
|
|
sourceProvider SourceProvider
|
2021-05-20 19:39:16 +02:00
|
|
|
|
|
|
|
collectedSnapshotHeaders android.Paths
|
2021-11-03 20:30:18 +01:00
|
|
|
|
|
|
|
// table-of-contents file for cdylib crates to optimize out relinking when possible
|
|
|
|
tocFile android.OptionalPath
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type libraryInterface interface {
|
|
|
|
rlib() bool
|
|
|
|
dylib() bool
|
2019-10-18 23:49:46 +02:00
|
|
|
static() bool
|
|
|
|
shared() bool
|
2020-09-08 18:46:52 +02:00
|
|
|
sysroot() bool
|
2020-09-23 18:10:17 +02:00
|
|
|
source() bool
|
2019-08-27 21:03:00 +02:00
|
|
|
|
|
|
|
// Returns true if the build options for the module have selected a particular build type
|
|
|
|
buildRlib() bool
|
|
|
|
buildDylib() bool
|
2019-10-18 23:49:46 +02:00
|
|
|
buildShared() bool
|
|
|
|
buildStatic() bool
|
2019-08-27 21:03:00 +02:00
|
|
|
|
|
|
|
// Sets a particular variant type
|
|
|
|
setRlib()
|
|
|
|
setDylib()
|
2019-10-18 23:49:46 +02:00
|
|
|
setShared()
|
|
|
|
setStatic()
|
2020-09-23 18:10:17 +02:00
|
|
|
setSource()
|
2019-10-18 23:49:46 +02:00
|
|
|
|
2021-06-01 21:09:53 +02:00
|
|
|
// libstd linkage functions
|
|
|
|
rlibStd() bool
|
2020-09-08 18:46:52 +02:00
|
|
|
setRlibStd()
|
|
|
|
setDylibStd()
|
|
|
|
|
2019-10-18 23:49:46 +02:00
|
|
|
// Build a specific library variant
|
2020-06-23 23:28:53 +02:00
|
|
|
BuildOnlyFFI()
|
|
|
|
BuildOnlyRust()
|
2019-10-18 23:49:46 +02:00
|
|
|
BuildOnlyRlib()
|
|
|
|
BuildOnlyDylib()
|
|
|
|
BuildOnlyStatic()
|
|
|
|
BuildOnlyShared()
|
2021-11-03 20:30:18 +01:00
|
|
|
|
|
|
|
toc() android.OptionalPath
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 15:56:02 +02:00
|
|
|
func (library *libraryDecorator) nativeCoverage() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-11-03 20:30:18 +01:00
|
|
|
func (library *libraryDecorator) toc() android.OptionalPath {
|
|
|
|
return library.tocFile
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:03:00 +02:00
|
|
|
func (library *libraryDecorator) rlib() bool {
|
|
|
|
return library.MutatedProperties.VariantIsRlib
|
|
|
|
}
|
|
|
|
|
2020-09-08 18:46:52 +02:00
|
|
|
func (library *libraryDecorator) sysroot() bool {
|
|
|
|
return Bool(library.Properties.Sysroot)
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:03:00 +02:00
|
|
|
func (library *libraryDecorator) dylib() bool {
|
|
|
|
return library.MutatedProperties.VariantIsDylib
|
|
|
|
}
|
|
|
|
|
2019-10-18 23:49:46 +02:00
|
|
|
func (library *libraryDecorator) shared() bool {
|
|
|
|
return library.MutatedProperties.VariantIsShared
|
|
|
|
}
|
|
|
|
|
|
|
|
func (library *libraryDecorator) static() bool {
|
|
|
|
return library.MutatedProperties.VariantIsStatic
|
|
|
|
}
|
|
|
|
|
2020-09-23 18:10:17 +02:00
|
|
|
func (library *libraryDecorator) source() bool {
|
|
|
|
return library.MutatedProperties.VariantIsSource
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:03:00 +02:00
|
|
|
func (library *libraryDecorator) buildRlib() bool {
|
|
|
|
return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (library *libraryDecorator) buildDylib() bool {
|
|
|
|
return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
|
|
|
|
}
|
|
|
|
|
2019-10-18 23:49:46 +02:00
|
|
|
func (library *libraryDecorator) buildShared() bool {
|
|
|
|
return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (library *libraryDecorator) buildStatic() bool {
|
|
|
|
return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:03:00 +02:00
|
|
|
func (library *libraryDecorator) setRlib() {
|
|
|
|
library.MutatedProperties.VariantIsRlib = true
|
|
|
|
library.MutatedProperties.VariantIsDylib = false
|
2019-10-18 23:49:46 +02:00
|
|
|
library.MutatedProperties.VariantIsStatic = false
|
|
|
|
library.MutatedProperties.VariantIsShared = false
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (library *libraryDecorator) setDylib() {
|
|
|
|
library.MutatedProperties.VariantIsRlib = false
|
|
|
|
library.MutatedProperties.VariantIsDylib = true
|
2019-10-18 23:49:46 +02:00
|
|
|
library.MutatedProperties.VariantIsStatic = false
|
|
|
|
library.MutatedProperties.VariantIsShared = false
|
|
|
|
}
|
|
|
|
|
2021-06-01 21:09:53 +02:00
|
|
|
func (library *libraryDecorator) rlibStd() bool {
|
|
|
|
return library.MutatedProperties.VariantIsStaticStd
|
|
|
|
}
|
|
|
|
|
2020-09-08 18:46:52 +02:00
|
|
|
func (library *libraryDecorator) setRlibStd() {
|
|
|
|
library.MutatedProperties.VariantIsStaticStd = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (library *libraryDecorator) setDylibStd() {
|
|
|
|
library.MutatedProperties.VariantIsStaticStd = false
|
|
|
|
}
|
|
|
|
|
2019-10-18 23:49:46 +02:00
|
|
|
func (library *libraryDecorator) setShared() {
|
|
|
|
library.MutatedProperties.VariantIsStatic = false
|
|
|
|
library.MutatedProperties.VariantIsShared = true
|
|
|
|
library.MutatedProperties.VariantIsRlib = false
|
|
|
|
library.MutatedProperties.VariantIsDylib = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (library *libraryDecorator) setStatic() {
|
|
|
|
library.MutatedProperties.VariantIsStatic = true
|
|
|
|
library.MutatedProperties.VariantIsShared = false
|
|
|
|
library.MutatedProperties.VariantIsRlib = false
|
|
|
|
library.MutatedProperties.VariantIsDylib = false
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 18:10:17 +02:00
|
|
|
func (library *libraryDecorator) setSource() {
|
|
|
|
library.MutatedProperties.VariantIsSource = true
|
|
|
|
}
|
|
|
|
|
2021-01-26 15:18:53 +01:00
|
|
|
func (library *libraryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
|
2023-07-13 17:01:41 +02:00
|
|
|
if library.preferRlib() {
|
2020-12-08 20:43:00 +01:00
|
|
|
return rlibAutoDep
|
|
|
|
} else if library.rlib() || library.static() {
|
2020-06-29 23:34:06 +02:00
|
|
|
return rlibAutoDep
|
|
|
|
} else if library.dylib() || library.shared() {
|
|
|
|
return dylibAutoDep
|
|
|
|
} else {
|
2020-09-23 18:10:17 +02:00
|
|
|
panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
|
2020-06-29 23:34:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 20:43:00 +01:00
|
|
|
func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
|
2023-07-13 17:01:41 +02:00
|
|
|
if library.static() || library.MutatedProperties.VariantIsStaticStd {
|
2020-12-08 20:43:00 +01:00
|
|
|
return RlibLinkage
|
|
|
|
} else if library.baseCompiler.preferRlib() {
|
|
|
|
return RlibLinkage
|
|
|
|
}
|
|
|
|
return DefaultLinkage
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:03:00 +02:00
|
|
|
var _ compiler = (*libraryDecorator)(nil)
|
2019-10-18 23:49:46 +02:00
|
|
|
var _ libraryInterface = (*libraryDecorator)(nil)
|
2020-06-25 18:34:12 +02:00
|
|
|
var _ exportedFlagsProducer = (*libraryDecorator)(nil)
|
2019-08-27 21:03:00 +02:00
|
|
|
|
2022-11-18 12:08:55 +01:00
|
|
|
// rust_library produces all Rust variants (rust_library_dylib and
|
|
|
|
// rust_library_rlib).
|
2019-08-27 21:03:00 +02:00
|
|
|
func RustLibraryFactory() android.Module {
|
2020-06-23 23:28:53 +02:00
|
|
|
module, library := NewRustLibrary(android.HostAndDeviceSupported)
|
|
|
|
library.BuildOnlyRust()
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
2022-11-18 12:08:55 +01:00
|
|
|
// rust_ffi produces all FFI variants (rust_ffi_shared and
|
|
|
|
// rust_ffi_static).
|
2020-06-23 23:28:53 +02:00
|
|
|
func RustFFIFactory() android.Module {
|
|
|
|
module, library := NewRustLibrary(android.HostAndDeviceSupported)
|
|
|
|
library.BuildOnlyFFI()
|
2019-08-27 21:03:00 +02:00
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
2022-11-18 12:08:55 +01:00
|
|
|
// rust_library_dylib produces a Rust dylib (Rust crate type "dylib").
|
2019-08-27 21:03:00 +02:00
|
|
|
func RustLibraryDylibFactory() android.Module {
|
|
|
|
module, library := NewRustLibrary(android.HostAndDeviceSupported)
|
|
|
|
library.BuildOnlyDylib()
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
2022-11-18 12:08:55 +01:00
|
|
|
// rust_library_rlib produces an rlib (Rust crate type "rlib").
|
2019-08-27 21:03:00 +02:00
|
|
|
func RustLibraryRlibFactory() android.Module {
|
|
|
|
module, library := NewRustLibrary(android.HostAndDeviceSupported)
|
|
|
|
library.BuildOnlyRlib()
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
2022-11-18 12:08:55 +01:00
|
|
|
// rust_ffi_shared produces a shared library (Rust crate type
|
|
|
|
// "cdylib").
|
2020-06-23 23:28:53 +02:00
|
|
|
func RustFFISharedFactory() android.Module {
|
2019-10-18 23:49:46 +02:00
|
|
|
module, library := NewRustLibrary(android.HostAndDeviceSupported)
|
|
|
|
library.BuildOnlyShared()
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
2022-11-18 12:08:55 +01:00
|
|
|
// rust_ffi_static produces a static library (Rust crate type
|
|
|
|
// "staticlib").
|
2020-06-23 23:28:53 +02:00
|
|
|
func RustFFIStaticFactory() android.Module {
|
2019-10-18 23:49:46 +02:00
|
|
|
module, library := NewRustLibrary(android.HostAndDeviceSupported)
|
|
|
|
library.BuildOnlyStatic()
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
2022-11-18 12:08:55 +01:00
|
|
|
// rust_library_host produces all Rust variants for the host
|
|
|
|
// (rust_library_dylib_host and rust_library_rlib_host).
|
2019-08-27 21:03:00 +02:00
|
|
|
func RustLibraryHostFactory() android.Module {
|
2020-06-23 23:28:53 +02:00
|
|
|
module, library := NewRustLibrary(android.HostSupported)
|
|
|
|
library.BuildOnlyRust()
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
2022-11-18 12:08:55 +01:00
|
|
|
// rust_ffi_host produces all FFI variants for the host
|
|
|
|
// (rust_ffi_static_host and rust_ffi_shared_host).
|
2020-06-23 23:28:53 +02:00
|
|
|
func RustFFIHostFactory() android.Module {
|
|
|
|
module, library := NewRustLibrary(android.HostSupported)
|
|
|
|
library.BuildOnlyFFI()
|
2019-08-27 21:03:00 +02:00
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
2022-11-18 12:08:55 +01:00
|
|
|
// rust_library_dylib_host produces a dylib for the host (Rust crate
|
|
|
|
// type "dylib").
|
2019-08-27 21:03:00 +02:00
|
|
|
func RustLibraryDylibHostFactory() android.Module {
|
|
|
|
module, library := NewRustLibrary(android.HostSupported)
|
|
|
|
library.BuildOnlyDylib()
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
2022-11-18 12:08:55 +01:00
|
|
|
// rust_library_rlib_host produces an rlib for the host (Rust crate
|
|
|
|
// type "rlib").
|
2019-08-27 21:03:00 +02:00
|
|
|
func RustLibraryRlibHostFactory() android.Module {
|
|
|
|
module, library := NewRustLibrary(android.HostSupported)
|
|
|
|
library.BuildOnlyRlib()
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
2022-11-18 12:08:55 +01:00
|
|
|
// rust_ffi_static_host produces a static library for the host (Rust
|
|
|
|
// crate type "staticlib").
|
2020-06-23 23:28:53 +02:00
|
|
|
func RustFFIStaticHostFactory() android.Module {
|
2019-10-18 23:49:46 +02:00
|
|
|
module, library := NewRustLibrary(android.HostSupported)
|
|
|
|
library.BuildOnlyStatic()
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
2022-11-18 12:08:55 +01:00
|
|
|
// rust_ffi_shared_host produces an shared library for the host (Rust
|
|
|
|
// crate type "cdylib").
|
2020-06-23 23:28:53 +02:00
|
|
|
func RustFFISharedHostFactory() android.Module {
|
2019-10-18 23:49:46 +02:00
|
|
|
module, library := NewRustLibrary(android.HostSupported)
|
|
|
|
library.BuildOnlyShared()
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
2020-06-23 23:28:53 +02:00
|
|
|
func (library *libraryDecorator) BuildOnlyFFI() {
|
|
|
|
library.MutatedProperties.BuildDylib = false
|
2019-08-27 21:03:00 +02:00
|
|
|
library.MutatedProperties.BuildRlib = false
|
2020-06-23 23:28:53 +02:00
|
|
|
library.MutatedProperties.BuildShared = true
|
|
|
|
library.MutatedProperties.BuildStatic = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (library *libraryDecorator) BuildOnlyRust() {
|
|
|
|
library.MutatedProperties.BuildDylib = true
|
|
|
|
library.MutatedProperties.BuildRlib = true
|
2019-10-18 23:49:46 +02:00
|
|
|
library.MutatedProperties.BuildShared = false
|
|
|
|
library.MutatedProperties.BuildStatic = false
|
2020-06-23 23:28:53 +02:00
|
|
|
}
|
2019-10-18 23:49:46 +02:00
|
|
|
|
2020-06-23 23:28:53 +02:00
|
|
|
func (library *libraryDecorator) BuildOnlyDylib() {
|
|
|
|
library.MutatedProperties.BuildDylib = true
|
|
|
|
library.MutatedProperties.BuildRlib = false
|
|
|
|
library.MutatedProperties.BuildShared = false
|
|
|
|
library.MutatedProperties.BuildStatic = false
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (library *libraryDecorator) BuildOnlyRlib() {
|
|
|
|
library.MutatedProperties.BuildDylib = false
|
2020-06-23 23:28:53 +02:00
|
|
|
library.MutatedProperties.BuildRlib = true
|
2019-10-18 23:49:46 +02:00
|
|
|
library.MutatedProperties.BuildShared = false
|
|
|
|
library.MutatedProperties.BuildStatic = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (library *libraryDecorator) BuildOnlyStatic() {
|
|
|
|
library.MutatedProperties.BuildRlib = false
|
|
|
|
library.MutatedProperties.BuildDylib = false
|
2020-06-23 23:28:53 +02:00
|
|
|
library.MutatedProperties.BuildShared = false
|
|
|
|
library.MutatedProperties.BuildStatic = true
|
2019-10-18 23:49:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (library *libraryDecorator) BuildOnlyShared() {
|
|
|
|
library.MutatedProperties.BuildRlib = false
|
|
|
|
library.MutatedProperties.BuildDylib = false
|
2020-06-23 23:28:53 +02:00
|
|
|
library.MutatedProperties.BuildStatic = false
|
|
|
|
library.MutatedProperties.BuildShared = true
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
|
2020-04-28 16:10:23 +02:00
|
|
|
module := newModule(hod, android.MultilibBoth)
|
2019-08-27 21:03:00 +02:00
|
|
|
|
2023-08-24 17:10:01 +02:00
|
|
|
android.InitBazelModule(module)
|
|
|
|
|
2019-08-27 21:03:00 +02:00
|
|
|
library := &libraryDecorator{
|
|
|
|
MutatedProperties: LibraryMutatedProperties{
|
2020-06-23 23:28:53 +02:00
|
|
|
BuildDylib: false,
|
|
|
|
BuildRlib: false,
|
|
|
|
BuildShared: false,
|
|
|
|
BuildStatic: false,
|
2019-08-27 21:03:00 +02:00
|
|
|
},
|
2019-12-13 04:36:05 +01:00
|
|
|
baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
|
2020-06-25 18:34:12 +02:00
|
|
|
flagExporter: NewFlagExporter(),
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.compiler = library
|
|
|
|
|
|
|
|
return module, library
|
|
|
|
}
|
|
|
|
|
|
|
|
func (library *libraryDecorator) compilerProps() []interface{} {
|
|
|
|
return append(library.baseCompiler.compilerProps(),
|
|
|
|
&library.Properties,
|
2020-08-27 13:48:36 +02:00
|
|
|
&library.MutatedProperties,
|
|
|
|
&library.stripper.StripProperties)
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
2019-09-20 20:00:37 +02:00
|
|
|
func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
|
|
|
|
deps = library.baseCompiler.compilerDeps(ctx, deps)
|
|
|
|
|
2022-01-24 05:48:36 +01:00
|
|
|
if library.dylib() || library.shared() {
|
|
|
|
if ctx.toolchain().Bionic() {
|
|
|
|
deps = bionicDeps(ctx, deps, false)
|
|
|
|
deps.CrtBegin = []string{"crtbegin_so"}
|
|
|
|
deps.CrtEnd = []string{"crtend_so"}
|
|
|
|
} else if ctx.Os() == android.LinuxMusl {
|
|
|
|
deps = muslDeps(ctx, deps, false)
|
|
|
|
deps.CrtBegin = []string{"libc_musl_crtbegin_so"}
|
|
|
|
deps.CrtEnd = []string{"libc_musl_crtend_so"}
|
|
|
|
}
|
2019-09-20 20:00:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return deps
|
|
|
|
}
|
2020-06-09 14:27:49 +02:00
|
|
|
|
|
|
|
func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
|
|
|
|
return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
|
|
|
|
}
|
|
|
|
|
2021-09-23 17:50:33 +02:00
|
|
|
func (library *libraryDecorator) cfgFlags(ctx ModuleContext, flags Flags) Flags {
|
|
|
|
flags = library.baseCompiler.cfgFlags(ctx, flags)
|
2021-07-08 04:05:02 +02:00
|
|
|
if library.dylib() {
|
|
|
|
// We need to add a dependency on std in order to link crates as dylibs.
|
|
|
|
// The hack to add this dependency is guarded by the following cfg so
|
|
|
|
// that we don't force a dependency when it isn't needed.
|
|
|
|
library.baseCompiler.Properties.Cfgs = append(library.baseCompiler.Properties.Cfgs, "android_dylib")
|
|
|
|
}
|
2021-09-23 17:50:33 +02:00
|
|
|
|
|
|
|
flags.RustFlags = append(flags.RustFlags, library.baseCompiler.cfgsToFlags()...)
|
|
|
|
flags.RustdocFlags = append(flags.RustdocFlags, library.baseCompiler.cfgsToFlags()...)
|
|
|
|
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
|
|
|
func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
|
2019-11-07 04:15:49 +01:00
|
|
|
flags = library.baseCompiler.compilerFlags(ctx, flags)
|
2021-09-23 17:50:33 +02:00
|
|
|
|
|
|
|
flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
|
2019-11-07 04:15:49 +01:00
|
|
|
if library.shared() || library.static() {
|
|
|
|
library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
|
|
|
|
}
|
2020-06-09 14:27:49 +02:00
|
|
|
if library.shared() {
|
2023-08-15 06:09:47 +02:00
|
|
|
if ctx.Darwin() {
|
|
|
|
flags.LinkFlags = append(
|
|
|
|
flags.LinkFlags,
|
|
|
|
"-dynamic_lib",
|
|
|
|
"-install_name @rpath/"+library.sharedLibFilename(ctx),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
|
|
|
|
}
|
2020-06-09 14:27:49 +02:00
|
|
|
}
|
|
|
|
|
2019-11-07 04:15:49 +01:00
|
|
|
return flags
|
|
|
|
}
|
2019-09-20 20:00:37 +02:00
|
|
|
|
2022-04-19 05:12:56 +02:00
|
|
|
func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
|
|
|
|
var outputFile android.ModuleOutPath
|
|
|
|
var ret buildOutput
|
2020-08-27 13:48:36 +02:00
|
|
|
var fileName string
|
2021-03-19 23:06:02 +01:00
|
|
|
srcPath := library.srcPath(ctx, deps)
|
2019-08-27 21:03:00 +02:00
|
|
|
|
2020-07-31 19:40:31 +02:00
|
|
|
if library.sourceProvider != nil {
|
2020-12-01 15:25:22 +01:00
|
|
|
deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
|
2020-07-31 19:40:31 +02:00
|
|
|
}
|
2019-08-27 21:03:00 +02:00
|
|
|
|
2021-11-05 21:36:47 +01:00
|
|
|
// Calculate output filename
|
|
|
|
if library.rlib() {
|
|
|
|
fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
|
|
|
|
outputFile = android.PathForModuleOut(ctx, fileName)
|
2022-04-19 05:12:56 +02:00
|
|
|
ret.outputFile = outputFile
|
2021-11-05 21:36:47 +01:00
|
|
|
} else if library.dylib() {
|
|
|
|
fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
|
|
|
|
outputFile = android.PathForModuleOut(ctx, fileName)
|
2022-04-19 05:12:56 +02:00
|
|
|
ret.outputFile = outputFile
|
2021-11-05 21:36:47 +01:00
|
|
|
} else if library.static() {
|
|
|
|
fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
|
|
|
|
outputFile = android.PathForModuleOut(ctx, fileName)
|
2022-04-19 05:12:56 +02:00
|
|
|
ret.outputFile = outputFile
|
2021-11-05 21:36:47 +01:00
|
|
|
} else if library.shared() {
|
|
|
|
fileName = library.sharedLibFilename(ctx)
|
|
|
|
outputFile = android.PathForModuleOut(ctx, fileName)
|
2022-04-19 05:12:56 +02:00
|
|
|
ret.outputFile = outputFile
|
2021-11-05 21:36:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) {
|
|
|
|
strippedOutputFile := outputFile
|
|
|
|
outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
|
|
|
|
library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
|
|
|
|
|
|
|
|
library.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
|
|
|
|
}
|
|
|
|
library.baseCompiler.unstrippedOutputFile = outputFile
|
|
|
|
|
2019-08-27 21:03:00 +02:00
|
|
|
flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
|
2021-02-04 17:29:41 +01:00
|
|
|
flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
|
2023-04-01 05:21:19 +02:00
|
|
|
flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects.Strings()...)
|
2019-08-27 21:03:00 +02:00
|
|
|
|
2020-01-14 01:34:34 +01:00
|
|
|
if library.dylib() {
|
2019-10-18 23:49:46 +02:00
|
|
|
// We need prefer-dynamic for now to avoid linking in the static stdlib. See:
|
|
|
|
// https://github.com/rust-lang/rust/issues/19680
|
|
|
|
// https://github.com/rust-lang/rust/issues/34909
|
|
|
|
flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
|
|
|
|
}
|
|
|
|
|
2021-11-05 21:36:47 +01:00
|
|
|
// Call the appropriate builder for this library type
|
2019-08-27 21:03:00 +02:00
|
|
|
if library.rlib() {
|
2022-04-19 05:12:56 +02:00
|
|
|
ret.kytheFile = TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile).kytheFile
|
2019-08-27 21:03:00 +02:00
|
|
|
} else if library.dylib() {
|
2022-04-19 05:12:56 +02:00
|
|
|
ret.kytheFile = TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile).kytheFile
|
2019-10-18 23:49:46 +02:00
|
|
|
} else if library.static() {
|
2022-04-19 05:12:56 +02:00
|
|
|
ret.kytheFile = TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile).kytheFile
|
2019-10-18 23:49:46 +02:00
|
|
|
} else if library.shared() {
|
2022-04-19 05:12:56 +02:00
|
|
|
ret.kytheFile = TransformSrctoShared(ctx, srcPath, deps, flags, outputFile).kytheFile
|
2020-04-09 15:56:02 +02:00
|
|
|
}
|
|
|
|
|
2019-10-18 23:49:46 +02:00
|
|
|
if library.rlib() || library.dylib() {
|
2020-09-18 23:15:30 +02:00
|
|
|
library.flagExporter.exportLinkDirs(deps.linkDirs...)
|
|
|
|
library.flagExporter.exportLinkObjects(deps.linkObjects...)
|
2023-04-01 05:21:19 +02:00
|
|
|
library.flagExporter.exportLibDeps(deps.LibDeps...)
|
2019-10-18 23:49:46 +02:00
|
|
|
}
|
2019-08-27 21:03:00 +02:00
|
|
|
|
2020-09-18 23:15:30 +02:00
|
|
|
if library.static() || library.shared() {
|
|
|
|
ctx.SetProvider(cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
|
|
|
|
IncludeDirs: library.includeDirs,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if library.shared() {
|
2021-11-03 20:30:18 +01:00
|
|
|
// Optimize out relinking against shared libraries whose interface hasn't changed by
|
|
|
|
// depending on a table of contents file instead of the library itself.
|
|
|
|
tocFile := outputFile.ReplaceExtension(ctx, flags.Toolchain.SharedLibSuffix()[1:]+".toc")
|
|
|
|
library.tocFile = android.OptionalPathForPath(tocFile)
|
|
|
|
cc.TransformSharedObjectToToc(ctx, outputFile, tocFile)
|
|
|
|
|
2020-09-18 23:15:30 +02:00
|
|
|
ctx.SetProvider(cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{
|
2021-11-03 20:30:18 +01:00
|
|
|
TableOfContents: android.OptionalPathForPath(tocFile),
|
|
|
|
SharedLibrary: outputFile,
|
|
|
|
Target: ctx.Target(),
|
2020-09-18 23:15:30 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if library.static() {
|
2022-04-21 21:50:51 +02:00
|
|
|
depSet := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(outputFile).Build()
|
2020-09-18 23:15:30 +02:00
|
|
|
ctx.SetProvider(cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{
|
|
|
|
StaticLibrary: outputFile,
|
|
|
|
|
|
|
|
TransitiveStaticLibrariesForOrdering: depSet,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
library.flagExporter.setProvider(ctx)
|
|
|
|
|
2021-11-05 21:36:47 +01:00
|
|
|
return ret
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
2022-04-19 05:12:56 +02:00
|
|
|
func (library *libraryDecorator) srcPath(ctx ModuleContext, _ PathDeps) android.Path {
|
2021-03-19 23:06:02 +01:00
|
|
|
if library.sourceProvider != nil {
|
|
|
|
// Assume the first source from the source provider is the library entry point.
|
|
|
|
return library.sourceProvider.Srcs()[0]
|
|
|
|
} else {
|
|
|
|
path, _ := srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
|
|
|
|
deps PathDeps) android.OptionalPath {
|
|
|
|
// rustdoc has builtin support for documenting config specific information
|
|
|
|
// regardless of the actual config it was given
|
|
|
|
// (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information),
|
|
|
|
// so we generate the rustdoc for only the primary module so that we have a
|
|
|
|
// single set of docs to refer to.
|
|
|
|
if ctx.Module() != ctx.PrimaryModule() {
|
|
|
|
return android.OptionalPath{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return android.OptionalPathForPath(Rustdoc(ctx, library.srcPath(ctx, deps),
|
|
|
|
deps, flags))
|
|
|
|
}
|
|
|
|
|
2019-11-01 03:38:29 +01:00
|
|
|
func (library *libraryDecorator) getStem(ctx ModuleContext) string {
|
|
|
|
stem := library.baseCompiler.getStemWithoutSuffix(ctx)
|
|
|
|
validateLibraryStem(ctx, stem, library.crateName())
|
|
|
|
|
|
|
|
return stem + String(library.baseCompiler.Properties.Suffix)
|
|
|
|
}
|
|
|
|
|
2020-09-08 18:46:52 +02:00
|
|
|
func (library *libraryDecorator) install(ctx ModuleContext) {
|
|
|
|
// Only shared and dylib variants make sense to install.
|
|
|
|
if library.shared() || library.dylib() {
|
|
|
|
library.baseCompiler.install(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 19:40:31 +02:00
|
|
|
func (library *libraryDecorator) Disabled() bool {
|
|
|
|
return library.MutatedProperties.VariantIsDisabled
|
|
|
|
}
|
|
|
|
|
|
|
|
func (library *libraryDecorator) SetDisabled() {
|
|
|
|
library.MutatedProperties.VariantIsDisabled = true
|
|
|
|
}
|
|
|
|
|
2019-11-01 03:38:29 +01:00
|
|
|
var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
|
|
|
|
|
|
|
|
func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
|
|
|
|
if crate_name == "" {
|
|
|
|
ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// crate_names are used for the library output file, and rustc expects these
|
|
|
|
// to be alphanumeric with underscores allowed.
|
|
|
|
if validCrateName.MatchString(crate_name) {
|
|
|
|
ctx.PropertyErrorf("crate_name",
|
|
|
|
"library crate_names must be alphanumeric with underscores allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Libraries are expected to begin with "lib" followed by the crate_name
|
|
|
|
if !strings.HasPrefix(filename, "lib"+crate_name) {
|
|
|
|
ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 18:10:17 +02:00
|
|
|
// LibraryMutator mutates the libraries into variants according to the
|
|
|
|
// build{Rlib,Dylib} attributes.
|
2019-08-27 21:03:00 +02:00
|
|
|
func LibraryMutator(mctx android.BottomUpMutatorContext) {
|
2020-09-23 18:10:17 +02:00
|
|
|
// Only mutate on Rust libraries.
|
|
|
|
m, ok := mctx.Module().(*Module)
|
|
|
|
if !ok || m.compiler == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
library, ok := m.compiler.(libraryInterface)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2020-07-31 19:40:31 +02:00
|
|
|
|
2020-09-23 18:10:17 +02:00
|
|
|
var variants []string
|
|
|
|
// The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
|
|
|
|
// depend on this variant. It must be the first variant to be declared.
|
|
|
|
sourceVariant := false
|
|
|
|
if m.sourceProvider != nil {
|
|
|
|
variants = append(variants, "source")
|
|
|
|
sourceVariant = true
|
|
|
|
}
|
|
|
|
if library.buildRlib() {
|
|
|
|
variants = append(variants, rlibVariation)
|
|
|
|
}
|
|
|
|
if library.buildDylib() {
|
|
|
|
variants = append(variants, dylibVariation)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(variants) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
modules := mctx.CreateLocalVariations(variants...)
|
|
|
|
|
|
|
|
// The order of the variations (modules) matches the variant names provided. Iterate
|
|
|
|
// through the new variation modules and set their mutated properties.
|
|
|
|
for i, v := range modules {
|
|
|
|
switch variants[i] {
|
|
|
|
case rlibVariation:
|
|
|
|
v.(*Module).compiler.(libraryInterface).setRlib()
|
|
|
|
case dylibVariation:
|
|
|
|
v.(*Module).compiler.(libraryInterface).setDylib()
|
2021-04-02 18:41:32 +02:00
|
|
|
if v.(*Module).ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
|
2020-12-02 15:15:16 +01:00
|
|
|
// TODO(b/165791368)
|
2021-04-02 18:41:32 +02:00
|
|
|
// Disable dylib Vendor Ramdisk variations until we support these.
|
2020-12-02 15:15:16 +01:00
|
|
|
v.(*Module).Disable()
|
|
|
|
}
|
2021-05-20 19:39:16 +02:00
|
|
|
|
2020-09-23 18:10:17 +02:00
|
|
|
case "source":
|
|
|
|
v.(*Module).compiler.(libraryInterface).setSource()
|
|
|
|
// The source variant does not produce any library.
|
|
|
|
// Disable the compilation steps.
|
|
|
|
v.(*Module).compiler.SetDisabled()
|
|
|
|
}
|
|
|
|
}
|
2020-07-31 19:40:31 +02:00
|
|
|
|
2020-09-23 18:10:17 +02:00
|
|
|
// If a source variant is created, add an inter-variant dependency
|
|
|
|
// between the other variants and the source variant.
|
|
|
|
if sourceVariant {
|
|
|
|
sv := modules[0]
|
|
|
|
for _, v := range modules[1:] {
|
|
|
|
if !v.Enabled() {
|
|
|
|
continue
|
2020-07-31 19:40:31 +02:00
|
|
|
}
|
2020-09-23 18:10:17 +02:00
|
|
|
mctx.AddInterVariantDependency(sourceDepTag, v, sv)
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
2020-09-23 18:10:17 +02:00
|
|
|
// Alias the source variation so it can be named directly in "srcs" properties.
|
|
|
|
mctx.AliasVariation("source")
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-08 18:46:52 +02:00
|
|
|
|
|
|
|
func LibstdMutator(mctx android.BottomUpMutatorContext) {
|
|
|
|
if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
|
|
|
|
switch library := m.compiler.(type) {
|
|
|
|
case libraryInterface:
|
|
|
|
// Only create a variant if a library is actually being built.
|
|
|
|
if library.rlib() && !library.sysroot() {
|
|
|
|
variants := []string{"rlib-std", "dylib-std"}
|
|
|
|
modules := mctx.CreateLocalVariations(variants...)
|
|
|
|
|
|
|
|
rlib := modules[0].(*Module)
|
|
|
|
dylib := modules[1].(*Module)
|
|
|
|
rlib.compiler.(libraryInterface).setRlibStd()
|
|
|
|
dylib.compiler.(libraryInterface).setDylibStd()
|
2023-07-13 17:01:41 +02:00
|
|
|
if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
|
2020-12-02 15:15:16 +01:00
|
|
|
// TODO(b/165791368)
|
2023-07-13 17:01:41 +02:00
|
|
|
// Disable rlibs that link against dylib-std on vendor ramdisk variations until those dylib
|
2020-12-02 15:15:16 +01:00
|
|
|
// variants are properly supported.
|
|
|
|
dylib.Disable()
|
|
|
|
}
|
2021-04-02 18:41:32 +02:00
|
|
|
rlib.Properties.RustSubName += RlibStdlibSuffix
|
2020-09-08 18:46:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-20 19:39:16 +02:00
|
|
|
|
|
|
|
func (l *libraryDecorator) snapshotHeaders() android.Paths {
|
|
|
|
if l.collectedSnapshotHeaders == nil {
|
|
|
|
panic("snapshotHeaders() must be called after collectHeadersForSnapshot()")
|
|
|
|
}
|
|
|
|
return l.collectedSnapshotHeaders
|
|
|
|
}
|
|
|
|
|
|
|
|
// collectHeadersForSnapshot collects all exported headers from library.
|
|
|
|
// It globs header files in the source tree for exported include directories,
|
|
|
|
// and tracks generated header files separately.
|
|
|
|
//
|
|
|
|
// This is to be called from GenerateAndroidBuildActions, and then collected
|
|
|
|
// header files can be retrieved by snapshotHeaders().
|
|
|
|
func (l *libraryDecorator) collectHeadersForSnapshot(ctx android.ModuleContext, deps PathDeps) {
|
|
|
|
ret := android.Paths{}
|
|
|
|
|
|
|
|
// Glob together the headers from the modules include_dirs property
|
|
|
|
for _, path := range android.CopyOfPaths(l.includeDirs) {
|
|
|
|
dir := path.String()
|
2022-02-07 14:51:47 +01:00
|
|
|
globDir := dir + "/**/*"
|
|
|
|
glob, err := ctx.GlobWithDeps(globDir, nil)
|
2021-05-20 19:39:16 +02:00
|
|
|
if err != nil {
|
2022-02-07 14:51:47 +01:00
|
|
|
ctx.ModuleErrorf("glob of %q failed: %s", globDir, err)
|
2021-05-20 19:39:16 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, header := range glob {
|
|
|
|
// Filter out only the files with extensions that are headers.
|
|
|
|
found := false
|
|
|
|
for _, ext := range cc.HeaderExts {
|
|
|
|
if strings.HasSuffix(header, ext) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret = append(ret, android.PathForSource(ctx, header))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Glob together the headers from C dependencies as well, starting with non-generated headers.
|
|
|
|
ret = append(ret, cc.GlobHeadersForSnapshot(ctx, append(android.CopyOfPaths(deps.depIncludePaths), deps.depSystemIncludePaths...))...)
|
|
|
|
|
|
|
|
// Collect generated headers from C dependencies.
|
|
|
|
ret = append(ret, cc.GlobGeneratedHeadersForSnapshot(ctx, deps.depGeneratedHeaders)...)
|
|
|
|
|
|
|
|
// TODO(185577950): If support for generated headers is added, they need to be collected here as well.
|
|
|
|
l.collectedSnapshotHeaders = ret
|
|
|
|
}
|
2023-08-24 17:10:01 +02:00
|
|
|
|
|
|
|
type rustLibraryAttributes struct {
|
2023-08-24 17:10:01 +02:00
|
|
|
Srcs bazel.LabelListAttribute
|
|
|
|
Compile_data bazel.LabelListAttribute
|
|
|
|
Crate_name bazel.StringAttribute
|
|
|
|
Edition bazel.StringAttribute
|
|
|
|
Crate_features bazel.StringListAttribute
|
|
|
|
Deps bazel.LabelListAttribute
|
|
|
|
Rustc_flags bazel.StringListAttribute
|
|
|
|
Proc_macro_deps bazel.LabelListAttribute
|
2023-08-24 17:10:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func libraryBp2build(ctx android.TopDownMutatorContext, m *Module) {
|
|
|
|
lib := m.compiler.(*libraryDecorator)
|
2023-08-24 18:55:12 +02:00
|
|
|
|
2023-08-24 17:10:01 +02:00
|
|
|
srcs, compileData := srcsAndCompileDataAttrs(ctx, *lib.baseCompiler)
|
2023-08-24 18:55:12 +02:00
|
|
|
|
2023-08-24 17:10:01 +02:00
|
|
|
deps := android.BazelLabelForModuleDeps(ctx, append(
|
|
|
|
lib.baseCompiler.Properties.Rustlibs,
|
|
|
|
lib.baseCompiler.Properties.Rlibs...,
|
|
|
|
))
|
2023-08-24 18:55:12 +02:00
|
|
|
|
|
|
|
cargoBuildScript := cargoBuildScriptBp2build(ctx, m)
|
|
|
|
if cargoBuildScript != nil {
|
|
|
|
deps.Add(&bazel.Label{
|
|
|
|
Label: ":" + *cargoBuildScript,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-24 17:10:01 +02:00
|
|
|
procMacroDeps := android.BazelLabelForModuleDeps(ctx, lib.baseCompiler.Properties.Proc_macros)
|
2023-08-24 17:10:01 +02:00
|
|
|
|
|
|
|
var rustcFLags []string
|
|
|
|
for _, cfg := range lib.baseCompiler.Properties.Cfgs {
|
|
|
|
rustcFLags = append(rustcFLags, fmt.Sprintf("--cfg=%s", cfg))
|
|
|
|
}
|
|
|
|
|
|
|
|
attrs := &rustLibraryAttributes{
|
|
|
|
Srcs: bazel.MakeLabelListAttribute(
|
|
|
|
srcs,
|
|
|
|
),
|
|
|
|
Compile_data: bazel.MakeLabelListAttribute(
|
|
|
|
compileData,
|
|
|
|
),
|
|
|
|
Crate_name: bazel.StringAttribute{
|
|
|
|
Value: &lib.baseCompiler.Properties.Crate_name,
|
|
|
|
},
|
|
|
|
Edition: bazel.StringAttribute{
|
|
|
|
Value: lib.baseCompiler.Properties.Edition,
|
|
|
|
},
|
|
|
|
Crate_features: bazel.StringListAttribute{
|
|
|
|
Value: lib.baseCompiler.Properties.Features,
|
|
|
|
},
|
|
|
|
Deps: bazel.MakeLabelListAttribute(
|
|
|
|
deps,
|
|
|
|
),
|
2023-08-24 17:10:01 +02:00
|
|
|
Proc_macro_deps: bazel.MakeLabelListAttribute(
|
|
|
|
procMacroDeps,
|
|
|
|
),
|
2023-08-24 17:10:01 +02:00
|
|
|
Rustc_flags: bazel.StringListAttribute{
|
|
|
|
Value: append(
|
|
|
|
rustcFLags,
|
|
|
|
lib.baseCompiler.Properties.Flags...,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(b/290790800): Remove the restriction when rust toolchain for android is implemented
|
|
|
|
var restriction bazel.BoolAttribute
|
|
|
|
restriction.SetSelectValue(bazel.OsConfigurationAxis, "android", proptools.BoolPtr(false))
|
|
|
|
|
|
|
|
ctx.CreateBazelTargetModuleWithRestrictions(
|
|
|
|
bazel.BazelTargetModuleProperties{
|
|
|
|
Rule_class: "rust_library",
|
|
|
|
Bzl_load_location: "@rules_rust//rust:defs.bzl",
|
|
|
|
},
|
|
|
|
android.CommonAttributes{
|
|
|
|
Name: m.Name(),
|
|
|
|
},
|
|
|
|
attrs,
|
|
|
|
restriction,
|
|
|
|
)
|
|
|
|
}
|
2023-08-24 18:55:12 +02:00
|
|
|
|
|
|
|
type cargoBuildScriptAttributes struct {
|
|
|
|
Srcs bazel.LabelListAttribute
|
|
|
|
Edition bazel.StringAttribute
|
|
|
|
Version bazel.StringAttribute
|
|
|
|
}
|
|
|
|
|
|
|
|
func cargoBuildScriptBp2build(ctx android.TopDownMutatorContext, m *Module) *string {
|
|
|
|
// Soong treats some crates like libprotobuf as special in that they have
|
|
|
|
// cargo build script ran to produce an out folder and check it into AOSP
|
|
|
|
// For example, https://cs.android.com/android/platform/superproject/main/+/main:external/rust/crates/protobuf/out/
|
|
|
|
// is produced by cargo build script https://cs.android.com/android/platform/superproject/main/+/main:external/rust/crates/protobuf/build.rs
|
|
|
|
// The out folder is then fed into `rust_library` by a genrule
|
|
|
|
// https://cs.android.com/android/platform/superproject/main/+/main:external/rust/crates/protobuf/Android.bp;l=22
|
|
|
|
// This allows Soong to decouple from cargo completely.
|
|
|
|
|
|
|
|
// Soong decouples from cargo so that it has control over cc compilation.
|
|
|
|
// https://cs.android.com/android/platform/superproject/main/+/main:development/scripts/cargo2android.py;l=1033-1041;drc=8449944a50a0445a5ecaf9b7aed12608c81bf3f1
|
|
|
|
// generates a `cc_library_static` module to have custom cc flags.
|
|
|
|
// Since bp2build will convert the cc modules to cc targets which include the cflags,
|
|
|
|
// Bazel does not need to have this optimization.
|
|
|
|
|
|
|
|
// Performance-wise: rust_library -> cargo_build_script vs rust_library -> genrule (like Soong)
|
|
|
|
// don't have any major difference in build time in Bazel. So using cargo_build_script does not slow
|
|
|
|
// down the build.
|
|
|
|
|
|
|
|
// The benefit of using `cargo_build_script` here is that it would take care of setting correct
|
|
|
|
// `OUT_DIR` for us - similar to what Soong does here
|
|
|
|
// https://cs.android.com/android/platform/superproject/main/+/main:build/soong/rust/builder.go;l=202-218;drc=f29ca58e88c5846bbe8955e5192135e5ab4f14a1
|
|
|
|
|
|
|
|
// TODO(b/297364081): cargo2android.py has logic for when generate/not cc_library_static and out directory
|
|
|
|
// bp2build might be able use the same logic for when to use `cargo_build_script`.
|
|
|
|
// For now, we're building libprotobuf_build_script as a one-off until we have a more principled solution
|
|
|
|
if m.Name() != "libprotobuf" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lib := m.compiler.(*libraryDecorator)
|
|
|
|
|
|
|
|
name := m.Name() + "_build_script"
|
|
|
|
attrs := &cargoBuildScriptAttributes{
|
|
|
|
Srcs: bazel.MakeLabelListAttribute(
|
|
|
|
android.BazelLabelForModuleSrc(ctx, []string{"build.rs"}),
|
|
|
|
),
|
|
|
|
Edition: bazel.StringAttribute{
|
|
|
|
Value: lib.baseCompiler.Properties.Edition,
|
|
|
|
},
|
|
|
|
Version: bazel.StringAttribute{
|
|
|
|
Value: lib.baseCompiler.Properties.Cargo_pkg_version,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(b/290790800): Remove the restriction when rust toolchain for android is implemented
|
|
|
|
var restriction bazel.BoolAttribute
|
|
|
|
restriction.SetSelectValue(bazel.OsConfigurationAxis, "android", proptools.BoolPtr(false))
|
|
|
|
|
|
|
|
ctx.CreateBazelTargetModuleWithRestrictions(
|
|
|
|
bazel.BazelTargetModuleProperties{
|
|
|
|
Rule_class: "cargo_build_script",
|
|
|
|
Bzl_load_location: "@rules_rust//cargo:cargo_build_script.bzl",
|
|
|
|
},
|
|
|
|
android.CommonAttributes{
|
|
|
|
Name: name,
|
|
|
|
},
|
|
|
|
attrs,
|
|
|
|
restriction,
|
|
|
|
)
|
|
|
|
|
|
|
|
return &name
|
|
|
|
}
|