update_inline_libs.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 bittype = "source"
  15. local typename = bittype
  16. if bittype == "32bit" then
  17. typename = ""
  18. else
  19. typename = "_" .. bittype
  20. end
  21. local function lsdir(path, files, shortname)
  22. local cmd = "dir /b " .. (shortname and " " or " /s ") .. path
  23. log.info("cmd", cmd)
  24. local exe = io.popen(cmd)
  25. if exe then
  26. for line in exe:lines() do
  27. if string.endsWith(line, ".lua") then
  28. log.info("found", line)
  29. table.insert(files, line)
  30. end
  31. end
  32. exe:close()
  33. end
  34. end
  35. local function oscall(cmd, quite, cwd)
  36. if cwd and Base_CWD then
  37. lfs.chdir(cwd)
  38. end
  39. if tool_debug then
  40. log.info("cmd", cmd)
  41. end
  42. local exe = io.popen(cmd)
  43. if exe then
  44. for line in exe:lines() do
  45. if not quite then
  46. log.info("cmd", line)
  47. end
  48. end
  49. exe:close()
  50. end
  51. if cwd and Base_CWD then
  52. lfs.chdir(Base_CWD)
  53. end
  54. end
  55. function TLD(buff, T, D)
  56. buff:pack("bb", T, D:len())
  57. buff:write(D)
  58. end
  59. local files = {}
  60. lsdir("corelib", files, true)
  61. oscall("mkdir tmp")
  62. local path = "..\\luat\\vfs\\luat_inline_libs".. typename .. ".c"
  63. local f = io.open(path, "wb")
  64. if not f then
  65. log.info("can't write", path)
  66. os.exit(-1)
  67. end
  68. f:write([[#include "luat_base.h"]])
  69. f:write("\r\n")
  70. f:write([[#include "luat_fs.h"]])
  71. f:write("\r\n")
  72. f:write([[#include "luat_luadb.h"]])
  73. f:write("\r\n\r\n")
  74. kvs = {}
  75. for _, value in ipairs(files) do
  76. local data = nil
  77. if bittype == "source" then
  78. data = io.readFile("corelib\\" .. value)
  79. else
  80. local lf = loadfile("corelib\\" .. value)
  81. data = string.dump(lf, true)
  82. end
  83. -- local cmd =
  84. -- io.popen("luac_" .. bittype .. ".exe -s -o tmp.luac " .. "core\\" .. value):read("*a")
  85. -- local data = io.readFile("tmp.luac")
  86. local buff = zbuff.create(256*1024)
  87. -- local data = io.readFile("tmp\\" .. value .. "c")
  88. buff:write(data)
  89. f:write("//------- " .. value .. "\r\n")
  90. local k = "luat_inline2_" .. value:sub(1, value:len() - 4) .. typename
  91. print(value, #data, k)
  92. table.insert(kvs, {size=#data,ptr=k, name=value})
  93. f:write("const char ".. k .."[] = {\r\n")
  94. local index = 0
  95. local max = #data
  96. while index < max do
  97. if index % 8 == 0 then
  98. f:write("\r\n")
  99. end
  100. f:write(string.format("0x%02X, ", buff[index]))
  101. index = index + 1
  102. end
  103. f:write("};\r\n\r\n")
  104. end
  105. f:write("const luadb_file_t luat_inline2_libs".. typename .. "[] = {\r\n")
  106. for _, value in ipairs(kvs) do
  107. f:write(" {.name=\"" .. value.name .. "\",.size=" .. tostring(value.size) .. ", .ptr=" .. value.ptr .. "},\r\n")
  108. end
  109. f:write(" {.name=\"\",.size=0,.ptr=NULL}\r\n")
  110. f:write("};\r\n\r\n")
  111. f:close()
  112. log.info("all done")
  113. os.exit(0)