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