blob: 2b3bd514630f0597022e2875dabb9cebd2d4684c [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)
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",
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020036 "${cc_config.ClangBase}/${config.HostPrebuiltTag}/"+bindgenClangVersion+"/bin/clang")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040037 _ = pctx.SourcePathVariable("bindgenLibClang",
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020038 "${cc_config.ClangBase}/${config.HostPrebuiltTag}/"+bindgenClangVersion+"/lib64/libclang.so."+bindgenLibClangSoGit)
Ivan Lozano4fef93c2020-07-08 08:39:44 -040039
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 Lozanoc564d2d2020-08-04 15:43:37 -040044 "$cmd $flags $in -o $out -- -MD -MF $out.d $cflags",
45 CommandDeps: []string{"$cmd"},
Ivan Lozanoe1e844b2020-07-24 15:16:30 -040046 Deps: blueprint.DepsGCC,
47 Depfile: "$out.d",
Ivan Lozano4fef93c2020-07-08 08:39:44 -040048 },
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040049 "cmd", "flags", "cflags")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040050)
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
Ivan Lozanoc564d2d2020-08-04 15:43:37 -040079 // module name of a custom binary/script which should be used instead of the 'bindgen' binary. This custom
80 // binary must expect arguments in a similar fashion to bindgen, e.g.
81 //
82 // "my_bindgen [flags] wrapper_header.h -o [output_path] -- [clang flags]"
83 Custom_bindgen string `android:"path"`
84
Ivan Lozano4fef93c2020-07-08 08:39:44 -040085 //TODO(b/161141999) Add support for headers from cc_library_header modules.
86}
87
88type bindgenDecorator struct {
Andrei Homescuc7767922020-08-05 06:36:19 -070089 *BaseSourceProvider
Ivan Lozano4fef93c2020-07-08 08:39:44 -040090
91 Properties BindgenProperties
92}
93
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020094func (b *bindgenDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path {
95 ccToolchain := ctx.RustModule().ccToolchain(ctx)
Ivan Lozano4fef93c2020-07-08 08:39:44 -040096
97 var cflags []string
Ivan Lozano45901ed2020-07-24 16:05:01 -040098 var implicits android.Paths
99
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400100 implicits = append(implicits, deps.depGeneratedHeaders...)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400101
102 // Default clang flags
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +0200103 cflags = append(cflags, "${cc_config.CommonClangGlobalCflags}")
Ivan Lozano45901ed2020-07-24 16:05:01 -0400104 if ctx.Device() {
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +0200105 cflags = append(cflags, "${cc_config.DeviceClangGlobalCflags}")
Ivan Lozano45901ed2020-07-24 16:05:01 -0400106 }
107
108 // Toolchain clang flags
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400109 cflags = append(cflags, "-target "+ccToolchain.ClangTriple())
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +0200110 cflags = append(cflags, strings.ReplaceAll(ccToolchain.ToolchainClangCflags(), "${config.", "${cc_config."))
Ivan Lozano45901ed2020-07-24 16:05:01 -0400111
112 // Dependency clang flags and include paths
113 cflags = append(cflags, deps.depClangFlags...)
114 for _, include := range deps.depIncludePaths {
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400115 cflags = append(cflags, "-I"+include.String())
116 }
Ivan Lozano45901ed2020-07-24 16:05:01 -0400117 for _, include := range deps.depSystemIncludePaths {
118 cflags = append(cflags, "-isystem "+include.String())
119 }
120
Stephen Crane12e2cb72020-08-04 12:26:10 -0700121 esc := proptools.NinjaAndShellEscapeList
122
Ivan Lozano45901ed2020-07-24 16:05:01 -0400123 // Module defined clang flags and include paths
Stephen Crane12e2cb72020-08-04 12:26:10 -0700124 cflags = append(cflags, esc(b.Properties.Cflags)...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400125 for _, include := range b.Properties.Local_include_dirs {
126 cflags = append(cflags, "-I"+android.PathForModuleSrc(ctx, include).String())
Ivan Lozano45901ed2020-07-24 16:05:01 -0400127 implicits = append(implicits, android.PathForModuleSrc(ctx, include))
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400128 }
129
130 bindgenFlags := defaultBindgenFlags
Stephen Crane12e2cb72020-08-04 12:26:10 -0700131 bindgenFlags = append(bindgenFlags, esc(b.Properties.Bindgen_flags)...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400132
133 wrapperFile := android.OptionalPathForModuleSrc(ctx, b.Properties.Wrapper_src)
134 if !wrapperFile.Valid() {
135 ctx.PropertyErrorf("wrapper_src", "invalid path to wrapper source")
136 }
137
Andrei Homescuc7767922020-08-05 06:36:19 -0700138 outputFile := android.PathForModuleOut(ctx, b.BaseSourceProvider.getStem(ctx)+".rs")
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400139
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400140 var cmd, cmdDesc string
141 if b.Properties.Custom_bindgen != "" {
142 cmd = ctx.GetDirectDepWithTag(b.Properties.Custom_bindgen, customBindgenDepTag).(*Module).HostToolPath().String()
143 cmdDesc = b.Properties.Custom_bindgen
144 } else {
145 cmd = "$bindgenCmd"
146 cmdDesc = "bindgen"
147 }
148
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400149 ctx.Build(pctx, android.BuildParams{
150 Rule: bindgen,
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400151 Description: strings.Join([]string{cmdDesc, wrapperFile.Path().Rel()}, " "),
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400152 Output: outputFile,
153 Input: wrapperFile.Path(),
Ivan Lozano45901ed2020-07-24 16:05:01 -0400154 Implicits: implicits,
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400155 Args: map[string]string{
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400156 "cmd": cmd,
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400157 "flags": strings.Join(bindgenFlags, " "),
158 "cflags": strings.Join(cflags, " "),
159 },
160 })
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400161
Andrei Homescuc7767922020-08-05 06:36:19 -0700162 b.BaseSourceProvider.OutputFile = outputFile
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400163 return outputFile
164}
165
Andrei Homescuc7767922020-08-05 06:36:19 -0700166func (b *bindgenDecorator) SourceProviderProps() []interface{} {
167 return append(b.BaseSourceProvider.SourceProviderProps(),
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400168 &b.Properties)
169}
170
Ivan Lozano10735d92020-07-22 09:14:47 -0400171// rust_bindgen generates Rust FFI bindings to C libraries using bindgen given a wrapper header as the primary input.
172// Bindgen has a number of flags to control the generated source, and additional flags can be passed to clang to ensure
173// the header and generated source is appropriately handled.
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400174func RustBindgenFactory() android.Module {
175 module, _ := NewRustBindgen(android.HostAndDeviceSupported)
176 return module.Init()
177}
178
Ivan Lozanof6fe9952020-07-22 16:30:20 -0400179func RustBindgenHostFactory() android.Module {
180 module, _ := NewRustBindgen(android.HostSupported)
181 return module.Init()
182}
183
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400184func NewRustBindgen(hod android.HostOrDeviceSupported) (*Module, *bindgenDecorator) {
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400185 bindgen := &bindgenDecorator{
Andrei Homescuc7767922020-08-05 06:36:19 -0700186 BaseSourceProvider: NewSourceProvider(),
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400187 Properties: BindgenProperties{},
188 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400189
Andrei Homescuc7767922020-08-05 06:36:19 -0700190 module := NewSourceProviderModule(hod, bindgen, false)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400191
192 return module, bindgen
193}
194
Andrei Homescuc7767922020-08-05 06:36:19 -0700195func (b *bindgenDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps {
196 deps = b.BaseSourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400197 if ctx.toolchain().Bionic() {
198 deps = bionicDeps(deps)
199 }
200
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400201 deps.SharedLibs = append(deps.SharedLibs, b.Properties.Shared_libs...)
202 deps.StaticLibs = append(deps.StaticLibs, b.Properties.Static_libs...)
203 return deps
204}