blob: 2e24091a2db5dd484923cf10d8c80a365c044c22 [file] [log] [blame]
Greg Clayton1dae6f32012-04-25 18:40:20 +00001#! /bin/sh
Chris Lattner24943d22010-06-08 16:52:24 +00002
3# finish-swig-Python.sh
4#
5# For the Python script interpreter (external to liblldb) to be able to import
Caroline Tice9dbe7172010-06-16 19:26:52 +00006# 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#
Chris Lattner24943d22010-06-08 16:52:24 +000020
Caroline Tice9dbe7172010-06-16 19:26:52 +000021# 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
35
Jim Inghamf4ec2ea2012-05-26 00:23:52 +000036# If we don't want Python, then just do nothing here.
37# Note, at present iOS doesn't have Python, so if you're building for iOS be sure to
38# set LLDB_DISABLE_PYTHON to 1.
39
Johnny Chen4ff16d72012-05-24 18:14:18 +000040if [ ! $LLDB_DISABLE_PYTHON = "1" ] ; then
Greg Clayton3e4238d2011-11-04 03:34:56 +000041
Caroline Tice9dbe7172010-06-16 19:26:52 +000042if [ -n "$debug_flag" -a "$debug_flag" == "-debug" ]
Chris Lattner24943d22010-06-08 16:52:24 +000043then
Caroline Tice9dbe7172010-06-16 19:26:52 +000044 Debug=1
45else
46 Debug=0
47fi
48
49OS_NAME=`uname -s`
50PYTHON_VERSION=`/usr/bin/python --version 2>&1 | sed -e 's,Python ,,' -e 's,[.][0-9],,2' -e 's,[a-z][a-z][0-9],,'`
51
52
53if [ $Debug == 1 ]
54then
55 echo "The current OS is $OS_NAME"
56 echo "The Python version is $PYTHON_VERSION"
57fi
58
59#
60# Determine where to put the files.
61
62if [ ${OS_NAME} == "Darwin" ]
63then
64 # We are on a Darwin system, so all the lldb Python files can go
65 # into the LLDB.framework/Resources/Python subdirectory.
66
67 if [ ! -d "${TARGET_DIR}/LLDB.framework" ]
68 then
69 echo "Error: Unable to find LLDB.framework" >&2
70 exit 1
71 else
72 if [ $Debug == 1 ]
73 then
74 echo "Found ${TARGET_DIR}/LLDB.framework."
75 fi
76 fi
77
78 # Make the Python directory in the framework if it doesn't already exist
79
Greg Clayton4e651b12012-04-25 00:58:03 +000080 framework_python_dir="${TARGET_DIR}/LLDB.framework/Resources/Python/lldb"
Caroline Tice9dbe7172010-06-16 19:26:52 +000081else
82 # We are on a non-Darwin system, so use the PYTHON_INSTALL_DIR argument,
83 # and append the python version directory to the end of it. Depending on
84 # the system other stuff may need to be put here as well.
85
Greg Clayton4e651b12012-04-25 00:58:03 +000086 framework_python_dir="${PYTHON_INSTALL_DIR}/python${PYTHON_VERSION}/lldb"
Caroline Tice9dbe7172010-06-16 19:26:52 +000087fi
88
89#
90# Look for the directory in which to put the Python files; if it does not
91# already exist, attempt to make it.
92#
93
94if [ $Debug == 1 ]
95then
96 echo "Python files will be put in ${framework_python_dir}"
97fi
98
Greg Clayton4e651b12012-04-25 00:58:03 +000099python_dirs="${framework_python_dir}"
Caroline Tice9dbe7172010-06-16 19:26:52 +0000100
Greg Clayton4e651b12012-04-25 00:58:03 +0000101for python_dir in $python_dirs
102do
103 if [ ! -d "${python_dir}" ]
104 then
105 if [ $Debug == 1 ]
106 then
107 echo "Making directory ${python_dir}"
108 fi
109 mkdir -p "${python_dir}"
110 else
111 if [ $Debug == 1 ]
112 then
113 echo "${python_dir} already exists."
114 fi
115 fi
116
117 if [ ! -d "${python_dir}" ]
118 then
119 echo "Error: Unable to find or create ${python_dir}" >&2
120 exit 1
121 fi
122done
Chris Lattner24943d22010-06-08 16:52:24 +0000123
Caroline Tice9dbe7172010-06-16 19:26:52 +0000124# Make the symlink that the script bridge for Python will need in the
125# Python framework directory
Chris Lattner24943d22010-06-08 16:52:24 +0000126
Chris Lattner24943d22010-06-08 16:52:24 +0000127if [ ! -L "${framework_python_dir}/_lldb.so" ]
128then
Caroline Tice9dbe7172010-06-16 19:26:52 +0000129 if [ $Debug == 1 ]
130 then
131 echo "Creating symlink for _lldb.so"
132 fi
133 if [ ${OS_NAME} == "Darwin" ]
134 then
135 cd "${framework_python_dir}"
Greg Clayton4e651b12012-04-25 00:58:03 +0000136 ln -s "../../../LLDB" _lldb.so
Caroline Tice9dbe7172010-06-16 19:26:52 +0000137 else
138 cd "${TARGET_DIR}"
Greg Clayton4e651b12012-04-25 00:58:03 +0000139 ln -s "../LLDB" _lldb.so
Caroline Tice9dbe7172010-06-16 19:26:52 +0000140 fi
141else
142 if [ $Debug == 1 ]
143 then
144 echo "${framework_python_dir}/_lldb.so already exists."
145 fi
Chris Lattner24943d22010-06-08 16:52:24 +0000146fi
147
Chris Lattner24943d22010-06-08 16:52:24 +0000148
Greg Clayton4e651b12012-04-25 00:58:03 +0000149function create_python_package {
150 package_dir="${framework_python_dir}$1"
151 package_files="$2"
Enrico Granataa5c2ce02012-04-25 17:53:41 +0000152 package_name=`echo $1 | tr '/' '.'`
153 package_name="lldb${package_name}"
Chris Lattner24943d22010-06-08 16:52:24 +0000154
Greg Clayton4e651b12012-04-25 00:58:03 +0000155 if [ ! -d "${package_dir}" ]
Enrico Granata074e3b62011-08-17 19:07:52 +0000156 then
Greg Clayton4e651b12012-04-25 00:58:03 +0000157 mkdir -p "${package_dir}"
Enrico Granata074e3b62011-08-17 19:07:52 +0000158 fi
Enrico Granata074e3b62011-08-17 19:07:52 +0000159
Greg Clayton4e651b12012-04-25 00:58:03 +0000160 for package_file in $package_files
161 do
162 if [ -f "${package_file}" ]
163 then
164 cp "${package_file}" "${package_dir}"
165 package_file_basename=$(basename "${package_file}")
166 fi
167 done
Enrico Granata66205ce2012-03-12 19:47:17 +0000168
Enrico Granatab8dc7332012-01-31 17:01:51 +0000169
Greg Clayton4e651b12012-04-25 00:58:03 +0000170 # Create a packate init file if there wasn't one
171 package_init_file="${package_dir}/__init__.py"
172 if [ ! -f "${package_init_file}" ]
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000173 then
Greg Clayton4e651b12012-04-25 00:58:03 +0000174 echo -n "__all__ = [" > "${package_init_file}"
175 python_module_separator=""
176 for package_file in $package_files
177 do
178 if [ -f "${package_file}" ]
179 then
180 package_file_basename=$(basename "${package_file}")
181 echo -n "${python_module_separator}\"${package_file_basename%.*}\"" >> "${package_init_file}"
182 python_module_separator=", "
183 fi
184 done
185 echo "]" >> "${package_init_file}"
Enrico Granataa5c2ce02012-04-25 17:53:41 +0000186 echo "for x in __all__:" >> "${package_init_file}"
187 echo " __import__('${package_name}.'+x)" >> "${package_init_file}"
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000188 fi
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000189
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000190
Greg Clayton4e651b12012-04-25 00:58:03 +0000191}
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000192
Greg Clayton4e651b12012-04-25 00:58:03 +0000193# Copy the lldb.py file into the lldb package directory and rename to __init_.py
194cp "${CONFIG_BUILD_DIR}/lldb.py" "${framework_python_dir}/__init__.py"
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000195
Greg Clayton4e651b12012-04-25 00:58:03 +0000196# lldb
197package_files="${SRC_ROOT}/source/Interpreter/embedded_interpreter.py"
198create_python_package "" "${package_files}"
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000199
Greg Clayton4e651b12012-04-25 00:58:03 +0000200# lldb/formatters/cpp
201package_files="${SRC_ROOT}/examples/synthetic/gnu_libstdcpp.py
202${SRC_ROOT}/examples/synthetic/libcxx.py"
203create_python_package "/formatters/cpp" "${package_files}"
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000204
Greg Clayton4e651b12012-04-25 00:58:03 +0000205# lldb/formatters/objc
Greg Clayton4e651b12012-04-25 00:58:03 +0000206package_files="${SRC_ROOT}/examples/summaries/cocoa/Selector.py
207${SRC_ROOT}/examples/summaries/objc.py
208${SRC_ROOT}/examples/summaries/cocoa/Class.py
209${SRC_ROOT}/examples/summaries/cocoa/CFArray.py
210${SRC_ROOT}/examples/summaries/cocoa/CFBag.py
211${SRC_ROOT}/examples/summaries/cocoa/CFBinaryHeap.py
212${SRC_ROOT}/examples/summaries/cocoa/CFBitVector.py
213${SRC_ROOT}/examples/summaries/cocoa/CFDictionary.py
214${SRC_ROOT}/examples/summaries/cocoa/CFString.py
215${SRC_ROOT}/examples/summaries/cocoa/NSBundle.py
216${SRC_ROOT}/examples/summaries/cocoa/NSData.py
217${SRC_ROOT}/examples/summaries/cocoa/NSDate.py
218${SRC_ROOT}/examples/summaries/cocoa/NSException.py
219${SRC_ROOT}/examples/summaries/cocoa/NSIndexSet.py
220${SRC_ROOT}/examples/summaries/cocoa/NSMachPort.py
221${SRC_ROOT}/examples/summaries/cocoa/NSNotification.py
222${SRC_ROOT}/examples/summaries/cocoa/NSNumber.py
223${SRC_ROOT}/examples/summaries/cocoa/NSSet.py
224${SRC_ROOT}/examples/summaries/cocoa/NSURL.py"
225create_python_package "/formatters/objc" "${package_files}"
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000226
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000227
Enrico Granata65e3dcb2012-04-25 01:26:37 +0000228# make an empty __init__.py in lldb/runtime
229# this is required for Python to recognize lldb.runtime as a valid package
230# (and hence, lldb.runtime.objc as a valid contained package)
231create_python_package "/runtime" ""
232
Greg Clayton4e651b12012-04-25 00:58:03 +0000233# lldb/runtime/objc
234package_files="${SRC_ROOT}/examples/summaries/cocoa/objc_runtime.py"
235create_python_package "/runtime/objc" "${package_files}"
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000236
Greg Clayton4e651b12012-04-25 00:58:03 +0000237# lldb/formatters
Enrico Granata65e3dcb2012-04-25 01:26:37 +0000238# having these files copied here ensures that lldb/formatters is a valid package itself
Greg Clayton4e651b12012-04-25 00:58:03 +0000239package_files="${SRC_ROOT}/examples/summaries/cocoa/cache.py
240${SRC_ROOT}/examples/summaries/cocoa/metrics.py
241${SRC_ROOT}/examples/summaries/cocoa/attrib_fromdict.py
242${SRC_ROOT}/examples/summaries/cocoa/Logger.py"
243create_python_package "/formatters" "${package_files}"
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000244
Greg Clayton4e651b12012-04-25 00:58:03 +0000245# lldb/utils
246package_files="${SRC_ROOT}/examples/python/symbolication.py"
247create_python_package "/utils" "${package_files}"
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000248
Greg Clayton6f2f0ab2012-04-25 01:49:50 +0000249if [ ${OS_NAME} == "Darwin" ]
250then
251 # lldb/macosx
252 package_files="${SRC_ROOT}/examples/python/crashlog.py
253 ${SRC_ROOT}/examples/darwin/heap_find/heap.py"
254 create_python_package "/macosx" "${package_files}"
Enrico Granata1328b142012-02-29 03:28:49 +0000255
Greg Clayton6f2f0ab2012-04-25 01:49:50 +0000256 # Copy files needed by lldb/macosx/heap.py to build libheap.dylib
257 heap_dir="${framework_python_dir}/macosx/heap"
258 if [ ! -d "${heap_dir}" ]
259 then
260 mkdir -p "${heap_dir}"
261 cp "${SRC_ROOT}/examples/darwin/heap_find/heap/heap_find.cpp" "${heap_dir}"
262 cp "${SRC_ROOT}/examples/darwin/heap_find/heap/Makefile" "${heap_dir}"
263 fi
264fi
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000265
Greg Clayton3e4238d2011-11-04 03:34:56 +0000266fi
Greg Clayton6f2f0ab2012-04-25 01:49:50 +0000267
Chris Lattner24943d22010-06-08 16:52:24 +0000268exit 0
Caroline Tice9dbe7172010-06-16 19:26:52 +0000269