]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - scripts/Python/finish-swig-Python-LLDB.sh
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / scripts / Python / finish-swig-Python-LLDB.sh
1 #!/bin/sh
2
3 # finish-swig-Python.sh
4 #
5 # For the Python script interpreter (external to liblldb) to be able to import
6 # and use the lldb module, there must be two files, lldb.py and _lldb.so, that
7 # it can find. lldb.py is generated by SWIG at the same time it generates the
8 # C++ file.  _lldb.so is actually a symlink file that points to the
9 # LLDB shared library/framework.
10 #
11 # The Python script interpreter needs to be able to automatically find
12 # these two files. On Darwin systems it searches in the LLDB.framework, as
13 # well as in all the normal Python search paths.  On non-Darwin systems
14 # these files will need to be put someplace where Python will find them.
15 #
16 # This shell script creates the _lldb.so symlink in the appropriate place,
17 # and copies the lldb.py (and embedded_interpreter.py) file to the correct
18 # directory.
19 #
20
21 # SRC_ROOT is the root of the lldb source tree.
22 # TARGET_DIR is where the lldb framework/shared library gets put.
23 # CONFIG_BUILD_DIR is where the build-swig-Python-LLDB.sh  shell script
24 #           put the lldb.py file it was generated from running SWIG.
25 # PYTHON_INSTALL_DIR is where non-Darwin systems want to put the .py and .so
26 #           files so that Python can find them automatically.
27 # debug_flag (optional) determines whether or not this script outputs
28 #           additional information when running.
29
30 SRC_ROOT=$1
31 TARGET_DIR=$2
32 CONFIG_BUILD_DIR=$3
33 PYTHON_INSTALL_DIR=$4
34 debug_flag=$5
35 makefile_flag=$6
36
37 # If we don't want Python, then just do nothing here.
38 # Note, at present iOS doesn't have Python, so if you're building for iOS be sure to
39 # set LLDB_DISABLE_PYTHON to 1.
40
41 if [ ! "$LLDB_DISABLE_PYTHON" = "1" ] ; then
42
43 if [ -n "$debug_flag" -a "$debug_flag" = "-debug" ]
44 then
45     Debug=1
46 else
47     Debug=0
48 fi
49
50 if [ -n "$makefile_flag" -a "$makefile_flag" = "-m" ]
51 then
52     MakefileCalled=1
53 else
54     MakefileCalled=0
55 fi
56
57 OS_NAME=`uname -s`
58 PYTHON=${PYTHON_EXECUTABLE:-/usr/bin/env python}
59 PYTHON_VERSION=`${PYTHON} --version 2>&1 | sed -e 's,Python ,,' -e 's,[.][0-9],,2' -e 's,[a-z][a-z][0-9],,'`
60
61
62 if [ $Debug -eq 1 ]
63 then
64     echo "The current OS is $OS_NAME"
65     echo "The Python version is $PYTHON_VERSION"
66 fi
67
68 if [ ${OS_NAME} = "Darwin" ]
69 then
70     SOEXT=".dylib"
71 else
72     SOEXT=".so"
73 fi
74
75 #
76 #  Determine where to put the files.
77
78 if [ $MakefileCalled -eq 0 ]
79 then
80     # We are being built by Xcode, so all the lldb Python files can go
81     # into the LLDB.framework/Resources/Python subdirectory.
82
83     if [ ! -d "${TARGET_DIR}/LLDB.framework" ]
84     then
85         echo "Error:  Unable to find LLDB.framework" >&2
86         exit 1
87     else
88         if [ $Debug -eq 1 ]
89         then
90             echo "Found ${TARGET_DIR}/LLDB.framework."
91         fi
92     fi
93
94     # Make the Python directory in the framework if it doesn't already exist
95
96     framework_python_dir="${TARGET_DIR}/LLDB.framework/Resources/Python/lldb"
97 else
98     # We are being built by LLVM, so use the PYTHON_INSTALL_DIR argument,
99     # and append the python version directory to the end of it.  Depending on
100     # the system other stuff may need to be put here as well.
101
102     if [ -n "${PYTHON_INSTALL_DIR}" ]
103     then
104         framework_python_dir=`${PYTHON} -c "from distutils.sysconfig import get_python_lib; print get_python_lib(True, False, \"${PYTHON_INSTALL_DIR}\");"`/lldb
105     else
106         framework_python_dir=`${PYTHON} -c "from distutils.sysconfig import get_python_lib; print get_python_lib(True, False);"`/lldb
107     fi
108 fi
109
110 [ -n "${CONFIG_BUILD_DIR}" ] || CONFIG_BUILD_DIR=${framework_python_dir}
111
112 #
113 # Look for the directory in which to put the Python files;  if it does not
114 # already exist, attempt to make it.
115 #
116
117 if [ $Debug -eq 1 ]
118 then
119     echo "Python files will be put in ${framework_python_dir}"
120 fi
121
122 python_dirs="${framework_python_dir}"
123
124 for python_dir in $python_dirs
125 do
126     if [ ! -d "${python_dir}" ]
127     then
128         if [ $Debug -eq 1 ]
129         then
130             echo "Making directory ${python_dir}"
131         fi
132         mkdir -p "${python_dir}"
133     else
134         if [ $Debug -eq 1 ]
135         then
136             echo "${python_dir} already exists."
137         fi
138     fi
139
140     if [ ! -d "${python_dir}" ]
141     then
142         echo "Error: Unable to find or create ${python_dir}" >&2
143         exit 1
144     fi
145 done
146
147 # Make the symlink that the script bridge for Python will need in the
148 # Python framework directory
149
150 if [ ! -L "${framework_python_dir}/_lldb.so" ]
151 then
152     if [ $Debug -eq 1 ]
153     then
154         echo "Creating symlink for _lldb.so"
155     fi
156     cd "${framework_python_dir}"
157     if [ $MakefileCalled -eq 0 ]
158     then
159         ln -s "../../../LLDB" _lldb.so
160         else
161         ln -s "../../../liblldb${SOEXT}" _lldb.so
162     fi
163 else
164     if [ $Debug -eq 1 ]
165     then
166         echo "${framework_python_dir}/_lldb.so already exists."
167     fi
168 fi
169
170 # Make symlink for darwin-debug on Darwin
171 if [ ${OS_NAME} = "Darwin" ] && [ $MakefileCalled -ne 0 ]
172 then
173     # We are being built by CMake on Darwin
174
175     if [ ! -L "${framework_python_dir}/darwin-debug" ]
176     then
177         if [ $Debug -eq 1 ]
178         then
179             echo "Creating symlink for darwin-debug"
180         fi
181         cd "${framework_python_dir}"
182     else
183         if [ $Debug -eq 1 ]
184         then
185             echo "${framework_python_dir}/darwin-debug already exists."
186         fi
187     fi
188 fi
189
190 # Make symlink for lldb-argdumper on any platform
191 if [ $MakefileCalled -ne 0 ]
192 then
193     # We are being built by CMake
194
195     if [ ! -L "${framework_python_dir}/lldb-argdumper" ]
196     then
197         if [ $Debug -eq 1 ]
198         then
199             echo "Creating symlink for lldb-argdumper"
200         fi
201         cd "${framework_python_dir}"
202         ln -s "../../../../bin/lldb-argdumper" lldb-argdumper
203     else
204         if [ $Debug -eq 1 ]
205         then
206             echo "${framework_python_dir}/lldb-argdumper already exists."
207         fi
208     fi
209 fi
210
211 create_python_package () {
212     package_dir="${framework_python_dir}$1"
213     package_files="$2"
214     package_name=`echo $1 | tr '/' '.'`
215     package_name="lldb${package_name}"
216
217     if [ ! -d "${package_dir}" ]
218     then
219         mkdir -p "${package_dir}"
220     fi
221
222     for package_file in $package_files
223     do
224         if [ -f "${package_file}" ]
225         then
226             cp "${package_file}" "${package_dir}"
227             package_file_basename=$(basename "${package_file}")
228         fi
229     done
230
231
232     # Create a packate init file if there wasn't one
233     package_init_file="${package_dir}/__init__.py"
234     if [ ! -f "${package_init_file}" ]
235     then
236         printf "__all__ = [" > "${package_init_file}"
237         python_module_separator=""
238         for package_file in $package_files
239         do
240             if [ -f "${package_file}" ]
241             then
242                 package_file_basename=$(basename "${package_file}")
243                 printf "${python_module_separator}\"${package_file_basename%.*}\"" >> "${package_init_file}"
244                 python_module_separator=", "
245             fi
246         done
247         echo "]" >> "${package_init_file}"
248         echo "for x in __all__:" >> "${package_init_file}"
249         echo "    __import__('${package_name}.'+x)" >> "${package_init_file}"
250     fi
251
252
253 }
254
255 # Copy the lldb.py file into the lldb package directory and rename to __init_.py
256 cp "${CONFIG_BUILD_DIR}/lldb.py" "${framework_python_dir}/__init__.py"
257
258 # lldb
259 package_files="${SRC_ROOT}/source/Interpreter/embedded_interpreter.py"
260 create_python_package "" "${package_files}"
261
262 # lldb/formatters/cpp
263 package_files="${SRC_ROOT}/examples/synthetic/gnu_libstdcpp.py
264 ${SRC_ROOT}/examples/synthetic/libcxx.py"
265 create_python_package "/formatters/cpp" "${package_files}"
266
267 # make an empty __init__.py in lldb/runtime
268 # this is required for Python to recognize lldb.runtime as a valid package
269 # (and hence, lldb.runtime.objc as a valid contained package)
270 create_python_package "/runtime" ""
271
272 # lldb/formatters
273 # having these files copied here ensures that lldb/formatters is a valid package itself
274 package_files="${SRC_ROOT}/examples/summaries/cocoa/cache.py
275 ${SRC_ROOT}/examples/summaries/synth.py
276 ${SRC_ROOT}/examples/summaries/cocoa/metrics.py
277 ${SRC_ROOT}/examples/summaries/cocoa/attrib_fromdict.py
278 ${SRC_ROOT}/examples/summaries/cocoa/Logger.py"
279 create_python_package "/formatters" "${package_files}"
280
281 # lldb/utils
282 package_files="${SRC_ROOT}/examples/python/symbolication.py"
283 create_python_package "/utils" "${package_files}"
284
285 if [ ${OS_NAME} = "Darwin" ]
286 then
287     # lldb/macosx
288     package_files="${SRC_ROOT}/examples/python/crashlog.py
289     ${SRC_ROOT}/examples/darwin/heap_find/heap.py"
290     create_python_package "/macosx" "${package_files}"
291
292     # lldb/diagnose
293     package_files="${SRC_ROOT}/examples/python/diagnose_unwind.py
294     ${SRC_ROOT}/examples/python/diagnose_nsstring.py"
295     create_python_package "/diagnose" "${package_files}"
296
297     # Copy files needed by lldb/macosx/heap.py to build libheap.dylib
298     heap_dir="${framework_python_dir}/macosx/heap"
299     if [ ! -d "${heap_dir}" ]
300     then
301         mkdir -p "${heap_dir}"
302         cp "${SRC_ROOT}/examples/darwin/heap_find/heap/heap_find.cpp" "${heap_dir}"
303         cp "${SRC_ROOT}/examples/darwin/heap_find/heap/Makefile" "${heap_dir}"
304     fi
305 fi
306
307 fi
308
309 exit 0
310