xmake.lua 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. -- 自动生成 compile_commands.json,方便 VSCode/Clangd 做代码补全与跳转
  2. add_rules("plugin.compile_commands.autoupdate", {outputdir = ".vscode"})
  3. target("RyanJson", function()
  4. -- 目标类型:二进制可执行文件
  5. set_kind("binary")
  6. -- 编译工具链与平台配置
  7. -- set_toolchains("gcc") -- 使用 GCC
  8. set_toolchains("clang") -- 使用 Clang 编译器
  9. set_plat("linux") -- 平台:Linux
  10. set_arch("x86") -- 架构:x86(32位)
  11. set_languages("gnu99") -- 使用 GNU C99 标准,启用 GNU 扩展
  12. -- 编译优化策略
  13. set_policy("build.ccache", false) -- 禁用 ccache 缓存
  14. set_optimize("fastest") -- 使用 -O3,最高级别优化
  15. -- 警告设置:启用所有警告(Clang 下相当于 -Weverything)
  16. set_warnings("everything")
  17. add_defines("RyanJsonSnprintfSupportScientific=true")
  18. add_defines("RyanJsonLinuxTestEnv")
  19. -- 定义宏:启用 Fuzzer 功能
  20. -- Fuzzer 与覆盖率相关编译/链接选项
  21. add_defines("isEnableFuzzer")
  22. add_cxflags("-fsanitize=fuzzer", {force = true})
  23. add_ldflags("-fsanitize=fuzzer", {force = true})
  24. add_cxflags("-fprofile-instr-generate", "-fcoverage-mapping", {force = true})
  25. add_ldflags("-fprofile-instr-generate", "-fcoverage-mapping", {force = true})
  26. -- 链接器安全硬化与优化选项
  27. add_ldflags(
  28. "-flto", -- 启用 LTO(链接时优化)
  29. "-fPIE", -- 位置无关可执行
  30. "-pie", -- 与 -fPIE 搭配,启用 ASLR
  31. "-fno-omit-frame-pointer", -- 保留帧指针,便于调试和崩溃分析
  32. "-fstack-clash-protection", -- 栈碰撞保护
  33. "-Wl,-z,relro", -- 重定位表只读
  34. "-Wl,-z,now", -- 立即绑定符号
  35. "-Wl,-z,noexecstack", -- 栈不可执行
  36. "-Wl,-z,separate-code", -- 代码段与数据段分离
  37. {force = true}
  38. )
  39. -- Sanitizer 检测项:运行时错误检测
  40. add_cxflags("-fsanitize=alignment", "-fno-sanitize-recover=undefined", {force = true})
  41. add_ldflags(
  42. "-fsanitize=alignment", -- 检查未对齐访问
  43. "-fno-sanitize-recover=undefined", -- 遇到未定义行为立即终止
  44. "-fsanitize=address", -- 内存越界、释放后使用
  45. "-fsanitize=leak", -- 内存泄漏
  46. "-fsanitize=undefined", -- 常见未定义行为
  47. "-fsanitize=pointer-compare", -- 无效指针比较
  48. "-fsanitize=pointer-subtract", -- 无效指针相减
  49. "-fsanitize=bounds", -- 数组越界
  50. "-fsanitize=float-divide-by-zero", -- 浮点除零
  51. "-fsanitize=float-cast-overflow", -- 浮点转整数溢出
  52. -- "-fsanitize=thread", -- 多线程数据竞争
  53. -- "-fsanitize=memory", -- 未初始化内存使用
  54. -- "-fsanitize=safe-stack", -- 栈分离机制
  55. -- "-fsanitize=cfi", -- 控制流完整性(需 LTO 与 Clang)
  56. {force = true}
  57. )
  58. -- 编译器警告与静态分析
  59. add_cxflags(
  60. "-g3", -- 生成详细的调试信息
  61. "-pedantic", -- 严格遵循 ISO C 标准
  62. "-Wall", -- 常见警告
  63. "-Wextra", -- 额外警告
  64. "-Wconversion", -- 隐式类型转换风险
  65. "-Wsign-conversion", -- 有符号/无符号转换风险
  66. "-Wdouble-promotion", -- float 自动提升为 double
  67. "-Wstrict-prototypes", -- 函数声明必须带参数类型
  68. "-Wold-style-definition", -- 检测旧式函数定义
  69. "-Wimplicit-fallthrough", -- switch/case 未显式 fallthrough
  70. "-Wshadow", -- 局部变量遮蔽
  71. "-Wcast-align", -- 类型转换可能导致未对齐
  72. "-Wpointer-arith", -- 指针运算风险
  73. "-Warray-bounds", -- 数组越界访问
  74. "-Wshift-overflow", -- 位移溢出
  75. "-Wformat-truncation", -- 格式化字符串截断风险
  76. "-Walloc-size", -- 分配大小问题
  77. "-Wnull-dereference", -- 空指针解引用
  78. "-Wtautological-compare", -- 恒真/恒假的比较
  79. "-Wstrict-overflow", -- 有符号溢出优化假设
  80. "-Wmissing-prototypes", -- 全局函数未在头文件声明
  81. "-Wmissing-declarations", -- 全局变量/函数未声明
  82. "-Wredundant-decls", -- 重复声明
  83. "-Wunreachable-code", -- 不可达代码
  84. "-Wtype-limits", -- 比较恒真/恒假的表达式(如 unsigned < 0)
  85. "-Wshift-negative-value", -- 对负数移位
  86. "-Wdiv-by-zero", -- 除以零(编译期可分析)
  87. "-Wformat-security", -- 格式化字符串安全问题
  88. "-Wdisabled-optimization", -- 被禁用的优化
  89. "-Wreturn-local-addr", -- 返回局部变量地址
  90. "-Wdeprecated", -- 使用已弃用特性
  91. -- "-Wunsafe-buffer-usage", -- 不安全的数组/指针用法
  92. "-Wuninitialized", -- 使用未初始化变量
  93. "-fstack-protector-strong",-- 栈保护
  94. "-Wmissing-include-dirs", -- 头文件目录缺失
  95. "-Wcast-qual", -- 丢弃 const/volatile 限定符
  96. "-Wconditional-uninitialized", -- 条件路径未初始化
  97. "-Wcovered-switch-default", -- default 覆盖所有枚举值
  98. "-Wformat-nonliteral", -- 非字面量格式串
  99. "-Wformat-signedness", -- 格式化与符号性不匹配
  100. "-Wvla", -- 可变长度数组
  101. "-fno-common", -- 禁止旧式多重定义
  102. "-fno-strict-aliasing", -- 禁止严格别名优化,减少别名相关 UB 风险
  103. "-Wdocumentation",
  104. "-Wparentheses-equality",
  105. "-Wno-documentation", -- 临时关闭文档警告
  106. -- "-Wno-parentheses-equality", -- 临时关闭括号比较警告
  107. "-Wno-extra-semi-stmt", -- 关闭分号警告
  108. "-Wno-unsafe-buffer-usage", -- 关闭不安全的数组/指针用法警告
  109. "-Wno-declaration-after-statement", -- 关闭声明在语句后的警告
  110. "-Wno-padded", -- 关闭结构体填充警告
  111. "-Wno-switch-default", -- 关闭 switch 语句缺少 default 的警告
  112. "-Wno-unused-macros", -- 关闭未使用的宏定义警告
  113. "-Wno-unused-includes", -- 关闭未使用的头文件警告
  114. {force = true}
  115. )
  116. -- 头文件
  117. add_includedirs('./RyanJson', {public = true})
  118. add_includedirs('./example', {public = true})
  119. add_includedirs('./test/fuzzer', {public = true})
  120. add_includedirs('./test', {public = true})
  121. add_includedirs('./test/baseTest', {public = true})
  122. add_includedirs('./test/baseTest/equality', {public = true})
  123. add_includedirs('./test/RFC8259Test', {public = true})
  124. add_includedirs('./test/externalModule/valloc', {public = true})
  125. add_includedirs('./test/externalModule/tlsf', {public = true})
  126. add_includedirs('./test/externalModule/cJSON', {public = true})
  127. add_includedirs('./test/externalModule/yyjson', {public = true})
  128. -- 源文件
  129. add_files('./RyanJson/*.c', {public = true})
  130. add_files('./example/*.c', {public = true})
  131. add_files('./test/fuzzer/*.c', {public = true})
  132. add_files('./test/*.c', {public = true}, {cxflags = "-w"}) -- 测试代码,关闭警告
  133. add_files('./test/baseTest/*.c', {public = true}, {cxflags = "-w"}) -- 基础测试,关闭警告
  134. add_files('./test/baseTest/equality/*.c', {public = true}, {cxflags = "-w"}) -- 一致性测试
  135. add_files('./test/RFC8259Test/*.c', {public = true}, {cxflags = "-w"}) --
  136. add_files('./test/externalModule/valloc/*.c', {public = true}, {cxflags = "-w"}) -- valloc,关闭警告
  137. add_files('./test/externalModule/tlsf/*.c', {public = true}, {cxflags = "-w"}) -- tlsf,关闭警告
  138. add_files('./test/externalModule/cJSON/*.c', {public = true}, {cxflags = "-w"}) -- 第三方库 cJSON,关闭警告
  139. add_files('./test/externalModule/yyjson/*.c', {public = true}, {cxflags = "-w"}) -- 第三方库 yyjson,关闭警告
  140. end)