xmake.lua 7.9 KB

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