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" |
| 21 | |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 22 | "android/soong/android" |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 23 | ccConfig "android/soong/cc/config" |
| 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. |
| 30 | bindgenClangVersion = "clang-r383902c" |
| 31 | bindgenLibClangSoGit = "11git" |
| 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", |
| 36 | "${ccConfig.ClangBase}/${config.HostPrebuiltTag}/"+bindgenClangVersion+"/bin/clang") |
| 37 | _ = pctx.SourcePathVariable("bindgenLibClang", |
| 38 | "${ccConfig.ClangBase}/${config.HostPrebuiltTag}/"+bindgenClangVersion+"/lib64/libclang.so."+bindgenLibClangSoGit) |
| 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 { |
| 60 | // The wrapper header file |
| 61 | Wrapper_src *string `android:"path,arch_variant"` |
| 62 | |
| 63 | // list of bindgen-specific flags and options |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 64 | Bindgen_flags []string `android:"arch_variant"` |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 65 | |
| 66 | // list of clang flags required to correctly interpret the headers. |
| 67 | Cflags []string `android:"arch_variant"` |
| 68 | |
| 69 | // list of directories relative to the Blueprints file that will |
| 70 | // be added to the include path using -I |
| 71 | Local_include_dirs []string `android:"arch_variant,variant_prepend"` |
| 72 | |
| 73 | // list of static libraries that provide headers for this binding. |
| 74 | Static_libs []string `android:"arch_variant,variant_prepend"` |
| 75 | |
| 76 | // list of shared libraries that provide headers for this binding. |
| 77 | Shared_libs []string `android:"arch_variant"` |
| 78 | |
Ivan Lozano | c564d2d | 2020-08-04 15:43:37 -0400 | [diff] [blame^] | 79 | // 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]" |
| 83 | Custom_bindgen string `android:"path"` |
| 84 | |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 85 | //TODO(b/161141999) Add support for headers from cc_library_header modules. |
| 86 | } |
| 87 | |
| 88 | type bindgenDecorator struct { |
| 89 | *baseSourceProvider |
| 90 | |
| 91 | Properties BindgenProperties |
| 92 | } |
| 93 | |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 94 | func (b *bindgenDecorator) generateSource(ctx android.ModuleContext, deps PathDeps) android.Path { |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 95 | ccToolchain := ccConfig.FindToolchain(ctx.Os(), ctx.Arch()) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 96 | |
| 97 | var cflags []string |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 98 | var implicits android.Paths |
| 99 | |
| 100 | implicits = append(implicits, deps.depIncludePaths...) |
| 101 | implicits = append(implicits, deps.depSystemIncludePaths...) |
| 102 | |
| 103 | // Default clang flags |
| 104 | cflags = append(cflags, "${ccConfig.CommonClangGlobalCflags}") |
| 105 | if ctx.Device() { |
| 106 | cflags = append(cflags, "${ccConfig.DeviceClangGlobalCflags}") |
| 107 | } |
| 108 | |
| 109 | // Toolchain clang flags |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 110 | cflags = append(cflags, "-target "+ccToolchain.ClangTriple()) |
| 111 | cflags = append(cflags, strings.ReplaceAll(ccToolchain.ToolchainClangCflags(), "${config.", "${ccConfig.")) |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 112 | |
| 113 | // Dependency clang flags and include paths |
| 114 | cflags = append(cflags, deps.depClangFlags...) |
| 115 | for _, include := range deps.depIncludePaths { |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 116 | cflags = append(cflags, "-I"+include.String()) |
| 117 | } |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 118 | for _, include := range deps.depSystemIncludePaths { |
| 119 | cflags = append(cflags, "-isystem "+include.String()) |
| 120 | } |
| 121 | |
| 122 | // Module defined clang flags and include paths |
| 123 | cflags = append(cflags, 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 |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 130 | bindgenFlags = append(bindgenFlags, strings.Join(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 | |
| 137 | outputFile := android.PathForModuleOut(ctx, b.baseSourceProvider.getStem(ctx)+".rs") |
| 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 | |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 161 | b.baseSourceProvider.outputFile = outputFile |
| 162 | return outputFile |
| 163 | } |
| 164 | |
| 165 | func (b *bindgenDecorator) sourceProviderProps() []interface{} { |
| 166 | return append(b.baseSourceProvider.sourceProviderProps(), |
| 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 |
| 172 | // the header and generated source is appropriately handled. |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 173 | func RustBindgenFactory() android.Module { |
| 174 | module, _ := NewRustBindgen(android.HostAndDeviceSupported) |
| 175 | return module.Init() |
| 176 | } |
| 177 | |
Ivan Lozano | f6fe995 | 2020-07-22 16:30:20 -0400 | [diff] [blame] | 178 | func RustBindgenHostFactory() android.Module { |
| 179 | module, _ := NewRustBindgen(android.HostSupported) |
| 180 | return module.Init() |
| 181 | } |
| 182 | |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 183 | func NewRustBindgen(hod android.HostOrDeviceSupported) (*Module, *bindgenDecorator) { |
| 184 | module := newModule(hod, android.MultilibBoth) |
| 185 | |
| 186 | bindgen := &bindgenDecorator{ |
| 187 | baseSourceProvider: NewSourceProvider(), |
| 188 | Properties: BindgenProperties{}, |
| 189 | } |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 190 | |
| 191 | _, library := NewRustLibrary(hod) |
| 192 | library.BuildOnlyRust() |
Stephen Crane | da931d4 | 2020-08-04 13:02:28 -0700 | [diff] [blame] | 193 | library.setNoLint() |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 194 | library.sourceProvider = bindgen |
| 195 | |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 196 | module.sourceProvider = bindgen |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 197 | module.compiler = library |
Ivan Lozano | 32267c8 | 2020-08-04 16:27:16 -0400 | [diff] [blame] | 198 | module.setClippy(false) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 199 | |
| 200 | return module, bindgen |
| 201 | } |
| 202 | |
| 203 | func (b *bindgenDecorator) sourceProviderDeps(ctx DepsContext, deps Deps) Deps { |
| 204 | deps = b.baseSourceProvider.sourceProviderDeps(ctx, deps) |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 205 | if ctx.toolchain().Bionic() { |
| 206 | deps = bionicDeps(deps) |
| 207 | } |
| 208 | |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 209 | deps.SharedLibs = append(deps.SharedLibs, b.Properties.Shared_libs...) |
| 210 | deps.StaticLibs = append(deps.StaticLibs, b.Properties.Static_libs...) |
| 211 | return deps |
| 212 | } |