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 ( |
| 18 | "github.com/google/blueprint" |
| 19 | "strings" |
| 20 | |
| 21 | "android/soong/android" |
| 22 | "android/soong/cc" |
| 23 | ccConfig "android/soong/cc/config" |
| 24 | ) |
| 25 | |
| 26 | var ( |
| 27 | defaultBindgenFlags = []string{"--no-rustfmt-bindings"} |
| 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{ |
| 43 | Command: "CLANG_PATH=$bindgenClang LIBCLANG_PATH=$bindgenLibClang $bindgenCmd $flags $in -o $out -- $cflags", |
| 44 | CommandDeps: []string{"$bindgenCmd"}, |
| 45 | }, |
| 46 | "flags", "cflags") |
| 47 | ) |
| 48 | |
| 49 | func init() { |
| 50 | android.RegisterModuleType("rust_bindgen", RustBindgenFactory) |
Ivan Lozano | f6fe995 | 2020-07-22 16:30:20 -0400 | [diff] [blame^] | 51 | android.RegisterModuleType("rust_bindgen_host", RustBindgenHostFactory) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | var _ SourceProvider = (*bindgenDecorator)(nil) |
| 55 | |
| 56 | type BindgenProperties struct { |
| 57 | // The wrapper header file |
| 58 | Wrapper_src *string `android:"path,arch_variant"` |
| 59 | |
| 60 | // list of bindgen-specific flags and options |
| 61 | Flags []string `android:"arch_variant"` |
| 62 | |
| 63 | // list of clang flags required to correctly interpret the headers. |
| 64 | Cflags []string `android:"arch_variant"` |
| 65 | |
| 66 | // list of directories relative to the Blueprints file that will |
| 67 | // be added to the include path using -I |
| 68 | Local_include_dirs []string `android:"arch_variant,variant_prepend"` |
| 69 | |
| 70 | // list of static libraries that provide headers for this binding. |
| 71 | Static_libs []string `android:"arch_variant,variant_prepend"` |
| 72 | |
| 73 | // list of shared libraries that provide headers for this binding. |
| 74 | Shared_libs []string `android:"arch_variant"` |
| 75 | |
| 76 | //TODO(b/161141999) Add support for headers from cc_library_header modules. |
| 77 | } |
| 78 | |
| 79 | type bindgenDecorator struct { |
| 80 | *baseSourceProvider |
| 81 | |
| 82 | Properties BindgenProperties |
| 83 | } |
| 84 | |
| 85 | func (b *bindgenDecorator) libraryExports(ctx android.ModuleContext) (android.Paths, []string) { |
| 86 | var libraryPaths android.Paths |
| 87 | var libraryFlags []string |
| 88 | |
| 89 | for _, static_lib := range b.Properties.Static_libs { |
| 90 | if dep, ok := ctx.GetDirectDepWithTag(static_lib, cc.StaticDepTag).(*cc.Module); ok { |
| 91 | libraryPaths = append(libraryPaths, dep.ExportedIncludeDirs()...) |
| 92 | libraryFlags = append(libraryFlags, dep.ExportedFlags()...) |
| 93 | } |
| 94 | } |
| 95 | for _, shared_lib := range b.Properties.Shared_libs { |
| 96 | if dep, ok := ctx.GetDirectDepWithTag(shared_lib, cc.SharedDepTag).(*cc.Module); ok { |
| 97 | libraryPaths = append(libraryPaths, dep.ExportedIncludeDirs()...) |
| 98 | libraryFlags = append(libraryFlags, dep.ExportedFlags()...) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return libraryPaths, libraryFlags |
| 103 | } |
| 104 | |
| 105 | func (b *bindgenDecorator) generateSource(ctx android.ModuleContext) android.Path { |
| 106 | ccToolchain := ccConfig.FindToolchain(ctx.Os(), ctx.Arch()) |
| 107 | includes, exportedFlags := b.libraryExports(ctx) |
| 108 | |
| 109 | var cflags []string |
| 110 | cflags = append(cflags, b.Properties.Cflags...) |
| 111 | cflags = append(cflags, "-target "+ccToolchain.ClangTriple()) |
| 112 | cflags = append(cflags, strings.ReplaceAll(ccToolchain.ToolchainClangCflags(), "${config.", "${ccConfig.")) |
| 113 | cflags = append(cflags, exportedFlags...) |
| 114 | for _, include := range includes { |
| 115 | cflags = append(cflags, "-I"+include.String()) |
| 116 | } |
| 117 | for _, include := range b.Properties.Local_include_dirs { |
| 118 | cflags = append(cflags, "-I"+android.PathForModuleSrc(ctx, include).String()) |
| 119 | } |
| 120 | |
| 121 | bindgenFlags := defaultBindgenFlags |
| 122 | bindgenFlags = append(bindgenFlags, strings.Join(b.Properties.Flags, " ")) |
| 123 | |
| 124 | wrapperFile := android.OptionalPathForModuleSrc(ctx, b.Properties.Wrapper_src) |
| 125 | if !wrapperFile.Valid() { |
| 126 | ctx.PropertyErrorf("wrapper_src", "invalid path to wrapper source") |
| 127 | } |
| 128 | |
| 129 | outputFile := android.PathForModuleOut(ctx, b.baseSourceProvider.getStem(ctx)+".rs") |
| 130 | |
| 131 | ctx.Build(pctx, android.BuildParams{ |
| 132 | Rule: bindgen, |
| 133 | Description: "bindgen " + wrapperFile.Path().Rel(), |
| 134 | Output: outputFile, |
| 135 | Input: wrapperFile.Path(), |
| 136 | Implicits: includes, |
| 137 | Args: map[string]string{ |
| 138 | "flags": strings.Join(bindgenFlags, " "), |
| 139 | "cflags": strings.Join(cflags, " "), |
| 140 | }, |
| 141 | }) |
| 142 | b.baseSourceProvider.outputFile = outputFile |
| 143 | return outputFile |
| 144 | } |
| 145 | |
| 146 | func (b *bindgenDecorator) sourceProviderProps() []interface{} { |
| 147 | return append(b.baseSourceProvider.sourceProviderProps(), |
| 148 | &b.Properties) |
| 149 | } |
| 150 | |
| 151 | func RustBindgenFactory() android.Module { |
| 152 | module, _ := NewRustBindgen(android.HostAndDeviceSupported) |
| 153 | return module.Init() |
| 154 | } |
| 155 | |
Ivan Lozano | f6fe995 | 2020-07-22 16:30:20 -0400 | [diff] [blame^] | 156 | func RustBindgenHostFactory() android.Module { |
| 157 | module, _ := NewRustBindgen(android.HostSupported) |
| 158 | return module.Init() |
| 159 | } |
| 160 | |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 161 | func NewRustBindgen(hod android.HostOrDeviceSupported) (*Module, *bindgenDecorator) { |
| 162 | module := newModule(hod, android.MultilibBoth) |
| 163 | |
| 164 | bindgen := &bindgenDecorator{ |
| 165 | baseSourceProvider: NewSourceProvider(), |
| 166 | Properties: BindgenProperties{}, |
| 167 | } |
| 168 | module.sourceProvider = bindgen |
| 169 | |
| 170 | return module, bindgen |
| 171 | } |
| 172 | |
| 173 | func (b *bindgenDecorator) sourceProviderDeps(ctx DepsContext, deps Deps) Deps { |
| 174 | deps = b.baseSourceProvider.sourceProviderDeps(ctx, deps) |
| 175 | deps.SharedLibs = append(deps.SharedLibs, b.Properties.Shared_libs...) |
| 176 | deps.StaticLibs = append(deps.StaticLibs, b.Properties.Static_libs...) |
| 177 | return deps |
| 178 | } |