rust.gni 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # Copyright 2021 The Chromium Project. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. import("//build/config/chrome_build.gni")
  5. import("//build/config/compiler/compiler.gni")
  6. import("//build/toolchain/toolchain.gni")
  7. if (is_android) {
  8. import("//build/config/android/config.gni")
  9. }
  10. declare_args() {
  11. # Whether to allow Rust code to be part of the Chromium *build process*.
  12. # This can be used to create Rust test binaries, even if the flag below
  13. # is false.
  14. enable_rust = false
  15. # Use experimental Rust toolchain built in-tree. See //tools/rust. For now,
  16. # only use it for linux targets. The package only has prebuilt libs for linux.
  17. # More targets will be added later.
  18. #
  19. # Ideally this should check `current_os` so that e.g. Android builds will use
  20. # the Android toolchain for target artifacts and the Chromium Rust toolchain
  21. # for host artifacts. Currently there is an std mixup in //build/rust/std that
  22. # prevents this.
  23. #
  24. # TODO(https://crbug.com/1245714): fix std handling and check `current_os`.
  25. use_chromium_rust_toolchain = target_os == "linux" && host_os == "linux"
  26. # The version string returned by rustc -V, if using an alternative toolchain.
  27. rustc_version = ""
  28. # Chromium currently has a Rust toolchain for Android and Linux, but
  29. # if you wish to experiment on more platforms you can use this
  30. # argument to specify an alternative toolchain.
  31. # This should be an absolute path to a directory
  32. # containing a 'bin' directory and others. Commonly
  33. # <home dir>/.rustup/toolchains/nightly-<something>-<something>
  34. rust_sysroot_absolute = ""
  35. # Any extra std rlibs in your Rust toolchain, relative to the standard
  36. # Rust toolchain. Typically used with 'use_unverified_rust_toolchain' = true
  37. added_rust_stdlib_libs = []
  38. # Any removed std rlibs in your Rust toolchain, relative to the standard
  39. # Rust toolchain. Typically used with 'use_unverified_rust_toolchain' = true
  40. removed_rust_stdlib_libs = []
  41. # Use LTO when using rustc to link binaries. Experimental. Currently incompatible
  42. # with the options we use in our C++ toolchain to split LTO units.
  43. # This has no effect on the production of normal Chrome binaries, which are
  44. # linked by clang/lld rather than rustc.
  45. # https://crbug.com/1229419
  46. use_lto_in_rustc_linking = false
  47. # Use goma for Rust builds. Experimental. The only known problem is
  48. # b/193072381, but then again, we don't expect a build speedup before much
  49. # more work is done.
  50. use_goma_rust = false
  51. # Rust code may end up being linked into a final executable by:
  52. # * rustc (which calls lld)
  53. # * our pre-existing C++ linker invocations
  54. # At the moment, this first pipeline is incompatible with the ldflags we use
  55. # for thin LTO, due to some problem in escaping gn rules. There's a further
  56. # problem with -lunwind on Android.
  57. # However, Rust code is still useful if it's contributing to our existing
  58. # C++ linker invocations, so this doesn't disable Rust entirely. It does
  59. # disable Rust unit test executables, so we do need to fix this.
  60. # https://crbug.com/1229423
  61. # NB this may be overridden by individual toolchains
  62. rustc_can_link = !is_android
  63. }
  64. # Set rustc_version to the in-tree toolchain version, if enabled, or otherwise
  65. # the Android toolchain version. If using a custom toolchain it is not changed.
  66. if (enable_rust) {
  67. if (use_chromium_rust_toolchain) {
  68. assert(rustc_version == "",
  69. "Cannot override rustc_version when using in-tree rust build")
  70. rustc_version =
  71. read_file("//third_party/rust-toolchain/VERSION", "trim string")
  72. } else if (rustc_version == "") {
  73. # Android toolchain version.
  74. rustc_version = "rustc 1.62.0-dev"
  75. }
  76. }
  77. # Platform support for "official" toolchains (Android or Chromium)
  78. android_toolchain_supports_platform =
  79. (!is_nacl &&
  80. (is_android && (current_cpu == "arm" || current_cpu == "arm64" ||
  81. current_cpu == "x64" || current_cpu == "x86"))) ||
  82. (is_linux && current_cpu == "x64")
  83. chromium_toolchain_supports_platform =
  84. !is_nacl && is_linux && current_cpu == "x64"
  85. toolchain_has_rust =
  86. enable_rust &&
  87. ((use_chromium_rust_toolchain && chromium_toolchain_supports_platform) ||
  88. (!use_chromium_rust_toolchain && android_toolchain_supports_platform) ||
  89. rust_sysroot_absolute != "")
  90. # We use the Rust linker for building test executables, so we only build them
  91. # if we're able to use the Rust linker. We could use the C++ linker for this
  92. # too, we've just not set up GN to do so at the moment.
  93. can_build_rust_unit_tests = toolchain_has_rust && rustc_can_link
  94. # We want to store rust_sysroot as a source-relative variable for ninja
  95. # portability. In practice if an external toolchain was specified, it might
  96. # be an absolute path, but we'll do our best.
  97. if (enable_rust) {
  98. if (rust_sysroot_absolute != "") {
  99. rust_sysroot = get_path_info(rust_sysroot_absolute, "abspath")
  100. use_unverified_rust_toolchain = true
  101. } else if (use_chromium_rust_toolchain) {
  102. if (host_os != "linux") {
  103. assert(
  104. false,
  105. "Attempt to use Chromium Rust toolchain on an unsupported platform")
  106. }
  107. rust_sysroot = "//third_party/rust-toolchain"
  108. use_unverified_rust_toolchain = false
  109. } else {
  110. if (host_os != "linux") {
  111. assert(false,
  112. "Attempt to use Android Rust toolchain on an unsupported platform")
  113. }
  114. rust_sysroot = "//third_party/android_rust_toolchain/toolchain"
  115. use_unverified_rust_toolchain = false
  116. }
  117. }
  118. # Figure out the Rust target triple (aka 'rust_abi_target')
  119. #
  120. # This is here rather than in the toolchain files because it's used
  121. # also by //build/rust/std to find the Rust standard library.
  122. #
  123. # The list of architectures supported by Rust is here:
  124. # https://doc.rust-lang.org/nightly/rustc/platform-support.html.
  125. # We map Chromium targets to Rust targets comprehensively despite not having
  126. # official support (see '*_toolchain_supports_platform above') to enable
  127. # experimentation with other toolchains.
  128. #
  129. # It's OK if rust_abi_target is blank. That means we're building for the host
  130. # and the host stdlib will be used.
  131. rust_abi_target = ""
  132. if (is_android) {
  133. import("//build/config/android/abi.gni")
  134. rust_abi_target = android_abi_target
  135. if (rust_abi_target == "arm-linux-androideabi") {
  136. # Android clang target specifications mostly match Rust, but this
  137. # is an exception
  138. rust_abi_target = "armv7-linux-androideabi"
  139. }
  140. } else if (is_fuchsia) {
  141. if (current_cpu == "arm64") {
  142. rust_abi_target = "aarch64-fuchsia"
  143. } else if (current_cpu == "x64") {
  144. rust_abi_target = "x86_64-fuchsia"
  145. } else {
  146. assert(false, "Architecture not supported")
  147. }
  148. } else if (is_ios) {
  149. if (current_cpu == "arm64") {
  150. rust_abi_target = "aarch64-apple-ios"
  151. } else if (current_cpu == "arm") {
  152. # There's also an armv7s-apple-ios, which targets a more recent ARMv7
  153. # generation CPU found in later iPhones. We'll go with the older one for
  154. # maximal compatibility. As we come to support all the different platforms
  155. # with Rust, we might want to be more precise here.
  156. rust_abi_target = "armv7-apple-ios"
  157. } else if (current_cpu == "x64") {
  158. rust_abi_target = "x86_64-apple-ios"
  159. } else if (current_cpu == "x86") {
  160. rust_abi_target = "i386-apple-ios"
  161. } else {
  162. assert(false, "Architecture not supported")
  163. }
  164. }
  165. # Arguments for Rust invocation.
  166. # This is common between gcc/clang, Mac and Windows toolchains so specify once,
  167. # here. This is not the complete command-line: toolchains should add -o
  168. # and probably --emit arguments too.
  169. rustc_common_args = "--crate-name {{crate_name}} {{source}} --crate-type {{crate_type}} {{rustflags}}"