blob: d8d126d37c07ced981afe53f9f9f285a32263cde [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 Lozano3d947522020-09-23 13:26:25 -040024 cc_config "android/soong/cc/config"
Ivan Lozano4fef93c2020-07-08 08:39:44 -040025)
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.
Ivan Lozano54a8ee12020-09-08 13:10:36 -040031 bindgenClangVersion = "clang-r383902c"
Ivan Lozano4fef93c2020-07-08 08:39:44 -040032
33 //TODO(b/160803703) Use a prebuilt bindgen instead of the built bindgen.
34 _ = pctx.SourcePathVariable("bindgenCmd", "out/host/${config.HostPrebuiltTag}/bin/bindgen")
35 _ = pctx.SourcePathVariable("bindgenClang",
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020036 "${cc_config.ClangBase}/${config.HostPrebuiltTag}/"+bindgenClangVersion+"/bin/clang")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040037 _ = pctx.SourcePathVariable("bindgenLibClang",
Ivan Lozano54a8ee12020-09-08 13:10:36 -040038 "${cc_config.ClangBase}/${config.HostPrebuiltTag}/"+bindgenClangVersion+"/lib64/")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040039
40 //TODO(ivanlozano) Switch this to RuleBuilder
41 bindgen = pctx.AndroidStaticRule("bindgen",
42 blueprint.RuleParams{
Ivan Lozanoec54eec2020-07-22 16:48:53 -040043 Command: "CLANG_PATH=$bindgenClang LIBCLANG_PATH=$bindgenLibClang RUSTFMT=${config.RustBin}/rustfmt " +
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040044 "$cmd $flags $in -o $out -- -MD -MF $out.d $cflags",
45 CommandDeps: []string{"$cmd"},
Ivan Lozanoe1e844b2020-07-24 15:16:30 -040046 Deps: blueprint.DepsGCC,
47 Depfile: "$out.d",
Ivan Lozano4fef93c2020-07-08 08:39:44 -040048 },
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040049 "cmd", "flags", "cflags")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040050)
51
52func init() {
53 android.RegisterModuleType("rust_bindgen", RustBindgenFactory)
Ivan Lozanof6fe9952020-07-22 16:30:20 -040054 android.RegisterModuleType("rust_bindgen_host", RustBindgenHostFactory)
Ivan Lozano4fef93c2020-07-08 08:39:44 -040055}
56
57var _ SourceProvider = (*bindgenDecorator)(nil)
58
59type BindgenProperties struct {
Ivan Lozano3d947522020-09-23 13:26:25 -040060 // The wrapper header file. By default this is assumed to be a C header unless the extension is ".hh" or ".hpp".
61 // This is used to specify how to interpret the header and determines which '-std' flag to use by default.
62 //
63 // If your C++ header must have some other extension, then the default behavior can be overridden by setting the
64 // cpp_std property.
Ivan Lozano4fef93c2020-07-08 08:39:44 -040065 Wrapper_src *string `android:"path,arch_variant"`
66
67 // list of bindgen-specific flags and options
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040068 Bindgen_flags []string `android:"arch_variant"`
Ivan Lozano4fef93c2020-07-08 08:39:44 -040069
70 // list of clang flags required to correctly interpret the headers.
71 Cflags []string `android:"arch_variant"`
72
73 // list of directories relative to the Blueprints file that will
74 // be added to the include path using -I
75 Local_include_dirs []string `android:"arch_variant,variant_prepend"`
76
77 // list of static libraries that provide headers for this binding.
78 Static_libs []string `android:"arch_variant,variant_prepend"`
79
80 // list of shared libraries that provide headers for this binding.
81 Shared_libs []string `android:"arch_variant"`
82
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040083 // module name of a custom binary/script which should be used instead of the 'bindgen' binary. This custom
84 // binary must expect arguments in a similar fashion to bindgen, e.g.
85 //
86 // "my_bindgen [flags] wrapper_header.h -o [output_path] -- [clang flags]"
87 Custom_bindgen string `android:"path"`
88
Ivan Lozano3d947522020-09-23 13:26:25 -040089 // C standard version to use. Can be a specific version (such as "gnu11"),
90 // "experimental" (which will use draft versions like C1x when available),
91 // or the empty string (which will use the default).
92 //
93 // If this is set, the file extension will be ignored and this will be used as the std version value. Setting this
94 // to "default" will use the build system default version. This cannot be set at the same time as cpp_std.
95 C_std *string
96
97 // C++ standard version to use. Can be a specific version (such as
98 // "gnu++11"), "experimental" (which will use draft versions like C++1z when
99 // available), or the empty string (which will use the default).
100 //
101 // If this is set, the file extension will be ignored and this will be used as the std version value. Setting this
102 // to "default" will use the build system default version. This cannot be set at the same time as c_std.
103 Cpp_std *string
104
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400105 //TODO(b/161141999) Add support for headers from cc_library_header modules.
106}
107
108type bindgenDecorator struct {
Andrei Homescuc7767922020-08-05 06:36:19 -0700109 *BaseSourceProvider
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400110
111 Properties BindgenProperties
112}
113
Ivan Lozano3d947522020-09-23 13:26:25 -0400114func (b *bindgenDecorator) getStdVersion(ctx ModuleContext, src android.Path) (string, bool) {
115 // Assume headers are C headers
116 isCpp := false
117 stdVersion := ""
118
119 switch src.Ext() {
120 case ".hpp", ".hh":
121 isCpp = true
122 }
123
124 if String(b.Properties.Cpp_std) != "" && String(b.Properties.C_std) != "" {
125 ctx.PropertyErrorf("c_std", "c_std and cpp_std cannot both be defined at the same time.")
126 }
127
128 if String(b.Properties.Cpp_std) != "" {
129 if String(b.Properties.Cpp_std) == "experimental" {
130 stdVersion = cc_config.ExperimentalCppStdVersion
131 } else if String(b.Properties.Cpp_std) == "default" {
132 stdVersion = cc_config.CppStdVersion
133 } else {
134 stdVersion = String(b.Properties.Cpp_std)
135 }
136 } else if b.Properties.C_std != nil {
137 if String(b.Properties.C_std) == "experimental" {
138 stdVersion = cc_config.ExperimentalCStdVersion
139 } else if String(b.Properties.C_std) == "default" {
140 stdVersion = cc_config.CStdVersion
141 } else {
142 stdVersion = String(b.Properties.C_std)
143 }
144 } else if isCpp {
145 stdVersion = cc_config.CppStdVersion
146 } else {
147 stdVersion = cc_config.CStdVersion
148 }
149
150 return stdVersion, isCpp
151}
152
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200153func (b *bindgenDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path {
154 ccToolchain := ctx.RustModule().ccToolchain(ctx)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400155
156 var cflags []string
Ivan Lozano45901ed2020-07-24 16:05:01 -0400157 var implicits android.Paths
158
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400159 implicits = append(implicits, deps.depGeneratedHeaders...)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400160
161 // Default clang flags
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +0200162 cflags = append(cflags, "${cc_config.CommonClangGlobalCflags}")
Ivan Lozano45901ed2020-07-24 16:05:01 -0400163 if ctx.Device() {
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +0200164 cflags = append(cflags, "${cc_config.DeviceClangGlobalCflags}")
Ivan Lozano45901ed2020-07-24 16:05:01 -0400165 }
166
167 // Toolchain clang flags
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400168 cflags = append(cflags, "-target "+ccToolchain.ClangTriple())
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +0200169 cflags = append(cflags, strings.ReplaceAll(ccToolchain.ToolchainClangCflags(), "${config.", "${cc_config."))
Ivan Lozano45901ed2020-07-24 16:05:01 -0400170
171 // Dependency clang flags and include paths
172 cflags = append(cflags, deps.depClangFlags...)
173 for _, include := range deps.depIncludePaths {
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400174 cflags = append(cflags, "-I"+include.String())
175 }
Ivan Lozano45901ed2020-07-24 16:05:01 -0400176 for _, include := range deps.depSystemIncludePaths {
177 cflags = append(cflags, "-isystem "+include.String())
178 }
179
Stephen Crane12e2cb72020-08-04 12:26:10 -0700180 esc := proptools.NinjaAndShellEscapeList
181
Ivan Lozano45901ed2020-07-24 16:05:01 -0400182 // Module defined clang flags and include paths
Stephen Crane12e2cb72020-08-04 12:26:10 -0700183 cflags = append(cflags, esc(b.Properties.Cflags)...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400184 for _, include := range b.Properties.Local_include_dirs {
185 cflags = append(cflags, "-I"+android.PathForModuleSrc(ctx, include).String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400186 implicits = append(implicits, android.PathForModuleSrc(ctx, include))
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400187 }
188
189 bindgenFlags := defaultBindgenFlags
Stephen Crane12e2cb72020-08-04 12:26:10 -0700190 bindgenFlags = append(bindgenFlags, esc(b.Properties.Bindgen_flags)...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400191
192 wrapperFile := android.OptionalPathForModuleSrc(ctx, b.Properties.Wrapper_src)
193 if !wrapperFile.Valid() {
194 ctx.PropertyErrorf("wrapper_src", "invalid path to wrapper source")
195 }
196
Ivan Lozano3d947522020-09-23 13:26:25 -0400197 // Add C std version flag
198 stdVersion, isCpp := b.getStdVersion(ctx, wrapperFile.Path())
199 cflags = append(cflags, "-std="+stdVersion)
200
201 // Specify the header source language to avoid ambiguity.
202 if isCpp {
203 cflags = append(cflags, "-x c++")
204 } else {
205 cflags = append(cflags, "-x c")
206 }
207
Andrei Homescuc7767922020-08-05 06:36:19 -0700208 outputFile := android.PathForModuleOut(ctx, b.BaseSourceProvider.getStem(ctx)+".rs")
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400209
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400210 var cmd, cmdDesc string
211 if b.Properties.Custom_bindgen != "" {
212 cmd = ctx.GetDirectDepWithTag(b.Properties.Custom_bindgen, customBindgenDepTag).(*Module).HostToolPath().String()
213 cmdDesc = b.Properties.Custom_bindgen
214 } else {
215 cmd = "$bindgenCmd"
216 cmdDesc = "bindgen"
217 }
218
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400219 ctx.Build(pctx, android.BuildParams{
220 Rule: bindgen,
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400221 Description: strings.Join([]string{cmdDesc, wrapperFile.Path().Rel()}, " "),
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400222 Output: outputFile,
223 Input: wrapperFile.Path(),
Ivan Lozano45901ed2020-07-24 16:05:01 -0400224 Implicits: implicits,
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400225 Args: map[string]string{
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400226 "cmd": cmd,
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400227 "flags": strings.Join(bindgenFlags, " "),
228 "cflags": strings.Join(cflags, " "),
229 },
230 })
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400231
Andrei Homescuc7767922020-08-05 06:36:19 -0700232 b.BaseSourceProvider.OutputFile = outputFile
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400233 return outputFile
234}
235
Andrei Homescuc7767922020-08-05 06:36:19 -0700236func (b *bindgenDecorator) SourceProviderProps() []interface{} {
237 return append(b.BaseSourceProvider.SourceProviderProps(),
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400238 &b.Properties)
239}
240
Ivan Lozano10735d92020-07-22 09:14:47 -0400241// rust_bindgen generates Rust FFI bindings to C libraries using bindgen given a wrapper header as the primary input.
242// Bindgen has a number of flags to control the generated source, and additional flags can be passed to clang to ensure
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200243// the header and generated source is appropriately handled. It is recommended to add it as a dependency in the
244// rlibs, dylibs or rustlibs property. It may also be added in the srcs property for external crates, using the ":"
245// prefix.
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400246func RustBindgenFactory() android.Module {
247 module, _ := NewRustBindgen(android.HostAndDeviceSupported)
248 return module.Init()
249}
250
Ivan Lozanof6fe9952020-07-22 16:30:20 -0400251func RustBindgenHostFactory() android.Module {
252 module, _ := NewRustBindgen(android.HostSupported)
253 return module.Init()
254}
255
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400256func NewRustBindgen(hod android.HostOrDeviceSupported) (*Module, *bindgenDecorator) {
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400257 bindgen := &bindgenDecorator{
Andrei Homescuc7767922020-08-05 06:36:19 -0700258 BaseSourceProvider: NewSourceProvider(),
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400259 Properties: BindgenProperties{},
260 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400261
Andrei Homescuc7767922020-08-05 06:36:19 -0700262 module := NewSourceProviderModule(hod, bindgen, false)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400263
264 return module, bindgen
265}
266
Andrei Homescuc7767922020-08-05 06:36:19 -0700267func (b *bindgenDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps {
268 deps = b.BaseSourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400269 if ctx.toolchain().Bionic() {
270 deps = bionicDeps(deps)
271 }
272
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400273 deps.SharedLibs = append(deps.SharedLibs, b.Properties.Shared_libs...)
274 deps.StaticLibs = append(deps.StaticLibs, b.Properties.Static_libs...)
275 return deps
276}