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) |
| 81 | |
Ivan Lozano | 07cbaf4 | 2020-07-22 16:09:13 -0400 | [diff] [blame] | 82 | srcPath, _ := srcPathFromModuleSrcs(ctx, procMacro.baseCompiler.Properties.Srcs) |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 83 | ret := TransformSrctoProcMacro(ctx, srcPath, deps, flags, outputFile) |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 84 | procMacro.baseCompiler.unstrippedOutputFile = outputFile |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 85 | return ret |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 86 | } |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 87 | |
| 88 | func (procMacro *procMacroDecorator) getStem(ctx ModuleContext) string { |
| 89 | stem := procMacro.baseCompiler.getStemWithoutSuffix(ctx) |
| 90 | validateLibraryStem(ctx, stem, procMacro.crateName()) |
| 91 | |
| 92 | return stem + String(procMacro.baseCompiler.Properties.Suffix) |
| 93 | } |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 94 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 95 | func (procMacro *procMacroDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep { |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 96 | return rlibAutoDep |
| 97 | } |
Ivan Lozano | d7586b6 | 2021-04-01 09:49:36 -0400 | [diff] [blame] | 98 | |
Ivan Lozano | 872d579 | 2022-03-23 17:31:39 -0400 | [diff] [blame] | 99 | func (procMacro *procMacroDecorator) ProcMacro() bool { |
| 100 | return true |
| 101 | } |
| 102 | |
Ivan Lozano | d7586b6 | 2021-04-01 09:49:36 -0400 | [diff] [blame] | 103 | func (procMacro *procMacroDecorator) everInstallable() bool { |
| 104 | // Proc_macros are never installed |
| 105 | return false |
| 106 | } |
Vinh Tran | b4bb20f | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 107 | |
| 108 | type procMacroAttributes struct { |
| 109 | Srcs bazel.LabelListAttribute |
| 110 | Compile_data bazel.LabelListAttribute |
| 111 | Crate_name bazel.StringAttribute |
| 112 | Edition bazel.StringAttribute |
| 113 | Crate_features bazel.StringListAttribute |
| 114 | Deps bazel.LabelListAttribute |
| 115 | Rustc_flags bazel.StringListAttribute |
| 116 | } |
| 117 | |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 118 | func procMacroBp2build(ctx android.Bp2buildMutatorContext, m *Module) { |
Vinh Tran | b4bb20f | 2023-08-24 11:10:01 -0400 | [diff] [blame] | 119 | procMacro := m.compiler.(*procMacroDecorator) |
| 120 | srcs, compileData := srcsAndCompileDataAttrs(ctx, *procMacro.baseCompiler) |
| 121 | deps := android.BazelLabelForModuleDeps(ctx, append( |
| 122 | procMacro.baseCompiler.Properties.Rustlibs, |
| 123 | procMacro.baseCompiler.Properties.Rlibs..., |
| 124 | )) |
| 125 | |
| 126 | var rustcFLags []string |
| 127 | for _, cfg := range procMacro.baseCompiler.Properties.Cfgs { |
| 128 | rustcFLags = append(rustcFLags, fmt.Sprintf("--cfg=%s", cfg)) |
| 129 | } |
| 130 | |
| 131 | attrs := &procMacroAttributes{ |
| 132 | Srcs: bazel.MakeLabelListAttribute( |
| 133 | srcs, |
| 134 | ), |
| 135 | Compile_data: bazel.MakeLabelListAttribute( |
| 136 | compileData, |
| 137 | ), |
| 138 | Crate_name: bazel.StringAttribute{ |
| 139 | Value: &procMacro.baseCompiler.Properties.Crate_name, |
| 140 | }, |
| 141 | Edition: bazel.StringAttribute{ |
| 142 | Value: procMacro.baseCompiler.Properties.Edition, |
| 143 | }, |
| 144 | Crate_features: bazel.StringListAttribute{ |
| 145 | Value: procMacro.baseCompiler.Properties.Features, |
| 146 | }, |
| 147 | Deps: bazel.MakeLabelListAttribute( |
| 148 | deps, |
| 149 | ), |
| 150 | Rustc_flags: bazel.StringListAttribute{ |
| 151 | Value: append( |
| 152 | rustcFLags, |
| 153 | procMacro.baseCompiler.Properties.Flags..., |
| 154 | ), |
| 155 | }, |
| 156 | } |
| 157 | // m.IsConvertedByBp2build() |
| 158 | ctx.CreateBazelTargetModule( |
| 159 | bazel.BazelTargetModuleProperties{ |
| 160 | Rule_class: "rust_proc_macro", |
| 161 | Bzl_load_location: "@rules_rust//rust:defs.bzl", |
| 162 | }, |
| 163 | android.CommonAttributes{ |
| 164 | Name: m.Name(), |
| 165 | }, |
| 166 | attrs, |
| 167 | ) |
| 168 | } |