blob: 2b2f88520ad07974d042eee26efd691803248071 [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"
Sam Delmerico9333ac12023-09-11 17:18:08 +000021 "path/filepath"
Ivan Lozano45e0e5b2021-11-13 07:42:36 -050022
23 "android/soong/android"
24 "android/soong/rust/config"
Sam Delmerico9333ac12023-09-11 17:18:08 +000025
26 "github.com/google/blueprint/proptools"
Ivan Lozano45e0e5b2021-11-13 07:42:36 -050027)
28
29// This module is used to compile the rust toolchain libraries
30// When RUST_PREBUILTS_VERSION is set, the library will generated
31// from the given Rust version.
32func init() {
33 android.RegisterModuleType("rust_toolchain_library",
34 rustToolchainLibraryFactory)
35 android.RegisterModuleType("rust_toolchain_library_rlib",
36 rustToolchainLibraryRlibFactory)
37 android.RegisterModuleType("rust_toolchain_library_dylib",
38 rustToolchainLibraryDylibFactory)
Sam Delmerico9333ac12023-09-11 17:18:08 +000039 android.RegisterModuleType("rust_toolchain_rustc_prebuilt",
40 rustToolchainRustcPrebuiltFactory)
Ivan Lozano45e0e5b2021-11-13 07:42:36 -050041}
42
43type toolchainLibraryProperties struct {
Wen-yi Chu9c642742023-09-22 03:58:59 +000044 // path to the toolchain source, relative to the top of the toolchain source
45 Toolchain_src *string `android:"arch_variant"`
Ivan Lozano45e0e5b2021-11-13 07:42:36 -050046}
47
48type toolchainLibraryDecorator struct {
49 *libraryDecorator
50 Properties toolchainLibraryProperties
51}
52
53// rust_toolchain_library produces all rust variants.
54func rustToolchainLibraryFactory() android.Module {
55 module, library := NewRustLibrary(android.HostAndDeviceSupported)
56 library.BuildOnlyRust()
57
58 return initToolchainLibrary(module, library)
59}
60
61// rust_toolchain_library_dylib produces a dylib.
62func rustToolchainLibraryDylibFactory() android.Module {
63 module, library := NewRustLibrary(android.HostAndDeviceSupported)
64 library.BuildOnlyDylib()
65
66 return initToolchainLibrary(module, library)
67}
68
69// rust_toolchain_library_rlib produces an rlib.
70func rustToolchainLibraryRlibFactory() android.Module {
71 module, library := NewRustLibrary(android.HostAndDeviceSupported)
72 library.BuildOnlyRlib()
73
74 return initToolchainLibrary(module, library)
75}
76
77func initToolchainLibrary(module *Module, library *libraryDecorator) android.Module {
78 toolchainLibrary := &toolchainLibraryDecorator{
79 libraryDecorator: library,
80 }
81 module.compiler = toolchainLibrary
82 module.AddProperties(&toolchainLibrary.Properties)
83 android.AddLoadHook(module, rustSetToolchainSource)
84
85 return module.Init()
86}
87
88func rustSetToolchainSource(ctx android.LoadHookContext) {
89 if toolchainLib, ok := ctx.Module().(*Module).compiler.(*toolchainLibraryDecorator); ok {
Colin Cross7ba7e822023-09-22 22:02:28 +000090 prefix := filepath.Join(config.HostPrebuiltTag(ctx.Config()), GetRustPrebuiltVersion(ctx))
Wen-yi Chu9c642742023-09-22 03:58:59 +000091 newSrcs := []string{path.Join(prefix, android.String(toolchainLib.Properties.Toolchain_src))}
Ivan Lozano45e0e5b2021-11-13 07:42:36 -050092
93 type props struct {
Wen-yi Chu9c642742023-09-22 03:58:59 +000094 Srcs []string
Ivan Lozano45e0e5b2021-11-13 07:42:36 -050095 }
96 p := &props{}
Wen-yi Chu9c642742023-09-22 03:58:59 +000097 p.Srcs = newSrcs
Ivan Lozano45e0e5b2021-11-13 07:42:36 -050098 ctx.AppendProperties(p)
Wen-yi Chu9c642742023-09-22 03:58:59 +000099
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500100 } else {
101 ctx.ModuleErrorf("Called rustSetToolchainSource on a non-Rust Module.")
102 }
103}
104
105// GetRustPrebuiltVersion returns the RUST_PREBUILTS_VERSION env var, or the default version if it is not defined.
106func GetRustPrebuiltVersion(ctx android.LoadHookContext) string {
107 return ctx.AConfig().GetenvWithDefault("RUST_PREBUILTS_VERSION", config.RustDefaultVersion)
108}
Sam Delmerico9333ac12023-09-11 17:18:08 +0000109
110type toolchainRustcPrebuiltProperties struct {
111 // path to rustc prebuilt, relative to the top of the toolchain source
112 Toolchain_prebuilt_src *string
113 // path to deps, relative to the top of the toolchain source
114 Toolchain_deps []string
115 // path to deps, relative to module directory
116 Deps []string
117}
118
119func rustToolchainRustcPrebuiltFactory() android.Module {
120 module := android.NewPrebuiltBuildTool()
121 module.AddProperties(&toolchainRustcPrebuiltProperties{})
122 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
123 var toolchainProps *toolchainRustcPrebuiltProperties
124 for _, p := range ctx.Module().GetProperties() {
125 toolchainProperties, ok := p.(*toolchainRustcPrebuiltProperties)
126 if ok {
127 toolchainProps = toolchainProperties
128 }
129 }
130
131 if toolchainProps.Toolchain_prebuilt_src == nil {
132 ctx.PropertyErrorf("toolchain_prebuilt_src", "must set path to rustc prebuilt")
133 }
134
135 prefix := filepath.Join(config.HostPrebuiltTag(ctx.Config()), GetRustPrebuiltVersion(ctx))
136 deps := make([]string, 0, len(toolchainProps.Toolchain_deps)+len(toolchainProps.Deps))
137 for _, d := range toolchainProps.Toolchain_deps {
138 deps = append(deps, path.Join(prefix, d))
139 }
140 deps = append(deps, toolchainProps.Deps...)
141
142 props := struct {
143 Src *string
144 Deps []string
145 }{
146 Src: proptools.StringPtr(path.Join(prefix, *toolchainProps.Toolchain_prebuilt_src)),
147 Deps: deps,
148 }
149 ctx.AppendProperties(&props)
150 })
151 return module
152}