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