• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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	ProcMacro() bool
37}
38
39var _ compiler = (*procMacroDecorator)(nil)
40var _ exportedFlagsProducer = (*procMacroDecorator)(nil)
41
42func ProcMacroFactory() android.Module {
43	module, _ := NewProcMacro(android.HostSupportedNoCross)
44	return module.Init()
45}
46
47func NewProcMacro(hod android.HostOrDeviceSupported) (*Module, *procMacroDecorator) {
48	module := newModule(hod, android.MultilibFirst)
49
50	procMacro := &procMacroDecorator{
51		baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
52		flagExporter: NewFlagExporter(),
53	}
54
55	// Don't sanitize procMacros
56	module.sanitize = nil
57	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
67func (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
73func (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
77	srcPath, _ := srcPathFromModuleSrcs(ctx, procMacro.baseCompiler.Properties.Srcs)
78	TransformSrctoProcMacro(ctx, srcPath, deps, flags, outputFile)
79	procMacro.baseCompiler.unstrippedOutputFile = outputFile
80	return outputFile
81}
82
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}
89
90func (procMacro *procMacroDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
91	return rlibAutoDep
92}
93
94func (procMacro *procMacroDecorator) ProcMacro() bool {
95	return true
96}
97
98func (procMacro *procMacroDecorator) everInstallable() bool {
99	// Proc_macros are never installed
100	return false
101}
102