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