Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | ) |
| 20 | |
| 21 | func init() { |
| 22 | android.RegisterModuleType("rust_proc_macro", ProcMacroFactory) |
| 23 | } |
| 24 | |
| 25 | type ProcMacroCompilerProperties struct { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | type procMacroDecorator struct { |
| 29 | *baseCompiler |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 30 | *flagExporter |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 31 | |
Ivan Lozano | 8a23fa4 | 2020-06-16 10:26:57 -0400 | [diff] [blame] | 32 | Properties ProcMacroCompilerProperties |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | type procMacroInterface interface { |
Ivan Lozano | 872d579 | 2022-03-23 17:31:39 -0400 | [diff] [blame] | 36 | ProcMacro() bool |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | var _ compiler = (*procMacroDecorator)(nil) |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 40 | var _ exportedFlagsProducer = (*procMacroDecorator)(nil) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 41 | |
| 42 | func ProcMacroFactory() android.Module { |
Ivan Lozano | 5ca5ef6 | 2019-09-23 10:10:40 -0700 | [diff] [blame] | 43 | module, _ := NewProcMacro(android.HostSupportedNoCross) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 44 | return module.Init() |
| 45 | } |
| 46 | |
| 47 | func NewProcMacro(hod android.HostOrDeviceSupported) (*Module, *procMacroDecorator) { |
| 48 | module := newModule(hod, android.MultilibFirst) |
| 49 | |
| 50 | procMacro := &procMacroDecorator{ |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 51 | baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem), |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 52 | flagExporter: NewFlagExporter(), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 55 | // Don't sanitize procMacros |
| 56 | module.sanitize = nil |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 57 | module.compiler = procMacro |
| 58 | |
| 59 | return module, procMacro |
| 60 | } |
| 61 | |
| 62 | func (procMacro *procMacroDecorator) compilerProps() []interface{} { |
| 63 | return append(procMacro.baseCompiler.compilerProps(), |
| 64 | &procMacro.Properties) |
| 65 | } |
| 66 | |
Joel Galenson | ce7bbdc | 2021-09-23 08:26:53 -0700 | [diff] [blame] | 67 | func (procMacro *procMacroDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags { |
| 68 | flags = procMacro.baseCompiler.compilerFlags(ctx, flags) |
| 69 | flags.RustFlags = append(flags.RustFlags, "--extern proc_macro") |
| 70 | return flags |
| 71 | } |
| 72 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 73 | func (procMacro *procMacroDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path { |
| 74 | fileName := procMacro.getStem(ctx) + ctx.toolchain().ProcMacroSuffix() |
| 75 | outputFile := android.PathForModuleOut(ctx, fileName) |
| 76 | |
Ivan Lozano | 07cbaf4 | 2020-07-22 16:09:13 -0400 | [diff] [blame] | 77 | srcPath, _ := srcPathFromModuleSrcs(ctx, procMacro.baseCompiler.Properties.Srcs) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 78 | TransformSrctoProcMacro(ctx, srcPath, deps, flags, outputFile) |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 79 | procMacro.baseCompiler.unstrippedOutputFile = outputFile |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 80 | return outputFile |
| 81 | } |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 82 | |
| 83 | func (procMacro *procMacroDecorator) getStem(ctx ModuleContext) string { |
| 84 | stem := procMacro.baseCompiler.getStemWithoutSuffix(ctx) |
| 85 | validateLibraryStem(ctx, stem, procMacro.crateName()) |
| 86 | |
| 87 | return stem + String(procMacro.baseCompiler.Properties.Suffix) |
| 88 | } |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 89 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 90 | func (procMacro *procMacroDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep { |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 91 | return rlibAutoDep |
| 92 | } |
Ivan Lozano | d7586b6 | 2021-04-01 09:49:36 -0400 | [diff] [blame] | 93 | |
Ivan Lozano | 872d579 | 2022-03-23 17:31:39 -0400 | [diff] [blame] | 94 | func (procMacro *procMacroDecorator) ProcMacro() bool { |
| 95 | return true |
| 96 | } |
| 97 | |
Ivan Lozano | d7586b6 | 2021-04-01 09:49:36 -0400 | [diff] [blame] | 98 | func (procMacro *procMacroDecorator) everInstallable() bool { |
| 99 | // Proc_macros are never installed |
| 100 | return false |
| 101 | } |