• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
30SRC_ROOT=$1
31TARGET_DIR=$2
32CONFIG_BUILD_DIR=$3
33PYTHON_INSTALL_DIR=$4
34debug_flag=$5
35makefile_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
41if [ ! "$LLDB_DISABLE_PYTHON" = "1" ] ; then
42
43if [ -n "$debug_flag" -a "$debug_flag" = "-debug" ]
44then
45    Debug=1
46else
47    Debug=0
48fi
49
50if [ -n "$makefile_flag" -a "$makefile_flag" = "-m" ]
51then
52    MakefileCalled=1
53else
54    MakefileCalled=0
55fi
56
57OS_NAME=`uname -s`
58PYTHON_VERSION=`/usr/bin/env python --version 2>&1 | sed -e 's,Python ,,' -e 's,[.][0-9],,2' -e 's,[a-z][a-z][0-9],,'`
59
60
61if [ $Debug -eq 1 ]
62then
63    echo "The current OS is $OS_NAME"
64    echo "The Python version is $PYTHON_VERSION"
65fi
66
67if [ ${OS_NAME} = "Darwin" ]
68then
69    SOEXT=".dylib"
70else
71    SOEXT=".so"
72fi
73
74#
75#  Determine where to put the files.
76
77if [ $MakefileCalled -eq 0 ]
78then
79    # We are being built by Xcode, so all the lldb Python files can go
80    # into the LLDB.framework/Resources/Python subdirectory.
81
82    if [ ! -d "${TARGET_DIR}/LLDB.framework" ]
83    then
84        echo "Error:  Unable to find LLDB.framework" >&2
85        exit 1
86    else
87        if [ $Debug -eq 1 ]
88        then
89            echo "Found ${TARGET_DIR}/LLDB.framework."
90        fi
91    fi
92
93    # Make the Python directory in the framework if it doesn't already exist
94
95    framework_python_dir="${TARGET_DIR}/LLDB.framework/Resources/Python/lldb"
96else
97    # We are being built by LLVM, so use the PYTHON_INSTALL_DIR argument,
98    # and append the python version directory to the end of it.  Depending on
99    # the system other stuff may need to be put here as well.
100
101    if [ -n "${PYTHON_INSTALL_DIR}" ]
102    then
103        framework_python_dir=`/usr/bin/env python -c "from distutils.sysconfig import get_python_lib; print get_python_lib(True, False, \"${PYTHON_INSTALL_DIR}\");"`/lldb
104    else
105        framework_python_dir=`/usr/bin/env python -c "from distutils.sysconfig import get_python_lib; print get_python_lib(True, False);"`/lldb
106    fi
107fi
108
109[ -n "${CONFIG_BUILD_DIR}" ] || CONFIG_BUILD_DIR=${framework_python_dir}
110
111#
112# Look for the directory in which to put the Python files;  if it does not
113# already exist, attempt to make it.
114#
115
116if [ $Debug -eq 1 ]
117then
118    echo "Python files will be put in ${framework_python_dir}"
119fi
120
121python_dirs="${framework_python_dir}"
122
123for python_dir in $python_dirs
124do
125    if [ ! -d "${python_dir}" ]
126    then
127        if [ $Debug -eq 1 ]
128        then
129            echo "Making directory ${python_dir}"
130        fi
131        mkdir -p "${python_dir}"
132    else
133        if [ $Debug -eq 1 ]
134        then
135            echo "${python_dir} already exists."
136        fi
137    fi
138
139    if [ ! -d "${python_dir}" ]
140    then
141        echo "Error: Unable to find or create ${python_dir}" >&2
142        exit 1
143    fi
144done
145
146# Make the symlink that the script bridge for Python will need in the
147# Python framework directory
148
149if [ ! -L "${framework_python_dir}/_lldb.so" ]
150then
151    if [ $Debug -eq 1 ]
152    then
153        echo "Creating symlink for _lldb.so"
154    fi
155    cd "${framework_python_dir}"
156    if [ $MakefileCalled -eq 0 ]
157    then
158        ln -s "../../../LLDB" _lldb.so
159    else
160        ln -s "../../../liblldb${SOEXT}" _lldb.so
161    fi
162else
163    if [ $Debug -eq 1 ]
164    then
165        echo "${framework_python_dir}/_lldb.so already exists."
166    fi
167fi
168
169
170create_python_package () {
171    package_dir="${framework_python_dir}$1"
172    package_files="$2"
173    package_name=`echo $1 | tr '/' '.'`
174    package_name="lldb${package_name}"
175
176    if [ ! -d "${package_dir}" ]
177    then
178        mkdir -p "${package_dir}"
179    fi
180
181    for package_file in $package_files
182    do
183        if [ -f "${package_file}" ]
184        then
185            cp "${package_file}" "${package_dir}"
186            package_file_basename=$(basename "${package_file}")
187        fi
188    done
189
190
191    # Create a packate init file if there wasn't one
192    package_init_file="${package_dir}/__init__.py"
193    if [ ! -f "${package_init_file}" ]
194    then
195        printf "__all__ = [" > "${package_init_file}"
196        python_module_separator=""
197        for package_file in $package_files
198        do
199            if [ -f "${package_file}" ]
200            then
201                package_file_basename=$(basename "${package_file}")
202                printf "${python_module_separator}\"${package_file_basename%.*}\"" >> "${package_init_file}"
203                python_module_separator=", "
204            fi
205        done
206        echo "]" >> "${package_init_file}"
207        echo "for x in __all__:" >> "${package_init_file}"
208        echo "    __import__('${package_name}.'+x)" >> "${package_init_file}"
209    fi
210
211
212}
213
214# Copy the lldb.py file into the lldb package directory and rename to __init_.py
215cp "${CONFIG_BUILD_DIR}/lldb.py" "${framework_python_dir}/__init__.py"
216
217# lldb
218package_files="${SRC_ROOT}/source/Interpreter/embedded_interpreter.py"
219create_python_package "" "${package_files}"
220
221# lldb/formatters/cpp
222package_files="${SRC_ROOT}/examples/synthetic/gnu_libstdcpp.py
223${SRC_ROOT}/examples/synthetic/libcxx.py"
224create_python_package "/formatters/cpp" "${package_files}"
225
226# make an empty __init__.py in lldb/runtime
227# this is required for Python to recognize lldb.runtime as a valid package
228# (and hence, lldb.runtime.objc as a valid contained package)
229create_python_package "/runtime" ""
230
231# lldb/formatters
232# having these files copied here ensures that lldb/formatters is a valid package itself
233package_files="${SRC_ROOT}/examples/summaries/cocoa/cache.py
234${SRC_ROOT}/examples/summaries/cocoa/metrics.py
235${SRC_ROOT}/examples/summaries/cocoa/attrib_fromdict.py
236${SRC_ROOT}/examples/summaries/cocoa/Logger.py"
237create_python_package "/formatters" "${package_files}"
238
239# lldb/utils
240package_files="${SRC_ROOT}/examples/python/symbolication.py"
241create_python_package "/utils" "${package_files}"
242
243if [ ${OS_NAME} = "Darwin" ]
244then
245    # lldb/macosx
246    package_files="${SRC_ROOT}/examples/python/crashlog.py
247    ${SRC_ROOT}/examples/darwin/heap_find/heap.py"
248    create_python_package "/macosx" "${package_files}"
249
250    # lldb/diagnose
251    package_files="${SRC_ROOT}/examples/python/diagnose_unwind.py
252    ${SRC_ROOT}/examples/python/diagnose_nsstring.py"
253    create_python_package "/diagnose" "${package_files}"
254
255    # Copy files needed by lldb/macosx/heap.py to build libheap.dylib
256    heap_dir="${framework_python_dir}/macosx/heap"
257    if [ ! -d "${heap_dir}" ]
258    then
259        mkdir -p "${heap_dir}"
260        cp "${SRC_ROOT}/examples/darwin/heap_find/heap/heap_find.cpp" "${heap_dir}"
261        cp "${SRC_ROOT}/examples/darwin/heap_find/heap/Makefile" "${heap_dir}"
262    fi
263fi
264
265fi
266
267exit 0
268
269