-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.lua
More file actions
executable file
·311 lines (262 loc) · 8.4 KB
/
build.lua
File metadata and controls
executable file
·311 lines (262 loc) · 8.4 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/lua
--[[
This is a custom build script to compile haste using nvcc, the CUDA compiler.
It uses some Lua logic to generate a Makefile and executes it automatically.
It can be run as follows:
./build.lua - build haste in default mode (debug at the moment)
./build.lua debug - build haste in debug mode
./build.lua release - build haste in release mode
./build.lua clean - cleanup everything
If you have any questions, just ask me:
bob@bobsomers.com
]]--
--------------------------------------------------------------------------
-- MAKEFILE SETTINGS --
--------------------------------------------------------------------------
-- C++ and CUDA source files and directory
srcdir = "src"
srcs = {
"control_thread.cu",
"host.cpp",
"main.cpp",
"scripting.cpp",
"device/raytrace.cu",
"scripting/lua_extract.cpp",
"scripting/lua_macros.cpp",
"util/image.cpp",
}
-- binary names
bin = {
debug = "hasteD",
release = "haste"
}
-- paths to search for including header files
includes = {
"/opt/nvidia/gpusdk/C/common/inc",
"include",
"src",
debug = {
-- nothing special
},
release = {
-- nothing special
}
}
-- preprocessor defines
defines = {
debug = {
"DEBUG"
},
release = {
"RELEASE",
"NDEBUG"
}
}
-- compiler options
cflags = {
"m64", -- 64-bit arch
"arch=compute_20", -- compute capability
"code=sm_20", -- device code generation version
debug = {
"g", -- host debug symbols
"G", -- device debug symbols
"pg", -- gprof profiling
"-ptxas-options=-v",-- kernel register and memory usage
"Xcompiler -Wall" -- All warnings to g++, must be last
},
release = {
"O3", -- optimizer level 3
"use_fast_math", -- fast math library
"-ptxas-options=-v",-- kernel register and memory usage
"Xcompiler -Wall" -- All warnings to g++, must be last
}
}
-- object files location
objdir = {
debug = "obj/debug",
release = "obj/release"
}
-- static library directories
libdirs = {
"lib",
debug = {
-- nothing special
},
release = {
-- nothing special
}
}
-- static libraries to link
libs = {
"m", -- math library
"dl", -- dynamic linking
"luajit", -- fast lua
debug = {
-- nothing special
},
release = {
-- nothing special
}
}
-- linker flags
lflags = {
"m64", -- 64-bit arch
debug = {
-- nothing special
},
release = {
-- nothing special
}
}
--------------------------------------------------------------------------
-- DO NOT EDIT BELOW THIS LINE! --
--------------------------------------------------------------------------
-- function for generating nvcc command line options from tables
function genopts(both, additional, sep, desc)
io.write(desc .. "... ")
io.flush()
local gen = ""
if #both > 0 then
gen = gen .. sep .. table.concat(both, sep)
end
if #additional > 0 then
gen = gen .. sep .. table.concat(additional, sep)
end
print("done.")
return gen
end
-- check command line arguments
mode = "debug"
if #arg > 1 then
print "Usage: build [debug|release]"
return
elseif #arg == 1 then
if arg[1] == "-help" or arg[1] == "--help" then
print "Usage: build [debug|release]"
elseif arg[1] == "clean" or arg[1] == "debug" or arg[1] == "release" then
mode = arg[1]
else
print "Usage: build [debug|release]"
end
end
-- process source files
sources = {}
for i, v in ipairs(srcs) do
local path, file = string.match(v, "([^/]+)/(.+)")
local extension = ""
if path == nil then
path = ""
file, extension = string.match(v, "([^.]+)\.(.+)")
else
file, extension = string.match(file, "([^.]+)\.(.+)")
end
if path == "" then
sources[i] = {path = srcdir .. path, file = file, extension = extension}
else
sources[i] = {path = srcdir .. "/" .. path, file = file, extension = extension}
end
end
-- CLEAN
if mode == "clean" then
print("========== CLEANING EVERYTHING ==========")
-- remove object files
os.execute("rm " .. objdir.debug .. "/*.o")
os.execute("rm " .. objdir.release .. "/*.o")
-- remove binaries
os.execute("rm " .. bin.debug .. " " .. bin.release)
-- remove any generated makefiles
os.execute("rm Makefile.debug Makefile.release")
print("========== CLEAN COMPLETE ==========")
else
if mode == "debug" then
print("========== DEBUG BUILD STARTING ==========")
-- build command line options for nvcc
includes.gen = genopts(includes, includes.debug, " -I", "Include paths")
defines.gen = genopts(defines, defines.debug, " -D", "Preprocessor defines")
cflags.gen = " -c" .. genopts(cflags, cflags.debug, " -", "Compiler flags")
libdirs.gen = genopts(libdirs, libdirs.debug, " -L", "Library paths")
libs.gen = genopts(libs, libs.debug, " -l", "Static libraries")
lflags.gen = " -link" .. genopts(lflags, lflags.debug, " -", "Linker flags")
else
print("========== RELEASE BUILD STARTING ==========")
-- build command line options for nvcc
includes.gen = genopts(includes, includes.release, " -I", "Include paths")
defines.gen = genopts(defines, defines.release, " -D", "Preprocessor defines")
cflags.gen = " -c" .. genopts(cflags, cflags.release, " -", "Compiler flags")
libdirs.gen = genopts(libdirs, libdirs.release, " -L", "Library paths")
libs.gen = genopts(libs, libs.release, " -l", "Static libraries")
lflags.gen = " -link" .. genopts(lflags, lflags.release, " -", "Linker flags")
end
-- generate dependencies for each source file using nvcc
print("Generating dependencies for:")
for i, v in ipairs(sources) do
local filename = table.concat({v.path, "/", v.file, ".", v.extension})
io.write("\t" .. filename .. "... ")
io.flush()
local cmd = table.concat({"nvcc", includes.gen, " -M ", filename})
local f = io.popen(cmd)
sources[i].deps = f:read("*a")
print("done.")
end
-- write out makefile
io.write("Writing Makefile.... ")
io.flush()
-- open output file
local makefilename = "Makefile." .. mode
local f = io.open(makefilename, "w")
-- build object files list
local objpath = ""
if mode == "debug" then
objpath = objdir.debug
else
objpath = objdir.release
end
local objs = ""
for i, v in ipairs(sources) do
local filename = table.concat({objpath, "/", v.file, ".o"})
objs = table.concat({objs, " ", filename})
end
-- write out "all" rule
local binary = ""
if mode == "debug" then
binary = bin.debug
else
binary = bin.release
end
f:write(table.concat({"\nall:", objs, "\n"}))
f:write(table.concat({"\tnvcc",
lflags.gen,
libdirs.gen,
libs.gen,
objs,
" -o ",
binary,
"\n\n"}))
-- write out individual file rules
for i, v in ipairs(sources) do
local filename = table.concat({v.path, "/", v.file, ".", v.extension})
f:write(table.concat({objpath, "/", v.deps}))
f:write(table.concat({"\tnvcc",
cflags.gen,
includes.gen,
defines.gen,
" -o $@ ",
filename,
"\n\n"}))
end
-- done
f:close()
print("done.")
-- run makefile
print("Running make...")
local gofast = ""
--[[if mode == "release" then
gofast = " -j 8"
end]]--
os.execute("make -f " .. makefilename .. gofast)
if mode == "debug" then
print("========== DEBUG BUILD COMPLETE ==========")
else
print("========== RELEASE BUILD COMPLETE ==========")
end
end