blob: b93b24f1571d4a459945bf73add719b9b0ee391e [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)
81
Ivan Lozano07cbaf42020-07-22 16:09:13 -040082 srcPath, _ := srcPathFromModuleSrcs(ctx, procMacro.baseCompiler.Properties.Srcs)
Sasha Smundaka76acba2022-04-18 20:12:56 -070083 ret := TransformSrctoProcMacro(ctx, srcPath, deps, flags, outputFile)
Ivan Lozano8d10fc32021-11-05 16:36:47 -040084 procMacro.baseCompiler.unstrippedOutputFile = outputFile
Sasha Smundaka76acba2022-04-18 20:12:56 -070085 return ret
Ivan Lozanoffee3342019-08-27 12:03:00 -070086}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070087
88func (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 Maurer0f003b12020-06-29 14:34:06 -070094
Liz Kammer356f7d42021-01-26 09:18:53 -050095func (procMacro *procMacroDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Matthew Maurer0f003b12020-06-29 14:34:06 -070096 return rlibAutoDep
97}
Ivan Lozanod7586b62021-04-01 09:49:36 -040098
Ivan Lozano872d5792022-03-23 17:31:39 -040099func (procMacro *procMacroDecorator) ProcMacro() bool {
100 return true
101}
102
Ivan Lozanod7586b62021-04-01 09:49:36 -0400103func (procMacro *procMacroDecorator) everInstallable() bool {
104 // Proc_macros are never installed
105 return false
106}
Vinh Tranb4bb20f2023-08-24 11:10:01 -0400107
108type 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 Parsons637458d2023-09-19 20:09:00 +0000118func procMacroBp2build(ctx android.Bp2buildMutatorContext, m *Module) {
Vinh Tranb4bb20f2023-08-24 11:10:01 -0400119 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}