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