aboutsummaryrefslogtreecommitdiff
path: root/tests/drivers/imagekit.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/drivers/imagekit.py')
-rw-r--r--tests/drivers/imagekit.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/drivers/imagekit.py b/tests/drivers/imagekit.py
new file mode 100644
index 0000000..6cc083f
--- /dev/null
+++ b/tests/drivers/imagekit.py
@@ -0,0 +1,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