blob: 26227d048de9c4fd6399cb108c5a127a9c844aac [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// 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
15package rust
16
17import (
18 "android/soong/android"
Vinh Tranb4bb20f2023-08-24 11:10:01 -040019 "android/soong/bazel"
20 "fmt"
Ivan Lozanoffee3342019-08-27 12:03:00 -070021)
22
23func init() {
24 android.RegisterModuleType("rust_proc_macro", ProcMacroFactory)
25}
26
27type ProcMacroCompilerProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070028}
29
30type procMacroDecorator struct {
31 *baseCompiler
Matthew Maurerbb3add12020-06-25 09:34:12 -070032 *flagExporter
Ivan Lozanoffee3342019-08-27 12:03:00 -070033
Ivan Lozano8a23fa42020-06-16 10:26:57 -040034 Properties ProcMacroCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -070035}
36
37type procMacroInterface interface {
Ivan Lozano872d5792022-03-23 17:31:39 -040038 ProcMacro() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070039}
40
41var _ compiler = (*procMacroDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -070042var _ exportedFlagsProducer = (*procMacroDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -070043
44func ProcMacroFactory() android.Module {
Ivan Lozano5ca5ef62019-09-23 10:10:40 -070045 module, _ := NewProcMacro(android.HostSupportedNoCross)
Ivan Lozanoffee3342019-08-27 12:03:00 -070046 return module.Init()
47}
48
49func NewProcMacro(hod android.HostOrDeviceSupported) (*Module, *procMacroDecorator) {
50 module := newModule(hod, android.MultilibFirst)
51
Vinh Tranb4bb20f2023-08-24 11:10:01 -040052 android.InitBazelModule(module)
53
Ivan Lozanoffee3342019-08-27 12:03:00 -070054 procMacro := &procMacroDecorator{
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080055 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -070056 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -070057 }
58
Ivan Lozano6cd99e62020-02-11 08:24:25 -050059 // Don't sanitize procMacros
60 module.sanitize = nil
Ivan Lozanoffee3342019-08-27 12:03:00 -070061 module.compiler = procMacro
62
63 return module, procMacro
64}
65
66func (procMacro *procMacroDecorator) compilerProps() []interface{} {
67 return append(procMacro.baseCompiler.compilerProps(),
68 &procMacro.Properties)
69}
70
Joel Galensonce7bbdc2021-09-23 08:26:53 -070071func (procMacro *procMacroDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
72 flags = procMacro.baseCompiler.compilerFlags(ctx, flags)
73 flags.RustFlags = append(flags.RustFlags, "--extern proc_macro")
74 return flags
75}
76
Sasha Smundaka76acba2022-04-18 20:12:56 -070077func (procMacro *procMacroDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -070078 fileName := procMacro.getStem(ctx) + ctx.toolchain().ProcMacroSuffix()
79 outputFile := android.PathForModuleOut(ctx, fileName)
80
Ivan Lozano07cbaf42020-07-22 16:09:13 -040081 srcPath, _ := srcPathFromModuleSrcs(ctx, procMacro.baseCompiler.Properties.Srcs)
Sasha Smundaka76acba2022-04-18 20:12:56 -070082 ret := TransformSrctoProcMacro(ctx, srcPath, deps, flags, outputFile)
Ivan Lozano8d10fc32021-11-05 16:36:47 -040083 procMacro.baseCompiler.unstrippedOutputFile = outputFile
Sasha Smundaka76acba2022-04-18 20:12:56 -070084 return ret
Ivan Lozanoffee3342019-08-27 12:03:00 -070085}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070086
87func (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 Maurer0f003b12020-06-29 14:34:06 -070093
Liz Kammer356f7d42021-01-26 09:18:53 -050094func (procMacro *procMacroDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Matthew Maurer0f003b12020-06-29 14:34:06 -070095 return rlibAutoDep
96}
Ivan Lozanod7586b62021-04-01 09:49:36 -040097
Ivan Lozano872d5792022-03-23 17:31:39 -040098func (procMacro *procMacroDecorator) ProcMacro() bool {
99 return true
100}
101
Ivan Lozanod7586b62021-04-01 09:49:36 -0400102func (procMacro *procMacroDecorator) everInstallable() bool {
103 // Proc_macros are never installed
104 return false
105}
Vinh Tranb4bb20f2023-08-24 11:10:01 -0400106
107type 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
117func procMacroBp2build(ctx android.TopDownMutatorContext, m *Module) {
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}