SConscript 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. from building import *
  2. import rtconfig
  3. import os
  4. cwd = GetCurrentDir()
  5. src = []
  6. CPPPATH = []
  7. CPPDEFINES = []
  8. LOCAL_CFLAGS = ''
  9. LOCAL_CCFLAGS = ''
  10. LIBS = []
  11. LIBPATH = []
  12. # 获取RT-Thread根目录路径
  13. bsp_dir = os.path.normpath(os.path.join(cwd, '..', '..'))
  14. rtthread_root = os.path.normpath(os.path.join(bsp_dir,'rt-thread'))
  15. if not os.path.exists(rtthread_root):
  16. rtthread_root = os.path.normpath(os.path.join(bsp_dir, '..', '..', '..','..'))
  17. # 优先添加本地 mbedtls 3.x 头文件路径(必须在 ROM mbedtls 2.17 之前)
  18. # 这样可以确保编译时使用 mbedtls 3.x 的头文件而不是 ROM 中的 2.17 版本
  19. mbedtls_local_path = cwd + '/src/mbedtls/mbedtls/include'
  20. if os.path.exists(mbedtls_local_path):
  21. CPPPATH += [
  22. mbedtls_local_path,
  23. cwd + '/src/mbedtls/mbedtls/library', # common.h 等内部头文件
  24. ]
  25. # 公共头文件路径
  26. CPPPATH += [
  27. cwd + '/inc',
  28. cwd + '/src/macsw/export',
  29. cwd + '/src/macsw/import',
  30. cwd + '/src/wifi_manager',
  31. cwd + '/src/wifi_manager/wpas',
  32. cwd + '/src/plf/src',
  33. cwd + '/src/plf/riscv/arch',
  34. cwd + '/src/plf/riscv/arch/boot',
  35. cwd + '/src/plf/riscv/arch/compiler',
  36. cwd + '/src/plf/riscv/arch/ll',
  37. cwd + '/src/plf/riscv/arch/lib',
  38. cwd + '/src/plf/riscv/gd32vw55x',
  39. cwd + '/src/plf/riscv/NMSIS/Include',
  40. cwd + '/src/plf/riscv/NMSIS/DSP/Include',
  41. cwd + '/src/rtos/rtos_wrapper',
  42. cwd + '/src/util/include',
  43. cwd + '/rom_export/halcomm',
  44. cwd + '/rom_export/bootloader',
  45. os.path.join(rtthread_root, 'components', 'net', 'netdev', 'include'),
  46. ]
  47. # ROM mbedtls 2.17 头文件路径(仅在没有本地 mbedtls 3.x 时使用)
  48. # 如果存在本地 mbedtls,这个路径会被忽略(因为符号已经被本地版本提供)
  49. if not os.path.exists(mbedtls_local_path):
  50. CPPPATH += [cwd + '/rom_export/mbedtls-2.17.0-rom/include']
  51. if GetDepend(['PKG_USING_GD32VW55X_WIFI']):
  52. # 公共定义
  53. CPPDEFINES += ['CFG_RTOS', 'CONFIG_WIFI_SUPPORT']
  54. # wifi_manager 源文件
  55. src += Glob('src/wifi_manager/*.c')
  56. # wpas 源文件(排除 wpas_sae_crypto_mbedtls.c,它被 wifi_wpa.c 包含)
  57. wpas_src = Glob('src/wifi_manager/wpas/*.c')
  58. wpas_src = [f for f in wpas_src if not f.rstr().endswith('wpas_sae_crypto_mbedtls.c')]
  59. src += wpas_src
  60. # 平台层源文件 (需要根据实际移植的文件调整)
  61. if os.path.exists(cwd + '/src/plf/src'):
  62. # 排除 init_rom_symbol.c(避免与 init_rom.c 重复定义 rom_symbol_init)
  63. plf_src = Glob('src/plf/src/*.c')
  64. plf_src = [f for f in plf_src if not f.rstr().endswith('init_rom_symbol.c')]
  65. src += plf_src
  66. # 平台子模块
  67. plf_modules = ['rf', 'dma', 'nvds', 'uart', 'time', 'trng', 'qspi_flash', 'raw_flash', 'wdt', 'spi', 'spi_i2s', 'reg']
  68. for module in plf_modules:
  69. module_path = cwd + '/src/plf/src/' + module
  70. if os.path.exists(module_path):
  71. src += Glob('src/plf/src/' + module + '/*.c')
  72. CPPPATH += [cwd + '/src/plf/src/' + module]
  73. # RISC-V 架构特定文件(中断处理、启动代码等)
  74. riscv_platform_path = cwd + '/src/plf/riscv/gd32vw55x'
  75. if os.path.exists(riscv_platform_path):
  76. riscv_src = Glob('src/plf/riscv/gd32vw55x/*.c')
  77. # 排除与 RT-Thread 或标准外设库冲突的文件
  78. exclude_files = [
  79. 'system_gd32vw55x.c', # 系统初始化函数与标准外设库冲突
  80. ]
  81. riscv_src = [f for f in riscv_src if not any(f.rstr().endswith(ex) for ex in exclude_files)]
  82. src += riscv_src
  83. # RTOS适配层
  84. if os.path.exists(cwd + '/src/rtos/rtos_wrapper'):
  85. src += Glob('src/rtos/rtos_wrapper/*.c')
  86. # 工具库
  87. if os.path.exists(cwd + '/src/util/src'):
  88. src += Glob('src/util/src/*.c')
  89. # mbedtls 库(如果存在本地源码)
  90. mbedtls_library_path = cwd + '/src/mbedtls/mbedtls/library'
  91. if os.path.exists(mbedtls_library_path):
  92. # 编译 mbedtls 核心库文件
  93. mbedtls_src = Glob('src/mbedtls/mbedtls/library/*.c')
  94. # 排除一些不需要的文件(可选,根据实际需求调整)
  95. exclude_files = [
  96. 'net_sockets.c', # 网络套接字(使用 LWIP)
  97. 'timing.c', # 时序函数(可能需要平台适配)
  98. ]
  99. mbedtls_src = [f for f in mbedtls_src if not any(f.rstr().endswith(ex) for ex in exclude_files)]
  100. src += mbedtls_src
  101. # mbedtls 头文件路径已在文件开头添加(在 ROM mbedtls 之前)
  102. # mbedtls 需要的宏定义
  103. # mbedtls 3.x 使用 mbedtls_config.h 作为默认配置文件
  104. # 不需要显式指定 MBEDTLS_CONFIG_FILE,因为build_info.h会自动包含
  105. CPPDEFINES += [
  106. #'MBEDTLS_CONFIG_FILE=<mbedtls/mbedtls_config.h>', # 注释掉,使用默认
  107. # RT-Thread 移植:避免包含 <time.h> 导致的头文件循环包含
  108. # 定义 time_t 类型为 long,避免触发 newlib 和 RT-Thread libc 的冲突
  109. 'MBEDTLS_PLATFORM_TIME_TYPE_MACRO=long',
  110. # RT-Thread 移植:避免 x509_crt.c 包含 <sys/socket.h> 和 <arpa/inet.h>
  111. # 这会导致 LwIP 和 netdev 的 IP 地址类型定义冲突
  112. # 强制使用 mbedtls 内置的软件实现 inet_pton
  113. 'MBEDTLS_TEST_SW_INET_PTON',
  114. ]
  115. # 强制启用 lwip netif 回调(GD32 WiFi 需要)
  116. CPPDEFINES += [
  117. 'LWIP_NETIF_STATUS_CALLBACK=1',
  118. 'LWIP_NETIF_LINK_CALLBACK=1',
  119. ]
  120. if GetDepend(['RT_USING_NETDEV']):
  121. src += [
  122. 'port/wifi_netif_port.c'
  123. ]
  124. lwip_path = cwd + '/src/lwip/port'
  125. if os.path.exists(lwip_path):
  126. src += [
  127. 'src/lwip/port/dhcpd.c',
  128. 'src/lwip/port/sys_arch.c',
  129. 'src/lwip/port/wifi_netif.c',
  130. 'src/lwip/port/dnsd.c',
  131. ]
  132. # LwIP 头文件路径(优先级最高,覆盖 RT-Thread 的 lwip)
  133. CPPPATH = [
  134. cwd + '/src/lwip/port',
  135. cwd + '/src/lwip/port/arch',
  136. ] + CPPPATH
  137. # LwIP 相关宏定义
  138. CPPDEFINES += [
  139. 'LWIP_TIMEVAL_PRIVATE=0', # 避免与系统 timeval 冲突
  140. ]
  141. # 添加 lwip mem.c 编译(RT-Thread 的 lwip SConscript 没有包含它)
  142. # GD32 的 lwipopts.h 定义了 MEM_LIBC_MALLOC=1,但仍需要 mem_init 等函数
  143. lwip_mem_c = os.path.join(rtthread_root,'components', 'net', 'lwip', 'lwip-2.1.2', 'src', 'core', 'mem.c')
  144. if os.path.exists(lwip_mem_c):
  145. src += [lwip_mem_c]
  146. # 预编译库配置
  147. lib_path = cwd + '/lib'
  148. dsp_lib_path = cwd + '/src/plf/riscv/NMSIS/Library/DSP/GCC'
  149. if os.path.exists(lib_path):
  150. LIBPATH = [lib_path]
  151. # 需要链接所有预编译库:wifi, wpas, rf
  152. # 注意:虽然本地编译了 mbedtls 3.x,但 wpas 源码不完整,仍需要 libwpas.a
  153. # 链接器会优先使用本地编译的 mbedtls 符号,libwpas.a 提供 wpas 特定函数
  154. LIBS = ['wifi', 'wpas', 'rf']
  155. # 添加 NMSIS DSP 库(用于 RF 校准和信号处理)
  156. if os.path.exists(dsp_lib_path):
  157. LIBPATH.append(dsp_lib_path)
  158. # 根据当前架构选择对应的 DSP 库:rv32imafc
  159. LIBS += ['nmsis_dsp_rv32imafc']
  160. # 添加必要的系统库(用于浮点运算)
  161. # libgcc 提供浮点运算支持
  162. if rtconfig.PLATFORM == 'gcc':
  163. LIBS += ['gcc'] # 链接 libgcc.a
  164. # ROM 符号表链接(已在 rtconfig.py 的 LFLAGS 中全局添加)
  165. # 配置选项
  166. if GetDepend(['GD32VW55X_WIFI_STATION_MODE']):
  167. CPPDEFINES += ['CONFIG_WIFI_STA_MODE']
  168. if GetDepend(['GD32VW55X_WIFI_SOFTAP_MODE']):
  169. CPPDEFINES += ['CONFIG_WIFI_SOFTAP_MODE']
  170. if GetDepend(['GD32VW55X_WIFI_WPA_SECURITY']):
  171. CPPDEFINES += ['CONFIG_WPA_SECURITY']
  172. if GetDepend(['GD32VW55X_WIFI_WPA3_SECURITY']):
  173. CPPDEFINES += ['CONFIG_WPA3_SECURITY', 'CONFIG_SAE']
  174. if GetDepend(['GD32VW55X_WIFI_WPS_SUPPORT']):
  175. CPPDEFINES += ['CONFIG_WPS']
  176. if GetDepend(['GD32VW55X_WIFI_DEBUG']):
  177. CPPDEFINES += ['CONFIG_WIFI_DEBUG']
  178. debug_level = GetConfigValue('GD32VW55X_WIFI_DEBUG_LEVEL')
  179. if debug_level:
  180. CPPDEFINES += ['CONFIG_WIFI_DEBUG_LEVEL=' + str(debug_level)]
  181. if GetDepend(['GD32VW55X_WIFI_POWER_SAVE']):
  182. CPPDEFINES += ['CONFIG_WIFI_POWER_SAVE']
  183. # 示例代码
  184. # 定义组
  185. group = DefineGroup('gd32vw55x-wifi', src,
  186. depend=['PKG_USING_GD32VW55X_WIFI'],
  187. CPPPATH=CPPPATH,
  188. CPPDEFINES=CPPDEFINES,
  189. LIBS=LIBS,
  190. LIBPATH=LIBPATH,
  191. LOCAL_CFLAGS=LOCAL_CFLAGS,
  192. LOCAL_CCFLAGS=LOCAL_CCFLAGS)
  193. Return('group')