embed.lua 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. --
  2. -- Embed the Lua scripts into src/host/scripts.c as static data buffers.
  3. -- I embed the actual scripts, rather than Lua bytecodes, because the
  4. -- bytecodes are not portable to different architectures, which causes
  5. -- issues in Mac OS X Universal builds.
  6. --
  7. local scriptdir = path.getdirectory(_SCRIPT)
  8. local function stripfile(fname)
  9. local f = io.open(fname)
  10. local s = assert(f:read("*a"))
  11. f:close()
  12. -- strip tabs
  13. s = s:gsub("[\t]", "")
  14. -- strip any CRs
  15. s = s:gsub("[\r]", "")
  16. -- strip out comments
  17. s = s:gsub("\n%-%-[^\n]*", "")
  18. -- escape backslashes
  19. s = s:gsub("\\", "\\\\")
  20. -- strip duplicate line feeds
  21. s = s:gsub("\n+", "\n")
  22. -- strip out leading comments
  23. s = s:gsub("^%-%-\n", "")
  24. -- escape line feeds
  25. s = s:gsub("\n", "\\n")
  26. -- escape double quote marks
  27. s = s:gsub("\"", "\\\"")
  28. return s
  29. end
  30. local function writeline(out, s, continues)
  31. out:write("\t\"")
  32. out:write(s)
  33. out:write(iif(continues, "\"\n", "\",\n"))
  34. end
  35. local function writefile(out, fname, contents)
  36. local max = 1024
  37. out:write("\t/* " .. fname .. " */\n")
  38. -- break up large strings to fit in Visual Studio's string length limit
  39. local start = 1
  40. local len = contents:len()
  41. while start <= len do
  42. local n = len - start
  43. if n > max then n = max end
  44. local finish = start + n
  45. -- make sure I don't cut an escape sequence
  46. while contents:sub(finish, finish) == "\\" do
  47. finish = finish - 1
  48. end
  49. writeline(out, contents:sub(start, finish), finish < len)
  50. start = finish + 1
  51. end
  52. out:write("\n")
  53. end
  54. function doembed()
  55. -- load the manifest of script files
  56. scripts = dofile(path.join(scriptdir, "../src/_manifest.lua"))
  57. -- main script always goes at the end
  58. table.insert(scripts, "_premake_main.lua")
  59. -- open scripts.c and write the file header
  60. local out = io.open(path.join(scriptdir, "../src/host/scripts.c"), "w+b")
  61. out:write("/* Premake's Lua scripts, as static data buffers for release mode builds */\n")
  62. out:write("/* DO NOT EDIT - this file is autogenerated - see BUILD.txt */\n")
  63. out:write("/* To regenerate this file, run: premake4 embed */ \n\n")
  64. out:write("const char* builtin_scripts[] = {\n")
  65. for i,fn in ipairs(scripts) do
  66. print(fn)
  67. local s = stripfile(path.join(scriptdir,"../src/" .. fn))
  68. writefile(out, fn, s)
  69. end
  70. out:write("\t0\n};\n");
  71. out:close()
  72. end