source_provider.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. package rust
  15. import (
  16. "android/soong/android"
  17. )
  18. type SourceProviderProperties struct {
  19. // filename for the generated source file (<source_stem>.rs). This field is required.
  20. // The inherited "stem" property sets the output filename for the generated library variants only.
  21. Source_stem *string `android:"arch_variant"`
  22. // crate name, used for the library variant of this source provider. See additional details in rust_library.
  23. Crate_name string `android:"arch_variant"`
  24. }
  25. type BaseSourceProvider struct {
  26. Properties SourceProviderProperties
  27. OutputFile android.Path
  28. subAndroidMkOnce map[SubAndroidMkProvider]bool
  29. subName string
  30. }
  31. var _ SourceProvider = (*BaseSourceProvider)(nil)
  32. type SourceProvider interface {
  33. GenerateSource(ctx android.ModuleContext, deps PathDeps) android.Path
  34. Srcs() android.Paths
  35. SourceProviderProps() []interface{}
  36. SourceProviderDeps(ctx DepsContext, deps Deps) Deps
  37. setSubName(subName string)
  38. }
  39. func (sp *BaseSourceProvider) Srcs() android.Paths {
  40. return android.Paths{sp.OutputFile}
  41. }
  42. func (sp *BaseSourceProvider) GenerateSource(ctx android.ModuleContext, deps PathDeps) android.Path {
  43. panic("BaseSourceProviderModule does not implement GenerateSource()")
  44. }
  45. func (sp *BaseSourceProvider) SourceProviderProps() []interface{} {
  46. return []interface{}{&sp.Properties}
  47. }
  48. func NewSourceProvider() *BaseSourceProvider {
  49. return &BaseSourceProvider{
  50. Properties: SourceProviderProperties{},
  51. }
  52. }
  53. func NewSourceProviderModule(hod android.HostOrDeviceSupported, sourceProvider SourceProvider, enableLints bool) *Module {
  54. _, library := NewRustLibrary(hod)
  55. library.BuildOnlyRust()
  56. library.sourceProvider = sourceProvider
  57. module := newModule(hod, android.MultilibBoth)
  58. module.sourceProvider = sourceProvider
  59. module.compiler = library
  60. if !enableLints {
  61. library.disableLints()
  62. module.disableClippy()
  63. }
  64. return module
  65. }
  66. func (sp *BaseSourceProvider) getStem(ctx android.ModuleContext) string {
  67. if String(sp.Properties.Source_stem) == "" {
  68. ctx.PropertyErrorf("source_stem",
  69. "source_stem property is undefined but required for rust_bindgen modules")
  70. }
  71. return String(sp.Properties.Source_stem)
  72. }
  73. func (sp *BaseSourceProvider) SourceProviderDeps(ctx DepsContext, deps Deps) Deps {
  74. return deps
  75. }
  76. func (sp *BaseSourceProvider) setSubName(subName string) {
  77. sp.subName = subName
  78. }