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 | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | var ( |
Ivan Lozano | ec54eec | 2020-07-22 16:48:53 -0400 | [diff] [blame] | 27 | defaultBindgenFlags = []string{""} |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 28 | |
| 29 | // 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] | 30 | bindgenClangVersion = "clang-r383902c" |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 31 | |
| 32 | //TODO(b/160803703) Use a prebuilt bindgen instead of the built bindgen. |
| 33 | _ = pctx.SourcePathVariable("bindgenCmd", "out/host/${config.HostPrebuiltTag}/bin/bindgen") |
| 34 | _ = pctx.SourcePathVariable("bindgenClang", |
Thiébaud Weksteen | 682c9d7 | 2020-08-31 10:06:16 +0200 | [diff] [blame] | 35 | "${cc_config.ClangBase}/${config.HostPrebuiltTag}/"+bindgenClangVersion+"/bin/clang") |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 36 | _ = pctx.SourcePathVariable("bindgenLibClang", |
Ivan Lozano | 54a8ee1 | 2020-09-08 13:10:36 -0400 | [diff] [blame] | 37 | "${cc_config.ClangBase}/${config.HostPrebuiltTag}/"+bindgenClangVersion+"/lib64/") |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 38 | |
| 39 | //TODO(ivanlozano) Switch this to RuleBuilder |
| 40 | bindgen = pctx.AndroidStaticRule("bindgen", |
| 41 | blueprint.RuleParams{ |
Ivan Lozano | ec54eec | 2020-07-22 16:48:53 -0400 | [diff] [blame] | 42 | Command: "CLANG_PATH=$bindgenClang LIBCLANG_PATH=$bindgenLibClang RUSTFMT=${config.RustBin}/rustfmt " + |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 43 | "$cmd $flags $in -o $out -- -MD -MF $out.d $cflags", |
| 44 | CommandDeps: []string{"$cmd"}, |
Ivan Lozano | e1e844b | 2020-07-24 15:16:30 -0400 | [diff] [blame] | 45 | Deps: blueprint.DepsGCC, |
| 46 | Depfile: "$out.d", |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 47 | }, |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 48 | "cmd", "flags", "cflags") |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 49 | ) |
| 50 | |
| 51 | func init() { |
| 52 | android.RegisterModuleType("rust_bindgen", RustBindgenFactory) |
Ivan Lozano | f6fe995 | 2020-07-22 16:30:20 -0400 | [diff] [blame] | 53 | android.RegisterModuleType("rust_bindgen_host", RustBindgenHostFactory) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | var _ SourceProvider = (*bindgenDecorator)(nil) |
| 57 | |
| 58 | type BindgenProperties struct { |
| 59 | // The wrapper header file |
| 60 | Wrapper_src *string `android:"path,arch_variant"` |
| 61 | |
| 62 | // list of bindgen-specific flags and options |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 63 | Bindgen_flags []string `android:"arch_variant"` |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 64 | |
| 65 | // list of clang flags required to correctly interpret the headers. |
| 66 | Cflags []string `android:"arch_variant"` |
| 67 | |
| 68 | // list of directories relative to the Blueprints file that will |
| 69 | // be added to the include path using -I |
| 70 | Local_include_dirs []string `android:"arch_variant,variant_prepend"` |
| 71 | |
| 72 | // list of static libraries that provide headers for this binding. |
| 73 | Static_libs []string `android:"arch_variant,variant_prepend"` |
| 74 | |
| 75 | // list of shared libraries that provide headers for this binding. |
| 76 | Shared_libs []string `android:"arch_variant"` |
| 77 | |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 78 | // module name of a custom binary/script which should be used instead of the 'bindgen' binary. This custom |
| 79 | // binary must expect arguments in a similar fashion to bindgen, e.g. |
| 80 | // |
| 81 | // "my_bindgen [flags] wrapper_header.h -o [output_path] -- [clang flags]" |
| 82 | Custom_bindgen string `android:"path"` |
| 83 | |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 84 | //TODO(b/161141999) Add support for headers from cc_library_header modules. |
| 85 | } |
| 86 | |
| 87 | type bindgenDecorator struct { |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 88 | *BaseSourceProvider |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 89 | |
| 90 | Properties BindgenProperties |
| 91 | } |
| 92 | |
Thiébaud Weksteen | 31f1bb8 | 2020-08-27 13:37:29 +0200 | [diff] [blame] | 93 | func (b *bindgenDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path { |
| 94 | ccToolchain := ctx.RustModule().ccToolchain(ctx) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 95 | |
| 96 | var cflags []string |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 97 | var implicits android.Paths |
| 98 | |
Ivan Lozano | ddd0bdb | 2020-08-28 17:00:26 -0400 | [diff] [blame] | 99 | implicits = append(implicits, deps.depGeneratedHeaders...) |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 100 | |
| 101 | // Default clang flags |
Thiébaud Weksteen | 682c9d7 | 2020-08-31 10:06:16 +0200 | [diff] [blame] | 102 | cflags = append(cflags, "${cc_config.CommonClangGlobalCflags}") |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 103 | if ctx.Device() { |
Thiébaud Weksteen | 682c9d7 | 2020-08-31 10:06:16 +0200 | [diff] [blame] | 104 | cflags = append(cflags, "${cc_config.DeviceClangGlobalCflags}") |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // Toolchain clang flags |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 108 | cflags = append(cflags, "-target "+ccToolchain.ClangTriple()) |
Thiébaud Weksteen | 682c9d7 | 2020-08-31 10:06:16 +0200 | [diff] [blame] | 109 | cflags = append(cflags, strings.ReplaceAll(ccToolchain.ToolchainClangCflags(), "${config.", "${cc_config.")) |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 110 | |
| 111 | // Dependency clang flags and include paths |
| 112 | cflags = append(cflags, deps.depClangFlags...) |
| 113 | for _, include := range deps.depIncludePaths { |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 114 | cflags = append(cflags, "-I"+include.String()) |
| 115 | } |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 116 | for _, include := range deps.depSystemIncludePaths { |
| 117 | cflags = append(cflags, "-isystem "+include.String()) |
| 118 | } |
| 119 | |
Stephen Crane | 12e2cb7 | 2020-08-04 12:26:10 -0700 | [diff] [blame] | 120 | esc := proptools.NinjaAndShellEscapeList |
| 121 | |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 122 | // Module defined clang flags and include paths |
Stephen Crane | 12e2cb7 | 2020-08-04 12:26:10 -0700 | [diff] [blame] | 123 | cflags = append(cflags, esc(b.Properties.Cflags)...) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 124 | for _, include := range b.Properties.Local_include_dirs { |
| 125 | cflags = append(cflags, "-I"+android.PathForModuleSrc(ctx, include).String()) |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 126 | implicits = append(implicits, android.PathForModuleSrc(ctx, include)) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | bindgenFlags := defaultBindgenFlags |
Stephen Crane | 12e2cb7 | 2020-08-04 12:26:10 -0700 | [diff] [blame] | 130 | bindgenFlags = append(bindgenFlags, esc(b.Properties.Bindgen_flags)...) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 131 | |
| 132 | wrapperFile := android.OptionalPathForModuleSrc(ctx, b.Properties.Wrapper_src) |
| 133 | if !wrapperFile.Valid() { |
| 134 | ctx.PropertyErrorf("wrapper_src", "invalid path to wrapper source") |
| 135 | } |
| 136 | |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 137 | outputFile := android.PathForModuleOut(ctx, b.BaseSourceProvider.getStem(ctx)+".rs") |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 138 | |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 139 | var cmd, cmdDesc string |
| 140 | if b.Properties.Custom_bindgen != "" { |
| 141 | cmd = ctx.GetDirectDepWithTag(b.Properties.Custom_bindgen, customBindgenDepTag).(*Module).HostToolPath().String() |
| 142 | cmdDesc = b.Properties.Custom_bindgen |
| 143 | } else { |
| 144 | cmd = "$bindgenCmd" |
| 145 | cmdDesc = "bindgen" |
| 146 | } |
| 147 | |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 148 | ctx.Build(pctx, android.BuildParams{ |
| 149 | Rule: bindgen, |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 150 | Description: strings.Join([]string{cmdDesc, wrapperFile.Path().Rel()}, " "), |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 151 | Output: outputFile, |
| 152 | Input: wrapperFile.Path(), |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 153 | Implicits: implicits, |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 154 | Args: map[string]string{ |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 155 | "cmd": cmd, |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 156 | "flags": strings.Join(bindgenFlags, " "), |
| 157 | "cflags": strings.Join(cflags, " "), |
| 158 | }, |
| 159 | }) |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame] | 160 | |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 161 | b.BaseSourceProvider.OutputFile = outputFile |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 162 | return outputFile |
| 163 | } |
| 164 | |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 165 | func (b *bindgenDecorator) SourceProviderProps() []interface{} { |
| 166 | return append(b.BaseSourceProvider.SourceProviderProps(), |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 167 | &b.Properties) |
| 168 | } |
| 169 | |
Ivan Lozano | 10735d9 | 2020-07-22 09:14:47 -0400 | [diff] [blame] | 170 | // rust_bindgen generates Rust FFI bindings to C libraries using bindgen given a wrapper header as the primary input. |
| 171 | // 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^] | 172 | // the header and generated source is appropriately handled. It is recommended to add it as a dependency in the |
| 173 | // rlibs, dylibs or rustlibs property. It may also be added in the srcs property for external crates, using the ":" |
| 174 | // prefix. |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 175 | func RustBindgenFactory() android.Module { |
| 176 | module, _ := NewRustBindgen(android.HostAndDeviceSupported) |
| 177 | return module.Init() |
| 178 | } |
| 179 | |
Ivan Lozano | f6fe995 | 2020-07-22 16:30:20 -0400 | [diff] [blame] | 180 | func RustBindgenHostFactory() android.Module { |
| 181 | module, _ := NewRustBindgen(android.HostSupported) |
| 182 | return module.Init() |
| 183 | } |
| 184 | |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 185 | func NewRustBindgen(hod android.HostOrDeviceSupported) (*Module, *bindgenDecorator) { |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 186 | bindgen := &bindgenDecorator{ |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 187 | BaseSourceProvider: NewSourceProvider(), |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 188 | Properties: BindgenProperties{}, |
| 189 | } |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 190 | |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 191 | module := NewSourceProviderModule(hod, bindgen, false) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 192 | |
| 193 | return module, bindgen |
| 194 | } |
| 195 | |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 196 | func (b *bindgenDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps { |
| 197 | deps = b.BaseSourceProvider.SourceProviderDeps(ctx, deps) |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 198 | if ctx.toolchain().Bionic() { |
| 199 | deps = bionicDeps(deps) |
| 200 | } |
| 201 | |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 202 | deps.SharedLibs = append(deps.SharedLibs, b.Properties.Shared_libs...) |
| 203 | deps.StaticLibs = append(deps.StaticLibs, b.Properties.Static_libs...) |
| 204 | return deps |
| 205 | } |