Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 1 | // 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
Ivan Lozano | e950cda | 2021-11-09 11:26:04 -0500 | [diff] [blame] | 18 | "fmt" |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 19 | "strings" |
| 20 | |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 21 | "github.com/google/blueprint" |
Stephen Crane | 12e2cb7 | 2020-08-04 12:26:10 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 23 | |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 24 | "android/soong/android" |
Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 25 | "android/soong/cc" |
Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 26 | cc_config "android/soong/cc/config" |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | var ( |
Ivan Lozano | ec54eec | 2020-07-22 16:48:53 -0400 | [diff] [blame] | 30 | defaultBindgenFlags = []string{""} |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 31 | |
| 32 | // bindgen should specify its own Clang revision so updating Clang isn't potentially blocked on bindgen failures. |
Yi Kong | 4f6c927 | 2022-05-02 23:52:44 +0800 | [diff] [blame] | 33 | bindgenClangVersion = "clang-r450784d" |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 34 | |
Peter Collingbourne | 9a868f1 | 2020-12-30 20:23:01 -0800 | [diff] [blame] | 35 | _ = 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 Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 42 | //TODO(b/160803703) Use a prebuilt bindgen instead of the built bindgen. |
Shao-Chuan Lee | aa3231c | 2020-11-05 22:20:17 +0900 | [diff] [blame] | 43 | _ = pctx.HostBinToolVariable("bindgenCmd", "bindgen") |
Colin Cross | 567d342 | 2022-04-28 15:30:09 -0700 | [diff] [blame^] | 44 | _ = pctx.VariableFunc("bindgenHostPrebuiltTag", func(ctx android.PackageVarContext) string { |
| 45 | if ctx.Config().UseHostMusl() { |
| 46 | // This is a hack to use the glibc bindgen binary until we have a musl version checked in. |
| 47 | return "linux-x86" |
| 48 | } else { |
| 49 | return "${config.HostPrebuiltTag}" |
| 50 | } |
| 51 | }) |
| 52 | _ = pctx.VariableFunc("bindgenClangLibdir", func(ctx android.PackageVarContext) string { |
| 53 | if ctx.Config().UseHostMusl() { |
| 54 | return "musl/lib64/" |
| 55 | } else { |
| 56 | return "lib64/" |
| 57 | } |
| 58 | }) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 59 | _ = pctx.SourcePathVariable("bindgenClang", |
Colin Cross | 567d342 | 2022-04-28 15:30:09 -0700 | [diff] [blame^] | 60 | "${cc_config.ClangBase}/${bindgenHostPrebuiltTag}/${bindgenClangVersion}/bin/clang") |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 61 | _ = pctx.SourcePathVariable("bindgenLibClang", |
Colin Cross | 567d342 | 2022-04-28 15:30:09 -0700 | [diff] [blame^] | 62 | "${cc_config.ClangBase}/${bindgenHostPrebuiltTag}/${bindgenClangVersion}/${bindgenClangLibdir}") |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 63 | |
| 64 | //TODO(ivanlozano) Switch this to RuleBuilder |
| 65 | bindgen = pctx.AndroidStaticRule("bindgen", |
| 66 | blueprint.RuleParams{ |
Ivan Lozano | ec54eec | 2020-07-22 16:48:53 -0400 | [diff] [blame] | 67 | Command: "CLANG_PATH=$bindgenClang LIBCLANG_PATH=$bindgenLibClang RUSTFMT=${config.RustBin}/rustfmt " + |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 68 | "$cmd $flags $in -o $out -- -MD -MF $out.d $cflags", |
| 69 | CommandDeps: []string{"$cmd"}, |
Ivan Lozano | e1e844b | 2020-07-24 15:16:30 -0400 | [diff] [blame] | 70 | Deps: blueprint.DepsGCC, |
| 71 | Depfile: "$out.d", |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 72 | }, |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 73 | "cmd", "flags", "cflags") |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 74 | ) |
| 75 | |
| 76 | func init() { |
| 77 | android.RegisterModuleType("rust_bindgen", RustBindgenFactory) |
Ivan Lozano | f6fe995 | 2020-07-22 16:30:20 -0400 | [diff] [blame] | 78 | android.RegisterModuleType("rust_bindgen_host", RustBindgenHostFactory) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | var _ SourceProvider = (*bindgenDecorator)(nil) |
| 82 | |
| 83 | type BindgenProperties struct { |
Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 84 | // The wrapper header file. By default this is assumed to be a C header unless the extension is ".hh" or ".hpp". |
| 85 | // This is used to specify how to interpret the header and determines which '-std' flag to use by default. |
| 86 | // |
| 87 | // If your C++ header must have some other extension, then the default behavior can be overridden by setting the |
| 88 | // cpp_std property. |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 89 | Wrapper_src *string `android:"path,arch_variant"` |
| 90 | |
| 91 | // list of bindgen-specific flags and options |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 92 | Bindgen_flags []string `android:"arch_variant"` |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 93 | |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 94 | // module name of a custom binary/script which should be used instead of the 'bindgen' binary. This custom |
| 95 | // binary must expect arguments in a similar fashion to bindgen, e.g. |
| 96 | // |
| 97 | // "my_bindgen [flags] wrapper_header.h -o [output_path] -- [clang flags]" |
Liz Kammer | eda9398 | 2021-04-20 10:15:41 -0400 | [diff] [blame] | 98 | Custom_bindgen string |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | type bindgenDecorator struct { |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 102 | *BaseSourceProvider |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 103 | |
Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 104 | Properties BindgenProperties |
| 105 | ClangProperties cc.RustBindgenClangProperties |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 106 | } |
| 107 | |
Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 108 | func (b *bindgenDecorator) getStdVersion(ctx ModuleContext, src android.Path) (string, bool) { |
| 109 | // Assume headers are C headers |
| 110 | isCpp := false |
| 111 | stdVersion := "" |
| 112 | |
| 113 | switch src.Ext() { |
| 114 | case ".hpp", ".hh": |
| 115 | isCpp = true |
| 116 | } |
| 117 | |
Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 118 | if String(b.ClangProperties.Cpp_std) != "" && String(b.ClangProperties.C_std) != "" { |
Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 119 | ctx.PropertyErrorf("c_std", "c_std and cpp_std cannot both be defined at the same time.") |
| 120 | } |
| 121 | |
Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 122 | if String(b.ClangProperties.Cpp_std) != "" { |
| 123 | if String(b.ClangProperties.Cpp_std) == "experimental" { |
Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 124 | stdVersion = cc_config.ExperimentalCppStdVersion |
Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 125 | } else if String(b.ClangProperties.Cpp_std) == "default" { |
Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 126 | stdVersion = cc_config.CppStdVersion |
| 127 | } else { |
Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 128 | stdVersion = String(b.ClangProperties.Cpp_std) |
Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 129 | } |
Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 130 | } else if b.ClangProperties.C_std != nil { |
| 131 | if String(b.ClangProperties.C_std) == "experimental" { |
Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 132 | stdVersion = cc_config.ExperimentalCStdVersion |
Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 133 | } else if String(b.ClangProperties.C_std) == "default" { |
Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 134 | stdVersion = cc_config.CStdVersion |
| 135 | } else { |
Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 136 | stdVersion = String(b.ClangProperties.C_std) |
Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 137 | } |
| 138 | } else if isCpp { |
| 139 | stdVersion = cc_config.CppStdVersion |
| 140 | } else { |
| 141 | stdVersion = cc_config.CStdVersion |
| 142 | } |
| 143 | |
| 144 | return stdVersion, isCpp |
| 145 | } |
| 146 | |
ThiƩbaud Weksteen | 31f1bb8 | 2020-08-27 13:37:29 +0200 | [diff] [blame] | 147 | func (b *bindgenDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path { |
| 148 | ccToolchain := ctx.RustModule().ccToolchain(ctx) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 149 | |
| 150 | var cflags []string |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 151 | var implicits android.Paths |
| 152 | |
Ivan Lozano | ddd0bdb | 2020-08-28 17:00:26 -0400 | [diff] [blame] | 153 | implicits = append(implicits, deps.depGeneratedHeaders...) |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 154 | |
| 155 | // Default clang flags |
Colin Cross | 0523ba2 | 2021-07-14 18:45:05 -0700 | [diff] [blame] | 156 | cflags = append(cflags, "${cc_config.CommonGlobalCflags}") |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 157 | if ctx.Device() { |
Colin Cross | 0523ba2 | 2021-07-14 18:45:05 -0700 | [diff] [blame] | 158 | cflags = append(cflags, "${cc_config.DeviceGlobalCflags}") |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // Toolchain clang flags |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 162 | cflags = append(cflags, "-target "+ccToolchain.ClangTriple()) |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 163 | cflags = append(cflags, strings.ReplaceAll(ccToolchain.Cflags(), "${config.", "${cc_config.")) |
| 164 | cflags = append(cflags, strings.ReplaceAll(ccToolchain.ToolchainCflags(), "${config.", "${cc_config.")) |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 165 | |
Ivan Lozano | e950cda | 2021-11-09 11:26:04 -0500 | [diff] [blame] | 166 | if ctx.RustModule().UseVndk() { |
| 167 | cflags = append(cflags, "-D__ANDROID_VNDK__") |
| 168 | if ctx.RustModule().InVendor() { |
| 169 | cflags = append(cflags, "-D__ANDROID_VENDOR__") |
| 170 | } else if ctx.RustModule().InProduct() { |
| 171 | cflags = append(cflags, "-D__ANDROID_PRODUCT__") |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if ctx.RustModule().InRecovery() { |
| 176 | cflags = append(cflags, "-D__ANDROID_RECOVERY__") |
| 177 | } |
| 178 | |
| 179 | if mctx, ok := ctx.(*moduleContext); ok && mctx.apexVariationName() != "" { |
| 180 | cflags = append(cflags, "-D__ANDROID_APEX__") |
| 181 | if ctx.Device() { |
| 182 | cflags = append(cflags, fmt.Sprintf("-D__ANDROID_APEX_MIN_SDK_VERSION__=%d", |
| 183 | ctx.RustModule().apexSdkVersion.FinalOrFutureInt())) |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if ctx.Target().NativeBridge == android.NativeBridgeEnabled { |
| 188 | cflags = append(cflags, "-D__ANDROID_NATIVE_BRIDGE__") |
| 189 | } |
| 190 | |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 191 | // Dependency clang flags and include paths |
| 192 | cflags = append(cflags, deps.depClangFlags...) |
| 193 | for _, include := range deps.depIncludePaths { |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 194 | cflags = append(cflags, "-I"+include.String()) |
| 195 | } |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 196 | for _, include := range deps.depSystemIncludePaths { |
| 197 | cflags = append(cflags, "-isystem "+include.String()) |
| 198 | } |
| 199 | |
Stephen Crane | 12e2cb7 | 2020-08-04 12:26:10 -0700 | [diff] [blame] | 200 | esc := proptools.NinjaAndShellEscapeList |
| 201 | |
Ivan Lozano | 0a2a115 | 2020-10-16 10:49:08 -0400 | [diff] [blame] | 202 | // Filter out invalid cflags |
| 203 | for _, flag := range b.ClangProperties.Cflags { |
| 204 | if flag == "-x c++" || flag == "-xc++" { |
| 205 | ctx.PropertyErrorf("cflags", |
| 206 | "-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'") |
| 207 | } |
| 208 | if strings.HasPrefix(flag, "-std=") { |
| 209 | ctx.PropertyErrorf("cflags", |
| 210 | "-std should not be specified in cflags; instead use c_std or cpp_std") |
| 211 | } |
| 212 | } |
| 213 | |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 214 | // Module defined clang flags and include paths |
Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 215 | cflags = append(cflags, esc(b.ClangProperties.Cflags)...) |
| 216 | for _, include := range b.ClangProperties.Local_include_dirs { |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 217 | cflags = append(cflags, "-I"+android.PathForModuleSrc(ctx, include).String()) |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 218 | implicits = append(implicits, android.PathForModuleSrc(ctx, include)) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | bindgenFlags := defaultBindgenFlags |
Stephen Crane | 12e2cb7 | 2020-08-04 12:26:10 -0700 | [diff] [blame] | 222 | bindgenFlags = append(bindgenFlags, esc(b.Properties.Bindgen_flags)...) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 223 | |
| 224 | wrapperFile := android.OptionalPathForModuleSrc(ctx, b.Properties.Wrapper_src) |
| 225 | if !wrapperFile.Valid() { |
| 226 | ctx.PropertyErrorf("wrapper_src", "invalid path to wrapper source") |
| 227 | } |
| 228 | |
Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 229 | // Add C std version flag |
| 230 | stdVersion, isCpp := b.getStdVersion(ctx, wrapperFile.Path()) |
| 231 | cflags = append(cflags, "-std="+stdVersion) |
| 232 | |
| 233 | // Specify the header source language to avoid ambiguity. |
| 234 | if isCpp { |
| 235 | cflags = append(cflags, "-x c++") |
Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 236 | // Add any C++ only flags. |
| 237 | cflags = append(cflags, esc(b.ClangProperties.Cppflags)...) |
Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 238 | } else { |
| 239 | cflags = append(cflags, "-x c") |
| 240 | } |
| 241 | |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 242 | outputFile := android.PathForModuleOut(ctx, b.BaseSourceProvider.getStem(ctx)+".rs") |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 243 | |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 244 | var cmd, cmdDesc string |
| 245 | if b.Properties.Custom_bindgen != "" { |
| 246 | cmd = ctx.GetDirectDepWithTag(b.Properties.Custom_bindgen, customBindgenDepTag).(*Module).HostToolPath().String() |
| 247 | cmdDesc = b.Properties.Custom_bindgen |
| 248 | } else { |
| 249 | cmd = "$bindgenCmd" |
| 250 | cmdDesc = "bindgen" |
| 251 | } |
| 252 | |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 253 | ctx.Build(pctx, android.BuildParams{ |
| 254 | Rule: bindgen, |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 255 | Description: strings.Join([]string{cmdDesc, wrapperFile.Path().Rel()}, " "), |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 256 | Output: outputFile, |
| 257 | Input: wrapperFile.Path(), |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 258 | Implicits: implicits, |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 259 | Args: map[string]string{ |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 260 | "cmd": cmd, |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 261 | "flags": strings.Join(bindgenFlags, " "), |
| 262 | "cflags": strings.Join(cflags, " "), |
| 263 | }, |
| 264 | }) |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 265 | |
Chih-Hung Hsieh | c49649c | 2020-10-01 21:25:05 -0700 | [diff] [blame] | 266 | b.BaseSourceProvider.OutputFiles = android.Paths{outputFile} |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 267 | return outputFile |
| 268 | } |
| 269 | |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 270 | func (b *bindgenDecorator) SourceProviderProps() []interface{} { |
| 271 | return append(b.BaseSourceProvider.SourceProviderProps(), |
Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 272 | &b.Properties, &b.ClangProperties) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 273 | } |
| 274 | |
Ivan Lozano | 10735d9 | 2020-07-22 09:14:47 -0400 | [diff] [blame] | 275 | // rust_bindgen generates Rust FFI bindings to C libraries using bindgen given a wrapper header as the primary input. |
| 276 | // Bindgen has a number of flags to control the generated source, and additional flags can be passed to clang to ensure |
ThiƩbaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 277 | // the header and generated source is appropriately handled. It is recommended to add it as a dependency in the |
| 278 | // rlibs, dylibs or rustlibs property. It may also be added in the srcs property for external crates, using the ":" |
| 279 | // prefix. |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 280 | func RustBindgenFactory() android.Module { |
| 281 | module, _ := NewRustBindgen(android.HostAndDeviceSupported) |
| 282 | return module.Init() |
| 283 | } |
| 284 | |
Ivan Lozano | f6fe995 | 2020-07-22 16:30:20 -0400 | [diff] [blame] | 285 | func RustBindgenHostFactory() android.Module { |
| 286 | module, _ := NewRustBindgen(android.HostSupported) |
| 287 | return module.Init() |
| 288 | } |
| 289 | |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 290 | func NewRustBindgen(hod android.HostOrDeviceSupported) (*Module, *bindgenDecorator) { |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 291 | bindgen := &bindgenDecorator{ |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 292 | BaseSourceProvider: NewSourceProvider(), |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 293 | Properties: BindgenProperties{}, |
Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 294 | ClangProperties: cc.RustBindgenClangProperties{}, |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 295 | } |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 296 | |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 297 | module := NewSourceProviderModule(hod, bindgen, false) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 298 | |
| 299 | return module, bindgen |
| 300 | } |
| 301 | |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 302 | func (b *bindgenDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps { |
| 303 | deps = b.BaseSourceProvider.SourceProviderDeps(ctx, deps) |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 304 | if ctx.toolchain().Bionic() { |
ThiƩbaud Weksteen | f1ff54a | 2021-03-22 14:24:54 +0100 | [diff] [blame] | 305 | deps = bionicDeps(ctx, deps, false) |
Colin Cross | e32f093 | 2022-01-23 20:48:36 -0800 | [diff] [blame] | 306 | } else if ctx.Os() == android.LinuxMusl { |
| 307 | deps = muslDeps(ctx, deps, false) |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 308 | } |
| 309 | |
Ivan Lozano | bc9e421 | 2020-09-25 16:08:34 -0400 | [diff] [blame] | 310 | deps.SharedLibs = append(deps.SharedLibs, b.ClangProperties.Shared_libs...) |
| 311 | deps.StaticLibs = append(deps.StaticLibs, b.ClangProperties.Static_libs...) |
Ivan Lozano | 9b44383 | 2020-11-17 13:39:30 -0500 | [diff] [blame] | 312 | deps.HeaderLibs = append(deps.StaticLibs, b.ClangProperties.Header_libs...) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 313 | return deps |
| 314 | } |