• 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	flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
71	return flags
72}
73
74func (procMacro *procMacroDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
75	fileName := procMacro.getStem(ctx) + ctx.toolchain().ProcMacroSuffix()
76	outputFile := android.PathForModuleOut(ctx, fileName)
77	srcPath := crateRootPath(ctx, procMacro)
78	ret := TransformSrctoProcMacro(ctx, srcPath, deps, flags, outputFile)
79	procMacro.baseCompiler.unstrippedOutputFile = outputFile
80
81	return ret
82}
83
84func (procMacro *procMacroDecorator) getStem(ctx ModuleContext) string {
85	stem := procMacro.baseCompiler.getStemWithoutSuffix(ctx)
86	validateLibraryStem(ctx, stem, procMacro.crateName())
87
88	return stem + String(procMacro.baseCompiler.Properties.Suffix)
89}
90
91func (procMacro *procMacroDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
92	return rlibAutoDep
93}
94
95func (procMacro *procMacroDecorator) ProcMacro() bool {
96	return true
97}
98
99func (procMacro *procMacroDecorator) everInstallable() bool {
100	// Proc_macros are never installed
101	return false
102}
103
104func (library *procMacroDecorator) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
105	library.baseCompiler.moduleInfoJSON(ctx, moduleInfoJSON)
106
107	moduleInfoJSON.Class = []string{"PROC_MACRO_LIBRARIES"}
108}
109