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