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 | e1e844b | 2020-07-24 15:16:30 -0400 | [diff] [blame] | 45 | "$bindgenCmd $flags $in -o $out -- -MD -MF $out.d $cflags", |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 46 | CommandDeps: []string{"$bindgenCmd"}, |
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 | }, |
| 50 | "flags", "cflags") |
| 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 | |
| 80 | //TODO(b/161141999) Add support for headers from cc_library_header modules. |
| 81 | } |
| 82 | |
| 83 | type bindgenDecorator struct { |
| 84 | *baseSourceProvider |
| 85 | |
| 86 | Properties BindgenProperties |
| 87 | } |
| 88 | |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 89 | func (b *bindgenDecorator) generateSource(ctx android.ModuleContext, deps PathDeps) android.Path { |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 90 | ccToolchain := ccConfig.FindToolchain(ctx.Os(), ctx.Arch()) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 91 | |
| 92 | var cflags []string |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 93 | var implicits android.Paths |
| 94 | |
| 95 | implicits = append(implicits, deps.depIncludePaths...) |
| 96 | implicits = append(implicits, deps.depSystemIncludePaths...) |
| 97 | |
| 98 | // Default clang flags |
| 99 | cflags = append(cflags, "${ccConfig.CommonClangGlobalCflags}") |
| 100 | if ctx.Device() { |
| 101 | cflags = append(cflags, "${ccConfig.DeviceClangGlobalCflags}") |
| 102 | } |
| 103 | |
| 104 | // Toolchain clang flags |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 105 | cflags = append(cflags, "-target "+ccToolchain.ClangTriple()) |
| 106 | cflags = append(cflags, strings.ReplaceAll(ccToolchain.ToolchainClangCflags(), "${config.", "${ccConfig.")) |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 107 | |
| 108 | // Dependency clang flags and include paths |
| 109 | cflags = append(cflags, deps.depClangFlags...) |
| 110 | for _, include := range deps.depIncludePaths { |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 111 | cflags = append(cflags, "-I"+include.String()) |
| 112 | } |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 113 | for _, include := range deps.depSystemIncludePaths { |
| 114 | cflags = append(cflags, "-isystem "+include.String()) |
| 115 | } |
| 116 | |
Stephen Crane | 12e2cb7 | 2020-08-04 12:26:10 -0700 | [diff] [blame^] | 117 | esc := proptools.NinjaAndShellEscapeList |
| 118 | |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 119 | // Module defined clang flags and include paths |
Stephen Crane | 12e2cb7 | 2020-08-04 12:26:10 -0700 | [diff] [blame^] | 120 | cflags = append(cflags, esc(b.Properties.Cflags)...) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 121 | for _, include := range b.Properties.Local_include_dirs { |
| 122 | cflags = append(cflags, "-I"+android.PathForModuleSrc(ctx, include).String()) |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 123 | implicits = append(implicits, android.PathForModuleSrc(ctx, include)) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | bindgenFlags := defaultBindgenFlags |
Stephen Crane | 12e2cb7 | 2020-08-04 12:26:10 -0700 | [diff] [blame^] | 127 | bindgenFlags = append(bindgenFlags, esc(b.Properties.Bindgen_flags)...) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 128 | |
| 129 | wrapperFile := android.OptionalPathForModuleSrc(ctx, b.Properties.Wrapper_src) |
| 130 | if !wrapperFile.Valid() { |
| 131 | ctx.PropertyErrorf("wrapper_src", "invalid path to wrapper source") |
| 132 | } |
| 133 | |
| 134 | outputFile := android.PathForModuleOut(ctx, b.baseSourceProvider.getStem(ctx)+".rs") |
| 135 | |
| 136 | ctx.Build(pctx, android.BuildParams{ |
| 137 | Rule: bindgen, |
| 138 | Description: "bindgen " + wrapperFile.Path().Rel(), |
| 139 | Output: outputFile, |
| 140 | Input: wrapperFile.Path(), |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 141 | Implicits: implicits, |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 142 | Args: map[string]string{ |
| 143 | "flags": strings.Join(bindgenFlags, " "), |
| 144 | "cflags": strings.Join(cflags, " "), |
| 145 | }, |
| 146 | }) |
| 147 | b.baseSourceProvider.outputFile = outputFile |
| 148 | return outputFile |
| 149 | } |
| 150 | |
| 151 | func (b *bindgenDecorator) sourceProviderProps() []interface{} { |
| 152 | return append(b.baseSourceProvider.sourceProviderProps(), |
| 153 | &b.Properties) |
| 154 | } |
| 155 | |
Ivan Lozano | 10735d9 | 2020-07-22 09:14:47 -0400 | [diff] [blame] | 156 | // rust_bindgen generates Rust FFI bindings to C libraries using bindgen given a wrapper header as the primary input. |
| 157 | // Bindgen has a number of flags to control the generated source, and additional flags can be passed to clang to ensure |
| 158 | // the header and generated source is appropriately handled. |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 159 | func RustBindgenFactory() android.Module { |
| 160 | module, _ := NewRustBindgen(android.HostAndDeviceSupported) |
| 161 | return module.Init() |
| 162 | } |
| 163 | |
Ivan Lozano | f6fe995 | 2020-07-22 16:30:20 -0400 | [diff] [blame] | 164 | func RustBindgenHostFactory() android.Module { |
| 165 | module, _ := NewRustBindgen(android.HostSupported) |
| 166 | return module.Init() |
| 167 | } |
| 168 | |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 169 | func NewRustBindgen(hod android.HostOrDeviceSupported) (*Module, *bindgenDecorator) { |
| 170 | module := newModule(hod, android.MultilibBoth) |
| 171 | |
| 172 | bindgen := &bindgenDecorator{ |
| 173 | baseSourceProvider: NewSourceProvider(), |
| 174 | Properties: BindgenProperties{}, |
| 175 | } |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 176 | |
| 177 | _, library := NewRustLibrary(hod) |
| 178 | library.BuildOnlyRust() |
| 179 | library.sourceProvider = bindgen |
| 180 | |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 181 | module.sourceProvider = bindgen |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 182 | module.compiler = library |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 183 | |
| 184 | return module, bindgen |
| 185 | } |
| 186 | |
| 187 | func (b *bindgenDecorator) sourceProviderDeps(ctx DepsContext, deps Deps) Deps { |
| 188 | deps = b.baseSourceProvider.sourceProviderDeps(ctx, deps) |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 189 | if ctx.toolchain().Bionic() { |
| 190 | deps = bionicDeps(deps) |
| 191 | } |
| 192 | |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 193 | deps.SharedLibs = append(deps.SharedLibs, b.Properties.Shared_libs...) |
| 194 | deps.StaticLibs = append(deps.StaticLibs, b.Properties.Static_libs...) |
| 195 | return deps |
| 196 | } |