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