blob: 755a369a72b5ccc25caa0c11994ac1c09ea4ab52 [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 "android/soong/android"
19)
20
21type SourceProviderProperties struct {
Ivan Lozanoff3a5b32020-08-05 13:28:32 -040022 // filename for the generated source file (<source_stem>.rs). This field is required.
23 // The inherited "stem" property sets the output filename for the generated library variants only.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040024 Source_stem *string `android:"arch_variant"`
25
26 // crate name, used for the library variant of this source provider. See additional details in rust_library.
27 Crate_name string `android:"arch_variant"`
Ivan Lozano4fef93c2020-07-08 08:39:44 -040028}
29
Andrei Homescuc7767922020-08-05 06:36:19 -070030type BaseSourceProvider struct {
Ivan Lozano4fef93c2020-07-08 08:39:44 -040031 Properties SourceProviderProperties
32
Andrei Homescuc7767922020-08-05 06:36:19 -070033 OutputFile android.Path
34 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040035 subName string
Ivan Lozano4fef93c2020-07-08 08:39:44 -040036}
37
Andrei Homescuc7767922020-08-05 06:36:19 -070038var _ SourceProvider = (*BaseSourceProvider)(nil)
Ivan Lozano4fef93c2020-07-08 08:39:44 -040039
40type SourceProvider interface {
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020041 GenerateSource(ctx ModuleContext, deps PathDeps) android.Path
Ivan Lozano4fef93c2020-07-08 08:39:44 -040042 Srcs() android.Paths
Andrei Homescuc7767922020-08-05 06:36:19 -070043 SourceProviderProps() []interface{}
44 SourceProviderDeps(ctx DepsContext, deps Deps) Deps
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040045 setSubName(subName string)
Ivan Lozano4fef93c2020-07-08 08:39:44 -040046}
47
Andrei Homescuc7767922020-08-05 06:36:19 -070048func (sp *BaseSourceProvider) Srcs() android.Paths {
49 return android.Paths{sp.OutputFile}
Ivan Lozano4fef93c2020-07-08 08:39:44 -040050}
51
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020052func (sp *BaseSourceProvider) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path {
Andrei Homescuc7767922020-08-05 06:36:19 -070053 panic("BaseSourceProviderModule does not implement GenerateSource()")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040054}
55
Andrei Homescuc7767922020-08-05 06:36:19 -070056func (sp *BaseSourceProvider) SourceProviderProps() []interface{} {
Ivan Lozano4fef93c2020-07-08 08:39:44 -040057 return []interface{}{&sp.Properties}
58}
59
Andrei Homescuc7767922020-08-05 06:36:19 -070060func NewSourceProvider() *BaseSourceProvider {
61 return &BaseSourceProvider{
Ivan Lozano4fef93c2020-07-08 08:39:44 -040062 Properties: SourceProviderProperties{},
63 }
64}
65
Andrei Homescuc7767922020-08-05 06:36:19 -070066func NewSourceProviderModule(hod android.HostOrDeviceSupported, sourceProvider SourceProvider, enableLints bool) *Module {
67 _, library := NewRustLibrary(hod)
68 library.BuildOnlyRust()
69 library.sourceProvider = sourceProvider
70
71 module := newModule(hod, android.MultilibBoth)
72 module.sourceProvider = sourceProvider
73 module.compiler = library
74
75 if !enableLints {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020076 library.disableLints()
77 module.disableClippy()
Andrei Homescuc7767922020-08-05 06:36:19 -070078 }
79
80 return module
81}
82
83func (sp *BaseSourceProvider) getStem(ctx android.ModuleContext) string {
Ivan Lozanoff3a5b32020-08-05 13:28:32 -040084 if String(sp.Properties.Source_stem) == "" {
85 ctx.PropertyErrorf("source_stem",
86 "source_stem property is undefined but required for rust_bindgen modules")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040087 }
Ivan Lozanoff3a5b32020-08-05 13:28:32 -040088 return String(sp.Properties.Source_stem)
Ivan Lozano4fef93c2020-07-08 08:39:44 -040089}
90
Andrei Homescuc7767922020-08-05 06:36:19 -070091func (sp *BaseSourceProvider) SourceProviderDeps(ctx DepsContext, deps Deps) Deps {
Ivan Lozano4fef93c2020-07-08 08:39:44 -040092 return deps
93}
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040094
Andrei Homescuc7767922020-08-05 06:36:19 -070095func (sp *BaseSourceProvider) setSubName(subName string) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040096 sp.subName = subName
97}