blob: 35a807bfd904d2cf2991e8a343216893ea9fff3b [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.
Stephen Hinesa3faafa2020-11-09 16:28:23 -080032 bindgenClangVersion = "clang-r399163b"
Ivan Lozano4fef93c2020-07-08 08:39:44 -040033
34 //TODO(b/160803703) Use a prebuilt bindgen instead of the built bindgen.
Shao-Chuan Leeaa3231c2020-11-05 22:20:17 +090035 _ = pctx.HostBinToolVariable("bindgenCmd", "bindgen")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040036 _ = pctx.SourcePathVariable("bindgenClang",
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020037 "${cc_config.ClangBase}/${config.HostPrebuiltTag}/"+bindgenClangVersion+"/bin/clang")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040038 _ = pctx.SourcePathVariable("bindgenLibClang",
Ivan Lozano54a8ee12020-09-08 13:10:36 -040039 "${cc_config.ClangBase}/${config.HostPrebuiltTag}/"+bindgenClangVersion+"/lib64/")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040040
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 Lozanoc564d2d2020-08-04 15:43:37 -040045 "$cmd $flags $in -o $out -- -MD -MF $out.d $cflags",
46 CommandDeps: []string{"$cmd"},
Ivan Lozanoe1e844b2020-07-24 15:16:30 -040047 Deps: blueprint.DepsGCC,
48 Depfile: "$out.d",
Ivan Lozano4fef93c2020-07-08 08:39:44 -040049 },
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040050 "cmd", "flags", "cflags")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040051)
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 {
Ivan Lozano3d947522020-09-23 13:26:25 -040061 // The wrapper header file. By default this is assumed to be a C header unless the extension is ".hh" or ".hpp".
62 // This is used to specify how to interpret the header and determines which '-std' flag to use by default.
63 //
64 // If your C++ header must have some other extension, then the default behavior can be overridden by setting the
65 // cpp_std property.
Ivan Lozano4fef93c2020-07-08 08:39:44 -040066 Wrapper_src *string `android:"path,arch_variant"`
67
68 // list of bindgen-specific flags and options
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040069 Bindgen_flags []string `android:"arch_variant"`
Ivan Lozano4fef93c2020-07-08 08:39:44 -040070
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040071 // module name of a custom binary/script which should be used instead of the 'bindgen' binary. This custom
72 // binary must expect arguments in a similar fashion to bindgen, e.g.
73 //
74 // "my_bindgen [flags] wrapper_header.h -o [output_path] -- [clang flags]"
75 Custom_bindgen string `android:"path"`
Ivan Lozano4fef93c2020-07-08 08:39:44 -040076}
77
78type bindgenDecorator struct {
Andrei Homescuc7767922020-08-05 06:36:19 -070079 *BaseSourceProvider
Ivan Lozano4fef93c2020-07-08 08:39:44 -040080
Ivan Lozanobc9e4212020-09-25 16:08:34 -040081 Properties BindgenProperties
82 ClangProperties cc.RustBindgenClangProperties
Ivan Lozano4fef93c2020-07-08 08:39:44 -040083}
84
Ivan Lozano3d947522020-09-23 13:26:25 -040085func (b *bindgenDecorator) getStdVersion(ctx ModuleContext, src android.Path) (string, bool) {
86 // Assume headers are C headers
87 isCpp := false
88 stdVersion := ""
89
90 switch src.Ext() {
91 case ".hpp", ".hh":
92 isCpp = true
93 }
94
Ivan Lozanobc9e4212020-09-25 16:08:34 -040095 if String(b.ClangProperties.Cpp_std) != "" && String(b.ClangProperties.C_std) != "" {
Ivan Lozano3d947522020-09-23 13:26:25 -040096 ctx.PropertyErrorf("c_std", "c_std and cpp_std cannot both be defined at the same time.")
97 }
98
Ivan Lozanobc9e4212020-09-25 16:08:34 -040099 if String(b.ClangProperties.Cpp_std) != "" {
100 if String(b.ClangProperties.Cpp_std) == "experimental" {
Ivan Lozano3d947522020-09-23 13:26:25 -0400101 stdVersion = cc_config.ExperimentalCppStdVersion
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400102 } else if String(b.ClangProperties.Cpp_std) == "default" {
Ivan Lozano3d947522020-09-23 13:26:25 -0400103 stdVersion = cc_config.CppStdVersion
104 } else {
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400105 stdVersion = String(b.ClangProperties.Cpp_std)
Ivan Lozano3d947522020-09-23 13:26:25 -0400106 }
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400107 } else if b.ClangProperties.C_std != nil {
108 if String(b.ClangProperties.C_std) == "experimental" {
Ivan Lozano3d947522020-09-23 13:26:25 -0400109 stdVersion = cc_config.ExperimentalCStdVersion
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400110 } else if String(b.ClangProperties.C_std) == "default" {
Ivan Lozano3d947522020-09-23 13:26:25 -0400111 stdVersion = cc_config.CStdVersion
112 } else {
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400113 stdVersion = String(b.ClangProperties.C_std)
Ivan Lozano3d947522020-09-23 13:26:25 -0400114 }
115 } else if isCpp {
116 stdVersion = cc_config.CppStdVersion
117 } else {
118 stdVersion = cc_config.CStdVersion
119 }
120
121 return stdVersion, isCpp
122}
123
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200124func (b *bindgenDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path {
125 ccToolchain := ctx.RustModule().ccToolchain(ctx)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400126
127 var cflags []string
Ivan Lozano45901ed2020-07-24 16:05:01 -0400128 var implicits android.Paths
129
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400130 implicits = append(implicits, deps.depGeneratedHeaders...)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400131
132 // Default clang flags
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +0200133 cflags = append(cflags, "${cc_config.CommonClangGlobalCflags}")
Ivan Lozano45901ed2020-07-24 16:05:01 -0400134 if ctx.Device() {
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +0200135 cflags = append(cflags, "${cc_config.DeviceClangGlobalCflags}")
Ivan Lozano45901ed2020-07-24 16:05:01 -0400136 }
137
138 // Toolchain clang flags
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400139 cflags = append(cflags, "-target "+ccToolchain.ClangTriple())
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +0200140 cflags = append(cflags, strings.ReplaceAll(ccToolchain.ToolchainClangCflags(), "${config.", "${cc_config."))
Ivan Lozano45901ed2020-07-24 16:05:01 -0400141
142 // Dependency clang flags and include paths
143 cflags = append(cflags, deps.depClangFlags...)
144 for _, include := range deps.depIncludePaths {
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400145 cflags = append(cflags, "-I"+include.String())
146 }
Ivan Lozano45901ed2020-07-24 16:05:01 -0400147 for _, include := range deps.depSystemIncludePaths {
148 cflags = append(cflags, "-isystem "+include.String())
149 }
150
Stephen Crane12e2cb72020-08-04 12:26:10 -0700151 esc := proptools.NinjaAndShellEscapeList
152
Ivan Lozano0a2a1152020-10-16 10:49:08 -0400153 // Filter out invalid cflags
154 for _, flag := range b.ClangProperties.Cflags {
155 if flag == "-x c++" || flag == "-xc++" {
156 ctx.PropertyErrorf("cflags",
157 "-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'")
158 }
159 if strings.HasPrefix(flag, "-std=") {
160 ctx.PropertyErrorf("cflags",
161 "-std should not be specified in cflags; instead use c_std or cpp_std")
162 }
163 }
164
Ivan Lozano45901ed2020-07-24 16:05:01 -0400165 // Module defined clang flags and include paths
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400166 cflags = append(cflags, esc(b.ClangProperties.Cflags)...)
167 for _, include := range b.ClangProperties.Local_include_dirs {
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400168 cflags = append(cflags, "-I"+android.PathForModuleSrc(ctx, include).String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400169 implicits = append(implicits, android.PathForModuleSrc(ctx, include))
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400170 }
171
172 bindgenFlags := defaultBindgenFlags
Stephen Crane12e2cb72020-08-04 12:26:10 -0700173 bindgenFlags = append(bindgenFlags, esc(b.Properties.Bindgen_flags)...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400174
175 wrapperFile := android.OptionalPathForModuleSrc(ctx, b.Properties.Wrapper_src)
176 if !wrapperFile.Valid() {
177 ctx.PropertyErrorf("wrapper_src", "invalid path to wrapper source")
178 }
179
Ivan Lozano3d947522020-09-23 13:26:25 -0400180 // Add C std version flag
181 stdVersion, isCpp := b.getStdVersion(ctx, wrapperFile.Path())
182 cflags = append(cflags, "-std="+stdVersion)
183
184 // Specify the header source language to avoid ambiguity.
185 if isCpp {
186 cflags = append(cflags, "-x c++")
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400187 // Add any C++ only flags.
188 cflags = append(cflags, esc(b.ClangProperties.Cppflags)...)
Ivan Lozano3d947522020-09-23 13:26:25 -0400189 } else {
190 cflags = append(cflags, "-x c")
191 }
192
Andrei Homescuc7767922020-08-05 06:36:19 -0700193 outputFile := android.PathForModuleOut(ctx, b.BaseSourceProvider.getStem(ctx)+".rs")
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400194
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400195 var cmd, cmdDesc string
196 if b.Properties.Custom_bindgen != "" {
197 cmd = ctx.GetDirectDepWithTag(b.Properties.Custom_bindgen, customBindgenDepTag).(*Module).HostToolPath().String()
198 cmdDesc = b.Properties.Custom_bindgen
199 } else {
200 cmd = "$bindgenCmd"
201 cmdDesc = "bindgen"
202 }
203
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400204 ctx.Build(pctx, android.BuildParams{
205 Rule: bindgen,
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400206 Description: strings.Join([]string{cmdDesc, wrapperFile.Path().Rel()}, " "),
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400207 Output: outputFile,
208 Input: wrapperFile.Path(),
Ivan Lozano45901ed2020-07-24 16:05:01 -0400209 Implicits: implicits,
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400210 Args: map[string]string{
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400211 "cmd": cmd,
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400212 "flags": strings.Join(bindgenFlags, " "),
213 "cflags": strings.Join(cflags, " "),
214 },
215 })
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400216
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700217 b.BaseSourceProvider.OutputFiles = android.Paths{outputFile}
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400218 return outputFile
219}
220
Andrei Homescuc7767922020-08-05 06:36:19 -0700221func (b *bindgenDecorator) SourceProviderProps() []interface{} {
222 return append(b.BaseSourceProvider.SourceProviderProps(),
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400223 &b.Properties, &b.ClangProperties)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400224}
225
Ivan Lozano10735d92020-07-22 09:14:47 -0400226// rust_bindgen generates Rust FFI bindings to C libraries using bindgen given a wrapper header as the primary input.
227// 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 +0200228// the header and generated source is appropriately handled. It is recommended to add it as a dependency in the
229// rlibs, dylibs or rustlibs property. It may also be added in the srcs property for external crates, using the ":"
230// prefix.
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400231func RustBindgenFactory() android.Module {
232 module, _ := NewRustBindgen(android.HostAndDeviceSupported)
233 return module.Init()
234}
235
Ivan Lozanof6fe9952020-07-22 16:30:20 -0400236func RustBindgenHostFactory() android.Module {
237 module, _ := NewRustBindgen(android.HostSupported)
238 return module.Init()
239}
240
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400241func NewRustBindgen(hod android.HostOrDeviceSupported) (*Module, *bindgenDecorator) {
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400242 bindgen := &bindgenDecorator{
Andrei Homescuc7767922020-08-05 06:36:19 -0700243 BaseSourceProvider: NewSourceProvider(),
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400244 Properties: BindgenProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400245 ClangProperties: cc.RustBindgenClangProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400246 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400247
Andrei Homescuc7767922020-08-05 06:36:19 -0700248 module := NewSourceProviderModule(hod, bindgen, false)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400249
250 return module, bindgen
251}
252
Andrei Homescuc7767922020-08-05 06:36:19 -0700253func (b *bindgenDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps {
254 deps = b.BaseSourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400255 if ctx.toolchain().Bionic() {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400256 deps = bionicDeps(deps, false)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400257 }
258
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400259 deps.SharedLibs = append(deps.SharedLibs, b.ClangProperties.Shared_libs...)
260 deps.StaticLibs = append(deps.StaticLibs, b.ClangProperties.Static_libs...)
Ivan Lozano9b443832020-11-17 13:39:30 -0500261 deps.HeaderLibs = append(deps.StaticLibs, b.ClangProperties.Header_libs...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400262 return deps
263}