blob: b4626a048391aa9af33c1787f0204b94f9170c45 [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 Lozanoe950cda2021-11-09 11:26:04 -050018 "fmt"
Ivan Lozano4fef93c2020-07-08 08:39:44 -040019 "strings"
20
Ivan Lozano45901ed2020-07-24 16:05:01 -040021 "github.com/google/blueprint"
Stephen Crane12e2cb72020-08-04 12:26:10 -070022 "github.com/google/blueprint/proptools"
Ivan Lozano45901ed2020-07-24 16:05:01 -040023
Ivan Lozano4fef93c2020-07-08 08:39:44 -040024 "android/soong/android"
Ivan Lozanobc9e4212020-09-25 16:08:34 -040025 "android/soong/cc"
Ivan Lozano3d947522020-09-23 13:26:25 -040026 cc_config "android/soong/cc/config"
Ivan Lozano4fef93c2020-07-08 08:39:44 -040027)
28
29var (
Ivan Lozanoec54eec2020-07-22 16:48:53 -040030 defaultBindgenFlags = []string{""}
Ivan Lozano4fef93c2020-07-08 08:39:44 -040031
32 // bindgen should specify its own Clang revision so updating Clang isn't potentially blocked on bindgen failures.
Yi Kongb3089e82022-05-02 23:52:44 +080033 bindgenClangVersion = "clang-r450784d"
Ivan Lozano4fef93c2020-07-08 08:39:44 -040034
Peter Collingbourne9a868f12020-12-30 20:23:01 -080035 _ = pctx.VariableFunc("bindgenClangVersion", func(ctx android.PackageVarContext) string {
36 if override := ctx.Config().Getenv("LLVM_BINDGEN_PREBUILTS_VERSION"); override != "" {
37 return override
38 }
39 return bindgenClangVersion
40 })
41
Ivan Lozano4fef93c2020-07-08 08:39:44 -040042 //TODO(b/160803703) Use a prebuilt bindgen instead of the built bindgen.
Shao-Chuan Leeaa3231c2020-11-05 22:20:17 +090043 _ = pctx.HostBinToolVariable("bindgenCmd", "bindgen")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040044 _ = pctx.SourcePathVariable("bindgenClang",
Peter Collingbourne9a868f12020-12-30 20:23:01 -080045 "${cc_config.ClangBase}/${config.HostPrebuiltTag}/${bindgenClangVersion}/bin/clang")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040046 _ = pctx.SourcePathVariable("bindgenLibClang",
Peter Collingbourne9a868f12020-12-30 20:23:01 -080047 "${cc_config.ClangBase}/${config.HostPrebuiltTag}/${bindgenClangVersion}/lib64/")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040048
49 //TODO(ivanlozano) Switch this to RuleBuilder
50 bindgen = pctx.AndroidStaticRule("bindgen",
51 blueprint.RuleParams{
Ivan Lozanoec54eec2020-07-22 16:48:53 -040052 Command: "CLANG_PATH=$bindgenClang LIBCLANG_PATH=$bindgenLibClang RUSTFMT=${config.RustBin}/rustfmt " +
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040053 "$cmd $flags $in -o $out -- -MD -MF $out.d $cflags",
54 CommandDeps: []string{"$cmd"},
Ivan Lozanoe1e844b2020-07-24 15:16:30 -040055 Deps: blueprint.DepsGCC,
56 Depfile: "$out.d",
Ivan Lozano4fef93c2020-07-08 08:39:44 -040057 },
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040058 "cmd", "flags", "cflags")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040059)
60
61func init() {
62 android.RegisterModuleType("rust_bindgen", RustBindgenFactory)
Ivan Lozanof6fe9952020-07-22 16:30:20 -040063 android.RegisterModuleType("rust_bindgen_host", RustBindgenHostFactory)
Ivan Lozano4fef93c2020-07-08 08:39:44 -040064}
65
66var _ SourceProvider = (*bindgenDecorator)(nil)
67
68type BindgenProperties struct {
Ivan Lozano3d947522020-09-23 13:26:25 -040069 // The wrapper header file. By default this is assumed to be a C header unless the extension is ".hh" or ".hpp".
70 // This is used to specify how to interpret the header and determines which '-std' flag to use by default.
71 //
72 // If your C++ header must have some other extension, then the default behavior can be overridden by setting the
73 // cpp_std property.
Ivan Lozano4fef93c2020-07-08 08:39:44 -040074 Wrapper_src *string `android:"path,arch_variant"`
75
76 // list of bindgen-specific flags and options
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040077 Bindgen_flags []string `android:"arch_variant"`
Ivan Lozano4fef93c2020-07-08 08:39:44 -040078
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040079 // module name of a custom binary/script which should be used instead of the 'bindgen' binary. This custom
80 // binary must expect arguments in a similar fashion to bindgen, e.g.
81 //
82 // "my_bindgen [flags] wrapper_header.h -o [output_path] -- [clang flags]"
Liz Kammereda93982021-04-20 10:15:41 -040083 Custom_bindgen string
Ivan Lozano4fef93c2020-07-08 08:39:44 -040084}
85
86type bindgenDecorator struct {
Andrei Homescuc7767922020-08-05 06:36:19 -070087 *BaseSourceProvider
Ivan Lozano4fef93c2020-07-08 08:39:44 -040088
Ivan Lozanobc9e4212020-09-25 16:08:34 -040089 Properties BindgenProperties
90 ClangProperties cc.RustBindgenClangProperties
Ivan Lozano4fef93c2020-07-08 08:39:44 -040091}
92
Ivan Lozano3d947522020-09-23 13:26:25 -040093func (b *bindgenDecorator) getStdVersion(ctx ModuleContext, src android.Path) (string, bool) {
94 // Assume headers are C headers
95 isCpp := false
96 stdVersion := ""
97
98 switch src.Ext() {
99 case ".hpp", ".hh":
100 isCpp = true
101 }
102
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400103 if String(b.ClangProperties.Cpp_std) != "" && String(b.ClangProperties.C_std) != "" {
Ivan Lozano3d947522020-09-23 13:26:25 -0400104 ctx.PropertyErrorf("c_std", "c_std and cpp_std cannot both be defined at the same time.")
105 }
106
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400107 if String(b.ClangProperties.Cpp_std) != "" {
108 if String(b.ClangProperties.Cpp_std) == "experimental" {
Ivan Lozano3d947522020-09-23 13:26:25 -0400109 stdVersion = cc_config.ExperimentalCppStdVersion
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400110 } else if String(b.ClangProperties.Cpp_std) == "default" {
Ivan Lozano3d947522020-09-23 13:26:25 -0400111 stdVersion = cc_config.CppStdVersion
112 } else {
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400113 stdVersion = String(b.ClangProperties.Cpp_std)
Ivan Lozano3d947522020-09-23 13:26:25 -0400114 }
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400115 } else if b.ClangProperties.C_std != nil {
116 if String(b.ClangProperties.C_std) == "experimental" {
Ivan Lozano3d947522020-09-23 13:26:25 -0400117 stdVersion = cc_config.ExperimentalCStdVersion
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400118 } else if String(b.ClangProperties.C_std) == "default" {
Ivan Lozano3d947522020-09-23 13:26:25 -0400119 stdVersion = cc_config.CStdVersion
120 } else {
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400121 stdVersion = String(b.ClangProperties.C_std)
Ivan Lozano3d947522020-09-23 13:26:25 -0400122 }
123 } else if isCpp {
124 stdVersion = cc_config.CppStdVersion
125 } else {
126 stdVersion = cc_config.CStdVersion
127 }
128
129 return stdVersion, isCpp
130}
131
ThiƩbaud Weksteen31f1bb82020-08-27 13:37:29 +0200132func (b *bindgenDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path {
133 ccToolchain := ctx.RustModule().ccToolchain(ctx)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400134
135 var cflags []string
Ivan Lozano45901ed2020-07-24 16:05:01 -0400136 var implicits android.Paths
137
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400138 implicits = append(implicits, deps.depGeneratedHeaders...)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400139
140 // Default clang flags
Colin Cross0523ba22021-07-14 18:45:05 -0700141 cflags = append(cflags, "${cc_config.CommonGlobalCflags}")
Ivan Lozano45901ed2020-07-24 16:05:01 -0400142 if ctx.Device() {
Colin Cross0523ba22021-07-14 18:45:05 -0700143 cflags = append(cflags, "${cc_config.DeviceGlobalCflags}")
Ivan Lozano45901ed2020-07-24 16:05:01 -0400144 }
145
146 // Toolchain clang flags
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400147 cflags = append(cflags, "-target "+ccToolchain.ClangTriple())
Colin Cross33bac242021-07-14 17:03:16 -0700148 cflags = append(cflags, strings.ReplaceAll(ccToolchain.Cflags(), "${config.", "${cc_config."))
149 cflags = append(cflags, strings.ReplaceAll(ccToolchain.ToolchainCflags(), "${config.", "${cc_config."))
Ivan Lozano45901ed2020-07-24 16:05:01 -0400150
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500151 if ctx.RustModule().UseVndk() {
152 cflags = append(cflags, "-D__ANDROID_VNDK__")
153 if ctx.RustModule().InVendor() {
154 cflags = append(cflags, "-D__ANDROID_VENDOR__")
155 } else if ctx.RustModule().InProduct() {
156 cflags = append(cflags, "-D__ANDROID_PRODUCT__")
157 }
158 }
159
160 if ctx.RustModule().InRecovery() {
161 cflags = append(cflags, "-D__ANDROID_RECOVERY__")
162 }
163
164 if mctx, ok := ctx.(*moduleContext); ok && mctx.apexVariationName() != "" {
165 cflags = append(cflags, "-D__ANDROID_APEX__")
166 if ctx.Device() {
167 cflags = append(cflags, fmt.Sprintf("-D__ANDROID_APEX_MIN_SDK_VERSION__=%d",
168 ctx.RustModule().apexSdkVersion.FinalOrFutureInt()))
169 }
170 }
171
172 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
173 cflags = append(cflags, "-D__ANDROID_NATIVE_BRIDGE__")
174 }
175
Ivan Lozano45901ed2020-07-24 16:05:01 -0400176 // Dependency clang flags and include paths
177 cflags = append(cflags, deps.depClangFlags...)
178 for _, include := range deps.depIncludePaths {
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400179 cflags = append(cflags, "-I"+include.String())
180 }
Ivan Lozano45901ed2020-07-24 16:05:01 -0400181 for _, include := range deps.depSystemIncludePaths {
182 cflags = append(cflags, "-isystem "+include.String())
183 }
184
Stephen Crane12e2cb72020-08-04 12:26:10 -0700185 esc := proptools.NinjaAndShellEscapeList
186
Ivan Lozano0a2a1152020-10-16 10:49:08 -0400187 // Filter out invalid cflags
188 for _, flag := range b.ClangProperties.Cflags {
189 if flag == "-x c++" || flag == "-xc++" {
190 ctx.PropertyErrorf("cflags",
191 "-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'")
192 }
193 if strings.HasPrefix(flag, "-std=") {
194 ctx.PropertyErrorf("cflags",
195 "-std should not be specified in cflags; instead use c_std or cpp_std")
196 }
197 }
198
Ivan Lozano45901ed2020-07-24 16:05:01 -0400199 // Module defined clang flags and include paths
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400200 cflags = append(cflags, esc(b.ClangProperties.Cflags)...)
201 for _, include := range b.ClangProperties.Local_include_dirs {
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400202 cflags = append(cflags, "-I"+android.PathForModuleSrc(ctx, include).String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400203 implicits = append(implicits, android.PathForModuleSrc(ctx, include))
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400204 }
205
206 bindgenFlags := defaultBindgenFlags
Stephen Crane12e2cb72020-08-04 12:26:10 -0700207 bindgenFlags = append(bindgenFlags, esc(b.Properties.Bindgen_flags)...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400208
209 wrapperFile := android.OptionalPathForModuleSrc(ctx, b.Properties.Wrapper_src)
210 if !wrapperFile.Valid() {
211 ctx.PropertyErrorf("wrapper_src", "invalid path to wrapper source")
212 }
213
Ivan Lozano3d947522020-09-23 13:26:25 -0400214 // Add C std version flag
215 stdVersion, isCpp := b.getStdVersion(ctx, wrapperFile.Path())
216 cflags = append(cflags, "-std="+stdVersion)
217
218 // Specify the header source language to avoid ambiguity.
219 if isCpp {
220 cflags = append(cflags, "-x c++")
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400221 // Add any C++ only flags.
222 cflags = append(cflags, esc(b.ClangProperties.Cppflags)...)
Ivan Lozano3d947522020-09-23 13:26:25 -0400223 } else {
224 cflags = append(cflags, "-x c")
225 }
226
Andrei Homescuc7767922020-08-05 06:36:19 -0700227 outputFile := android.PathForModuleOut(ctx, b.BaseSourceProvider.getStem(ctx)+".rs")
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400228
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400229 var cmd, cmdDesc string
230 if b.Properties.Custom_bindgen != "" {
231 cmd = ctx.GetDirectDepWithTag(b.Properties.Custom_bindgen, customBindgenDepTag).(*Module).HostToolPath().String()
232 cmdDesc = b.Properties.Custom_bindgen
233 } else {
234 cmd = "$bindgenCmd"
235 cmdDesc = "bindgen"
236 }
237
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400238 ctx.Build(pctx, android.BuildParams{
239 Rule: bindgen,
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400240 Description: strings.Join([]string{cmdDesc, wrapperFile.Path().Rel()}, " "),
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400241 Output: outputFile,
242 Input: wrapperFile.Path(),
Ivan Lozano45901ed2020-07-24 16:05:01 -0400243 Implicits: implicits,
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400244 Args: map[string]string{
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400245 "cmd": cmd,
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400246 "flags": strings.Join(bindgenFlags, " "),
247 "cflags": strings.Join(cflags, " "),
248 },
249 })
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400250
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700251 b.BaseSourceProvider.OutputFiles = android.Paths{outputFile}
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400252 return outputFile
253}
254
Andrei Homescuc7767922020-08-05 06:36:19 -0700255func (b *bindgenDecorator) SourceProviderProps() []interface{} {
256 return append(b.BaseSourceProvider.SourceProviderProps(),
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400257 &b.Properties, &b.ClangProperties)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400258}
259
Ivan Lozano10735d92020-07-22 09:14:47 -0400260// rust_bindgen generates Rust FFI bindings to C libraries using bindgen given a wrapper header as the primary input.
261// 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 +0200262// the header and generated source is appropriately handled. It is recommended to add it as a dependency in the
263// rlibs, dylibs or rustlibs property. It may also be added in the srcs property for external crates, using the ":"
264// prefix.
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400265func RustBindgenFactory() android.Module {
266 module, _ := NewRustBindgen(android.HostAndDeviceSupported)
267 return module.Init()
268}
269
Ivan Lozanof6fe9952020-07-22 16:30:20 -0400270func RustBindgenHostFactory() android.Module {
271 module, _ := NewRustBindgen(android.HostSupported)
272 return module.Init()
273}
274
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400275func NewRustBindgen(hod android.HostOrDeviceSupported) (*Module, *bindgenDecorator) {
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400276 bindgen := &bindgenDecorator{
Andrei Homescuc7767922020-08-05 06:36:19 -0700277 BaseSourceProvider: NewSourceProvider(),
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400278 Properties: BindgenProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400279 ClangProperties: cc.RustBindgenClangProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400280 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400281
Andrei Homescuc7767922020-08-05 06:36:19 -0700282 module := NewSourceProviderModule(hod, bindgen, false)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400283
284 return module, bindgen
285}
286
Andrei Homescuc7767922020-08-05 06:36:19 -0700287func (b *bindgenDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps {
288 deps = b.BaseSourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400289 if ctx.toolchain().Bionic() {
ThiƩbaud Weksteenf1ff54a2021-03-22 14:24:54 +0100290 deps = bionicDeps(ctx, deps, false)
Colin Crosse32f0932022-01-23 20:48:36 -0800291 } else if ctx.Os() == android.LinuxMusl {
292 deps = muslDeps(ctx, deps, false)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400293 }
294
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400295 deps.SharedLibs = append(deps.SharedLibs, b.ClangProperties.Shared_libs...)
296 deps.StaticLibs = append(deps.StaticLibs, b.ClangProperties.Static_libs...)
Ivan Lozano9b443832020-11-17 13:39:30 -0500297 deps.HeaderLibs = append(deps.StaticLibs, b.ClangProperties.Header_libs...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400298 return deps
299}