| 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 ( | 
| Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 18 | "fmt" | 
|  | 19 |  | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 20 | "android/soong/android" | 
| Vinh Tran | b4bb20f | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 21 | "android/soong/bazel" | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 22 | ) | 
|  | 23 |  | 
|  | 24 | func init() { | 
|  | 25 | android.RegisterModuleType("rust_proc_macro", ProcMacroFactory) | 
|  | 26 | } | 
|  | 27 |  | 
|  | 28 | type ProcMacroCompilerProperties struct { | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 29 | } | 
|  | 30 |  | 
|  | 31 | type procMacroDecorator struct { | 
|  | 32 | *baseCompiler | 
| Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 33 | *flagExporter | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 34 |  | 
| Ivan Lozano | 8a23fa4 | 2020-06-16 10:26:57 -0400 | [diff] [blame] | 35 | Properties ProcMacroCompilerProperties | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 36 | } | 
|  | 37 |  | 
|  | 38 | type procMacroInterface interface { | 
| Ivan Lozano | 872d579 | 2022-03-23 17:31:39 -0400 | [diff] [blame] | 39 | ProcMacro() bool | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 40 | } | 
|  | 41 |  | 
|  | 42 | var _ compiler = (*procMacroDecorator)(nil) | 
| Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 43 | var _ exportedFlagsProducer = (*procMacroDecorator)(nil) | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 44 |  | 
|  | 45 | func ProcMacroFactory() android.Module { | 
| Ivan Lozano | 5ca5ef6 | 2019-09-23 10:10:40 -0700 | [diff] [blame] | 46 | module, _ := NewProcMacro(android.HostSupportedNoCross) | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 47 | return module.Init() | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | func NewProcMacro(hod android.HostOrDeviceSupported) (*Module, *procMacroDecorator) { | 
|  | 51 | module := newModule(hod, android.MultilibFirst) | 
|  | 52 |  | 
| Vinh Tran | b4bb20f | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 53 | android.InitBazelModule(module) | 
|  | 54 |  | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 55 | procMacro := &procMacroDecorator{ | 
| Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 56 | baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem), | 
| Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 57 | flagExporter: NewFlagExporter(), | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 58 | } | 
|  | 59 |  | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 60 | // Don't sanitize procMacros | 
|  | 61 | module.sanitize = nil | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 62 | module.compiler = procMacro | 
|  | 63 |  | 
|  | 64 | return module, procMacro | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | func (procMacro *procMacroDecorator) compilerProps() []interface{} { | 
|  | 68 | return append(procMacro.baseCompiler.compilerProps(), | 
|  | 69 | &procMacro.Properties) | 
|  | 70 | } | 
|  | 71 |  | 
| Joel Galenson | ce7bbdc | 2021-09-23 08:26:53 -0700 | [diff] [blame] | 72 | func (procMacro *procMacroDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags { | 
|  | 73 | flags = procMacro.baseCompiler.compilerFlags(ctx, flags) | 
|  | 74 | flags.RustFlags = append(flags.RustFlags, "--extern proc_macro") | 
|  | 75 | return flags | 
|  | 76 | } | 
|  | 77 |  | 
| Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 78 | func (procMacro *procMacroDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput { | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 79 | fileName := procMacro.getStem(ctx) + ctx.toolchain().ProcMacroSuffix() | 
|  | 80 | outputFile := android.PathForModuleOut(ctx, fileName) | 
| Matthew Maurer | a28404a | 2023-11-20 23:33:28 +0000 | [diff] [blame] | 81 | srcPath := crateRootPath(ctx, procMacro) | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 82 | ret := TransformSrctoProcMacro(ctx, srcPath, deps, flags, outputFile) | 
| Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 83 | procMacro.baseCompiler.unstrippedOutputFile = outputFile | 
| Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 84 | return ret | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 85 | } | 
| Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 86 |  | 
|  | 87 | func (procMacro *procMacroDecorator) getStem(ctx ModuleContext) string { | 
|  | 88 | stem := procMacro.baseCompiler.getStemWithoutSuffix(ctx) | 
|  | 89 | validateLibraryStem(ctx, stem, procMacro.crateName()) | 
|  | 90 |  | 
|  | 91 | return stem + String(procMacro.baseCompiler.Properties.Suffix) | 
|  | 92 | } | 
| Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 93 |  | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 94 | func (procMacro *procMacroDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep { | 
| Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 95 | return rlibAutoDep | 
|  | 96 | } | 
| Ivan Lozano | d7586b6 | 2021-04-01 09:49:36 -0400 | [diff] [blame] | 97 |  | 
| Ivan Lozano | 872d579 | 2022-03-23 17:31:39 -0400 | [diff] [blame] | 98 | func (procMacro *procMacroDecorator) ProcMacro() bool { | 
|  | 99 | return true | 
|  | 100 | } | 
|  | 101 |  | 
| Ivan Lozano | d7586b6 | 2021-04-01 09:49:36 -0400 | [diff] [blame] | 102 | func (procMacro *procMacroDecorator) everInstallable() bool { | 
|  | 103 | // Proc_macros are never installed | 
|  | 104 | return false | 
|  | 105 | } | 
| Vinh Tran | b4bb20f | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 106 |  | 
|  | 107 | type procMacroAttributes struct { | 
|  | 108 | Srcs           bazel.LabelListAttribute | 
|  | 109 | Compile_data   bazel.LabelListAttribute | 
|  | 110 | Crate_name     bazel.StringAttribute | 
|  | 111 | Edition        bazel.StringAttribute | 
|  | 112 | Crate_features bazel.StringListAttribute | 
|  | 113 | Deps           bazel.LabelListAttribute | 
|  | 114 | Rustc_flags    bazel.StringListAttribute | 
|  | 115 | } | 
|  | 116 |  | 
| Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 117 | func procMacroBp2build(ctx android.Bp2buildMutatorContext, m *Module) { | 
| Vinh Tran | b4bb20f | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 118 | procMacro := m.compiler.(*procMacroDecorator) | 
|  | 119 | srcs, compileData := srcsAndCompileDataAttrs(ctx, *procMacro.baseCompiler) | 
|  | 120 | deps := android.BazelLabelForModuleDeps(ctx, append( | 
|  | 121 | procMacro.baseCompiler.Properties.Rustlibs, | 
|  | 122 | procMacro.baseCompiler.Properties.Rlibs..., | 
|  | 123 | )) | 
|  | 124 |  | 
|  | 125 | var rustcFLags []string | 
|  | 126 | for _, cfg := range procMacro.baseCompiler.Properties.Cfgs { | 
|  | 127 | rustcFLags = append(rustcFLags, fmt.Sprintf("--cfg=%s", cfg)) | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | attrs := &procMacroAttributes{ | 
|  | 131 | Srcs: bazel.MakeLabelListAttribute( | 
|  | 132 | srcs, | 
|  | 133 | ), | 
|  | 134 | Compile_data: bazel.MakeLabelListAttribute( | 
|  | 135 | compileData, | 
|  | 136 | ), | 
|  | 137 | Crate_name: bazel.StringAttribute{ | 
|  | 138 | Value: &procMacro.baseCompiler.Properties.Crate_name, | 
|  | 139 | }, | 
|  | 140 | Edition: bazel.StringAttribute{ | 
|  | 141 | Value: procMacro.baseCompiler.Properties.Edition, | 
|  | 142 | }, | 
|  | 143 | Crate_features: bazel.StringListAttribute{ | 
|  | 144 | Value: procMacro.baseCompiler.Properties.Features, | 
|  | 145 | }, | 
|  | 146 | Deps: bazel.MakeLabelListAttribute( | 
|  | 147 | deps, | 
|  | 148 | ), | 
|  | 149 | Rustc_flags: bazel.StringListAttribute{ | 
|  | 150 | Value: append( | 
|  | 151 | rustcFLags, | 
|  | 152 | procMacro.baseCompiler.Properties.Flags..., | 
|  | 153 | ), | 
|  | 154 | }, | 
|  | 155 | } | 
|  | 156 | // m.IsConvertedByBp2build() | 
|  | 157 | ctx.CreateBazelTargetModule( | 
|  | 158 | bazel.BazelTargetModuleProperties{ | 
|  | 159 | Rule_class:        "rust_proc_macro", | 
|  | 160 | Bzl_load_location: "@rules_rust//rust:defs.bzl", | 
|  | 161 | }, | 
|  | 162 | android.CommonAttributes{ | 
|  | 163 | Name: m.Name(), | 
|  | 164 | }, | 
|  | 165 | attrs, | 
|  | 166 | ) | 
|  | 167 | } |