blob: 18b95b34f7dfbd449856ed38c8e8ac3507e5e10f [file] [log] [blame]
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001#!/usr/bin/python
Jack Jansen6116f072004-12-26 23:02:05 +00002"""fixapplepython23 - Fix Apple-installed Python 2.3 (on Mac OS X 10.3)
3
4Python 2.3 (and 2.3.X for X<5) have the problem that building an extension
5for a framework installation may accidentally pick up the framework
6of a newer Python, in stead of the one that was used to build the extension.
7
8This script modifies the Makefile (in .../lib/python2.3/config) to use
9the newer method of linking extensions with "-undefined dynamic_lookup"
10which fixes this problem.
11
12The script will first check all prerequisites, and return a zero exit
13status also when nothing needs to be fixed.
14"""
15import sys
16import os
Ronald Oussoren21600152008-12-30 12:59:02 +000017import platform
Jack Jansen6116f072004-12-26 23:02:05 +000018
19MAKEFILE='/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/Makefile'
Jack Jansen64585982005-01-01 22:33:36 +000020CHANGES=((
21 'LDSHARED=\t$(CC) $(LDFLAGS) -bundle -framework $(PYTHONFRAMEWORK)\n',
22 'LDSHARED=\t$(CC) $(LDFLAGS) -bundle -undefined dynamic_lookup\n'
23 ),(
24 'BLDSHARED=\t$(CC) $(LDFLAGS) -bundle -framework $(PYTHONFRAMEWORK)\n',
25 'BLDSHARED=\t$(CC) $(LDFLAGS) -bundle -undefined dynamic_lookup\n'
26 ),(
27 'CC=\t\tgcc\n',
28 'CC=\t\t/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-gcc\n'
29 ),(
30 'CXX=\t\tc++\n',
31 'CXX=\t\t/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-g++\n'
32))
33
34GCC_SCRIPT='/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-gcc'
35GXX_SCRIPT='/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-g++'
36SCRIPT="""#!/bin/sh
37export MACOSX_DEPLOYMENT_TARGET=10.3
38exec %s "${@}"
39"""
Jack Jansen6116f072004-12-26 23:02:05 +000040
41def findline(lines, start):
42 """return line starting with given string or -1"""
43 for i in range(len(lines)):
44 if lines[i][:len(start)] == start:
45 return i
46 return -1
Tim Peters5a9fb3c2005-01-07 16:01:32 +000047
Jack Jansen6116f072004-12-26 23:02:05 +000048def fix(makefile, do_apply):
49 """Fix the Makefile, if required."""
50 fixed = False
51 lines = open(makefile).readlines()
Tim Peters5a9fb3c2005-01-07 16:01:32 +000052
Jack Jansen64585982005-01-01 22:33:36 +000053 for old, new in CHANGES:
54 i = findline(lines, new)
55 if i >= 0:
56 # Already fixed
57 continue
58 i = findline(lines, old)
59 if i < 0:
Collin Wintere7bf59f2007-08-30 18:39:28 +000060 print('fixapplepython23: Python installation not fixed (appears broken)')
61 print('fixapplepython23: missing line:', old)
Jack Jansen64585982005-01-01 22:33:36 +000062 return 2
63 lines[i] = new
Jack Jansen6116f072004-12-26 23:02:05 +000064 fixed = True
Tim Peters5a9fb3c2005-01-07 16:01:32 +000065
Jack Jansen6116f072004-12-26 23:02:05 +000066 if fixed:
67 if do_apply:
Collin Wintere7bf59f2007-08-30 18:39:28 +000068 print('fixapplepython23: Fix to Apple-installed Python 2.3 applied')
Jack Jansen6116f072004-12-26 23:02:05 +000069 os.rename(makefile, makefile + '~')
70 open(makefile, 'w').writelines(lines)
71 return 0
72 else:
Collin Wintere7bf59f2007-08-30 18:39:28 +000073 print('fixapplepython23: Fix to Apple-installed Python 2.3 should be applied')
Jack Jansen6116f072004-12-26 23:02:05 +000074 return 1
75 else:
Collin Wintere7bf59f2007-08-30 18:39:28 +000076 print('fixapplepython23: No fix needed, appears to have been applied before')
Jack Jansen6116f072004-12-26 23:02:05 +000077 return 0
Tim Peters5a9fb3c2005-01-07 16:01:32 +000078
Jack Jansen64585982005-01-01 22:33:36 +000079def makescript(filename, compiler):
80 """Create a wrapper script for a compiler"""
81 dirname = os.path.split(filename)[0]
82 if not os.access(dirname, os.X_OK):
Georg Brandlc76473d2007-09-03 07:27:49 +000083 os.mkdir(dirname, 0o755)
Jack Jansen64585982005-01-01 22:33:36 +000084 fp = open(filename, 'w')
85 fp.write(SCRIPT % compiler)
86 fp.close()
Georg Brandlc76473d2007-09-03 07:27:49 +000087 os.chmod(filename, 0o755)
Collin Wintere7bf59f2007-08-30 18:39:28 +000088 print('fixapplepython23: Created', filename)
Tim Peters5a9fb3c2005-01-07 16:01:32 +000089
Jack Jansen6116f072004-12-26 23:02:05 +000090def main():
91 # Check for -n option
92 if len(sys.argv) > 1 and sys.argv[1] == '-n':
93 do_apply = False
94 else:
95 do_apply = True
96 # First check OS version
Thomas Wouters0e3f5912006-08-11 14:57:12 +000097 if sys.byteorder == 'little':
98 # All intel macs are fine
Collin Wintere7bf59f2007-08-30 18:39:28 +000099 print("fixapplypython23: no fix is needed on MacOSX on Intel")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000100 sys.exit(0)
101
Ronald Oussoren21600152008-12-30 12:59:02 +0000102 osver = platform.mac_ver()
103 if osver != '10.3' and os.ver < '10.3.':
Collin Wintere7bf59f2007-08-30 18:39:28 +0000104 print('fixapplepython23: no fix needed on MacOSX < 10.3')
Jack Jansen6116f072004-12-26 23:02:05 +0000105 sys.exit(0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000106
Ronald Oussoren21600152008-12-30 12:59:02 +0000107 if osver >= '10.4':
Collin Wintere7bf59f2007-08-30 18:39:28 +0000108 print('fixapplepython23: no fix needed on MacOSX >= 10.4')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000109 sys.exit(0)
110
Jack Jansen6116f072004-12-26 23:02:05 +0000111 # Test that a framework Python is indeed installed
112 if not os.path.exists(MAKEFILE):
Collin Wintere7bf59f2007-08-30 18:39:28 +0000113 print('fixapplepython23: Python framework does not appear to be installed (?), nothing fixed')
Jack Jansen6116f072004-12-26 23:02:05 +0000114 sys.exit(0)
115 # Check that we can actually write the file
116 if do_apply and not os.access(MAKEFILE, os.W_OK):
Collin Wintere7bf59f2007-08-30 18:39:28 +0000117 print('fixapplepython23: No write permission, please run with "sudo"')
Jack Jansen6116f072004-12-26 23:02:05 +0000118 sys.exit(2)
Jack Jansen64585982005-01-01 22:33:36 +0000119 # Create the shell scripts
120 if do_apply:
121 if not os.access(GCC_SCRIPT, os.X_OK):
122 makescript(GCC_SCRIPT, "gcc")
123 if not os.access(GXX_SCRIPT, os.X_OK):
124 makescript(GXX_SCRIPT, "g++")
125 # Finally fix the makefile
Jack Jansen6116f072004-12-26 23:02:05 +0000126 rv = fix(MAKEFILE, do_apply)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000127 #sys.exit(rv)
128 sys.exit(0)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000129
Jack Jansen6116f072004-12-26 23:02:05 +0000130if __name__ == '__main__':
131 main()