summaryrefslogtreecommitdiff
path: root/app-editors/atom/files/gyp-unbundle.py
blob: 370221380b64a61cfe1adf7312be2279a69a1061 (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
#!/usr/bin/env python2

from __future__ import print_function


import argparse
import pprint
import sys


def die(msg):
    print(msg, file=sys.stderr)
    sys.exit(1)


def do_unbundle(gypdata, targets):
    gyptargets = {t['target_name']: t for t in gypdata['targets']}
    dropped_deps = set()

    def _unbundle_in_block(gypblock):
        gypdeps = gypblock.get('dependencies') or {}

        for dep, (libs, defines) in unbundlings.items():
            if dep not in gypdeps:
                continue

            gypdeps.remove(dep)

            try:
                ls = gyptarget['link_settings']
            except KeyError:
                ls = gyptarget['link_settings'] = {}

            try:
                gyplibs = ls['libraries']
            except KeyError:
                gyplibs = ls['libraries'] = []

            gyplibs.extend('-l{}'.format(lib) for lib in libs)

            if defines:
                try:
                    dd = gyptarget['defines']
                except KeyError:
                    dd = gyptarget['defines'] = []

                dd.extend(defines)

            dropped_deps.add(dep)

        gypconds = gypblock.get('conditions') or []
        for cond in gypconds:
            condblocks = cond[1:]
            for condblock in condblocks:
                _unbundle_in_block(condblock)

    for target, unbundlings in targets.items():
        if target not in gyptargets:
            die('There is no {} target in gyp file'.format(target))

        gyptarget = gyptargets[target]

        _unbundle_in_block(gyptarget)

    for gyptarget in gypdata['targets']:
        if gyptarget['target_name'] in dropped_deps:
            if gyptarget.get('dependencies'):
                dropped_deps.update(gyptarget.get('dependencies'))

    new_targets = []
    for gyptarget in gypdata['targets']:
        if gyptarget['target_name'] not in dropped_deps:
            new_targets.append(gyptarget)

    gypdata['targets'] = new_targets

    gypconds = gypdata.get('conditions')
    if gypconds:
        for cond in gypconds:
            condblocks = cond[1:]
            for condblock in condblocks:
                new_targets = []
                blocktargets = condblock.get('targets')
                if blocktargets:
                    for blocktarget in blocktargets:
                        if blocktarget['target_name'] not in dropped_deps:
                            new_targets.append(blocktarget)
                    condblock['targets'] = new_targets


def main():
    parser = argparse.ArgumentParser(description='Unbundle libs in gyp files')
    parser.add_argument('gypfile', type=str, help='input gyp file')
    parser.add_argument(
        '--unbundle', type=str, action='append',
        help='unbundle rule in the format '
             '<target>;<dep>;<lib>[;lib][;-DMACRO]')
    parser.add_argument(
        '-i', '--inplace', action='store_true',
        help='modify gyp file in-place')

    args = parser.parse_args()

    targets = {}

    for unbundle in args.unbundle:
        rule = list(filter(None, (i.strip() for i in unbundle.split(';'))))
        if len(rule) < 3:
            die('Invalid unbundle rule: {!r}'.format(unbundle))
        target, dep = rule[:2]

        defines = []
        libs = []

        for item in rule[2:]:
            if item.startswith('-D'):
                defines.append(item[2:])
            else:
                libs.append(item)

        try:
            target_unbundlings = targets[target]
        except KeyError:
            target_unbundlings = targets[target] = {}

        target_unbundlings[dep] = libs, defines

    with open(args.gypfile, 'rt') as f:
        gypdata = eval(f.read())

    do_unbundle(gypdata, targets)

    if args.inplace:
        with open(args.gypfile, 'wt') as f:
            pprint.pprint(gypdata, stream=f)
    else:
        pprint.pprint(gypdata)


if __name__ == '__main__':
    main()