|
42 | 42 | # should just happily stuff them into the preprocessor/compiler/linker |
43 | 43 | # options and carry on. |
44 | 44 |
|
| 45 | +def _darwin_compiler_fixup(compiler_so, cc_args): |
| 46 | + """ |
| 47 | + This function will strip '-isysroot PATH' and '-arch ARCH' from the |
| 48 | + compile flag if the user has specified one of them in extra_compile_flags. |
| 49 | +
|
| 50 | + This is needed because '-arch ARCH' adds another architecture to the |
| 51 | + build, without a way to remove an architecture. Furthermore GCC will |
| 52 | + barf if multiple '-isysroot' arguments are present. |
| 53 | + """ |
| 54 | + stripArch = stripSysroot = 0 |
| 55 | + |
| 56 | + compiler_so = list(compiler_so) |
| 57 | + kernel_version = os.uname()[2] # 8.4.3 |
| 58 | + major_version = int(kernel_version.split('.')[0]) |
| 59 | + |
| 60 | + if major_version < 8: |
| 61 | + # OSX before 10.4.0, these don't support -arch and -isysroot at |
| 62 | + # all. |
| 63 | + stripArch = stripSysroot = True |
| 64 | + else: |
| 65 | + stripArch = '-arch' in cc_args |
| 66 | + stripSysroot = '-isysroot' in cc_args |
| 67 | + |
| 68 | + if stripArch: |
| 69 | + while 1: |
| 70 | + try: |
| 71 | + index = compiler_so.index('-arch') |
| 72 | + # Strip this argument and the next one: |
| 73 | + del compiler_so[index:index+2] |
| 74 | + except ValueError: |
| 75 | + break |
| 76 | + |
| 77 | + if stripSysroot: |
| 78 | + try: |
| 79 | + index = compiler_so.index('-isysroot') |
| 80 | + # Strip this argument and the next one: |
| 81 | + del compiler_so[index:index+2] |
| 82 | + except ValueError: |
| 83 | + pass |
| 84 | + |
| 85 | + return compiler_so |
| 86 | + |
| 87 | + |
45 | 88 | class UnixCCompiler(CCompiler): |
46 | 89 |
|
47 | 90 | compiler_type = 'unix' |
@@ -108,8 +151,11 @@ def preprocess(self, source, |
108 | 151 | raise CompileError, msg |
109 | 152 |
|
110 | 153 | def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts): |
| 154 | + compiler_so = self.compiler_so |
| 155 | + if sys.platform == 'darwin': |
| 156 | + compiler_so = _darwin_compiler_fixup(compiler_so, cc_args + extra_postargs) |
111 | 157 | try: |
112 | | - self.spawn(self.compiler_so + cc_args + [src, '-o', obj] + |
| 158 | + self.spawn(compiler_so + cc_args + [src, '-o', obj] + |
113 | 159 | extra_postargs) |
114 | 160 | except DistutilsExecError, msg: |
115 | 161 | raise CompileError, msg |
@@ -173,6 +219,10 @@ def link(self, target_desc, objects, |
173 | 219 | linker = self.linker_so[:] |
174 | 220 | if target_lang == "c++" and self.compiler_cxx: |
175 | 221 | linker[0] = self.compiler_cxx[0] |
| 222 | + |
| 223 | + if sys.platform == 'darwin': |
| 224 | + linker = _darwin_compiler_fixup(linker, ld_args) |
| 225 | + |
176 | 226 | self.spawn(linker + ld_args) |
177 | 227 | except DistutilsExecError, msg: |
178 | 228 | raise LinkError, msg |
|
0 commit comments