• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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	getSubName() 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, rlibOnly bool) *Module {
69	_, library := NewRustLibrary(hod)
70	library.BuildOnlyRust()
71	if rlibOnly {
72		library.BuildOnlyRlib()
73	}
74	library.sourceProvider = sourceProvider
75
76	module := newModule(hod, android.MultilibBoth)
77	module.sourceProvider = sourceProvider
78	module.compiler = library
79
80	if !enableLints {
81		library.disableLints()
82		module.disableClippy()
83	}
84
85	return module
86}
87
88func (sp *BaseSourceProvider) getStem(ctx android.ModuleContext) string {
89	if String(sp.Properties.Source_stem) == "" {
90		ctx.PropertyErrorf("source_stem",
91			"source_stem property is undefined but required for rust_bindgen modules")
92	}
93	return String(sp.Properties.Source_stem)
94}
95
96func (sp *BaseSourceProvider) SourceProviderDeps(ctx DepsContext, deps Deps) Deps {
97	return deps
98}
99
100func (sp *BaseSourceProvider) setSubName(subName string) {
101	sp.subName = subName
102}
103
104func (sp *BaseSourceProvider) getSubName() string {
105	return sp.subName
106}
107
108func (sp *BaseSourceProvider) setOutputFiles(outputFiles android.Paths) {
109	sp.OutputFiles = outputFiles
110}
111