global.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2019 The Android Open Source Project
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package config
  15. import (
  16. "strings"
  17. "android/soong/android"
  18. _ "android/soong/cc/config"
  19. )
  20. var pctx = android.NewPackageContext("android/soong/rust/config")
  21. var (
  22. RustDefaultVersion = "1.70.0"
  23. RustDefaultBase = "prebuilts/rust/"
  24. DefaultEdition = "2021"
  25. Stdlibs = []string{
  26. "libstd",
  27. }
  28. // Mapping between Soong internal arch types and std::env constants.
  29. // Required as Rust uses aarch64 when Soong uses arm64.
  30. StdEnvArch = map[android.ArchType]string{
  31. android.Arm: "arm",
  32. android.Arm64: "aarch64",
  33. android.X86: "x86",
  34. android.X86_64: "x86_64",
  35. }
  36. GlobalRustFlags = []string{
  37. "-Z stack-protector=strong",
  38. "-Z remap-cwd-prefix=.",
  39. "-C codegen-units=1",
  40. "-C debuginfo=2",
  41. "-C opt-level=3",
  42. "-C relocation-model=pic",
  43. "-C overflow-checks=on",
  44. "-C force-unwind-tables=yes",
  45. // Use v0 mangling to distinguish from C++ symbols
  46. "-C symbol-mangling-version=v0",
  47. "--color always",
  48. "-Zdylib-lto",
  49. }
  50. deviceGlobalRustFlags = []string{
  51. "-C panic=abort",
  52. "-Z link-native-libraries=no",
  53. // Generate additional debug info for AutoFDO
  54. "-Z debug-info-for-profiling",
  55. }
  56. deviceGlobalLinkFlags = []string{
  57. // Prepend the lld flags from cc_config so we stay in sync with cc
  58. "${cc_config.DeviceGlobalLldflags}",
  59. // Override cc's --no-undefined-version to allow rustc's generated alloc functions
  60. "-Wl,--undefined-version",
  61. "-Wl,-Bdynamic",
  62. "-nostdlib",
  63. "-Wl,--pack-dyn-relocs=android+relr",
  64. "-Wl,--use-android-relr-tags",
  65. "-Wl,--no-undefined",
  66. "-B${cc_config.ClangBin}",
  67. }
  68. )
  69. func init() {
  70. pctx.SourcePathVariable("RustDefaultBase", RustDefaultBase)
  71. pctx.VariableConfigMethod("HostPrebuiltTag", func(config android.Config) string {
  72. if config.UseHostMusl() {
  73. return "linux-musl-x86"
  74. } else {
  75. return config.PrebuiltOS()
  76. }
  77. })
  78. pctx.VariableFunc("RustBase", func(ctx android.PackageVarContext) string {
  79. if override := ctx.Config().Getenv("RUST_PREBUILTS_BASE"); override != "" {
  80. return override
  81. }
  82. return "${RustDefaultBase}"
  83. })
  84. pctx.VariableFunc("RustVersion", getRustVersionPctx)
  85. pctx.StaticVariable("RustPath", "${RustBase}/${HostPrebuiltTag}/${RustVersion}")
  86. pctx.StaticVariable("RustBin", "${RustPath}/bin")
  87. pctx.ImportAs("cc_config", "android/soong/cc/config")
  88. pctx.StaticVariable("RustLinker", "${cc_config.ClangBin}/clang++")
  89. pctx.StaticVariable("RustLinkerArgs", "-Wl,--as-needed")
  90. pctx.StaticVariable("DeviceGlobalLinkFlags", strings.Join(deviceGlobalLinkFlags, " "))
  91. }
  92. func getRustVersionPctx(ctx android.PackageVarContext) string {
  93. return GetRustVersion(ctx)
  94. }
  95. func GetRustVersion(ctx android.PathContext) string {
  96. if override := ctx.Config().Getenv("RUST_PREBUILTS_VERSION"); override != "" {
  97. return override
  98. }
  99. return RustDefaultVersion
  100. }