nuclei_gnu.mk 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. ## NUCLEI RISCV GCC COMPILER
  2. ## NUCLEI RISCV OPENOCD
  3. #
  4. #!< Nuclei SDK Tools Root
  5. COMPILE_PREFIX ?= riscv64-unknown-elf-
  6. NUCLEI_SDK_TOOL_ROOT ?= $(NUCLEI_SDK_ROOT)/prebuilt_tools
  7. NUCLEI_RISCV_GCC_ROOT ?= $(NUCLEI_SDK_TOOL_ROOT)/gcc
  8. NUCLEI_OPENOCD_ROOT ?= $(NUCLEI_SDK_TOOL_ROOT)/openocd
  9. NUCLEI_SDK_TOOL_ROOT_EXIST = 0
  10. ifneq ($(wildcard $(NUCLEI_RISCV_GCC_ROOT)),)
  11. ifneq ($(wildcard $(NUCLEI_OPENOCD_ROOT)),)
  12. NUCLEI_SDK_TOOL_ROOT_EXIST = 1
  13. endif
  14. endif
  15. ifeq ($(NUCLEI_SDK_TOOL_ROOT_EXIST),1)
  16. CC := $(abspath $(NUCLEI_RISCV_GCC_ROOT)/bin/$(COMPILE_PREFIX)gcc)
  17. CXX := $(abspath $(NUCLEI_RISCV_GCC_ROOT)/bin/$(COMPILE_PREFIX)g++)
  18. OBJDUMP := $(abspath $(NUCLEI_RISCV_GCC_ROOT)/bin/$(COMPILE_PREFIX)objdump)
  19. OBJCOPY := $(abspath $(NUCLEI_RISCV_GCC_ROOT)/bin/$(COMPILE_PREFIX)objcopy)
  20. GDB := $(abspath $(NUCLEI_RISCV_GCC_ROOT)/bin/$(COMPILE_PREFIX)gdb)
  21. AR := $(abspath $(NUCLEI_RISCV_GCC_ROOT)/bin/$(COMPILE_PREFIX)ar)
  22. SIZE := $(abspath $(NUCLEI_RISCV_GCC_ROOT)/bin/$(COMPILE_PREFIX)size)
  23. OPENOCD := $(abspath $(NUCLEI_OPENOCD_ROOT)/bin/openocd)
  24. else
  25. CC := $(COMPILE_PREFIX)gcc
  26. CXX := $(COMPILE_PREFIX)g++
  27. OBJDUMP := $(COMPILE_PREFIX)objdump
  28. OBJCOPY := $(COMPILE_PREFIX)objcopy
  29. GDB := $(COMPILE_PREFIX)gdb
  30. AR := $(COMPILE_PREFIX)ar
  31. SIZE := $(COMPILE_PREFIX)size
  32. OPENOCD := openocd
  33. endif
  34. # Handle standard c library selection variable STDCLIB
  35. ifneq ($(findstring newlib,$(STDCLIB)),)
  36. ### Handle cases when STDCLIB variable has newlib in it
  37. ifeq ($(STDCLIB),newlib_full)
  38. LDLIBS += -lc -lgcc
  39. else ifeq ($(STDCLIB),newlib_fast)
  40. LDLIBS += -lc_nano -lgcc
  41. STDCLIB_LDFLAGS += -u _printf_float -u _scanf_float
  42. COMMON_FLAGS += -isystem=/include/newlib-nano
  43. else ifeq ($(STDCLIB),newlib_small)
  44. LDLIBS += -lc_nano -lgcc
  45. STDCLIB_LDFLAGS += -u _printf_float
  46. COMMON_FLAGS += -isystem=/include/newlib-nano
  47. else ifeq ($(STDCLIB),newlib_nano)
  48. LDLIBS += -lc_nano -lgcc
  49. COMMON_FLAGS += -isystem=/include/newlib-nano
  50. else
  51. LDLIBS += -lc_nano -lgcc
  52. COMMON_FLAGS += -isystem=/include/newlib-nano
  53. endif
  54. ###
  55. else ifneq ($(findstring libncrt,$(STDCLIB)),)
  56. ### Handle cases when STDCLIB variable has libncrt in it
  57. LDLIBS += -l$(patsubst lib%,%,$(STDCLIB))
  58. ifneq ($(NCRTHEAP),)
  59. ifeq ($(COMPILE_PREFIX),riscv64-unknown-elf-)
  60. LDLIBS += -lheapops_$(NCRTHEAP)
  61. endif
  62. endif
  63. COMMON_FLAGS += -isystem=/include/libncrt
  64. ###
  65. else ifeq ($(STDCLIB),nostd)
  66. ### Handle cases when no standard system directories for header files
  67. COMMON_FLAGS += -nostdinc
  68. ###
  69. else ifeq ($(STDCLIB),nospec)
  70. ### Handle cases no specs configs are passed
  71. COMMON_FLAGS +=
  72. ###
  73. else
  74. LDLIBS += -lc_nano -lgcc
  75. ###
  76. endif
  77. ifneq ($(SEMIHOST),)
  78. ifneq ($(findstring libncrt,$(STDCLIB)),)
  79. ifeq ($(COMPILE_PREFIX),riscv64-unknown-elf-)
  80. LDLIBS += -lfileops_semi
  81. endif
  82. else
  83. LDLIBS += -lsemihost
  84. endif
  85. else
  86. ifneq ($(findstring libncrt,$(STDCLIB)),)
  87. ifneq ($(NCRTIO),)
  88. ifeq ($(COMPILE_PREFIX),riscv64-unknown-elf-)
  89. LDLIBS += -lfileops_$(NCRTIO)
  90. endif
  91. endif
  92. endif
  93. endif
  94. ## Link with standard c++ library
  95. LDLIBS += -lstdc++
  96. ## Heap and stack size settings
  97. ## It will define symbols only used in linker script
  98. ## __STACK_SIZE and __HEAP_SIZE are not a c marco
  99. ## they are ld symbols used by linker
  100. ifneq ($(STACKSZ),)
  101. LDFLAGS += -Wl,--defsym=__STACK_SIZE=$(STACKSZ)
  102. endif
  103. ifneq ($(HEAPSZ),)
  104. LDFLAGS += -Wl,--defsym=__HEAP_SIZE=$(HEAPSZ)
  105. endif
  106. ## SIMU=xlspike/qemu
  107. ### enable run on xlspike and qemu auto-exit if return from main
  108. ifneq ($(SIMU),)
  109. SIMULATION_MODE=SIMULATION_MODE_$(call uc, $(SIMU))
  110. COMMON_FLAGS += -DSIMULATION_MODE=$(SIMULATION_MODE)
  111. endif
  112. COMMON_FLAGS += -march=$(RISCV_ARCH) -mabi=$(RISCV_ABI) -mcmodel=$(RISCV_CMODEL)
  113. ## Append mtune options when RISCV_TUNE is defined
  114. ## It might be defined in SoC/<SOC>/build.mk, and can be overwritten by make
  115. ifneq ($(RISCV_TUNE),)
  116. COMMON_FLAGS += -mtune=$(RISCV_TUNE)
  117. endif
  118. # Generate zcmp related asm by passing extra options
  119. ifneq ($(findstring zc,$(ARCH_EXT)),)
  120. COMMON_FLAGS += -fomit-frame-pointer -fno-shrink-wrap-separate
  121. endif
  122. # TODO Workaround: when v extension is enabled, disable memcpy generated using vector instruction
  123. # upstream just rename this -mmemcpy-strategy option to -mstringop-strategy option now
  124. # see https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=4ae5a7336ac8e1ba57ee1e885b5b76ed86cdbfd5
  125. ifneq ($(findstring v,$(ARCH_EXT)),)
  126. #COMMON_FLAGS += -mmemcpy-strategy=scalar
  127. COMMON_FLAGS += -mstringop-strategy=scalar
  128. endif
  129. # Disable auto vectorization when AUTOVEC=0
  130. # see https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-ftree-vectorize
  131. ifeq ($(AUTOVEC),0)
  132. COMMON_FLAGS += -fno-tree-vectorize -fno-tree-loop-vectorize -fno-tree-slp-vectorize
  133. endif
  134. LDFLAGS += -nodefaultlibs
  135. # For binutils 2.40 in Nuclei GNU Toolchain, need add below option
  136. # to remove link warning
  137. ifeq ($(COMPILE_PREFIX),riscv64-unknown-elf-)
  138. LDFLAGS += -Wl,--no-warn-rwx-segments
  139. endif
  140. ifneq ($(findstring newlib,$(STDCLIB)),)
  141. # when compiled with -flto, need to pass the following options
  142. ifeq ($(CODESIZE),)
  143. LDFLAGS += -u _isatty -u _write -u _sbrk -u _read -u _close -u _fstat -u _lseek -u errno
  144. endif
  145. endif