Skip to content

Commit

Permalink
Merge pull request #352 from mithro/makefiles-fixes
Browse files Browse the repository at this point in the history
Makefiles fixes
  • Loading branch information
mithro committed Aug 17, 2017
2 parents 4242bb5 + a09c6ea commit ed6c81b
Show file tree
Hide file tree
Showing 17 changed files with 268 additions and 131 deletions.
162 changes: 136 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,40 @@ endif

# Include platform specific targets
include targets/$(PLATFORM)/Makefile.mk
TARGET ?= $(DEFAULT_TARGET)
ifeq ($(TARGET),)
$(error "Internal error: TARGET not set.")
endif
export TARGET

DEFAULT_CPU = lm32
CPU ?= $(DEFAULT_CPU)
ifeq ($(CPU),)
$(error "Internal error: CPU not set.")
endif
export CPU

FIRMWARE ?= firmware

# We don't use CLANG
CLANG = 0
export CLANG

ifeq ($(TOFE_BOARD),)
ifeq ($(PLATFORM_EXPANSION),)
FULL_PLATFORM = $(PLATFORM)
else
FULL_PLATFORM = $(PLATFORM).$(TOFE_BOARD)
LITEX_EXTRA_CMDLINE += -Ot tofe_board $(TOFE_BOARD)
FULL_PLATFORM = $(PLATFORM).$(PLATFORM_EXPANSION)
LITEX_EXTRA_CMDLINE += -Ot expansion $(PLATFORM_EXPANSION)
endif
TARGET_BUILD_DIR = build/$(FULL_PLATFORM)_$(TARGET)_$(CPU)/

IPRANGE ?= 192.168.100
export IPRANGE
GATEWARE_FILEBASE = $(TARGET_BUILD_DIR)/gateware/top
BIOS_FILE = $(TARGET_BUILD_DIR)/software/bios/bios.bin
FIRMWARE_FILEBASE = $(TARGET_BUILD_DIR)/software/$(FIRMWARE)/firmware
IMAGE_FILE = $(TARGET_BUILD_DIR)/image-gateware+bios+$(FIRMWARE).bin

TFTP_IPRANGE ?= 192.168.100
export TFTP_IPRANGE
TFTPD_DIR ?= build/tftpd/

# Couple of Python settings.
Expand All @@ -54,7 +64,7 @@ MAKE_CMD=\
--platform=$(PLATFORM) \
--target=$(TARGET) \
--cpu-type=$(CPU) \
--iprange=$(IPRANGE) \
--iprange=$(TFTP_IPRANGE) \
$(MISOC_EXTRA_CMDLINE) \
$(LITEX_EXTRA_CMDLINE) \

Expand All @@ -64,6 +74,15 @@ SHELL := /bin/bash
FILTER ?= tee -a
LOGFILE ?= $(PWD)/$(TARGET_BUILD_DIR)/output.$(shell date +%Y%m%d-%H%M%S).log

build/cache.mk: targets/*/*.py scripts/makefile-cache.sh
@mkdir -p build
@./scripts/makefile-cache.sh

-include build/cache.mk

TARGETS=$(TARGETS_$(PLATFORM))


# Initialize submodules automatically
third_party/%/.git: .gitmodules
git submodule sync --recursive -- $$(dirname $@)
Expand All @@ -72,8 +91,18 @@ third_party/%/.git: .gitmodules

# Image - a combination of multiple parts (gateware+bios+firmware+more?)
# --------------------------------------
image:
$(PYTHON) mkimage.py $(MISOC_EXTRA_CMDLINE) $(LITEX_EXTRA_CMDLINE)
$(IMAGE_FILE): $(GATEWARE_FILEBASE).bin $(BIOS_FILE) $(FIRMWARE_FILEBASE).fbi
$(PYTHON) mkimage.py \
$(MISOC_EXTRA_CMDLINE) $(LITEX_EXTRA_CMDLINE) \
--override-gateware=$(GATEWARE_FILEBASE).bin \
--override-bios=$(BIOS_FILE) \
--override-firmware=$(FIRMWARE_FILEBASE).fbi

$(TARGET_BUILD_DIR)/image.bin: $(IMAGE_FILE)
cp $< $@

image: $(IMAGE_FILE)
@true

image-load: image image-load-$(PLATFORM)
@true
Expand All @@ -98,10 +127,16 @@ else
$(MAKE_CMD)
endif

gateware-load: gateware-load-$(PLATFORM)
$(GATEWARE_FILEBASE).bit:
@touch $<

$(GATEWARE_FILEBASE).bin:
@touch $<

gateware-load: $(GATEWARE_FILEBASE).bit gateware-load-$(PLATFORM)
@true

gateware-flash: gateware-flash-$(PLATFORM)
gateware-flash: $(GATEWARE_FILEBASE).bin gateware-flash-$(PLATFORM)
@true

gateware-clean:
Expand All @@ -111,7 +146,8 @@ gateware-clean:

# Firmware - the stuff which runs in the soft CPU inside the FPGA.
# --------------------------------------
firmware:
$(BIOS_FILE):
$(FIRMWARE_FILEBASE).bin:
mkdir -p $(TARGET_BUILD_DIR)
ifneq ($(OS),Windows_NT)
$(MAKE_CMD) --no-compile-gateware \
Expand All @@ -120,6 +156,12 @@ else
$(MAKE_CMD) --no-compile-gateware
endif

$(FIRMWARE_FILEBASE).fbi: $(FIRMWARE_FILEBASE).bin
$(PYTHON) -m litex.soc.tools.mkmscimg -f $< -o $@

firmware: $(FIRMWARE_FILEBASE).bin
@true

firmware-load: firmware firmware-load-$(PLATFORM)
@true

Expand All @@ -134,6 +176,9 @@ firmware-clean:

.PHONY: firmware firmware-load firmware-flash firmware-connect firmware-clean

bios-flash: firmware bios-flash-$(PLATFORM)
@true

# TFTP booting stuff
# --------------------------------------
# TFTP server for minisoc to load firmware from
Expand All @@ -148,7 +193,7 @@ tftpd_stop:
tftpd_start:
mkdir -p $(TFTPD_DIR)
sudo true
sudo atftpd --verbose --bind-address $(IPRANGE).100 --daemon --logfile /dev/stdout --no-fork --user $(shell whoami) $(TFTPD_DIR) &
sudo atftpd --verbose --bind-address $(TFTP_IPRANGE).100 --daemon --logfile /dev/stdout --no-fork --user $(shell whoami) $(TFTPD_DIR) &

.PHONY: tftp tftpd_stop tftpd_start

Expand All @@ -157,31 +202,96 @@ tftpd_start:
flash: image-flash
@true

info:
@echo "TARGET='$(TARGET)'"
env:
@echo "PLATFORM='$(PLATFORM)'"
@echo "PLATFORM_EXPANSION='$(PLATFORM_EXPANSION)'"
@echo "FULL_PLATFORM='$(FULL_PLATFORM)'"
@echo "TARGET='$(TARGET)'"
@echo "DEFAULT_TARGET='$(DEFAULT_TARGET)'"
@echo "CPU='$(CPU)'"
@echo "FIRMWARE='$(FIRMWARE)'"
@echo "PROG='$(PROG)'"
@echo "TARGET_BUILD_DIR='$(TARGET_BUILD_DIR)'"
@echo "MISOC_EXTRA_CMDLINE='$(MISOC_EXTRA_CMDLINE)'"
@echo "LITEX_EXTRA_CMDLINE='$(LITEX_EXTRA_CMDLINE)'"
# Hardcoded values
@echo "CLANG=$(CLANG)"
@echo "PYTHONHASHSEED=$(PYTHONHASHSEED)"

info:
@echo " Platform: $(FULL_PLATFORM)"
@echo " Target: $(TARGET)"
@echo " CPU: $(CPU)"
@if [ x"$(FIRMWARE)" != x"firmware" ]; then \
echo " Firmare: $(FIRMWARE)"; \
fi

prompt:
@echo -n "P=$(PLATFORM)"
@if [ x"$(TARGET)" != x"$(DEFAULT_TARGET)" ]; then echo -n " T=$(TARGET)"; fi
@if [ x"$(CPU)" != x"$(DEFAULT_CPU)" ]; then echo -n " C=$(CPU)"; fi
@if [ x"$(FIRMWARE)" != x"firmware" ]; then \
echo -n " F=$(FIRMWARE)"; \
fi
@if [ x"$(PROG)" != x"" ]; then echo -n " P=$(PROG)"; fi
@BRANCH="$(shell git symbolic-ref --short HEAD 2> /dev/null)"; \
if [ "$$BRANCH" != "master" ]; then \
if [ x"$$BRANCH" = x"" ]; then \
BRANCH="???"; \
fi; \
echo " R=$$BRANCH"; \
fi

# @if [ ! -z "$(TARGETS)" ]; then echo " Extra firmware needed for: $(TARGETS)"; echo ""; fi
# FIXME: Add something about the TFTP stuff
# FIXME: Add something about TFTP_IPRANGE for platforms which have NET targets.
help:
@echo "Environment:"
@echo " PLATFORM=$(shell ls targets/ | grep -v ".py" | grep -v "common" | sed -e"s+targets/++" -e's/$$/ OR/')" | sed -e's/ OR$$//'
@echo " PLATFORM describes which device you are targetting."
@echo " PLATFORM=$(shell echo $(PLATFORMS) | sed -e's/ / OR /g')" | sed -e's/ OR $$//'
@echo " (current: $(PLATFORM))"
@echo " TARGET=$(shell ls targets/$(PLATFORM)/ | grep ".py" | grep -v "__" | sed -e"s+targets/$(PLATFORM)/++" -e's/.py/ OR/')" | sed -e's/ OR$$//'
@echo " (current: $(TARGET))"
@echo ""
@if [ ! -z "$(TARGETS)" ]; then echo " Extra firmware needed for: $(TARGETS)"; echo ""; fi
@echo "Targets avaliable:"
@echo " make help"
@echo " make all"
@echo " make gateware"
@echo " make firmware"
@echo " make flash"
@for T in $(TARGETS); do make -s help-$$T; done
@echo " make clean"
@echo " PLATFORM_EXPANSION describes any expansion board you have plugged into your device."
@echo " PLATFORM_EXPANSION=<expansion board>"
@echo " (current: $(PLATFORM_EXPANSION))"
@echo ""
@echo " TARGET describes a set of functionality to use (see doc/targets.md for more info)."
@echo " TARGET=$(shell echo $(TARGETS) | sed -e's/ / OR /g')" | sed -e's/ OR $$//'
@echo " (current: $(TARGET), default: $(DEFAULT_TARGET))"
@echo ""
@echo " CPU describes which soft-CPU to use on the FPGA."
@echo " CPU=lm32 OR or1k"
@echo " (current: $(CPU), default: $(DEFAULT_CPU))"
@echo ""
@echo " FIRMWARE describes the code running on the soft-CPU inside the FPGA."
@echo " FIRMWARE=firmware OR micropython"
@echo " (current: $(FIRMWARE))"
@echo ""
@echo "Gateware make commands avaliable:"
@echo " make gateware - Build the gateware"
@echo " make gateware-load - *Temporarily* load the gateware onto a device"
@echo " make gateware-flash - *Permanently* flash gateware onto a device"
@echo " make bios - Build the bios"
@echo " make bios-flash - *Permanently* flash the bios onto a device"
@echo " (Only needed on low resource boards.)"
@echo ""
@echo "Firmware make commands avaliable:"
@echo " make firmware - Build the firmware"
@echo " make firmware-load - *Temporarily* load the firmware onto a device"
@echo " make firmware-flash - *Permanently* flash the firmware onto a device"
@echo " make firmware-connect - *Connect* to the firmware running on a device"
@echo ""
@echo "Image commands avaliable:"
@echo " make image - Make an image containing gateware+bios+firmware"
@echo " make image-flash - *Permanently* flash an image onto a device"
@echo " make flash - Alias for image-flash"
@echo ""
@echo "Other Make commands avaliable:"
@make -s help-$(PLATFORM)
@echo " make clean - Clean all build artifacts."

clean:
rm build/cache.mk
rm -rf $(TARGET_BUILD_DIR)
py3clean . || rm -rf $$(find -name __pycache__)

Expand Down
7 changes: 5 additions & 2 deletions mkimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def main():
parser.add_argument("--override-gateware")
parser.add_argument("--override-bios")
parser.add_argument("--override-firmware")
parser.add_argument("--firmware-name", default="HDMI2USB")
parser.add_argument("--force-image-size")

args = parser.parse_args()
Expand Down Expand Up @@ -101,8 +102,10 @@ def main():

# SoftCPU firmware
print(("Firmware @ 0x{:08x} ({:10} bytes) {:60}"
" - HDMI2USB Firmware in FBI format (loaded into DRAM)"
).format(firmware_pos, len(firmware_data), firmware))
" - {} Firmware in FBI format (loaded into DRAM)"
).format(
firmware_pos, len(firmware_data), firmware,
args.firmware_name))
print(" ".join("{:02x}".format(i) for i in firmware_data[:64]))
f.seek(firmware_pos)
f.write(firmware_data)
Expand Down
28 changes: 9 additions & 19 deletions scripts/build-micropython.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,8 @@ if [ -z "$HDMI2USB_ENV" ]; then
exit 1
fi

if [ -z "$PLATFORM" ]; then
echo "Please set PLATFORM"
exit 1
fi
if [ -z "$TARGET" ]; then
echo "Please set TARGET"
exit 1
fi
if [ -z "$CPU" ]; then
echo "Please set CPU"
exit 1
fi
# Imports TARGET, PLATFORM, CPU and TARGET_BUILD_DIR from Makefile
eval $(make env)

set -x
set -e
Expand All @@ -59,7 +49,6 @@ if [ ! -d "$MPY_SRC_DIR" ]; then
fi

# Generate the bios and local firmware
TARGET_BUILD_DIR=$(realpath build)/${PLATFORM}_${TARGET}_${CPU}/
if [ ! -d $TARGET_BUILD_DIR/software/include/generated ]; then
make firmware
fi
Expand All @@ -70,19 +59,20 @@ if [ ! -e "$TARGET_MPY_BUILD_DIR/generated" ]; then
mkdir -p $TARGET_MPY_BUILD_DIR
(
cd $TARGET_MPY_BUILD_DIR
ln -s $(realpath $PWD/../../software/include/generated) $TARGET_MPY_BUILD_DIR/generated
ln -s $(realpath $PWD/../../software/include/generated) generated
)
fi
TARGET_MPY_BUILD_DIR="$(realpath $TARGET_BUILD_DIR/software/micropython)"

# Build micropython
export CROSS_COMPILE=$CPU-elf-newlib-
export BUILDINC_DIRECTORY="$(realpath $TARGET_BUILD_DIR/software/include)"
export BUILD="$(realpath $TARGET_MPY_BUILD_DIR)"
OLD_DIR=$PWD
cd $TARGET_MPY_BUILD_DIR
export CROSS_COMPILE=$CPU-elf-newlib-
export BUILDINC_DIRECTORY=$TARGET_BUILD_DIR/software/include
export BUILD=$TARGET_MPY_BUILD_DIR
make -C ../../../../third_party/micropython/litex/
make V=1 -C $(realpath ../../../../third_party/micropython/litex/)
cd $OLD_DIR

# Generate a firmware image suitable for flashing.
python -m litex.soc.tools.mkmscimg -f $TARGET_MPY_BUILD_DIR/firmware.bin -o $TARGET_MPY_BUILD_DIR/firmware.fbi
/usr/bin/env python mkimage.py --output-file=$TARGET_BUILD_DIR/micropython.bin --override-firmware=$TARGET_MPY_BUILD_DIR/firmware.fbi
/usr/bin/env python mkimage.py $MISOC_EXTRA_CMDLINE $LITEX_EXTRA_CMDLINE --output-file=$TARGET_BUILD_DIR/micropython.bin --override-firmware=$TARGET_MPY_BUILD_DIR/firmware.fbi
17 changes: 3 additions & 14 deletions scripts/build-qemu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,8 @@ if [ -z "$HDMI2USB_ENV" ]; then
exit 1
fi

if [ -z "$PLATFORM" ]; then
echo "Please set PLATFORM"
exit 1
fi
if [ -z "$TARGET" ]; then
echo "Please set TARGET"
exit 1
fi
if [ -z "$CPU" ]; then
echo "Please set CPU"
exit 1
fi
# Imports TARGET, PLATFORM, CPU and TARGET_BUILD_DIR from Makefile
eval $(make env)

set -x
set -e
Expand All @@ -52,7 +42,6 @@ if [ ! -d "$QEMU_SRC_DIR" ]; then
)
fi

TARGET_BUILD_DIR=$(realpath build)/${PLATFORM}_${TARGET}_${CPU}/
TARGET_QEMU_BUILD_DIR=$TARGET_BUILD_DIR/qemu

case $CPU in
Expand Down Expand Up @@ -96,7 +85,7 @@ cd $TARGET_QEMU_BUILD_DIR
make -j8
cd $OLD_DIR

/usr/bin/env python mkimage.py --output-file=qemu.bin --override-gateware=none --force-image-size=true
/usr/bin/env python mkimage.py $MISOC_EXTRA_CMDLINE $LITEX_EXTRA_CMDLINE --output-file=qemu.bin --override-gateware=none --force-image-size=true
$TARGET_QEMU_BUILD_DIR/qemu-img convert -f raw $TARGET_BUILD_DIR/qemu.bin -O qcow2 -S 16M $TARGET_BUILD_DIR/qemu.qcow2

# BIOS
Expand Down

0 comments on commit ed6c81b

Please sign in to comment.