Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(10)

Delta Between Two Patch Sets: Lib/distutils/unixccompiler.py

Issue 3871: cross and native build of python for mingw32 with distutils
Left Patch Set: Created 9 years, 6 months ago
Right Patch Set: Created 7 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/distutils/tests/test_cygwinccompiler.py ('k') | Lib/plat-generic/regen » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 """distutils.unixccompiler 1 """distutils.unixccompiler
2 2
3 Contains the UnixCCompiler class, a subclass of CCompiler that handles 3 Contains the UnixCCompiler class, a subclass of CCompiler that handles
4 the "typical" Unix-style command-line C compiler: 4 the "typical" Unix-style command-line C compiler:
5 * macros defined with -Dname[=value] 5 * macros defined with -Dname[=value]
6 * macros undefined with -Uname 6 * macros undefined with -Uname
7 * include search directories specified with -Idir 7 * include search directories specified with -Idir
8 * libraries specified with -lllib 8 * libraries specified with -lllib
9 * library search directories specified with -Ldir 9 * library search directories specified with -Ldir
10 * compile handled by 'cc' (or similar) executable with -c option: 10 * compile handled by 'cc' (or similar) executable with -c option:
11 compiles .c to .o 11 compiles .c to .o
12 * link static library handled by 'ar' command (possibly with 'ranlib') 12 * link static library handled by 'ar' command (possibly with 'ranlib')
13 * link shared library handled by 'cc -shared' 13 * link shared library handled by 'cc -shared'
14 """ 14 """
15 15
16 __revision__ = "$Id$"
17
18 import os, sys, re 16 import os, sys, re
19 17
20 from distutils import sysconfig 18 from distutils import sysconfig
21 from distutils.dep_util import newer 19 from distutils.dep_util import newer
22 from distutils.ccompiler import \ 20 from distutils.ccompiler import \
23 CCompiler, gen_preprocess_options, gen_lib_options 21 CCompiler, gen_preprocess_options, gen_lib_options
24 from distutils.errors import \ 22 from distutils.errors import \
25 DistutilsExecError, CompileError, LibError, LinkError 23 DistutilsExecError, CompileError, LibError, LinkError
26 from distutils import log 24 from distutils import log
27 from distutils.util import msysize 25
26 if sys.platform == 'darwin':
27 import _osx_support
28 28
29 # XXX Things not currently handled: 29 # XXX Things not currently handled:
30 # * optimization/debug/warning flags; we just use whatever's in Python's 30 # * optimization/debug/warning flags; we just use whatever's in Python's
31 # Makefile and live with it. Is this adequate? If not, we might 31 # Makefile and live with it. Is this adequate? If not, we might
32 # have to have a bunch of subclasses GNUCCompiler, SGICCompiler, 32 # have to have a bunch of subclasses GNUCCompiler, SGICCompiler,
33 # SunCCompiler, and I suspect down that road lies madness. 33 # SunCCompiler, and I suspect down that road lies madness.
34 # * even if we don't know a warning flag from an optimization flag, 34 # * even if we don't know a warning flag from an optimization flag,
35 # we need some way for outsiders to feed preprocessor/compiler/linker 35 # we need some way for outsiders to feed preprocessor/compiler/linker
36 # flags in to us -- eg. a sysadmin might want to mandate certain flags 36 # flags in to us -- eg. a sysadmin might want to mandate certain flags
37 # via a site config file, or a user might want to set something for 37 # via a site config file, or a user might want to set something for
38 # compiling this module distribution only via the setup.py command 38 # compiling this module distribution only via the setup.py command
39 # line, whatever. As long as these options come from something on the 39 # line, whatever. As long as these options come from something on the
40 # current system, they can be as system-dependent as they like, and we 40 # current system, they can be as system-dependent as they like, and we
41 # should just happily stuff them into the preprocessor/compiler/linker 41 # should just happily stuff them into the preprocessor/compiler/linker
42 # options and carry on. 42 # options and carry on.
43 43
44 def _darwin_compiler_fixup(compiler_so, cc_args):
45 """
46 This function will strip '-isysroot PATH' and '-arch ARCH' from the
47 compile flags if the user has specified one them in extra_compile_flags.
48
49 This is needed because '-arch ARCH' adds another architecture to the
50 build, without a way to remove an architecture. Furthermore GCC will
51 barf if multiple '-isysroot' arguments are present.
52 """
53 stripArch = stripSysroot = False
54
55 compiler_so = list(compiler_so)
56 kernel_version = os.uname()[2] # 8.4.3
57 major_version = int(kernel_version.split('.')[0])
58
59 if major_version < 8:
60 # OSX before 10.4.0, these don't support -arch and -isysroot at
61 # all.
62 stripArch = stripSysroot = True
63 else:
64 stripArch = '-arch' in cc_args
65 stripSysroot = '-isysroot' in cc_args
66
67 if stripArch or 'ARCHFLAGS' in os.environ:
68 while True:
69 try:
70 index = compiler_so.index('-arch')
71 # Strip this argument and the next one:
72 del compiler_so[index:index+2]
73 except ValueError:
74 break
75
76 if 'ARCHFLAGS' in os.environ and not stripArch:
77 # User specified different -arch flags in the environ,
78 # see also distutils.sysconfig
79 compiler_so = compiler_so + os.environ['ARCHFLAGS'].split()
80
81 if stripSysroot:
82 try:
83 index = compiler_so.index('-isysroot')
84 # Strip this argument and the next one:
85 del compiler_so[index:index+2]
86 except ValueError:
87 pass
88
89 # Check if the SDK that is used during compilation actually exists,
90 # the universal build requires the usage of a universal SDK and not all
91 # users have that installed by default.
92 sysroot = None
93 if '-isysroot' in cc_args:
94 idx = cc_args.index('-isysroot')
95 sysroot = cc_args[idx+1]
96 elif '-isysroot' in compiler_so:
97 idx = compiler_so.index('-isysroot')
98 sysroot = compiler_so[idx+1]
99
100 if sysroot and not os.path.isdir(sysroot):
101 log.warn("Compiling with an SDK that doesn't seem to exist: %s",
102 sysroot)
103 log.warn("Please check your Xcode installation")
104
105 return compiler_so
106 44
107 class UnixCCompiler(CCompiler): 45 class UnixCCompiler(CCompiler):
108 46
109 compiler_type = 'unix' 47 compiler_type = 'unix'
110 48
111 # These are used by CCompiler in two places: the constructor sets 49 # These are used by CCompiler in two places: the constructor sets
112 # instance attributes 'preprocessor', 'compiler', etc. from them, and 50 # instance attributes 'preprocessor', 'compiler', etc. from them, and
113 # 'set_executable()' allows any of these to be set. The defaults here 51 # 'set_executable()' allows any of these to be set. The defaults here
114 # are pretty generic; they will probably have to be set by an outsider 52 # are pretty generic; they will probably have to be set by an outsider
115 # (eg. using information discovered by the sysconfig about building 53 # (eg. using information discovered by the sysconfig about building
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 if self.force or output_file is None or newer(source, output_file): 101 if self.force or output_file is None or newer(source, output_file):
164 if output_file: 102 if output_file:
165 self.mkpath(os.path.dirname(output_file)) 103 self.mkpath(os.path.dirname(output_file))
166 try: 104 try:
167 self.spawn(pp_args) 105 self.spawn(pp_args)
168 except DistutilsExecError as msg: 106 except DistutilsExecError as msg:
169 raise CompileError(msg) 107 raise CompileError(msg)
170 108
171 def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts): 109 def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
172 compiler_so = self.compiler_so 110 compiler_so = self.compiler_so
173 if 'msys' in sys.builtin_module_names:
174 src = msysize(src)
175 obj = msysize(obj)
176 cc_args = [msysize(x) for x in cc_args]
177 extra_postargs = [msysize(x) for x in extra_postargs]
178 if sys.platform == 'darwin': 111 if sys.platform == 'darwin':
179 compiler_so = _darwin_compiler_fixup(compiler_so, cc_args + extra_po stargs) 112 compiler_so = _osx_support.compiler_fixup(compiler_so,
113 cc_args + extra_postargs)
180 try: 114 try:
181 self.spawn(compiler_so + cc_args + [src, '-o', obj] + 115 self.spawn(compiler_so + cc_args + [src, '-o', obj] +
182 extra_postargs) 116 extra_postargs)
183 except DistutilsExecError as msg: 117 except DistutilsExecError as msg:
184 raise CompileError(msg) 118 raise CompileError(msg)
185 119
186 def create_static_lib(self, objects, output_libname, 120 def create_static_lib(self, objects, output_libname,
187 output_dir=None, debug=0, target_lang=None): 121 output_dir=None, debug=0, target_lang=None):
188 objects, output_dir = self._fix_object_args(objects, output_dir) 122 objects, output_dir = self._fix_object_args(objects, output_dir)
189 123
(...skipping 24 matching lines...) Expand all
214 library_dirs=None, runtime_library_dirs=None, 148 library_dirs=None, runtime_library_dirs=None,
215 export_symbols=None, debug=0, extra_preargs=None, 149 export_symbols=None, debug=0, extra_preargs=None,
216 extra_postargs=None, build_temp=None, target_lang=None): 150 extra_postargs=None, build_temp=None, target_lang=None):
217 objects, output_dir = self._fix_object_args(objects, output_dir) 151 objects, output_dir = self._fix_object_args(objects, output_dir)
218 fixed_args = self._fix_lib_args(libraries, library_dirs, 152 fixed_args = self._fix_lib_args(libraries, library_dirs,
219 runtime_library_dirs) 153 runtime_library_dirs)
220 libraries, library_dirs, runtime_library_dirs = fixed_args 154 libraries, library_dirs, runtime_library_dirs = fixed_args
221 155
222 lib_opts = gen_lib_options(self, library_dirs, runtime_library_dirs, 156 lib_opts = gen_lib_options(self, library_dirs, runtime_library_dirs,
223 libraries) 157 libraries)
158
159 lib_opts.append('-L.')
160
224 if not isinstance(output_dir, (str, type(None))): 161 if not isinstance(output_dir, (str, type(None))):
225 raise TypeError("'output_dir' must be a string or None") 162 raise TypeError("'output_dir' must be a string or None")
226 if output_dir is not None: 163 if output_dir is not None:
227 output_filename = os.path.join(output_dir, output_filename) 164 output_filename = os.path.join(output_dir, output_filename)
228 165
229 if self._need_link(objects, output_filename): 166 if self._need_link(objects, output_filename):
230 ld_args = (objects + self.objects + 167 ld_args = (objects + self.objects +
231 lib_opts + ['-o', output_filename]) 168 lib_opts + ['-o', output_filename])
232 if debug: 169 if debug:
233 ld_args[:0] = ['-g'] 170 ld_args[:0] = ['-g']
(...skipping 14 matching lines...) Expand all
248 # normal and C++ compiler have the same environment 185 # normal and C++ compiler have the same environment
249 # settings. 186 # settings.
250 i = 0 187 i = 0
251 if os.path.basename(linker[0]) == "env": 188 if os.path.basename(linker[0]) == "env":
252 i = 1 189 i = 1
253 while '=' in linker[i]: 190 while '=' in linker[i]:
254 i += 1 191 i += 1
255 linker[i] = self.compiler_cxx[i] 192 linker[i] = self.compiler_cxx[i]
256 193
257 if sys.platform == 'darwin': 194 if sys.platform == 'darwin':
258 linker = _darwin_compiler_fixup(linker, ld_args) 195 linker = _osx_support.compiler_fixup(linker, ld_args)
259 196
260 self.spawn(linker + ld_args) 197 self.spawn(linker + ld_args)
261 except DistutilsExecError as msg: 198 except DistutilsExecError as msg:
262 raise LinkError(msg) 199 raise LinkError(msg)
263 else: 200 else:
264 log.debug("skipping %s (up-to-date)", output_filename) 201 log.debug("skipping %s (up-to-date)", output_filename)
265 202
266 # -- Miscellaneous methods ----------------------------------------- 203 # -- Miscellaneous methods -----------------------------------------
267 # These are all used by the 'gen_lib_options() function, in 204 # These are all used by the 'gen_lib_options() function, in
268 # ccompiler.py. 205 # ccompiler.py.
(...skipping 27 matching lines...) Expand all
296 return ["-Wl,+s", "-L" + dir] 233 return ["-Wl,+s", "-L" + dir]
297 return ["+s", "-L" + dir] 234 return ["+s", "-L" + dir]
298 elif sys.platform[:7] == "irix646" or sys.platform[:6] == "osf1V5": 235 elif sys.platform[:7] == "irix646" or sys.platform[:6] == "osf1V5":
299 return ["-rpath", dir] 236 return ["-rpath", dir]
300 else: 237 else:
301 if self._is_gcc(compiler): 238 if self._is_gcc(compiler):
302 # gcc on non-GNU systems does not need -Wl, but can 239 # gcc on non-GNU systems does not need -Wl, but can
303 # use it anyway. Since distutils has always passed in 240 # use it anyway. Since distutils has always passed in
304 # -Wl whenever gcc was used in the past it is probably 241 # -Wl whenever gcc was used in the past it is probably
305 # safest to keep doing so. 242 # safest to keep doing so.
306 if sysconfig.get_config_var("GNULD") == "yes" and \ 243 if sysconfig.get_config_var("GNULD") == "yes":
307 sys.platform != 'win32': 244 # GNU ELF ld needs an extra option to get a RUNPATH
308 # GNU ld needs an extra option to get a RUNPATH
309 # instead of just an RPATH. 245 # instead of just an RPATH.
310 return "-Wl,--enable-new-dtags,-R" + dir 246 if sys.platform in ["win32", "cygwin"] or \
247 "mingw" in compiler:
248 return []
249 else:
250 return "-Wl,--enable-new-dtags,-R" + dir
311 else: 251 else:
312 return "-Wl,-R" + dir 252 return "-Wl,-R" + dir
313 else: 253 else:
314 # No idea how --enable-new-dtags would be passed on to 254 # No idea how --enable-new-dtags would be passed on to
315 # ld if this system was using GNU ld. Don't know if a 255 # ld if this system was using GNU ld. Don't know if a
316 # system like this even exists. 256 # system like this even exists.
317 return "-R" + dir 257 return "-R" + dir
318 258
319 def library_option(self, lib): 259 def library_option(self, lib):
320 return "-l" + lib 260 return "-l" + lib
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 # ignoring even GCC's "-static" option. So sue me. 296 # ignoring even GCC's "-static" option. So sue me.
357 if os.path.exists(dylib): 297 if os.path.exists(dylib):
358 return dylib 298 return dylib
359 elif os.path.exists(shared): 299 elif os.path.exists(shared):
360 return shared 300 return shared
361 elif os.path.exists(static): 301 elif os.path.exists(static):
362 return static 302 return static
363 303
364 # Oops, didn't find it in *any* of 'dirs' 304 # Oops, didn't find it in *any* of 'dirs'
365 return None 305 return None
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+