qemu.lua 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. -- QEMU Cortex-M targets for hardware semantics validation.
  2. -- QEMU Cortex-M 目标:用于硬件语义校验(含非对齐访问 fault)
  3. local function setupRyanJsonQemuTarget(options)
  4. local cpu = options.cpu
  5. local freertosPort = options.freertosPort
  6. local isCm4f = options.isCm4f
  7. set_kind("binary")
  8. set_default(false)
  9. set_extension(".elf")
  10. set_toolchains("gcc")
  11. set_plat("cross")
  12. set_arch("arm")
  13. set_languages("gnu99")
  14. set_optimize("fastest")
  15. set_symbols("debug")
  16. set_toolset("cc", "arm-none-eabi-gcc")
  17. set_toolset("as", "arm-none-eabi-gcc")
  18. set_toolset("ld", "arm-none-eabi-gcc")
  19. set_toolset("ar", "arm-none-eabi-ar")
  20. local strictObjectKeyCheck = getBooleanEnvDefineValue("RYANJSON_STRICT_OBJECT_KEY_CHECK", "false")
  21. local defaultAddAtHead = getBooleanEnvDefineValue("RYANJSON_DEFAULT_ADD_AT_HEAD", "false")
  22. local snprintfSupportScientific = getBooleanEnvDefineValue("RYANJSON_SNPRINTF_SUPPORT_SCIENTIFIC", "true")
  23. local unitOnlyMemory = getBooleanEnvDefineValue("RYANJSON_UNIT_ONLY_MEMORY", "false")
  24. local unitOnlyRfc8259 = getBooleanEnvDefineValue("RYANJSON_UNIT_ONLY_RFC8259", "false")
  25. add_defines("RyanJsonStrictObjectKeyCheck=" .. strictObjectKeyCheck)
  26. add_defines("RyanJsonDefaultAddAtHead=" .. defaultAddAtHead)
  27. add_defines("RyanJsonSnprintfSupportScientific=" .. snprintfSupportScientific)
  28. if "true" == unitOnlyMemory then
  29. add_defines("RyanJsonUnitOnlyMemory")
  30. end
  31. if "true" == unitOnlyRfc8259 then
  32. add_defines("RyanJsonUnitOnlyRfc8259")
  33. end
  34. add_defines("RyanJsonTestPlatformQemu")
  35. add_defines("RyanJsonFreeRtosHeap4")
  36. add_defines("RyanJsonProjectRootPath=\"$(projectdir)\"")
  37. add_defines("UNITY_INCLUDE_CONFIG_H")
  38. add_defines("YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS=1")
  39. -- 测试分配模拟参数(可选):用于评估不同 malloc 头部与对齐策略的影响。
  40. tryAddNumericEnvDefine("RYANJSON_TEST_ALLOC_HEADER_SIZE", "RyanJsonTestAllocHeaderSize", 0)
  41. tryAddNumericEnvDefine("RYANJSON_TEST_ALLOC_ALIGN_SIZE", "RyanJsonTestAllocAlignSize", 1)
  42. add_cxflags(
  43. "-mcpu=" .. cpu,
  44. "-mthumb",
  45. "-ffunction-sections",
  46. "-fdata-sections",
  47. "-fno-common",
  48. "-fno-strict-aliasing",
  49. "-Wall",
  50. "-Wextra",
  51. "-Wno-unused-parameter",
  52. {force = true}
  53. )
  54. if isCm4f then
  55. add_cxflags(
  56. "-mfpu=fpv4-sp-d16",
  57. "-mfloat-abi=hard",
  58. {force = true}
  59. )
  60. end
  61. add_asflags(
  62. "-mcpu=" .. cpu,
  63. "-mthumb",
  64. {force = true}
  65. )
  66. if isCm4f then
  67. add_asflags(
  68. "-mfpu=fpv4-sp-d16",
  69. "-mfloat-abi=hard",
  70. {force = true}
  71. )
  72. end
  73. local linkerScript = project_path("test/qemu/platform/linkerMps2An386.ld")
  74. add_ldflags(
  75. "-mcpu=" .. cpu,
  76. "-mthumb",
  77. "-T" .. linkerScript,
  78. "-Wl,--gc-sections",
  79. "-Wl,--print-memory-usage",
  80. "--specs=nosys.specs",
  81. "-nostartfiles",
  82. {force = true}
  83. )
  84. if isCm4f then
  85. add_ldflags(
  86. "-mfpu=fpv4-sp-d16",
  87. "-mfloat-abi=hard",
  88. {force = true}
  89. )
  90. end
  91. add_syslinks("c", "m", "gcc")
  92. add_includedirs(project_path("./RyanJson"), {public = true})
  93. add_includedirs(project_path("./test/qemu/platform"), {public = true})
  94. add_includedirs(project_path("./test/qemu/common"), {public = true})
  95. add_includedirs(project_path("./example"), {public = true})
  96. add_includedirs(project_path("./test/unityTest/runner"), {public = true})
  97. add_includedirs(project_path("./test/unityTest/common"), {public = true})
  98. add_includedirs(project_path("./test/unityTest/include"), {public = true})
  99. add_includedirs(project_path("./test/unityTest/cases/core"), {public = true})
  100. add_includedirs(project_path("./test/unityTest/cases/scenario"), {public = true})
  101. add_includedirs(project_path("./test/unityTest/cases/usage"), {public = true})
  102. add_includedirs(project_path("./test/unityTest/cases/edge"), {public = true})
  103. add_includedirs(project_path("./test/unityTest/cases/stability"), {public = true})
  104. add_includedirs(project_path("./test/unityTest/cases/equality"), {public = true})
  105. add_includedirs(project_path("./test/unityTest/cases/utils"), {public = true})
  106. add_includedirs(project_path("./test/unityTest/cases/RFC8259"), {public = true})
  107. add_includedirs(project_path("./test/unityTest/cases/performance"), {public = true})
  108. add_includedirs(project_path("./test/externalModule/valloc"), {public = true})
  109. add_includedirs(project_path("./test/externalModule/tlsf"), {public = true})
  110. add_includedirs(project_path("./test/externalModule/cJSON"), {public = true})
  111. add_includedirs(project_path("./test/externalModule/unity/src"), {public = true})
  112. add_includedirs(project_path("./test/externalModule/yyjson"), {public = true})
  113. add_includedirs(project_path("./test/externalModule/FreeRTOS-Kernel/include"), {public = true})
  114. add_includedirs(project_path("test/externalModule/FreeRTOS-Kernel/portable/GCC/" .. freertosPort), {public = true})
  115. add_files(project_path("./RyanJson/*.c"), {public = true})
  116. add_files(project_path("./example/*.c"), {public = true})
  117. add_files(project_path("./test/qemu/platform/qemuPlatform.c"), {public = true})
  118. add_files(project_path("./test/qemu/platform/qemuStartup.c"), {public = true})
  119. add_files(project_path("./test/qemu/platform/qemuFault.c"), {public = true})
  120. add_files(project_path("./test/qemu/platform/qemuSyscalls.c"), {public = true})
  121. add_files(project_path("./test/qemu/platform/qemuFreertosHeap.c"), {public = true})
  122. add_files(project_path("./test/unityTest/**.c"), {public = true}, {cxflags = "-w"})
  123. add_files(project_path("./test/externalModule/valloc/*.c"), {public = true}, {cxflags = "-w"})
  124. add_files(project_path("./test/externalModule/tlsf/*.c"), {public = true}, {cxflags = "-w"})
  125. add_files(project_path("./test/externalModule/cJSON/*.c"), {public = true}, {cxflags = "-w"})
  126. add_files(project_path("./test/externalModule/unity/src/*.c"), {public = true}, {cxflags = "-w"})
  127. add_files(project_path("./test/externalModule/yyjson/*.c"), {public = true}, {cxflags = "-w"})
  128. add_files(project_path("./test/externalModule/FreeRTOS-Kernel/list.c"), {public = true}, {cxflags = "-w"})
  129. add_files(project_path("./test/externalModule/FreeRTOS-Kernel/queue.c"), {public = true}, {cxflags = "-w"})
  130. add_files(project_path("./test/externalModule/FreeRTOS-Kernel/tasks.c"), {public = true}, {cxflags = "-w"})
  131. add_files(project_path("./test/externalModule/FreeRTOS-Kernel/timers.c"), {public = true}, {cxflags = "-w"})
  132. add_files(project_path("./test/externalModule/FreeRTOS-Kernel/event_groups.c"), {public = true}, {cxflags = "-w"})
  133. add_files(project_path("./test/externalModule/FreeRTOS-Kernel/stream_buffer.c"), {public = true}, {cxflags = "-w"})
  134. add_files(project_path("test/externalModule/FreeRTOS-Kernel/portable/GCC/" .. freertosPort .. "/port.c"),
  135. {public = true},
  136. {cxflags = "-w"})
  137. add_files(project_path("./test/externalModule/FreeRTOS-Kernel/portable/MemMang/heap_4.c"), {public = true}, {cxflags = "-w"})
  138. after_build(function(target)
  139. local elfFile = target:targetfile()
  140. local outDir = path.directory(elfFile)
  141. local baseName = path.basename(elfFile)
  142. local binFile = path.join(outDir, baseName .. ".bin")
  143. local hexFile = path.join(outDir, baseName .. ".hex")
  144. os.execv("arm-none-eabi-objcopy", {"-O", "binary", elfFile, binFile})
  145. os.execv("arm-none-eabi-objcopy", {"-O", "ihex", elfFile, hexFile})
  146. end)
  147. end
  148. target("RyanJsonQemu", function()
  149. setupRyanJsonQemuTarget({
  150. cpu = "cortex-m4",
  151. freertosPort = "ARM_CM4F",
  152. isCm4f = true,
  153. })
  154. end)
  155. target("RyanJsonQemuCm3", function()
  156. setupRyanJsonQemuTarget({
  157. cpu = "cortex-m3",
  158. freertosPort = "ARM_CM3",
  159. isCm4f = false,
  160. })
  161. add_defines("RyanJsonQemuSoftUnalignedTrap")
  162. end)