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

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

Enrico Pozzobon committed
10

11
def build(algo_dir, template_dir):
Enrico Pozzobon committed
12
    # create a new directory for the build
Enrico Pozzobon committed
13 14 15 16 17 18
    build_dir = None
    while build_dir is None:
        r = "%09d" % random.randint(0, 999999999)
        d = os.path.join("build", r)
        if not os.path.isdir(d):
            build_dir = d
19
    print("Building in %s" % build_dir)
Enrico Pozzobon committed
20 21
   
    # copy all the files from the submitted algorithm into the build directory
Enrico Pozzobon committed
22 23
    shutil.copytree(algo_dir, build_dir)

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

Enrico Pozzobon committed
29
    # find all c and h files, since they will be added to the makefile
30 31 32 33 34 35 36 37 38
    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
39
    # copy all the files from the template directory into the build directory
40 41 42 43 44 45 46 47 48 49
    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
50
    # prepare the environmental variables for the makefile
Enrico Pozzobon committed
51 52 53 54
    env = os.environ
    env['SRC_FILES'] = ' '.join(cfiles)
    env['HDR_FILES'] = ' '.join(hfiles)

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

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

    finally:
69 70
        sys.stdout.flush()
        sys.stderr.flush()
Enrico Pozzobon committed
71 72
        os.chdir(wd)

Enrico Pozzobon committed
73
    # if execution arrives here, the build was successful
Enrico Pozzobon committed
74 75
    return build_dir

Enrico Pozzobon committed
76 77

# Find test vectors in directory or one of the parent directories
Enrico Pozzobon committed
78 79
def find_test_vectors(d):
    kat = None
Enrico Pozzobon committed
80
    while True:
Enrico Pozzobon committed
81 82 83 84 85 86 87
        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
88 89 90 91
        if kat is None:
            d = os.path.split(d)[0]
        else:
            break
Enrico Pozzobon committed
92 93
    kat = os.path.join(d, kat)
    return kat
Enrico Pozzobon committed
94

Enrico Pozzobon committed
95

96
def main(argv):
Enrico Pozzobon committed
97
    submissions_dir = "all-lwc-submission-files"
98 99 100 101
    template_dir = "templates/linux"
    if len(argv) > 1:
        template_dir = argv[1]
    print("Using template %s" % template_dir)
Enrico Pozzobon committed
102 103
    subs = os.listdir(submissions_dir)

Enrico Pozzobon committed
104
    # get all the submissions by looking for files named "api.h"
Enrico Pozzobon committed
105
    files = []
Enrico Pozzobon committed
106 107 108
    for submission in subs:
        implementations_dir = os.path.join(submissions_dir, submission, "Implementations", "crypto_aead")

Enrico Pozzobon committed
109 110 111 112 113 114 115 116 117 118
        if not os.path.isdir(implementations_dir):
            continue

        if "NOT ACCEPTED" in implementations_dir:
            continue

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

        c = 0
Enrico Pozzobon committed
119 120 121
        # r=root, d=directories, f = files
        for r, d, f in os.walk(implementations_dir):
            for file in f:
Enrico Pozzobon committed
122
                if file == "api.h":
Enrico Pozzobon committed
123 124 125
                    f = os.path.join(r, file)
                    d = os.path.split(f)[0]
                    assert os.path.isdir(d)
Enrico Pozzobon committed
126 127 128
                    print(d)
                    t = find_test_vectors(d)
                    print(t)
Enrico Pozzobon committed
129
                    files.append((t, d))
Enrico Pozzobon committed
130 131 132 133 134 135 136
                    c += 1

        if c == 0:
            raise Exception("No implementations found")



137
    # For testing, we only do the first 1
138
    #files = files[:1]
Enrico Pozzobon committed
139

Enrico Pozzobon committed
140
    # Clear the build directory as it is a leftover from the previous execution
Enrico Pozzobon committed
141 142
    if os.path.isdir('build'):
        shutil.rmtree('build')
Enrico Pozzobon committed
143
    os.mkdir('build')
Enrico Pozzobon committed
144 145 146

    print()

Enrico Pozzobon committed
147 148 149 150
    # Write a script that executes all the tests one after the other
    test_script_path = os.path.join("build", "test_all.sh")
    with open(test_script_path, 'w') as test_script:
        test_script.write("#!/bin/sh\n")
Enrico Pozzobon committed
151 152
        test_script.write("mkdir -p logs\n")
        test_script.write("mkdir -p measurements\n")
Enrico Pozzobon committed
153 154 155
        for t, d in files:
            print()
            print(d)
156 157
            try:
                b = build(d, template_dir)
Enrico Pozzobon committed
158
                test_script.write("echo \"TESTING %s\"\n" % d)
Sebastian Renner committed
159
                test_script.write("./test.py %s %s 2> %s | tee %s\n" % 
Enrico Pozzobon committed
160 161 162 163 164 165 166
                        t,
                        os.path.join(b, 'test'),
                        os.path.join(b, 'test_stderr.log'),
                        os.path.join(b, 'test_stdout.log'))

                #./test.py all-lwc-submission-files/tinyjambu/Implementations/crypto_aead/tinyjambu192/LWC_AEAD_KAT_192_96.txt build/731759111/test 2> build/731759111/test_stderr.log | tee build/731759111/test_stdout.log

167 168 169 170
                print("COMPILATION SUCCESS FOR %s" % d)
            except Exception:
                print("COMPILATION FAILED FOR %s" % d)

Enrico Pozzobon committed
171 172
    st = os.stat(test_script_path)
    os.chmod(test_script_path, st.st_mode | stat.S_IEXEC)
Enrico Pozzobon committed
173

174 175 176 177
    print()
    print()
    print("Now execute ' %s ' to start the test" % test_script_path)

Enrico Pozzobon committed
178 179 180

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