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