Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | # |
| 2 | # edit-swig-python-wrapper-file.py |
| 3 | # |
| 4 | # This script performs some post-processing editing on the C++ file that |
| 5 | # SWIG generates for python, after running on 'lldb.swig'. In |
Caroline Tice | 5790e06 | 2010-10-28 21:51:20 +0000 | [diff] [blame] | 6 | # particular, on Apple systems we want to include the Python.h file that |
| 7 | # is used in the /System/Library/Frameworks/Python.framework, but on other |
| 8 | # systems we want to include plain <Python.h>. So we need to replace: |
| 9 | # |
| 10 | # #include <Python.h> |
| 11 | # |
| 12 | # with: |
| 13 | # |
| 14 | # #if defined (__APPLE__) |
| 15 | # #include <Python/Python.h> |
| 16 | # #else |
| 17 | # #include <Python.h> |
| 18 | # #endif |
| 19 | # |
| 20 | # That's what this python script does. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | # |
| 22 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | import os |
| 24 | |
Johnny Chen | 8c2e28d | 2011-05-27 21:58:22 +0000 | [diff] [blame] | 25 | include_python = '#include <Python.h>' |
| 26 | include_python_ifdef = '''#if defined (__APPLE__) |
| 27 | #include <Python/Python.h> |
| 28 | #else |
| 29 | #include <Python.h> |
| 30 | #endif |
| 31 | ''' |
| 32 | |
Caroline Tice | 5790e06 | 2010-10-28 21:51:20 +0000 | [diff] [blame] | 33 | input_dir_name = os.environ["SRCROOT"] |
| 34 | full_input_name = input_dir_name + "/source/LLDBWrapPython.cpp" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | full_output_name = full_input_name + ".edited" |
| 36 | |
Johnny Chen | 8c2e28d | 2011-05-27 21:58:22 +0000 | [diff] [blame] | 37 | with open(full_input_name, 'r') as f_in: |
| 38 | with open(full_output_name, 'w') as f_out: |
| 39 | include_python_found = False |
| 40 | for line in f_in: |
| 41 | if not include_python_found: |
| 42 | if line.startswith(include_python): |
| 43 | # Write out the modified lines. |
| 44 | f_out.write(include_python_ifdef) |
| 45 | include_python_found = True |
| 46 | continue |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | |
Johnny Chen | 8c2e28d | 2011-05-27 21:58:22 +0000 | [diff] [blame] | 48 | # Otherwise, copy the line verbatim to the output file. |
| 49 | f_out.write(line) |