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