blob: 2e286a632cceb0e16880bbed89f09e720b54b4bb [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`
Ed Maste97b9dfa2014-04-24 16:18:21 +000058PYTHON=${PYTHON_EXECUTABLE:-/usr/bin/env python}
59PYTHON_VERSION=`${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 +000060
61
Jason Molenda47d114a2012-09-27 21:26:57 +000062if [ $Debug -eq 1 ]
Caroline Ticed6ac3842010-06-16 19:26:52 +000063then
64 echo "The current OS is $OS_NAME"
65 echo "The Python version is $PYTHON_VERSION"
66fi
67
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +000068if [ ${OS_NAME} = "Darwin" ]
69then
70 SOEXT=".dylib"
71else
72 SOEXT=".so"
73fi
74
Caroline Ticed6ac3842010-06-16 19:26:52 +000075#
76# Determine where to put the files.
77
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +000078if [ $MakefileCalled -eq 0 ]
Caroline Ticed6ac3842010-06-16 19:26:52 +000079then
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +000080 # We are being built by Xcode, so all the lldb Python files can go
Caroline Ticed6ac3842010-06-16 19:26:52 +000081 # 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
Jason Molenda47d114a2012-09-27 21:26:57 +000088 if [ $Debug -eq 1 ]
Caroline Ticed6ac3842010-06-16 19:26:52 +000089 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
Greg Clayton6d98f562012-04-25 00:58:03 +000096 framework_python_dir="${TARGET_DIR}/LLDB.framework/Resources/Python/lldb"
Caroline Ticed6ac3842010-06-16 19:26:52 +000097else
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +000098 # We are being built by LLVM, so use the PYTHON_INSTALL_DIR argument,
Caroline Ticed6ac3842010-06-16 19:26:52 +000099 # 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
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +0000102 if [ -n "${PYTHON_INSTALL_DIR}" ]
103 then
Ed Maste97b9dfa2014-04-24 16:18:21 +0000104 framework_python_dir=`${PYTHON} -c "from distutils.sysconfig import get_python_lib; print get_python_lib(True, False, \"${PYTHON_INSTALL_DIR}\");"`/lldb
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +0000105 else
Ed Maste97b9dfa2014-04-24 16:18:21 +0000106 framework_python_dir=`${PYTHON} -c "from distutils.sysconfig import get_python_lib; print get_python_lib(True, False);"`/lldb
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +0000107 fi
Caroline Ticed6ac3842010-06-16 19:26:52 +0000108fi
109
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +0000110[ -n "${CONFIG_BUILD_DIR}" ] || CONFIG_BUILD_DIR=${framework_python_dir}
111
Caroline Ticed6ac3842010-06-16 19:26:52 +0000112#
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
Jason Molenda47d114a2012-09-27 21:26:57 +0000117if [ $Debug -eq 1 ]
Caroline Ticed6ac3842010-06-16 19:26:52 +0000118then
119 echo "Python files will be put in ${framework_python_dir}"
120fi
121
Greg Clayton6d98f562012-04-25 00:58:03 +0000122python_dirs="${framework_python_dir}"
Caroline Ticed6ac3842010-06-16 19:26:52 +0000123
Greg Clayton6d98f562012-04-25 00:58:03 +0000124for python_dir in $python_dirs
125do
126 if [ ! -d "${python_dir}" ]
127 then
Jason Molenda47d114a2012-09-27 21:26:57 +0000128 if [ $Debug -eq 1 ]
Greg Clayton6d98f562012-04-25 00:58:03 +0000129 then
130 echo "Making directory ${python_dir}"
131 fi
132 mkdir -p "${python_dir}"
133 else
Jason Molenda47d114a2012-09-27 21:26:57 +0000134 if [ $Debug -eq 1 ]
Greg Clayton6d98f562012-04-25 00:58:03 +0000135 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
145done
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146
Caroline Ticed6ac3842010-06-16 19:26:52 +0000147# Make the symlink that the script bridge for Python will need in the
148# Python framework directory
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150if [ ! -L "${framework_python_dir}/_lldb.so" ]
151then
Jason Molenda47d114a2012-09-27 21:26:57 +0000152 if [ $Debug -eq 1 ]
Caroline Ticed6ac3842010-06-16 19:26:52 +0000153 then
154 echo "Creating symlink for _lldb.so"
155 fi
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +0000156 cd "${framework_python_dir}"
157 if [ $MakefileCalled -eq 0 ]
Caroline Ticed6ac3842010-06-16 19:26:52 +0000158 then
Greg Clayton6d98f562012-04-25 00:58:03 +0000159 ln -s "../../../LLDB" _lldb.so
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000160 else
Filipe Cabecinhasc281a3b2012-11-01 18:55:16 +0000161 ln -s "../../../liblldb${SOEXT}" _lldb.so
Caroline Ticed6ac3842010-06-16 19:26:52 +0000162 fi
163else
Jason Molenda47d114a2012-09-27 21:26:57 +0000164 if [ $Debug -eq 1 ]
Caroline Ticed6ac3842010-06-16 19:26:52 +0000165 then
166 echo "${framework_python_dir}/_lldb.so already exists."
167 fi
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168fi
169
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170
Filipe Cabecinhas3e9e0812012-09-14 17:09:15 +0000171create_python_package () {
Greg Clayton6d98f562012-04-25 00:58:03 +0000172 package_dir="${framework_python_dir}$1"
173 package_files="$2"
Enrico Granata7d222212012-04-25 17:53:41 +0000174 package_name=`echo $1 | tr '/' '.'`
175 package_name="lldb${package_name}"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176
Greg Clayton6d98f562012-04-25 00:58:03 +0000177 if [ ! -d "${package_dir}" ]
Enrico Granata217f91f2011-08-17 19:07:52 +0000178 then
Greg Clayton6d98f562012-04-25 00:58:03 +0000179 mkdir -p "${package_dir}"
Enrico Granata217f91f2011-08-17 19:07:52 +0000180 fi
Enrico Granata217f91f2011-08-17 19:07:52 +0000181
Greg Clayton6d98f562012-04-25 00:58:03 +0000182 for package_file in $package_files
183 do
184 if [ -f "${package_file}" ]
185 then
186 cp "${package_file}" "${package_dir}"
187 package_file_basename=$(basename "${package_file}")
188 fi
189 done
Enrico Granatac7f87302012-03-12 19:47:17 +0000190
Enrico Granatabac45f62012-01-31 17:01:51 +0000191
Greg Clayton6d98f562012-04-25 00:58:03 +0000192 # Create a packate init file if there wasn't one
193 package_init_file="${package_dir}/__init__.py"
194 if [ ! -f "${package_init_file}" ]
Enrico Granatad3d444f2012-02-23 23:10:03 +0000195 then
Filipe Cabecinhas45f0c662013-01-30 00:48:11 +0000196 printf "__all__ = [" > "${package_init_file}"
Greg Clayton6d98f562012-04-25 00:58:03 +0000197 python_module_separator=""
198 for package_file in $package_files
199 do
200 if [ -f "${package_file}" ]
201 then
202 package_file_basename=$(basename "${package_file}")
Filipe Cabecinhas45f0c662013-01-30 00:48:11 +0000203 printf "${python_module_separator}\"${package_file_basename%.*}\"" >> "${package_init_file}"
Greg Clayton6d98f562012-04-25 00:58:03 +0000204 python_module_separator=", "
205 fi
206 done
207 echo "]" >> "${package_init_file}"
Enrico Granata7d222212012-04-25 17:53:41 +0000208 echo "for x in __all__:" >> "${package_init_file}"
209 echo " __import__('${package_name}.'+x)" >> "${package_init_file}"
Enrico Granatad3d444f2012-02-23 23:10:03 +0000210 fi
Enrico Granatad3d444f2012-02-23 23:10:03 +0000211
Enrico Granatad3d444f2012-02-23 23:10:03 +0000212
Greg Clayton6d98f562012-04-25 00:58:03 +0000213}
Enrico Granatad3d444f2012-02-23 23:10:03 +0000214
Greg Clayton6d98f562012-04-25 00:58:03 +0000215# Copy the lldb.py file into the lldb package directory and rename to __init_.py
216cp "${CONFIG_BUILD_DIR}/lldb.py" "${framework_python_dir}/__init__.py"
Enrico Granatad3d444f2012-02-23 23:10:03 +0000217
Greg Clayton6d98f562012-04-25 00:58:03 +0000218# lldb
219package_files="${SRC_ROOT}/source/Interpreter/embedded_interpreter.py"
220create_python_package "" "${package_files}"
Enrico Granatad3d444f2012-02-23 23:10:03 +0000221
Greg Clayton6d98f562012-04-25 00:58:03 +0000222# lldb/formatters/cpp
223package_files="${SRC_ROOT}/examples/synthetic/gnu_libstdcpp.py
224${SRC_ROOT}/examples/synthetic/libcxx.py"
225create_python_package "/formatters/cpp" "${package_files}"
Enrico Granatad3d444f2012-02-23 23:10:03 +0000226
Enrico Granata5b4ab4c2012-04-25 01:26:37 +0000227# make an empty __init__.py in lldb/runtime
228# this is required for Python to recognize lldb.runtime as a valid package
229# (and hence, lldb.runtime.objc as a valid contained package)
230create_python_package "/runtime" ""
231
Greg Clayton6d98f562012-04-25 00:58:03 +0000232# lldb/formatters
Enrico Granata5b4ab4c2012-04-25 01:26:37 +0000233# having these files copied here ensures that lldb/formatters is a valid package itself
Greg Clayton6d98f562012-04-25 00:58:03 +0000234package_files="${SRC_ROOT}/examples/summaries/cocoa/cache.py
235${SRC_ROOT}/examples/summaries/cocoa/metrics.py
236${SRC_ROOT}/examples/summaries/cocoa/attrib_fromdict.py
237${SRC_ROOT}/examples/summaries/cocoa/Logger.py"
238create_python_package "/formatters" "${package_files}"
Enrico Granatad3d444f2012-02-23 23:10:03 +0000239
Greg Clayton6d98f562012-04-25 00:58:03 +0000240# lldb/utils
241package_files="${SRC_ROOT}/examples/python/symbolication.py"
242create_python_package "/utils" "${package_files}"
Enrico Granatad3d444f2012-02-23 23:10:03 +0000243
Jason Molenda47d114a2012-09-27 21:26:57 +0000244if [ ${OS_NAME} = "Darwin" ]
Greg Claytoned3eee62012-04-25 01:49:50 +0000245then
246 # lldb/macosx
247 package_files="${SRC_ROOT}/examples/python/crashlog.py
Jason Molendaf7305a612013-04-30 03:03:06 +0000248 ${SRC_ROOT}/examples/darwin/heap_find/heap.py"
Greg Claytoned3eee62012-04-25 01:49:50 +0000249 create_python_package "/macosx" "${package_files}"
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000250
Jason Molendaf7305a612013-04-30 03:03:06 +0000251 # lldb/diagnose
Enrico Granataa5c03082013-05-30 23:36:47 +0000252 package_files="${SRC_ROOT}/examples/python/diagnose_unwind.py
253 ${SRC_ROOT}/examples/python/diagnose_nsstring.py"
Jason Molendaf7305a612013-04-30 03:03:06 +0000254 create_python_package "/diagnose" "${package_files}"
255
Greg Claytoned3eee62012-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 Granatad3d444f2012-02-23 23:10:03 +0000265
Greg Claytondce502e2011-11-04 03:34:56 +0000266fi
Greg Claytoned3eee62012-04-25 01:49:50 +0000267
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268exit 0
Caroline Ticed6ac3842010-06-16 19:26:52 +0000269