Masood Malekghassemi | 586e383 | 2016-06-03 19:29:12 -0700 | [diff] [blame^] | 1 | # Copyright 2016, Google Inc. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # Redistribution and use in source and binary forms, with or without |
| 5 | # modification, are permitted provided that the following conditions are |
| 6 | # met: |
| 7 | # |
| 8 | # * Redistributions of source code must retain the above copyright |
| 9 | # notice, this list of conditions and the following disclaimer. |
| 10 | # * Redistributions in binary form must reproduce the above |
| 11 | # copyright notice, this list of conditions and the following disclaimer |
| 12 | # in the documentation and/or other materials provided with the |
| 13 | # distribution. |
| 14 | # * Neither the name of Google Inc. nor the names of its |
| 15 | # contributors may be used to endorse or promote products derived from |
| 16 | # this software without specific prior written permission. |
| 17 | # |
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | """Covers inadequacies in distutils.""" |
| 31 | |
| 32 | from distutils import ccompiler |
| 33 | from distutils import errors |
| 34 | from distutils import unixccompiler |
| 35 | import os |
| 36 | import os.path |
| 37 | import shutil |
| 38 | import sys |
| 39 | import tempfile |
| 40 | |
| 41 | |
| 42 | def _unix_piecemeal_link( |
| 43 | self, target_desc, objects, output_filename, output_dir=None, |
| 44 | libraries=None, library_dirs=None, runtime_library_dirs=None, |
| 45 | export_symbols=None, debug=0, extra_preargs=None, extra_postargs=None, |
| 46 | build_temp=None, target_lang=None): |
| 47 | """`link` externalized method taken almost verbatim from UnixCCompiler. |
| 48 | |
| 49 | Modifies the link command for unix-like compilers by using a command file so |
| 50 | that long command line argument strings don't break the command shell's |
| 51 | ARG_MAX character limit. |
| 52 | """ |
| 53 | objects, output_dir = self._fix_object_args(objects, output_dir) |
| 54 | libraries, library_dirs, runtime_library_dirs = self._fix_lib_args( |
| 55 | libraries, library_dirs, runtime_library_dirs) |
| 56 | # filter out standard library paths, which are not explicitely needed |
| 57 | # for linking |
| 58 | library_dirs = [dir for dir in library_dirs |
| 59 | if not dir in ('/lib', '/lib64', '/usr/lib', '/usr/lib64')] |
| 60 | runtime_library_dirs = [dir for dir in runtime_library_dirs |
| 61 | if not dir in ('/lib', '/lib64', '/usr/lib', '/usr/lib64')] |
| 62 | lib_opts = ccompiler.gen_lib_options(self, library_dirs, runtime_library_dirs, |
| 63 | libraries) |
| 64 | if not isinstance(output_dir, basestring) and output_dir is not None: |
| 65 | raise TypeError, "'output_dir' must be a string or None" |
| 66 | if output_dir is not None: |
| 67 | output_filename = os.path.join(output_dir, output_filename) |
| 68 | |
| 69 | if self._need_link(objects, output_filename): |
| 70 | ld_args = (objects + self.objects + |
| 71 | lib_opts + ['-o', output_filename]) |
| 72 | if debug: |
| 73 | ld_args[:0] = ['-g'] |
| 74 | if extra_preargs: |
| 75 | ld_args[:0] = extra_preargs |
| 76 | if extra_postargs: |
| 77 | ld_args.extend(extra_postargs) |
| 78 | self.mkpath(os.path.dirname(output_filename)) |
| 79 | try: |
| 80 | if target_desc == ccompiler.CCompiler.EXECUTABLE: |
| 81 | linker = self.linker_exe[:] |
| 82 | else: |
| 83 | linker = self.linker_so[:] |
| 84 | if target_lang == "c++" and self.compiler_cxx: |
| 85 | # skip over environment variable settings if /usr/bin/env |
| 86 | # is used to set up the linker's environment. |
| 87 | # This is needed on OSX. Note: this assumes that the |
| 88 | # normal and C++ compiler have the same environment |
| 89 | # settings. |
| 90 | i = 0 |
| 91 | if os.path.basename(linker[0]) == "env": |
| 92 | i = 1 |
| 93 | while '=' in linker[i]: |
| 94 | i = i + 1 |
| 95 | |
| 96 | linker[i] = self.compiler_cxx[i] |
| 97 | |
| 98 | if sys.platform == 'darwin': |
| 99 | import _osx_support |
| 100 | linker = _osx_support.compiler_fixup(linker, ld_args) |
| 101 | |
| 102 | temporary_directory = tempfile.mkdtemp() |
| 103 | command_filename = os.path.abspath( |
| 104 | os.path.join(temporary_directory, 'command')) |
| 105 | with open(command_filename, 'w') as command_file: |
| 106 | escaped_ld_args = [arg.replace('\\', '\\\\') for arg in ld_args] |
| 107 | command_file.write(' '.join(escaped_ld_args)) |
| 108 | self.spawn(linker + ['@{}'.format(command_filename)]) |
| 109 | except errors.DistutilsExecError, msg: |
| 110 | raise ccompiler.LinkError, msg |
| 111 | else: |
| 112 | log.debug("skipping %s (up-to-date)", output_filename) |
| 113 | |
| 114 | def monkeypatch_unix_compiler(): |
| 115 | """Monkeypatching is dumb, but it's either that or we become maintainers of |
| 116 | something much, much bigger.""" |
| 117 | unixccompiler.UnixCCompiler.link = _unix_piecemeal_link |