blob: 4f8d22b1f058decd9cda785de8c863e2983afec5 [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
Ivan Lozano57f434e2020-10-28 09:32:10 -040033 // The first file in OutputFiles must be the library entry point.
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -070034 OutputFiles android.Paths
Andrei Homescuc7767922020-08-05 06:36:19 -070035 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040036 subName string
Ivan Lozano4fef93c2020-07-08 08:39:44 -040037}
38
Andrei Homescuc7767922020-08-05 06:36:19 -070039var _ SourceProvider = (*BaseSourceProvider)(nil)
Ivan Lozano4fef93c2020-07-08 08:39:44 -040040
41type SourceProvider interface {
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020042 GenerateSource(ctx ModuleContext, deps PathDeps) android.Path
Ivan Lozano4fef93c2020-07-08 08:39:44 -040043 Srcs() android.Paths
Andrei Homescuc7767922020-08-05 06:36:19 -070044 SourceProviderProps() []interface{}
45 SourceProviderDeps(ctx DepsContext, deps Deps) Deps
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040046 setSubName(subName string)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -070047 setOutputFiles(outputFiles android.Paths)
Ivan Lozano4fef93c2020-07-08 08:39:44 -040048}
49
Andrei Homescuc7767922020-08-05 06:36:19 -070050func (sp *BaseSourceProvider) Srcs() android.Paths {
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -070051 return sp.OutputFiles
Ivan Lozano4fef93c2020-07-08 08:39:44 -040052}
53
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020054func (sp *BaseSourceProvider) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path {
Andrei Homescuc7767922020-08-05 06:36:19 -070055 panic("BaseSourceProviderModule does not implement GenerateSource()")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040056}
57
Andrei Homescuc7767922020-08-05 06:36:19 -070058func (sp *BaseSourceProvider) SourceProviderProps() []interface{} {
Ivan Lozano4fef93c2020-07-08 08:39:44 -040059 return []interface{}{&sp.Properties}
60}
61
Andrei Homescuc7767922020-08-05 06:36:19 -070062func NewSourceProvider() *BaseSourceProvider {
63 return &BaseSourceProvider{
Ivan Lozano4fef93c2020-07-08 08:39:44 -040064 Properties: SourceProviderProperties{},
65 }
66}
67
Matthew Maurere94f3e72022-08-10 20:25:50 +000068func NewSourceProviderModule(hod android.HostOrDeviceSupported, sourceProvider SourceProvider, enableLints bool, rlibOnly bool) *Module {
Andrei Homescuc7767922020-08-05 06:36:19 -070069 _, library := NewRustLibrary(hod)
70 library.BuildOnlyRust()
Matthew Maurere94f3e72022-08-10 20:25:50 +000071 if rlibOnly {
72 library.BuildOnlyRlib()
73 }
Andrei Homescuc7767922020-08-05 06:36:19 -070074 library.sourceProvider = sourceProvider
75
76 module := newModule(hod, android.MultilibBoth)
77 module.sourceProvider = sourceProvider
78 module.compiler = library
79
80 if !enableLints {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020081 library.disableLints()
82 module.disableClippy()
Andrei Homescuc7767922020-08-05 06:36:19 -070083 }
84
85 return module
86}
87
88func (sp *BaseSourceProvider) getStem(ctx android.ModuleContext) string {
Ivan Lozanoff3a5b32020-08-05 13:28:32 -040089 if String(sp.Properties.Source_stem) == "" {
90 ctx.PropertyErrorf("source_stem",
91 "source_stem property is undefined but required for rust_bindgen modules")
Ivan Lozano4fef93c2020-07-08 08:39:44 -040092 }
Ivan Lozanoff3a5b32020-08-05 13:28:32 -040093 return String(sp.Properties.Source_stem)
Ivan Lozano4fef93c2020-07-08 08:39:44 -040094}
95
Andrei Homescuc7767922020-08-05 06:36:19 -070096func (sp *BaseSourceProvider) SourceProviderDeps(ctx DepsContext, deps Deps) Deps {
Ivan Lozano4fef93c2020-07-08 08:39:44 -040097 return deps
98}
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040099
Andrei Homescuc7767922020-08-05 06:36:19 -0700100func (sp *BaseSourceProvider) setSubName(subName string) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400101 sp.subName = subName
102}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200103
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700104func (sp *BaseSourceProvider) setOutputFiles(outputFiles android.Paths) {
105 sp.OutputFiles = outputFiles
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200106}