SConscript 6.2 KB

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