ch32v-template/Makefile

81 lines
2.3 KiB
Makefile

.PHONY: \
all rb gdb \
lall lrb lgdb \
clean flash openocd shell wipe
DOCKER_IMAGE ?= riscv-ubuntu
DOCKER_CMD ?= docker run --rm -it -v ${PWD}:/project
DOCKER_TEMP ?= docker-temp-$(shell date +%s)
LOCALHOST ?= localhost
MESON_FILE := meson.build
MESON_CROSS ?= meson_cross_riscv.txt
MESON_TARGET ?= main
MESON_DIR ?= mbuild_${MESON_TARGET}
OPENOCD_DIR ?= $(dir $(shell command -v openocd))/..
TRANSLATE_COMPILE_COMMANDS := \
sed -i -e "s%"'"'"/project/%"'"'"$$(readlink -f .)/%" ${MESON_DIR}/compile_commands.json ; \
sed -i -e "s%riscv32-unknown-elf-gcc %\0 -isystem=$$(readlink -f ./.docker_includes) %" ${MESON_DIR}/compile_commands.json
all: .have_docker
${DOCKER_CMD} ${DOCKER_IMAGE} make MESON_TARGET=${MESON_TARGET} lall
${TRANSLATE_COMPILE_COMMANDS}
wipe: clean all
lall: ${MESON_DIR}
meson compile -C ${MESON_DIR}
${MESON_DIR}: ${MESON_FILE} ${MESON_CROSS}
meson setup $@ --cross-file -D build_target=${MESON_TARGET} ${MESON_CROSS}
rb: .have_docker
${DOCKER_CMD} ${DOCKER_IMAGE} make MESON_TARGET=${MESON_TARGET} lrb
${TRANSLATE_COMPILE_COMMANDS}
lrb:
rm -rf ${MESON_DIR}
meson setup ${MESON_DIR} --cross-file ${MESON_CROSS} -D build_target=${MESON_TARGET}
meson compile -C ${MESON_DIR}
clean:
rm -rf ${MESON_DIR}
rm -f .have_docker
rm -rf .docker_includes
shell: .have_docker
${DOCKER_CMD} ${DOCKER_IMAGE} bash
flash: all
${OPENOCD_DIR}/bin/openocd \
-s ${OPENOCD_DIR}/scripts \
-f ./wch-riscv.cfg \
-c init -c halt \
-c "program $(shell readlink -f ${MESON_DIR}/firmware-for-make.hex) verify reset" \
-c wlink_reset_resume -c exit
openocd:
${OPENOCD_DIR}/bin/openocd \
-s ${OPENOCD_DIR}/scripts \
-f ./wch-riscv.cfg \
-c init
gdb: .have_docker
${DOCKER_CMD} --add-host=host.docker.internal:host-gateway \
-e COLUMNS=$(shell tput cols) -e LINES=$(shell tput lines) -e TERM \
${DOCKER_IMAGE} make LOCALHOST=host.docker.internal lgdb
lgdb:
cd ${MESON_DIR} && \
riscv32-unknown-elf-gdb ./firmware-for-make.elf -ex "tar ext ${LOCALHOST}:3333"
.have_docker: Dockerfile
docker build -t ${DOCKER_IMAGE} \
--build-arg uid=$(shell id -u) --build-arg gid=$(shell id -g) \
.
docker create --name ${DOCKER_TEMP} ${DOCKER_IMAGE}
docker cp ${DOCKER_TEMP}:/usr/local/riscv32-unknown-elf/include .docker_includes
docker rm ${DOCKER_TEMP}
touch $@