blob: 3470e51fe655b39dc28c492650f4ab31b1f0ef58 [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 Lozanobc9e4212020-09-25 16:08:34 -040024 "android/soong/cc"
Ivan Lozano3d947522020-09-23 13:26:25 -040025 cc_config "android/soong/cc/config"
Ivan Lozano4fef93c2020-07-08 08:39:44 -040026)
27
28var (
Ivan Lozanoec54eec2020-07-22 16:48:53 -040029 defaultBindgenFlags = []string{""}
Ivan Lozano4fef93c2020-07-08 08:39:44 -040030
31 // bindgen should specify its own Clang revision so updating Clang isn't potentially blocked on bindgen failures.
Pirama Arumuga Nainar655a7d52021-04-19 10:10:21 -070032 bindgenClangVersion = "clang-r416183b"
Ivan Lozano4fef93c2020-07-08 08:39:44 -040033
Peter Collingbourne9a868f12020-12-30 20:23:01 -080034 _ = pctx.VariableFunc("bindgenClangVersion", func(ctx android.PackageVarContext) string {
35 if override := ctx.Config().Getenv("LLVM_BINDGEN_PREBUILTS_VERSION"); override != "" {
36 return override
37 }
38 return bindgenClangVersion
39 })
40
Ivan Lozano4fef93c2020-07-08 08:39:44 -040041 //TODO(b/160803703) Use a prebuilt bindgen instead of the built bindgen.
Shao-Chuan Leeaa3231c2020-11-05 22:20:17 +090042 _ = pctx.HostBinToolVariable("bindgenCmd", "bindgen")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040043 _ = pctx.SourcePathVariable("bindgenClang",
Peter Collingbourne9a868f12020-12-30 20:23:01 -080044 "${cc_config.ClangBase}/${config.HostPrebuiltTag}/${bindgenClangVersion}/bin/clang")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040045 _ = pctx.SourcePathVariable("bindgenLibClang",
Peter Collingbourne9a868f12020-12-30 20:23:01 -080046 "${cc_config.ClangBase}/${config.HostPrebuiltTag}/${bindgenClangVersion}/lib64/")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040047
48 //TODO(ivanlozano) Switch this to RuleBuilder
49 bindgen = pctx.AndroidStaticRule("bindgen",
50 blueprint.RuleParams{
Ivan Lozanoec54eec2020-07-22 16:48:53 -040051 Command: "CLANG_PATH=$bindgenClang LIBCLANG_PATH=$bindgenLibClang RUSTFMT=${config.RustBin}/rustfmt " +
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040052 "$cmd $flags $in -o $out -- -MD -MF $out.d $cflags",
53 CommandDeps: []string{"$cmd"},
Ivan Lozanoe1e844b2020-07-24 15:16:30 -040054 Deps: blueprint.DepsGCC,
55 Depfile: "$out.d",
Ivan Lozano4fef93c2020-07-08 08:39:44 -040056 },
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040057 "cmd", "flags", "cflags")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040058)
59
60func init() {
61 android.RegisterModuleType("rust_bindgen", RustBindgenFactory)
Ivan Lozanof6fe9952020-07-22 16:30:20 -040062 android.RegisterModuleType("rust_bindgen_host", RustBindgenHostFactory)
Ivan Lozano4fef93c2020-07-08 08:39:44 -040063}
64
65var _ SourceProvider = (*bindgenDecorator)(nil)
66
67type BindgenProperties struct {
Ivan Lozano3d947522020-09-23 13:26:25 -040068 // The wrapper header file. By default this is assumed to be a C header unless the extension is ".hh" or ".hpp".
69 // This is used to specify how to interpret the header and determines which '-std' flag to use by default.
70 //
71 // If your C++ header must have some other extension, then the default behavior can be overridden by setting the
72 // cpp_std property.
Ivan Lozano4fef93c2020-07-08 08:39:44 -040073 Wrapper_src *string `android:"path,arch_variant"`
74
75 // list of bindgen-specific flags and options
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040076 Bindgen_flags []string `android:"arch_variant"`
Ivan Lozano4fef93c2020-07-08 08:39:44 -040077
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040078 // module name of a custom binary/script which should be used instead of the 'bindgen' binary. This custom
79 // binary must expect arguments in a similar fashion to bindgen, e.g.
80 //
81 // "my_bindgen [flags] wrapper_header.h -o [output_path] -- [clang flags]"
Liz Kammereda93982021-04-20 10:15:41 -040082 Custom_bindgen string
Ivan Lozano4fef93c2020-07-08 08:39:44 -040083}
84
85type bindgenDecorator struct {
Andrei Homescuc7767922020-08-05 06:36:19 -070086 *BaseSourceProvider
Ivan Lozano4fef93c2020-07-08 08:39:44 -040087
Ivan Lozanobc9e4212020-09-25 16:08:34 -040088 Properties BindgenProperties
89 ClangProperties cc.RustBindgenClangProperties
Ivan Lozano4fef93c2020-07-08 08:39:44 -040090}
91
Ivan Lozano3d947522020-09-23 13:26:25 -040092func (b *bindgenDecorator) getStdVersion(ctx ModuleContext, src android.Path) (string, bool) {
93 // Assume headers are C headers
94 isCpp := false
95 stdVersion := ""
96
97 switch src.Ext() {
98 case ".hpp", ".hh":
99 isCpp = true
100 }
101
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400102 if String(b.ClangProperties.Cpp_std) != "" && String(b.ClangProperties.C_std) != "" {
Ivan Lozano3d947522020-09-23 13:26:25 -0400103 ctx.PropertyErrorf("c_std", "c_std and cpp_std cannot both be defined at the same time.")
104 }
105
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400106 if String(b.ClangProperties.Cpp_std) != "" {
107 if String(b.ClangProperties.Cpp_std) == "experimental" {
Ivan Lozano3d947522020-09-23 13:26:25 -0400108 stdVersion = cc_config.ExperimentalCppStdVersion
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400109 } else if String(b.ClangProperties.Cpp_std) == "default" {
Ivan Lozano3d947522020-09-23 13:26:25 -0400110 stdVersion = cc_config.CppStdVersion
111 } else {
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400112 stdVersion = String(b.ClangProperties.Cpp_std)
Ivan Lozano3d947522020-09-23 13:26:25 -0400113 }
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400114 } else if b.ClangProperties.C_std != nil {
115 if String(b.ClangProperties.C_std) == "experimental" {
Ivan Lozano3d947522020-09-23 13:26:25 -0400116 stdVersion = cc_config.ExperimentalCStdVersion
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400117 } else if String(b.ClangProperties.C_std) == "default" {
Ivan Lozano3d947522020-09-23 13:26:25 -0400118 stdVersion = cc_config.CStdVersion
119 } else {
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400120 stdVersion = String(b.ClangProperties.C_std)
Ivan Lozano3d947522020-09-23 13:26:25 -0400121 }
122 } else if isCpp {
123 stdVersion = cc_config.CppStdVersion
124 } else {
125 stdVersion = cc_config.CStdVersion
126 }
127
128 return stdVersion, isCpp
129}
130
ThiƩbaud Weksteen31f1bb82020-08-27 13:37:29 +0200131func (b *bindgenDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path {
132 ccToolchain := ctx.RustModule().ccToolchain(ctx)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400133
134 var cflags []string
Ivan Lozano45901ed2020-07-24 16:05:01 -0400135 var implicits android.Paths
136
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400137 implicits = append(implicits, deps.depGeneratedHeaders...)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400138
139 // Default clang flags
Colin Cross0523ba22021-07-14 18:45:05 -0700140 cflags = append(cflags, "${cc_config.CommonGlobalCflags}")
Ivan Lozano45901ed2020-07-24 16:05:01 -0400141 if ctx.Device() {
Colin Cross0523ba22021-07-14 18:45:05 -0700142 cflags = append(cflags, "${cc_config.DeviceGlobalCflags}")
Ivan Lozano45901ed2020-07-24 16:05:01 -0400143 }
144
145 // Toolchain clang flags
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400146 cflags = append(cflags, "-target "+ccToolchain.ClangTriple())
Colin Cross33bac242021-07-14 17:03:16 -0700147 cflags = append(cflags, strings.ReplaceAll(ccToolchain.Cflags(), "${config.", "${cc_config."))
148 cflags = append(cflags, strings.ReplaceAll(ccToolchain.ToolchainCflags(), "${config.", "${cc_config."))
Ivan Lozano45901ed2020-07-24 16:05:01 -0400149
150 // Dependency clang flags and include paths
151 cflags = append(cflags, deps.depClangFlags...)
152 for _, include := range deps.depIncludePaths {
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400153 cflags = append(cflags, "-I"+include.String())
154 }
Ivan Lozano45901ed2020-07-24 16:05:01 -0400155 for _, include := range deps.depSystemIncludePaths {
156 cflags = append(cflags, "-isystem "+include.String())
157 }
158
Stephen Crane12e2cb72020-08-04 12:26:10 -0700159 esc := proptools.NinjaAndShellEscapeList
160
Ivan Lozano0a2a1152020-10-16 10:49:08 -0400161 // Filter out invalid cflags
162 for _, flag := range b.ClangProperties.Cflags {
163 if flag == "-x c++" || flag == "-xc++" {
164 ctx.PropertyErrorf("cflags",
165 "-x c++ should not be specified in cflags; setting cpp_std specifies this is a C++ header, or change the file extension to '.hpp' or '.hh'")
166 }
167 if strings.HasPrefix(flag, "-std=") {
168 ctx.PropertyErrorf("cflags",
169 "-std should not be specified in cflags; instead use c_std or cpp_std")
170 }
171 }
172
Ivan Lozano45901ed2020-07-24 16:05:01 -0400173 // Module defined clang flags and include paths
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400174 cflags = append(cflags, esc(b.ClangProperties.Cflags)...)
175 for _, include := range b.ClangProperties.Local_include_dirs {
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400176 cflags = append(cflags, "-I"+android.PathForModuleSrc(ctx, include).String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400177 implicits = append(implicits, android.PathForModuleSrc(ctx, include))
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400178 }
179
180 bindgenFlags := defaultBindgenFlags
Stephen Crane12e2cb72020-08-04 12:26:10 -0700181 bindgenFlags = append(bindgenFlags, esc(b.Properties.Bindgen_flags)...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400182
183 wrapperFile := android.OptionalPathForModuleSrc(ctx, b.Properties.Wrapper_src)
184 if !wrapperFile.Valid() {
185 ctx.PropertyErrorf("wrapper_src", "invalid path to wrapper source")
186 }
187
Ivan Lozano3d947522020-09-23 13:26:25 -0400188 // Add C std version flag
189 stdVersion, isCpp := b.getStdVersion(ctx, wrapperFile.Path())
190 cflags = append(cflags, "-std="+stdVersion)
191
192 // Specify the header source language to avoid ambiguity.
193 if isCpp {
194 cflags = append(cflags, "-x c++")
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400195 // Add any C++ only flags.
196 cflags = append(cflags, esc(b.ClangProperties.Cppflags)...)
Ivan Lozano3d947522020-09-23 13:26:25 -0400197 } else {
198 cflags = append(cflags, "-x c")
199 }
200
Andrei Homescuc7767922020-08-05 06:36:19 -0700201 outputFile := android.PathForModuleOut(ctx, b.BaseSourceProvider.getStem(ctx)+".rs")
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400202
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400203 var cmd, cmdDesc string
204 if b.Properties.Custom_bindgen != "" {
205 cmd = ctx.GetDirectDepWithTag(b.Properties.Custom_bindgen, customBindgenDepTag).(*Module).HostToolPath().String()
206 cmdDesc = b.Properties.Custom_bindgen
207 } else {
208 cmd = "$bindgenCmd"
209 cmdDesc = "bindgen"
210 }
211
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400212 ctx.Build(pctx, android.BuildParams{
213 Rule: bindgen,
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400214 Description: strings.Join([]string{cmdDesc, wrapperFile.Path().Rel()}, " "),
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400215 Output: outputFile,
216 Input: wrapperFile.Path(),
Ivan Lozano45901ed2020-07-24 16:05:01 -0400217 Implicits: implicits,
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400218 Args: map[string]string{
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400219 "cmd": cmd,
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400220 "flags": strings.Join(bindgenFlags, " "),
221 "cflags": strings.Join(cflags, " "),
222 },
223 })
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400224
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700225 b.BaseSourceProvider.OutputFiles = android.Paths{outputFile}
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400226 return outputFile
227}
228
Andrei Homescuc7767922020-08-05 06:36:19 -0700229func (b *bindgenDecorator) SourceProviderProps() []interface{} {
230 return append(b.BaseSourceProvider.SourceProviderProps(),
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400231 &b.Properties, &b.ClangProperties)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400232}
233
Ivan Lozano10735d92020-07-22 09:14:47 -0400234// rust_bindgen generates Rust FFI bindings to C libraries using bindgen given a wrapper header as the primary input.
235// Bindgen has a number of flags to control the generated source, and additional flags can be passed to clang to ensure
ThiƩbaud Weksteen295c72b2020-09-23 18:10:17 +0200236// the header and generated source is appropriately handled. It is recommended to add it as a dependency in the
237// rlibs, dylibs or rustlibs property. It may also be added in the srcs property for external crates, using the ":"
238// prefix.
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400239func RustBindgenFactory() android.Module {
240 module, _ := NewRustBindgen(android.HostAndDeviceSupported)
241 return module.Init()
242}
243
Ivan Lozanof6fe9952020-07-22 16:30:20 -0400244func RustBindgenHostFactory() android.Module {
245 module, _ := NewRustBindgen(android.HostSupported)
246 return module.Init()
247}
248
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400249func NewRustBindgen(hod android.HostOrDeviceSupported) (*Module, *bindgenDecorator) {
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400250 bindgen := &bindgenDecorator{
Andrei Homescuc7767922020-08-05 06:36:19 -0700251 BaseSourceProvider: NewSourceProvider(),
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400252 Properties: BindgenProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400253 ClangProperties: cc.RustBindgenClangProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400254 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400255
Andrei Homescuc7767922020-08-05 06:36:19 -0700256 module := NewSourceProviderModule(hod, bindgen, false)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400257
258 return module, bindgen
259}
260
Andrei Homescuc7767922020-08-05 06:36:19 -0700261func (b *bindgenDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps {
262 deps = b.BaseSourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400263 if ctx.toolchain().Bionic() {
ThiƩbaud Weksteenf1ff54a2021-03-22 14:24:54 +0100264 deps = bionicDeps(ctx, deps, false)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400265 }
266
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400267 deps.SharedLibs = append(deps.SharedLibs, b.ClangProperties.Shared_libs...)
268 deps.StaticLibs = append(deps.StaticLibs, b.ClangProperties.Static_libs...)
Ivan Lozano9b443832020-11-17 13:39:30 -0500269 deps.HeaderLibs = append(deps.StaticLibs, b.ClangProperties.Header_libs...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400270 return deps
271}