]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - scripts/swig_bot_lib/local.py
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / scripts / swig_bot_lib / local.py
1 #!/usr/bin/env python
2
3 """
4 Shared functionality used by `client` and `server` when generating or preparing
5 to generate SWIG on the local machine.
6 """
7
8 # Future imports
9 from __future__ import absolute_import
10 from __future__ import print_function
11
12 # Python modules
13 import argparse
14 import imp
15 import io
16 import logging
17 import os
18 import subprocess
19 import sys
20 import tempfile
21 import zipfile
22
23 # LLDB modules
24 import use_lldb_suite
25
26 # Package imports
27 from lldbsuite.support import fs
28
29
30 class LocalConfig(object):
31     src_root = None
32     target_dir = None
33     languages = None
34     swig_executable = None
35
36
37 def pack_archive(bytes_io, src_root, filters):
38     logging.info("Creating input file package...")
39     zip_file = None
40     try:
41         # It's possible that a custom-built interpreter will not have the
42         # standard zlib module.  If so, we can only store, not compress.  By
43         # try to compress since we usually have a standard Python distribution.
44         zip_file = zipfile.ZipFile(bytes_io, mode='w',
45                                    compression=zipfile.ZIP_DEFLATED)
46     except RuntimeError:
47         zip_file = zipfile.ZipFile(bytes_io, mode='w',
48                                    compression=zipfile.ZIP_STORED)
49     archive_entries = []
50     if filters is not None:
51         def filter_func(t):
52             subfolder = t[0]
53             ext = t[1]
54             full_path = os.path.normpath(os.path.join(src_root, subfolder))
55             candidates = [os.path.normpath(os.path.join(full_path, f))
56                           for f in os.listdir(full_path)]
57             actual = filter(
58                 lambda f: os.path.isfile(f) and os.path.splitext(f)[1] == ext,
59                 candidates)
60             return (subfolder, map(lambda f: os.path.basename(f), actual))
61         archive_entries = map(filter_func, filters)
62     else:
63         for (root, dirs, files) in os.walk(src_root):
64             logging.debug("Adding files {} from directory {} to output package"
65                           .format(files, root))
66             if len(files) > 0:
67                 rel_root = os.path.relpath(root, src_root)
68                 archive_entries.append((rel_root, files))
69
70     archive_entries = list(archive_entries)
71     for entry in archive_entries:
72         subfolder = entry[0]
73         files = list(entry[1])
74         for file in files:
75             rel_path = os.path.normpath(os.path.join(subfolder, file))
76             full_path = os.path.join(src_root, rel_path)
77             logging.info("{} -> {}".format(full_path, rel_path))
78             zip_file.write(full_path, rel_path)
79
80     return zip_file
81
82
83 def unpack_archive(folder, archive_bytes):
84     zip_data = io.BytesIO(archive_bytes)
85     logging.debug("Opening zip archive...")
86     zip_file = zipfile.ZipFile(zip_data, mode='r')
87     zip_file.extractall(folder)
88     zip_file.close()
89
90
91 def generate(options):
92     include_folder = os.path.join(options.src_root, "include")
93     in_file = os.path.join(options.src_root, "scripts", "lldb.swig")
94     include_folder = os.path.normcase(include_folder)
95
96     for lang in options.languages:
97         lang = lang.lower()
98         out_dir = os.path.join(options.target_dir, lang.title())
99         if not os.path.exists(out_dir):
100             os.makedirs(out_dir)
101         out_file = os.path.join(out_dir, "LLDBWrap{}.cpp".format(lang.title()))
102         swig_command = [
103             options.swig_executable,
104             "-c++",
105         ]
106         swig_command.append("-" + lang)
107         if lang == "python":
108             swig_command.append("-threads")
109
110         swig_command.extend([
111             "-I" + include_folder,
112             "-D__STDC_LIMIT_MACROS",
113             "-D__STDC_CONSTANT_MACROS",
114             "-outdir", out_dir,
115             "-o", out_file,
116             in_file
117         ])
118
119         logging.info("generating swig {} bindings into {}"
120                      .format(lang, out_dir))
121         logging.debug("swig command line: {}".format(swig_command))
122         try:
123             # Execute swig
124             swig_output = subprocess.check_output(
125                 swig_command, stderr=subprocess.STDOUT, universal_newlines=True)
126
127             logging.info("swig generation succeeded")
128             if swig_output is not None and len(swig_output) > 0:
129                 logging.info("swig output: %s", swig_output)
130             return (0, swig_output)
131         except subprocess.CalledProcessError as e:
132             logging.error("An error occurred executing swig.  returncode={}"
133                           .format(e.returncode))
134             logging.error(e.output)
135             return (e.returncode, e.output)