blob: e118f92bec37a018e4077301d4a9f6dd21884704 [file] [log] [blame]
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001//
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
17package rust
18
19import (
20 "path"
21
22 "android/soong/android"
23 "android/soong/rust/config"
Sam Delmericob45c8442023-07-13 10:52:57 -040024
25 "github.com/google/blueprint/proptools"
Ivan Lozano45e0e5b2021-11-13 07:42:36 -050026)
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.
31func 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 Delmericob45c8442023-07-13 10:52:57 -040038 android.RegisterModuleType("rust_toolchain_rustc_prebuilt",
39 rustToolchainRustcPrebuiltFactory)
Ivan Lozano45e0e5b2021-11-13 07:42:36 -050040}
41
42type toolchainLibraryProperties struct {
Sam Delmerico553edff2023-07-13 11:15:37 -040043 // 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 Lozano45e0e5b2021-11-13 07:42:36 -050047}
48
49type toolchainLibraryDecorator struct {
50 *libraryDecorator
51 Properties toolchainLibraryProperties
52}
53
54// rust_toolchain_library produces all rust variants.
55func 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.
63func 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.
71func rustToolchainLibraryRlibFactory() android.Module {
72 module, library := NewRustLibrary(android.HostAndDeviceSupported)
73 library.BuildOnlyRlib()
74
75 return initToolchainLibrary(module, library)
76}
77
78func 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
89func rustSetToolchainSource(ctx android.LoadHookContext) {
90 if toolchainLib, ok := ctx.Module().(*Module).compiler.(*toolchainLibraryDecorator); ok {
91 prefix := "linux-x86/" + GetRustPrebuiltVersion(ctx)
Sam Delmerico553edff2023-07-13 11:15:37 -040092 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 Lozano45e0e5b2021-11-13 07:42:36 -050097
98 type props struct {
Sam Delmerico553edff2023-07-13 11:15:37 -040099 Crate_root *string
100 Srcs []string
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500101 }
102 p := &props{}
Sam Delmerico553edff2023-07-13 11:15:37 -0400103 p.Crate_root = &versionedCrateRoot
104 p.Srcs = versionedSrcs
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500105 ctx.AppendProperties(p)
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500106 } 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.
112func GetRustPrebuiltVersion(ctx android.LoadHookContext) string {
113 return ctx.AConfig().GetenvWithDefault("RUST_PREBUILTS_VERSION", config.RustDefaultVersion)
114}
Sam Delmericob45c8442023-07-13 10:52:57 -0400115
116type 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
125func 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}