• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2023 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/compute_inputs_for_analyze.gni")
6import("//build/config/rust.gni")
7
8if (compute_inputs_for_analyze) {
9  template("analyze_rust") {
10    _target_name = target_name
11    assert(defined(invoker.crate_root))
12
13    action("${_target_name}_collect_sources") {
14      forward_variables_from(invoker,
15                             "*",
16                             TESTONLY_AND_VISIBILITY + [
17                                   "inputs",
18                                   "script",
19                                   "sources",
20                                   "depfile",
21                                   "outputs",
22                                   "args",
23                                 ])
24      forward_variables_from(invoker, [ "testonly" ])
25
26      script = "//build/rust/collect_rust_sources.py"
27      depfile = "${target_gen_dir}/${target_name}.verify.d"
28      outputs = [ depfile ]
29
30      args = [
31        "--generate-depfile",
32        "${rust_sysroot}/bin/rustc",
33        rebase_path(crate_root, root_build_dir),
34        rebase_path(depfile, root_build_dir),
35        "{{rustflags}}",
36      ]
37    }
38
39    action(_target_name) {
40      forward_variables_from(invoker, [ "testonly" ])
41
42      # Constructs a depfile of all rust sources in the crate.
43      deps = [ ":${_target_name}_collect_sources" ]
44
45      # This target is reached once during `gn gen` and then again during
46      # `gn analyze`.
47      #
48      # 1. When doing `gn gen`, the ':${_target_name}_collect_sources'
49      #    target generates a depfile containing all the rust sources of
50      #    the crate. The exec_script() below runs first, and it produces an
51      #    empty result.
52      # 2. When doing `gn analyze`, the exec_script() reads the depfile that
53      #    was written during `gn gen` and puts each Rust file in the crate
54      #    into `inputs`.
55      depfile_path = []
56      foreach(d, get_target_outputs(":${_target_name}_collect_sources")) {
57        depfile_path += [ rebase_path(d, root_build_dir) ]
58      }
59
60      # Here we read the depfile from `gn gen` when doing `gn analyze`, and
61      # add all the rust files in the crate to `inputs`. This ensures that
62      # analyze considers them as affecting tests that depend on the crate.
63      rust_srcs = exec_script("//build/rust/collect_rust_sources.py",
64                              [ "--read-depfile" ] + depfile_path,
65                              "list lines")
66      inputs = []
67      foreach(s, rust_srcs) {
68        inputs += [ rebase_path(s, "//", root_build_dir) ]
69      }
70      script = "//build/rust/collect_rust_sources.py"
71      args = [
72        "--stamp",
73        rebase_path("${target_gen_dir}/${target_name}.verify.stamp",
74                    root_build_dir),
75      ]
76      outputs = [ "${target_gen_dir}/${target_name}.verify.stamp" ]
77    }
78  }
79}
80