blob: f8a4bbdeddfd5180174911cc2cf3c1fc1802c6a4 [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"
19)
20
21func init() {
22 android.RegisterModuleType("rust_proc_macro", ProcMacroFactory)
23}
24
25type ProcMacroCompilerProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070026}
27
28type procMacroDecorator struct {
29 *baseCompiler
Matthew Maurerbb3add12020-06-25 09:34:12 -070030 *flagExporter
Ivan Lozanoffee3342019-08-27 12:03:00 -070031
Ivan Lozano8a23fa42020-06-16 10:26:57 -040032 Properties ProcMacroCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -070033}
34
35type procMacroInterface interface {
Ivan Lozano872d5792022-03-23 17:31:39 -040036 ProcMacro() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070037}
38
39var _ compiler = (*procMacroDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -070040var _ exportedFlagsProducer = (*procMacroDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -070041
42func ProcMacroFactory() android.Module {
Ivan Lozano5ca5ef62019-09-23 10:10:40 -070043 module, _ := NewProcMacro(android.HostSupportedNoCross)
Ivan Lozanoffee3342019-08-27 12:03:00 -070044 return module.Init()
45}
46
47func NewProcMacro(hod android.HostOrDeviceSupported) (*Module, *procMacroDecorator) {
48 module := newModule(hod, android.MultilibFirst)
49
50 procMacro := &procMacroDecorator{
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080051 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -070052 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -070053 }
54
Ivan Lozano6cd99e62020-02-11 08:24:25 -050055 // Don't sanitize procMacros
56 module.sanitize = nil
Ivan Lozanoffee3342019-08-27 12:03:00 -070057 module.compiler = procMacro
58
59 return module, procMacro
60}
61
62func (procMacro *procMacroDecorator) compilerProps() []interface{} {
63 return append(procMacro.baseCompiler.compilerProps(),
64 &procMacro.Properties)
65}
66
Joel Galensonce7bbdc2021-09-23 08:26:53 -070067func (procMacro *procMacroDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
68 flags = procMacro.baseCompiler.compilerFlags(ctx, flags)
69 flags.RustFlags = append(flags.RustFlags, "--extern proc_macro")
70 return flags
71}
72
Ivan Lozanoffee3342019-08-27 12:03:00 -070073func (procMacro *procMacroDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
74 fileName := procMacro.getStem(ctx) + ctx.toolchain().ProcMacroSuffix()
75 outputFile := android.PathForModuleOut(ctx, fileName)
76
Ivan Lozano07cbaf42020-07-22 16:09:13 -040077 srcPath, _ := srcPathFromModuleSrcs(ctx, procMacro.baseCompiler.Properties.Srcs)
Dan Albert06feee92021-03-19 15:06:02 -070078 TransformSrctoProcMacro(ctx, srcPath, deps, flags, outputFile)
Ivan Lozano8d10fc32021-11-05 16:36:47 -040079 procMacro.baseCompiler.unstrippedOutputFile = outputFile
Ivan Lozanoffee3342019-08-27 12:03:00 -070080 return outputFile
81}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070082
83func (procMacro *procMacroDecorator) getStem(ctx ModuleContext) string {
84 stem := procMacro.baseCompiler.getStemWithoutSuffix(ctx)
85 validateLibraryStem(ctx, stem, procMacro.crateName())
86
87 return stem + String(procMacro.baseCompiler.Properties.Suffix)
88}
Matthew Maurer0f003b12020-06-29 14:34:06 -070089
Liz Kammer356f7d42021-01-26 09:18:53 -050090func (procMacro *procMacroDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Matthew Maurer0f003b12020-06-29 14:34:06 -070091 return rlibAutoDep
92}
Ivan Lozanod7586b62021-04-01 09:49:36 -040093
Ivan Lozano872d5792022-03-23 17:31:39 -040094func (procMacro *procMacroDecorator) ProcMacro() bool {
95 return true
96}
97
Ivan Lozanod7586b62021-04-01 09:49:36 -040098func (procMacro *procMacroDecorator) everInstallable() bool {
99 // Proc_macros are never installed
100 return false
101}