File: test-features.py

package info (click to toggle)
simdutf 7.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,244 kB
  • sloc: cpp: 60,074; ansic: 14,226; python: 3,364; sh: 321; makefile: 12
file content (262 lines) | stat: -rwxr-xr-x 9,181 bytes parent folder | download
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
#!/usr/bin/env python3
from pathlib import Path
from io import StringIO
import os


TMP = Path("/dev/shm/")
if not TMP.exists():
    TMP = Path(os.environ.get('TMP', '.'))


def main():
    compilers = [
        ('default', 'c++', [])
    ]

    crosscompilers = find_crosscompilers()
    if crosscompilers:
        print("Found the following crosscompilers in $PATH:")
        for arch, compiler, opts in crosscompilers:
            print('%-16s: %s' % (arch, compiler))

        compilers.extend(crosscompilers)

    make = create_make(compilers)

    file = Path('Makefile')
    update_file(make, file)


def create_make(compilers):
    f = StringIO()

    def writeln(s):
        f.write(s + '\n')

    writeln("SRC=../src")
    writeln("INC=../include")

    archs = []
    for arch, compiler, opts in compilers:
        targets = []
        for features in feature_combinations:
            parts = [arch] + name_for_features(features)

            target_dir = TMP / '-'.join(parts)
            target_obj = target_dir / 'simdutf.o'
            target_exe = target_dir / 'a.out'

            targets.append((target_obj, target_exe, features))

        archs.append((arch, compiler, opts, targets))

        writeln('')
        writeln(f"{arch.upper()}=\\")
        for (target_obj, target_exe, _) in targets:
            writeln(f'\t{target_exe} {target_obj}\\')

    # first add 'all' target
    writeln('')
    writeln('.PHONY: all')
    writeln('all: %s' % (' '.join(arch for arch, _, _ in compilers)))
    writeln('')
    writeln('.PHONY: all')
    writeln('clean:')
    writeln('\t$(RM) %s' % ' '.join(f"$({arch.upper()})" for arch, _, _ in compilers))

    # add 'help' target
    writeln('')
    writeln(".PHONY: help")
    writeln("help:")
    width = max((len(arch) for arch, _, _ in compilers))
    width = max(width, len("all"), len("clear"))

    target = "all"
    writeln(f'\t@echo "make {target:{width}} --- build all targets"')
    target = "clear"
    writeln(f'\t@echo "make {target:{width}} --- remove all generated files"')
    target = "help"
    writeln(f'\t@echo "make {target:{width}} --- show this help"')
    writeln('\t@echo')
    for arch, compiler, _ in compilers:
        writeln(f'\t@echo "make {arch:{width}} --- build using {compiler}"')

    # add convienent targets for architectures ('default' is the first)
    for arch, _, _ in compilers:
        writeln('')
        writeln(f'.PHONY: {arch}')
        writeln(f'{arch}: $({arch.upper()})')

    # add individual targets
    for arch, compiler, opts, targets in archs:
        compiler_opts   = ' '.join(opts)
        for target_obj, target_exe, features in targets:
            amalgamate_opts = ' '.join(feature2option[feat] for feat in features)

            target_dir = target_obj.parent

            writeln('')
            writeln(f"{target_obj}: amalgamate.py")
            writeln(f"\tmkdir -p {target_dir}")
            writeln(f"\tpython3 amalgamate.py --no-zip --no-readme --source-dir=$(SRC) --include-dir=$(INC) --output {target_dir} {amalgamate_opts}")
            writeln(f"\tcd {target_dir} && {compiler} {compiler_opts} -c simdutf.cpp")
            writeln('')
            writeln(f"{target_exe}: {target_obj}")
            writeln(f"\tcd {target_dir} && {compiler} {compiler_opts} amalgamation_demo.cpp")

    return f.getvalue()


def update_file(contents, path):
    if path.exists():
        if path.read_text() == contents:
            return

        print(f"updating {path}")
    else:
        print(f"creating {path}")

    path.write_text(contents)


def find_crosscompilers():
    return list(find_crosscompilers_aux())


def find_crosscompilers_aux():
    found = set()
    for path in os.environ['PATH'].split(':'):
        path = Path(path)
        gxx = glob_many(path, ['*-g++*', '*-c++*', '*clang++'])
        for item in crosscompilers:
            if isinstance(item, str):
                arch = item
                name = arch
                opts = []
            elif isinstance(item, tuple):
                assert len(item) == 3, item
                arch, name, opts = item
            else:
                assert False, item

            if name in found:
                continue

            for filename in gxx:
                if is_compiler(arch, ['g++', 'c++', 'clang++'], filename):
                    yield (name, filename, opts)
                    found.add(name)
                    break


def glob_many(rootdir, patterns):
    tmp = []
    for pat in patterns:
        tmp.extend([file.name for file in rootdir.glob(pat)])

    tmp.sort()
    return tmp


def is_compiler(arch, compilers, name):
    # we're looking for "arch-foo-bar-g++" or "arch-foo-bar-g++-version"
    tmp = name.split('-')
    if tmp[0] != arch:
        return False

    if tmp[-1] in compilers:
        return True

    if len(tmp) >= 3 and tmp[-2] in compilers and is_number(tmp[-1]):
        return True


def is_number(s):
    try:
        _ = int(s)
        return True
    except ValueError:
        return False


def name_for_features(features):
    return [feature2stem[feat] for feat in features]


SIMDUTF_FEATURE_DETECT_ENCODING = 'SIMDUTF_FEATURE_DETECT_ENCODING'
SIMDUTF_FEATURE_LATIN1          = 'SIMDUTF_FEATURE_LATIN1'
SIMDUTF_FEATURE_ASCII           = 'SIMDUTF_FEATURE_ASCII'
SIMDUTF_FEATURE_BASE64          = 'SIMDUTF_FEATURE_BASE64'
SIMDUTF_FEATURE_UTF8            = 'SIMDUTF_FEATURE_UTF8'
SIMDUTF_FEATURE_UTF16           = 'SIMDUTF_FEATURE_UTF16'
SIMDUTF_FEATURE_UTF32           = 'SIMDUTF_FEATURE_UTF32'


feature2stem = {
    SIMDUTF_FEATURE_DETECT_ENCODING : 'de',
    SIMDUTF_FEATURE_LATIN1          : 'lat1',
    SIMDUTF_FEATURE_ASCII           : 'ascii',
    SIMDUTF_FEATURE_BASE64          : 'base64',
    SIMDUTF_FEATURE_UTF8            : 'utf8',
    SIMDUTF_FEATURE_UTF16           : 'utf16',
    SIMDUTF_FEATURE_UTF32           : 'utf32',
}

feature2option = {
    SIMDUTF_FEATURE_DETECT_ENCODING : '--with-detect-enc',
    SIMDUTF_FEATURE_LATIN1          : '--with-latin1',
    SIMDUTF_FEATURE_ASCII           : '--with-ascii',
    SIMDUTF_FEATURE_BASE64          : '--with-base64',
    SIMDUTF_FEATURE_UTF8            : '--with-utf8',
    SIMDUTF_FEATURE_UTF16           : '--with-utf16',
    SIMDUTF_FEATURE_UTF32           : '--with-utf32',
}

feature_combinations = [
    [SIMDUTF_FEATURE_DETECT_ENCODING],
    [SIMDUTF_FEATURE_ASCII],
    [SIMDUTF_FEATURE_UTF8],
    [SIMDUTF_FEATURE_UTF16],
    [SIMDUTF_FEATURE_UTF32],
    [SIMDUTF_FEATURE_BASE64],
    [SIMDUTF_FEATURE_UTF8, SIMDUTF_FEATURE_LATIN1],
    [SIMDUTF_FEATURE_UTF16, SIMDUTF_FEATURE_LATIN1],
    [SIMDUTF_FEATURE_UTF32, SIMDUTF_FEATURE_LATIN1],
    [SIMDUTF_FEATURE_UTF8, SIMDUTF_FEATURE_LATIN1, SIMDUTF_FEATURE_DETECT_ENCODING],
    [SIMDUTF_FEATURE_UTF16, SIMDUTF_FEATURE_LATIN1, SIMDUTF_FEATURE_DETECT_ENCODING],
    [SIMDUTF_FEATURE_UTF32, SIMDUTF_FEATURE_LATIN1, SIMDUTF_FEATURE_DETECT_ENCODING],
    [SIMDUTF_FEATURE_UTF8, SIMDUTF_FEATURE_LATIN1, SIMDUTF_FEATURE_DETECT_ENCODING, SIMDUTF_FEATURE_ASCII],
    [SIMDUTF_FEATURE_UTF16, SIMDUTF_FEATURE_LATIN1, SIMDUTF_FEATURE_DETECT_ENCODING, SIMDUTF_FEATURE_ASCII],
    [SIMDUTF_FEATURE_UTF32, SIMDUTF_FEATURE_LATIN1, SIMDUTF_FEATURE_DETECT_ENCODING, SIMDUTF_FEATURE_ASCII],
    [SIMDUTF_FEATURE_UTF8, SIMDUTF_FEATURE_UTF16],
    [SIMDUTF_FEATURE_UTF16, SIMDUTF_FEATURE_UTF32],
    [SIMDUTF_FEATURE_UTF32, SIMDUTF_FEATURE_UTF8],
    [SIMDUTF_FEATURE_UTF32, SIMDUTF_FEATURE_UTF16],
    [SIMDUTF_FEATURE_UTF32, SIMDUTF_FEATURE_UTF16, SIMDUTF_FEATURE_UTF8],
    [SIMDUTF_FEATURE_UTF8, SIMDUTF_FEATURE_UTF16, SIMDUTF_FEATURE_LATIN1],
    [SIMDUTF_FEATURE_UTF16, SIMDUTF_FEATURE_UTF32, SIMDUTF_FEATURE_LATIN1],
    [SIMDUTF_FEATURE_UTF32, SIMDUTF_FEATURE_UTF8, SIMDUTF_FEATURE_LATIN1],
    [SIMDUTF_FEATURE_UTF32, SIMDUTF_FEATURE_UTF16, SIMDUTF_FEATURE_LATIN1],
    [SIMDUTF_FEATURE_UTF32, SIMDUTF_FEATURE_UTF16, SIMDUTF_FEATURE_UTF8, SIMDUTF_FEATURE_LATIN1],
    [SIMDUTF_FEATURE_BASE64, SIMDUTF_FEATURE_DETECT_ENCODING],
    [SIMDUTF_FEATURE_BASE64, SIMDUTF_FEATURE_DETECT_ENCODING, SIMDUTF_FEATURE_ASCII],
    [SIMDUTF_FEATURE_BASE64, SIMDUTF_FEATURE_DETECT_ENCODING, SIMDUTF_FEATURE_ASCII, SIMDUTF_FEATURE_UTF8],
    [SIMDUTF_FEATURE_BASE64, SIMDUTF_FEATURE_DETECT_ENCODING, SIMDUTF_FEATURE_ASCII, SIMDUTF_FEATURE_UTF16],
    [SIMDUTF_FEATURE_BASE64, SIMDUTF_FEATURE_DETECT_ENCODING, SIMDUTF_FEATURE_ASCII, SIMDUTF_FEATURE_UTF32],
    [SIMDUTF_FEATURE_BASE64, SIMDUTF_FEATURE_DETECT_ENCODING, SIMDUTF_FEATURE_ASCII, SIMDUTF_FEATURE_UTF8, SIMDUTF_FEATURE_UTF16],
    [SIMDUTF_FEATURE_BASE64, SIMDUTF_FEATURE_DETECT_ENCODING, SIMDUTF_FEATURE_ASCII, SIMDUTF_FEATURE_UTF8, SIMDUTF_FEATURE_UTF32],
    [SIMDUTF_FEATURE_BASE64, SIMDUTF_FEATURE_DETECT_ENCODING, SIMDUTF_FEATURE_ASCII, SIMDUTF_FEATURE_UTF16, SIMDUTF_FEATURE_UTF32],
    [SIMDUTF_FEATURE_BASE64, SIMDUTF_FEATURE_DETECT_ENCODING, SIMDUTF_FEATURE_ASCII, SIMDUTF_FEATURE_UTF8, SIMDUTF_FEATURE_UTF16, SIMDUTF_FEATURE_UTF32],
]

crosscompilers = [
    'aarch64',
    'powerpc64',
    'loongarch64',
    ('loongarch64', 'loongarch64lasx', ["-mlsx", "-mlasx"]),
    ('riscv64', 'riscv64', ["-march=rv64gv"]),
]

if __name__ == '__main__':
    main()