xref: /aosp_15_r20/external/cronet/build/rust/BUILD.gn (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Copyright 2021 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import("//build/config/rust.gni")
6
7if (toolchain_has_rust) {
8  config("edition_2021") {
9    rustflags = [ "--edition=2021" ]
10  }
11
12  config("edition_2018") {
13    rustflags = [ "--edition=2018" ]
14  }
15
16  config("edition_2015") {
17    rustflags = [ "--edition=2015" ]
18  }
19
20  if (enable_cxx) {
21    # The version of cxx under //third_party/rust/chromium_crates_io/vendor.
22    # Update this whenever cxx is rolled. Also update the version number in the
23    # //third_party/rust/cxx/v1/cxx.h header.
24    cxx_version = "1.0.120"
25
26    # The required dependencies for cxx-generated bindings, that must be included
27    # on the C++ side.
28    static_library("cxx_cppdeps") {
29      sources = [
30        "//third_party/rust/chromium_crates_io/vendor/cxx-${cxx_version}/include/cxx.h",
31        "//third_party/rust/chromium_crates_io/vendor/cxx-${cxx_version}/src/cxx.cc",
32
33        # Our version-independent forwarding header, which we patch cxx.cc to
34        # use since we want it to use an absolute path for its include.
35        "//third_party/rust/cxx/v1/cxx.h",
36      ]
37
38      defines = [ "RUST_CXX_NO_EXCEPTIONS" ]
39
40      # We cannot depend on base/base_export.h because base depends on us.
41      if (is_component_build) {
42        if (is_win) {
43          defines += [ "CXX_RS_EXPORT=__declspec(dllexport)" ]
44        } else {
45          defines +=
46              [ "CXX_RS_EXPORT=__attribute__((visibility(\"default\")))" ]
47        }
48      } else {
49        defines += [ "CXX_RS_EXPORT=" ]
50      }
51
52      deps = [
53        # Depending on the C++ bindings side of cxx then requires also depending
54        # on the Rust bindings, since one calls the other. And the Rust bindings
55        # require the Rust standard library.
56        ":cxx_rustdeps",
57      ]
58    }
59
60    group("cxx_rustdeps") {
61      # The required dependencies for cxx-generated bindings, that must be
62      # included on the Rust side.
63      public_deps = [ "//third_party/rust/cxx/v1:lib" ]
64    }
65  }
66}
67
68# Enables code behind #[cfg(test)]. This should only be used for targets where
69# testonly=true.
70config("test") {
71  rustflags = [
72    "--cfg",
73    "test",
74  ]
75}
76
77# TODO(crbug.com/gn/104): GN rust_proc_macro targets are missing this
78# command line flag, for the proc_macro crate which is provided by rustc for
79# compiling proc-macros.
80config("proc_macro_extern") {
81  rustflags = [
82    "--extern",
83    "proc_macro",
84  ]
85}
86
87# Forbids unsafe code in crates with this config.
88config("forbid_unsafe") {
89  rustflags = [ "-Funsafe_code" ]
90}
91
92config("panic_immediate_abort") {
93  visibility = [ "//build/rust/std/rules:*" ]
94  if (is_official_build) {
95    rustflags = [
96      "--cfg",
97      "feature=\"panic_immediate_abort\"",
98    ]
99  }
100}
101
102config("is_gtest_unittests") {
103  rustflags = [
104    "--cfg",
105    "is_gtest_unittests",
106  ]
107}
108