1// 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 {
26}
27
28type procMacroDecorator struct {
29	*baseCompiler
30	*flagExporter
31
32	Properties ProcMacroCompilerProperties
33}
34
35type procMacroInterface interface {
36}
37
38var _ compiler = (*procMacroDecorator)(nil)
39var _ exportedFlagsProducer = (*procMacroDecorator)(nil)
40
41func ProcMacroFactory() android.Module {
42	module, _ := NewProcMacro(android.HostSupportedNoCross)
43	return module.Init()
44}
45
46func NewProcMacro(hod android.HostOrDeviceSupported) (*Module, *procMacroDecorator) {
47	module := newModule(hod, android.MultilibFirst)
48
49	procMacro := &procMacroDecorator{
50		baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
51		flagExporter: NewFlagExporter(),
52	}
53
54	// Don't sanitize procMacros
55	module.sanitize = nil
56	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
66func (procMacro *procMacroDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
67	fileName := procMacro.getStem(ctx) + ctx.toolchain().ProcMacroSuffix()
68	outputFile := android.PathForModuleOut(ctx, fileName)
69
70	srcPath, _ := srcPathFromModuleSrcs(ctx, procMacro.baseCompiler.Properties.Srcs)
71	TransformSrctoProcMacro(ctx, srcPath, deps, flags, outputFile)
72	return outputFile
73}
74
75func (procMacro *procMacroDecorator) getStem(ctx ModuleContext) string {
76	stem := procMacro.baseCompiler.getStemWithoutSuffix(ctx)
77	validateLibraryStem(ctx, stem, procMacro.crateName())
78
79	return stem + String(procMacro.baseCompiler.Properties.Suffix)
80}
81
82func (procMacro *procMacroDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
83	return rlibAutoDep
84}
85
86func (procMacro *procMacroDecorator) everInstallable() bool {
87	// Proc_macros are never installed
88	return false
89}
90