blob: d0f3c251a2224ee21156283c3e4a5090829d475a [file] [log] [blame]
Filipe Cabecinhas4bb4f302012-09-14 17:09:15 +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
Filipe Cabecinhasf2b0fef2012-11-01 18:55:16 +00008# C++ file. _lldb.so is actually a symlink file that points to the
Caroline Tice9dbe7172010-06-16 19:26:52 +00009# LLDB shared library/framework.
10#
Filipe Cabecinhasf2b0fef2012-11-01 18:55:16 +000011# The Python script interpreter needs to be able to automatically find
Caroline Tice9dbe7172010-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 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.
Filipe Cabecinhasf2b0fef2012-11-01 18:55:16 +000023# CONFIG_BUILD_DIR is where the build-swig-Python-LLDB.sh shell script
Caroline Tice9dbe7172010-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 Cabecinhasf2b0fef2012-11-01 18:55:16 +000027# debug_flag (optional) determines whether or not this script outputs
Caroline Tice9dbe7172010-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 Cabecinhasf2b0fef2012-11-01 18:55:16 +000035makefile_flag=$6
Caroline Tice9dbe7172010-06-16 19:26:52 +000036
Jim Inghamf4ec2ea2012-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 Cabecinhas4bb4f302012-09-14 17:09:15 +000041if [ ! "$LLDB_DISABLE_PYTHON" = "1" ] ; then
Greg Clayton3e4238d2011-11-04 03:34:56 +000042
Jason Molendae2801e12012-09-27 21:26:57 +000043if [ -n "$debug_flag" -a "$debug_flag" = "-debug" ]
Chris Lattner24943d22010-06-08 16:52:24 +000044then
Caroline Tice9dbe7172010-06-16 19:26:52 +000045 Debug=1
46else
47 Debug=0
48fi
49
Filipe Cabecinhasf2b0fef2012-11-01 18:55:16 +000050if [ -n "$makefile_flag" -a "$makefile_flag" = "-m" ]
51then
52 MakefileCalled=1
53else
54 MakefileCalled=0
55fi
56
Caroline Tice9dbe7172010-06-16 19:26:52 +000057OS_NAME=`uname -s`
Filipe Cabecinhas4bb4f302012-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 Tice9dbe7172010-06-16 19:26:52 +000059
60
Jason Molendae2801e12012-09-27 21:26:57 +000061if [ $Debug -eq 1 ]
Caroline Tice9dbe7172010-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 Cabecinhasf2b0fef2012-11-01 18:55:16 +000067if [ ${OS_NAME} = "Darwin" ]
68then
69 SOEXT=".dylib"
70else
71 SOEXT=".so"
72fi
73
Caroline Tice9dbe7172010-06-16 19:26:52 +000074#
75# Determine where to put the files.
76
Filipe Cabecinhasf2b0fef2012-11-01 18:55:16 +000077if [ $MakefileCalled -eq 0 ]
Caroline Tice9dbe7172010-06-16 19:26:52 +000078then
Filipe Cabecinhasf2b0fef2012-11-01 18:55:16 +000079 # We are being built by Xcode, so all the lldb Python files can go
Caroline Tice9dbe7172010-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 Molendae2801e12012-09-27 21:26:57 +000087 if [ $Debug -eq 1 ]
Caroline Tice9dbe7172010-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 Clayton4e651b12012-04-25 00:58:03 +000095 framework_python_dir="${TARGET_DIR}/LLDB.framework/Resources/Python/lldb"
Caroline Tice9dbe7172010-06-16 19:26:52 +000096else
Filipe Cabecinhasf2b0fef2012-11-01 18:55:16 +000097 # We are being built by LLVM, so use the PYTHON_INSTALL_DIR argument,
Caroline Tice9dbe7172010-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 Cabecinhasf2b0fef2012-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 Tice9dbe7172010-06-16 19:26:52 +0000107fi
108
Filipe Cabecinhasf2b0fef2012-11-01 18:55:16 +0000109[ -n "${CONFIG_BUILD_DIR}" ] || CONFIG_BUILD_DIR=${framework_python_dir}
110
Caroline Tice9dbe7172010-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 Molendae2801e12012-09-27 21:26:57 +0000116if [ $Debug -eq 1 ]
Caroline Tice9dbe7172010-06-16 19:26:52 +0000117then
118 echo "Python files will be put in ${framework_python_dir}"
119fi
120
Greg Clayton4e651b12012-04-25 00:58:03 +0000121python_dirs="${framework_python_dir}"
Caroline Tice9dbe7172010-06-16 19:26:52 +0000122
Greg Clayton4e651b12012-04-25 00:58:03 +0000123for python_dir in $python_dirs
124do
125 if [ ! -d "${python_dir}" ]
126 then
Jason Molendae2801e12012-09-27 21:26:57 +0000127 if [ $Debug -eq 1 ]
Greg Clayton4e651b12012-04-25 00:58:03 +0000128 then
129 echo "Making directory ${python_dir}"
130 fi
131 mkdir -p "${python_dir}"
132 else
Jason Molendae2801e12012-09-27 21:26:57 +0000133 if [ $Debug -eq 1 ]
Greg Clayton4e651b12012-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 Lattner24943d22010-06-08 16:52:24 +0000145
Caroline Tice9dbe7172010-06-16 19:26:52 +0000146# Make the symlink that the script bridge for Python will need in the
147# Python framework directory
Chris Lattner24943d22010-06-08 16:52:24 +0000148
Chris Lattner24943d22010-06-08 16:52:24 +0000149if [ ! -L "${framework_python_dir}/_lldb.so" ]
150then
Jason Molendae2801e12012-09-27 21:26:57 +0000151 if [ $Debug -eq 1 ]
Caroline Tice9dbe7172010-06-16 19:26:52 +0000152 then
153 echo "Creating symlink for _lldb.so"
154 fi
Filipe Cabecinhasf2b0fef2012-11-01 18:55:16 +0000155 cd "${framework_python_dir}"
156 if [ $MakefileCalled -eq 0 ]
Caroline Tice9dbe7172010-06-16 19:26:52 +0000157 then
Greg Clayton4e651b12012-04-25 00:58:03 +0000158 ln -s "../../../LLDB" _lldb.so
Filipe Cabecinhasf2b0fef2012-11-01 18:55:16 +0000159 else
160 ln -s "../../../liblldb${SOEXT}" _lldb.so
Caroline Tice9dbe7172010-06-16 19:26:52 +0000161 fi
162else
Jason Molendae2801e12012-09-27 21:26:57 +0000163 if [ $Debug -eq 1 ]
Caroline Tice9dbe7172010-06-16 19:26:52 +0000164 then
165 echo "${framework_python_dir}/_lldb.so already exists."
166 fi
Chris Lattner24943d22010-06-08 16:52:24 +0000167fi
168
Chris Lattner24943d22010-06-08 16:52:24 +0000169
Filipe Cabecinhas4bb4f302012-09-14 17:09:15 +0000170create_python_package () {
Greg Clayton4e651b12012-04-25 00:58:03 +0000171 package_dir="${framework_python_dir}$1"
172 package_files="$2"
Enrico Granataa5c2ce02012-04-25 17:53:41 +0000173 package_name=`echo $1 | tr '/' '.'`
174 package_name="lldb${package_name}"
Chris Lattner24943d22010-06-08 16:52:24 +0000175
Greg Clayton4e651b12012-04-25 00:58:03 +0000176 if [ ! -d "${package_dir}" ]
Enrico Granata074e3b62011-08-17 19:07:52 +0000177 then
Greg Clayton4e651b12012-04-25 00:58:03 +0000178 mkdir -p "${package_dir}"
Enrico Granata074e3b62011-08-17 19:07:52 +0000179 fi
Enrico Granata074e3b62011-08-17 19:07:52 +0000180
Greg Clayton4e651b12012-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 Granata66205ce2012-03-12 19:47:17 +0000189
Enrico Granatab8dc7332012-01-31 17:01:51 +0000190
Greg Clayton4e651b12012-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 Granata8f84cfb2012-02-23 23:10:03 +0000194 then
Filipe Cabecinhas59d9f112013-01-30 00:48:11 +0000195 printf "__all__ = [" > "${package_init_file}"
Greg Clayton4e651b12012-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 Cabecinhas59d9f112013-01-30 00:48:11 +0000202 printf "${python_module_separator}\"${package_file_basename%.*}\"" >> "${package_init_file}"
Greg Clayton4e651b12012-04-25 00:58:03 +0000203 python_module_separator=", "
204 fi
205 done
206 echo "]" >> "${package_init_file}"
Enrico Granataa5c2ce02012-04-25 17:53:41 +0000207 echo "for x in __all__:" >> "${package_init_file}"
208 echo " __import__('${package_name}.'+x)" >> "${package_init_file}"
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000209 fi
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000210
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000211
Greg Clayton4e651b12012-04-25 00:58:03 +0000212}
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000213
Greg Clayton4e651b12012-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 Granata8f84cfb2012-02-23 23:10:03 +0000216
Greg Clayton4e651b12012-04-25 00:58:03 +0000217# lldb
218package_files="${SRC_ROOT}/source/Interpreter/embedded_interpreter.py"
219create_python_package "" "${package_files}"
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000220
Greg Clayton4e651b12012-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 Granata8f84cfb2012-02-23 23:10:03 +0000225
Greg Clayton4e651b12012-04-25 00:58:03 +0000226# lldb/formatters/objc
Greg Clayton4e651b12012-04-25 00:58:03 +0000227package_files="${SRC_ROOT}/examples/summaries/cocoa/Selector.py
228${SRC_ROOT}/examples/summaries/objc.py
229${SRC_ROOT}/examples/summaries/cocoa/Class.py
230${SRC_ROOT}/examples/summaries/cocoa/CFArray.py
231${SRC_ROOT}/examples/summaries/cocoa/CFBag.py
232${SRC_ROOT}/examples/summaries/cocoa/CFBinaryHeap.py
233${SRC_ROOT}/examples/summaries/cocoa/CFBitVector.py
234${SRC_ROOT}/examples/summaries/cocoa/CFDictionary.py
235${SRC_ROOT}/examples/summaries/cocoa/CFString.py
236${SRC_ROOT}/examples/summaries/cocoa/NSBundle.py
237${SRC_ROOT}/examples/summaries/cocoa/NSData.py
238${SRC_ROOT}/examples/summaries/cocoa/NSDate.py
239${SRC_ROOT}/examples/summaries/cocoa/NSException.py
240${SRC_ROOT}/examples/summaries/cocoa/NSIndexSet.py
241${SRC_ROOT}/examples/summaries/cocoa/NSMachPort.py
242${SRC_ROOT}/examples/summaries/cocoa/NSNotification.py
243${SRC_ROOT}/examples/summaries/cocoa/NSNumber.py
244${SRC_ROOT}/examples/summaries/cocoa/NSSet.py
245${SRC_ROOT}/examples/summaries/cocoa/NSURL.py"
246create_python_package "/formatters/objc" "${package_files}"
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000247
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000248
Enrico Granata65e3dcb2012-04-25 01:26:37 +0000249# make an empty __init__.py in lldb/runtime
250# this is required for Python to recognize lldb.runtime as a valid package
251# (and hence, lldb.runtime.objc as a valid contained package)
252create_python_package "/runtime" ""
253
Greg Clayton4e651b12012-04-25 00:58:03 +0000254# lldb/runtime/objc
255package_files="${SRC_ROOT}/examples/summaries/cocoa/objc_runtime.py"
256create_python_package "/runtime/objc" "${package_files}"
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000257
Greg Clayton4e651b12012-04-25 00:58:03 +0000258# lldb/formatters
Enrico Granata65e3dcb2012-04-25 01:26:37 +0000259# having these files copied here ensures that lldb/formatters is a valid package itself
Greg Clayton4e651b12012-04-25 00:58:03 +0000260package_files="${SRC_ROOT}/examples/summaries/cocoa/cache.py
261${SRC_ROOT}/examples/summaries/cocoa/metrics.py
262${SRC_ROOT}/examples/summaries/cocoa/attrib_fromdict.py
263${SRC_ROOT}/examples/summaries/cocoa/Logger.py"
264create_python_package "/formatters" "${package_files}"
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000265
Greg Clayton4e651b12012-04-25 00:58:03 +0000266# lldb/utils
267package_files="${SRC_ROOT}/examples/python/symbolication.py"
268create_python_package "/utils" "${package_files}"
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000269
Jason Molendae2801e12012-09-27 21:26:57 +0000270if [ ${OS_NAME} = "Darwin" ]
Greg Clayton6f2f0ab2012-04-25 01:49:50 +0000271then
272 # lldb/macosx
273 package_files="${SRC_ROOT}/examples/python/crashlog.py
274 ${SRC_ROOT}/examples/darwin/heap_find/heap.py"
275 create_python_package "/macosx" "${package_files}"
Enrico Granata1328b142012-02-29 03:28:49 +0000276
Greg Clayton6f2f0ab2012-04-25 01:49:50 +0000277 # Copy files needed by lldb/macosx/heap.py to build libheap.dylib
278 heap_dir="${framework_python_dir}/macosx/heap"
279 if [ ! -d "${heap_dir}" ]
280 then
281 mkdir -p "${heap_dir}"
282 cp "${SRC_ROOT}/examples/darwin/heap_find/heap/heap_find.cpp" "${heap_dir}"
283 cp "${SRC_ROOT}/examples/darwin/heap_find/heap/Makefile" "${heap_dir}"
284 fi
285fi
Enrico Granata8f84cfb2012-02-23 23:10:03 +0000286
Greg Clayton3e4238d2011-11-04 03:34:56 +0000287fi
Greg Clayton6f2f0ab2012-04-25 01:49:50 +0000288
Chris Lattner24943d22010-06-08 16:52:24 +0000289exit 0
Caroline Tice9dbe7172010-06-16 19:26:52 +0000290