blob: 859223abf6a995408d0cf9bae88e1279e519368a [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 (
18 "github.com/google/blueprint"
19 "strings"
20
21 "android/soong/android"
22 "android/soong/cc"
23 ccConfig "android/soong/cc/config"
24)
25
26var (
27 defaultBindgenFlags = []string{"--no-rustfmt-bindings"}
28
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",
36 "${ccConfig.ClangBase}/${config.HostPrebuiltTag}/"+bindgenClangVersion+"/bin/clang")
37 _ = pctx.SourcePathVariable("bindgenLibClang",
38 "${ccConfig.ClangBase}/${config.HostPrebuiltTag}/"+bindgenClangVersion+"/lib64/libclang.so."+bindgenLibClangSoGit)
39
40 //TODO(ivanlozano) Switch this to RuleBuilder
41 bindgen = pctx.AndroidStaticRule("bindgen",
42 blueprint.RuleParams{
43 Command: "CLANG_PATH=$bindgenClang LIBCLANG_PATH=$bindgenLibClang $bindgenCmd $flags $in -o $out -- $cflags",
44 CommandDeps: []string{"$bindgenCmd"},
45 },
46 "flags", "cflags")
47)
48
49func init() {
50 android.RegisterModuleType("rust_bindgen", RustBindgenFactory)
51}
52
53var _ SourceProvider = (*bindgenDecorator)(nil)
54
55type BindgenProperties struct {
56 // The wrapper header file
57 Wrapper_src *string `android:"path,arch_variant"`
58
59 // list of bindgen-specific flags and options
60 Flags []string `android:"arch_variant"`
61
62 // list of clang flags required to correctly interpret the headers.
63 Cflags []string `android:"arch_variant"`
64
65 // list of directories relative to the Blueprints file that will
66 // be added to the include path using -I
67 Local_include_dirs []string `android:"arch_variant,variant_prepend"`
68
69 // list of static libraries that provide headers for this binding.
70 Static_libs []string `android:"arch_variant,variant_prepend"`
71
72 // list of shared libraries that provide headers for this binding.
73 Shared_libs []string `android:"arch_variant"`
74
75 //TODO(b/161141999) Add support for headers from cc_library_header modules.
76}
77
78type bindgenDecorator struct {
79 *baseSourceProvider
80
81 Properties BindgenProperties
82}
83
84func (b *bindgenDecorator) libraryExports(ctx android.ModuleContext) (android.Paths, []string) {
85 var libraryPaths android.Paths
86 var libraryFlags []string
87
88 for _, static_lib := range b.Properties.Static_libs {
89 if dep, ok := ctx.GetDirectDepWithTag(static_lib, cc.StaticDepTag).(*cc.Module); ok {
90 libraryPaths = append(libraryPaths, dep.ExportedIncludeDirs()...)
91 libraryFlags = append(libraryFlags, dep.ExportedFlags()...)
92 }
93 }
94 for _, shared_lib := range b.Properties.Shared_libs {
95 if dep, ok := ctx.GetDirectDepWithTag(shared_lib, cc.SharedDepTag).(*cc.Module); ok {
96 libraryPaths = append(libraryPaths, dep.ExportedIncludeDirs()...)
97 libraryFlags = append(libraryFlags, dep.ExportedFlags()...)
98 }
99 }
100
101 return libraryPaths, libraryFlags
102}
103
104func (b *bindgenDecorator) generateSource(ctx android.ModuleContext) android.Path {
105 ccToolchain := ccConfig.FindToolchain(ctx.Os(), ctx.Arch())
106 includes, exportedFlags := b.libraryExports(ctx)
107
108 var cflags []string
109 cflags = append(cflags, b.Properties.Cflags...)
110 cflags = append(cflags, "-target "+ccToolchain.ClangTriple())
111 cflags = append(cflags, strings.ReplaceAll(ccToolchain.ToolchainClangCflags(), "${config.", "${ccConfig."))
112 cflags = append(cflags, exportedFlags...)
113 for _, include := range includes {
114 cflags = append(cflags, "-I"+include.String())
115 }
116 for _, include := range b.Properties.Local_include_dirs {
117 cflags = append(cflags, "-I"+android.PathForModuleSrc(ctx, include).String())
118 }
119
120 bindgenFlags := defaultBindgenFlags
121 bindgenFlags = append(bindgenFlags, strings.Join(b.Properties.Flags, " "))
122
123 wrapperFile := android.OptionalPathForModuleSrc(ctx, b.Properties.Wrapper_src)
124 if !wrapperFile.Valid() {
125 ctx.PropertyErrorf("wrapper_src", "invalid path to wrapper source")
126 }
127
128 outputFile := android.PathForModuleOut(ctx, b.baseSourceProvider.getStem(ctx)+".rs")
129
130 ctx.Build(pctx, android.BuildParams{
131 Rule: bindgen,
132 Description: "bindgen " + wrapperFile.Path().Rel(),
133 Output: outputFile,
134 Input: wrapperFile.Path(),
135 Implicits: includes,
136 Args: map[string]string{
137 "flags": strings.Join(bindgenFlags, " "),
138 "cflags": strings.Join(cflags, " "),
139 },
140 })
141 b.baseSourceProvider.outputFile = outputFile
142 return outputFile
143}
144
145func (b *bindgenDecorator) sourceProviderProps() []interface{} {
146 return append(b.baseSourceProvider.sourceProviderProps(),
147 &b.Properties)
148}
149
150func RustBindgenFactory() android.Module {
151 module, _ := NewRustBindgen(android.HostAndDeviceSupported)
152 return module.Init()
153}
154
155func NewRustBindgen(hod android.HostOrDeviceSupported) (*Module, *bindgenDecorator) {
156 module := newModule(hod, android.MultilibBoth)
157
158 bindgen := &bindgenDecorator{
159 baseSourceProvider: NewSourceProvider(),
160 Properties: BindgenProperties{},
161 }
162 module.sourceProvider = bindgen
163
164 return module, bindgen
165}
166
167func (b *bindgenDecorator) sourceProviderDeps(ctx DepsContext, deps Deps) Deps {
168 deps = b.baseSourceProvider.sourceProviderDeps(ctx, deps)
169 deps.SharedLibs = append(deps.SharedLibs, b.Properties.Shared_libs...)
170 deps.StaticLibs = append(deps.StaticLibs, b.Properties.Static_libs...)
171 return deps
172}