summaryrefslogtreecommitdiff
path: root/libstdc++-v3/python/hook.in
diff options
context:
space:
mode:
Diffstat (limited to 'libstdc++-v3/python/hook.in')
-rw-r--r--libstdc++-v3/python/hook.in32
1 files changed, 30 insertions, 2 deletions
diff --git a/libstdc++-v3/python/hook.in b/libstdc++-v3/python/hook.in
index fe7c0722484..f7bf1afb44d 100644
--- a/libstdc++-v3/python/hook.in
+++ b/libstdc++-v3/python/hook.in
@@ -16,9 +16,37 @@
import sys
import gdb
+import os
+import os.path
+
+pythondir = '@pythondir@'
+libdir = '@toolexeclibdir@'
+
+# Update module path. We want to find the relative path from libdir
+# to pythondir, and then we want to apply that relative path to the
+# directory holding the objfile with which this file is associated.
+# This preserves relocatability of the gcc tree.
+
+# Do a simple normalization that removes duplicate separators.
+pythondir = os.path.join (*['/'] + pythondir.split (os.sep))
+libdir = os.path.join (*['/'] + libdir.split (os.sep))
+
+prefix = os.path.commonprefix ([libdir, pythondir])
+# In some bizarre configuration we might have found a match in the
+# middle of a directory name.
+if prefix[-1] != '/':
+ prefix = os.path.dirname (prefix)
+
+# Strip off the prefix.
+pythondir = pythondir[len (prefix):]
+libdir = libdir[len (prefix):]
+
+# Compute the ".."s needed to get from libdir to the prefix.
+dotdots = ('..' + os.sep) * len (libdir.split (os.sep))
+
+objfile = gdb.current_objfile ().filename
+dir = os.path.join (os.path.dirname (objfile), dotdots, pythondir)
-# Update module path.
-dir = '@dir@'
if not dir in sys.path:
sys.path.insert(0, dir)