blob: c18d5ec70548826251428387c5353aed068989ab [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 (
Chris Parsons637458d2023-09-19 20:09:00 +000018 "fmt"
19
Ivan Lozanoffee3342019-08-27 12:03:00 -070020 "android/soong/android"
Vinh Tranb4bb20f2023-08-24 11:10:01 -040021 "android/soong/bazel"
Ivan Lozanoffee3342019-08-27 12:03:00 -070022)
23
24func init() {
25 android.RegisterModuleType("rust_proc_macro", ProcMacroFactory)
26}
27
28type ProcMacroCompilerProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070029}
30
31type procMacroDecorator struct {
32 *baseCompiler
Matthew Maurerbb3add12020-06-25 09:34:12 -070033 *flagExporter
Ivan Lozanoffee3342019-08-27 12:03:00 -070034
Ivan Lozano8a23fa42020-06-16 10:26:57 -040035 Properties ProcMacroCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -070036}
37
38type procMacroInterface interface {
Ivan Lozano872d5792022-03-23 17:31:39 -040039 ProcMacro() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070040}
41
42var _ compiler = (*procMacroDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -070043var _ exportedFlagsProducer = (*procMacroDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -070044
45func ProcMacroFactory() android.Module {
Ivan Lozano5ca5ef62019-09-23 10:10:40 -070046 module, _ := NewProcMacro(android.HostSupportedNoCross)
Ivan Lozanoffee3342019-08-27 12:03:00 -070047 return module.Init()
48}
49
50func NewProcMacro(hod android.HostOrDeviceSupported) (*Module, *procMacroDecorator) {
51 module := newModule(hod, android.MultilibFirst)
52
Vinh Tranb4bb20f2023-08-24 11:10:01 -040053 android.InitBazelModule(module)
54
Ivan Lozanoffee3342019-08-27 12:03:00 -070055 procMacro := &procMacroDecorator{
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080056 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -070057 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -070058 }
59
Ivan Lozano6cd99e62020-02-11 08:24:25 -050060 // Don't sanitize procMacros
61 module.sanitize = nil
Ivan Lozanoffee3342019-08-27 12:03:00 -070062 module.compiler = procMacro
63
64 return module, procMacro
65}
66
67func (procMacro *procMacroDecorator) compilerProps() []interface{} {
68 return append(procMacro.baseCompiler.compilerProps(),
69 &procMacro.Properties)
70}
71
Joel Galensonce7bbdc2021-09-23 08:26:53 -070072func (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 Smundaka76acba2022-04-18 20:12:56 -070078func (procMacro *procMacroDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -070079 fileName := procMacro.getStem(ctx) + ctx.toolchain().ProcMacroSuffix()
80 outputFile := android.PathForModuleOut(ctx, fileName)
Matthew Maurera28404a2023-11-20 23:33:28 +000081 srcPath := crateRootPath(ctx, procMacro)
Wen-yi Chu41326c12023-09-22 03:58:59 +000082 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
Chris Parsons637458d2023-09-19 20:09:00 +0000117func procMacroBp2build(ctx android.Bp2buildMutatorContext, m *Module) {
Vinh Tranb4bb20f2023-08-24 11:10:01 -0400118 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}