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