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 { |
| 43 | // path to the toolchain source, relative to the top of the toolchain source |
| 44 | Toolchain_src *string `android:"arch_variant"` |
| 45 | } |
| 46 | |
| 47 | type toolchainLibraryDecorator struct { |
| 48 | *libraryDecorator |
| 49 | Properties toolchainLibraryProperties |
| 50 | } |
| 51 | |
| 52 | // rust_toolchain_library produces all rust variants. |
| 53 | func rustToolchainLibraryFactory() android.Module { |
| 54 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 55 | library.BuildOnlyRust() |
| 56 | |
| 57 | return initToolchainLibrary(module, library) |
| 58 | } |
| 59 | |
| 60 | // rust_toolchain_library_dylib produces a dylib. |
| 61 | func rustToolchainLibraryDylibFactory() android.Module { |
| 62 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 63 | library.BuildOnlyDylib() |
| 64 | |
| 65 | return initToolchainLibrary(module, library) |
| 66 | } |
| 67 | |
| 68 | // rust_toolchain_library_rlib produces an rlib. |
| 69 | func rustToolchainLibraryRlibFactory() android.Module { |
| 70 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 71 | library.BuildOnlyRlib() |
| 72 | |
| 73 | return initToolchainLibrary(module, library) |
| 74 | } |
| 75 | |
| 76 | func initToolchainLibrary(module *Module, library *libraryDecorator) android.Module { |
| 77 | toolchainLibrary := &toolchainLibraryDecorator{ |
| 78 | libraryDecorator: library, |
| 79 | } |
| 80 | module.compiler = toolchainLibrary |
| 81 | module.AddProperties(&toolchainLibrary.Properties) |
| 82 | android.AddLoadHook(module, rustSetToolchainSource) |
| 83 | |
| 84 | return module.Init() |
| 85 | } |
| 86 | |
| 87 | func rustSetToolchainSource(ctx android.LoadHookContext) { |
| 88 | if toolchainLib, ok := ctx.Module().(*Module).compiler.(*toolchainLibraryDecorator); ok { |
| 89 | prefix := "linux-x86/" + GetRustPrebuiltVersion(ctx) |
| 90 | newSrcs := []string{path.Join(prefix, android.String(toolchainLib.Properties.Toolchain_src))} |
| 91 | |
| 92 | type props struct { |
| 93 | Srcs []string |
| 94 | } |
| 95 | p := &props{} |
| 96 | p.Srcs = newSrcs |
| 97 | ctx.AppendProperties(p) |
| 98 | |
| 99 | } else { |
| 100 | ctx.ModuleErrorf("Called rustSetToolchainSource on a non-Rust Module.") |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // GetRustPrebuiltVersion returns the RUST_PREBUILTS_VERSION env var, or the default version if it is not defined. |
| 105 | func GetRustPrebuiltVersion(ctx android.LoadHookContext) string { |
| 106 | return ctx.AConfig().GetenvWithDefault("RUST_PREBUILTS_VERSION", config.RustDefaultVersion) |
| 107 | } |
Sam Delmerico | b45c844 | 2023-07-13 10:52:57 -0400 | [diff] [blame^] | 108 | |
| 109 | type toolchainRustcPrebuiltProperties struct { |
| 110 | // path to rustc prebuilt, relative to the top of the toolchain source |
| 111 | Toolchain_prebuilt_src *string |
| 112 | // path to deps, relative to the top of the toolchain source |
| 113 | Toolchain_deps []string |
| 114 | // path to deps, relative to module directory |
| 115 | Deps []string |
| 116 | } |
| 117 | |
| 118 | func rustToolchainRustcPrebuiltFactory() android.Module { |
| 119 | module := android.NewPrebuiltBuildTool() |
| 120 | module.AddProperties(&toolchainRustcPrebuiltProperties{}) |
| 121 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { |
| 122 | var toolchainProps *toolchainRustcPrebuiltProperties |
| 123 | for _, p := range ctx.Module().GetProperties() { |
| 124 | toolchainProperties, ok := p.(*toolchainRustcPrebuiltProperties) |
| 125 | if ok { |
| 126 | toolchainProps = toolchainProperties |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if toolchainProps.Toolchain_prebuilt_src == nil { |
| 131 | ctx.PropertyErrorf("toolchain_prebuilt_src", "must set path to rustc prebuilt") |
| 132 | } |
| 133 | |
| 134 | prefix := "linux-x86/" + GetRustPrebuiltVersion(ctx) |
| 135 | deps := make([]string, 0, len(toolchainProps.Toolchain_deps)+len(toolchainProps.Deps)) |
| 136 | for _, d := range toolchainProps.Toolchain_deps { |
| 137 | deps = append(deps, path.Join(prefix, d)) |
| 138 | } |
| 139 | deps = append(deps, toolchainProps.Deps...) |
| 140 | |
| 141 | props := struct { |
| 142 | Src *string |
| 143 | Deps []string |
| 144 | }{ |
| 145 | Src: proptools.StringPtr(path.Join(prefix, *toolchainProps.Toolchain_prebuilt_src)), |
| 146 | Deps: deps, |
| 147 | } |
| 148 | ctx.AppendProperties(&props) |
| 149 | }) |
| 150 | return module |
| 151 | } |