File: commands.py

package info (click to toggle)
roc-toolkit 0.4.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,700 kB
  • sloc: cpp: 102,987; ansic: 8,959; python: 6,125; sh: 942; makefile: 19; javascript: 9
file content (310 lines) | stat: -rw-r--r-- 9,676 bytes parent folder | download | duplicates (2)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import SCons.Script
import os
import os.path
import re
import shutil
import sys

try:
    from shlex import quote
except:
    from pipes import quote

def ClangFormat(env, src_dir):
    exclude_files = [
        os.path.relpath(env.File('#'+s).srcnode().abspath)
            for s in open(env.File('#.fmtignore').abspath).read().split()
        ]

    files = env.GlobRecursive(
        src_dir, ['*.h', '*.cpp'],
        exclude=exclude_files)

    return env.Action(
        '{clang_format} -i {files}'.format(
            clang_format=quote(env['CLANG_FORMAT']),
            files=' '.join(map(str, files))),
        env.PrettyCommand('FMT', env.Dir(src_dir).path, 'yellow'))

def HeaderFormat(env, src_dir):
    return env.Action(
        '{python} scripts/scons_helpers/format-header.py {src_dir}'.format(
            python=quote(env.GetPythonExecutable()),
            src_dir=quote(env.Dir(src_dir).path)),
        env.PrettyCommand('FMT', env.Dir(src_dir).path, 'yellow'))

def Doxygen(env, build_dir='', html_dir=None, config='', sources=[], werror=False):
    target = os.path.join(build_dir, 'commit')

    dirs = [env.Dir(build_dir).path]
    if html_dir:
        dirs += [env.Dir(html_dir).path]

    cmd = [
        quote(env.GetPythonExecutable()), 'scripts/scons_helpers/docfilt.py',
        '--root-dir', quote(env.Dir('#').path),
        '--work-dir', quote(env.Dir(os.path.dirname(config)).path),
        '--out-dirs', ' '.join(map(quote, dirs)),
        '--touch-file', quote(env.File(target).path),
        ]

    if werror:
        cmd += ['--werror']

    cmd += [
        '--',
        quote(env['DOXYGEN']),
        quote(env.File(config).name),
        ]

    env.Command(target, sources + [config], SCons.Action.CommandAction(
        ' '.join(cmd),
        cmdstr = env.PrettyCommand('DOXYGEN', env.Dir(build_dir).path, 'purple')))

    return target

def Sphinx(env, output_type, build_dir, output_dir, source_dir, sources, werror=False):
    target = os.path.join(build_dir, 'commit')

    cmd = [
        quote(env.GetPythonExecutable()), 'scripts/scons_helpers/docfilt.py',
        '--root-dir', quote(env.Dir('#').path),
        '--work-dir', quote(env.Dir('#').path),
        '--out-dirs', quote(env.Dir(output_dir).path),
        '--touch-file', quote(env.File(target).path),
        ]

    if werror:
        cmd += ['--werror']

    cmd += [
        '--',
        quote(env['SPHINX_BUILD']),
        '-j', str(SCons.Script.GetOption('num_jobs')),
        '-q',
        '-b', output_type,
        '-d', quote(env.Dir(build_dir).path),
        quote(env.Dir(source_dir).path),
        quote(env.Dir(output_dir).path),
        ]

    env.Command(target, sources, SCons.Action.CommandAction(
        ' '.join(cmd),
        cmdstr = env.PrettyCommand('SPHINX', env.Dir(output_dir).path, 'purple')))

    return env.File(target)

def Ragel(env, source, target=None):
    if 'RAGEL' in env.Dictionary():
        ragel = env['RAGEL']
    else:
        ragel = 'ragel'

    if not isinstance(ragel, str):
        ragel = env.File(ragel).path

    rl_file = env.File(source)

    cpp_file = env.File(os.path.join(
        str(source.dir),
        os.path.splitext(os.path.basename(source.path))[0] + '.cpp'))

    env.Command(cpp_file, rl_file, SCons.Action.CommandAction(
        '{ragel} -o {target} {source}'.format(
            ragel=quote(ragel),
            target=quote(cpp_file.path),
            source=quote(rl_file.srcnode().path)),
        cmdstr = env.PrettyCommand('RAGEL', '$SOURCE', 'purple')))

    return [env.Object(target=target, source=cpp_file)]

def GenGetOpt(env, source, version):
    if 'GENGETOPT' in env.Dictionary():
        gengetopt = env['GENGETOPT']
    else:
        gengetopt = 'gengetopt'

    if not isinstance(gengetopt, str):
        gengetopt = env.File(gengetopt).path

    source = env.File(source)
    source_name = os.path.splitext(os.path.basename(source.path))[0]
    target = [
        os.path.join(str(source.dir), source_name + '.c'),
        os.path.join(str(source.dir), source_name + '.h'),
    ]

    env.Command(target, source, SCons.Action.CommandAction(
        ('{gengetopt} -i {source} -F {source_name} --output-dir {output_dir}'
         ' --set-version {version}').format(
            gengetopt=quote(gengetopt),
            source=quote(source.srcnode().path),
            source_name=quote(source_name),
            output_dir=quote(os.path.dirname(source.path)),
            version=quote(version)),
        cmdstr = env.PrettyCommand('GGO', '$SOURCE', 'purple')))

    return env.Object(target[0])

def SupportsRelocatableObject(env):
    if not env.get('LD', None):
        return False

    out = env.GetCommandOutput('{} -V'.format(env['LD']))
    return 'GNU' in out

def RelocatableObject(env, dst, src_list):
    dst += env['OBJSUFFIX']

    action = SCons.Action.CommandAction(
        '$LD -r $SOURCES -o $TARGET',
        cmdstr=env.PrettyCommand('LD', env.File(dst).path, 'red'))

    return env.Command(dst, src_list, [action])

def SupportsLocalizedObject(env):
    if not env.get('OBJCOPY', None):
        return False

    out = env.GetCommandOutput('{} -V'.format(env['OBJCOPY']))
    return 'GNU' in out

def LocalizedObject(env, dst, src):
    dst += env['OBJSUFFIX']

    action = SCons.Action.CommandAction(
        '$OBJCOPY --localize-hidden --strip-unneeded $SOURCE $TARGET',
        cmdstr=env.PrettyCommand('OBJCOPY', env.File(dst).path, 'red'))

    return env.Command(dst, src, [action])

def SupportsStripSharedLibrary(env):
    return env.get('STRIP', None) is not None

def StripSharedLibrary(env, dst, src):
    def _copy(target, source, env):
        shutil.copy(source[0].path, target[0].path)

    actions =  [
        env.Action(
            _copy,
            env.PrettyCommand('CP', env.File(dst).path, 'yellow'),
            ),
        SCons.Action.CommandAction(
            '$STRIP $STRIPFLAGS $TARGET',
            cmdstr=env.PrettyCommand('STRIP', '$TARGET', 'red'),
            ),
    ]

    return env.Command(dst, src, actions)

def SymlinkLibrary(env, src):
    def _symlink(target, source, env):
        os.symlink(os.path.relpath(source[0].path, os.path.dirname(target[0].path)),
                   target[0].path)

    path = src.abspath
    ret = []

    while True:
        m = re.match(r'^(.+)\.[0-9]+(\.[a-z]+)?$', path)
        if not m:
            break

        path = m.group(1) + (m.group(2) or '')

        dst = env.File(path)
        ret += [dst]

        env.Command(dst, src, env.Action(
            _symlink, env.PrettyCommand('LN', dst.path, 'yellow', 'ln({})'.format(dst.path))))

    return ret

def NeedsFixupSharedLibrary(env):
    return env.get('INSTALL_NAME_TOOL', None) is not None

def FixupSharedLibrary(env, path):
    return [
        SCons.Action.CommandAction(
            '$INSTALL_NAME_TOOL -id {0} {0}'.format(quote(path)),
            cmdstr=env.PrettyCommand('FIXUP', path, 'yellow')),
            ]

def ComposeStaticLibraries(env, dst_lib, src_libs):
    dst_lib = env['LIBPREFIX'] + dst_lib + env['LIBSUFFIX']

    cmd = [
        quote(env.GetPythonExecutable()), 'scripts/scons_helpers/compose-libs.py',
        '--out', quote(env.File(dst_lib).path),
        '--in', ' '.join([quote(env.File(lib).path) for lib in src_libs]),
        ]

    if env['ROC_PLATFORM'] == 'darwin' and env['ROC_MACOS_ARCH']:
        cmd += ['--arch', ' '.join(env['ROC_MACOS_ARCH'])]

    cmd += ['--tools']
    for tool in ['AR', 'OBJCOPY', 'LIPO']:
        if env.get(tool, None):
            cmd += [
                '{}={}'.format(tool, quote(env[tool])),
                ]

    action = SCons.Action.CommandAction(
        ' '.join(cmd),
        cmdstr=env.PrettyCommand('COMPOSE', env.File(dst_lib).path, 'red'))

    return env.Command(dst_lib, [src_libs[0]], [action])

def DeleteFile(env, path):
    path = env.File(path).path

    def rmfile(target, source, env):
        if os.path.exists(path):
            os.remove(path)

    return env.Action(rmfile, env.PrettyCommand('RM', path, 'red', 'rm({})'.format(path)))

def DeleteDir(env, path):
    path = env.Dir(path).path

    def rmtree(target, source, env):
        if os.path.exists(path):
            shutil.rmtree(path)

    return env.Action(rmtree, env.PrettyCommand('RM', path, 'red', 'rm({})'.format(path)))

def Artifact(env, dst, src):
    def noop(target, source, env):
        pass

    target = env.File(dst)

    env.Command(dst, src, env.Action(noop, env.PrettyCommand(
        'ART', target.path, 'yellow', 'art({})'.format(target.path))))

    env.Precious(dst)
    env.Requires(dst, src)

    return target

def init(env):
    env.AddMethod(ClangFormat, 'ClangFormat')
    env.AddMethod(HeaderFormat, 'HeaderFormat')
    env.AddMethod(Doxygen, 'Doxygen')
    env.AddMethod(Sphinx, 'Sphinx')
    env.AddMethod(Ragel, 'Ragel')
    env.AddMethod(GenGetOpt, 'GenGetOpt')
    env.AddMethod(SupportsRelocatableObject, 'SupportsRelocatableObject')
    env.AddMethod(RelocatableObject, 'RelocatableObject')
    env.AddMethod(SupportsLocalizedObject, 'SupportsLocalizedObject')
    env.AddMethod(LocalizedObject, 'LocalizedObject')
    env.AddMethod(SupportsStripSharedLibrary, 'SupportsStripSharedLibrary')
    env.AddMethod(StripSharedLibrary, 'StripSharedLibrary')
    env.AddMethod(SymlinkLibrary, 'SymlinkLibrary')
    env.AddMethod(NeedsFixupSharedLibrary, 'NeedsFixupSharedLibrary')
    env.AddMethod(FixupSharedLibrary, 'FixupSharedLibrary')
    env.AddMethod(ComposeStaticLibraries, 'ComposeStaticLibraries')
    env.AddMethod(DeleteFile, 'DeleteFile')
    env.AddMethod(DeleteDir, 'DeleteDir')
    env.AddMethod(Artifact, 'Artifact')