summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorNathaniel Graff <nathaniel.graff@sifive.com>2019-01-15 16:41:05 -0800
committerNathaniel Graff <nathaniel.graff@sifive.com>2019-01-18 10:22:28 -0800
commite122cc888ddd6608d26281ea83b300f491ea4f25 (patch)
tree50650880958c2b868c5dabe30850238857af6bea /scripts
parentfd1de76453afe3b9ab61002ce2efed6b46786256 (diff)
Add export of standalone project
Split out board init, toolchain setup, and program build into scripts/standalone.mk which can be included in the top-level Makefile to allow in-SDK builds or be exported and used on its own. Create a `standalone` Make target which copies and example, the built Freedom Metal library, and the standalone Makefile into an external project. Signed-off-by: Nathaniel Graff <nathaniel.graff@sifive.com>
Diffstat (limited to 'scripts')
-rw-r--r--scripts/standalone.mk105
1 files changed, 105 insertions, 0 deletions
diff --git a/scripts/standalone.mk b/scripts/standalone.mk
new file mode 100644
index 0000000..244bb7c
--- /dev/null
+++ b/scripts/standalone.mk
@@ -0,0 +1,105 @@
+#############################################################
+# Makefile Arguments
+#############################################################
+
+# BSP_DIR sets the path to the target-specific board support package.
+BSP_DIR ?= $(abspath bsp)
+# SRC_DIR sets the path to the program source directory
+SRC_DIR ?= $(abspath src)
+
+#############################################################
+# BSP loading
+#############################################################
+
+# There must be a settings makefile fragment in the BSP's board directory.
+ifeq ($(wildcard $(BSP_DIR)/settings.mk),)
+$(error Unable to find BSP for $(BOARD), expected to find $(BSP_DIR)/settings.mk)
+endif
+
+# Include the BSP settings
+include $(BSP_DIR)/settings.mk
+
+# Check that settings.mk sets RISCV_ARCH and RISCV_ABI
+ifeq ($(RISCV_ARCH),)
+$(error $(BSP_DIR)/board.mk must set RISCV_ARCH, the RISC-V ISA string to target)
+endif
+
+ifeq ($(RISCV_ABI),)
+$(error $(BSP_DIR)/board.mk must set RISCV_ABI, the ABI to target)
+endif
+
+# Determines the XLEN from the toolchain tuple
+ifeq ($(patsubst rv32%,rv32,$(RISCV_ARCH)),rv32)
+RISCV_XLEN := 32
+else ifeq ($(patsubst rv64%,rv64,$(RISCV_ARCH)),rv64)
+RISCV_XLEN := 64
+else
+$(error Unable to determine XLEN from $(RISCV_ARCH))
+endif
+
+#############################################################
+# Toolchain
+#############################################################
+
+# Allow users to select a different cross compiler.
+CROSS_COMPILE ?= riscv64-unknown-elf
+
+# If users don't specify RISCV_PATH then assume that the tools will just be in
+# their path.
+ifeq ($(RISCV_PATH),)
+RISCV_GCC := $(CROSS_COMPILE)-gcc
+RISCV_GXX := $(CROSS_COMPILE)-g++
+RISCV_OBJDUMP := $(CROSS_COMPILE)-objdump
+RISCV_GDB := $(CROSS_COMPILE)-gdb
+RISCV_AR := $(CROSS_COMPILE)-ar
+else
+RISCV_GCC := $(abspath $(RISCV_PATH)/bin/$(CROSS_COMPILE)-gcc)
+RISCV_GXX := $(abspath $(RISCV_PATH)/bin/$(CROSS_COMPILE)-g++)
+RISCV_OBJDUMP := $(abspath $(RISCV_PATH)/bin/$(CROSS_COMPILE)-objdump)
+RISCV_GDB := $(abspath $(RISCV_PATH)/bin/$(CROSS_COMPILE)-gdb)
+RISCV_AR := $(abspath $(RISCV_PATH)/bin/$(CROSS_COMPILE)-ar)
+PATH := $(abspath $(RISCV_PATH)/bin):$(PATH)
+endif
+
+#############################################################
+# Software
+#############################################################
+
+ifneq ($(BSP),legacy)
+
+PROGRAM_ELF ?= $(SRC_DIR)/$(PROGRAM)
+
+.PHONY: all
+all: software
+
+.PHONY: software
+software: $(PROGRAM_ELF)
+
+ifneq ($(COREIP_MEM_WIDTH),)
+software: $(PROGRAM_HEX)
+endif
+
+PROGRAM_SRCS = $(wildcard $(SRC_DIR)/*.c) $(wildcard $(SRC_DIR)/*.h) $(wildcard $(SRC_DIR)/*.S)
+
+$(PROGRAM_ELF): \
+ $(PROGRAM_SRCS) \
+ $(BSP_DIR)/install/lib/libmee.a \
+ $(BSP_DIR)/install/lib/libmee-gloss.a \
+ $(BSP_DIR)/mee.lds
+ $(MAKE) -C $(dir $@) $(notdir $@) \
+ AR=$(RISCV_AR) \
+ CC=$(RISCV_GCC) \
+ CXX=$(RISCV_GXX) \
+ CFLAGS="-Os -march=$(RISCV_ARCH) -mabi=$(RISCV_ABI) -g -mcmodel=medany -I$(abspath $(BSP_DIR)/install/include/)" \
+ CXXFLAGS="-Os -march=$(RISCV_ARCH) -mabi=$(RISCV_ABI) -g -mcmodel=medany -I$(abspath $(BSP_DIR)/install/include/)" \
+ LDFLAGS="-nostartfiles -nostdlib -L$(sort $(dir $(abspath $(filter %.a,$^)))) -T$(abspath $(filter %.lds,$^))" \
+ LDLIBS="-Wl,--start-group -lc -lgcc -lmee -lmee-gloss -Wl,--end-group"
+ touch -c $@
+
+.PHONY: clean-software
+clean-software:
+ $(MAKE) -C $(SRC_DIR) clean
+clean: clean-software
+
+endif # BSP != legacy
+