blob: 801c0c271cdb41eb3980271cb51bc2174d9023b0 [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 Delmerico5162ff12023-09-08 16:10:47 +000043 // path to the toolchain source, relative to the top of the toolchain source
44 Toolchain_src *string `android:"arch_variant"`
Ivan Lozano45e0e5b2021-11-13 07:42:36 -050045}
46
47type toolchainLibraryDecorator struct {
48 *libraryDecorator
49 Properties toolchainLibraryProperties
50}
51
52// rust_toolchain_library produces all rust variants.
53func 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.
61func 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.
69func rustToolchainLibraryRlibFactory() android.Module {
70 module, library := NewRustLibrary(android.HostAndDeviceSupported)
71 library.BuildOnlyRlib()
72
73 return initToolchainLibrary(module, library)
74}
75
76func 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
87func rustSetToolchainSource(ctx android.LoadHookContext) {
88 if toolchainLib, ok := ctx.Module().(*Module).compiler.(*toolchainLibraryDecorator); ok {
89 prefix := "linux-x86/" + GetRustPrebuiltVersion(ctx)
Sam Delmerico5162ff12023-09-08 16:10:47 +000090 newSrcs := []string{path.Join(prefix, android.String(toolchainLib.Properties.Toolchain_src))}
Ivan Lozano45e0e5b2021-11-13 07:42:36 -050091
92 type props struct {
Sam Delmerico5162ff12023-09-08 16:10:47 +000093 Srcs []string
Ivan Lozano45e0e5b2021-11-13 07:42:36 -050094 }
95 p := &props{}
Sam Delmerico5162ff12023-09-08 16:10:47 +000096 p.Srcs = newSrcs
Ivan Lozano45e0e5b2021-11-13 07:42:36 -050097 ctx.AppendProperties(p)
Sam Delmerico5162ff12023-09-08 16:10:47 +000098
Ivan Lozano45e0e5b2021-11-13 07:42:36 -050099 } 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.
105func GetRustPrebuiltVersion(ctx android.LoadHookContext) string {
106 return ctx.AConfig().GetenvWithDefault("RUST_PREBUILTS_VERSION", config.RustDefaultVersion)
107}
Sam Delmericob45c8442023-07-13 10:52:57 -0400108
109type 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
118func 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}