Skip to content

Commit

Permalink
Streamline project build process
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrik Karlsson committed Oct 26, 2023
1 parent ea85188 commit d46f5ad
Show file tree
Hide file tree
Showing 58 changed files with 337 additions and 827 deletions.
33 changes: 21 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
SUBDIRS=common kernel projects/hello projects/trapped projects/ping-pong projects/demonstrator
export ROOT=${abspath .}

all:
for i in $(SUBDIRS); do \
make -C $$i all; \
done

$(SUBDIRS):
make -C $@ all
PROJECT?=hello
PLATFORM?=qemu_virt
BUILD?=${ROOT}/build/${PROJECT}
S3K_CONF_H?=${ROOT}/projects/${PROJECT}/s3k_conf.h

include tools.mk
include common/plat/${PLATFORM}.mk

all: common kernel projects/${PROJECT}

common kernel projects/${PROJECT}:
make -C $@ all \
PLATFORM=${PLATFORM} \
BUILD=${BUILD} \
S3K_CONF_H=${S3K_CONF_H}

qemu gdb: common kernel projects/${PROJECT}
bash scripts/$@.sh ${BUILD}

clean:
for i in $(SUBDIRS); do \
make -C $$i clean; \
done
rm -rf docs
rm -r ${BUILD}

docs:
doxygen

format:
clang-format -i $(shell find -name '*.[hc]' -not -path '*/.*')

.PHONY: all docs clean $(SUBDIRS)
.PHONY: all docs clean qemu gdb kernel common projects/${PROJECT}
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ Build and Run

For building and running the `hello` project
```bash
# Build common libraries
make common
# Go to project directory and build the project
cd projects/hello
make
# Build common libraries kernel and hello project
make PROJECT=hello
# Run the program using QEMU
./scripts/qemu.sh
make qemu PROJECT=hello
```

Requirements
Expand Down
49 changes: 41 additions & 8 deletions common/Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,47 @@

PLATFORM?=${patsubst plat/%.mk, %, ${wildcard plat/*.mk}}
include plat/${PLATFORM}.mk

all: ${PLATFORM}
CFLAGS=-march=${ARCH} -mabi=${ABI} -mcmodel=${CMODEL}
CFLAGS+=-DPLATFORM_${PLATFORM}
CFLAGS+=-Os
CFLAGS+=-flto -ffat-lto-objects
CFLAGS+=-nostdlib -Iinc

${PLATFORM}:
${MAKE} -f build.mk PLATFORM=$@ all
SRCS2OBJS=${patsubst src/%.S, ${BUILD}/common/%.o, ${filter %.S, ${1}}} \
${patsubst src/%.c, ${BUILD}/common/%.o, ${filter %.c, ${1}}}

ALTC_SRCS=${wildcard src/altc/*.[cS]}
S3K_SRCS=${wildcard src/s3k/*.[cS]}
START_SRCS=${wildcard src/start/*.S}

PLAT_OBJS=${call SRCS2OBJS, ${PLAT_SRCS}}
ALTC_OBJS=${call SRCS2OBJS, ${ALTC_SRCS}}
S3K_OBJS=${call SRCS2OBJS, ${S3K_SRCS}}
START_OBJS=${call SRCS2OBJS, ${START_SRCS}}

all: ${BUILD}/common/libplat.a ${BUILD}/common/libaltc.a ${BUILD}/common/libs3k.a ${START_OBJS}

${BUILD}/common/libplat.a: ${PLAT_OBJS}
echo ${PLAT_OBJS}
@mkdir -p ${@D}
${AR} cr $@ $^

${BUILD}/common/libaltc.a: ${ALTC_OBJS}
@mkdir -p ${@D}
${AR} cr $@ $^

${BUILD}/common/libs3k.a: ${S3K_OBJS}
@mkdir -p ${@D}
${AR} cr $@ $^

${BUILD}/common/%.o: src/%.c
@mkdir -p ${@D}
${CC} -o $@ $< -c ${CFLAGS}

${BUILD}/common/%.o: src/%.S
@mkdir -p ${@D}
${CC} -o $@ $< -c ${CFLAGS}
clean:
for i in ${PLATFORM}; do \
${MAKE} -f build.mk PLATFORM=$$i clean; \
done
rm -rf ${BUILD}

.PHONY: all
.PHONY: all clean
48 changes: 0 additions & 48 deletions common/build.mk

This file was deleted.

14 changes: 8 additions & 6 deletions common/plat/qemu_virt.mk
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
ARCH=rv64imac_zicsr
ABI=lp64
CMODEL=medany
COMMON_INC=${ROOT}/common/inc
COMMON_LIB=${ROOT}/common/plat/qemu_virt
STARTFILES=${ROOT}/common/plat/qemu_virt/start
export ARCH=rv64imac_zicsr
export ABI=lp64
export CMODEL=medany
export QEMU_MACHINE=virt
export QEMU_SMP=4
export COMMON_INC=${ROOT}/common/inc
export COMMON_LIB=${BUILD}/common
export STARTFILES=${BUILD}/common/start
PLAT_SRCS=src/drivers/uart/ns16550a.c src/drivers/time.c
14 changes: 8 additions & 6 deletions common/plat/sifive_unleashed.mk
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
ARCH=rv64imac_zicsr
ABI=lp64
CMODEL=medany
COMMON_INC=${ROOT}/common/inc
COMMON_LIB=${ROOT}/common/plat/sifive_unleashed
STARTFILES=${ROOT}/common/plat/sifive_unleashed/start
export ARCH=rv64imac_zicsr
export ABI=lp64
export CMODEL=medany
export QEMU_MACHINE=sifive_u
export QEMU_SMP=5
export COMMON_INC=${ROOT}/common/inc
export COMMON_LIB=${BUILD}/common
export STARTFILES=${BUILD}/common/start
PLAT_SRCS=src/drivers/uart/sifive.c src/drivers/time.c
42 changes: 4 additions & 38 deletions kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,11 @@
.POSIX:

# Kernel basename
PROGRAM?=s3k

# Kernel config
S3K_CONF_H?=s3k_conf.h

# Platform config
PLATFORM?=qemu_virt

# Repository root
ROOT=..

# Build target
BUILD?=build

# Get toolchain
include ${ROOT}/tools.mk

# Platform specific config
include ${ROOT}/common/plat/${PLATFORM}.mk
PROGRAM?=kernel

# CC flags
CFLAGS=-march=${ARCH} -mabi=${ABI} -mcmodel=${CMODEL}
CFLAGS+=-DPLATFORM_${PLATFORM}
CFLAGS+=-std=c11
CFLAGS+=-Os -g3
CFLAGS+=-Wall -Wextra -Werror
Expand All @@ -32,7 +15,6 @@ CFLAGS+=-Wshadow -fno-common
CFLAGS+=-Wno-builtin-declaration-mismatch
CFLAGS+=-fno-stack-protector
CFLAGS+=-flto
CFLAGS+=-DPLATFORM_${PLATFORM}

# Include files and directories
CFLAGS+=-include ${S3K_CONF_H}
Expand All @@ -53,7 +35,7 @@ LDFLAGS+=-laltc -lplat
# Source files
S_SRCS=${wildcard src/*.S}
C_SRCS=${wildcard src/*.c}

# Object files
OBJS=${patsubst src/%.S, ${BUILD}/${PROGRAM}/%.o, ${S_SRCS}} \
${patsubst src/%.c, ${BUILD}/${PROGRAM}/%.o, ${C_SRCS}}
Expand All @@ -63,14 +45,10 @@ DEPS=${patsubst %.o, ${BUILD}/obj/%.d, ${OBJS}}

# Targets
ELF=${BUILD}/${PROGRAM}.elf
BIN=${ELF:.elf=.bin}
DA=${ELF:.elf=.da}

all: ${ELF} ${BIN} ${DA}
all: ${ELF}

elf: ${ELF}
bin: ${BIN}
da: ${DA}

${BUILD}/${PROGRAM}/%.o: src/%.S
@mkdir -p ${@D}
Expand All @@ -84,18 +62,6 @@ ${ELF}: ${OBJS}
@mkdir -p ${@D}
${CC} -o $@ ${OBJS} ${LDFLAGS}

${BIN}: ${ELF}
${OBJCOPY} -O binary $< $@

${DA}: ${ELF}
${OBJDUMP} -D $< > $@

format:
clang-format -i $(shell find -name '*.[hc]' -not -path '*/.*')

clean:
rm -rf $(BUILD)

.PHONY: all elf bin da format clean

-include ${DEPS}
2 changes: 1 addition & 1 deletion kernel/inc/kassert.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#define _X_(x) #x
#define KASSERT_FAILURE(FILE, LINE) \
altio_puts("Kernel assertion failed at " FILE ":" _X_(LINE) ".");
alt_puts("Kernel assertion failed at " FILE ":" _X_(LINE) ".");

#define KASSERT(EXPR) \
do { \
Expand Down
24 changes: 0 additions & 24 deletions kernel/s3k_conf.h

This file was deleted.

21 changes: 5 additions & 16 deletions projects/demonstrator/Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
BUILD ?=build
S3K_CONF_H ?=s3k_conf.h
APPS=boot monitor uartppp crypto app0 app1

SUBDIRS=../../kernel boot monitor uartppp crypto app0 app1

all: ${SUBDIRS}
all: ${APPS}

boot: monitor uartppp crypto

${SUBDIRS}:
@${MAKE} -C $@ all \
BUILD=${abspath ${BUILD}} \
S3K_CONF_H=${abspath ${S3K_CONF_H}}

qemu gdb: all
./scripts/$@.sh

clean:
rm -rf ${BUILD}
${APPS}:
@${MAKE} -f build.mk PROGRAM=$@ all

.PHONY: all clean ${SUBDIRS} qemu gdb
.PHONY: all ${APPS}
5 changes: 0 additions & 5 deletions projects/demonstrator/app0/Makefile

This file was deleted.

5 changes: 0 additions & 5 deletions projects/demonstrator/app1/Makefile

This file was deleted.

12 changes: 0 additions & 12 deletions projects/demonstrator/boot/Makefile

This file was deleted.

8 changes: 1 addition & 7 deletions projects/demonstrator/boot/main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "../config.h"
#include "drivers/uart.h"
#include "s3k/s3k.h"
#include "s3klib/altio.h"
#include "altc/altio.h"
#include "string.h"

#include <stddef.h>
Expand Down Expand Up @@ -197,11 +196,6 @@ void main(void)
S3K_MEM_RW);
s3k_pmp_load(5, 1);
s3k_sync_mem();
uart_init();
while(1) {
char c = alt_getchar();
alt_putchar(c);
}

alt_puts("setting up memory ...");
/* Copy binary of monitor process, setup PMP and program counter. */
Expand Down
Loading

0 comments on commit d46f5ad

Please sign in to comment.