• 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	"path/filepath"
19
20	"android/soong/android"
21)
22
23type AndroidMkContext interface {
24	Name() string
25	Target() android.Target
26	SubAndroidMk(*android.AndroidMkEntries, interface{})
27}
28
29type SubAndroidMkProvider interface {
30	AndroidMk(AndroidMkContext, *android.AndroidMkEntries)
31}
32
33func (mod *Module) SubAndroidMk(data *android.AndroidMkEntries, obj interface{}) {
34	if mod.subAndroidMkOnce == nil {
35		mod.subAndroidMkOnce = make(map[SubAndroidMkProvider]bool)
36	}
37	if androidmk, ok := obj.(SubAndroidMkProvider); ok {
38		if !mod.subAndroidMkOnce[androidmk] {
39			mod.subAndroidMkOnce[androidmk] = true
40			androidmk.AndroidMk(mod, data)
41		}
42	}
43}
44
45func (mod *Module) AndroidMkSuffix() string {
46	return mod.Properties.RustSubName + mod.Properties.SubName
47}
48
49func (mod *Module) AndroidMkEntries() []android.AndroidMkEntries {
50	if mod.Properties.HideFromMake || mod.hideApexVariantFromMake {
51
52		return []android.AndroidMkEntries{android.AndroidMkEntries{Disabled: true}}
53	}
54
55	ret := android.AndroidMkEntries{
56		OutputFile: android.OptionalPathForPath(mod.UnstrippedOutputFile()),
57		Include:    "$(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk",
58		ExtraEntries: []android.AndroidMkExtraEntriesFunc{
59			func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
60				entries.AddStrings("LOCAL_RLIB_LIBRARIES", mod.Properties.AndroidMkRlibs...)
61				entries.AddStrings("LOCAL_DYLIB_LIBRARIES", mod.Properties.AndroidMkDylibs...)
62				entries.AddStrings("LOCAL_PROC_MACRO_LIBRARIES", mod.Properties.AndroidMkProcMacroLibs...)
63				entries.AddStrings("LOCAL_SHARED_LIBRARIES", mod.transitiveAndroidMkSharedLibs.ToList()...)
64				entries.AddStrings("LOCAL_STATIC_LIBRARIES", mod.Properties.AndroidMkStaticLibs...)
65				entries.AddStrings("LOCAL_HEADER_LIBRARIES", mod.Properties.AndroidMkHeaderLibs...)
66				entries.AddStrings("LOCAL_SOONG_LINK_TYPE", mod.makeLinkType)
67				if mod.InVendor() {
68					entries.SetBool("LOCAL_IN_VENDOR", true)
69				} else if mod.InProduct() {
70					entries.SetBool("LOCAL_IN_PRODUCT", true)
71				}
72			},
73		},
74	}
75
76	if mod.compiler != nil && !mod.compiler.Disabled() {
77		mod.SubAndroidMk(&ret, mod.compiler)
78	} else if mod.sourceProvider != nil {
79		// If the compiler is disabled, this is a SourceProvider.
80		mod.SubAndroidMk(&ret, mod.sourceProvider)
81	}
82
83	if mod.sanitize != nil {
84		mod.SubAndroidMk(&ret, mod.sanitize)
85	}
86
87	ret.SubName += mod.AndroidMkSuffix()
88
89	return []android.AndroidMkEntries{ret}
90}
91
92func (binary *binaryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
93	ctx.SubAndroidMk(ret, binary.baseCompiler)
94
95	ret.Class = "EXECUTABLES"
96}
97
98func (test *testDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
99	ctx.SubAndroidMk(ret, test.binaryDecorator)
100
101	ret.Class = "NATIVE_TESTS"
102	ret.ExtraEntries = append(ret.ExtraEntries,
103		func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
104			entries.AddCompatibilityTestSuites(test.Properties.Test_suites...)
105			if test.testConfig != nil {
106				entries.SetString("LOCAL_FULL_TEST_CONFIG", test.testConfig.String())
107			}
108			entries.SetBoolIfTrue("LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG", !BoolDefault(test.Properties.Auto_gen_config, true))
109			if test.Properties.Data_bins != nil {
110				entries.AddStrings("LOCAL_TEST_DATA_BINS", test.Properties.Data_bins...)
111			}
112
113			test.Properties.Test_options.SetAndroidMkEntries(entries)
114		})
115}
116
117func (benchmark *benchmarkDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
118	benchmark.binaryDecorator.AndroidMk(ctx, ret)
119	ret.Class = "NATIVE_TESTS"
120	ret.ExtraEntries = append(ret.ExtraEntries,
121		func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
122			entries.AddCompatibilityTestSuites(benchmark.Properties.Test_suites...)
123			if benchmark.testConfig != nil {
124				entries.SetString("LOCAL_FULL_TEST_CONFIG", benchmark.testConfig.String())
125			}
126			entries.SetBool("LOCAL_NATIVE_BENCHMARK", true)
127			entries.SetBoolIfTrue("LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG", !BoolDefault(benchmark.Properties.Auto_gen_config, true))
128		})
129}
130
131func (library *libraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
132	ctx.SubAndroidMk(ret, library.baseCompiler)
133
134	if library.rlib() {
135		ret.Class = "RLIB_LIBRARIES"
136	} else if library.dylib() {
137		ret.Class = "DYLIB_LIBRARIES"
138	} else if library.static() {
139		ret.Class = "STATIC_LIBRARIES"
140	} else if library.shared() {
141		ret.Class = "SHARED_LIBRARIES"
142	}
143	ret.ExtraEntries = append(ret.ExtraEntries,
144		func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
145			if library.tocFile.Valid() {
146				entries.SetString("LOCAL_SOONG_TOC", library.tocFile.String())
147			}
148		})
149}
150
151func (procMacro *procMacroDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
152	ctx.SubAndroidMk(ret, procMacro.baseCompiler)
153
154	ret.Class = "PROC_MACRO_LIBRARIES"
155}
156
157func (sourceProvider *BaseSourceProvider) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
158	outFile := sourceProvider.OutputFiles[0]
159	ret.Class = "ETC"
160	ret.OutputFile = android.OptionalPathForPath(outFile)
161	ret.SubName += sourceProvider.subName
162	ret.ExtraEntries = append(ret.ExtraEntries,
163		func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
164			_, file := filepath.Split(outFile.String())
165			stem, suffix, _ := android.SplitFileExt(file)
166			entries.SetString("LOCAL_MODULE_SUFFIX", suffix)
167			entries.SetString("LOCAL_MODULE_STEM", stem)
168			entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", true)
169		})
170}
171
172func (bindgen *bindgenDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
173	ctx.SubAndroidMk(ret, bindgen.BaseSourceProvider)
174}
175
176func (proto *protobufDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
177	ctx.SubAndroidMk(ret, proto.BaseSourceProvider)
178}
179
180func (compiler *baseCompiler) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
181	if compiler.path == (android.InstallPath{}) {
182		return
183	}
184
185	if compiler.strippedOutputFile.Valid() {
186		ret.OutputFile = compiler.strippedOutputFile
187	}
188
189	ret.ExtraEntries = append(ret.ExtraEntries,
190		func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
191			entries.SetPath("LOCAL_SOONG_UNSTRIPPED_BINARY", compiler.unstrippedOutputFile)
192			path, file := filepath.Split(compiler.path.String())
193			stem, suffix, _ := android.SplitFileExt(file)
194			entries.SetString("LOCAL_MODULE_SUFFIX", suffix)
195			entries.SetString("LOCAL_MODULE_PATH", path)
196			entries.SetString("LOCAL_MODULE_STEM", stem)
197		})
198}
199
200func (fuzz *fuzzDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
201	ctx.SubAndroidMk(ret, fuzz.binaryDecorator)
202
203	ret.ExtraEntries = append(ret.ExtraEntries, func(ctx android.AndroidMkExtraEntriesContext,
204		entries *android.AndroidMkEntries) {
205		entries.SetBool("LOCAL_IS_FUZZ_TARGET", true)
206		if fuzz.installedSharedDeps != nil {
207			entries.AddStrings("LOCAL_FUZZ_INSTALLED_SHARED_DEPS", fuzz.installedSharedDeps...)
208		}
209	})
210}
211