summaryrefslogtreecommitdiff
path: root/build3/stringifyKernel.lua
blob: e3f963c60508b2f9e73f9e398b840bfa834ee141 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98


function stringifyKernel(filenameIn, filenameOut, kernelMethod)
  local BUFSIZE = 10*1024*1024     -- 10MB
	local f = io.open(filenameIn,"r");
   	local fw = io.open(filenameOut,"w");
   	fw:write("//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project\n")
   	fw:write("static const char* " .. kernelMethod .. "= \\\n")
    local cc, lc, wc = 0, 0, 0   -- char, line, and word counts
    while true do
      local lines, rest = f:read(BUFSIZE, "*line")
      if not lines then break end
      
      local i = 0
      local startpos = 0
      local slen = string.len(lines)
      local endpos = 0
    	while true do
      	i = string.find(lines, "\n", i+1)    -- find 'next' newline
      	if i == nil then 
      		endpos = slen
      	else
      	  endpos = i
      	end
      	
	      	oneline = string.sub(lines,startpos,endpos)
	      	oneline = string.gsub(oneline,"\n","")
					oneline = string.gsub(oneline,"\"","\\\"");
	      	oneline = '\"' .. oneline .. '\\n\"'
	      	oneline = string.gsub(oneline,"\\\\n","")
	      	oneline = oneline .. "\n"
	      	--print(oneline)
					fw:write(oneline);      	
      	
      	if i == nil then break end
      	startpos = i+1
      end
      
      if rest then lines = lines .. rest .. '\n' end
      cc = cc + string.len(lines)
      -- count words in the chunk
      local _,t = string.gsub(lines, "%S+", "")
      wc = wc + t
      -- count newlines in the chunk
      _,t = string.gsub(lines, "\n", "\n")
      lc = lc + t
    end
    --print("stringified " .. filenameIn .. " into " .. filenameOut .. " processed " .. lc .. " lines")
    print(filenameIn .. " (" .. lc .. " lines)")

 		f:close()
 		fw:write(";\n")
 		fw:close()
 end


 
function preprocessKernel(kernelfile, filenameOut, kernelMethod)
	lcpp=require('lcpp');
	local out=lcpp.compileFile(kernelfile);
	local embedFileName = kernelfile .. ".embed"
	local fw = io.open(embedFileName,"w");
	fw:write(out)
	fw:close()
	stringifyKernel(embedFileName,filenameOut, kernelMethod);
end


 newoption {
    trigger     = "kernelfile",
    value				=	"kernelpath",
    description = "full path to the kernel source input file"
  }

 newoption {
    trigger     = "headerfile",
    value				=	"path",
    description = "full path to the header output file"
  }

 newoption {
    trigger     = "stringname",
    value				=	"var",
    description = "name of the kernel string variable"
  }
  


 newaction {
   trigger     = "stringify",
   description = "stringify kernels source code into strings",
   execute = 	function()
   preprocessKernel( _OPTIONS["kernelfile"] , _OPTIONS["headerfile"], _OPTIONS["stringname"])
   end
}