m1n1.loadobjs: Support loading of inline C snippets

Signed-off-by: Martin Povišer <povik@protonmail.com>
This commit is contained in:
Martin Povišer 2022-03-13 22:42:52 +01:00 committed by Hector Martin
parent 9212ac2036
commit 72c6170ee8
2 changed files with 27 additions and 4 deletions

View file

@ -123,7 +123,7 @@ TARGET_RAW := m1n1.bin
DEPDIR := build/.deps DEPDIR := build/.deps
.PHONY: all clean format update_tag update_cfg .PHONY: all clean format update_tag update_cfg invoke_cc
all: update_tag update_cfg build/$(TARGET) build/$(TARGET_RAW) all: update_tag update_cfg build/$(TARGET) build/$(TARGET_RAW)
clean: clean:
rm -rf build/* rm -rf build/*
@ -155,6 +155,10 @@ build/%.o: src/%.c
@mkdir -p "$(dir $@)" @mkdir -p "$(dir $@)"
@$(CC) -c $(CFLAGS) -MMD -MF $(DEPDIR)/$(*F).d -MQ "$@" -MP -o $@ $< @$(CC) -c $(CFLAGS) -MMD -MF $(DEPDIR)/$(*F).d -MQ "$@" -MP -o $@ $<
# special target for usage by m1n1.loadobjs
invoke_cc:
@$(CC) -c $(CFLAGS) -Isrc -o $(OBJFILE) $(CFILE)
build/$(NAME).elf: $(BUILD_OBJS) m1n1.ld build/$(NAME).elf: $(BUILD_OBJS) m1n1.ld
@echo " LD $@" @echo " LD $@"
@$(LD) -T m1n1.ld $(LDFLAGS) -o $@ $(BUILD_OBJS) @$(LD) -T m1n1.ld $(LDFLAGS) -o $@ $(BUILD_OBJS)

View file

@ -21,10 +21,11 @@ def tool_output_lines(progname, args):
yield line.decode("ascii") yield line.decode("ascii")
proc.wait() proc.wait()
if proc.returncode: if proc.returncode:
raise Exception(f"{repr(cmdline)} returned status {proc.returncode}") raise Exception(f"{repr(cmdline)} exited with status {proc.returncode}")
def run_tool(progname, args): def run_tool(progname, args, silent=False):
subprocess.check_call(_tool_cmdline(progname, args), shell=True) subprocess.check_call(_tool_cmdline(progname, args), shell=True,
stdout=subprocess.DEVNULL if silent else None)
class LinkedProgram: class LinkedProgram:
@ -156,8 +157,26 @@ class LinkedProgram:
return None, None return None, None
return self.symbols[idx] return self.symbols[idx]
def load_inline_c(self, source):
tmp = tempfile.mkdtemp()
cfile = tmp + ".c"
objfile = tmp + ".o"
with open(cfile, "w") as f:
f.write(source)
run_tool("make", f"-C {self.SOURCE_ROOT} invoke_cc "
f"OBJFILE={objfile} CFILE={cfile}", silent=True)
self.load_obj(objfile)
if __name__ == "__main__": if __name__ == "__main__":
from m1n1.setup import * from m1n1.setup import *
lp = LinkedProgram(u) lp = LinkedProgram(u)
lp.debug_printf("hello from the other side! (%d)\n", 42) lp.debug_printf("hello from the other side! (%d)\n", 42)
lp.load_inline_c('''
#include "utils.h"
int add(int a, int b) {
debug_printf("adding %d and %d\\n", a, b);
return a + b;
}
''')
print(f"1 + 2 = {lp.add(1, 2)}")