Ivan Lozano | 45e0e5b | 2021-11-13 07:42:36 -0500 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2021 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package rust |
| 18 | |
| 19 | import ( |
| 20 | "path" |
| 21 | |
| 22 | "android/soong/android" |
| 23 | "android/soong/rust/config" |
| 24 | ) |
| 25 | |
| 26 | // This module is used to compile the rust toolchain libraries |
| 27 | // When RUST_PREBUILTS_VERSION is set, the library will generated |
| 28 | // from the given Rust version. |
| 29 | func init() { |
| 30 | android.RegisterModuleType("rust_toolchain_library", |
| 31 | rustToolchainLibraryFactory) |
| 32 | android.RegisterModuleType("rust_toolchain_library_rlib", |
| 33 | rustToolchainLibraryRlibFactory) |
| 34 | android.RegisterModuleType("rust_toolchain_library_dylib", |
| 35 | rustToolchainLibraryDylibFactory) |
| 36 | } |
| 37 | |
| 38 | type toolchainLibraryProperties struct { |
| 39 | // path to the toolchain source, relative to the top of the toolchain source |
| 40 | Toolchain_src *string `android:"arch_variant"` |
| 41 | } |
| 42 | |
| 43 | type toolchainLibraryDecorator struct { |
| 44 | *libraryDecorator |
| 45 | Properties toolchainLibraryProperties |
| 46 | } |
| 47 | |
| 48 | // rust_toolchain_library produces all rust variants. |
| 49 | func rustToolchainLibraryFactory() android.Module { |
| 50 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 51 | library.BuildOnlyRust() |
| 52 | |
| 53 | return initToolchainLibrary(module, library) |
| 54 | } |
| 55 | |
| 56 | // rust_toolchain_library_dylib produces a dylib. |
| 57 | func rustToolchainLibraryDylibFactory() android.Module { |
| 58 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 59 | library.BuildOnlyDylib() |
| 60 | |
| 61 | return initToolchainLibrary(module, library) |
| 62 | } |
| 63 | |
| 64 | // rust_toolchain_library_rlib produces an rlib. |
| 65 | func rustToolchainLibraryRlibFactory() android.Module { |
| 66 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 67 | library.BuildOnlyRlib() |
| 68 | |
| 69 | return initToolchainLibrary(module, library) |
| 70 | } |
| 71 | |
| 72 | func initToolchainLibrary(module *Module, library *libraryDecorator) android.Module { |
| 73 | toolchainLibrary := &toolchainLibraryDecorator{ |
| 74 | libraryDecorator: library, |
| 75 | } |
| 76 | module.compiler = toolchainLibrary |
| 77 | module.AddProperties(&toolchainLibrary.Properties) |
| 78 | android.AddLoadHook(module, rustSetToolchainSource) |
| 79 | |
| 80 | return module.Init() |
| 81 | } |
| 82 | |
| 83 | func rustSetToolchainSource(ctx android.LoadHookContext) { |
| 84 | if toolchainLib, ok := ctx.Module().(*Module).compiler.(*toolchainLibraryDecorator); ok { |
| 85 | prefix := "linux-x86/" + GetRustPrebuiltVersion(ctx) |
| 86 | newSrcs := []string{path.Join(prefix, android.String(toolchainLib.Properties.Toolchain_src))} |
| 87 | |
| 88 | type props struct { |
| 89 | Srcs []string |
| 90 | } |
| 91 | p := &props{} |
| 92 | p.Srcs = newSrcs |
| 93 | ctx.AppendProperties(p) |
| 94 | |
| 95 | } else { |
| 96 | ctx.ModuleErrorf("Called rustSetToolchainSource on a non-Rust Module.") |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // GetRustPrebuiltVersion returns the RUST_PREBUILTS_VERSION env var, or the default version if it is not defined. |
| 101 | func GetRustPrebuiltVersion(ctx android.LoadHookContext) string { |
| 102 | return ctx.AConfig().GetenvWithDefault("RUST_PREBUILTS_VERSION", config.RustDefaultVersion) |
| 103 | } |