blob: 64c559c51f812e0680e4b94b86fa6238aafa8c2f [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"
Stephen Crane12e2cb72020-08-04 12:26:10 -070021 "github.com/google/blueprint/proptools"
Ivan Lozano45901ed2020-07-24 16:05:01 -040022
Ivan Lozano4fef93c2020-07-08 08:39:44 -040023 "android/soong/android"
Ivan Lozano4fef93c2020-07-08 08:39:44 -040024 ccConfig "android/soong/cc/config"
25)
26
27var (
Ivan Lozanoec54eec2020-07-22 16:48:53 -040028 defaultBindgenFlags = []string{""}
Ivan Lozano4fef93c2020-07-08 08:39:44 -040029
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 Lozanoec54eec2020-07-22 16:48:53 -040044 Command: "CLANG_PATH=$bindgenClang LIBCLANG_PATH=$bindgenLibClang RUSTFMT=${config.RustBin}/rustfmt " +
Ivan Lozanoe1e844b2020-07-24 15:16:30 -040045 "$bindgenCmd $flags $in -o $out -- -MD -MF $out.d $cflags",
Ivan Lozano4fef93c2020-07-08 08:39:44 -040046 CommandDeps: []string{"$bindgenCmd"},
Ivan Lozanoe1e844b2020-07-24 15:16:30 -040047 Deps: blueprint.DepsGCC,
48 Depfile: "$out.d",
Ivan Lozano4fef93c2020-07-08 08:39:44 -040049 },
50 "flags", "cflags")
51)
52
53func init() {
54 android.RegisterModuleType("rust_bindgen", RustBindgenFactory)
Ivan Lozanof6fe9952020-07-22 16:30:20 -040055 android.RegisterModuleType("rust_bindgen_host", RustBindgenHostFactory)
Ivan Lozano4fef93c2020-07-08 08:39:44 -040056}
57
58var _ SourceProvider = (*bindgenDecorator)(nil)
59
60type 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 Lozano26ecd6c2020-07-31 13:40:31 -040065 Bindgen_flags []string `android:"arch_variant"`
Ivan Lozano4fef93c2020-07-08 08:39:44 -040066
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
83type bindgenDecorator struct {
84 *baseSourceProvider
85
86 Properties BindgenProperties
87}
88
Ivan Lozano45901ed2020-07-24 16:05:01 -040089func (b *bindgenDecorator) generateSource(ctx android.ModuleContext, deps PathDeps) android.Path {
Ivan Lozano4fef93c2020-07-08 08:39:44 -040090 ccToolchain := ccConfig.FindToolchain(ctx.Os(), ctx.Arch())
Ivan Lozano4fef93c2020-07-08 08:39:44 -040091
92 var cflags []string
Ivan Lozano45901ed2020-07-24 16:05:01 -040093 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 Lozano4fef93c2020-07-08 08:39:44 -0400105 cflags = append(cflags, "-target "+ccToolchain.ClangTriple())
106 cflags = append(cflags, strings.ReplaceAll(ccToolchain.ToolchainClangCflags(), "${config.", "${ccConfig."))
Ivan Lozano45901ed2020-07-24 16:05:01 -0400107
108 // Dependency clang flags and include paths
109 cflags = append(cflags, deps.depClangFlags...)
110 for _, include := range deps.depIncludePaths {
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400111 cflags = append(cflags, "-I"+include.String())
112 }
Ivan Lozano45901ed2020-07-24 16:05:01 -0400113 for _, include := range deps.depSystemIncludePaths {
114 cflags = append(cflags, "-isystem "+include.String())
115 }
116
Stephen Crane12e2cb72020-08-04 12:26:10 -0700117 esc := proptools.NinjaAndShellEscapeList
118
Ivan Lozano45901ed2020-07-24 16:05:01 -0400119 // Module defined clang flags and include paths
Stephen Crane12e2cb72020-08-04 12:26:10 -0700120 cflags = append(cflags, esc(b.Properties.Cflags)...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400121 for _, include := range b.Properties.Local_include_dirs {
122 cflags = append(cflags, "-I"+android.PathForModuleSrc(ctx, include).String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400123 implicits = append(implicits, android.PathForModuleSrc(ctx, include))
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400124 }
125
126 bindgenFlags := defaultBindgenFlags
Stephen Crane12e2cb72020-08-04 12:26:10 -0700127 bindgenFlags = append(bindgenFlags, esc(b.Properties.Bindgen_flags)...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400128
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 Lozano45901ed2020-07-24 16:05:01 -0400141 Implicits: implicits,
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400142 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
151func (b *bindgenDecorator) sourceProviderProps() []interface{} {
152 return append(b.baseSourceProvider.sourceProviderProps(),
153 &b.Properties)
154}
155
Ivan Lozano10735d92020-07-22 09:14:47 -0400156// 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 Lozano4fef93c2020-07-08 08:39:44 -0400159func RustBindgenFactory() android.Module {
160 module, _ := NewRustBindgen(android.HostAndDeviceSupported)
161 return module.Init()
162}
163
Ivan Lozanof6fe9952020-07-22 16:30:20 -0400164func RustBindgenHostFactory() android.Module {
165 module, _ := NewRustBindgen(android.HostSupported)
166 return module.Init()
167}
168
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400169func NewRustBindgen(hod android.HostOrDeviceSupported) (*Module, *bindgenDecorator) {
170 module := newModule(hod, android.MultilibBoth)
171
172 bindgen := &bindgenDecorator{
173 baseSourceProvider: NewSourceProvider(),
174 Properties: BindgenProperties{},
175 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400176
177 _, library := NewRustLibrary(hod)
178 library.BuildOnlyRust()
179 library.sourceProvider = bindgen
180
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400181 module.sourceProvider = bindgen
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400182 module.compiler = library
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400183
184 return module, bindgen
185}
186
187func (b *bindgenDecorator) sourceProviderDeps(ctx DepsContext, deps Deps) Deps {
188 deps = b.baseSourceProvider.sourceProviderDeps(ctx, deps)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400189 if ctx.toolchain().Bionic() {
190 deps = bionicDeps(deps)
191 }
192
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400193 deps.SharedLibs = append(deps.SharedLibs, b.Properties.Shared_libs...)
194 deps.StaticLibs = append(deps.StaticLibs, b.Properties.Static_libs...)
195 return deps
196}