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