4 Shared functionality used by `client` and `server` when generating or preparing
5 to generate SWIG on the local machine.
9 from __future__ import absolute_import
10 from __future__ import print_function
27 from lldbsuite.support import fs
30 class LocalConfig(object):
34 swig_executable = None
37 def pack_archive(bytes_io, src_root, filters):
38 logging.info("Creating input file package...")
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)
47 zip_file = zipfile.ZipFile(bytes_io, mode='w',
48 compression=zipfile.ZIP_STORED)
50 if filters is not None:
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)]
58 lambda f: os.path.isfile(f) and os.path.splitext(f)[1] == ext,
60 return (subfolder, map(lambda f: os.path.basename(f), actual))
61 archive_entries = map(filter_func, filters)
63 for (root, dirs, files) in os.walk(src_root):
64 logging.debug("Adding files {} from directory {} to output package"
67 rel_root = os.path.relpath(root, src_root)
68 archive_entries.append((rel_root, files))
70 archive_entries = list(archive_entries)
71 for entry in archive_entries:
73 files = list(entry[1])
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)
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)
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)
96 for lang in options.languages:
98 out_dir = os.path.join(options.target_dir, lang.title())
99 if not os.path.exists(out_dir):
101 out_file = os.path.join(out_dir, "LLDBWrap{}.cpp".format(lang.title()))
103 options.swig_executable,
106 swig_command.append("-" + lang)
108 swig_command.append("-threads")
110 swig_command.extend([
111 "-I" + include_folder,
112 "-D__STDC_LIMIT_MACROS",
113 "-D__STDC_CONSTANT_MACROS",
119 logging.info("generating swig {} bindings into {}"
120 .format(lang, out_dir))
121 logging.debug("swig command line: {}".format(swig_command))
124 swig_output = subprocess.check_output(
125 swig_command, stderr=subprocess.STDOUT, universal_newlines=True)
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)