3 # finish-swig-Python.sh
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.
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.
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
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.
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.
41 if [ ! "$LLDB_DISABLE_PYTHON" = "1" ] ; then
43 if [ -n "$debug_flag" -a "$debug_flag" = "-debug" ]
50 if [ -n "$makefile_flag" -a "$makefile_flag" = "-m" ]
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],,'`
64 echo "The current OS is $OS_NAME"
65 echo "The Python version is $PYTHON_VERSION"
68 if [ ${OS_NAME} = "Darwin" ]
76 # Determine where to put the files.
78 if [ $MakefileCalled -eq 0 ]
80 # We are being built by Xcode, so all the lldb Python files can go
81 # into the LLDB.framework/Resources/Python subdirectory.
83 if [ ! -d "${TARGET_DIR}/LLDB.framework" ]
85 echo "Error: Unable to find LLDB.framework" >&2
90 echo "Found ${TARGET_DIR}/LLDB.framework."
94 # Make the Python directory in the framework if it doesn't already exist
96 framework_python_dir="${TARGET_DIR}/LLDB.framework/Resources/Python/lldb"
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.
102 if [ -n "${PYTHON_INSTALL_DIR}" ]
104 framework_python_dir=`${PYTHON} -c "from distutils.sysconfig import get_python_lib; print get_python_lib(True, False, \"${PYTHON_INSTALL_DIR}\");"`/lldb
106 framework_python_dir=`${PYTHON} -c "from distutils.sysconfig import get_python_lib; print get_python_lib(True, False);"`/lldb
110 [ -n "${CONFIG_BUILD_DIR}" ] || CONFIG_BUILD_DIR=${framework_python_dir}
113 # Look for the directory in which to put the Python files; if it does not
114 # already exist, attempt to make it.
119 echo "Python files will be put in ${framework_python_dir}"
122 python_dirs="${framework_python_dir}"
124 for python_dir in $python_dirs
126 if [ ! -d "${python_dir}" ]
130 echo "Making directory ${python_dir}"
132 mkdir -p "${python_dir}"
136 echo "${python_dir} already exists."
140 if [ ! -d "${python_dir}" ]
142 echo "Error: Unable to find or create ${python_dir}" >&2
147 # Make the symlink that the script bridge for Python will need in the
148 # Python framework directory
150 if [ ! -L "${framework_python_dir}/_lldb.so" ]
154 echo "Creating symlink for _lldb.so"
156 cd "${framework_python_dir}"
157 if [ $MakefileCalled -eq 0 ]
159 ln -s "../../../LLDB" _lldb.so
161 ln -s "../../../liblldb${SOEXT}" _lldb.so
166 echo "${framework_python_dir}/_lldb.so already exists."
170 # Make symlink for darwin-debug on Darwin
171 if [ ${OS_NAME} = "Darwin" ] && [ $MakefileCalled -ne 0 ]
173 # We are being built by CMake on Darwin
175 if [ ! -L "${framework_python_dir}/darwin-debug" ]
179 echo "Creating symlink for darwin-debug"
181 cd "${framework_python_dir}"
185 echo "${framework_python_dir}/darwin-debug already exists."
190 # Make symlink for lldb-argdumper on any platform
191 if [ $MakefileCalled -ne 0 ]
193 # We are being built by CMake
195 if [ ! -L "${framework_python_dir}/lldb-argdumper" ]
199 echo "Creating symlink for lldb-argdumper"
201 cd "${framework_python_dir}"
202 ln -s "../../../../bin/lldb-argdumper" lldb-argdumper
206 echo "${framework_python_dir}/lldb-argdumper already exists."
211 create_python_package () {
212 package_dir="${framework_python_dir}$1"
214 package_name=`echo $1 | tr '/' '.'`
215 package_name="lldb${package_name}"
217 if [ ! -d "${package_dir}" ]
219 mkdir -p "${package_dir}"
222 for package_file in $package_files
224 if [ -f "${package_file}" ]
226 cp "${package_file}" "${package_dir}"
227 package_file_basename=$(basename "${package_file}")
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}" ]
236 printf "__all__ = [" > "${package_init_file}"
237 python_module_separator=""
238 for package_file in $package_files
240 if [ -f "${package_file}" ]
242 package_file_basename=$(basename "${package_file}")
243 printf "${python_module_separator}\"${package_file_basename%.*}\"" >> "${package_init_file}"
244 python_module_separator=", "
247 echo "]" >> "${package_init_file}"
248 echo "for x in __all__:" >> "${package_init_file}"
249 echo " __import__('${package_name}.'+x)" >> "${package_init_file}"
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"
259 package_files="${SRC_ROOT}/source/Interpreter/embedded_interpreter.py"
260 create_python_package "" "${package_files}"
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}"
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" ""
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}"
282 package_files="${SRC_ROOT}/examples/python/symbolication.py"
283 create_python_package "/utils" "${package_files}"
285 if [ ${OS_NAME} = "Darwin" ]
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}"
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}"
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}" ]
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}"