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