PageRenderTime 177ms CodeModel.GetById 40ms app.highlight 81ms RepoModel.GetById 50ms app.codeStats 0ms

/lib-python/2/distutils/unixccompiler.py

https://bitbucket.org/kcr/pypy
Python | 361 lines | 298 code | 22 blank | 41 comment | 38 complexity | defdb7223298df7f15e9de355da76f64 MD5 | raw file
  1"""distutils.unixccompiler
  2
  3Contains the UnixCCompiler class, a subclass of CCompiler that handles
  4the "typical" Unix-style command-line C compiler:
  5  * macros defined with -Dname[=value]
  6  * macros undefined with -Uname
  7  * include search directories specified with -Idir
  8  * libraries specified with -lllib
  9  * library search directories specified with -Ldir
 10  * compile handled by 'cc' (or similar) executable with -c option:
 11    compiles .c to .o
 12  * link static library handled by 'ar' command (possibly with 'ranlib')
 13  * link shared library handled by 'cc -shared'
 14"""
 15
 16__revision__ = "$Id$"
 17
 18import os, sys, re
 19from types import StringType, NoneType
 20
 21from distutils import sysconfig
 22from distutils.dep_util import newer
 23from distutils.ccompiler import \
 24     CCompiler, gen_preprocess_options, gen_lib_options
 25from distutils.errors import \
 26     DistutilsExecError, CompileError, LibError, LinkError
 27from distutils import log
 28
 29# XXX Things not currently handled:
 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
 32#     have to have a bunch of subclasses GNUCCompiler, SGICCompiler,
 33#     SunCCompiler, and I suspect down that road lies madness.
 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
 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
 38#     compiling this module distribution only via the setup.py command
 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
 41#     should just happily stuff them into the preprocessor/compiler/linker
 42#     options and carry on.
 43
 44def _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 = 0
 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 1:
 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
107class UnixCCompiler(CCompiler):
108
109    compiler_type = 'unix'
110
111    # These are used by CCompiler in two places: the constructor sets
112    # instance attributes 'preprocessor', 'compiler', etc. from them, and
113    # '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
115    # (eg. using information discovered by the sysconfig about building
116    # Python extensions).
117    executables = {'preprocessor' : None,
118                   'compiler'     : ["cc"],
119                   'compiler_so'  : ["cc"],
120                   'compiler_cxx' : ["cc"],
121                   'linker_so'    : ["cc", "-shared"],
122                   'linker_exe'   : ["cc"],
123                   'archiver'     : ["ar", "-cr"],
124                   'ranlib'       : None,
125                  }
126
127    if sys.platform[:6] == "darwin":
128        import platform
129        if platform.machine() == 'i386':
130            if platform.architecture()[0] == '32bit':
131                arch = 'i386'
132            else:
133                arch = 'x86_64'
134        else:
135            # just a guess
136            arch = platform.machine()
137        executables['ranlib'] = ["ranlib"]
138        executables['linker_so'] += ['-undefined', 'dynamic_lookup']
139
140        for k, v in executables.iteritems():
141            if v and v[0] == 'cc':
142                v += ['-arch', arch]
143
144
145    # Needed for the filename generation methods provided by the base
146    # class, CCompiler.  NB. whoever instantiates/uses a particular
147    # UnixCCompiler instance should set 'shared_lib_ext' -- we set a
148    # reasonable common default here, but it's not necessarily used on all
149    # Unices!
150
151    src_extensions = [".c",".C",".cc",".cxx",".cpp",".m"]
152    obj_extension = ".o"
153    static_lib_extension = ".a"
154    shared_lib_extension = ".so"
155    dylib_lib_extension = ".dylib"
156    static_lib_format = shared_lib_format = dylib_lib_format = "lib%s%s"
157    if sys.platform == "cygwin":
158        exe_extension = ".exe"
159
160    def preprocess(self, source,
161                   output_file=None, macros=None, include_dirs=None,
162                   extra_preargs=None, extra_postargs=None):
163        ignore, macros, include_dirs = \
164            self._fix_compile_args(None, macros, include_dirs)
165        pp_opts = gen_preprocess_options(macros, include_dirs)
166        pp_args = self.preprocessor + pp_opts
167        if output_file:
168            pp_args.extend(['-o', output_file])
169        if extra_preargs:
170            pp_args[:0] = extra_preargs
171        if extra_postargs:
172            pp_args.extend(extra_postargs)
173        pp_args.append(source)
174
175        # We need to preprocess: either we're being forced to, or we're
176        # generating output to stdout, or there's a target output file and
177        # the source file is newer than the target (or the target doesn't
178        # exist).
179        if self.force or output_file is None or newer(source, output_file):
180            if output_file:
181                self.mkpath(os.path.dirname(output_file))
182            try:
183                self.spawn(pp_args)
184            except DistutilsExecError, msg:
185                raise CompileError, msg
186
187    def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
188        compiler_so = self.compiler_so
189        if sys.platform == 'darwin':
190            compiler_so = _darwin_compiler_fixup(compiler_so, cc_args + extra_postargs)
191        try:
192            self.spawn(compiler_so + cc_args + [src, '-o', obj] +
193                       extra_postargs)
194        except DistutilsExecError, msg:
195            raise CompileError, msg
196
197    def create_static_lib(self, objects, output_libname,
198                          output_dir=None, debug=0, target_lang=None):
199        objects, output_dir = self._fix_object_args(objects, output_dir)
200
201        output_filename = \
202            self.library_filename(output_libname, output_dir=output_dir)
203
204        if self._need_link(objects, output_filename):
205            self.mkpath(os.path.dirname(output_filename))
206            self.spawn(self.archiver +
207                       [output_filename] +
208                       objects + self.objects)
209
210            # Not many Unices required ranlib anymore -- SunOS 4.x is, I
211            # think the only major Unix that does.  Maybe we need some
212            # platform intelligence here to skip ranlib if it's not
213            # needed -- or maybe Python's configure script took care of
214            # it for us, hence the check for leading colon.
215            if self.ranlib:
216                try:
217                    self.spawn(self.ranlib + [output_filename])
218                except DistutilsExecError, msg:
219                    raise LibError, msg
220        else:
221            log.debug("skipping %s (up-to-date)", output_filename)
222
223    def link(self, target_desc, objects,
224             output_filename, output_dir=None, libraries=None,
225             library_dirs=None, runtime_library_dirs=None,
226             export_symbols=None, debug=0, extra_preargs=None,
227             extra_postargs=None, build_temp=None, target_lang=None):
228        objects, output_dir = self._fix_object_args(objects, output_dir)
229        libraries, library_dirs, runtime_library_dirs = \
230            self._fix_lib_args(libraries, library_dirs, runtime_library_dirs)
231
232        lib_opts = gen_lib_options(self, library_dirs, runtime_library_dirs,
233                                   libraries)
234        if type(output_dir) not in (StringType, NoneType):
235            raise TypeError, "'output_dir' must be a string or None"
236        if output_dir is not None:
237            output_filename = os.path.join(output_dir, output_filename)
238
239        if self._need_link(objects, output_filename):
240            ld_args = (objects + self.objects +
241                       lib_opts + ['-o', output_filename])
242            if debug:
243                ld_args[:0] = ['-g']
244            if extra_preargs:
245                ld_args[:0] = extra_preargs
246            if extra_postargs:
247                ld_args.extend(extra_postargs)
248            self.mkpath(os.path.dirname(output_filename))
249            try:
250                if target_desc == CCompiler.EXECUTABLE:
251                    linker = self.linker_exe[:]
252                else:
253                    linker = self.linker_so[:]
254                if target_lang == "c++" and self.compiler_cxx:
255                    # skip over environment variable settings if /usr/bin/env
256                    # is used to set up the linker's environment.
257                    # This is needed on OSX. Note: this assumes that the
258                    # normal and C++ compiler have the same environment
259                    # settings.
260                    i = 0
261                    if os.path.basename(linker[0]) == "env":
262                        i = 1
263                        while '=' in linker[i]:
264                            i = i + 1
265
266                    linker[i] = self.compiler_cxx[i]
267
268                if sys.platform == 'darwin':
269                    linker = _darwin_compiler_fixup(linker, ld_args)
270
271                self.spawn(linker + ld_args)
272            except DistutilsExecError, msg:
273                raise LinkError, msg
274        else:
275            log.debug("skipping %s (up-to-date)", output_filename)
276
277    # -- Miscellaneous methods -----------------------------------------
278    # These are all used by the 'gen_lib_options() function, in
279    # ccompiler.py.
280
281    def library_dir_option(self, dir):
282        return "-L" + dir
283
284    def _is_gcc(self, compiler_name):
285        return "gcc" in compiler_name or "g++" in compiler_name
286
287    def runtime_library_dir_option(self, dir):
288        # XXX Hackish, at the very least.  See Python bug #445902:
289        # http://sourceforge.net/tracker/index.php
290        #   ?func=detail&aid=445902&group_id=5470&atid=105470
291        # Linkers on different platforms need different options to
292        # specify that directories need to be added to the list of
293        # directories searched for dependencies when a dynamic library
294        # is sought.  GCC has to be told to pass the -R option through
295        # to the linker, whereas other compilers just know this.
296        # Other compilers may need something slightly different.  At
297        # this time, there's no way to determine this information from
298        # the configuration data stored in the Python installation, so
299        # we use this hack.
300        compiler = os.path.basename(sysconfig.get_config_var("CC"))
301        if sys.platform[:6] == "darwin":
302            # MacOSX's linker doesn't understand the -R flag at all
303            return "-L" + dir
304        elif sys.platform[:5] == "hp-ux":
305            if self._is_gcc(compiler):
306                return ["-Wl,+s", "-L" + dir]
307            return ["+s", "-L" + dir]
308        elif sys.platform[:7] == "irix646" or sys.platform[:6] == "osf1V5":
309            return ["-rpath", dir]
310        elif self._is_gcc(compiler):
311            return "-Wl,-R" + dir
312        else:
313            return "-R" + dir
314
315    def library_option(self, lib):
316        return "-l" + lib
317
318    def find_library_file(self, dirs, lib, debug=0):
319        shared_f = self.library_filename(lib, lib_type='shared')
320        dylib_f = self.library_filename(lib, lib_type='dylib')
321        static_f = self.library_filename(lib, lib_type='static')
322
323        if sys.platform == 'darwin':
324            # On OSX users can specify an alternate SDK using
325            # '-isysroot', calculate the SDK root if it is specified
326            # (and use it further on)
327            cflags = sysconfig.get_config_var('CFLAGS') or ''
328            m = re.search(r'-isysroot\s+(\S+)', cflags)
329            if m is None:
330                sysroot = '/'
331            else:
332                sysroot = m.group(1)
333
334
335
336        for dir in dirs:
337            shared = os.path.join(dir, shared_f)
338            dylib = os.path.join(dir, dylib_f)
339            static = os.path.join(dir, static_f)
340
341            if sys.platform == 'darwin' and (
342                dir.startswith('/System/') or (
343                dir.startswith('/usr/') and not dir.startswith('/usr/local/'))):
344
345                shared = os.path.join(sysroot, dir[1:], shared_f)
346                dylib = os.path.join(sysroot, dir[1:], dylib_f)
347                static = os.path.join(sysroot, dir[1:], static_f)
348
349            # We're second-guessing the linker here, with not much hard
350            # data to go on: GCC seems to prefer the shared library, so I'm
351            # assuming that *all* Unix C compilers do.  And of course I'm
352            # ignoring even GCC's "-static" option.  So sue me.
353            if os.path.exists(dylib):
354                return dylib
355            elif os.path.exists(shared):
356                return shared
357            elif os.path.exists(static):
358                return static
359
360        # Oops, didn't find it in *any* of 'dirs'
361        return None