Makefile patches from Charles Davis and Daniel Malea (+ one or two tweaks).

git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@167242 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/scripts/Python/build-swig-Python.sh b/scripts/Python/build-swig-Python.sh
index 59394f9..efbc748 100755
--- a/scripts/Python/build-swig-Python.sh
+++ b/scripts/Python/build-swig-Python.sh
@@ -4,22 +4,39 @@
 
 # SRC_ROOT is the root of the lldb source tree.
 # TARGET_DIR is where the lldb framework/shared library gets put.
-# CONFIG_BUILD_DIR is where the build-swig-Python-LLDB.sh  shell script 
+# CONFIG_BUILD_DIR is where the build-swig-Python-LLDB.sh  shell script
 #           put the lldb.py file it was generated from running SWIG.
 # PREFIX is the root directory used to determine where third-party modules
 #         for scripting languages should be installed.
-# debug_flag (optional) determines whether or not this script outputs 
+# debug_flag (optional) determines whether or not this script outputs
 #           additional information when running.
 
 SRC_ROOT=$1
 TARGET_DIR=$2
 CONFIG_BUILD_DIR=$3
 PYTHON_INSTALL_DIR=$4
-debug_flag=$5 
+debug_flag=$5
 SWIG=$6
+makefile_flag=$7
+dependency_flag=$8
 
-os_name=`uname -s`
-if [ "$os_name" = "Darwin" ]
+if [ -n "$makefile_flag" -a "$makefile_flag" = "-m" ]
+then
+    MakefileCalled=1
+    if [ -n "$dependency_flag" -a "$dependency_flag" = "-M" ]
+    then
+        GenerateDependencies=1
+        swig_depend_file="${TARGET_DIR}/LLDBWrapPython.cpp.d"
+        SWIG_DEPEND_OPTIONS="-MMD -MF \"${swig_depend_file}.tmp\""
+    else
+        GenerateDependencies=0
+    fi
+else
+    MakefileCalled=0
+    GenerateDependencies=0
+fi
+
+if [ $MakefileCalled -eq 0 ]
 then
   swig_output_file=${SRC_ROOT}/source/LLDBWrapPython.cpp
 else
@@ -263,20 +280,26 @@
 
 python_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],,'`
 
-if [ "$os_name" = "Darwin" ]
+if [ $MakefileCalled -eq 0 ]
 then
-    framework_python_dir="${TARGET_DIR}/LLDB.framework/Resources/Python"
+    framework_python_dir="${TARGET_DIR}/LLDB.framework/Resources/Python/lldb"
 else
-    framework_python_dir="${PYTHON_INSTALL_DIR}/python${python_version}"
+    if [ -n "${PYTHON_INSTALL_DIR}" ]
+    then
+        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
+    else
+        framework_python_dir=`/usr/bin/env python -c "from distutils.sysconfig import get_python_lib; print get_python_lib(True, False);"`/lldb
+    fi
 fi
 
+[ -n "${CONFIG_BUILD_DIR}" ] || CONFIG_BUILD_DIR=${framework_python_dir}
 
 if [ ! -L "${framework_python_dir}/_lldb.so" ]
 then
     NeedToUpdate=1
 fi
 
-if [ ! -f "${framework_python_dir}/lldb.py" ]
+if [ ! -f "${framework_python_dir}/__init__.py" ]
 then
     NeedToUpdate=1
 fi
@@ -297,7 +320,18 @@
 
 # Build the SWIG C++ wrapper file for Python.
 
-$SWIG -c++ -shadow -python -threads -I"/usr/include" -I"${SRC_ROOT}/include" -I./. -outdir "${CONFIG_BUILD_DIR}" -o "${swig_output_file}" "${swig_input_file}"
+if [ $GenerateDependencies -eq 1 ]
+then
+    if $SWIG -c++ -shadow -python -threads -I"${SRC_ROOT}/include" -I./. -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -MMD -MF "${swig_depend_file}.tmp" -outdir "${CONFIG_BUILD_DIR}" -o "${swig_output_file}" "${swig_input_file}"
+    then
+        mv -f "${swig_depend_file}.tmp" "${swig_depend_file}"
+    else
+        rm -f "${swig_depend_file}.tmp"
+        exit 1
+    fi
+else
+    $SWIG -c++ -shadow -python -threads -I"${SRC_ROOT}/include" -I./. -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -outdir "${CONFIG_BUILD_DIR}" -o "${swig_output_file}" "${swig_input_file}" || exit $?
+fi
 
 # Implement the iterator protocol and/or eq/ne operators for some lldb objects.
 # Append global variable to lldb Python module.
@@ -312,7 +346,12 @@
 
 if [ -f "${current_dir}/edit-swig-python-wrapper-file.py" ]
 then
-    python ${current_dir}/edit-swig-python-wrapper-file.py
+    if [ $MakefileCalled -eq 1 ]
+    then
+        python ${current_dir}/edit-swig-python-wrapper-file.py "${TARGET_DIR}"
+    else
+        python ${current_dir}/edit-swig-python-wrapper-file.py
+    fi
     if [ -f "${swig_output_file}.edited" ]
     then
         mv "${swig_output_file}.edited" ${swig_output_file}
diff --git a/scripts/Python/finish-swig-Python-LLDB.sh b/scripts/Python/finish-swig-Python-LLDB.sh
index 12ffb41..8c1fb4e 100755
--- a/scripts/Python/finish-swig-Python-LLDB.sh
+++ b/scripts/Python/finish-swig-Python-LLDB.sh
@@ -5,10 +5,10 @@
 # For the Python script interpreter (external to liblldb) to be able to import
 # and use the lldb module, there must be two files, lldb.py and _lldb.so, that
 # it can find. lldb.py is generated by SWIG at the same time it generates the
-# C++ file.  _lldb.so is actually a symlink file that points to the 
+# C++ file.  _lldb.so is actually a symlink file that points to the
 # LLDB shared library/framework.
 #
-# The Python script interpreter needs to be able to automatically find 
+# The Python script interpreter needs to be able to automatically find
 # these two files. On Darwin systems it searches in the LLDB.framework, as
 # well as in all the normal Python search paths.  On non-Darwin systems
 # these files will need to be put someplace where Python will find them.
@@ -20,11 +20,11 @@
 
 # SRC_ROOT is the root of the lldb source tree.
 # TARGET_DIR is where the lldb framework/shared library gets put.
-# CONFIG_BUILD_DIR is where the build-swig-Python-LLDB.sh  shell script 
+# CONFIG_BUILD_DIR is where the build-swig-Python-LLDB.sh  shell script
 #           put the lldb.py file it was generated from running SWIG.
 # PYTHON_INSTALL_DIR is where non-Darwin systems want to put the .py and .so
 #           files so that Python can find them automatically.
-# debug_flag (optional) determines whether or not this script outputs 
+# debug_flag (optional) determines whether or not this script outputs
 #           additional information when running.
 
 SRC_ROOT=$1
@@ -32,6 +32,7 @@
 CONFIG_BUILD_DIR=$3
 PYTHON_INSTALL_DIR=$4
 debug_flag=$5
+makefile_flag=$6
 
 # If we don't want Python, then just do nothing here.
 # Note, at present iOS doesn't have Python, so if you're building for iOS be sure to
@@ -46,6 +47,13 @@
     Debug=0
 fi
 
+if [ -n "$makefile_flag" -a "$makefile_flag" = "-m" ]
+then
+    MakefileCalled=1
+else
+    MakefileCalled=0
+fi
+
 OS_NAME=`uname -s`
 PYTHON_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],,'`
 
@@ -56,12 +64,19 @@
     echo "The Python version is $PYTHON_VERSION"
 fi
 
+if [ ${OS_NAME} = "Darwin" ]
+then
+    SOEXT=".dylib"
+else
+    SOEXT=".so"
+fi
+
 #
 #  Determine where to put the files.
 
-if [ ${OS_NAME} = "Darwin" ]
+if [ $MakefileCalled -eq 0 ]
 then
-    # We are on a Darwin system, so all the lldb Python files can go 
+    # We are being built by Xcode, so all the lldb Python files can go
     # into the LLDB.framework/Resources/Python subdirectory.
 
     if [ ! -d "${TARGET_DIR}/LLDB.framework" ]
@@ -79,13 +94,20 @@
 
     framework_python_dir="${TARGET_DIR}/LLDB.framework/Resources/Python/lldb"
 else
-    # We are on a non-Darwin system, so use the PYTHON_INSTALL_DIR argument,
+    # We are being built by LLVM, so use the PYTHON_INSTALL_DIR argument,
     # and append the python version directory to the end of it.  Depending on
     # the system other stuff may need to be put here as well.
 
-    framework_python_dir="${PYTHON_INSTALL_DIR}/python${PYTHON_VERSION}/lldb"
+    if [ -n "${PYTHON_INSTALL_DIR}" ]
+    then
+        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
+    else
+        framework_python_dir=`/usr/bin/env python -c "from distutils.sysconfig import get_python_lib; print get_python_lib(True, False);"`/lldb
+    fi
 fi
 
+[ -n "${CONFIG_BUILD_DIR}" ] || CONFIG_BUILD_DIR=${framework_python_dir}
+
 #
 # Look for the directory in which to put the Python files;  if it does not
 # already exist, attempt to make it.
@@ -130,10 +152,12 @@
     then
         echo "Creating symlink for _lldb.so"
     fi
-    if [ ${OS_NAME} = "Darwin" ]
+    cd "${framework_python_dir}"
+    if [ $MakefileCalled -eq 0 ]
     then
-        cd "${framework_python_dir}"
         ln -s "../../../LLDB" _lldb.so
+    else
+        ln -s "../../../liblldb${SOEXT}" _lldb.so
     fi
 else
     if [ $Debug -eq 1 ]
diff --git a/scripts/Python/modify-python-lldb.py b/scripts/Python/modify-python-lldb.py
index 75f97e7..4345ea2 100644
--- a/scripts/Python/modify-python-lldb.py
+++ b/scripts/Python/modify-python-lldb.py
@@ -386,7 +386,7 @@
                 new_content.add_line(section_iter % d[cls+'-section'])
                 new_content.add_line(compile_unit_iter % d[cls+'-compile-unit'])
                 new_content.add_line(d[cls+'-symbol-in-section'])
-            
+
             # This special purpose iterator is for SBValue only!!!
             if cls == "SBValue":
                 new_content.add_line(linked_list_iter_def)