2020-07-08 14:39:44 +02:00
|
|
|
// Copyright 2020 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 (
|
2021-11-09 17:26:04 +01:00
|
|
|
"fmt"
|
2020-07-08 14:39:44 +02:00
|
|
|
"strings"
|
|
|
|
|
2020-07-24 22:05:01 +02:00
|
|
|
"github.com/google/blueprint"
|
2020-08-04 21:26:10 +02:00
|
|
|
"github.com/google/blueprint/proptools"
|
2020-07-24 22:05:01 +02:00
|
|
|
|
2020-07-08 14:39:44 +02:00
|
|
|
"android/soong/android"
|
2020-09-25 22:08:34 +02:00
|
|
|
"android/soong/cc"
|
2020-09-23 19:26:25 +02:00
|
|
|
cc_config "android/soong/cc/config"
|
2020-07-08 14:39:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-07-22 22:48:53 +02:00
|
|
|
defaultBindgenFlags = []string{""}
|
2020-07-08 14:39:44 +02:00
|
|
|
|
|
|
|
// bindgen should specify its own Clang revision so updating Clang isn't potentially blocked on bindgen failures.
|
2022-02-23 20:07:58 +01:00
|
|
|
bindgenClangVersion = "clang-r445002"
|
2020-07-08 14:39:44 +02:00
|
|
|
|
2020-12-31 05:23:01 +01:00
|
|
|
_ = pctx.VariableFunc("bindgenClangVersion", func(ctx android.PackageVarContext) string {
|
|
|
|
if override := ctx.Config().Getenv("LLVM_BINDGEN_PREBUILTS_VERSION"); override != "" {
|
|
|
|
return override
|
|
|
|
}
|
|
|
|
return bindgenClangVersion
|
|
|
|
})
|
|
|
|
|
2020-07-08 14:39:44 +02:00
|
|
|
//TODO(b/160803703) Use a prebuilt bindgen instead of the built bindgen.
|
2020-11-05 14:20:17 +01:00
|
|
|
_ = pctx.HostBinToolVariable("bindgenCmd", "bindgen")
|
2020-07-08 14:39:44 +02:00
|
|
|
_ = pctx.SourcePathVariable("bindgenClang",
|
2020-12-31 05:23:01 +01:00
|
|
|
"${cc_config.ClangBase}/${config.HostPrebuiltTag}/${bindgenClangVersion}/bin/clang")
|
2020-07-08 14:39:44 +02:00
|
|
|
_ = pctx.SourcePathVariable("bindgenLibClang",
|
2020-12-31 05:23:01 +01:00
|
|
|
"${cc_config.ClangBase}/${config.HostPrebuiltTag}/${bindgenClangVersion}/lib64/")
|
2020-07-08 14:39:44 +02:00
|
|
|
|
|
|
|
//TODO(ivanlozano) Switch this to RuleBuilder
|
|
|
|
bindgen = pctx.AndroidStaticRule("bindgen",
|
|
|
|
blueprint.RuleParams{
|
2020-07-22 22:48:53 +02:00
|
|
|
Command: "CLANG_PATH=$bindgenClang LIBCLANG_PATH=$bindgenLibClang RUSTFMT=${config.RustBin}/rustfmt " +
|
2020-08-04 21:43:37 +02:00
|
|
|
"$cmd $flags $in -o $out -- -MD -MF $out.d $cflags",
|
|
|
|
CommandDeps: []string{"$cmd"},
|
2020-07-24 21:16:30 +02:00
|
|
|
Deps: blueprint.DepsGCC,
|
|
|
|
Depfile: "$out.d",
|
2020-07-08 14:39:44 +02:00
|
|
|
},
|
2020-08-04 21:43:37 +02:00
|
|
|
"cmd", "flags", "cflags")
|
2020-07-08 14:39:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
android.RegisterModuleType("rust_bindgen", RustBindgenFactory)
|
2020-07-22 22:30:20 +02:00
|
|
|
android.RegisterModuleType("rust_bindgen_host", RustBindgenHostFactory)
|
2020-07-08 14:39:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ SourceProvider = (*bindgenDecorator)(nil)
|
|
|
|
|
|
|
|
type BindgenProperties struct {
|
2020-09-23 19:26:25 +02:00
|
|
|
// The wrapper header file. By default this is assumed to be a C header unless the extension is ".hh" or ".hpp".
|
|
|
|
// This is used to specify how to interpret the header and determines which '-std' flag to use by default.
|
|
|
|
//
|
|
|
|
// If your C++ header must have some other extension, then the default behavior can be overridden by setting the
|
|
|
|
// cpp_std property.
|
2020-07-08 14:39:44 +02:00
|
|
|
Wrapper_src *string `android:"path,arch_variant"`
|
|
|
|
|
|
|
|
// list of bindgen-specific flags and options
|
2020-07-31 19:40:31 +02:00
|
|
|
Bindgen_flags []string `android:"arch_variant"`
|
2020-07-08 14:39:44 +02:00
|
|
|
|
2020-08-04 21:43:37 +02:00
|
|
|
// module name of a custom binary/script which should be used instead of the 'bindgen' binary. This custom
|
|
|
|
// binary must expect arguments in a similar fashion to bindgen, e.g.
|
|
|
|
//
|
|
|
|
// "my_bindgen [flags] wrapper_header.h -o [output_path] -- [clang flags]"
|
2021-04-20 16:15:41 +02:00
|
|
|
Custom_bindgen string
|
2020-07-08 14:39:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type bindgenDecorator struct {
|
2020-08-05 15:36:19 +02:00
|
|
|
*BaseSourceProvider
|
2020-07-08 14:39:44 +02:00
|
|
|
|
2020-09-25 22:08:34 +02:00
|
|
|
Properties BindgenProperties
|
|
|
|
ClangProperties cc.RustBindgenClangProperties
|
2020-07-08 14:39:44 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 19:26:25 +02:00
|
|
|
func (b *bindgenDecorator) getStdVersion(ctx ModuleContext, src android.Path) (string, bool) {
|
|
|
|
// Assume headers are C headers
|
|
|
|
isCpp := false
|
|
|
|
stdVersion := ""
|
|
|
|
|
|
|
|
switch src.Ext() {
|
|
|
|
case ".hpp", ".hh":
|
|
|
|
isCpp = true
|
|
|
|
}
|
|
|
|
|
2020-09-25 22:08:34 +02:00
|
|
|
if String(b.ClangProperties.Cpp_std) != "" && String(b.ClangProperties.C_std) != "" {
|
2020-09-23 19:26:25 +02:00
|
|
|
ctx.PropertyErrorf("c_std", "c_std and cpp_std cannot both be defined at the same time.")
|
|
|
|
}
|
|
|
|
|
2020-09-25 22:08:34 +02:00
|
|
|
if String(b.ClangProperties.Cpp_std) != "" {
|
|
|
|
if String(b.ClangProperties.Cpp_std) == "experimental" {
|
2020-09-23 19:26:25 +02:00
|
|
|
stdVersion = cc_config.ExperimentalCppStdVersion
|
2020-09-25 22:08:34 +02:00
|
|
|
} else if String(b.ClangProperties.Cpp_std) == "default" {
|
2020-09-23 19:26:25 +02:00
|
|
|
stdVersion = cc_config.CppStdVersion
|
|
|
|
} else {
|
2020-09-25 22:08:34 +02:00
|
|
|
stdVersion = String(b.ClangProperties.Cpp_std)
|
2020-09-23 19:26:25 +02:00
|
|
|
}
|
2020-09-25 22:08:34 +02:00
|
|
|
} else if b.ClangProperties.C_std != nil {
|
|
|
|
if String(b.ClangProperties.C_std) == "experimental" {
|
2020-09-23 19:26:25 +02:00
|
|
|
stdVersion = cc_config.ExperimentalCStdVersion
|
2020-09-25 22:08:34 +02:00
|
|
|
} else if String(b.ClangProperties.C_std) == "default" {
|
2020-09-23 19:26:25 +02:00
|
|
|
stdVersion = cc_config.CStdVersion
|
|
|
|
} else {
|
2020-09-25 22:08:34 +02:00
|
|
|
stdVersion = String(b.ClangProperties.C_std)
|
2020-09-23 19:26:25 +02:00
|
|
|
}
|
|
|
|
} else if isCpp {
|
|
|
|
stdVersion = cc_config.CppStdVersion
|
|
|
|
} else {
|
|
|
|
stdVersion = cc_config.CStdVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdVersion, isCpp
|
|
|
|
}
|
|
|
|
|
2020-08-27 13:37:29 +02:00
|
|
|
func (b *bindgenDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path {
|
|
|
|
ccToolchain := ctx.RustModule().ccToolchain(ctx)
|
2020-07-08 14:39:44 +02:00
|
|
|
|
2020-07-24 22:05:01 +02:00
|
|
|
var cflags []string
|
|
|
|
var implicits android.Paths
|
2020-07-08 14:39:44 +02:00
|
|
|
|
2020-08-28 23:00:26 +02:00
|
|
|
implicits = append(implicits, deps.depGeneratedHeaders...)
|
2020-07-08 14:39:44 +02:00
|
|
|
|
2020-07-24 22:05:01 +02:00
|
|
|
// Default clang flags
|
2021-07-15 03:45:05 +02:00
|
|
|
cflags = append(cflags, "${cc_config.CommonGlobalCflags}")
|
2020-07-24 22:05:01 +02:00
|
|
|
if ctx.Device() {
|
2021-07-15 03:45:05 +02:00
|
|
|
cflags = append(cflags, "${cc_config.DeviceGlobalCflags}")
|
2020-07-24 22:05:01 +02:00
|
|
|
}
|
2020-07-08 14:39:44 +02:00
|
|
|
|
2020-07-24 22:05:01 +02:00
|
|
|
// Toolchain clang flags
|
2020-07-08 14:39:44 +02:00
|
|
|
cflags = append(cflags, "-target "+ccToolchain.ClangTriple())
|
2021-07-15 02:03:16 +02:00
|
|
|
cflags = append(cflags, strings.ReplaceAll(ccToolchain.Cflags(), "${config.", "${cc_config."))
|
|
|
|
cflags = append(cflags, strings.ReplaceAll(ccToolchain.ToolchainCflags(), "${config.", "${cc_config."))
|
2020-07-24 22:05:01 +02:00
|
|
|
|
2021-11-09 17:26:04 +01:00
|
|
|
if ctx.RustModule().UseVndk() {
|
|
|
|
cflags = append(cflags, "-D__ANDROID_VNDK__")
|
|
|
|
if ctx.RustModule().InVendor() {
|
|
|
|
cflags = append(cflags, "-D__ANDROID_VENDOR__")
|
|
|
|
} else if ctx.RustModule().InProduct() {
|
|
|
|
cflags = append(cflags, "-D__ANDROID_PRODUCT__")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.RustModule().InRecovery() {
|
|
|
|
cflags = append(cflags, "-D__ANDROID_RECOVERY__")
|
|
|
|
}
|
|
|
|
|
|
|
|
if mctx, ok := ctx.(*moduleContext); ok && mctx.apexVariationName() != "" {
|
|
|
|
cflags = append(cflags, "-D__ANDROID_APEX__")
|
|
|
|
if ctx.Device() {
|
|
|
|
cflags = append(cflags, fmt.Sprintf("-D__ANDROID_APEX_MIN_SDK_VERSION__=%d",
|
|
|
|
ctx.RustModule().apexSdkVersion.FinalOrFutureInt()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
|
|
|
|
cflags = append(cflags, "-D__ANDROID_NATIVE_BRIDGE__")
|
|
|
|
}
|
|
|
|
|
2020-07-24 22:05:01 +02:00
|
|
|
// Dependency clang flags and include paths
|
|
|
|
cflags = append(cflags, deps.depClangFlags...)
|
|
|
|
for _, include := range deps.depIncludePaths {
|
2020-07-08 14:39:44 +02:00
|
|
|
cflags = append(cflags, "-I"+include.String())
|
|
|
|
}
|
2020-07-24 22:05:01 +02:00
|
|
|
for _, include := range deps.depSystemIncludePaths {
|
|
|
|
cflags = append(cflags, "-isystem "+include.String())
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:26:10 +02:00
|
|
|
esc := proptools.NinjaAndShellEscapeList
|
|
|
|
|
2020-10-16 16:49:08 +02:00
|
|
|
// Filter out invalid cflags
|
|
|
|
for _, flag := range b.ClangProperties.Cflags {
|
|
|
|
if flag == "-x c++" || flag == "-xc++" {
|
|
|
|
ctx.PropertyErrorf("cflags",
|
|
|
|
"-x c++ should not be specified in cflags; setting cpp_std specifies this is a C++ header, or change the file extension to '.hpp' or '.hh'")
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(flag, "-std=") {
|
|
|
|
ctx.PropertyErrorf("cflags",
|
|
|
|
"-std should not be specified in cflags; instead use c_std or cpp_std")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 22:05:01 +02:00
|
|
|
// Module defined clang flags and include paths
|
2020-09-25 22:08:34 +02:00
|
|
|
cflags = append(cflags, esc(b.ClangProperties.Cflags)...)
|
|
|
|
for _, include := range b.ClangProperties.Local_include_dirs {
|
2020-07-08 14:39:44 +02:00
|
|
|
cflags = append(cflags, "-I"+android.PathForModuleSrc(ctx, include).String())
|
2020-07-24 22:05:01 +02:00
|
|
|
implicits = append(implicits, android.PathForModuleSrc(ctx, include))
|
2020-07-08 14:39:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bindgenFlags := defaultBindgenFlags
|
2020-08-04 21:26:10 +02:00
|
|
|
bindgenFlags = append(bindgenFlags, esc(b.Properties.Bindgen_flags)...)
|
2020-07-08 14:39:44 +02:00
|
|
|
|
|
|
|
wrapperFile := android.OptionalPathForModuleSrc(ctx, b.Properties.Wrapper_src)
|
|
|
|
if !wrapperFile.Valid() {
|
|
|
|
ctx.PropertyErrorf("wrapper_src", "invalid path to wrapper source")
|
|
|
|
}
|
|
|
|
|
2020-09-23 19:26:25 +02:00
|
|
|
// Add C std version flag
|
|
|
|
stdVersion, isCpp := b.getStdVersion(ctx, wrapperFile.Path())
|
|
|
|
cflags = append(cflags, "-std="+stdVersion)
|
|
|
|
|
|
|
|
// Specify the header source language to avoid ambiguity.
|
|
|
|
if isCpp {
|
|
|
|
cflags = append(cflags, "-x c++")
|
2020-09-25 22:08:34 +02:00
|
|
|
// Add any C++ only flags.
|
|
|
|
cflags = append(cflags, esc(b.ClangProperties.Cppflags)...)
|
2020-09-23 19:26:25 +02:00
|
|
|
} else {
|
|
|
|
cflags = append(cflags, "-x c")
|
|
|
|
}
|
|
|
|
|
2020-08-05 15:36:19 +02:00
|
|
|
outputFile := android.PathForModuleOut(ctx, b.BaseSourceProvider.getStem(ctx)+".rs")
|
2020-07-08 14:39:44 +02:00
|
|
|
|
2020-08-04 21:43:37 +02:00
|
|
|
var cmd, cmdDesc string
|
|
|
|
if b.Properties.Custom_bindgen != "" {
|
|
|
|
cmd = ctx.GetDirectDepWithTag(b.Properties.Custom_bindgen, customBindgenDepTag).(*Module).HostToolPath().String()
|
|
|
|
cmdDesc = b.Properties.Custom_bindgen
|
|
|
|
} else {
|
|
|
|
cmd = "$bindgenCmd"
|
|
|
|
cmdDesc = "bindgen"
|
|
|
|
}
|
|
|
|
|
2020-07-08 14:39:44 +02:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: bindgen,
|
2020-08-04 21:43:37 +02:00
|
|
|
Description: strings.Join([]string{cmdDesc, wrapperFile.Path().Rel()}, " "),
|
2020-07-08 14:39:44 +02:00
|
|
|
Output: outputFile,
|
|
|
|
Input: wrapperFile.Path(),
|
2020-07-24 22:05:01 +02:00
|
|
|
Implicits: implicits,
|
2020-07-08 14:39:44 +02:00
|
|
|
Args: map[string]string{
|
2020-08-04 21:43:37 +02:00
|
|
|
"cmd": cmd,
|
2020-07-08 14:39:44 +02:00
|
|
|
"flags": strings.Join(bindgenFlags, " "),
|
|
|
|
"cflags": strings.Join(cflags, " "),
|
|
|
|
},
|
|
|
|
})
|
2020-08-04 21:43:37 +02:00
|
|
|
|
2020-10-02 06:25:05 +02:00
|
|
|
b.BaseSourceProvider.OutputFiles = android.Paths{outputFile}
|
2020-07-08 14:39:44 +02:00
|
|
|
return outputFile
|
|
|
|
}
|
|
|
|
|
2020-08-05 15:36:19 +02:00
|
|
|
func (b *bindgenDecorator) SourceProviderProps() []interface{} {
|
|
|
|
return append(b.BaseSourceProvider.SourceProviderProps(),
|
2020-09-25 22:08:34 +02:00
|
|
|
&b.Properties, &b.ClangProperties)
|
2020-07-08 14:39:44 +02:00
|
|
|
}
|
|
|
|
|
2020-07-22 15:14:47 +02:00
|
|
|
// rust_bindgen generates Rust FFI bindings to C libraries using bindgen given a wrapper header as the primary input.
|
|
|
|
// Bindgen has a number of flags to control the generated source, and additional flags can be passed to clang to ensure
|
2020-09-23 18:10:17 +02:00
|
|
|
// the header and generated source is appropriately handled. It is recommended to add it as a dependency in the
|
|
|
|
// rlibs, dylibs or rustlibs property. It may also be added in the srcs property for external crates, using the ":"
|
|
|
|
// prefix.
|
2020-07-08 14:39:44 +02:00
|
|
|
func RustBindgenFactory() android.Module {
|
|
|
|
module, _ := NewRustBindgen(android.HostAndDeviceSupported)
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
2020-07-22 22:30:20 +02:00
|
|
|
func RustBindgenHostFactory() android.Module {
|
|
|
|
module, _ := NewRustBindgen(android.HostSupported)
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
2020-07-08 14:39:44 +02:00
|
|
|
func NewRustBindgen(hod android.HostOrDeviceSupported) (*Module, *bindgenDecorator) {
|
|
|
|
bindgen := &bindgenDecorator{
|
2020-08-05 15:36:19 +02:00
|
|
|
BaseSourceProvider: NewSourceProvider(),
|
2020-07-08 14:39:44 +02:00
|
|
|
Properties: BindgenProperties{},
|
2020-09-25 22:08:34 +02:00
|
|
|
ClangProperties: cc.RustBindgenClangProperties{},
|
2020-07-08 14:39:44 +02:00
|
|
|
}
|
2020-07-31 19:40:31 +02:00
|
|
|
|
2020-08-05 15:36:19 +02:00
|
|
|
module := NewSourceProviderModule(hod, bindgen, false)
|
2020-07-08 14:39:44 +02:00
|
|
|
|
|
|
|
return module, bindgen
|
|
|
|
}
|
|
|
|
|
2020-08-05 15:36:19 +02:00
|
|
|
func (b *bindgenDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps {
|
|
|
|
deps = b.BaseSourceProvider.SourceProviderDeps(ctx, deps)
|
2020-07-24 22:05:01 +02:00
|
|
|
if ctx.toolchain().Bionic() {
|
2021-03-22 14:24:54 +01:00
|
|
|
deps = bionicDeps(ctx, deps, false)
|
2022-01-24 05:48:36 +01:00
|
|
|
} else if ctx.Os() == android.LinuxMusl {
|
|
|
|
deps = muslDeps(ctx, deps, false)
|
2020-07-24 22:05:01 +02:00
|
|
|
}
|
|
|
|
|
2020-09-25 22:08:34 +02:00
|
|
|
deps.SharedLibs = append(deps.SharedLibs, b.ClangProperties.Shared_libs...)
|
|
|
|
deps.StaticLibs = append(deps.StaticLibs, b.ClangProperties.Static_libs...)
|
2020-11-17 19:39:30 +01:00
|
|
|
deps.HeaderLibs = append(deps.StaticLibs, b.ClangProperties.Header_libs...)
|
2020-07-08 14:39:44 +02:00
|
|
|
return deps
|
|
|
|
}
|