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

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

Enrico Pozzobon committed
9
def build(algo_dir, template_dir="templates/linux"):
Enrico Pozzobon committed
10 11 12 13 14 15 16
    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
    
Enrico Pozzobon committed
17 18 19 20 21 22
    shutil.copytree(algo_dir, build_dir)

    c = os.path.join(build_dir, "genkat_aead.c")
    if os.path.exists(c):
        os.remove(c)

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    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)

    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
42 43 44 45
    env = os.environ
    env['SRC_FILES'] = ' '.join(cfiles)
    env['HDR_FILES'] = ' '.join(hfiles)

Enrico Pozzobon committed
46 47
    wd = os.getcwd()
    try:
48 49
        os.chdir(build_dir)
        if os.path.isfile('./configure'):
Enrico Pozzobon committed
50 51 52
            p = subprocess.Popen(["./configure"])
            p.wait()
            assert p.returncode == 0
53 54

        pargs = ['make']
Enrico Pozzobon committed
55 56 57
        p = subprocess.Popen(['make'])
        p.wait()
        assert p.returncode == 0
Enrico Pozzobon committed
58 59 60 61

    finally:
        os.chdir(wd)

Enrico Pozzobon committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    return build_dir

def find_test_vectors(d):
    kat = None
    while kat is None:
        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
        d = os.path.split(d)[0]
    kat = os.path.join(d, kat)
    return kat
Enrico Pozzobon committed
77

78
def main(argv):
Enrico Pozzobon committed
79
    submissions_dir = "all-lwc-submission-files"
80 81 82 83
    template_dir = "templates/linux"
    if len(argv) > 1:
        template_dir = argv[1]
    print("Using template %s" % template_dir)
Enrico Pozzobon committed
84 85
    subs = os.listdir(submissions_dir)

Enrico Pozzobon committed
86 87
    # get all the submissions by looking for files named "encrypt.c"
    files = []
Enrico Pozzobon committed
88 89 90
    for submission in subs:
        implementations_dir = os.path.join(submissions_dir, submission, "Implementations", "crypto_aead")

Enrico Pozzobon committed
91 92 93 94 95 96 97 98 99 100
        if not os.path.isdir(implementations_dir):
            continue

        if "NOT ACCEPTED" in implementations_dir:
            continue

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

        c = 0
Enrico Pozzobon committed
101 102 103
        # r=root, d=directories, f = files
        for r, d, f in os.walk(implementations_dir):
            for file in f:
Enrico Pozzobon committed
104
                if file == "api.h":
Enrico Pozzobon committed
105 106 107
                    f = os.path.join(r, file)
                    d = os.path.split(f)[0]
                    assert os.path.isdir(d)
Enrico Pozzobon committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
                    print(d)
                    t = find_test_vectors(d)
                    print(t)
                    files.append(d)
                    c += 1

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



    # For testing, we only do the first
    files = files[:1]

    if os.path.isdir('build'):
        shutil.rmtree('build')

    print()

    for d in files:
Enrico Pozzobon committed
128
        print()
Enrico Pozzobon committed
129 130
        print(d)
        b = build(d)
Enrico Pozzobon committed
131 132 133 134


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