blob: 4c2ff2ac525ad59dacf2add31aa6ed75328a57ab [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.
Ivan Lozano54a8ee12020-09-08 13:10:36 -040032 bindgenClangVersion = "clang-r383902c"
Ivan Lozano4fef93c2020-07-08 08:39:44 -040033
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",
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 Lozano45901ed2020-07-24 16:05:01 -0400153 // Module defined clang flags and include paths
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400154 cflags = append(cflags, esc(b.ClangProperties.Cflags)...)
155 for _, include := range b.ClangProperties.Local_include_dirs {
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400156 cflags = append(cflags, "-I"+android.PathForModuleSrc(ctx, include).String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400157 implicits = append(implicits, android.PathForModuleSrc(ctx, include))
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400158 }
159
160 bindgenFlags := defaultBindgenFlags
Stephen Crane12e2cb72020-08-04 12:26:10 -0700161 bindgenFlags = append(bindgenFlags, esc(b.Properties.Bindgen_flags)...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400162
163 wrapperFile := android.OptionalPathForModuleSrc(ctx, b.Properties.Wrapper_src)
164 if !wrapperFile.Valid() {
165 ctx.PropertyErrorf("wrapper_src", "invalid path to wrapper source")
166 }
167
Ivan Lozano3d947522020-09-23 13:26:25 -0400168 // Add C std version flag
169 stdVersion, isCpp := b.getStdVersion(ctx, wrapperFile.Path())
170 cflags = append(cflags, "-std="+stdVersion)
171
172 // Specify the header source language to avoid ambiguity.
173 if isCpp {
174 cflags = append(cflags, "-x c++")
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400175 // Add any C++ only flags.
176 cflags = append(cflags, esc(b.ClangProperties.Cppflags)...)
Ivan Lozano3d947522020-09-23 13:26:25 -0400177 } else {
178 cflags = append(cflags, "-x c")
179 }
180
Andrei Homescuc7767922020-08-05 06:36:19 -0700181 outputFile := android.PathForModuleOut(ctx, b.BaseSourceProvider.getStem(ctx)+".rs")
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400182
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400183 var cmd, cmdDesc string
184 if b.Properties.Custom_bindgen != "" {
185 cmd = ctx.GetDirectDepWithTag(b.Properties.Custom_bindgen, customBindgenDepTag).(*Module).HostToolPath().String()
186 cmdDesc = b.Properties.Custom_bindgen
187 } else {
188 cmd = "$bindgenCmd"
189 cmdDesc = "bindgen"
190 }
191
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400192 ctx.Build(pctx, android.BuildParams{
193 Rule: bindgen,
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400194 Description: strings.Join([]string{cmdDesc, wrapperFile.Path().Rel()}, " "),
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400195 Output: outputFile,
196 Input: wrapperFile.Path(),
Ivan Lozano45901ed2020-07-24 16:05:01 -0400197 Implicits: implicits,
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400198 Args: map[string]string{
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400199 "cmd": cmd,
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400200 "flags": strings.Join(bindgenFlags, " "),
201 "cflags": strings.Join(cflags, " "),
202 },
203 })
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400204
Andrei Homescuc7767922020-08-05 06:36:19 -0700205 b.BaseSourceProvider.OutputFile = outputFile
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400206 return outputFile
207}
208
Andrei Homescuc7767922020-08-05 06:36:19 -0700209func (b *bindgenDecorator) SourceProviderProps() []interface{} {
210 return append(b.BaseSourceProvider.SourceProviderProps(),
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400211 &b.Properties, &b.ClangProperties)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400212}
213
Ivan Lozano10735d92020-07-22 09:14:47 -0400214// rust_bindgen generates Rust FFI bindings to C libraries using bindgen given a wrapper header as the primary input.
215// 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 +0200216// the header and generated source is appropriately handled. It is recommended to add it as a dependency in the
217// rlibs, dylibs or rustlibs property. It may also be added in the srcs property for external crates, using the ":"
218// prefix.
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400219func RustBindgenFactory() android.Module {
220 module, _ := NewRustBindgen(android.HostAndDeviceSupported)
221 return module.Init()
222}
223
Ivan Lozanof6fe9952020-07-22 16:30:20 -0400224func RustBindgenHostFactory() android.Module {
225 module, _ := NewRustBindgen(android.HostSupported)
226 return module.Init()
227}
228
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400229func NewRustBindgen(hod android.HostOrDeviceSupported) (*Module, *bindgenDecorator) {
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400230 bindgen := &bindgenDecorator{
Andrei Homescuc7767922020-08-05 06:36:19 -0700231 BaseSourceProvider: NewSourceProvider(),
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400232 Properties: BindgenProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400233 ClangProperties: cc.RustBindgenClangProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400234 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400235
Andrei Homescuc7767922020-08-05 06:36:19 -0700236 module := NewSourceProviderModule(hod, bindgen, false)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400237
238 return module, bindgen
239}
240
Andrei Homescuc7767922020-08-05 06:36:19 -0700241func (b *bindgenDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps {
242 deps = b.BaseSourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400243 if ctx.toolchain().Bionic() {
244 deps = bionicDeps(deps)
245 }
246
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400247 deps.SharedLibs = append(deps.SharedLibs, b.ClangProperties.Shared_libs...)
248 deps.StaticLibs = append(deps.StaticLibs, b.ClangProperties.Static_libs...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400249 return deps
250}