blob: 804d79fe9a43f5ea802307b76288bd6b4320f93c [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 {
36}
37
38var _ compiler = (*procMacroDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -070039var _ exportedFlagsProducer = (*procMacroDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -070040
41func ProcMacroFactory() android.Module {
Ivan Lozano5ca5ef62019-09-23 10:10:40 -070042 module, _ := NewProcMacro(android.HostSupportedNoCross)
Ivan Lozanoffee3342019-08-27 12:03:00 -070043 return module.Init()
44}
45
46func NewProcMacro(hod android.HostOrDeviceSupported) (*Module, *procMacroDecorator) {
47 module := newModule(hod, android.MultilibFirst)
48
49 procMacro := &procMacroDecorator{
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080050 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -070051 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -070052 }
53
Ivan Lozano6cd99e62020-02-11 08:24:25 -050054 // Don't sanitize procMacros
55 module.sanitize = nil
Ivan Lozanoffee3342019-08-27 12:03:00 -070056 module.compiler = procMacro
57
58 return module, procMacro
59}
60
61func (procMacro *procMacroDecorator) compilerProps() []interface{} {
62 return append(procMacro.baseCompiler.compilerProps(),
63 &procMacro.Properties)
64}
65
Joel Galensonce7bbdc2021-09-23 08:26:53 -070066func (procMacro *procMacroDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
67 flags = procMacro.baseCompiler.compilerFlags(ctx, flags)
68 flags.RustFlags = append(flags.RustFlags, "--extern proc_macro")
69 return flags
70}
71
Ivan Lozanoffee3342019-08-27 12:03:00 -070072func (procMacro *procMacroDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
73 fileName := procMacro.getStem(ctx) + ctx.toolchain().ProcMacroSuffix()
74 outputFile := android.PathForModuleOut(ctx, fileName)
75
Ivan Lozano07cbaf42020-07-22 16:09:13 -040076 srcPath, _ := srcPathFromModuleSrcs(ctx, procMacro.baseCompiler.Properties.Srcs)
Dan Albert06feee92021-03-19 15:06:02 -070077 TransformSrctoProcMacro(ctx, srcPath, deps, flags, outputFile)
Ivan Lozanoffee3342019-08-27 12:03:00 -070078 return outputFile
79}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070080
81func (procMacro *procMacroDecorator) getStem(ctx ModuleContext) string {
82 stem := procMacro.baseCompiler.getStemWithoutSuffix(ctx)
83 validateLibraryStem(ctx, stem, procMacro.crateName())
84
85 return stem + String(procMacro.baseCompiler.Properties.Suffix)
86}
Matthew Maurer0f003b12020-06-29 14:34:06 -070087
Liz Kammer356f7d42021-01-26 09:18:53 -050088func (procMacro *procMacroDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Matthew Maurer0f003b12020-06-29 14:34:06 -070089 return rlibAutoDep
90}
Ivan Lozanod7586b62021-04-01 09:49:36 -040091
92func (procMacro *procMacroDecorator) everInstallable() bool {
93 // Proc_macros are never installed
94 return false
95}