toolchain_library.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. package rust
  17. import (
  18. "path"
  19. "android/soong/android"
  20. "android/soong/rust/config"
  21. )
  22. // This module is used to compile the rust toolchain libraries
  23. // When RUST_PREBUILTS_VERSION is set, the library will generated
  24. // from the given Rust version.
  25. func init() {
  26. android.RegisterModuleType("rust_toolchain_library",
  27. rustToolchainLibraryFactory)
  28. android.RegisterModuleType("rust_toolchain_library_rlib",
  29. rustToolchainLibraryRlibFactory)
  30. android.RegisterModuleType("rust_toolchain_library_dylib",
  31. rustToolchainLibraryDylibFactory)
  32. }
  33. type toolchainLibraryProperties struct {
  34. // path to the toolchain source, relative to the top of the toolchain source
  35. Toolchain_src *string `android:"arch_variant"`
  36. }
  37. type toolchainLibraryDecorator struct {
  38. *libraryDecorator
  39. Properties toolchainLibraryProperties
  40. }
  41. // rust_toolchain_library produces all rust variants.
  42. func rustToolchainLibraryFactory() android.Module {
  43. module, library := NewRustLibrary(android.HostAndDeviceSupported)
  44. library.BuildOnlyRust()
  45. return initToolchainLibrary(module, library)
  46. }
  47. // rust_toolchain_library_dylib produces a dylib.
  48. func rustToolchainLibraryDylibFactory() android.Module {
  49. module, library := NewRustLibrary(android.HostAndDeviceSupported)
  50. library.BuildOnlyDylib()
  51. return initToolchainLibrary(module, library)
  52. }
  53. // rust_toolchain_library_rlib produces an rlib.
  54. func rustToolchainLibraryRlibFactory() android.Module {
  55. module, library := NewRustLibrary(android.HostAndDeviceSupported)
  56. library.BuildOnlyRlib()
  57. return initToolchainLibrary(module, library)
  58. }
  59. func initToolchainLibrary(module *Module, library *libraryDecorator) android.Module {
  60. toolchainLibrary := &toolchainLibraryDecorator{
  61. libraryDecorator: library,
  62. }
  63. module.compiler = toolchainLibrary
  64. module.AddProperties(&toolchainLibrary.Properties)
  65. android.AddLoadHook(module, rustSetToolchainSource)
  66. return module.Init()
  67. }
  68. func rustSetToolchainSource(ctx android.LoadHookContext) {
  69. if toolchainLib, ok := ctx.Module().(*Module).compiler.(*toolchainLibraryDecorator); ok {
  70. prefix := "linux-x86/" + GetRustPrebuiltVersion(ctx)
  71. newSrcs := []string{path.Join(prefix, android.String(toolchainLib.Properties.Toolchain_src))}
  72. type props struct {
  73. Srcs []string
  74. }
  75. p := &props{}
  76. p.Srcs = newSrcs
  77. ctx.AppendProperties(p)
  78. } else {
  79. ctx.ModuleErrorf("Called rustSetToolchainSource on a non-Rust Module.")
  80. }
  81. }
  82. // GetRustPrebuiltVersion returns the RUST_PREBUILTS_VERSION env var, or the default version if it is not defined.
  83. func GetRustPrebuiltVersion(ctx android.LoadHookContext) string {
  84. return ctx.AConfig().GetenvWithDefault("RUST_PREBUILTS_VERSION", config.RustDefaultVersion)
  85. }