compile_all.py 6.45 KB
Newer Older
Enrico Pozzobon committed
1 2 3 4
#!/usr/bin/env python3

import os
import sys
Enrico Pozzobon committed
5
import stat
6
import argparse
Enrico Pozzobon committed
7
import shutil
Enrico Pozzobon committed
8
import random
Enrico Pozzobon committed
9
import subprocess
10
import shutil
Enrico Pozzobon committed
11

Enrico Pozzobon committed
12

Enrico Pozzobon committed
13
def build(algo_dir, template_dir, build_dir):
14 15 16
    if os.path.isdir(build_dir):
        return None

17
    print("Building in %s" % build_dir)
Enrico Pozzobon committed
18 19
   
    # copy all the files from the submitted algorithm into the build directory
Enrico Pozzobon committed
20 21
    shutil.copytree(algo_dir, build_dir)

Enrico Pozzobon committed
22
    # remove the test vectors generator if it is there
Enrico Pozzobon committed
23 24 25 26
    c = os.path.join(build_dir, "genkat_aead.c")
    if os.path.exists(c):
        os.remove(c)

Enrico Pozzobon committed
27
    # find all c and h files, since they will be added to the makefile
28 29 30 31 32 33 34 35 36
    hfiles = []
    cfiles = []
    for r, d, f in os.walk(build_dir):
        for file in f:
            if file.endswith(".c"):
                cfiles.append(file)
            elif file.endswith(".h"):
                hfiles.append(file)

Enrico Pozzobon committed
37
    # copy all the files from the template directory into the build directory
38 39 40 41 42 43 44 45 46 47
    for f in os.listdir(template_dir):
        dst = os.path.join(build_dir, f)
        src = os.path.join(template_dir, f)
        if os.path.isfile(src) or os.path.islink(src):
            shutil.copy2(src, dst)
        elif os.path.isdir(src):
            shutil.copytree(src, dst)
        else:
            raise Exception("I don't know what %s is" % src)

Enrico Pozzobon committed
48
    # prepare the environmental variables for the makefile
Enrico Pozzobon committed
49 50 51 52
    env = os.environ
    env['SRC_FILES'] = ' '.join(cfiles)
    env['HDR_FILES'] = ' '.join(hfiles)

Enrico Pozzobon committed
53
    # enter the directory and execute the makefile
Enrico Pozzobon committed
54
    wd = os.getcwd()
Enrico Pozzobon committed
55
    os.chdir(build_dir)
Enrico Pozzobon committed
56
    try:
57
        if os.path.isfile('./configure'):
Enrico Pozzobon committed
58 59 60
            p = subprocess.Popen(["./configure"])
            p.wait()
            assert p.returncode == 0
61

Enrico Pozzobon committed
62 63 64
        p = subprocess.Popen(['make'])
        p.wait()
        assert p.returncode == 0
Enrico Pozzobon committed
65

66 67 68 69 70 71
        if os.path.isfile('./cleanup'):
            p = subprocess.Popen(["./cleanup"])
            p.wait()
            assert p.returncode == 0


Enrico Pozzobon committed
72
    finally:
73 74
        sys.stdout.flush()
        sys.stderr.flush()
Enrico Pozzobon committed
75 76
        os.chdir(wd)

Enrico Pozzobon committed
77
    # if execution arrives here, the build was successful
Enrico Pozzobon committed
78 79
    return build_dir

Enrico Pozzobon committed
80 81

# Find test vectors in directory or one of the parent directories
Enrico Pozzobon committed
82 83
def find_test_vectors(d):
    kat = None
Enrico Pozzobon committed
84
    while True:
Enrico Pozzobon committed
85 86 87 88 89 90 91
        if d == '':
            raise Exception("Test vector not found")
        for f in os.listdir(d):
            if f.startswith("LWC_AEAD_KAT_") and f.endswith(".txt"):
                if kat is not None:
                    raise Exception("Multiple test vectors?")
                kat = f
Enrico Pozzobon committed
92 93 94 95
        if kat is None:
            d = os.path.split(d)[0]
        else:
            break
Enrico Pozzobon committed
96 97
    kat = os.path.join(d, kat)
    return kat
Enrico Pozzobon committed
98

Enrico Pozzobon committed
99

100
def main(argv):
Enrico Pozzobon committed
101
    submissions_dir = "all-lwc-submission-files"
102
    include_list = None
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

    # Parse the arguments
    argparser = argparse.ArgumentParser(
	description='Compiles all LWC submissions for a given template')

    argparser.add_argument('-v', '--verbose', action='count')
    argparser.add_argument('-t', '--template', default='templates/linux')
    argparser.add_argument('-b', '--build-dir')
    argparser.add_argument('-i', '--include', action='append')

    args = argparser.parse_args(argv[1:])
    template_dir = args.template
    build_root_dir = args.build_dir

    include_list = args.include

119
    print("Using template %s" % template_dir)
Enrico Pozzobon committed
120 121
    subs = os.listdir(submissions_dir)

Enrico Pozzobon committed
122
    # get all the submissions by looking for files named "api.h"
123
    subfiles = []
Enrico Pozzobon committed
124 125 126
    for submission in subs:
        implementations_dir = os.path.join(submissions_dir, submission, "Implementations", "crypto_aead")

Enrico Pozzobon committed
127 128 129 130 131 132 133 134 135 136
        if not os.path.isdir(implementations_dir):
            continue

        if "NOT ACCEPTED" in implementations_dir:
            continue

        print()
        print("###  %s  ###" % submission)

        c = 0
Enrico Pozzobon committed
137 138 139
        # r=root, d=directories, f = files
        for r, d, f in os.walk(implementations_dir):
            for file in f:
Enrico Pozzobon committed
140
                if file == "api.h":
Enrico Pozzobon committed
141
                    f = os.path.join(r, file)
142 143 144 145 146
                    subfiles.append(f)
                    c += 1

        if c == 0:
            raise Exception("No implementations found")
Enrico Pozzobon committed
147

148 149
    if include_list is not None:
        print("Include list has %d entries" % len(include_list))
Enrico Pozzobon committed
150

151 152
    files = []
    for f in subfiles:
Enrico Pozzobon committed
153

154 155 156 157
        # Source directory d
        d = os.path.split(f)[0]
        assert os.path.isdir(d)
        print(d)
Enrico Pozzobon committed
158

159 160 161
        # Test vectors file t
        t = find_test_vectors(d)
        print(t)
Enrico Pozzobon committed
162

163 164 165 166 167 168 169 170 171 172 173 174
        # base name n
        pieces = f.split(os.sep)
        n = pieces[1] + "." + ".".join(pieces[4:-1])
        print(n)
        
        # if include_list was provided, skip elements not in the list
        if include_list is not None:
            if not n in include_list:
                continue

        # Put all in a tuple and count
        files.append((t, d, n))
Enrico Pozzobon committed
175 176 177



178
    # For testing, we only do the first 1
179
    #files = files[:1]
180
    print("%d algorithms will be compiled" % len(files))
Enrico Pozzobon committed
181

182 183
    if not os.path.isdir(build_root_dir):
        os.mkdir(build_root_dir)
Enrico Pozzobon committed
184 185 186

    print()

Enrico Pozzobon committed
187
    # Write a script that executes all the tests one after the other
188
    test_script_path = os.path.join(build_root_dir, "test_all.sh")
Enrico Pozzobon committed
189 190
    with open(test_script_path, 'w') as test_script:
        test_script.write("#!/bin/sh\n")
Enrico Pozzobon committed
191 192
        test_script.write("mkdir -p logs\n")
        test_script.write("mkdir -p measurements\n")
Enrico Pozzobon committed
193
        for i, (t, d, name) in enumerate(files):
Enrico Pozzobon committed
194 195
            print()
            print(d)
196
            try:
197
                build_dir = os.path.join(build_root_dir, name)
Enrico Pozzobon committed
198
                b = build(d, template_dir, build_dir)
199 200
                if b is None:
                    continue
Enrico Pozzobon committed
201
                test_script.write("\n\necho \"TEST NUMBER %03d: TESTING %s\"\n" % (i, d))
202
                test_script.write("python3 -u ./test.py %s %s 2> %s | tee %s\n" % (
Enrico Pozzobon committed
203 204 205 206
                        t,
                        os.path.join(b, 'test'),
                        os.path.join(b, 'test_stderr.log'),
                        os.path.join(b, 'test_stdout.log'))
207
                )
208 209
                shutil.copyfile(t, os.path.join(b, 'LWC_AEAD_KAT.txt'))
                
Enrico Pozzobon committed
210

211
                print("COMPILATION SUCCESS FOR %s" % d)
Enrico Pozzobon committed
212
            except Exception as ex:
213
                print("COMPILATION FAILED FOR %s" % d)
Enrico Pozzobon committed
214
                print(ex)
215

Enrico Pozzobon committed
216 217
    st = os.stat(test_script_path)
    os.chmod(test_script_path, st.st_mode | stat.S_IEXEC)
Enrico Pozzobon committed
218

219 220 221 222
    print()
    print()
    print("Now execute ' %s ' to start the test" % test_script_path)

Enrico Pozzobon committed
223 224 225

if __name__ == "__main__":
    sys.exit(main(sys.argv))