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 | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 18 | "strings" |
| 19 | |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 20 | "github.com/google/blueprint" |
Stephen Crane | 12e2cb7 | 2020-08-04 12:26:10 -0700 | [diff] [blame] | 21 | "github.com/google/blueprint/proptools" |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 22 | |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 23 | "android/soong/android" |
Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 24 | cc_config "android/soong/cc/config" |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | var ( |
Ivan Lozano | ec54eec | 2020-07-22 16:48:53 -0400 | [diff] [blame] | 28 | defaultBindgenFlags = []string{""} |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 29 | |
| 30 | // bindgen should specify its own Clang revision so updating Clang isn't potentially blocked on bindgen failures. |
Ivan Lozano | 54a8ee1 | 2020-09-08 13:10:36 -0400 | [diff] [blame] | 31 | bindgenClangVersion = "clang-r383902c" |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 32 | |
| 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 Weksteen | 682c9d7 | 2020-08-31 10:06:16 +0200 | [diff] [blame] | 36 | "${cc_config.ClangBase}/${config.HostPrebuiltTag}/"+bindgenClangVersion+"/bin/clang") |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 37 | _ = pctx.SourcePathVariable("bindgenLibClang", |
Ivan Lozano | 54a8ee1 | 2020-09-08 13:10:36 -0400 | [diff] [blame] | 38 | "${cc_config.ClangBase}/${config.HostPrebuiltTag}/"+bindgenClangVersion+"/lib64/") |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 39 | |
| 40 | //TODO(ivanlozano) Switch this to RuleBuilder |
| 41 | bindgen = pctx.AndroidStaticRule("bindgen", |
| 42 | blueprint.RuleParams{ |
Ivan Lozano | ec54eec | 2020-07-22 16:48:53 -0400 | [diff] [blame] | 43 | Command: "CLANG_PATH=$bindgenClang LIBCLANG_PATH=$bindgenLibClang RUSTFMT=${config.RustBin}/rustfmt " + |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 44 | "$cmd $flags $in -o $out -- -MD -MF $out.d $cflags", |
| 45 | CommandDeps: []string{"$cmd"}, |
Ivan Lozano | e1e844b | 2020-07-24 15:16:30 -0400 | [diff] [blame] | 46 | Deps: blueprint.DepsGCC, |
| 47 | Depfile: "$out.d", |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 48 | }, |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 49 | "cmd", "flags", "cflags") |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 50 | ) |
| 51 | |
| 52 | func init() { |
| 53 | android.RegisterModuleType("rust_bindgen", RustBindgenFactory) |
Ivan Lozano | f6fe995 | 2020-07-22 16:30:20 -0400 | [diff] [blame] | 54 | android.RegisterModuleType("rust_bindgen_host", RustBindgenHostFactory) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | var _ SourceProvider = (*bindgenDecorator)(nil) |
| 58 | |
| 59 | type BindgenProperties struct { |
Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 60 | // 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 Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 65 | Wrapper_src *string `android:"path,arch_variant"` |
| 66 | |
| 67 | // list of bindgen-specific flags and options |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 68 | Bindgen_flags []string `android:"arch_variant"` |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 69 | |
| 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 Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 83 | // 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 Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 89 | // 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 Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 105 | //TODO(b/161141999) Add support for headers from cc_library_header modules. |
| 106 | } |
| 107 | |
| 108 | type bindgenDecorator struct { |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 109 | *BaseSourceProvider |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 110 | |
| 111 | Properties BindgenProperties |
| 112 | } |
| 113 | |
Ivan Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 114 | func (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 Weksteen | 31f1bb8 | 2020-08-27 13:37:29 +0200 | [diff] [blame] | 153 | func (b *bindgenDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path { |
| 154 | ccToolchain := ctx.RustModule().ccToolchain(ctx) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 155 | |
| 156 | var cflags []string |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 157 | var implicits android.Paths |
| 158 | |
Ivan Lozano | ddd0bdb | 2020-08-28 17:00:26 -0400 | [diff] [blame] | 159 | implicits = append(implicits, deps.depGeneratedHeaders...) |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 160 | |
| 161 | // Default clang flags |
Thiébaud Weksteen | 682c9d7 | 2020-08-31 10:06:16 +0200 | [diff] [blame] | 162 | cflags = append(cflags, "${cc_config.CommonClangGlobalCflags}") |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 163 | if ctx.Device() { |
Thiébaud Weksteen | 682c9d7 | 2020-08-31 10:06:16 +0200 | [diff] [blame] | 164 | cflags = append(cflags, "${cc_config.DeviceClangGlobalCflags}") |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // Toolchain clang flags |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 168 | cflags = append(cflags, "-target "+ccToolchain.ClangTriple()) |
Thiébaud Weksteen | 682c9d7 | 2020-08-31 10:06:16 +0200 | [diff] [blame] | 169 | cflags = append(cflags, strings.ReplaceAll(ccToolchain.ToolchainClangCflags(), "${config.", "${cc_config.")) |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 170 | |
| 171 | // Dependency clang flags and include paths |
| 172 | cflags = append(cflags, deps.depClangFlags...) |
| 173 | for _, include := range deps.depIncludePaths { |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 174 | cflags = append(cflags, "-I"+include.String()) |
| 175 | } |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 176 | for _, include := range deps.depSystemIncludePaths { |
| 177 | cflags = append(cflags, "-isystem "+include.String()) |
| 178 | } |
| 179 | |
Stephen Crane | 12e2cb7 | 2020-08-04 12:26:10 -0700 | [diff] [blame] | 180 | esc := proptools.NinjaAndShellEscapeList |
| 181 | |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 182 | // Module defined clang flags and include paths |
Stephen Crane | 12e2cb7 | 2020-08-04 12:26:10 -0700 | [diff] [blame] | 183 | cflags = append(cflags, esc(b.Properties.Cflags)...) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 184 | for _, include := range b.Properties.Local_include_dirs { |
| 185 | cflags = append(cflags, "-I"+android.PathForModuleSrc(ctx, include).String()) |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 186 | implicits = append(implicits, android.PathForModuleSrc(ctx, include)) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | bindgenFlags := defaultBindgenFlags |
Stephen Crane | 12e2cb7 | 2020-08-04 12:26:10 -0700 | [diff] [blame] | 190 | bindgenFlags = append(bindgenFlags, esc(b.Properties.Bindgen_flags)...) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 191 | |
| 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 Lozano | 3d94752 | 2020-09-23 13:26:25 -0400 | [diff] [blame] | 197 | // 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 Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 208 | outputFile := android.PathForModuleOut(ctx, b.BaseSourceProvider.getStem(ctx)+".rs") |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 209 | |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 210 | 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 Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 219 | ctx.Build(pctx, android.BuildParams{ |
| 220 | Rule: bindgen, |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 221 | Description: strings.Join([]string{cmdDesc, wrapperFile.Path().Rel()}, " "), |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 222 | Output: outputFile, |
| 223 | Input: wrapperFile.Path(), |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 224 | Implicits: implicits, |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 225 | Args: map[string]string{ |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 226 | "cmd": cmd, |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 227 | "flags": strings.Join(bindgenFlags, " "), |
| 228 | "cflags": strings.Join(cflags, " "), |
| 229 | }, |
| 230 | }) |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 231 | |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 232 | b.BaseSourceProvider.OutputFile = outputFile |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 233 | return outputFile |
| 234 | } |
| 235 | |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 236 | func (b *bindgenDecorator) SourceProviderProps() []interface{} { |
| 237 | return append(b.BaseSourceProvider.SourceProviderProps(), |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 238 | &b.Properties) |
| 239 | } |
| 240 | |
Ivan Lozano | 10735d9 | 2020-07-22 09:14:47 -0400 | [diff] [blame] | 241 | // 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 Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 243 | // 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 Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 246 | func RustBindgenFactory() android.Module { |
| 247 | module, _ := NewRustBindgen(android.HostAndDeviceSupported) |
| 248 | return module.Init() |
| 249 | } |
| 250 | |
Ivan Lozano | f6fe995 | 2020-07-22 16:30:20 -0400 | [diff] [blame] | 251 | func RustBindgenHostFactory() android.Module { |
| 252 | module, _ := NewRustBindgen(android.HostSupported) |
| 253 | return module.Init() |
| 254 | } |
| 255 | |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 256 | func NewRustBindgen(hod android.HostOrDeviceSupported) (*Module, *bindgenDecorator) { |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 257 | bindgen := &bindgenDecorator{ |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 258 | BaseSourceProvider: NewSourceProvider(), |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 259 | Properties: BindgenProperties{}, |
| 260 | } |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 261 | |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 262 | module := NewSourceProviderModule(hod, bindgen, false) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 263 | |
| 264 | return module, bindgen |
| 265 | } |
| 266 | |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 267 | func (b *bindgenDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps { |
| 268 | deps = b.BaseSourceProvider.SourceProviderDeps(ctx, deps) |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 269 | if ctx.toolchain().Bionic() { |
Ivan Lozano | bf63d00 | 2020-10-02 10:03:23 -0400 | [diff] [blame^] | 270 | deps = bionicDeps(deps, false) |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 271 | } |
| 272 | |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 273 | deps.SharedLibs = append(deps.SharedLibs, b.Properties.Shared_libs...) |
| 274 | deps.StaticLibs = append(deps.StaticLibs, b.Properties.Static_libs...) |
| 275 | return deps |
| 276 | } |