update_inline_libs.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. --[[
  2. 本文件用于生成内嵌到固件里的脚本
  3. 默认是corelib目录里的全部.lua文件
  4. 如需新增或更新, 把.lua文件放入corelib目录, 在当前目录下执行:
  5. luatos_32bit.exe update_inline_libs.lua
  6. 相关exe文件可以在 https://gitee.com/openLuat/LuatOS/attach_files 下载
  7. ]]
  8. -- 主要配置项, lua分需要区分两种编译环境:
  9. -- lua_Integer的大小, 受 LUA_32BITS控制, 默认是启用
  10. -- size_t 大小, 与平台相关, MCU上通常为32bit
  11. -- 虽然可以两两组成, 但实际情况只有2种,当前:
  12. -- local bittype = "64bit_size32"
  13. local bittype = "32bit"
  14. local typename = bittype
  15. if bittype == "32bit" then
  16. typename = ""
  17. else
  18. typename = "_" .. bittype
  19. end
  20. local function lsdir(path, files, shortname)
  21. local cmd = "dir /b " .. (shortname and " " or " /s ") .. path
  22. log.info("cmd", cmd)
  23. local exe = io.popen(cmd)
  24. if exe then
  25. for line in exe:lines() do
  26. if string.endsWith(line, ".lua") then
  27. log.info("found", line)
  28. table.insert(files, line)
  29. end
  30. end
  31. exe:close()
  32. end
  33. end
  34. local function oscall(cmd, quite, cwd)
  35. if cwd and Base_CWD then
  36. lfs.chdir(cwd)
  37. end
  38. if tool_debug then
  39. log.info("cmd", cmd)
  40. end
  41. local exe = io.popen(cmd)
  42. if exe then
  43. for line in exe:lines() do
  44. if not quite then
  45. log.info("cmd", line)
  46. end
  47. end
  48. exe:close()
  49. end
  50. if cwd and Base_CWD then
  51. lfs.chdir(Base_CWD)
  52. end
  53. end
  54. function TLD(buff, T, D)
  55. buff:pack("bb", T, D:len())
  56. buff:write(D)
  57. end
  58. local files = {}
  59. lsdir("corelib", files, true)
  60. oscall("mkdir tmp")
  61. local path = "..\\luat\\vfs\\luat_inline_libs".. typename .. ".c"
  62. local f = io.open(path, "wb")
  63. if not f then
  64. log.info("can't write", path)
  65. os.exit(-1)
  66. end
  67. f:write([[#include "luat_base.h"]])
  68. f:write("\r\n")
  69. f:write([[#include "luat_fs.h"]])
  70. f:write("\r\n")
  71. f:write([[#include "luat_luadb.h"]])
  72. f:write("\r\n\r\n")
  73. kvs = {}
  74. for _, value in ipairs(files) do
  75. local lf = loadfile("corelib\\" .. value)
  76. local data = string.dump(lf, true)
  77. -- local cmd =
  78. -- io.popen("luac_" .. bittype .. ".exe -s -o tmp.luac " .. "core\\" .. value):read("*a")
  79. -- local data = io.readFile("tmp.luac")
  80. local buff = zbuff.create(256*1024)
  81. -- local data = io.readFile("tmp\\" .. value .. "c")
  82. buff:write(data)
  83. f:write("//------- " .. value .. "\r\n")
  84. local k = "luat_inline2_" .. value:sub(1, value:len() - 4) .. typename
  85. print(value, #data, k)
  86. table.insert(kvs, {size=#data,ptr=k, name=value})
  87. f:write("const char ".. k .."[] = {\r\n")
  88. local index = 0
  89. local max = #data
  90. while index < max do
  91. if index % 8 == 0 then
  92. f:write("\r\n")
  93. end
  94. f:write(string.format("0x%02X, ", buff[index]))
  95. index = index + 1
  96. end
  97. f:write("};\r\n\r\n")
  98. end
  99. f:write("const luadb_file_t luat_inline2_libs".. typename .. "[] = {\r\n")
  100. for _, value in ipairs(kvs) do
  101. f:write(" {.name=\"" .. value.name .. "\",.size=" .. tostring(value.size) .. ", .ptr=" .. value.ptr .. "},\r\n")
  102. end
  103. f:write(" {.name=\"\",.size=0,.ptr=NULL}\r\n")
  104. f:write("};\r\n\r\n")
  105. f:close()
  106. log.info("all done")
  107. os.exit(0)