summaryrefslogtreecommitdiff
path: root/chromium/build/rust/clanglibs/BUILD.gn
blob: eb30c9890144a9b534b8142387a40a528dd1d8f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Copyright 2022 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Several Rust codegen tools (bindgen, autocxx_gen) link against
# libclang. We prefer to distribute the static libraries that make up
# the conceptual "libclang", but there are quite a few such libraries.
# This script enumerates them.

import("//build/rust/rust_static_library.gni")

_clang_libs_dir = "//third_party/llvm-build/Release+Asserts/lib"

if (toolchain_has_rust) {
  # This requires you to configure your .gclient file with
  # "custom_vars": {
  #   "checkout_clang_libs": True,
  # }
  rust_static_library("clanglibs") {
    crate_root = get_label_info(":find_clanglibs", "target_gen_dir") + "/lib.rs"
    sources = [ crate_root ]
    deps = [ ":find_clanglibs" ]

    # all_dependent_configs not public_configs because these directives
    # need to propagate to the eventual final linking step. Our immediate
    # dependent is probably libclang_sys.rlib, which will only later be
    # linked into an executable.
    all_dependent_configs = [ ":clanglibs_static_config" ]
  }

  action("find_clanglibs") {
    # We need to tell rustc to link such binaries against a large
    # and frequently-varying list of static libraries.
    # gn/ninja, however, require statically determined dependency
    # rules.
    # Our options are:
    # a) hard-code them in a .gn file like this, changing them frequently;
    # b) use exec_script to enumerate them. This will slow down everyone's
    #    'gn' invocations
    # c) use libclang.so instead
    # d) put these files entirely outside the visibility of gn/ninja
    # We choose option (d). We generate a crate which tells rustc to link
    # against these things.
    script = "find_clanglibs.py"
    _generated_crate_root = "$target_gen_dir/lib.rs"
    depfile = "$target_out_dir/find_clanglibs.d"
    args = [
      "--clang-libs-dir",
      rebase_path(_clang_libs_dir),
      "--output",
      rebase_path(_generated_crate_root, root_build_dir),
      "--depfile",
      rebase_path(depfile, root_build_dir),
    ]

    # Always rerun this script when clang rolls, in case the
    # set of libraries has changed. The following script contains
    # a clang revision label, so is guaranteed to change each time
    # clang rolls.
    inputs = [ rebase_path("//tools/clang/scripts/update.py") ]
    outputs = [ _generated_crate_root ]
    visibility = [ ":*" ]
  }

  config("clanglibs_static_config") {
    # Inform dependent rustc invocations where these various libraries
    # are to be found.
    rustflags = [ "-L" + rebase_path(_clang_libs_dir) ]

    # At present, all known Rust codegen tools are based ultimately
    # on bindgen and libclang-sys, so set the environment variable
    # required to inform those build processes where the libraries
    # are.
    rustenv = [ "LIBCLANG_STATIC_PATH=" + rebase_path(_clang_libs_dir) ]
  }
}