blob: 9b09e616e058cf499e0a3e9e44c51bd334cd9f70 [file] [log] [blame]
Ivan Lozano4fef93c2020-07-08 08:39:44 -04001// Copyright 2020 The Android Open Source Project
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
15package rust
16
17import (
Ivan Lozano4fef93c2020-07-08 08:39:44 -040018 "strings"
19
Ivan Lozano45901ed2020-07-24 16:05:01 -040020 "github.com/google/blueprint"
Stephen Crane12e2cb72020-08-04 12:26:10 -070021 "github.com/google/blueprint/proptools"
Ivan Lozano45901ed2020-07-24 16:05:01 -040022
Ivan Lozano4fef93c2020-07-08 08:39:44 -040023 "android/soong/android"
Ivan Lozano4fef93c2020-07-08 08:39:44 -040024 ccConfig "android/soong/cc/config"
25)
26
27var (
Ivan Lozanoec54eec2020-07-22 16:48:53 -040028 defaultBindgenFlags = []string{""}
Ivan Lozano4fef93c2020-07-08 08:39:44 -040029
30 // bindgen should specify its own Clang revision so updating Clang isn't potentially blocked on bindgen failures.
31 bindgenClangVersion = "clang-r383902c"
32 bindgenLibClangSoGit = "11git"
33
34 //TODO(b/160803703) Use a prebuilt bindgen instead of the built bindgen.
35 _ = pctx.SourcePathVariable("bindgenCmd", "out/host/${config.HostPrebuiltTag}/bin/bindgen")
36 _ = pctx.SourcePathVariable("bindgenClang",
37 "${ccConfig.ClangBase}/${config.HostPrebuiltTag}/"+bindgenClangVersion+"/bin/clang")
38 _ = pctx.SourcePathVariable("bindgenLibClang",
39 "${ccConfig.ClangBase}/${config.HostPrebuiltTag}/"+bindgenClangVersion+"/lib64/libclang.so."+bindgenLibClangSoGit)
40
41 //TODO(ivanlozano) Switch this to RuleBuilder
42 bindgen = pctx.AndroidStaticRule("bindgen",
43 blueprint.RuleParams{
Ivan Lozanoec54eec2020-07-22 16:48:53 -040044 Command: "CLANG_PATH=$bindgenClang LIBCLANG_PATH=$bindgenLibClang RUSTFMT=${config.RustBin}/rustfmt " +
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040045 "$cmd $flags $in -o $out -- -MD -MF $out.d $cflags",
46 CommandDeps: []string{"$cmd"},
Ivan Lozanoe1e844b2020-07-24 15:16:30 -040047 Deps: blueprint.DepsGCC,
48 Depfile: "$out.d",
Ivan Lozano4fef93c2020-07-08 08:39:44 -040049 },
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040050 "cmd", "flags", "cflags")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040051)
52
53func init() {
54 android.RegisterModuleType("rust_bindgen", RustBindgenFactory)
Ivan Lozanof6fe9952020-07-22 16:30:20 -040055 android.RegisterModuleType("rust_bindgen_host", RustBindgenHostFactory)
Ivan Lozano4fef93c2020-07-08 08:39:44 -040056}
57
58var _ SourceProvider = (*bindgenDecorator)(nil)
59
60type BindgenProperties struct {
61 // The wrapper header file
62 Wrapper_src *string `android:"path,arch_variant"`
63
64 // list of bindgen-specific flags and options
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040065 Bindgen_flags []string `android:"arch_variant"`
Ivan Lozano4fef93c2020-07-08 08:39:44 -040066
67 // list of clang flags required to correctly interpret the headers.
68 Cflags []string `android:"arch_variant"`
69
70 // list of directories relative to the Blueprints file that will
71 // be added to the include path using -I
72 Local_include_dirs []string `android:"arch_variant,variant_prepend"`
73
74 // list of static libraries that provide headers for this binding.
75 Static_libs []string `android:"arch_variant,variant_prepend"`
76
77 // list of shared libraries that provide headers for this binding.
78 Shared_libs []string `android:"arch_variant"`
79
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040080 // module name of a custom binary/script which should be used instead of the 'bindgen' binary. This custom
81 // binary must expect arguments in a similar fashion to bindgen, e.g.
82 //
83 // "my_bindgen [flags] wrapper_header.h -o [output_path] -- [clang flags]"
84 Custom_bindgen string `android:"path"`
85
Ivan Lozano4fef93c2020-07-08 08:39:44 -040086 //TODO(b/161141999) Add support for headers from cc_library_header modules.
87}
88
89type bindgenDecorator struct {
Andrei Homescuc7767922020-08-05 06:36:19 -070090 *BaseSourceProvider
Ivan Lozano4fef93c2020-07-08 08:39:44 -040091
92 Properties BindgenProperties
93}
94
Andrei Homescuc7767922020-08-05 06:36:19 -070095func (b *bindgenDecorator) GenerateSource(ctx android.ModuleContext, deps PathDeps) android.Path {
Ivan Lozano4fef93c2020-07-08 08:39:44 -040096 ccToolchain := ccConfig.FindToolchain(ctx.Os(), ctx.Arch())
Ivan Lozano4fef93c2020-07-08 08:39:44 -040097
98 var cflags []string
Ivan Lozano45901ed2020-07-24 16:05:01 -040099 var implicits android.Paths
100
101 implicits = append(implicits, deps.depIncludePaths...)
102 implicits = append(implicits, deps.depSystemIncludePaths...)
103
104 // Default clang flags
105 cflags = append(cflags, "${ccConfig.CommonClangGlobalCflags}")
106 if ctx.Device() {
107 cflags = append(cflags, "${ccConfig.DeviceClangGlobalCflags}")
108 }
109
110 // Toolchain clang flags
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400111 cflags = append(cflags, "-target "+ccToolchain.ClangTriple())
112 cflags = append(cflags, strings.ReplaceAll(ccToolchain.ToolchainClangCflags(), "${config.", "${ccConfig."))
Ivan Lozano45901ed2020-07-24 16:05:01 -0400113
114 // Dependency clang flags and include paths
115 cflags = append(cflags, deps.depClangFlags...)
116 for _, include := range deps.depIncludePaths {
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400117 cflags = append(cflags, "-I"+include.String())
118 }
Ivan Lozano45901ed2020-07-24 16:05:01 -0400119 for _, include := range deps.depSystemIncludePaths {
120 cflags = append(cflags, "-isystem "+include.String())
121 }
122
Stephen Crane12e2cb72020-08-04 12:26:10 -0700123 esc := proptools.NinjaAndShellEscapeList
124
Ivan Lozano45901ed2020-07-24 16:05:01 -0400125 // Module defined clang flags and include paths
Stephen Crane12e2cb72020-08-04 12:26:10 -0700126 cflags = append(cflags, esc(b.Properties.Cflags)...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400127 for _, include := range b.Properties.Local_include_dirs {
128 cflags = append(cflags, "-I"+android.PathForModuleSrc(ctx, include).String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400129 implicits = append(implicits, android.PathForModuleSrc(ctx, include))
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400130 }
131
132 bindgenFlags := defaultBindgenFlags
Stephen Crane12e2cb72020-08-04 12:26:10 -0700133 bindgenFlags = append(bindgenFlags, esc(b.Properties.Bindgen_flags)...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400134
135 wrapperFile := android.OptionalPathForModuleSrc(ctx, b.Properties.Wrapper_src)
136 if !wrapperFile.Valid() {
137 ctx.PropertyErrorf("wrapper_src", "invalid path to wrapper source")
138 }
139
Andrei Homescuc7767922020-08-05 06:36:19 -0700140 outputFile := android.PathForModuleOut(ctx, b.BaseSourceProvider.getStem(ctx)+".rs")
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400141
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400142 var cmd, cmdDesc string
143 if b.Properties.Custom_bindgen != "" {
144 cmd = ctx.GetDirectDepWithTag(b.Properties.Custom_bindgen, customBindgenDepTag).(*Module).HostToolPath().String()
145 cmdDesc = b.Properties.Custom_bindgen
146 } else {
147 cmd = "$bindgenCmd"
148 cmdDesc = "bindgen"
149 }
150
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400151 ctx.Build(pctx, android.BuildParams{
152 Rule: bindgen,
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400153 Description: strings.Join([]string{cmdDesc, wrapperFile.Path().Rel()}, " "),
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400154 Output: outputFile,
155 Input: wrapperFile.Path(),
Ivan Lozano45901ed2020-07-24 16:05:01 -0400156 Implicits: implicits,
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400157 Args: map[string]string{
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400158 "cmd": cmd,
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400159 "flags": strings.Join(bindgenFlags, " "),
160 "cflags": strings.Join(cflags, " "),
161 },
162 })
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400163
Andrei Homescuc7767922020-08-05 06:36:19 -0700164 b.BaseSourceProvider.OutputFile = outputFile
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400165 return outputFile
166}
167
Andrei Homescuc7767922020-08-05 06:36:19 -0700168func (b *bindgenDecorator) SourceProviderProps() []interface{} {
169 return append(b.BaseSourceProvider.SourceProviderProps(),
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400170 &b.Properties)
171}
172
Ivan Lozano10735d92020-07-22 09:14:47 -0400173// rust_bindgen generates Rust FFI bindings to C libraries using bindgen given a wrapper header as the primary input.
174// Bindgen has a number of flags to control the generated source, and additional flags can be passed to clang to ensure
175// the header and generated source is appropriately handled.
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400176func RustBindgenFactory() android.Module {
177 module, _ := NewRustBindgen(android.HostAndDeviceSupported)
178 return module.Init()
179}
180
Ivan Lozanof6fe9952020-07-22 16:30:20 -0400181func RustBindgenHostFactory() android.Module {
182 module, _ := NewRustBindgen(android.HostSupported)
183 return module.Init()
184}
185
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400186func NewRustBindgen(hod android.HostOrDeviceSupported) (*Module, *bindgenDecorator) {
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400187 bindgen := &bindgenDecorator{
Andrei Homescuc7767922020-08-05 06:36:19 -0700188 BaseSourceProvider: NewSourceProvider(),
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400189 Properties: BindgenProperties{},
190 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400191
Andrei Homescuc7767922020-08-05 06:36:19 -0700192 module := NewSourceProviderModule(hod, bindgen, false)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400193
194 return module, bindgen
195}
196
Andrei Homescuc7767922020-08-05 06:36:19 -0700197func (b *bindgenDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps {
198 deps = b.BaseSourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400199 if ctx.toolchain().Bionic() {
200 deps = bionicDeps(deps)
201 }
202
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400203 deps.SharedLibs = append(deps.SharedLibs, b.Properties.Shared_libs...)
204 deps.StaticLibs = append(deps.StaticLibs, b.Properties.Static_libs...)
205 return deps
206}