os-workshop

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit eefdd367c91d7a378cf452dda92455400b79d1a7
Author: Brian Swetland <swetland@frotz.net>
Date:   Fri, 22 Apr 2022 11:08:02 -0700

a simple build system

Diffstat:
A.gitignore | 1+
AMakefile | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Amake/app.mk | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 119 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +out/ diff --git a/Makefile b/Makefile @@ -0,0 +1,50 @@ +## Copyright 2022, Brian Swetland <swetland@frotz.net> +## Licensed under the Apache License, Version 2.0 + +all:: build-all + +-include local.mk + +V := @ + +XTOOLCHAIN ?= /toolchain/riscv32-11.2.0/bin/riscv32-elf- +QEMU ?= /work/app/qemu/bin/qemu-system-riscv32 + +QFLAGS := -machine virt -bios none -nographic +QFLAGS.GDB := $(QFLAGS) -gdb tcp::7777 -S + +ifeq ($(wildcard $(XTOOLCHAIN)gcc),) +$(warning Cannot find toolchain $(XTOOLCHAIN)) +$(error Please set XTOOLCHAIN in local.mk) +endif + +ifeq ($(wildcard $(QEMU)),) +$(warning Cannot find qemu-system-riscv32) +$(error Please set QEMU in local.mk) +endif + +XGCC := $(XTOOLCHAIN)gcc +XOBJDUMP := $(XTOOLCHAIN)objdump +XOBJCOPY := $(XTOOLCHAIN)objcopy + +ARCHFLAGS := -march=rv32im -mabi=ilp32 -mcmodel=medany +ARCHFLAGS += -static -nostdlib -nostartfiles -ffreestanding +ARCHFLAGS += -ffunction-sections -fdata-sections +ARCHFLAGS += -fno-builtin -fno-strict-aliasing + +LDSCRIPT := hw/simple.ld + +BUILD := out + +CFLAGS := -g -Wall -Ilibc/inc -Ihw/inc + +ALL := + +LIBC_SRC := libc/src/printf.c $(wildcard libc/src/string/*.c) + +include $(wildcard project/*.mk) + +build-all: $(ALL) + +clean:: + rm -rf $(BUILD) diff --git a/make/app.mk b/make/app.mk @@ -0,0 +1,68 @@ +## Copyright 2022, Brian Swetland <swetland@frotz.net> +## Licensed under the Apache License, Version 2.0 + +APPDIR := $(BUILD)/$(APP) +APPELF := $(BUILD)/$(APP).elf +APPLST := $(BUILD)/$(APP).lst + +ALL += $(APPELF) $(APPLST) + +# Generate objects from sources. +APPOBJ := $(patsubst %.c,$(APPDIR)/%.o,$(SRC)) +APPOBJ := $(patsubst %.S,$(APPDIR)/%.o,$(APPOBJ)) + +# Assemble compile and link flags. +APPCFLAGS := $(ARCHFLAGS) $(CFLAGS) $(patsubst %,-I%,$(APPINC)) +APPLDFLAGS := $(ARCHFLAGS) -Wl,--gc-sections + +$(shell mkdir -p $(APPDIR)) + +# Track flags in a build.opts file so we can depend on it. +# Write the file only if it does not already contain the +# same options as currently defined. +$(APPOBJ): $(APPDIR)/build.opts +$(APPELF): $(APPDIR)/build.opts + +OPTS.A := $(APPCFLAGS) $(APPLDFLAGS) +OPTS.B := $(file <$(APPDIR)/build.opts) + +ifneq ($(OPTS.A),$(OPTS.B)) +$(file >$(APPDIR)/build.opts,$(OPTS.A)) +endif + +$(APPELF): _OBJ := $(APPOBJ) +$(APPELF): _LDFLAGS := $(APPLDFLAGS) -T $(LDSCRIPT) +$(APPLIB): _LIB := -lgcc +$(APPELF): $(APPOBJ) $(LDSCRIPT) $(APPDIR)/build.opts + @$(info linking $@) + $(V)$(XGCC) $(_LDFLAGS) -o $@ $(_OBJ) $(_LIB) + +$(APPLST): $(APPELF) + $(V)$(XOBJDUMP) -D $< > $@ + +$(APPDIR)/%.o: _CFLAGS := $(APPCFLAGS) + +$(APPDIR)/%.o: %.c + @mkdir -p $(dir $@) + @$(info compiling $<) + $(V)$(XGCC) -c -o $@ $< $(_CFLAGS) -MD -MP -MT $@ -MF $(@:%o=%d) + +$(APPDIR)/%.o: %.S + @mkdir -p $(dir $@) + @$(info compiling $<) + $(V)$(XGCC) -c -o $@ $< $(_CFLAGS) -MD -MP -MT $@ -MF $(@:%o=%d) + +-include $(patsubst %.o,%.d,$(APPOBJ)) + +run.$(APP):: _BIN := $(APPELF) +run.$(APP):: $(APPELF) + $(QEMU) $(QFLAGS) -kernel $(_BIN) + +debug.$(APP):: _BIN := $(APPELF) +debug.$(APP):: $(APPELF) + $(QEMU) $(QFLAGS.GDB) -kernel $(_BIN) + +APP := +INC := +SRC := +