xref: /aosp_15_r20/build/soong/rust/compiler.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker// Copyright 2019 The Android Open Source Project
2*333d2b36SAndroid Build Coastguard Worker//
3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*333d2b36SAndroid Build Coastguard Worker//
7*333d2b36SAndroid Build Coastguard Worker//     http://www.apache.org/licenses/LICENSE-2.0
8*333d2b36SAndroid Build Coastguard Worker//
9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*333d2b36SAndroid Build Coastguard Worker// limitations under the License.
14*333d2b36SAndroid Build Coastguard Worker
15*333d2b36SAndroid Build Coastguard Workerpackage rust
16*333d2b36SAndroid Build Coastguard Worker
17*333d2b36SAndroid Build Coastguard Workerimport (
18*333d2b36SAndroid Build Coastguard Worker	"android/soong/cc"
19*333d2b36SAndroid Build Coastguard Worker	"errors"
20*333d2b36SAndroid Build Coastguard Worker	"fmt"
21*333d2b36SAndroid Build Coastguard Worker	"path/filepath"
22*333d2b36SAndroid Build Coastguard Worker	"strings"
23*333d2b36SAndroid Build Coastguard Worker
24*333d2b36SAndroid Build Coastguard Worker	"github.com/google/blueprint/proptools"
25*333d2b36SAndroid Build Coastguard Worker
26*333d2b36SAndroid Build Coastguard Worker	"android/soong/android"
27*333d2b36SAndroid Build Coastguard Worker	"android/soong/rust/config"
28*333d2b36SAndroid Build Coastguard Worker)
29*333d2b36SAndroid Build Coastguard Worker
30*333d2b36SAndroid Build Coastguard Workertype RustLinkage int
31*333d2b36SAndroid Build Coastguard Worker
32*333d2b36SAndroid Build Coastguard Workerconst (
33*333d2b36SAndroid Build Coastguard Worker	DefaultLinkage RustLinkage = iota
34*333d2b36SAndroid Build Coastguard Worker	RlibLinkage
35*333d2b36SAndroid Build Coastguard Worker	DylibLinkage
36*333d2b36SAndroid Build Coastguard Worker)
37*333d2b36SAndroid Build Coastguard Worker
38*333d2b36SAndroid Build Coastguard Workertype compiler interface {
39*333d2b36SAndroid Build Coastguard Worker	initialize(ctx ModuleContext)
40*333d2b36SAndroid Build Coastguard Worker	compilerFlags(ctx ModuleContext, flags Flags) Flags
41*333d2b36SAndroid Build Coastguard Worker	cfgFlags(ctx ModuleContext, flags Flags) Flags
42*333d2b36SAndroid Build Coastguard Worker	featureFlags(ctx ModuleContext, module *Module, flags Flags) Flags
43*333d2b36SAndroid Build Coastguard Worker	compilerProps() []interface{}
44*333d2b36SAndroid Build Coastguard Worker	compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput
45*333d2b36SAndroid Build Coastguard Worker	compilerDeps(ctx DepsContext, deps Deps) Deps
46*333d2b36SAndroid Build Coastguard Worker	crateName() string
47*333d2b36SAndroid Build Coastguard Worker	edition() string
48*333d2b36SAndroid Build Coastguard Worker	features(ctx android.ConfigurableEvaluatorContext, module *Module) []string
49*333d2b36SAndroid Build Coastguard Worker	rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
50*333d2b36SAndroid Build Coastguard Worker	Thinlto() bool
51*333d2b36SAndroid Build Coastguard Worker
52*333d2b36SAndroid Build Coastguard Worker	// Output directory in which source-generated code from dependencies is
53*333d2b36SAndroid Build Coastguard Worker	// copied. This is equivalent to Cargo's OUT_DIR variable.
54*333d2b36SAndroid Build Coastguard Worker	cargoOutDir() android.OptionalPath
55*333d2b36SAndroid Build Coastguard Worker
56*333d2b36SAndroid Build Coastguard Worker	// cargoPkgVersion returns the value of the Cargo_pkg_version property.
57*333d2b36SAndroid Build Coastguard Worker	cargoPkgVersion() string
58*333d2b36SAndroid Build Coastguard Worker
59*333d2b36SAndroid Build Coastguard Worker	// cargoEnvCompat returns whether Cargo environment variables should be used.
60*333d2b36SAndroid Build Coastguard Worker	cargoEnvCompat() bool
61*333d2b36SAndroid Build Coastguard Worker
62*333d2b36SAndroid Build Coastguard Worker	inData() bool
63*333d2b36SAndroid Build Coastguard Worker	install(ctx ModuleContext)
64*333d2b36SAndroid Build Coastguard Worker	relativeInstallPath() string
65*333d2b36SAndroid Build Coastguard Worker	everInstallable() bool
66*333d2b36SAndroid Build Coastguard Worker
67*333d2b36SAndroid Build Coastguard Worker	nativeCoverage() bool
68*333d2b36SAndroid Build Coastguard Worker
69*333d2b36SAndroid Build Coastguard Worker	Disabled() bool
70*333d2b36SAndroid Build Coastguard Worker	SetDisabled()
71*333d2b36SAndroid Build Coastguard Worker
72*333d2b36SAndroid Build Coastguard Worker	stdLinkage(ctx *depsContext) RustLinkage
73*333d2b36SAndroid Build Coastguard Worker	noStdlibs() bool
74*333d2b36SAndroid Build Coastguard Worker
75*333d2b36SAndroid Build Coastguard Worker	unstrippedOutputFilePath() android.Path
76*333d2b36SAndroid Build Coastguard Worker	strippedOutputFilePath() android.OptionalPath
77*333d2b36SAndroid Build Coastguard Worker
78*333d2b36SAndroid Build Coastguard Worker	checkedCrateRootPath() (android.Path, error)
79*333d2b36SAndroid Build Coastguard Worker
80*333d2b36SAndroid Build Coastguard Worker	Aliases() map[string]string
81*333d2b36SAndroid Build Coastguard Worker}
82*333d2b36SAndroid Build Coastguard Worker
83*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) edition() string {
84*333d2b36SAndroid Build Coastguard Worker	return proptools.StringDefault(compiler.Properties.Edition, config.DefaultEdition)
85*333d2b36SAndroid Build Coastguard Worker}
86*333d2b36SAndroid Build Coastguard Worker
87*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) setNoStdlibs() {
88*333d2b36SAndroid Build Coastguard Worker	compiler.Properties.No_stdlibs = proptools.BoolPtr(true)
89*333d2b36SAndroid Build Coastguard Worker}
90*333d2b36SAndroid Build Coastguard Worker
91*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) disableLints() {
92*333d2b36SAndroid Build Coastguard Worker	compiler.Properties.Lints = proptools.StringPtr("none")
93*333d2b36SAndroid Build Coastguard Worker}
94*333d2b36SAndroid Build Coastguard Worker
95*333d2b36SAndroid Build Coastguard Workerfunc NewBaseCompiler(dir, dir64 string, location installLocation) *baseCompiler {
96*333d2b36SAndroid Build Coastguard Worker	return &baseCompiler{
97*333d2b36SAndroid Build Coastguard Worker		Properties: BaseCompilerProperties{},
98*333d2b36SAndroid Build Coastguard Worker		dir:        dir,
99*333d2b36SAndroid Build Coastguard Worker		dir64:      dir64,
100*333d2b36SAndroid Build Coastguard Worker		location:   location,
101*333d2b36SAndroid Build Coastguard Worker	}
102*333d2b36SAndroid Build Coastguard Worker}
103*333d2b36SAndroid Build Coastguard Worker
104*333d2b36SAndroid Build Coastguard Workertype installLocation int
105*333d2b36SAndroid Build Coastguard Worker
106*333d2b36SAndroid Build Coastguard Workerconst (
107*333d2b36SAndroid Build Coastguard Worker	InstallInSystem installLocation = 0
108*333d2b36SAndroid Build Coastguard Worker	InstallInData                   = iota
109*333d2b36SAndroid Build Coastguard Worker
110*333d2b36SAndroid Build Coastguard Worker	incorrectSourcesError = "srcs can only contain one path for a rust file and source providers prefixed by \":\""
111*333d2b36SAndroid Build Coastguard Worker	genSubDir             = "out/"
112*333d2b36SAndroid Build Coastguard Worker)
113*333d2b36SAndroid Build Coastguard Worker
114*333d2b36SAndroid Build Coastguard Workertype BaseCompilerProperties struct {
115*333d2b36SAndroid Build Coastguard Worker	// path to the source file that is the main entry point of the program (e.g. main.rs or lib.rs).
116*333d2b36SAndroid Build Coastguard Worker	// Only a single source file can be defined. Modules which generate source can be included by prefixing
117*333d2b36SAndroid Build Coastguard Worker	// the module name with ":", for example ":libfoo_bindgen"
118*333d2b36SAndroid Build Coastguard Worker	//
119*333d2b36SAndroid Build Coastguard Worker	// If no source file is defined, a single generated source module can be defined to be used as the main source.
120*333d2b36SAndroid Build Coastguard Worker	Srcs []string `android:"path,arch_variant"`
121*333d2b36SAndroid Build Coastguard Worker
122*333d2b36SAndroid Build Coastguard Worker	// Entry point that is passed to rustc to begin the compilation. E.g. main.rs or lib.rs.
123*333d2b36SAndroid Build Coastguard Worker	// When this property is set,
124*333d2b36SAndroid Build Coastguard Worker	//    * sandboxing is enabled for this module, and
125*333d2b36SAndroid Build Coastguard Worker	//    * the srcs attribute is interpreted as a list of all source files potentially
126*333d2b36SAndroid Build Coastguard Worker	//          used in compilation, including the entrypoint, and
127*333d2b36SAndroid Build Coastguard Worker	//    * compile_data can be used to add additional files used in compilation that
128*333d2b36SAndroid Build Coastguard Worker	//          not directly used as source files.
129*333d2b36SAndroid Build Coastguard Worker	Crate_root *string `android:"path,arch_variant"`
130*333d2b36SAndroid Build Coastguard Worker
131*333d2b36SAndroid Build Coastguard Worker	// name of the lint set that should be used to validate this module.
132*333d2b36SAndroid Build Coastguard Worker	//
133*333d2b36SAndroid Build Coastguard Worker	// Possible values are "default" (for using a sensible set of lints
134*333d2b36SAndroid Build Coastguard Worker	// depending on the module's location), "android" (for the strictest
135*333d2b36SAndroid Build Coastguard Worker	// lint set that applies to all Android platform code), "vendor" (for
136*333d2b36SAndroid Build Coastguard Worker	// a relaxed set) and "none" (for ignoring all lint warnings and
137*333d2b36SAndroid Build Coastguard Worker	// errors). The default value is "default".
138*333d2b36SAndroid Build Coastguard Worker	Lints *string
139*333d2b36SAndroid Build Coastguard Worker
140*333d2b36SAndroid Build Coastguard Worker	// flags to pass to rustc. To enable configuration options or features, use the "cfgs" or "features" properties.
141*333d2b36SAndroid Build Coastguard Worker	Flags []string `android:"arch_variant"`
142*333d2b36SAndroid Build Coastguard Worker
143*333d2b36SAndroid Build Coastguard Worker	// flags to pass to the linker
144*333d2b36SAndroid Build Coastguard Worker	Ld_flags []string `android:"arch_variant"`
145*333d2b36SAndroid Build Coastguard Worker
146*333d2b36SAndroid Build Coastguard Worker	// Rust crate dependencies to rename. Each entry should be a string of the form "dependencyname:alias".
147*333d2b36SAndroid Build Coastguard Worker	//
148*333d2b36SAndroid Build Coastguard Worker	// "dependencyname" here should be the name of the crate, not the Android module. This is
149*333d2b36SAndroid Build Coastguard Worker	// equivalent to writing `alias = { package = "dependencyname" }` in a `Cargo.toml`.
150*333d2b36SAndroid Build Coastguard Worker	Aliases []string
151*333d2b36SAndroid Build Coastguard Worker
152*333d2b36SAndroid Build Coastguard Worker	// list of rust rlib crate dependencies
153*333d2b36SAndroid Build Coastguard Worker	Rlibs []string `android:"arch_variant"`
154*333d2b36SAndroid Build Coastguard Worker
155*333d2b36SAndroid Build Coastguard Worker	// list of rust automatic crate dependencies.
156*333d2b36SAndroid Build Coastguard Worker	// Rustlibs linkage is rlib for host targets and dylib for device targets.
157*333d2b36SAndroid Build Coastguard Worker	Rustlibs proptools.Configurable[[]string] `android:"arch_variant"`
158*333d2b36SAndroid Build Coastguard Worker
159*333d2b36SAndroid Build Coastguard Worker	// list of rust proc_macro crate dependencies
160*333d2b36SAndroid Build Coastguard Worker	Proc_macros []string `android:"arch_variant"`
161*333d2b36SAndroid Build Coastguard Worker
162*333d2b36SAndroid Build Coastguard Worker	// list of C shared library dependencies
163*333d2b36SAndroid Build Coastguard Worker	Shared_libs []string `android:"arch_variant"`
164*333d2b36SAndroid Build Coastguard Worker
165*333d2b36SAndroid Build Coastguard Worker	// list of C static library dependencies. These dependencies do not normally propagate to dependents
166*333d2b36SAndroid Build Coastguard Worker	// and may need to be redeclared. See whole_static_libs for bundling static dependencies into a library.
167*333d2b36SAndroid Build Coastguard Worker	Static_libs []string `android:"arch_variant"`
168*333d2b36SAndroid Build Coastguard Worker
169*333d2b36SAndroid Build Coastguard Worker	// Similar to static_libs, but will bundle the static library dependency into a library. This is helpful
170*333d2b36SAndroid Build Coastguard Worker	// to avoid having to redeclare the dependency for dependents of this library, but in some cases may also
171*333d2b36SAndroid Build Coastguard Worker	// result in bloat if multiple dependencies all include the same static library whole.
172*333d2b36SAndroid Build Coastguard Worker	//
173*333d2b36SAndroid Build Coastguard Worker	// The common use case for this is when the static library is unlikely to be a dependency of other modules to avoid
174*333d2b36SAndroid Build Coastguard Worker	// having to redeclare the static library dependency for every dependent module.
175*333d2b36SAndroid Build Coastguard Worker	// If you are not sure what to, for rust_library modules most static dependencies should go in static_libraries,
176*333d2b36SAndroid Build Coastguard Worker	// and for rust_ffi modules most static dependencies should go into whole_static_libraries.
177*333d2b36SAndroid Build Coastguard Worker	//
178*333d2b36SAndroid Build Coastguard Worker	// For rust_ffi static variants, these libraries will be included in the resulting static library archive.
179*333d2b36SAndroid Build Coastguard Worker	//
180*333d2b36SAndroid Build Coastguard Worker	// For rust_library rlib variants, these libraries will be bundled into the resulting rlib library. This will
181*333d2b36SAndroid Build Coastguard Worker	// include all of the static libraries symbols in any dylibs or binaries which use this rlib as well.
182*333d2b36SAndroid Build Coastguard Worker	Whole_static_libs []string `android:"arch_variant"`
183*333d2b36SAndroid Build Coastguard Worker
184*333d2b36SAndroid Build Coastguard Worker	// list of Rust system library dependencies.
185*333d2b36SAndroid Build Coastguard Worker	//
186*333d2b36SAndroid Build Coastguard Worker	// This is usually only needed when `no_stdlibs` is true, in which case it can be used to depend on system crates
187*333d2b36SAndroid Build Coastguard Worker	// like `core` and `alloc`.
188*333d2b36SAndroid Build Coastguard Worker	Stdlibs []string `android:"arch_variant"`
189*333d2b36SAndroid Build Coastguard Worker
190*333d2b36SAndroid Build Coastguard Worker	// crate name, required for modules which produce Rust libraries: rust_library, rust_ffi and SourceProvider
191*333d2b36SAndroid Build Coastguard Worker	// modules which create library variants (rust_bindgen). This must be the expected extern crate name used in
192*333d2b36SAndroid Build Coastguard Worker	// source, and is required to conform to an enforced format matching library output files (if the output file is
193*333d2b36SAndroid Build Coastguard Worker	// lib<someName><suffix>, the crate_name property must be <someName>).
194*333d2b36SAndroid Build Coastguard Worker	Crate_name string `android:"arch_variant"`
195*333d2b36SAndroid Build Coastguard Worker
196*333d2b36SAndroid Build Coastguard Worker	// list of features to enable for this crate
197*333d2b36SAndroid Build Coastguard Worker	Features proptools.Configurable[[]string] `android:"arch_variant"`
198*333d2b36SAndroid Build Coastguard Worker
199*333d2b36SAndroid Build Coastguard Worker	// list of configuration options to enable for this crate. To enable features, use the "features" property.
200*333d2b36SAndroid Build Coastguard Worker	Cfgs proptools.Configurable[[]string] `android:"arch_variant"`
201*333d2b36SAndroid Build Coastguard Worker
202*333d2b36SAndroid Build Coastguard Worker	// specific rust edition that should be used if the default version is not desired
203*333d2b36SAndroid Build Coastguard Worker	Edition *string `android:"arch_variant"`
204*333d2b36SAndroid Build Coastguard Worker
205*333d2b36SAndroid Build Coastguard Worker	// sets name of the output
206*333d2b36SAndroid Build Coastguard Worker	Stem *string `android:"arch_variant"`
207*333d2b36SAndroid Build Coastguard Worker
208*333d2b36SAndroid Build Coastguard Worker	// append to name of output
209*333d2b36SAndroid Build Coastguard Worker	Suffix *string `android:"arch_variant"`
210*333d2b36SAndroid Build Coastguard Worker
211*333d2b36SAndroid Build Coastguard Worker	// install to a subdirectory of the default install path for the module
212*333d2b36SAndroid Build Coastguard Worker	Relative_install_path *string `android:"arch_variant"`
213*333d2b36SAndroid Build Coastguard Worker
214*333d2b36SAndroid Build Coastguard Worker	// whether to suppress inclusion of standard crates - defaults to false
215*333d2b36SAndroid Build Coastguard Worker	No_stdlibs *bool `android:"arch_variant"`
216*333d2b36SAndroid Build Coastguard Worker
217*333d2b36SAndroid Build Coastguard Worker	// Change the rustlibs linkage to select rlib linkage by default for device targets.
218*333d2b36SAndroid Build Coastguard Worker	// Also link libstd as an rlib as well on device targets.
219*333d2b36SAndroid Build Coastguard Worker	// Note: This is the default behavior for host targets.
220*333d2b36SAndroid Build Coastguard Worker	//
221*333d2b36SAndroid Build Coastguard Worker	// This is primarily meant for rust_binary and rust_ffi modules where the default
222*333d2b36SAndroid Build Coastguard Worker	// linkage of libstd might need to be overridden in some use cases. This should
223*333d2b36SAndroid Build Coastguard Worker	// generally be avoided with other module types since it may cause collisions at
224*333d2b36SAndroid Build Coastguard Worker	// linkage if all dependencies of the root binary module do not link against libstd
225*333d2b36SAndroid Build Coastguard Worker	// the same way.
226*333d2b36SAndroid Build Coastguard Worker	Prefer_rlib *bool `android:"arch_variant"`
227*333d2b36SAndroid Build Coastguard Worker
228*333d2b36SAndroid Build Coastguard Worker	// Enables emitting certain Cargo environment variables. Only intended to be used for compatibility purposes.
229*333d2b36SAndroid Build Coastguard Worker	// Will set CARGO_CRATE_NAME to the crate_name property's value.
230*333d2b36SAndroid Build Coastguard Worker	// Will set CARGO_BIN_NAME to the output filename value without the extension.
231*333d2b36SAndroid Build Coastguard Worker	Cargo_env_compat *bool
232*333d2b36SAndroid Build Coastguard Worker
233*333d2b36SAndroid Build Coastguard Worker	// If cargo_env_compat is true, sets the CARGO_PKG_VERSION env var to this value.
234*333d2b36SAndroid Build Coastguard Worker	Cargo_pkg_version *string
235*333d2b36SAndroid Build Coastguard Worker
236*333d2b36SAndroid Build Coastguard Worker	// Control whether LTO is used for the final (Rust) linkage. This does not impact
237*333d2b36SAndroid Build Coastguard Worker	// cross-language LTO.
238*333d2b36SAndroid Build Coastguard Worker	Lto struct {
239*333d2b36SAndroid Build Coastguard Worker		// Whether thin LTO should be enabled. By default this is true.
240*333d2b36SAndroid Build Coastguard Worker		// LTO provides such a large code size benefit for Rust, this should always
241*333d2b36SAndroid Build Coastguard Worker		// be enabled for production builds unless there's a clear need to disable it.
242*333d2b36SAndroid Build Coastguard Worker		Thin *bool `android:"arch_variant"`
243*333d2b36SAndroid Build Coastguard Worker	} `android:"arch_variant"`
244*333d2b36SAndroid Build Coastguard Worker}
245*333d2b36SAndroid Build Coastguard Worker
246*333d2b36SAndroid Build Coastguard Workertype baseCompiler struct {
247*333d2b36SAndroid Build Coastguard Worker	Properties BaseCompilerProperties
248*333d2b36SAndroid Build Coastguard Worker
249*333d2b36SAndroid Build Coastguard Worker	// Install related
250*333d2b36SAndroid Build Coastguard Worker	dir      string
251*333d2b36SAndroid Build Coastguard Worker	dir64    string
252*333d2b36SAndroid Build Coastguard Worker	subDir   string
253*333d2b36SAndroid Build Coastguard Worker	relative string
254*333d2b36SAndroid Build Coastguard Worker	path     android.InstallPath
255*333d2b36SAndroid Build Coastguard Worker	location installLocation
256*333d2b36SAndroid Build Coastguard Worker	sanitize *sanitize
257*333d2b36SAndroid Build Coastguard Worker
258*333d2b36SAndroid Build Coastguard Worker	distFile android.OptionalPath
259*333d2b36SAndroid Build Coastguard Worker
260*333d2b36SAndroid Build Coastguard Worker	installDeps android.InstallPaths
261*333d2b36SAndroid Build Coastguard Worker
262*333d2b36SAndroid Build Coastguard Worker	// unstripped output file.
263*333d2b36SAndroid Build Coastguard Worker	unstrippedOutputFile android.Path
264*333d2b36SAndroid Build Coastguard Worker
265*333d2b36SAndroid Build Coastguard Worker	// stripped output file.
266*333d2b36SAndroid Build Coastguard Worker	strippedOutputFile android.OptionalPath
267*333d2b36SAndroid Build Coastguard Worker
268*333d2b36SAndroid Build Coastguard Worker	// If a crate has a source-generated dependency, a copy of the source file
269*333d2b36SAndroid Build Coastguard Worker	// will be available in cargoOutDir (equivalent to Cargo OUT_DIR).
270*333d2b36SAndroid Build Coastguard Worker	// This is stored internally because it may not be available during
271*333d2b36SAndroid Build Coastguard Worker	// singleton-generation passes like rustdoc/rust_project.json, but should
272*333d2b36SAndroid Build Coastguard Worker	// be stashed during initial generation.
273*333d2b36SAndroid Build Coastguard Worker	cachedCargoOutDir android.ModuleOutPath
274*333d2b36SAndroid Build Coastguard Worker	// Calculated crate root cached internally because ModuleContext is not
275*333d2b36SAndroid Build Coastguard Worker	// available to singleton targets like rustdoc/rust_project.json
276*333d2b36SAndroid Build Coastguard Worker	cachedCrateRootPath android.Path
277*333d2b36SAndroid Build Coastguard Worker	// If cachedCrateRootPath is nil after initialization, this will contain
278*333d2b36SAndroid Build Coastguard Worker	// an explanation of why
279*333d2b36SAndroid Build Coastguard Worker	cachedCrateRootError error
280*333d2b36SAndroid Build Coastguard Worker}
281*333d2b36SAndroid Build Coastguard Worker
282*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) Disabled() bool {
283*333d2b36SAndroid Build Coastguard Worker	return false
284*333d2b36SAndroid Build Coastguard Worker}
285*333d2b36SAndroid Build Coastguard Worker
286*333d2b36SAndroid Build Coastguard Worker// Thin LTO is enabled by default.
287*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) Thinlto() bool {
288*333d2b36SAndroid Build Coastguard Worker	return BoolDefault(compiler.Properties.Lto.Thin, true)
289*333d2b36SAndroid Build Coastguard Worker}
290*333d2b36SAndroid Build Coastguard Worker
291*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) SetDisabled() {
292*333d2b36SAndroid Build Coastguard Worker	panic("baseCompiler does not implement SetDisabled()")
293*333d2b36SAndroid Build Coastguard Worker}
294*333d2b36SAndroid Build Coastguard Worker
295*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) noStdlibs() bool {
296*333d2b36SAndroid Build Coastguard Worker	return Bool(compiler.Properties.No_stdlibs)
297*333d2b36SAndroid Build Coastguard Worker}
298*333d2b36SAndroid Build Coastguard Worker
299*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath {
300*333d2b36SAndroid Build Coastguard Worker	panic("baseCompiler does not implement coverageOutputZipPath()")
301*333d2b36SAndroid Build Coastguard Worker}
302*333d2b36SAndroid Build Coastguard Worker
303*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) preferRlib() bool {
304*333d2b36SAndroid Build Coastguard Worker	return Bool(compiler.Properties.Prefer_rlib)
305*333d2b36SAndroid Build Coastguard Worker}
306*333d2b36SAndroid Build Coastguard Worker
307*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) Aliases() map[string]string {
308*333d2b36SAndroid Build Coastguard Worker	aliases := map[string]string{}
309*333d2b36SAndroid Build Coastguard Worker	for _, entry := range compiler.Properties.Aliases {
310*333d2b36SAndroid Build Coastguard Worker		dep, alias, found := strings.Cut(entry, ":")
311*333d2b36SAndroid Build Coastguard Worker		if !found {
312*333d2b36SAndroid Build Coastguard Worker			panic(fmt.Errorf("invalid aliases entry %q missing ':'", entry))
313*333d2b36SAndroid Build Coastguard Worker		}
314*333d2b36SAndroid Build Coastguard Worker		aliases[dep] = alias
315*333d2b36SAndroid Build Coastguard Worker	}
316*333d2b36SAndroid Build Coastguard Worker	return aliases
317*333d2b36SAndroid Build Coastguard Worker}
318*333d2b36SAndroid Build Coastguard Worker
319*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) stdLinkage(ctx *depsContext) RustLinkage {
320*333d2b36SAndroid Build Coastguard Worker	// For devices, we always link stdlibs in as dylibs by default.
321*333d2b36SAndroid Build Coastguard Worker	if compiler.preferRlib() {
322*333d2b36SAndroid Build Coastguard Worker		return RlibLinkage
323*333d2b36SAndroid Build Coastguard Worker	} else if ctx.Device() {
324*333d2b36SAndroid Build Coastguard Worker		return DylibLinkage
325*333d2b36SAndroid Build Coastguard Worker	} else {
326*333d2b36SAndroid Build Coastguard Worker		return RlibLinkage
327*333d2b36SAndroid Build Coastguard Worker	}
328*333d2b36SAndroid Build Coastguard Worker}
329*333d2b36SAndroid Build Coastguard Worker
330*333d2b36SAndroid Build Coastguard Workervar _ compiler = (*baseCompiler)(nil)
331*333d2b36SAndroid Build Coastguard Worker
332*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) inData() bool {
333*333d2b36SAndroid Build Coastguard Worker	return compiler.location == InstallInData
334*333d2b36SAndroid Build Coastguard Worker}
335*333d2b36SAndroid Build Coastguard Worker
336*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) compilerProps() []interface{} {
337*333d2b36SAndroid Build Coastguard Worker	return []interface{}{&compiler.Properties}
338*333d2b36SAndroid Build Coastguard Worker}
339*333d2b36SAndroid Build Coastguard Worker
340*333d2b36SAndroid Build Coastguard Workerfunc cfgsToFlags(cfgs []string) []string {
341*333d2b36SAndroid Build Coastguard Worker	flags := make([]string, 0, len(cfgs))
342*333d2b36SAndroid Build Coastguard Worker	for _, cfg := range cfgs {
343*333d2b36SAndroid Build Coastguard Worker		flags = append(flags, "--cfg '"+cfg+"'")
344*333d2b36SAndroid Build Coastguard Worker	}
345*333d2b36SAndroid Build Coastguard Worker
346*333d2b36SAndroid Build Coastguard Worker	return flags
347*333d2b36SAndroid Build Coastguard Worker}
348*333d2b36SAndroid Build Coastguard Worker
349*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) features(ctx android.ConfigurableEvaluatorContext, module *Module) []string {
350*333d2b36SAndroid Build Coastguard Worker	eval := module.ConfigurableEvaluator(ctx)
351*333d2b36SAndroid Build Coastguard Worker	return compiler.Properties.Features.GetOrDefault(eval, nil)
352*333d2b36SAndroid Build Coastguard Worker}
353*333d2b36SAndroid Build Coastguard Worker
354*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) featuresToFlags(ctx android.ConfigurableEvaluatorContext, module *Module) []string {
355*333d2b36SAndroid Build Coastguard Worker	flags := []string{}
356*333d2b36SAndroid Build Coastguard Worker	for _, feature := range compiler.features(ctx, module) {
357*333d2b36SAndroid Build Coastguard Worker		flags = append(flags, "--cfg 'feature=\""+feature+"\"'")
358*333d2b36SAndroid Build Coastguard Worker	}
359*333d2b36SAndroid Build Coastguard Worker
360*333d2b36SAndroid Build Coastguard Worker	return flags
361*333d2b36SAndroid Build Coastguard Worker}
362*333d2b36SAndroid Build Coastguard Worker
363*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) featureFlags(ctx ModuleContext, module *Module, flags Flags) Flags {
364*333d2b36SAndroid Build Coastguard Worker	flags.RustFlags = append(flags.RustFlags, compiler.featuresToFlags(ctx, module)...)
365*333d2b36SAndroid Build Coastguard Worker	flags.RustdocFlags = append(flags.RustdocFlags, compiler.featuresToFlags(ctx, module)...)
366*333d2b36SAndroid Build Coastguard Worker
367*333d2b36SAndroid Build Coastguard Worker	return flags
368*333d2b36SAndroid Build Coastguard Worker}
369*333d2b36SAndroid Build Coastguard Worker
370*333d2b36SAndroid Build Coastguard Workerfunc CommonDefaultCfgFlags(flags Flags, vendor bool, product bool) Flags {
371*333d2b36SAndroid Build Coastguard Worker	var cfgs []string
372*333d2b36SAndroid Build Coastguard Worker	if vendor || product {
373*333d2b36SAndroid Build Coastguard Worker		cfgs = append(cfgs, "android_vndk")
374*333d2b36SAndroid Build Coastguard Worker		if vendor {
375*333d2b36SAndroid Build Coastguard Worker			cfgs = append(cfgs, "android_vendor")
376*333d2b36SAndroid Build Coastguard Worker		} else if product {
377*333d2b36SAndroid Build Coastguard Worker			cfgs = append(cfgs, "android_product")
378*333d2b36SAndroid Build Coastguard Worker		}
379*333d2b36SAndroid Build Coastguard Worker	}
380*333d2b36SAndroid Build Coastguard Worker
381*333d2b36SAndroid Build Coastguard Worker	flags.RustFlags = append(flags.RustFlags, cfgsToFlags(cfgs)...)
382*333d2b36SAndroid Build Coastguard Worker	flags.RustdocFlags = append(flags.RustdocFlags, cfgsToFlags(cfgs)...)
383*333d2b36SAndroid Build Coastguard Worker	return flags
384*333d2b36SAndroid Build Coastguard Worker}
385*333d2b36SAndroid Build Coastguard Worker
386*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) cfgFlags(ctx ModuleContext, flags Flags) Flags {
387*333d2b36SAndroid Build Coastguard Worker	flags = CommonDefaultCfgFlags(flags, ctx.RustModule().InVendor(), ctx.RustModule().InProduct())
388*333d2b36SAndroid Build Coastguard Worker
389*333d2b36SAndroid Build Coastguard Worker	cfgFlags := cfgsToFlags(compiler.Properties.Cfgs.GetOrDefault(ctx, nil))
390*333d2b36SAndroid Build Coastguard Worker	flags.RustFlags = append(flags.RustFlags, cfgFlags...)
391*333d2b36SAndroid Build Coastguard Worker	flags.RustdocFlags = append(flags.RustdocFlags, cfgFlags...)
392*333d2b36SAndroid Build Coastguard Worker
393*333d2b36SAndroid Build Coastguard Worker	return flags
394*333d2b36SAndroid Build Coastguard Worker}
395*333d2b36SAndroid Build Coastguard Worker
396*333d2b36SAndroid Build Coastguard Workerfunc CommonDefaultFlags(ctx android.ModuleContext, toolchain config.Toolchain, flags Flags) Flags {
397*333d2b36SAndroid Build Coastguard Worker	flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...)
398*333d2b36SAndroid Build Coastguard Worker	flags.GlobalRustFlags = append(flags.GlobalRustFlags, toolchain.ToolchainRustFlags())
399*333d2b36SAndroid Build Coastguard Worker	flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, toolchain.ToolchainLinkFlags())
400*333d2b36SAndroid Build Coastguard Worker	flags.EmitXrefs = ctx.Config().EmitXrefRules()
401*333d2b36SAndroid Build Coastguard Worker
402*333d2b36SAndroid Build Coastguard Worker	if ctx.Host() && !ctx.Windows() {
403*333d2b36SAndroid Build Coastguard Worker		flags.LinkFlags = append(flags.LinkFlags, cc.RpathFlags(ctx)...)
404*333d2b36SAndroid Build Coastguard Worker	}
405*333d2b36SAndroid Build Coastguard Worker
406*333d2b36SAndroid Build Coastguard Worker	if ctx.Os() == android.Linux {
407*333d2b36SAndroid Build Coastguard Worker		// Add -lc, -lrt, -ldl, -lpthread, -lm and -lgcc_s to glibc builds to match
408*333d2b36SAndroid Build Coastguard Worker		// the default behavior of device builds.
409*333d2b36SAndroid Build Coastguard Worker		flags.LinkFlags = append(flags.LinkFlags, config.LinuxHostGlobalLinkFlags...)
410*333d2b36SAndroid Build Coastguard Worker	} else if ctx.Os() == android.Darwin {
411*333d2b36SAndroid Build Coastguard Worker		// Add -lc, -ldl, -lpthread and -lm to glibc darwin builds to match the default
412*333d2b36SAndroid Build Coastguard Worker		// behavior of device builds.
413*333d2b36SAndroid Build Coastguard Worker		flags.LinkFlags = append(flags.LinkFlags,
414*333d2b36SAndroid Build Coastguard Worker			"-lc",
415*333d2b36SAndroid Build Coastguard Worker			"-ldl",
416*333d2b36SAndroid Build Coastguard Worker			"-lpthread",
417*333d2b36SAndroid Build Coastguard Worker			"-lm",
418*333d2b36SAndroid Build Coastguard Worker		)
419*333d2b36SAndroid Build Coastguard Worker	}
420*333d2b36SAndroid Build Coastguard Worker	return flags
421*333d2b36SAndroid Build Coastguard Worker}
422*333d2b36SAndroid Build Coastguard Worker
423*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
424*333d2b36SAndroid Build Coastguard Worker
425*333d2b36SAndroid Build Coastguard Worker	flags = CommonDefaultFlags(ctx, ctx.toolchain(), flags)
426*333d2b36SAndroid Build Coastguard Worker	lintFlags, err := config.RustcLintsForDir(ctx.ModuleDir(), compiler.Properties.Lints)
427*333d2b36SAndroid Build Coastguard Worker	if err != nil {
428*333d2b36SAndroid Build Coastguard Worker		ctx.PropertyErrorf("lints", err.Error())
429*333d2b36SAndroid Build Coastguard Worker	}
430*333d2b36SAndroid Build Coastguard Worker
431*333d2b36SAndroid Build Coastguard Worker	// linkage-related flags are disallowed.
432*333d2b36SAndroid Build Coastguard Worker	for _, s := range compiler.Properties.Ld_flags {
433*333d2b36SAndroid Build Coastguard Worker		if strings.HasPrefix(s, "-Wl,-l") || strings.HasPrefix(s, "-Wl,-L") {
434*333d2b36SAndroid Build Coastguard Worker			ctx.PropertyErrorf("ld_flags", "'-Wl,-l' and '-Wl,-L' flags cannot be manually specified")
435*333d2b36SAndroid Build Coastguard Worker		}
436*333d2b36SAndroid Build Coastguard Worker	}
437*333d2b36SAndroid Build Coastguard Worker	for _, s := range compiler.Properties.Flags {
438*333d2b36SAndroid Build Coastguard Worker		if strings.HasPrefix(s, "-l") || strings.HasPrefix(s, "-L") {
439*333d2b36SAndroid Build Coastguard Worker			ctx.PropertyErrorf("flags", "'-l' and '-L' flags cannot be manually specified")
440*333d2b36SAndroid Build Coastguard Worker		}
441*333d2b36SAndroid Build Coastguard Worker		if strings.HasPrefix(s, "--extern") {
442*333d2b36SAndroid Build Coastguard Worker			ctx.PropertyErrorf("flags", "'--extern' flag cannot be manually specified")
443*333d2b36SAndroid Build Coastguard Worker		}
444*333d2b36SAndroid Build Coastguard Worker		if strings.HasPrefix(s, "-Clink-args=") || strings.HasPrefix(s, "-C link-args=") {
445*333d2b36SAndroid Build Coastguard Worker			ctx.PropertyErrorf("flags", "'-C link-args' flag cannot be manually specified")
446*333d2b36SAndroid Build Coastguard Worker		}
447*333d2b36SAndroid Build Coastguard Worker	}
448*333d2b36SAndroid Build Coastguard Worker
449*333d2b36SAndroid Build Coastguard Worker	flags.RustFlags = append(flags.RustFlags, lintFlags)
450*333d2b36SAndroid Build Coastguard Worker	flags.RustFlags = append(flags.RustFlags, compiler.Properties.Flags...)
451*333d2b36SAndroid Build Coastguard Worker	flags.RustFlags = append(flags.RustFlags, "--edition="+compiler.edition())
452*333d2b36SAndroid Build Coastguard Worker	flags.RustdocFlags = append(flags.RustdocFlags, "--edition="+compiler.edition())
453*333d2b36SAndroid Build Coastguard Worker	flags.LinkFlags = append(flags.LinkFlags, compiler.Properties.Ld_flags...)
454*333d2b36SAndroid Build Coastguard Worker
455*333d2b36SAndroid Build Coastguard Worker	return flags
456*333d2b36SAndroid Build Coastguard Worker}
457*333d2b36SAndroid Build Coastguard Worker
458*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
459*333d2b36SAndroid Build Coastguard Worker	panic(fmt.Errorf("baseCrater doesn't know how to crate things!"))
460*333d2b36SAndroid Build Coastguard Worker}
461*333d2b36SAndroid Build Coastguard Worker
462*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) rustdoc(ctx ModuleContext, flags Flags,
463*333d2b36SAndroid Build Coastguard Worker	deps PathDeps) android.OptionalPath {
464*333d2b36SAndroid Build Coastguard Worker
465*333d2b36SAndroid Build Coastguard Worker	return android.OptionalPath{}
466*333d2b36SAndroid Build Coastguard Worker}
467*333d2b36SAndroid Build Coastguard Worker
468*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) initialize(ctx ModuleContext) {
469*333d2b36SAndroid Build Coastguard Worker	compiler.cachedCargoOutDir = android.PathForModuleOut(ctx, genSubDir)
470*333d2b36SAndroid Build Coastguard Worker	if compiler.Properties.Crate_root == nil {
471*333d2b36SAndroid Build Coastguard Worker		compiler.cachedCrateRootPath, compiler.cachedCrateRootError = srcPathFromModuleSrcs(ctx, compiler.Properties.Srcs)
472*333d2b36SAndroid Build Coastguard Worker	} else {
473*333d2b36SAndroid Build Coastguard Worker		compiler.cachedCrateRootPath = android.PathForModuleSrc(ctx, *compiler.Properties.Crate_root)
474*333d2b36SAndroid Build Coastguard Worker		compiler.cachedCrateRootError = nil
475*333d2b36SAndroid Build Coastguard Worker	}
476*333d2b36SAndroid Build Coastguard Worker}
477*333d2b36SAndroid Build Coastguard Worker
478*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) cargoOutDir() android.OptionalPath {
479*333d2b36SAndroid Build Coastguard Worker	return android.OptionalPathForPath(compiler.cachedCargoOutDir)
480*333d2b36SAndroid Build Coastguard Worker}
481*333d2b36SAndroid Build Coastguard Worker
482*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) cargoEnvCompat() bool {
483*333d2b36SAndroid Build Coastguard Worker	return Bool(compiler.Properties.Cargo_env_compat)
484*333d2b36SAndroid Build Coastguard Worker}
485*333d2b36SAndroid Build Coastguard Worker
486*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) cargoPkgVersion() string {
487*333d2b36SAndroid Build Coastguard Worker	return String(compiler.Properties.Cargo_pkg_version)
488*333d2b36SAndroid Build Coastguard Worker}
489*333d2b36SAndroid Build Coastguard Worker
490*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) unstrippedOutputFilePath() android.Path {
491*333d2b36SAndroid Build Coastguard Worker	return compiler.unstrippedOutputFile
492*333d2b36SAndroid Build Coastguard Worker}
493*333d2b36SAndroid Build Coastguard Worker
494*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) strippedOutputFilePath() android.OptionalPath {
495*333d2b36SAndroid Build Coastguard Worker	return compiler.strippedOutputFile
496*333d2b36SAndroid Build Coastguard Worker}
497*333d2b36SAndroid Build Coastguard Worker
498*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
499*333d2b36SAndroid Build Coastguard Worker	deps.Rlibs = append(deps.Rlibs, compiler.Properties.Rlibs...)
500*333d2b36SAndroid Build Coastguard Worker	deps.Rustlibs = append(deps.Rustlibs, compiler.Properties.Rustlibs.GetOrDefault(ctx, nil)...)
501*333d2b36SAndroid Build Coastguard Worker	deps.ProcMacros = append(deps.ProcMacros, compiler.Properties.Proc_macros...)
502*333d2b36SAndroid Build Coastguard Worker	deps.StaticLibs = append(deps.StaticLibs, compiler.Properties.Static_libs...)
503*333d2b36SAndroid Build Coastguard Worker	deps.WholeStaticLibs = append(deps.WholeStaticLibs, compiler.Properties.Whole_static_libs...)
504*333d2b36SAndroid Build Coastguard Worker	deps.SharedLibs = append(deps.SharedLibs, compiler.Properties.Shared_libs...)
505*333d2b36SAndroid Build Coastguard Worker	deps.Stdlibs = append(deps.Stdlibs, compiler.Properties.Stdlibs...)
506*333d2b36SAndroid Build Coastguard Worker
507*333d2b36SAndroid Build Coastguard Worker	if !Bool(compiler.Properties.No_stdlibs) {
508*333d2b36SAndroid Build Coastguard Worker		for _, stdlib := range config.Stdlibs {
509*333d2b36SAndroid Build Coastguard Worker			// If we're building for the build host, use the prebuilt stdlibs, unless the host
510*333d2b36SAndroid Build Coastguard Worker			// is linux_bionic which doesn't have prebuilts.
511*333d2b36SAndroid Build Coastguard Worker			if ctx.Host() && !ctx.Target().HostCross && ctx.Target().Os != android.LinuxBionic {
512*333d2b36SAndroid Build Coastguard Worker				stdlib = "prebuilt_" + stdlib
513*333d2b36SAndroid Build Coastguard Worker			}
514*333d2b36SAndroid Build Coastguard Worker			deps.Stdlibs = append(deps.Stdlibs, stdlib)
515*333d2b36SAndroid Build Coastguard Worker		}
516*333d2b36SAndroid Build Coastguard Worker	}
517*333d2b36SAndroid Build Coastguard Worker	return deps
518*333d2b36SAndroid Build Coastguard Worker}
519*333d2b36SAndroid Build Coastguard Worker
520*333d2b36SAndroid Build Coastguard Workerfunc bionicDeps(ctx DepsContext, deps Deps, static bool) Deps {
521*333d2b36SAndroid Build Coastguard Worker	bionicLibs := []string{}
522*333d2b36SAndroid Build Coastguard Worker	bionicLibs = append(bionicLibs, "liblog")
523*333d2b36SAndroid Build Coastguard Worker	bionicLibs = append(bionicLibs, "libc")
524*333d2b36SAndroid Build Coastguard Worker	bionicLibs = append(bionicLibs, "libm")
525*333d2b36SAndroid Build Coastguard Worker	bionicLibs = append(bionicLibs, "libdl")
526*333d2b36SAndroid Build Coastguard Worker
527*333d2b36SAndroid Build Coastguard Worker	if static {
528*333d2b36SAndroid Build Coastguard Worker		deps.StaticLibs = append(deps.StaticLibs, bionicLibs...)
529*333d2b36SAndroid Build Coastguard Worker	} else {
530*333d2b36SAndroid Build Coastguard Worker		deps.SharedLibs = append(deps.SharedLibs, bionicLibs...)
531*333d2b36SAndroid Build Coastguard Worker	}
532*333d2b36SAndroid Build Coastguard Worker	if ctx.RustModule().StaticExecutable() {
533*333d2b36SAndroid Build Coastguard Worker		deps.StaticLibs = append(deps.StaticLibs, "libunwind")
534*333d2b36SAndroid Build Coastguard Worker	}
535*333d2b36SAndroid Build Coastguard Worker	if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
536*333d2b36SAndroid Build Coastguard Worker		deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
537*333d2b36SAndroid Build Coastguard Worker	}
538*333d2b36SAndroid Build Coastguard Worker	return deps
539*333d2b36SAndroid Build Coastguard Worker}
540*333d2b36SAndroid Build Coastguard Worker
541*333d2b36SAndroid Build Coastguard Workerfunc muslDeps(ctx DepsContext, deps Deps, static bool) Deps {
542*333d2b36SAndroid Build Coastguard Worker	muslLibs := []string{"libc_musl"}
543*333d2b36SAndroid Build Coastguard Worker	if static {
544*333d2b36SAndroid Build Coastguard Worker		deps.StaticLibs = append(deps.StaticLibs, muslLibs...)
545*333d2b36SAndroid Build Coastguard Worker	} else {
546*333d2b36SAndroid Build Coastguard Worker		deps.SharedLibs = append(deps.SharedLibs, muslLibs...)
547*333d2b36SAndroid Build Coastguard Worker	}
548*333d2b36SAndroid Build Coastguard Worker	if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
549*333d2b36SAndroid Build Coastguard Worker		deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
550*333d2b36SAndroid Build Coastguard Worker	}
551*333d2b36SAndroid Build Coastguard Worker
552*333d2b36SAndroid Build Coastguard Worker	return deps
553*333d2b36SAndroid Build Coastguard Worker}
554*333d2b36SAndroid Build Coastguard Worker
555*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) crateName() string {
556*333d2b36SAndroid Build Coastguard Worker	return compiler.Properties.Crate_name
557*333d2b36SAndroid Build Coastguard Worker}
558*333d2b36SAndroid Build Coastguard Worker
559*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) everInstallable() bool {
560*333d2b36SAndroid Build Coastguard Worker	// Most modules are installable, so return true by default.
561*333d2b36SAndroid Build Coastguard Worker	return true
562*333d2b36SAndroid Build Coastguard Worker}
563*333d2b36SAndroid Build Coastguard Worker
564*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) installDir(ctx ModuleContext) android.InstallPath {
565*333d2b36SAndroid Build Coastguard Worker	dir := compiler.dir
566*333d2b36SAndroid Build Coastguard Worker	if ctx.toolchain().Is64Bit() && compiler.dir64 != "" {
567*333d2b36SAndroid Build Coastguard Worker		dir = compiler.dir64
568*333d2b36SAndroid Build Coastguard Worker	}
569*333d2b36SAndroid Build Coastguard Worker	if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
570*333d2b36SAndroid Build Coastguard Worker		dir = filepath.Join(dir, ctx.Target().NativeBridgeRelativePath)
571*333d2b36SAndroid Build Coastguard Worker	}
572*333d2b36SAndroid Build Coastguard Worker	if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
573*333d2b36SAndroid Build Coastguard Worker		dir = filepath.Join(dir, ctx.Arch().ArchType.String())
574*333d2b36SAndroid Build Coastguard Worker	}
575*333d2b36SAndroid Build Coastguard Worker
576*333d2b36SAndroid Build Coastguard Worker	if compiler.location == InstallInData && ctx.RustModule().InVendorOrProduct() {
577*333d2b36SAndroid Build Coastguard Worker		if ctx.RustModule().InProduct() {
578*333d2b36SAndroid Build Coastguard Worker			dir = filepath.Join(dir, "product")
579*333d2b36SAndroid Build Coastguard Worker		} else if ctx.RustModule().InVendor() {
580*333d2b36SAndroid Build Coastguard Worker			dir = filepath.Join(dir, "vendor")
581*333d2b36SAndroid Build Coastguard Worker		} else {
582*333d2b36SAndroid Build Coastguard Worker			ctx.ModuleErrorf("Unknown data+VNDK installation kind")
583*333d2b36SAndroid Build Coastguard Worker		}
584*333d2b36SAndroid Build Coastguard Worker	}
585*333d2b36SAndroid Build Coastguard Worker
586*333d2b36SAndroid Build Coastguard Worker	return android.PathForModuleInstall(ctx, dir, compiler.subDir,
587*333d2b36SAndroid Build Coastguard Worker		compiler.relativeInstallPath(), compiler.relative)
588*333d2b36SAndroid Build Coastguard Worker}
589*333d2b36SAndroid Build Coastguard Worker
590*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) nativeCoverage() bool {
591*333d2b36SAndroid Build Coastguard Worker	return false
592*333d2b36SAndroid Build Coastguard Worker}
593*333d2b36SAndroid Build Coastguard Worker
594*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) install(ctx ModuleContext) {
595*333d2b36SAndroid Build Coastguard Worker	path := ctx.RustModule().OutputFile()
596*333d2b36SAndroid Build Coastguard Worker	compiler.path = ctx.InstallFile(compiler.installDir(ctx), path.Path().Base(), path.Path(), compiler.installDeps...)
597*333d2b36SAndroid Build Coastguard Worker}
598*333d2b36SAndroid Build Coastguard Worker
599*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) installTestData(ctx ModuleContext, data []android.DataPath) {
600*333d2b36SAndroid Build Coastguard Worker	installedData := ctx.InstallTestData(compiler.installDir(ctx), data)
601*333d2b36SAndroid Build Coastguard Worker	compiler.installDeps = append(compiler.installDeps, installedData...)
602*333d2b36SAndroid Build Coastguard Worker}
603*333d2b36SAndroid Build Coastguard Worker
604*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) getStem(ctx android.ModuleContext) string {
605*333d2b36SAndroid Build Coastguard Worker	return compiler.getStemWithoutSuffix(ctx) + String(compiler.Properties.Suffix)
606*333d2b36SAndroid Build Coastguard Worker}
607*333d2b36SAndroid Build Coastguard Worker
608*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) getStemWithoutSuffix(ctx android.BaseModuleContext) string {
609*333d2b36SAndroid Build Coastguard Worker	stem := ctx.ModuleName()
610*333d2b36SAndroid Build Coastguard Worker	if String(compiler.Properties.Stem) != "" {
611*333d2b36SAndroid Build Coastguard Worker		stem = String(compiler.Properties.Stem)
612*333d2b36SAndroid Build Coastguard Worker	}
613*333d2b36SAndroid Build Coastguard Worker
614*333d2b36SAndroid Build Coastguard Worker	return stem
615*333d2b36SAndroid Build Coastguard Worker}
616*333d2b36SAndroid Build Coastguard Worker
617*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) relativeInstallPath() string {
618*333d2b36SAndroid Build Coastguard Worker	return String(compiler.Properties.Relative_install_path)
619*333d2b36SAndroid Build Coastguard Worker}
620*333d2b36SAndroid Build Coastguard Worker
621*333d2b36SAndroid Build Coastguard Workerfunc (compiler *baseCompiler) checkedCrateRootPath() (android.Path, error) {
622*333d2b36SAndroid Build Coastguard Worker	return compiler.cachedCrateRootPath, compiler.cachedCrateRootError
623*333d2b36SAndroid Build Coastguard Worker}
624*333d2b36SAndroid Build Coastguard Worker
625*333d2b36SAndroid Build Coastguard Workerfunc crateRootPath(ctx ModuleContext, compiler compiler) android.Path {
626*333d2b36SAndroid Build Coastguard Worker	root, err := compiler.checkedCrateRootPath()
627*333d2b36SAndroid Build Coastguard Worker	if err != nil {
628*333d2b36SAndroid Build Coastguard Worker		ctx.PropertyErrorf("srcs", err.Error())
629*333d2b36SAndroid Build Coastguard Worker	}
630*333d2b36SAndroid Build Coastguard Worker	return root
631*333d2b36SAndroid Build Coastguard Worker}
632*333d2b36SAndroid Build Coastguard Worker
633*333d2b36SAndroid Build Coastguard Worker// Returns the Path for the main source file along with Paths for generated source files from modules listed in srcs.
634*333d2b36SAndroid Build Coastguard Workerfunc srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) (android.Path, error) {
635*333d2b36SAndroid Build Coastguard Worker	// The srcs can contain strings with prefix ":".
636*333d2b36SAndroid Build Coastguard Worker	// They are dependent modules of this module, with android.SourceDepTag.
637*333d2b36SAndroid Build Coastguard Worker	// They are not the main source file compiled by rustc.
638*333d2b36SAndroid Build Coastguard Worker	numSrcs := 0
639*333d2b36SAndroid Build Coastguard Worker	srcIndex := 0
640*333d2b36SAndroid Build Coastguard Worker	for i, s := range srcs {
641*333d2b36SAndroid Build Coastguard Worker		if android.SrcIsModule(s) == "" {
642*333d2b36SAndroid Build Coastguard Worker			numSrcs++
643*333d2b36SAndroid Build Coastguard Worker			srcIndex = i
644*333d2b36SAndroid Build Coastguard Worker		}
645*333d2b36SAndroid Build Coastguard Worker	}
646*333d2b36SAndroid Build Coastguard Worker	if numSrcs > 1 {
647*333d2b36SAndroid Build Coastguard Worker		return nil, errors.New(incorrectSourcesError)
648*333d2b36SAndroid Build Coastguard Worker	}
649*333d2b36SAndroid Build Coastguard Worker
650*333d2b36SAndroid Build Coastguard Worker	// If a main source file is not provided we expect only a single SourceProvider module to be defined
651*333d2b36SAndroid Build Coastguard Worker	// within srcs, with the expectation that the first source it provides is the entry point.
652*333d2b36SAndroid Build Coastguard Worker	if srcIndex != 0 {
653*333d2b36SAndroid Build Coastguard Worker		return nil, errors.New("main source file must be the first in srcs")
654*333d2b36SAndroid Build Coastguard Worker	} else if numSrcs > 1 {
655*333d2b36SAndroid Build Coastguard Worker		return nil, errors.New("only a single generated source module can be defined without a main source file.")
656*333d2b36SAndroid Build Coastguard Worker	}
657*333d2b36SAndroid Build Coastguard Worker
658*333d2b36SAndroid Build Coastguard Worker	// TODO: b/297264540 - once all modules are sandboxed, we need to select the proper
659*333d2b36SAndroid Build Coastguard Worker	// entry point file from Srcs rather than taking the first one
660*333d2b36SAndroid Build Coastguard Worker	paths := android.PathsForModuleSrc(ctx, srcs)
661*333d2b36SAndroid Build Coastguard Worker	if len(paths) == 0 {
662*333d2b36SAndroid Build Coastguard Worker		return nil, errors.New("srcs must not be empty")
663*333d2b36SAndroid Build Coastguard Worker	}
664*333d2b36SAndroid Build Coastguard Worker	return paths[srcIndex], nil
665*333d2b36SAndroid Build Coastguard Worker}
666