blob: 403f4665a16d8fe4a42a68431c7f191c03bef95b [file] [log] [blame]
Ivan Lozano4fef93c2020-07-08 08:39:44 -04001// 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
15package rust
16
17import (
Ivan Lozano4fef93c2020-07-08 08:39:44 -040018 "strings"
19
Ivan Lozano45901ed2020-07-24 16:05:01 -040020 "github.com/google/blueprint"
21
Ivan Lozano4fef93c2020-07-08 08:39:44 -040022 "android/soong/android"
Ivan Lozano4fef93c2020-07-08 08:39:44 -040023 ccConfig "android/soong/cc/config"
24)
25
26var (
Ivan Lozanoec54eec2020-07-22 16:48:53 -040027 defaultBindgenFlags = []string{""}
Ivan Lozano4fef93c2020-07-08 08:39:44 -040028
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 Lozanoec54eec2020-07-22 16:48:53 -040043 Command: "CLANG_PATH=$bindgenClang LIBCLANG_PATH=$bindgenLibClang RUSTFMT=${config.RustBin}/rustfmt " +
Ivan Lozanoe1e844b2020-07-24 15:16:30 -040044 "$bindgenCmd $flags $in -o $out -- -MD -MF $out.d $cflags",
Ivan Lozano4fef93c2020-07-08 08:39:44 -040045 CommandDeps: []string{"$bindgenCmd"},
Ivan Lozanoe1e844b2020-07-24 15:16:30 -040046 Deps: blueprint.DepsGCC,
47 Depfile: "$out.d",
Ivan Lozano4fef93c2020-07-08 08:39:44 -040048 },
49 "flags", "cflags")
50)
51
52func init() {
53 android.RegisterModuleType("rust_bindgen", RustBindgenFactory)
Ivan Lozanof6fe9952020-07-22 16:30:20 -040054 android.RegisterModuleType("rust_bindgen_host", RustBindgenHostFactory)
Ivan Lozano4fef93c2020-07-08 08:39:44 -040055}
56
57var _ SourceProvider = (*bindgenDecorator)(nil)
58
59type 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 Lozano26ecd6c2020-07-31 13:40:31 -040064 Bindgen_flags []string `android:"arch_variant"`
Ivan Lozano4fef93c2020-07-08 08:39:44 -040065
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
79 //TODO(b/161141999) Add support for headers from cc_library_header modules.
80}
81
82type bindgenDecorator struct {
83 *baseSourceProvider
84
85 Properties BindgenProperties
86}
87
Ivan Lozano45901ed2020-07-24 16:05:01 -040088func (b *bindgenDecorator) generateSource(ctx android.ModuleContext, deps PathDeps) android.Path {
Ivan Lozano4fef93c2020-07-08 08:39:44 -040089 ccToolchain := ccConfig.FindToolchain(ctx.Os(), ctx.Arch())
Ivan Lozano4fef93c2020-07-08 08:39:44 -040090
91 var cflags []string
Ivan Lozano45901ed2020-07-24 16:05:01 -040092 var implicits android.Paths
93
94 implicits = append(implicits, deps.depIncludePaths...)
95 implicits = append(implicits, deps.depSystemIncludePaths...)
96
97 // Default clang flags
98 cflags = append(cflags, "${ccConfig.CommonClangGlobalCflags}")
99 if ctx.Device() {
100 cflags = append(cflags, "${ccConfig.DeviceClangGlobalCflags}")
101 }
102
103 // Toolchain clang flags
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400104 cflags = append(cflags, "-target "+ccToolchain.ClangTriple())
105 cflags = append(cflags, strings.ReplaceAll(ccToolchain.ToolchainClangCflags(), "${config.", "${ccConfig."))
Ivan Lozano45901ed2020-07-24 16:05:01 -0400106
107 // Dependency clang flags and include paths
108 cflags = append(cflags, deps.depClangFlags...)
109 for _, include := range deps.depIncludePaths {
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400110 cflags = append(cflags, "-I"+include.String())
111 }
Ivan Lozano45901ed2020-07-24 16:05:01 -0400112 for _, include := range deps.depSystemIncludePaths {
113 cflags = append(cflags, "-isystem "+include.String())
114 }
115
116 // Module defined clang flags and include paths
117 cflags = append(cflags, b.Properties.Cflags...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400118 for _, include := range b.Properties.Local_include_dirs {
119 cflags = append(cflags, "-I"+android.PathForModuleSrc(ctx, include).String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400120 implicits = append(implicits, android.PathForModuleSrc(ctx, include))
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400121 }
122
123 bindgenFlags := defaultBindgenFlags
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400124 bindgenFlags = append(bindgenFlags, strings.Join(b.Properties.Bindgen_flags, " "))
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400125
126 wrapperFile := android.OptionalPathForModuleSrc(ctx, b.Properties.Wrapper_src)
127 if !wrapperFile.Valid() {
128 ctx.PropertyErrorf("wrapper_src", "invalid path to wrapper source")
129 }
130
131 outputFile := android.PathForModuleOut(ctx, b.baseSourceProvider.getStem(ctx)+".rs")
132
133 ctx.Build(pctx, android.BuildParams{
134 Rule: bindgen,
135 Description: "bindgen " + wrapperFile.Path().Rel(),
136 Output: outputFile,
137 Input: wrapperFile.Path(),
Ivan Lozano45901ed2020-07-24 16:05:01 -0400138 Implicits: implicits,
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400139 Args: map[string]string{
140 "flags": strings.Join(bindgenFlags, " "),
141 "cflags": strings.Join(cflags, " "),
142 },
143 })
144 b.baseSourceProvider.outputFile = outputFile
145 return outputFile
146}
147
148func (b *bindgenDecorator) sourceProviderProps() []interface{} {
149 return append(b.baseSourceProvider.sourceProviderProps(),
150 &b.Properties)
151}
152
Ivan Lozano10735d92020-07-22 09:14:47 -0400153// rust_bindgen generates Rust FFI bindings to C libraries using bindgen given a wrapper header as the primary input.
154// Bindgen has a number of flags to control the generated source, and additional flags can be passed to clang to ensure
155// the header and generated source is appropriately handled.
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400156func RustBindgenFactory() android.Module {
157 module, _ := NewRustBindgen(android.HostAndDeviceSupported)
158 return module.Init()
159}
160
Ivan Lozanof6fe9952020-07-22 16:30:20 -0400161func RustBindgenHostFactory() android.Module {
162 module, _ := NewRustBindgen(android.HostSupported)
163 return module.Init()
164}
165
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400166func NewRustBindgen(hod android.HostOrDeviceSupported) (*Module, *bindgenDecorator) {
167 module := newModule(hod, android.MultilibBoth)
168
169 bindgen := &bindgenDecorator{
170 baseSourceProvider: NewSourceProvider(),
171 Properties: BindgenProperties{},
172 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400173
174 _, library := NewRustLibrary(hod)
175 library.BuildOnlyRust()
Stephen Craneda931d42020-08-04 13:02:28 -0700176 library.setNoLint()
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400177 library.sourceProvider = bindgen
178
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400179 module.sourceProvider = bindgen
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400180 module.compiler = library
Ivan Lozano32267c82020-08-04 16:27:16 -0400181 module.setClippy(false)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400182
183 return module, bindgen
184}
185
186func (b *bindgenDecorator) sourceProviderDeps(ctx DepsContext, deps Deps) Deps {
187 deps = b.baseSourceProvider.sourceProviderDeps(ctx, deps)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400188 if ctx.toolchain().Bionic() {
189 deps = bionicDeps(deps)
190 }
191
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400192 deps.SharedLibs = append(deps.SharedLibs, b.Properties.Shared_libs...)
193 deps.StaticLibs = append(deps.StaticLibs, b.Properties.Static_libs...)
194 return deps
195}