Makefile.rules 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. TARGET_ELF := $(TARGET).elf
  2. ALL_CSRCS = $(filter-out $(wildcard $(EXCLUDE_SRCS)), $(sort $(wildcard $(C_SRCS)) $(call get_csrcs, $(SRCDIRS) $(C_SRCDIRS))))
  3. ALL_CXXSRCS = $(filter-out $(wildcard $(EXCLUDE_SRCS)), $(sort $(wildcard $(CXX_SRCS)) $(call get_cxxsrcs, $(SRCDIRS) $(CXX_SRCDIRS))))
  4. ALL_ASMSRCS = $(filter-out $(wildcard $(EXCLUDE_SRCS)), $(sort $(wildcard $(ASM_SRCS)) $(call get_asmsrcs, $(SRCDIRS) $(ASM_SRCDIRS))))
  5. ALL_ASM_OBJS := $(ALL_ASMSRCS:=.o)
  6. ALL_C_OBJS := $(ALL_CSRCS:=.o)
  7. ALL_CXX_OBJS := $(ALL_CXXSRCS:=.o)
  8. ALL_OBJS += $(ALL_ASM_OBJS) $(ALL_C_OBJS) $(ALL_CXX_OBJS)
  9. # Application Specific compilation flags control
  10. # If APPDIRS is defined, we will collect sources in those directories
  11. # and apply APP_* flags to them
  12. ifneq ($(APPDIRS),)
  13. APP_CSRCS = $(call get_csrcs, $(APPDIRS))
  14. APP_CXXSRCS = $(call get_cxxsrcs, $(APPDIRS))
  15. APP_ASMSRCS = $(call get_asmsrcs, $(APPDIRS))
  16. APP_ASM_OBJS := $(APP_ASMSRCS:=.o)
  17. APP_C_OBJS := $(APP_CSRCS:=.o)
  18. APP_CXX_OBJS := $(APP_CXXSRCS:=.o)
  19. $(APP_C_OBJS): CFLAGS += $(_REAL_APP_CFLAGS) $(_REAL_APP_COMMON_FLAGS)
  20. $(APP_CXX_OBJS): CXXFLAGS += $(_REAL_APP_CXXFLAGS) $(_REAL_APP_COMMON_FLAGS)
  21. $(APP_ASM_OBJS): ASMFLAGS += $(_REAL_APP_ASMFLAGS) $(_REAL_APP_COMMON_FLAGS)
  22. endif
  23. ALL_DEPS := $(ALL_OBJS:=.d)
  24. ALL_GCNOS := $(ALL_OBJS:.o=.gcno)
  25. ALL_GCDAS := $(ALL_OBJS:.o=.gcda)
  26. ALL_GCOVS := $(ALL_OBJS:.o=.gcov)
  27. CLEAN_OBJS += $(TARGET).elf $(TARGET).map $(TARGET).bin $(TARGET).dump $(TARGET).dasm \
  28. $(TARGET).srec $(TARGET).hex $(TARGET).verilog gmon.out openocd.log $(wildcard $(ALL_OBJS) $(ALL_DEPS) $(ALL_GCNOS) $(ALL_GCDAS) $(ALL_GCOVS))
  29. REAL_CLEAN_OBJS = $(subst /,$(PS), $(CLEAN_OBJS))
  30. # Default goal, placed before dependency includes
  31. all: info $(TARGET).elf
  32. # include dependency files of application
  33. ifneq ($(MAKECMDGOALS),clean)
  34. -include $(ALL_DEPS)
  35. endif
  36. .PHONY: all info showflags showtoolver help bin size dasm upload run_openocd run_gdb run_qemu run_xlspike run_xlmodel run_qemu_debug run_xlspike_rbb run_xlspike_openocd clean debug
  37. info:
  38. @$(ECHO) Current Configuration: TOOLCHAIN=$(TOOLCHAIN) RISCV_ARCH=$(RISCV_ARCH) RISCV_ABI=$(RISCV_ABI) RISCV_TUNE=$(RISCV_TUNE) RISCV_CMODEL=$(RISCV_CMODEL) SOC=$(SOC) BOARD=$(BOARD) CORE=$(CORE) ARCH_EXT=$(ARCH_EXT) DOWNLOAD=$(DOWNLOAD) STDCLIB=$(STDCLIB) SMP=$(SMP) BOOT_HARTID=$(BOOT_HARTID)
  39. showflags:
  40. @$(ECHO) TARGET: $(TARGET)
  41. @$(ECHO) NUCLEI_SDK_ROOT: $(NUCLEI_SDK_ROOT)
  42. @$(ECHO) NUCLEI_SDK_NMSIS: $(NUCLEI_SDK_NMSIS)
  43. @$(ECHO) OPENOCD_CFG: $(OPENOCD_CFG)
  44. @$(ECHO) LINKER_SCRIPT: $(LINKER_SCRIPT)
  45. @$(ECHO) ASMFLAGS: $(ASMFLAGS)
  46. @$(ECHO) CFLAGS: $(CFLAGS)
  47. @$(ECHO) CXXFLAGS: $(CXXFLAGS)
  48. @$(ECHO) LDFLAGS: $(LDFLAGS)
  49. @$(ECHO) CC: $(CC)
  50. @$(ECHO) CXX: $(CXX)
  51. @$(ECHO) GDB: $(GDB)
  52. showtoolver:
  53. @$(ECHO) Show $(CC) version
  54. @$(CC) -v
  55. @$(ECHO) Show $(OPENOCD) version
  56. @$(OPENOCD) -v
  57. @$(ECHO) Show $(QEMU) version
  58. -@$(QEMU) --version
  59. help:
  60. @$(ECHO) "Nuclei N/NX-series RISC-V Embedded Processor Software Development Kit "
  61. @$(ECHO) "== For detailed user guide, please check https://doc.nucleisys.com/nuclei_sdk/"
  62. @$(ECHO) "== Make variables used in Nuclei SDK =="
  63. @$(ECHO) "SOC: Select SoC built in Nuclei SDK, will select evalsoc by default"
  64. @$(ECHO) "BOARD: Select SoC's Board built in Nuclei SDK, will select nuclei_fpga_eval by default"
  65. @$(ECHO) "CORE: Not required for all SoCs, currently only evalsoc require it, n307fd by default"
  66. @$(ECHO) "ARCH_EXT: Not required for all SoCs, currently only evalsoc require it, such as ARCH_EXT=_zba_zbb_zbc_zbs_xxldspn3x, means B and Nuclei DSP(p v0.5.3) with N1/N2/N3 extension, please use >= gcc 13"
  67. @$(ECHO) "DOWNLOAD: Not required for all SoCs, use ilm by default, optional flashxip/ilm/flash/ddr"
  68. @$(ECHO) "V: V=1 verbose make, will print more information, by default V=0"
  69. @$(ECHO) "== How to Use with Make =="
  70. @$(ECHO) "1. Build Application:"
  71. @$(ECHO) "all [PROGRAM=flash/flashxip/ilm/ddr]"
  72. @$(ECHO) " Build a software program to load with the debugger."
  73. @$(ECHO) "2. Upload Application to Board using OpenOCD and GDB:"
  74. @$(ECHO) "upload [PROGRAM=flash/flashxip/ilm/ddr]"
  75. @$(ECHO) " Launch OpenOCD to flash your program to the on-board Flash."
  76. @$(ECHO) "3:(Option 1) Debug Application using OpenOCD and GDB"
  77. @$(ECHO) " 3.1: run_openocd"
  78. @$(ECHO) " 3.2: run_gdb [PROGRAM=flash/flashxip/ilm/ddr]"
  79. @$(ECHO) " Step 1: Launch OpenOCD for Debugger connection: make run_openocd"
  80. @$(ECHO) " Step 2: Launch GDB to connect openocd server, you can set breakpoints using gdb and debug it."
  81. @$(ECHO) " If you want to load your application, you need to run load in gdb command terminal"
  82. @$(ECHO) " to load your program, then use gdb to debug it."
  83. @$(ECHO) "3:(Option 2) Debug Application using OpenOCD and GDB"
  84. @$(ECHO) "debug [PROGRAM=flash/flashxip/ilm/ddr]"
  85. @$(ECHO) " Launch GDB and OpenOCD to debug your application on-board, you can set breakpoints using gdb and debug it."
  86. @$(ECHO) " If you want to load your application, you need to run load in gdb command terminal"
  87. @$(ECHO) " to load your program, then use gdb to debug it."
  88. @$(ECHO) ""
  89. @$(ECHO) "== Example Usage =="
  90. @$(ECHO) "1. cd <NUCLEI_SDK_ROOT>/application/helloworld"
  91. @$(ECHO) "2. Build for ILM download mode: make DOWNLOAD=ilm all"
  92. @$(ECHO) "3. Download application to board: make DOWNLOAD=ilm upload"
  93. @$(ECHO) "4.(Option 1) Debug application:"
  94. @$(ECHO) " 4.1 Terminal One: make DOWNLOAD=ilm run_openocd"
  95. @$(ECHO) " 4.2 Terminal Two: make DOWNLOAD=ilm run_gdb"
  96. @$(ECHO) "4.(Option 2) Debug application: make DOWNLOAD=ilm debug"
  97. @$(ECHO) ""
  98. $(TARGET).elf: $(ALL_OBJS)
  99. $(TRACE_LINK)
  100. $(Q)$(CC) $(CFLAGS) $(ALL_OBJS) -o $@ $(LDFLAGS)
  101. $(Q)$(SIZE) $@
  102. $(ALL_ASM_OBJS): %.o: % $(COMMON_PREREQS)
  103. $(TRACE_ASSEMBLE)
  104. $(Q)$(CC) $(ASMFLAGS) $(MKDEP_OPT) -c -o $@ $<
  105. $(ALL_C_OBJS): %.o: % $(COMMON_PREREQS)
  106. $(TRACE_COMPILE)
  107. $(Q)$(CC) $(CFLAGS) $(MKDEP_OPT) -c -o $@ $<
  108. $(ALL_CXX_OBJS): %.o: % $(COMMON_PREREQS)
  109. $(TRACE_COMPILE)
  110. $(Q)$(CXX) $(CXXFLAGS) $(MKDEP_OPT) -c -o $@ $<
  111. dasm: $(TARGET_ELF)
  112. -$(OBJDUMP) -S -d -M no-aliases $< > $(TARGET).dump
  113. -$(OBJDUMP) -d -M no-aliases $< > $(TARGET).dasm
  114. -$(OBJCOPY) $< -O ihex $(TARGET).hex
  115. ifeq ($(TOOLCHAIN),nuclei_gnu)
  116. -$(OBJCOPY) $< -O srec $(TARGET).srec
  117. -$(OBJCOPY) $< -O verilog $(TARGET).verilog
  118. endif
  119. bin: $(TARGET_ELF)
  120. $(OBJCOPY) $< -O binary $(TARGET).bin
  121. size: $(TARGET_ELF)
  122. $(Q)$(SIZE) $<
  123. upload: $(TARGET_ELF)
  124. @$(ECHO) "Download and run $<"
  125. $(GDB) -ex "set remotetimeout 240" -ex "target remote $(GDBREMOTE)" $(GDB_UPLOAD_ARGS) $(GDB_UPLOAD_CMDS)
  126. run_openocd:
  127. @$(ECHO) "Start openocd server"
  128. $(OPENOCD) $(OPENOCD_PORT_ARGS) $(OPENOCD_ARGS)
  129. run_gdb: $(TARGET_ELF)
  130. @$(ECHO) "Run gdb to connect openocd server and debug"
  131. $(GDB) $< $(GDB_ARGS) $(GDB_CMDS)
  132. debug: $(TARGET_ELF)
  133. @$(ECHO) "Download and debug $<"
  134. $(GDB) $< -ex "set remotetimeout 240" -ex "target remote $(GDBREMOTE)"
  135. # just for evalsoc purpose
  136. # https://doc.nucleisys.com/nuclei_tools/qemu/intro.html
  137. run_qemu: $(TARGET_ELF)
  138. @$(ECHO) "Run program $< on $(QEMU)"
  139. $(QEMU) -M $(QEMU_MACHINE) -cpu $(QEMU_CPU) $(QEMU_OPT) -nodefaults -nographic -serial stdio $(QEMU_ELF_OPT) $<
  140. # just for evalsoc purpose
  141. run_qemu_debug: $(TARGET_ELF)
  142. @$(ECHO) "Run program $< on $(QEMU), and waiting for gdb connection on port $(GDB_PORT)"
  143. @$(ECHO) "Then you can open a new terminal in the same directory and run make target: run_gdb to connect to this gdb connection"
  144. $(QEMU) -M $(QEMU_MACHINE) -cpu $(QEMU_CPU) $(QEMU_OPT) -nodefaults -nographic -serial stdio $(QEMU_ELF_OPT) $< -S -gdb tcp::$(GDB_PORT)
  145. # just for evalsoc purpose using xlmodel
  146. # https://doc.nucleisys.com/nuclei_tools/xlmodel/intro.html
  147. run_xlmodel: $(TARGET_ELF)
  148. @$(ECHO) "Run program $< on $(XLMODEL)"
  149. $(XLMODEL) $(XLMODEL_OPT) $(XLMODEL_EXTOPT) $<
  150. # just works on evalsoc purpose, experimental support
  151. run_xlspike: $(TARGET_ELF)
  152. @$(ECHO) "Run program $< on $(XLSPIKE)"
  153. $(XLSPIKE) $(XLSPIKE_OPT) --isa $(XLSPIKE_ARCH) $<
  154. run_xlspike_rbb: $(TARGET_ELF)
  155. @$(ECHO) "Run program $< on $(XLSPIKE), and waiting for remote bitbang connection on port $(RBB_PORT)"
  156. @$(ECHO) "Then you need open a new terminal in the same directory and run make target: run_xlspike_openocd to connect to this remote bitbang connection"
  157. $(XLSPIKE) $(XLSPIKE_OPT) --rbb-port=$(RBB_PORT) -H --isa $(XLSPIKE_ARCH) $<
  158. run_xlspike_openocd:
  159. @$(ECHO) "Run openocd to connect to remote bitbang connection"
  160. @$(ECHO) "Then you can open a new terminal in the same directory and run make target: run_gdb to connect to this gdb connection"
  161. $(OPENOCD) $(OPENOCD_PORT_ARGS) -f $(OPENOCD_XLSPIKE_CFG)
  162. clean:
  163. @$(ECHO) "Clean all build objects"
  164. $(Q)$(RM) $(REAL_CLEAN_OBJS)