-
Notifications
You must be signed in to change notification settings - Fork 6
/
codegen.lua
274 lines (227 loc) · 11.2 KB
/
codegen.lua
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
function scandir(command)
local i, t, popen = 0, {}, io.popen
local pfile = popen(command)
for filename in pfile:lines() do
i = i + 1
t[i] = filename
end
pfile:close()
return t
end
print "=====Generating Code====="
local dotHHeader = "// Autogenerated by codegen.lua during premake process\n\n#pragma once\n\n"
local file
-- make ./build/codegen/codegen.h
file = io.open("./build/codegen/codegen.h", "w")
file:write(dotHHeader)
file:write('#define _CRT_SECURE_NO_WARNINGS // for stb\n\n')
file:write("#include <vector>\n")
file:write("#include \"shared/vector.h\"\n\n")
file:write("typedef float SampleType_1d;\n")
file:write("typedef Vec2 SampleType_2d;\n\n")
file:write("using SampleGenerate_1d = void(*)(std::vector<float>& values, size_t numValues, const char* cacheKey, bool wantUnique);\n\n")
file:write("struct SampleGenerateInfo_1d\n{\n")
file:write(" SampleGenerate_1d function;\n")
file:write(" const char* cacheKey;\n")
file:write(" const char* sampleFamily;\n")
file:write(" const char* sampleType;\n")
file:write(" const char* name;\n")
file:write(" bool progressive;\n")
file:write(" bool randomized;\n")
file:write("};\n\n")
file:write("using SampleGenerate_2d = void(*)(std::vector<Vec2>& values, size_t numValues, const char* cacheKey, bool wantUnique);\n\n")
file:write("struct SampleGenerateInfo_2d\n{\n")
file:write(" SampleGenerate_2d function;\n")
file:write(" const char* cacheKey;\n")
file:write(" const char* sampleFamily;\n")
file:write(" const char* sampleType;\n")
file:write(" const char* name;\n")
file:write(" bool progressive;\n")
file:write(" bool randomized;\n")
file:write("};\n\n")
file:write("#define countof(array) (sizeof(array) / sizeof(array[0]))\n\n");
file:write('#include "shared/datacache.h"\n')
file:write('#include "tests.h"\n')
file:write('#include "samples.h"\n')
file:write('#include "autotest.h"\n')
file:close()
-- gather the list of sample familiessampl
local sampleFamilies = scandir('cd ./src/families/ && ls -d ./*/ && cd ../..')
-- make ./build/codegen/samples.h
file = io.open("./build/codegen/samples.h", "w")
file:write(dotHHeader)
for k,v in pairs(sampleFamilies) do
local sampleFamily = string.sub(v,3,-2)
file:write('#include "'..sampleFamily..'/samples/samples.h"\n')
end
file:close()
-- make ./build/codegen/X/samples/samples.h
for k,v in pairs(sampleFamilies) do
local sampleFamily = string.sub(v,3,-2)
file = io.open("./build/codegen/"..sampleFamily.."/samples/samples.h", "w")
file:write(dotHHeader)
local sampleTypes = scandir('cd ./src/families/'..sampleFamily..'/samples/ && ls -d ./*/ && cd ../../..')
for k2,v2 in pairs(sampleTypes) do
local sampleType = string.sub(v2,3,-2)
file:write('#include "'..sampleType..'/samples.h"\n')
end
file:write("\nnamespace "..sampleFamily.."\n{\n")
file:write(" extern std::vector<std::vector<SampleGenerateInfo"..sampleFamily..">> sampleFunctions;\n")
file:write("}\n")
file:close()
end
-- make ./build/codegen/X/samples/samples.cpp
for k,v in pairs(sampleFamilies) do
local sampleFamily = string.sub(v,3,-2)
file = io.open("./build/codegen/"..sampleFamily.."/samples/samples.cpp", "w")
file:write(dotHHeader)
file:write('#include "codegen.h"\n')
file:write('#include "'..sampleFamily..'/samples/samples.h"\n')
file:write("\nnamespace "..sampleFamily.."\n{\n")
file:write(" std::vector<std::vector<SampleGenerateInfo"..sampleFamily..">> sampleFunctions =\n {\n")
local sampleTypes = scandir('cd ./src/families/'..sampleFamily..'/samples/ && ls -d ./*/ && cd ../../..')
for k3, v3 in pairs(sampleTypes) do
local sampleType = string.sub(v3,3,-2)
dofile("./src/families/"..sampleFamily.."/samples/"..sampleType.."/samples.lua")
file:write(" {\n")
for functionIndex, functionInfo in ipairs(sampleInfo.Functions) do
local cacheKey = sampleType.."::"..functionInfo.name;
file:write(" { [](std::vector<SampleType"..sampleFamily..">& values, size_t numValues, const char* cacheKey, bool wantUnique) {")
if functionInfo.progressive then
file:write(" DataCache::Instance().m_samples_"..sampleFamily..".GetSamples_Progressive(");
else
file:write(" DataCache::Instance().m_samples_"..sampleFamily..".GetSamples_NonProgressive(")
end
file:write("\""..cacheKey.."\", ")
file:write(sampleFamily.."::Samples::"..sampleInfo.CodeName.."::"..functionInfo.name..", ")
file:write("values, numValues, wantUnique, "..tostring(functionInfo.randomized) .."); }, \""..cacheKey.."\", \""..sampleFamily.."\", \""..sampleType.."\", \""..functionInfo.name.."\", "..tostring(functionInfo.progressive)..", "..tostring(functionInfo.randomized).." },\n")
end
file:write(" },\n")
end
file:write(" };\n}\n")
file:close()
end
-- make ./build/codegen/tests.h
file = io.open("./build/codegen/tests.h", "w")
file:write(dotHHeader)
for k,v in pairs(sampleFamilies) do
local sampleFamily = string.sub(v,3,-2)
file:write('#include "'..sampleFamily..'/tests/tests.h"\n')
end
file:close()
-- make ./build/codegen/autotest.h
file = io.open("./build/codegen/autotest.h", "w")
file:write(dotHHeader)
for k,v in pairs(sampleFamilies) do
local sampleFamily = string.sub(v,3,-2)
file:write('#include "'..sampleFamily..'/tests/autotest.h"\n')
end
file:close()
-- make ./build/codegen/datacachesavelist.h
file = io.open("./build/codegen/datacachesavelist.h", "w")
file:write(dotHHeader)
file:write("inline void SetupSaveKeys()\n{\n")
for k,v in pairs(sampleFamilies) do
local sampleFamily = string.sub(v,3,-2)
local sampleTypes = scandir('cd ./src/families/'..sampleFamily..'/samples/ && ls -d ./*/ && cd ../../..')
for k3, v3 in pairs(sampleTypes) do
local sampleType = string.sub(v3,3,-2)
dofile("./src/families/"..sampleFamily.."/samples/"..sampleType.."/samples.lua")
for functionIndex, functionInfo in ipairs(sampleInfo.Functions) do
if functionInfo.cache then
file:write(" DataCache::Instance().m_samples_"..sampleFamily..".SaveKey(\""..sampleType.."::"..functionInfo.name.."\", "..tostring(functionInfo.progressive)..");\n")
end
end
end
end
file:write("}\n")
file:close()
-- make ./build/codegen/X/tests/tests.h
for k,v in pairs(sampleFamilies) do
local sampleFamily = string.sub(v,3,-2)
file = io.open("./build/codegen/"..sampleFamily.."/tests/tests.h", "w")
file:write(dotHHeader)
local subTestTypes = scandir('cd ./src/families/'..sampleFamily..'/tests/ && ls -d ./*/ && cd ../../..')
for k2,v2 in pairs(subTestTypes) do
local subTestType = string.sub(v2,3,-2)
file:write('#include "'..subTestType..'/tests.h"\n')
end
file:close()
end
-- make ./build/codegen/X/tests/Y/tests.h
for k,v in pairs(sampleFamilies) do
local sampleFamily = string.sub(v,3,-2)
local subTestTypes = scandir('cd ./src/families/'..sampleFamily..'/tests/ && ls -d ./*/ && cd ../../..')
for k2,v2 in pairs(subTestTypes) do
local subTestType = string.sub(v2,3,-2)
file = io.open("./build/codegen/"..sampleFamily.."/tests/"..subTestType.."/tests.h", "w")
file:write(dotHHeader)
dofile("./src/families/"..sampleFamily.."/tests/"..subTestType.."/tests.lua")
file:write("namespace "..sampleFamily.."\n{\n namespace Tests\n {\n namespace "..testInfo.CodeName.."\n {\n")
for functionIndex, functionName in ipairs(testInfo.Functions) do
file:write(" void "..functionName.."(const std::vector<std::vector<SampleGenerateInfo"..sampleFamily..">>&, const char* testName, const char* fileNamePrefix);\n")
end
file:write(" };\n };\n};\n")
end
end
-- make ./build/codegen/X/tests/autotest.h
for k,v in pairs(sampleFamilies) do
local sampleFamily = string.sub(v,3,-2)
file = io.open("./build/codegen/"..sampleFamily.."/tests/autotest.h", "w")
file:write(dotHHeader)
local subTestTypes = scandir('cd ./src/families/'..sampleFamily..'/tests/ && ls -d ./*/ && cd ../../..')
for k2,v2 in pairs(subTestTypes) do
local subTestType = string.sub(v2,3,-2)
file:write('#include "'..subTestType..'/autotest.h"\n')
end
file:write("\nnamespace "..sampleFamily.."\n{\n namespace Tests\n {\n")
file:write(" inline void AutoTest()\n {\n")
local subTestTypes = scandir('cd ./src/families/'..sampleFamily..'/tests/ && ls -d ./*/ && cd ../../..')
for k2,v2 in pairs(subTestTypes) do
local subTestType = string.sub(v2,3,-2)
dofile("./src/families/"..sampleFamily.."/tests/"..subTestType.."/tests.lua")
file:write(" "..testInfo.CodeName.."::AutoTest();\n")
end
file:write(" };\n };\n};\n")
file:close()
end
-- make ./build/codegen/X/tests/Y/autotest.h
for k,v in pairs(sampleFamilies) do
local sampleFamily = string.sub(v,3,-2)
local subTestTypes = scandir('cd ./src/families/'..sampleFamily..'/tests/ && ls -d ./*/ && cd ../../..')
for k2,v2 in pairs(subTestTypes) do
local subTestType = string.sub(v2,3,-2)
dofile("./src/families/"..sampleFamily.."/tests/"..subTestType.."/tests.lua")
file = io.open("./build/codegen/"..sampleFamily.."/tests/"..subTestType.."/autotest.h", "w")
file:write(dotHHeader)
file:write("namespace "..sampleFamily.."\n{\n namespace Tests\n {\n namespace "..testInfo.CodeName.."\n {\n")
file:write(" inline void AutoTest()\n {\n")
for functionIndex, functionName in ipairs(testInfo.Functions) do
file:write(' printf("'..sampleFamily..'::'..subTestType..'::'..functionName..'...\\n");\n')
file:write(" "..functionName.."("..sampleFamily.."::sampleFunctions, \""..functionName.."\", \"\");\n")
file:write(' DataCache::Instance().PostTest();\n')
end
file:write(" }\n")
file:write(" };\n };\n};\n")
os.mkdir("./output/"..sampleFamily.."/tests/"..subTestType.."/")
end
end
-- make ./build/codegen/X/samples/Y/samples.h
for k,v in pairs(sampleFamilies) do
local sampleFamily = string.sub(v,3,-2)
local sampleTypes = scandir('cd ./src/families/'..sampleFamily..'/samples/ && ls -d ./*/ && cd ../../..')
for k2,v2 in pairs(sampleTypes) do
local sampleType = string.sub(v2,3,-2)
file = io.open("./build/codegen/"..sampleFamily.."/samples/"..sampleType.."/samples.h", "w")
file:write(dotHHeader)
dofile("./src/families/"..sampleFamily.."/samples/"..sampleType.."/samples.lua")
file:write("namespace "..sampleFamily.."\n{\n namespace Samples\n {\n namespace "..sampleInfo.CodeName.."\n {\n")
for functionIndex, functionInfo in ipairs(sampleInfo.Functions) do
file:write(" void "..functionInfo.name.."(std::vector<SampleType"..sampleFamily..">& values, size_t numValues, std::mt19937& rng);\n")
end
file:write(" };\n };\n};\n")
-- also make output/samples/X/Y/
os.mkdir("./output/"..sampleFamily.."/samples/"..sampleType.."/")
end
os.mkdir("./datacache/")
end