summaryrefslogtreecommitdiffstats
path: root/tools/scan-build-py/libear/__init__.py
blob: 421e2e74f02327d4bf338c282f53e809c3dcc7d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# -*- coding: utf-8 -*-
#                     The LLVM Compiler Infrastructure
#
# This file is distributed under the University of Illinois Open Source
# License. See LICENSE.TXT for details.
""" This module compiles the intercept library. """

import sys
import os
import os.path
import re
import tempfile
import shutil
import contextlib
import logging

__all__ = ['build_libear']


def build_libear(compiler, dst_dir):
    """ Returns the full path to the 'libear' library. """

    try:
        src_dir = os.path.dirname(os.path.realpath(__file__))
        toolset = make_toolset(src_dir)
        toolset.set_compiler(compiler)
        toolset.set_language_standard('c99')
        toolset.add_definitions(['-D_GNU_SOURCE'])

        configure = do_configure(toolset)
        configure.check_function_exists('execve', 'HAVE_EXECVE')
        configure.check_function_exists('execv', 'HAVE_EXECV')
        configure.check_function_exists('execvpe', 'HAVE_EXECVPE')
        configure.check_function_exists('execvp', 'HAVE_EXECVP')
        configure.check_function_exists('execvP', 'HAVE_EXECVP2')
        configure.check_function_exists('exect', 'HAVE_EXECT')
        configure.check_function_exists('execl', 'HAVE_EXECL')
        configure.check_function_exists('execlp', 'HAVE_EXECLP')
        configure.check_function_exists('execle', 'HAVE_EXECLE')
        configure.check_function_exists('posix_spawn', 'HAVE_POSIX_SPAWN')
        configure.check_function_exists('posix_spawnp', 'HAVE_POSIX_SPAWNP')
        configure.check_symbol_exists('_NSGetEnviron', 'crt_externs.h',
                                      'HAVE_NSGETENVIRON')
        configure.write_by_template(
            os.path.join(src_dir, 'config.h.in'),
            os.path.join(dst_dir, 'config.h'))

        target = create_shared_library('ear', toolset)
        target.add_include(dst_dir)
        target.add_sources('ear.c')
        target.link_against(toolset.dl_libraries())
        target.link_against(['pthread'])
        target.build_release(dst_dir)

        return os.path.join(dst_dir, target.name)

    except Exception:
        logging.info("Could not build interception library.", exc_info=True)
        return None


def execute(cmd, *args, **kwargs):
    """ Make subprocess execution silent. """

    import subprocess
    kwargs.update({'stdout': subprocess.PIPE, 'stderr': subprocess.STDOUT})
    return subprocess.check_call(cmd, *args, **kwargs)


@contextlib.contextmanager
def TemporaryDirectory(**kwargs):
    name = tempfile.mkdtemp(**kwargs)
    try:
        yield name
    finally:
        shutil.rmtree(name)


class Toolset(object):
    """ Abstract class to represent different toolset. """

    def __init__(self, src_dir):
        self.src_dir = src_dir
        self.compiler = None
        self.c_flags = []

    def set_compiler(self, compiler):
        """ part of public interface """
        self.compiler = compiler

    def set_language_standard(self, standard):
        """ part of public interface """
        self.c_flags.append('-std=' + standard)

    def add_definitions(self, defines):
        """ part of public interface """
        self.c_flags.extend(defines)

    def dl_libraries(self):
        raise NotImplementedError()

    def shared_library_name(self, name):
        raise NotImplementedError()

    def shared_library_c_flags(self, release):
        extra = ['-DNDEBUG', '-O3'] if release else []
        return extra + ['-fPIC'] + self.c_flags

    def shared_library_ld_flags(self, release, name):
        raise NotImplementedError()


class DarwinToolset(Toolset):
    def __init__(self, src_dir):
        Toolset.__init__(self, src_dir)

    def dl_libraries(self):
        return []

    def shared_library_name(self, name):
        return 'lib' + name + '.dylib'

    def shared_library_ld_flags(self, release, name):
        extra = ['-dead_strip'] if release else []
        return extra + ['-dynamiclib', '-install_name', '@rpath/' + name]


class UnixToolset(Toolset):
    def __init__(self, src_dir):
        Toolset.__init__(self, src_dir)

    def dl_libraries(self):
        return []

    def shared_library_name(self, name):
        return 'lib' + name + '.so'

    def shared_library_ld_flags(self, release, name):
        extra = [] if release else []
        return extra + ['-shared', '-Wl,-soname,' + name]


class LinuxToolset(UnixToolset):
    def __init__(self, src_dir):
        UnixToolset.__init__(self, src_dir)

    def dl_libraries(self):
        return ['dl']


def make_toolset(src_dir):
    platform = sys.platform
    if platform in {'win32', 'cygwin'}:
        raise RuntimeError('not implemented on this platform')
    elif platform == 'darwin':
        return DarwinToolset(src_dir)
    elif platform in {'linux', 'linux2'}:
        return LinuxToolset(src_dir)
    else:
        return UnixToolset(src_dir)


class Configure(object):
    def __init__(self, toolset):
        self.ctx = toolset
        self.results = {'APPLE': sys.platform == 'darwin'}

    def _try_to_compile_and_link(self, source):
        try:
            with TemporaryDirectory() as work_dir:
                src_file = 'check.c'
                with open(os.path.join(work_dir, src_file), 'w') as handle:
                    handle.write(source)

                execute([self.ctx.compiler, src_file] + self.ctx.c_flags,
                        cwd=work_dir)
                return True
        except Exception:
            return False

    def check_function_exists(self, function, name):
        template = "int FUNCTION(); int main() { return FUNCTION(); }"
        source = template.replace("FUNCTION", function)

        logging.debug('Checking function %s', function)
        found = self._try_to_compile_and_link(source)
        logging.debug('Checking function %s -- %s', function,
                      'found' if found else 'not found')
        self.results.update({name: found})

    def check_symbol_exists(self, symbol, include, name):
        template = """#include <INCLUDE>
                      int main() { return ((int*)(&SYMBOL))[0]; }"""
        source = template.replace('INCLUDE', include).replace("SYMBOL", symbol)

        logging.debug('Checking symbol %s', symbol)
        found = self._try_to_compile_and_link(source)
        logging.debug('Checking symbol %s -- %s', symbol,
                      'found' if found else 'not found')
        self.results.update({name: found})

    def write_by_template(self, template, output):
        def transform(line, definitions):

            pattern = re.compile(r'^#cmakedefine\s+(\S+)')
            m = pattern.match(line)
            if m:
                key = m.group(1)
                if key not in definitions or not definitions[key]:
                    return '/* #undef {0} */{1}'.format(key, os.linesep)
                else:
                    return '#define {0}{1}'.format(key, os.linesep)
            return line

        with open(template, 'r') as src_handle:
            logging.debug('Writing config to %s', output)
            with open(output, 'w') as dst_handle:
                for line in src_handle:
                    dst_handle.write(transform(line, self.results))


def do_configure(toolset):
    return Configure(toolset)


class SharedLibrary(object):
    def __init__(self, name, toolset):
        self.name = toolset.shared_library_name(name)
        self.ctx = toolset
        self.inc = []
        self.src = []
        self.lib = []

    def add_include(self, directory):
        self.inc.extend(['-I', directory])

    def add_sources(self, source):
        self.src.append(source)

    def link_against(self, libraries):
        self.lib.extend(['-l' + lib for lib in libraries])

    def build_release(self, directory):
        for src in self.src:
            logging.debug('Compiling %s', src)
            execute(
                [self.ctx.compiler, '-c', os.path.join(self.ctx.src_dir, src),
                 '-o', src + '.o'] + self.inc +
                self.ctx.shared_library_c_flags(True),
                cwd=directory)
        logging.debug('Linking %s', self.name)
        execute(
            [self.ctx.compiler] + [src + '.o' for src in self.src] +
            ['-o', self.name] + self.lib +
            self.ctx.shared_library_ld_flags(True, self.name),
            cwd=directory)


def create_shared_library(name, toolset):
    return SharedLibrary(name, toolset)