summaryrefslogtreecommitdiff
path: root/site_scons/site_tools/split_dwarf.py
blob: 95130c9e9a387cb48cb129b0e3b2f62ed48f7962 (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
# Copyright 2017 MongoDB Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import SCons

_splitDwarfFlag = '-gsplit-dwarf'

# Cribbed from Tool/cc.py and Tool/c++.py. It would be better if
# we could obtain this from SCons.
_CSuffixes = ['.c']
if not SCons.Util.case_sensitive_suffixes('.c', '.C'):
    _CSuffixes.append('.C')

_CXXSuffixes = ['.cpp', '.cc', '.cxx', '.c++', '.C++']
if SCons.Util.case_sensitive_suffixes('.c', '.C'):
    _CXXSuffixes.append('.C')

def _dwo_emitter(target, source, env):
    new_targets = []
    for t in target:
        base, ext = SCons.Util.splitext(str(t))
        if not any(ext == env[osuffix] for osuffix in ['OBJSUFFIX', 'SHOBJSUFFIX']):
            continue
        # TODO: Move 'dwo' into DWOSUFFIX so it can be customized? For
        # now, GCC doesn't let you control the output filename, so it
        # doesn't matter.
        dwotarget = (t.builder.target_factory or env.File)(base + ".dwo")
        new_targets.append(dwotarget)
    targets = target + new_targets
    return (targets, source)

def generate(env):
    suffixes = []
    if _splitDwarfFlag in env['CCFLAGS']:
        suffixes = _CSuffixes + _CXXSuffixes
    else:
        if _splitDwarfFlag in env['CFLAGS']:
            suffixes.extend(_CSuffixes)
        if _splitDwarfFlag in env['CXXFLAGS']:
            suffixes.extend(_CXXSuffixes)

    for object_builder in SCons.Tool.createObjBuilders(env):
        emitterdict = object_builder.builder.emitter
        for suffix in emitterdict.iterkeys():
            if not suffix in suffixes:
                continue
            base = emitterdict[suffix]
            emitterdict[suffix] = SCons.Builder.ListEmitter([
                base,
                _dwo_emitter,
            ])

def exists(env):
    return any(_splitDwarfFlag in env[f] for f in ['CCFLAGS', 'CFLAGS', 'CXXFLAGS'])