aboutsummaryrefslogtreecommitdiff
path: root/tests/drivers/imagekit.py
blob: 6cc083fcbd743e104eb38dabd797aa53276e0da5 (plain) (blame)
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
"""shared steps for the drivers that build their own images: pick a c compiler with
#embed, and link a microcore around a genome image (little-endian 64-bit words)
for a given memory map. everything is written into the caller's directory."""
import os
import shutil
import subprocess
from pathlib import Path

ROOT = Path(__file__).resolve().parents[2]
DEFAULT_MAP = ROOT / 'build' / 'core' / 'map' / 'memmap.h'
MICRO_SOURCES = ['src/micro/micro.c', 'src/micro/micro_port.h', 'src/micro/port_linux.c',
                 'src/core/fops.h', 'src/core/bops.h', 'src/core/stack.h', 'src/core/genome.h']


def compiler():
    """the c compiler: CC, or the one src/tools/findcc.sh picks."""
    if os.environ.get('CC'):
        return os.environ['CC']
    return subprocess.run(['sh', str(ROOT / 'src/tools/findcc.sh')], check=True, capture_output=True, text=True).stdout.strip()


def micro_image(directory, image, memmap=DEFAULT_MAP, threads=True, name='image', timeout=60):
    """link a microcore in directory that boots image (bytes). memmap is the map header the
    image was built for. returns the executable's path."""
    directory = Path(directory)
    (directory / 'genome.bin').write_bytes(image)
    shutil.copy(memmap, directory / 'memmap.h')
    for f in MICRO_SOURCES:
        shutil.copy(ROOT / f, directory)
    flags = ['-pthread', '-DZY_THREADS'] if threads else []
    out = directory / name
    subprocess.run([compiler(), '-O2', '-w', '-fwrapv', *flags, str(directory / 'micro.c'), str(directory / 'port_linux.c'),
                    '-I' + str(directory), '-o', str(out)], check=True, timeout=timeout)
    return out