blob: 6f0bc387afd39b887b44852c27b118940ab60255 [file] [log] [blame]
Filipe Cabecinhas3e9e0812012-09-14 17:09:15 +00001#!/bin/sh
Chris Lattner30fdc8d2010-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 Ticed6ac3842010-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
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +00008# C++ file. _lldb.so is actually a symlink file that points to the
Caroline Ticed6ac3842010-06-16 19:26:52 +00009# LLDB shared library/framework.
10#
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +000011# The Python script interpreter needs to be able to automatically find
Caroline Ticed6ac3842010-06-16 19:26:52 +000012# 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 Lattner30fdc8d2010-06-08 16:52:24 +000020
Caroline Ticed6ac3842010-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.
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +000023# CONFIG_BUILD_DIR is where the build-swig-Python-LLDB.sh shell script
Caroline Ticed6ac3842010-06-16 19:26:52 +000024# 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.
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +000027# debug_flag (optional) determines whether or not this script outputs
Caroline Ticed6ac3842010-06-16 19:26:52 +000028# additional information when running.
29
30SRC_ROOT=$1
31TARGET_DIR=$2
32CONFIG_BUILD_DIR=$3
33PYTHON_INSTALL_DIR=$4
34debug_flag=$5
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +000035makefile_flag=$6
Caroline Ticed6ac3842010-06-16 19:26:52 +000036
Jim Inghamde221822012-05-26 00:23:52 +000037# 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
Filipe Cabecinhas3e9e0812012-09-14 17:09:15 +000041if [ ! "$LLDB_DISABLE_PYTHON" = "1" ] ; then
Greg Claytondce502e2011-11-04 03:34:56 +000042
Jason Molenda47d114a2012-09-27 21:26:57 +000043if [ -n "$debug_flag" -a "$debug_flag" = "-debug" ]
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044then
Caroline Ticed6ac3842010-06-16 19:26:52 +000045 Debug=1
46else
47 Debug=0
48fi
49
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +000050if [ -n "$makefile_flag" -a "$makefile_flag" = "-m" ]
51then
52 MakefileCalled=1
53else
54 MakefileCalled=0
55fi
56
Caroline Ticed6ac3842010-06-16 19:26:52 +000057OS_NAME=`uname -s`
Filipe Cabecinhas3e9e0812012-09-14 17:09:15 +000058PYTHON_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],,'`
Caroline Ticed6ac3842010-06-16 19:26:52 +000059
60
Jason Molenda47d114a2012-09-27 21:26:57 +000061if [ $Debug -eq 1 ]
Caroline Ticed6ac3842010-06-16 19:26:52 +000062then
63 echo "The current OS is $OS_NAME"
64 echo "The Python version is $PYTHON_VERSION"
65fi
66
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +000067if [ ${OS_NAME} = "Darwin" ]
68then
69 SOEXT=".dylib"
70else
71 SOEXT=".so"
72fi
73
Caroline Ticed6ac3842010-06-16 19:26:52 +000074#
75# Determine where to put the files.
76
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +000077if [ $MakefileCalled -eq 0 ]
Caroline Ticed6ac3842010-06-16 19:26:52 +000078then
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +000079 # We are being built by Xcode, so all the lldb Python files can go
Caroline Ticed6ac3842010-06-16 19:26:52 +000080 # 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
Jason Molenda47d114a2012-09-27 21:26:57 +000087 if [ $Debug -eq 1 ]
Caroline Ticed6ac3842010-06-16 19:26:52 +000088 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
Greg Clayton6d98f562012-04-25 00:58:03 +000095 framework_python_dir="${TARGET_DIR}/LLDB.framework/Resources/Python/lldb"
Caroline Ticed6ac3842010-06-16 19:26:52 +000096else
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +000097 # We are being built by LLVM, so use the PYTHON_INSTALL_DIR argument,
Caroline Ticed6ac3842010-06-16 19:26:52 +000098 # 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
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +0000101 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
Caroline Ticed6ac3842010-06-16 19:26:52 +0000107fi
108
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +0000109[ -n "${CONFIG_BUILD_DIR}" ] || CONFIG_BUILD_DIR=${framework_python_dir}
110
Caroline Ticed6ac3842010-06-16 19:26:52 +0000111#
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
Jason Molenda47d114a2012-09-27 21:26:57 +0000116if [ $Debug -eq 1 ]
Caroline Ticed6ac3842010-06-16 19:26:52 +0000117then
118 echo "Python files will be put in ${framework_python_dir}"
119fi
120
Greg Clayton6d98f562012-04-25 00:58:03 +0000121python_dirs="${framework_python_dir}"
Caroline Ticed6ac3842010-06-16 19:26:52 +0000122
Greg Clayton6d98f562012-04-25 00:58:03 +0000123for python_dir in $python_dirs
124do
125 if [ ! -d "${python_dir}" ]
126 then
Jason Molenda47d114a2012-09-27 21:26:57 +0000127 if [ $Debug -eq 1 ]
Greg Clayton6d98f562012-04-25 00:58:03 +0000128 then
129 echo "Making directory ${python_dir}"
130 fi
131 mkdir -p "${python_dir}"
132 else
Jason Molenda47d114a2012-09-27 21:26:57 +0000133 if [ $Debug -eq 1 ]
Greg Clayton6d98f562012-04-25 00:58:03 +0000134 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000145
Caroline Ticed6ac3842010-06-16 19:26:52 +0000146# Make the symlink that the script bridge for Python will need in the
147# Python framework directory
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149if [ ! -L "${framework_python_dir}/_lldb.so" ]
150then
Jason Molenda47d114a2012-09-27 21:26:57 +0000151 if [ $Debug -eq 1 ]
Caroline Ticed6ac3842010-06-16 19:26:52 +0000152 then
153 echo "Creating symlink for _lldb.so"
154 fi
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +0000155 cd "${framework_python_dir}"
156 if [ $MakefileCalled -eq 0 ]
Caroline Ticed6ac3842010-06-16 19:26:52 +0000157 then
Greg Clayton6d98f562012-04-25 00:58:03 +0000158 ln -s "../../../LLDB" _lldb.so
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +0000159 else
160 ln -s "../../../liblldb${SOEXT}" _lldb.so
Caroline Ticed6ac3842010-06-16 19:26:52 +0000161 fi
162else
Jason Molenda47d114a2012-09-27 21:26:57 +0000163 if [ $Debug -eq 1 ]
Caroline Ticed6ac3842010-06-16 19:26:52 +0000164 then
165 echo "${framework_python_dir}/_lldb.so already exists."
166 fi
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167fi
168
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169
Filipe Cabecinhas3e9e0812012-09-14 17:09:15 +0000170create_python_package () {
Greg Clayton6d98f562012-04-25 00:58:03 +0000171 package_dir="${framework_python_dir}$1"
172 package_files="$2"
Enrico Granata7d222212012-04-25 17:53:41 +0000173 package_name=`echo $1 | tr '/' '.'`
174 package_name="lldb${package_name}"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175
Greg Clayton6d98f562012-04-25 00:58:03 +0000176 if [ ! -d "${package_dir}" ]
Enrico Granata217f91f2011-08-17 19:07:52 +0000177 then
Greg Clayton6d98f562012-04-25 00:58:03 +0000178 mkdir -p "${package_dir}"
Enrico Granata217f91f2011-08-17 19:07:52 +0000179 fi
Enrico Granata217f91f2011-08-17 19:07:52 +0000180
Greg Clayton6d98f562012-04-25 00:58:03 +0000181 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
Enrico Granatac7f87302012-03-12 19:47:17 +0000189
Enrico Granatabac45f62012-01-31 17:01:51 +0000190
Greg Clayton6d98f562012-04-25 00:58:03 +0000191 # Create a packate init file if there wasn't one
192 package_init_file="${package_dir}/__init__.py"
193 if [ ! -f "${package_init_file}" ]
Enrico Granatad3d444f2012-02-23 23:10:03 +0000194 then
Filipe Cabecinhas45f0c662013-01-30 00:48:11 +0000195 printf "__all__ = [" > "${package_init_file}"
Greg Clayton6d98f562012-04-25 00:58:03 +0000196 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}")
Filipe Cabecinhas45f0c662013-01-30 00:48:11 +0000202 printf "${python_module_separator}\"${package_file_basename%.*}\"" >> "${package_init_file}"
Greg Clayton6d98f562012-04-25 00:58:03 +0000203 python_module_separator=", "
204 fi
205 done
206 echo "]" >> "${package_init_file}"
Enrico Granata7d222212012-04-25 17:53:41 +0000207 echo "for x in __all__:" >> "${package_init_file}"
208 echo " __import__('${package_name}.'+x)" >> "${package_init_file}"
Enrico Granatad3d444f2012-02-23 23:10:03 +0000209 fi
Enrico Granatad3d444f2012-02-23 23:10:03 +0000210
Enrico Granatad3d444f2012-02-23 23:10:03 +0000211
Greg Clayton6d98f562012-04-25 00:58:03 +0000212}
Enrico Granatad3d444f2012-02-23 23:10:03 +0000213
Greg Clayton6d98f562012-04-25 00:58:03 +0000214# 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"
Enrico Granatad3d444f2012-02-23 23:10:03 +0000216
Greg Clayton6d98f562012-04-25 00:58:03 +0000217# lldb
218package_files="${SRC_ROOT}/source/Interpreter/embedded_interpreter.py"
219create_python_package "" "${package_files}"
Enrico Granatad3d444f2012-02-23 23:10:03 +0000220
Greg Clayton6d98f562012-04-25 00:58:03 +0000221# 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}"
Enrico Granatad3d444f2012-02-23 23:10:03 +0000225
Enrico Granata5b4ab4c2012-04-25 01:26:37 +0000226# 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
Greg Clayton6d98f562012-04-25 00:58:03 +0000231# lldb/formatters
Enrico Granata5b4ab4c2012-04-25 01:26:37 +0000232# having these files copied here ensures that lldb/formatters is a valid package itself
Greg Clayton6d98f562012-04-25 00:58:03 +0000233package_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}"
Enrico Granatad3d444f2012-02-23 23:10:03 +0000238
Greg Clayton6d98f562012-04-25 00:58:03 +0000239# lldb/utils
240package_files="${SRC_ROOT}/examples/python/symbolication.py"
241create_python_package "/utils" "${package_files}"
Enrico Granatad3d444f2012-02-23 23:10:03 +0000242
Jason Molenda47d114a2012-09-27 21:26:57 +0000243if [ ${OS_NAME} = "Darwin" ]
Greg Claytoned3eee62012-04-25 01:49:50 +0000244then
245 # lldb/macosx
246 package_files="${SRC_ROOT}/examples/python/crashlog.py
Jason Molendaf7305a612013-04-30 03:03:06 +0000247 ${SRC_ROOT}/examples/darwin/heap_find/heap.py"
Greg Claytoned3eee62012-04-25 01:49:50 +0000248 create_python_package "/macosx" "${package_files}"
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000249
Jason Molendaf7305a612013-04-30 03:03:06 +0000250 # lldb/diagnose
Enrico Granataa5c03082013-05-30 23:36:47 +0000251 package_files="${SRC_ROOT}/examples/python/diagnose_unwind.py
252 ${SRC_ROOT}/examples/python/diagnose_nsstring.py"
Jason Molendaf7305a612013-04-30 03:03:06 +0000253 create_python_package "/diagnose" "${package_files}"
254
Greg Claytoned3eee62012-04-25 01:49:50 +0000255 # 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
Enrico Granatad3d444f2012-02-23 23:10:03 +0000264
Greg Claytondce502e2011-11-04 03:34:56 +0000265fi
Greg Claytoned3eee62012-04-25 01:49:50 +0000266
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267exit 0
Caroline Ticed6ac3842010-06-16 19:26:52 +0000268