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" |
Sam Delmerico | b45c844 | 2023-07-13 10:52:57 -0400 | [diff] [blame] | 24 | |
| 25 | "github.com/google/blueprint/proptools" |
Ivan Lozano | 45e0e5b | 2021-11-13 07:42:36 -0500 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | // This module is used to compile the rust toolchain libraries |
| 29 | // When RUST_PREBUILTS_VERSION is set, the library will generated |
| 30 | // from the given Rust version. |
| 31 | func init() { |
| 32 | android.RegisterModuleType("rust_toolchain_library", |
| 33 | rustToolchainLibraryFactory) |
| 34 | android.RegisterModuleType("rust_toolchain_library_rlib", |
| 35 | rustToolchainLibraryRlibFactory) |
| 36 | android.RegisterModuleType("rust_toolchain_library_dylib", |
| 37 | rustToolchainLibraryDylibFactory) |
Sam Delmerico | b45c844 | 2023-07-13 10:52:57 -0400 | [diff] [blame] | 38 | android.RegisterModuleType("rust_toolchain_rustc_prebuilt", |
| 39 | rustToolchainRustcPrebuiltFactory) |
Ivan Lozano | 45e0e5b | 2021-11-13 07:42:36 -0500 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | type toolchainLibraryProperties struct { |
Sam Delmerico | 553edff | 2023-07-13 11:15:37 -0400 | [diff] [blame] | 43 | // path to the toolchain crate root, relative to the top of the toolchain source |
| 44 | Toolchain_crate_root *string `android:"arch_variant"` |
| 45 | // path to the rest of the toolchain srcs, relative to the top of the toolchain source |
| 46 | Toolchain_srcs []string `android:"arch_variant"` |
Ivan Lozano | 45e0e5b | 2021-11-13 07:42:36 -0500 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | type toolchainLibraryDecorator struct { |
| 50 | *libraryDecorator |
| 51 | Properties toolchainLibraryProperties |
| 52 | } |
| 53 | |
| 54 | // rust_toolchain_library produces all rust variants. |
| 55 | func rustToolchainLibraryFactory() android.Module { |
| 56 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 57 | library.BuildOnlyRust() |
| 58 | |
| 59 | return initToolchainLibrary(module, library) |
| 60 | } |
| 61 | |
| 62 | // rust_toolchain_library_dylib produces a dylib. |
| 63 | func rustToolchainLibraryDylibFactory() android.Module { |
| 64 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 65 | library.BuildOnlyDylib() |
| 66 | |
| 67 | return initToolchainLibrary(module, library) |
| 68 | } |
| 69 | |
| 70 | // rust_toolchain_library_rlib produces an rlib. |
| 71 | func rustToolchainLibraryRlibFactory() android.Module { |
| 72 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 73 | library.BuildOnlyRlib() |
| 74 | |
| 75 | return initToolchainLibrary(module, library) |
| 76 | } |
| 77 | |
| 78 | func initToolchainLibrary(module *Module, library *libraryDecorator) android.Module { |
| 79 | toolchainLibrary := &toolchainLibraryDecorator{ |
| 80 | libraryDecorator: library, |
| 81 | } |
| 82 | module.compiler = toolchainLibrary |
| 83 | module.AddProperties(&toolchainLibrary.Properties) |
| 84 | android.AddLoadHook(module, rustSetToolchainSource) |
| 85 | |
| 86 | return module.Init() |
| 87 | } |
| 88 | |
| 89 | func rustSetToolchainSource(ctx android.LoadHookContext) { |
| 90 | if toolchainLib, ok := ctx.Module().(*Module).compiler.(*toolchainLibraryDecorator); ok { |
| 91 | prefix := "linux-x86/" + GetRustPrebuiltVersion(ctx) |
Sam Delmerico | 553edff | 2023-07-13 11:15:37 -0400 | [diff] [blame] | 92 | versionedCrateRoot := path.Join(prefix, android.String(toolchainLib.Properties.Toolchain_crate_root)) |
| 93 | versionedSrcs := make([]string, len(toolchainLib.Properties.Toolchain_srcs)) |
| 94 | for i, src := range toolchainLib.Properties.Toolchain_srcs { |
| 95 | versionedSrcs[i] = path.Join(prefix, src) |
| 96 | } |
Ivan Lozano | 45e0e5b | 2021-11-13 07:42:36 -0500 | [diff] [blame] | 97 | |
| 98 | type props struct { |
Sam Delmerico | 553edff | 2023-07-13 11:15:37 -0400 | [diff] [blame] | 99 | Crate_root *string |
| 100 | Srcs []string |
Ivan Lozano | 45e0e5b | 2021-11-13 07:42:36 -0500 | [diff] [blame] | 101 | } |
| 102 | p := &props{} |
Sam Delmerico | 553edff | 2023-07-13 11:15:37 -0400 | [diff] [blame] | 103 | p.Crate_root = &versionedCrateRoot |
| 104 | p.Srcs = versionedSrcs |
Ivan Lozano | 45e0e5b | 2021-11-13 07:42:36 -0500 | [diff] [blame] | 105 | ctx.AppendProperties(p) |
Ivan Lozano | 45e0e5b | 2021-11-13 07:42:36 -0500 | [diff] [blame] | 106 | } else { |
| 107 | ctx.ModuleErrorf("Called rustSetToolchainSource on a non-Rust Module.") |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // GetRustPrebuiltVersion returns the RUST_PREBUILTS_VERSION env var, or the default version if it is not defined. |
| 112 | func GetRustPrebuiltVersion(ctx android.LoadHookContext) string { |
| 113 | return ctx.AConfig().GetenvWithDefault("RUST_PREBUILTS_VERSION", config.RustDefaultVersion) |
| 114 | } |
Sam Delmerico | b45c844 | 2023-07-13 10:52:57 -0400 | [diff] [blame] | 115 | |
| 116 | type toolchainRustcPrebuiltProperties struct { |
| 117 | // path to rustc prebuilt, relative to the top of the toolchain source |
| 118 | Toolchain_prebuilt_src *string |
| 119 | // path to deps, relative to the top of the toolchain source |
| 120 | Toolchain_deps []string |
| 121 | // path to deps, relative to module directory |
| 122 | Deps []string |
| 123 | } |
| 124 | |
| 125 | func rustToolchainRustcPrebuiltFactory() android.Module { |
| 126 | module := android.NewPrebuiltBuildTool() |
| 127 | module.AddProperties(&toolchainRustcPrebuiltProperties{}) |
| 128 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { |
| 129 | var toolchainProps *toolchainRustcPrebuiltProperties |
| 130 | for _, p := range ctx.Module().GetProperties() { |
| 131 | toolchainProperties, ok := p.(*toolchainRustcPrebuiltProperties) |
| 132 | if ok { |
| 133 | toolchainProps = toolchainProperties |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if toolchainProps.Toolchain_prebuilt_src == nil { |
| 138 | ctx.PropertyErrorf("toolchain_prebuilt_src", "must set path to rustc prebuilt") |
| 139 | } |
| 140 | |
| 141 | prefix := "linux-x86/" + GetRustPrebuiltVersion(ctx) |
| 142 | deps := make([]string, 0, len(toolchainProps.Toolchain_deps)+len(toolchainProps.Deps)) |
| 143 | for _, d := range toolchainProps.Toolchain_deps { |
| 144 | deps = append(deps, path.Join(prefix, d)) |
| 145 | } |
| 146 | deps = append(deps, toolchainProps.Deps...) |
| 147 | |
| 148 | props := struct { |
| 149 | Src *string |
| 150 | Deps []string |
| 151 | }{ |
| 152 | Src: proptools.StringPtr(path.Join(prefix, *toolchainProps.Toolchain_prebuilt_src)), |
| 153 | Deps: deps, |
| 154 | } |
| 155 | ctx.AppendProperties(&props) |
| 156 | }) |
| 157 | return module |
| 158 | } |