SConscript 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # RT-Thread building script for component
  2. Import('rtconfig')
  3. Import('RTT_ROOT')
  4. import os
  5. import re
  6. import rtconfig
  7. from building import *
  8. BUILTIN_ALL_DOWNLOADED_MODES = {
  9. "gd32vf103": ("flashxip"),
  10. "evalsoc": ("ilm", "flash", "flashxip", "ddr", "sram")
  11. }
  12. DEFAULT_DOWNLOAD_MODES = ("ilm", "flashxip", "ddr", "sram", "sramxip")
  13. default_arch_abi = ("rv32imac", "ilp32")
  14. cwd = GetCurrentDir()
  15. FRAMEWORK_DIR = cwd
  16. def parse_nuclei_soc_predefined_cores(core_mk):
  17. if not os.path.isfile(core_mk):
  18. return dict()
  19. core_arch_abis = dict()
  20. core_arch_abi_re = re.compile(r'^([A-Z]+\d+[A-Z]*)_CORE_ARCH_ABI\s*=\s*(rv\d+\w*)\s+(i*lp\d+\w*)')
  21. with open(core_mk, "r") as core_mk_file:
  22. for line in core_mk_file.readlines():
  23. line = line.strip()
  24. matches = core_arch_abi_re.match(line)
  25. if matches:
  26. core_lower = matches.groups()[0].lower()
  27. core_arch_abis[core_lower] = (matches.groups()[1:3])
  28. return core_arch_abis
  29. NUCLEI_SOC_CORES_MK = os.path.join(cwd, "Build/Makefile.core")
  30. core_arch_abis = parse_nuclei_soc_predefined_cores(NUCLEI_SOC_CORES_MK)
  31. # Get configurations from rtconfig
  32. build_soc = getattr(rtconfig, "NUCLEI_SDK_SOC").lower().strip()
  33. build_board = getattr(rtconfig, "NUCLEI_SDK_BOARD").lower().strip()
  34. build_download_mode = getattr(rtconfig, "NUCLEI_SDK_DOWNLOAD", "").lower().strip()
  35. build_core = getattr(rtconfig, "NUCLEI_SDK_CORE", "").lower().strip()
  36. build_march = getattr(rtconfig, "NUCLEI_SDK_RISCV_ARCH", "").lower().strip()
  37. build_mabi = getattr(rtconfig, "NUCLEI_SDK_RISCV_ABI", "").lower().strip()
  38. build_mcmodel = getattr(rtconfig, "NUCLEI_SDK_RISCV_MCMODEL", "medany").lower().strip()
  39. build_ldscript = getattr(rtconfig, "NUCLEI_SDK_LDSCRIPT", "").lower().strip()
  40. # Backward compatibility with previous Nuclei SDK releases
  41. if build_soc == "hbird":
  42. print("Warning! Since Nuclei SDK 0.3.1, SoC hbird is renamed to demosoc, please change NUCLEI_SDK_SOC to demosoc!")
  43. build_soc = "demosoc"
  44. if build_board == "hbird_eval":
  45. print("Warning! Since Nuclei SDK 0.3.1, Board hbird_eval is renamed to nuclei_fpga_eval, please change NUCLEI_SDK_BOARD to nuclei_fpga_eval!")
  46. build_board = "nuclei_fpga_eval"
  47. if build_soc == "demosoc":
  48. print("Warning! Since Nuclei SDK 0.5.0, SoC demosoc is removed, please change NUCLEI_SDK_SOC to evalsoc!")
  49. build_soc = "evalsoc"
  50. if not build_march and not build_mabi and build_core in core_arch_abis:
  51. build_march, build_mabi = core_arch_abis[build_core]
  52. else:
  53. if not build_mabi or not build_march:
  54. build_march, build_mabi = default_arch_abi
  55. print("No mabi and march specified in rtconfig.py, use default -march=%s -mabi=%s!" % (build_march, build_mabi))
  56. if build_soc in BUILTIN_ALL_DOWNLOADED_MODES:
  57. supported_download_modes = BUILTIN_ALL_DOWNLOADED_MODES[build_soc]
  58. else:
  59. if os.path.isfile(os.path.join(cwd, build_soc, "build.mk")) == False:
  60. print("SoC={} is not supported in Nuclei SDK".format(build_soc))
  61. exit(0)
  62. else:
  63. supported_download_modes = DEFAULT_DOWNLOAD_MODES
  64. SoC_Common = 'SoC/{}/Common'.format(build_soc)
  65. SoC_Board = 'SoC/{}/Board/{}'.format(build_soc, build_board)
  66. build_core_options = " -march=%s -mabi=%s -mcmodel=%s " % (build_march, build_mabi, build_mcmodel)
  67. # required by rt-thread v5
  68. rtconfig.DEVICE = build_core_options
  69. rtconfig.NUCLEI_SDK_OPENOCD_CFG = os.path.join(FRAMEWORK_DIR, \
  70. "SoC", build_soc, "Board", build_board, "openocd_{}.cfg".format(build_soc))
  71. if build_soc == "evalsoc":
  72. if build_download_mode not in supported_download_modes:
  73. # If build.download not defined for Eval SoC, use default "ILM"
  74. chosen_download_mode = "ilm" if len(supported_download_modes) == 0 else supported_download_modes[0]
  75. print("Download mode %s is not supported for SOC %s, use default download mode %s" \
  76. % (build_download_mode, build_soc, chosen_download_mode))
  77. build_download_mode = chosen_download_mode
  78. else:
  79. if build_download_mode not in supported_download_modes:
  80. chosen_download_mode = "flashxip" if len(supported_download_modes) == 0 else supported_download_modes[0]
  81. print("Download mode %s is not supported for SOC %s, use default download mode %s" \
  82. % (build_download_mode, build_soc, chosen_download_mode))
  83. build_download_mode = chosen_download_mode
  84. print("Supported downloaded modes for board %s are %s, chosen downloaded mode is %s" \
  85. % (build_board, supported_download_modes, build_download_mode))
  86. if not build_ldscript:
  87. ld_script = "gcc_%s_%s.ld" % (
  88. build_soc, build_download_mode) if build_download_mode else "gcc_%s.ld" % build_soc
  89. build_ldscript = os.path.join(
  90. FRAMEWORK_DIR, "SoC", build_soc, "Board", build_board, "Source", "GCC", ld_script)
  91. else:
  92. print("Use user defined ldscript %s" % build_ldscript)
  93. # Use correct downloaded modes
  94. DOWNLOAD_MODE = "DOWNLOAD_MODE_%s" % build_download_mode.upper()
  95. build_download_mode_upper = build_download_mode.upper()
  96. src = Glob(SoC_Common + '/Source/*.c')
  97. src += Glob(SoC_Common + '/Source/Drivers/*.c')
  98. if build_soc == "gd32vf103":
  99. src += Glob(SoC_Common + '/Source/Drivers/Usb/*.c')
  100. src += Glob(SoC_Common + '/Source/Stubs/newlib/*.c')
  101. src += Glob(SoC_Common + '/Source/GCC/*.S')
  102. src += Glob(SoC_Board + '/Source/*.c')
  103. CPPPATH = [ cwd + '/NMSIS/Core/Include',
  104. cwd + '/NMSIS/DSP/Include',
  105. cwd + '/NMSIS/DSP/PrivateInclude',
  106. cwd + '/NMSIS/NN/Include',
  107. cwd + '/' + SoC_Common + '/Include',
  108. cwd + '/' + SoC_Common + '/Source/Stubs/newlib',
  109. cwd + '/' + SoC_Board + '/Include']
  110. LIBPATH = [ cwd + '/NMSIS/Library/DSP/GCC',
  111. cwd + '/NMSIS/Library/NN/GCC' ]
  112. if build_soc == "gd32vf103":
  113. CPPPATH.append(cwd + '/' + SoC_Common + '/Include/Usb')
  114. CPPDEFINES = [ '-DDOWNLOAD_MODE={}'.format(DOWNLOAD_MODE),
  115. '-DDOWNLOAD_MODE_STRING=\"{}\"'.format(build_download_mode_upper),
  116. '-DRTOS_RTTHREAD', '-DNUCLEI_BANNER=0' ]
  117. # Flash download mode vector table need to remapped
  118. if build_download_mode_upper == "FLASH":
  119. CPPDEFINES.extend(['-DVECTOR_TABLE_REMAPPED'])
  120. extra_flags = build_core_options
  121. extra_lflags = "{} -L {} -T {}".format(build_core_options, os.path.dirname(build_ldscript), build_ldscript)
  122. # rtconfig.CFLAGS = "{} {}".format(build_core_options, rtconfig.CFLAGS)
  123. # rtconfig.AFLAGS = "{} {}".format(build_core_options, rtconfig.AFLAGS)
  124. # rtconfig.LFLAGS = "{} {} -T {}".format(build_core_options, rtconfig.LFLAGS, build_ldscript)
  125. # print(rtconfig.CFLAGS)
  126. # print(rtconfig.AFLAGS)
  127. # print(rtconfig.LFLAGS)
  128. group = DefineGroup('nuclei_sdk', src, depend = [''], \
  129. CCFLAGS=extra_flags, ASFLAGS=extra_flags, LINKFLAGS=extra_lflags, \
  130. CPPPATH = CPPPATH, CPPDEFINES=CPPDEFINES, LIBPATH=LIBPATH)
  131. Return('group')