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