summaryrefslogtreecommitdiff
path: root/chromium/build/rust/clanglibs/find_clanglibs.py
blob: 654f41ac85710cc32a85f9a61fdf0d75655395cf (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
#!/usr/bin/env/python3

# Copyright 2022 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Enumerate all clang libraries and generate a .rs file which is built into a
# library that links all the clanglibs into it. Depending on that library
# ensures linking against our clang libraries.
# See BUILD.gn in this directory for more context.

import argparse
import os
import stat
import sys
import shutil
import subprocess
import re

LIB_BASENAME_RE = re.compile(r"lib(.*)\.(?:lib|a)$", re.I)


def main():
  parser = argparse.ArgumentParser("find_clanglibs.py")
  parser.add_argument("--output", help="Path to rsp file", required=True)
  parser.add_argument("--depfile", help="Path to write depfile", required=True)
  parser.add_argument("--clang-libs-dir",
                      help="Where to find clang and LLVM libs",
                      required=True)
  args = parser.parse_args()
  if not os.path.exists(os.path.join(
      args.clang_libs_dir, "libclang.a")) and not os.path.exists(
          os.path.join(args.clang_libs_dir, "libclang.lib")):
    print("Couldn't find libclang.a|lib. Please ensure you set a custom var in "
          "your .gclient file like so:\n"
          "  \"custom_vars\": { \"checkout_clang_libs\": True, }\n"
          "Then run gclient sync; rm "
          "<out dir>/obj/build/rust/clanglibs/clang_libs.rsp.")
    return -1
  with open(args.depfile, 'w') as depfile:
    depfile.write("%s:" % args.output)
    with open(args.output, 'w') as output:
      output.write("//! Generated by build/rust/clanglibs/find_clanglibs.py\n")
      output.write("//! @generated\n")
      for f in os.listdir(args.clang_libs_dir):
        m = LIB_BASENAME_RE.match(f)
        if m:
          basename = m.group(1)
          output.write("#[link(name=\"{}\")]\n".format(basename))
          full_path = os.path.join(args.clang_libs_dir, f)
          depfile.write(" {}\n".format(full_path))
      output.write("#[link(name=\"stdc++\")]\n")
      output.write("#[link(name=\"z\")]\n")
      output.write("extern {}\n")


if __name__ == '__main__':
  sys.exit(main())