proc_macro.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2019 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. func init() {
  19. android.RegisterModuleType("rust_proc_macro", ProcMacroFactory)
  20. }
  21. type ProcMacroCompilerProperties struct {
  22. }
  23. type procMacroDecorator struct {
  24. *baseCompiler
  25. *flagExporter
  26. Properties ProcMacroCompilerProperties
  27. }
  28. type procMacroInterface interface {
  29. ProcMacro() bool
  30. }
  31. var _ compiler = (*procMacroDecorator)(nil)
  32. var _ exportedFlagsProducer = (*procMacroDecorator)(nil)
  33. func ProcMacroFactory() android.Module {
  34. module, _ := NewProcMacro(android.HostSupportedNoCross)
  35. return module.Init()
  36. }
  37. func NewProcMacro(hod android.HostOrDeviceSupported) (*Module, *procMacroDecorator) {
  38. module := newModule(hod, android.MultilibFirst)
  39. procMacro := &procMacroDecorator{
  40. baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
  41. flagExporter: NewFlagExporter(),
  42. }
  43. // Don't sanitize procMacros
  44. module.sanitize = nil
  45. module.compiler = procMacro
  46. return module, procMacro
  47. }
  48. func (procMacro *procMacroDecorator) compilerProps() []interface{} {
  49. return append(procMacro.baseCompiler.compilerProps(),
  50. &procMacro.Properties)
  51. }
  52. func (procMacro *procMacroDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
  53. flags = procMacro.baseCompiler.compilerFlags(ctx, flags)
  54. flags.RustFlags = append(flags.RustFlags, "--extern proc_macro")
  55. return flags
  56. }
  57. func (procMacro *procMacroDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
  58. fileName := procMacro.getStem(ctx) + ctx.toolchain().ProcMacroSuffix()
  59. outputFile := android.PathForModuleOut(ctx, fileName)
  60. srcPath, _ := srcPathFromModuleSrcs(ctx, procMacro.baseCompiler.Properties.Srcs)
  61. ret := TransformSrctoProcMacro(ctx, srcPath, deps, flags, outputFile)
  62. procMacro.baseCompiler.unstrippedOutputFile = outputFile
  63. return ret
  64. }
  65. func (procMacro *procMacroDecorator) getStem(ctx ModuleContext) string {
  66. stem := procMacro.baseCompiler.getStemWithoutSuffix(ctx)
  67. validateLibraryStem(ctx, stem, procMacro.crateName())
  68. return stem + String(procMacro.baseCompiler.Properties.Suffix)
  69. }
  70. func (procMacro *procMacroDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
  71. return rlibAutoDep
  72. }
  73. func (procMacro *procMacroDecorator) ProcMacro() bool {
  74. return true
  75. }
  76. func (procMacro *procMacroDecorator) everInstallable() bool {
  77. // Proc_macros are never installed
  78. return false
  79. }