embed.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. --
  2. -- Embed the Lua scripts into src/host/scripts.c as static data buffers.
  3. -- Embeds minified versions of the actual scripts by default, rather than
  4. -- bytecode, as bytecodes are not portable to different architectures. Use
  5. -- the `--bytecode` flag to override.
  6. --
  7. local scriptCount = 0
  8. local function loadScript(fname)
  9. fname = path.getabsolute(fname)
  10. local f = io.open(fname, "rb")
  11. local s = assert(f:read("*all"))
  12. f:close()
  13. return s
  14. end
  15. local function stripScript(s)
  16. -- strip tabs
  17. local result = s:gsub("[\t]", "")
  18. -- strip any CRs
  19. result = result:gsub("[\r]", "")
  20. -- strip out block comments
  21. result = result:gsub("[^\"']%-%-%[%[.-%]%]", "")
  22. result = result:gsub("[^\"']%-%-%[=%[.-%]=%]", "")
  23. result = result:gsub("[^\"']%-%-%[==%[.-%]==%]", "")
  24. -- strip out inline comments
  25. result = result:gsub("\n%-%-[^\n]*", "\n")
  26. -- strip duplicate line feeds
  27. result = result:gsub("\n+", "\n")
  28. -- strip out leading comments
  29. result = result:gsub("^%-%-[^\n]*\n", "")
  30. return result
  31. end
  32. local function outputScript(result, script)
  33. local data = script.data
  34. local length = #data
  35. if length > 0 then
  36. script.table = string.format("builtin_script_%d", scriptCount)
  37. scriptCount = scriptCount + 1
  38. buffered.writeln(result, "// ".. script.name)
  39. buffered.writeln(result, "static const unsigned char " .. script.table .. "[] = {")
  40. for i = 1, length do
  41. buffered.write(result, string.format("%3d, ", data:byte(i)))
  42. if (i % 32 == 0) then
  43. buffered.writeln(result)
  44. end
  45. end
  46. buffered.writeln(result, "};")
  47. buffered.writeln(result)
  48. end
  49. end
  50. local function addScript(result, filename, name, data)
  51. if not data then
  52. if not path.hasextension(filename, ".lua") then
  53. data = loadScript(filename)
  54. elseif _OPTIONS["bytecode"] then
  55. verbosef("Compiling... " .. filename)
  56. local output = path.replaceextension(filename, ".luac")
  57. local res, err = os.compile(filename, output);
  58. if res ~= nil then
  59. data = loadScript(output)
  60. os.remove(output)
  61. else
  62. print(err)
  63. print("Embedding source instead.")
  64. data = stripScript(loadScript(filename))
  65. end
  66. else
  67. data = stripScript(loadScript(filename))
  68. end
  69. end
  70. local script = {}
  71. script.filename = filename
  72. script.name = name
  73. script.data = data
  74. table.insert(result, script)
  75. end
  76. -- Prepare the file header
  77. local result = buffered.new()
  78. buffered.writeln(result, "/* Premake's Lua scripts, as static data buffers for release mode builds */")
  79. buffered.writeln(result, "/* DO NOT EDIT - this file is autogenerated - see BUILD.txt */")
  80. buffered.writeln(result, "/* To regenerate this file, run: premake5 embed */")
  81. buffered.writeln(result, "")
  82. buffered.writeln(result, '#include "host/premake.h"')
  83. buffered.writeln(result, "")
  84. -- Find all of the _manifest.lua files within the project
  85. local mask = path.join(_MAIN_SCRIPT_DIR, "**/_manifest.lua")
  86. local manifests = os.matchfiles(mask)
  87. -- Find all of the _user_modules.lua files within the project
  88. local userModuleFiles = {}
  89. userModuleFiles = table.join(userModuleFiles, os.matchfiles(path.join(_MAIN_SCRIPT_DIR, "**/_user_modules.lua")))
  90. userModuleFiles = table.join(userModuleFiles, os.matchfiles(path.join(_MAIN_SCRIPT_DIR, "_user_modules.lua")))
  91. -- Generate table of embedded content.
  92. local contentTable = {}
  93. local nativeTable = {}
  94. print("Compiling... ")
  95. for mi = 1, #manifests do
  96. local manifestName = manifests[mi]
  97. local manifestDir = path.getdirectory(manifestName)
  98. local moduleName = path.getbasename(manifestDir)
  99. local baseDir = path.getdirectory(manifestDir)
  100. local files = dofile(manifests[mi])
  101. for fi = 1, #files do
  102. local filename = path.join(manifestDir, files[fi])
  103. addScript(contentTable, filename, path.getrelative(baseDir, filename))
  104. end
  105. -- find native code in modules.
  106. if moduleName ~= "src" then
  107. local nativeFile = path.join(manifestDir, 'native', moduleName .. '.c')
  108. if os.isfile(nativeFile) then
  109. local pretty_name = moduleName:gsub("^%l", string.upper)
  110. table.insert(nativeTable, pretty_name)
  111. end
  112. end
  113. end
  114. addScript(contentTable, path.join(_SCRIPT_DIR, "../src/_premake_main.lua"), "src/_premake_main.lua")
  115. addScript(contentTable, path.join(_SCRIPT_DIR, "../src/_manifest.lua"), "src/_manifest.lua")
  116. -- Add the list of modules
  117. local modules = dofile("../src/_modules.lua")
  118. for _, userModules in ipairs(userModuleFiles) do
  119. modules = table.join(modules, dofile(userModules))
  120. end
  121. addScript(contentTable, "_modules.lua", "src/_modules.lua", "return {" .. table.implode(modules, '"', '"', ', ') .. "}")
  122. -- Embed the actual script contents
  123. print("Embedding...")
  124. for mi = 1, #contentTable do
  125. outputScript(result, contentTable[mi])
  126. end
  127. -- Generate an index of the script file names. Script names are stored
  128. -- relative to the directory containing the manifest, i.e. the main
  129. -- Xcode script, which is at $/modules/xcode/xcode.lua is stored as
  130. -- "xcode/xcode.lua".
  131. buffered.writeln(result, "const buildin_mapping builtin_scripts[] = {")
  132. for mi = 1, #contentTable do
  133. if contentTable[mi].table then
  134. buffered.writeln(result, string.format('\t{"%s", %s, sizeof(%s)},', contentTable[mi].name, contentTable[mi].table, contentTable[mi].table))
  135. else
  136. buffered.writeln(result, string.format('\t{"%s", NULL, 0},', contentTable[mi].name))
  137. end
  138. end
  139. buffered.writeln(result, "\t{NULL, NULL, 0}")
  140. buffered.writeln(result, "};")
  141. buffered.writeln(result, "")
  142. -- write out the registerModules method.
  143. for _, name in ipairs(nativeTable) do
  144. buffered.writeln(result, string.format("extern void register%s(lua_State* L);", name))
  145. end
  146. buffered.writeln(result, "")
  147. buffered.writeln(result, "void registerModules(lua_State* L)")
  148. buffered.writeln(result, "{")
  149. buffered.writeln(result, "\t(void)(L);")
  150. for _, name in ipairs(nativeTable) do
  151. buffered.writeln(result, string.format("\tregister%s(L);", name))
  152. end
  153. buffered.writeln(result, "}")
  154. buffered.writeln(result, "")
  155. -- Write it all out. Check against the current contents of scripts.c first,
  156. -- and only overwrite it if there are actual changes.
  157. print("Writing...")
  158. local scriptsFile = path.getabsolute(path.join(_SCRIPT_DIR, "../src/scripts.c"))
  159. local output = buffered.tostring(result)
  160. local f, err = os.writefile_ifnotequal(output, scriptsFile);
  161. if (f < 0) then
  162. error(err, 0)
  163. elseif (f > 0) then
  164. printf("Generated %s...", path.getrelative(os.getcwd(), scriptsFile))
  165. end