1// 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 {
22	// 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.
24	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"`
28}
29
30type BaseSourceProvider struct {
31	Properties SourceProviderProperties
32
33	// The first file in OutputFiles must be the library entry point.
34	OutputFiles android.Paths
35	subName     string
36}
37
38var _ SourceProvider = (*BaseSourceProvider)(nil)
39
40type SourceProvider interface {
41	GenerateSource(ctx ModuleContext, deps PathDeps) android.Path
42	Srcs() android.Paths
43	SourceProviderProps() []interface{}
44	SourceProviderDeps(ctx DepsContext, deps Deps) Deps
45	setSubName(subName string)
46	setOutputFiles(outputFiles android.Paths)
47}
48
49func (sp *BaseSourceProvider) Srcs() android.Paths {
50	return sp.OutputFiles
51}
52
53func (sp *BaseSourceProvider) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path {
54	panic("BaseSourceProviderModule does not implement GenerateSource()")
55}
56
57func (sp *BaseSourceProvider) SourceProviderProps() []interface{} {
58	return []interface{}{&sp.Properties}
59}
60
61func NewSourceProvider() *BaseSourceProvider {
62	return &BaseSourceProvider{
63		Properties: SourceProviderProperties{},
64	}
65}
66
67func NewSourceProviderModule(hod android.HostOrDeviceSupported, sourceProvider SourceProvider, enableLints bool, rlibOnly bool) *Module {
68	_, library := NewRustLibrary(hod)
69	library.BuildOnlyRust()
70	if rlibOnly {
71		library.BuildOnlyRlib()
72	}
73	library.sourceProvider = sourceProvider
74
75	module := newModule(hod, android.MultilibBoth)
76	module.sourceProvider = sourceProvider
77	module.compiler = library
78
79	if !enableLints {
80		library.disableLints()
81		module.disableClippy()
82	}
83
84	return module
85}
86
87func (sp *BaseSourceProvider) getStem(ctx android.ModuleContext) string {
88	if String(sp.Properties.Source_stem) == "" {
89		ctx.PropertyErrorf("source_stem",
90			"source_stem property is undefined but required for rust_bindgen modules")
91	}
92	return String(sp.Properties.Source_stem)
93}
94
95func (sp *BaseSourceProvider) SourceProviderDeps(ctx DepsContext, deps Deps) Deps {
96	return deps
97}
98
99func (sp *BaseSourceProvider) setSubName(subName string) {
100	sp.subName = subName
101}
102
103func (sp *BaseSourceProvider) setOutputFiles(outputFiles android.Paths) {
104	sp.OutputFiles = outputFiles
105}
106