stringifyKernel.lua 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. function stringifyKernel(filenameIn, filenameOut, kernelMethod)
  2. local BUFSIZE = 10*1024*1024 -- 10MB
  3. local f = io.open(filenameIn,"r");
  4. local fw = io.open(filenameOut,"w");
  5. fw:write("//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project\n")
  6. fw:write("static const char* " .. kernelMethod .. "= \\\n")
  7. local cc, lc, wc = 0, 0, 0 -- char, line, and word counts
  8. while true do
  9. local lines, rest = f:read(BUFSIZE, "*line")
  10. if not lines then break end
  11. local i = 0
  12. local startpos = 0
  13. local slen = string.len(lines)
  14. local endpos = 0
  15. while true do
  16. i = string.find(lines, "\n", i+1) -- find 'next' newline
  17. if i == nil then
  18. endpos = slen
  19. else
  20. endpos = i
  21. end
  22. oneline = string.sub(lines,startpos,endpos)
  23. oneline = string.gsub(oneline,"\n","")
  24. oneline = string.gsub(oneline,"\"","\\\"");
  25. oneline = '\"' .. oneline .. '\\n\"'
  26. oneline = string.gsub(oneline,"\\\\n","")
  27. oneline = oneline .. "\n"
  28. --print(oneline)
  29. fw:write(oneline);
  30. if i == nil then break end
  31. startpos = i+1
  32. end
  33. if rest then lines = lines .. rest .. '\n' end
  34. cc = cc + string.len(lines)
  35. -- count words in the chunk
  36. local _,t = string.gsub(lines, "%S+", "")
  37. wc = wc + t
  38. -- count newlines in the chunk
  39. _,t = string.gsub(lines, "\n", "\n")
  40. lc = lc + t
  41. end
  42. --print("stringified " .. filenameIn .. " into " .. filenameOut .. " processed " .. lc .. " lines")
  43. print(filenameIn .. " (" .. lc .. " lines)")
  44. f:close()
  45. fw:write(";\n")
  46. fw:close()
  47. end
  48. function preprocessKernel(kernelfile, filenameOut, kernelMethod)
  49. lcpp=require('lcpp');
  50. local out=lcpp.compileFile(kernelfile);
  51. local embedFileName = kernelfile .. ".embed"
  52. local fw = io.open(embedFileName,"w");
  53. fw:write(out)
  54. fw:close()
  55. stringifyKernel(embedFileName,filenameOut, kernelMethod);
  56. end
  57. newoption {
  58. trigger = "kernelfile",
  59. value = "kernelpath",
  60. description = "full path to the kernel source input file"
  61. }
  62. newoption {
  63. trigger = "headerfile",
  64. value = "path",
  65. description = "full path to the header output file"
  66. }
  67. newoption {
  68. trigger = "stringname",
  69. value = "var",
  70. description = "name of the kernel string variable"
  71. }
  72. newaction {
  73. trigger = "stringify",
  74. description = "stringify kernels source code into strings",
  75. execute = function()
  76. preprocessKernel( _OPTIONS["kernelfile"] , _OPTIONS["headerfile"], _OPTIONS["stringname"])
  77. end
  78. }