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