• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/rust/rust_target.gni")
6
7# Defines a Rust static library which can be used by downstream Rust or C++
8# targets. This is a single Rust compilation unit consisting of potentially
9# multiple .rs files.
10#
11# We term this 'rust_static_library' because it is used most analogously
12# to a C++ 'static_library' in Chromium. Like the C++ one, it can be compiled
13# independently into an intermediate linking target. The output contains the
14# object file(s) of the GN target's sources, and not its dependencies.
15#
16# Parameters
17#
18#   sources
19#     List of source files which this crate is allowed to compile, which is
20#     used to determine the impact of source code changes on other GN targets.
21#     This is not used by the Rust compiler, as it discovers source files by
22#     following `mod` declarations starting at the `crate_root`. The
23#     discovered source files must match this list. (This is not yet enforced,
24#     but will be.)
25#
26#   epoch (optional)
27#     The major version of the library, which is used to differentiate between
28#     multiple versions of the same library name. This includes all leading 0s
29#     and the first non-zero value in the crate's version. This should be left
30#     as the default, which is "0", for first-party code unless there are
31#     multiple versions of a crate present. For third-party code, the version
32#     epoch (matching the directory it is found in) should be specified.
33#
34#     Examples:
35#       1.0.2 => epoch = "1"
36#       4.2.0 => epoch = "4"
37#       0.2.7 => epoch = "0.2"
38#       0.0.3 => epoch = "0.0.3"
39#
40#   edition (optional)
41#     Edition of the Rust language to be used.
42#     Options are "2015", "2018" and "2021". Defaults to "2021".
43#
44#   allow_unsafe (optional)
45#     Set to true to allow unsafe code in this target. Defaults to false.
46#
47#   configs (optional)
48#     A list of config labels (in the GN meaning) applying to this target.
49#
50#   rustflags (optional)
51#     Explicit flags for rustc command line. (Use 'edition' or 'features'
52#     where possible).
53#
54#   deps (optional)
55#     List of GN targets on which this crate depends. These may be Rust
56#     or non-Rust targets.
57#
58#   public_deps (optional)
59#     List of GN targets on which this crate depends, and which are exported
60#     into the dependency list of any crate that depends on it. Dependency
61#     crates that appear in the public API should be included here.
62#
63#   test_deps (optional)
64#     List of GN targets on which this crate's tests depend, in addition
65#     to deps.
66#
67#   is_gtest_unittests (optional)
68#     Should only be set to true for rlibs of gtest unit tests. This ensures
69#     all objects in the rlib are linked into the final target, rather than
70#     pruning dead code, so that the tests themselves are not discarded by the
71#     linker.
72#
73#   mutually_dependent_target (optional)
74#   mutually_dependent_public_deps (optional)
75#     These is for use by the mixed_target() template.
76#
77#     If this Rust code is intrinsically paired with some C/C++ code,
78#     with bidirectional calls between the two, then this would
79#     be a circular dependency. GN does not allow circular dependencies,
80#     (other than for header files per allow_circular_includes_from).
81#     But this is common for a 'component' which has both Rust and C++
82#     code. You should structure things such that the C++ code depends
83#     on the Rust code in the normal way:
84#        static_library("cpp_stuff") {
85#          deps = [ "rust_stuff" ]
86#          # ..
87#        }
88#     but that the Rust target also notes the C++ target using this
89#     'mutually_dependent_target' parameter.
90#        rust_static_library("rust_stuff") {
91#          mutually_dependent_target = "cpp_stuff"
92#          mutually_dependent_public_deps = _cpp_stuff_public_deps
93#          # ..
94#        }
95#
96#     This causes the Rust unit tests, if generated, to depend on the mutually
97#     dependent target, since depending on the Rust code only would be
98#     insufficient. And it allows any C++ bindings generated from the Rust code
99#     to include headers from the mutually_dependent_target by depending on its
100#     public_deps.
101#
102#   build_native_rust_unit_tests (optional)
103#     Builds native unit tests (under #[cfg(test)]) written inside the Rust
104#     crate. This will create a `<name>_unittests` executable in the output
105#     directory when set to true.
106#
107#   unit_test_target (optional)
108#     Overrides the default name for the unit tests target
109#
110#   crate_root (optional)
111#     Location of the crate root.
112#     This defaults to `./src/lib.rs` and should only be changed when
113#     absolutely necessary (such as in the case of generated code).
114#
115#   features (optional)
116#     A list of conditional compilation flags to enable. This can be used
117#     to set features for crates built in-tree which are also published to
118#     crates.io. Each feature in the list will be passed to rustc as
119#     '--cfg feature=XXX'
120#
121#   cxx_bindings (optional)
122#     A list of Rust files which contain #[cxx::bridge] mods and should
123#     therefore have C++ bindings generated. See https://cxx.rs.
124#     This will automatically add appropriate dependencies: there's no
125#     need to depend on the cxx crate or any generated bindings.
126#
127#   visibility (optional)
128#   rustflags (optional)
129#   crate_name (optional)
130#     Per the usual gn meaning for Rust targets.
131#
132#   inputs (optional)
133#     Additional input files needed for compilation (such as `include!`ed files)
134#
135#   test_inputs (optional)
136#     Same as above but for the unit tests target
137#
138# Example of usage:
139#
140#   rust_static_library("foo_bar") {
141#     deps = [
142#       "//boo/public/rust/bar",
143#       "//third_party/rust/crates:argh",
144#       "//third_party/rust/crates:serde",
145#       "//third_party/rust/crates:slab",
146#     ]
147#     sources = [ "src/lib.rs" ]
148#   }
149#
150# This template is intended to serve the same purpose as 'rustc_library'
151# in Fuchsia.
152template("rust_static_library") {
153  exclude_forwards = TESTONLY_AND_VISIBILITY + [ "configs" ]
154  _target_name = target_name
155
156  rust_target(_target_name) {
157    forward_variables_from(invoker, "*", exclude_forwards)
158    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
159    if (defined(invoker.configs)) {
160      library_configs = []
161      library_configs = invoker.configs
162    }
163    target_type = "rust_library"
164  }
165}
166
167set_defaults("rust_static_library") {
168  configs = default_compiler_configs
169}
170