-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.lua
More file actions
executable file
·514 lines (425 loc) · 12.8 KB
/
install.lua
File metadata and controls
executable file
·514 lines (425 loc) · 12.8 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
package.path = package.path .. ";?.lua"
-- This table stores colors used by luandarray installer
local colors = {
red = "\27[31m",
red_bold = "\27[31;1m",
green = "\27[32m",
green_bold = "\27[32;1m",
yellow = "\27[33m",
yellow_bold = "\27[33;1m",
blue = "\27[34m",
blue_bold = "\27[34;1m",
pink = "\27[35m",
pink_bold = "\27[35;1m",
bold = "\27[;1m",
none = "\27[0m"
}
-- LuaNdarray's write function
local function lnwrite(txt)
txt = tostring(txt)
local i = 0
while i < #txt do
i=i + 1
if txt:sub(i,i+1) == "\\<" then
io.write("<")
i=i + 1
elseif txt:sub(i,i) == "<" then
local color = txt:sub(i, -1):match("[<]([%w_]+)[>]")
io.write(colors[color] or error("color '" .. color .."' does not exist"))
i=i + #color + 1
else
io.write(txt:sub(i,i))
end
end
end
function print(...)
local args = {...}
for i, a in ipairs(args) do
lnwrite(a)
if i < #args then
io.write("\t")
end
end
io.write("\n")
end
function NOTICE(msg)
print("<yellow>[NOTICE]<none> " .. msg)
return 0
end
function SUCCESS(msg)
print("<green>[SUCCESS]<none> " .. msg)
end
function ERROR(msg)
print("<red_bold>[ERROR]<none> " .. msg)
return 1
end
function WARNING(msg)
print("<red>[WARNING]<none> " .. msg)
end
local os = require "ospp"
local lfs_support, lfs = pcall(require, "lfs")
if not lfs_support then
WARNING("LuaFileSystem not detected, backing to os++")
end
-- Some useful classes
local CCmeta = {
__tostring = function (self)
return string.format('CC("%s")', self.name)
end
}
CCmeta.__index = CCmeta
local function newCC(name)
local self = setmetatable({}, CCmeta)
self.name = name
return self
end
function CCmeta:generateObj(source, dst, flags)
if dst == nil then
dst = source:sub(1, -3) .. ".o"
end
if flags == nil then
flags = ""
end
os.execute(self.name .. " " .. flags .. " -c " .. source .. " -o " .. dst)
return os.exists(dst), dst -- Compiled successfully?
end
function CCmeta:link(flags, outname, o_files)
os.execute(self.name .. " " .. flags .. " -o " .. outname .. " " .. table.concat(o_files, " "))
return os.exists(outname)
end
-- Returns true if the compiler is supported
function CCmeta:supported()
return os.execute(self.name .. " -v > " .. os.devnull .. " 2>&1") ~= nil
end
-- for detect ospp.lua
package.path = package.path .. ";?.lua"
local function adjust_path(path)
if package.config:sub(1,1) == "/" then
return path:gsub("/", "\\"):gsub("\\+", "\\")
end
return path:gsub("\\", "/"):gsub("/+", "/")
end
local function SUCCESSFULLY()
SUCCESS("luandarray installed successfully")
os.exit(0)
end
local function INSTALL_ERROR()
SUCCESS("luandarray installed successfully")
os.exit(0)
end
local function compiler_exists(cc)
return os.execute(cc.." -v > " .. os.devnull .. " 2>&1") ~= nil
end
local function cacheExists()
return io.open(".cache", "r") ~= nil
end
local function createCache(cc, simd)
local f = io.open(".cache", "w")
if f then
f:write("CC:"..cc .. "\nsimd:"..simd)
f:close()
end
end
local function getCacheInfos()
local cache_file = io.lines(".cache")
local infos = {}
for line in cache_file do
local conf_name, conf_value = line:match("(%w+)%:(.+)")
if not conf_name then break end
infos[conf_name] = conf_value
end
return infos
end
local function detect_cc()
for i, cc in ipairs({"gcc", "mingw64-gcc", "mingw32-gcc", "clang"}) do
if newCC(cc):supported() then
return cc
end
end
-- No compiler detected
return nil
end
local function get_OS()
if jit then
return jit.os
end
local success, name = pcall(io.popen, "uname -s")
if success and name then return name:read() end
local nt = os.getenv("Windows_NT")
if nt then
return nt:lower():match("windows") ~= nil and "windows" or "unknows"
end
return "unknown"
end
-- detects simd architecture
local function detect_simd(cc)
os.execute(cc .. " detect_simd.c -o detect_simd")
local simd = io.popen((os.get() == "unix" and "./" or "") .. "detect_simd" .. (os.get() == "win" and ".exe" or ""), "r"):read("*a")
return simd
end
-- Initialize LuaNdarray's folder
local function init()
if lfs_support then
if not lfs.attributes("luandarray") then
lfs.mkdir("luandarray")
end
if not lfs.attributes("luandarray/luajit") then
os.mkdir("luandarray/luajit")
end
if not lfs.attributes("luandarray/luajit/bin") then
lfs.mkdir("luandarray/luajit/bin")
end
return
end
if not os.exists("luandarray") then
os.mkdir("luandarray")
end
if not os.exists("luandarray/luajit") then
os.mkdir("luandarray/luajit")
end
if not os.exists("luandarray/luajit/bin") then
os.mkdir("luandarray/luajit/bin")
end
end
local generateObjFiles
if lfs_support then
function generateObjFiles(cc, flags, dir)
local obj_files = {} for file in lfs.dir(dir) do
if file:sub(-2, -1) == ".c" then
local success, out = cc:generateObj(dir .. "/" .. file, nil, flags)
if not success then
ERROR("error on generate object file <bold>" .. out .. "<none>")
os.exit(1)
end
NOTICE("compiling <bold>" .. dir .. "/" .. file.."<none>")
table.insert(obj_files, out)
end
end
return obj_files
end
else
function generateObjFiles(cc, flags, dir)
local files = os.listdir(dir)
local obj_files = {}
for i, file in ipairs(files) do
-- only .c files will be compiled
if file:sub(-2, -1) == ".c" then
local success, out = cc:generateObj(dir .. "/" .. file, nil, flags)
table.insert(obj_files, out)
if not success then
ERROR("error on generate object file <bold>" .. out .. "<none>")
os.exit(1)
end
NOTICE("compiling <bold>" .. dir .. "/" .. file.."<none>")
end
end
return obj_files
end
end
-- Returns the full path where Lua interpreter is.
local function getLuaInterpreterFolder()
local i_min = 0
while arg[ i_min] do
i_min = i_min - 1
end
local path = arg[i_min+1]
local sep = package.config:sub(1,1)
for i=#path, 1, -1 do
if path:sub(i,i) == sep then
path = path:sub(1, i-1)
break
end
end
return path
end
local function luaLibPathExistsIn(dir, version)
version = version or _VERSION:match("[%d%.]+")
local version_num = version:gsub("%.", "")
local ext = os.get() == "win" and "dll" or "so"
local finder
if jit and os.get() == "unix" then
function finder(name)
return name:match("libluajit%-" .. version:gsub("%.", "%.") .. ".so%.%d+")
end
else
if os.get() == "win" then
function finder(name)
return name == "lua" .. version .. ".dll" or
name == "lua" .. version_num .. ".dll"
end
else
function finder(name)
return name:match("liblua" .. version:gsub("%.", "%%.") .. "%.so%.%d+") or
name:match("liblua" .. version_num .. "%.so%.%d+") or
name == "liblua.so"
end
end
end
if lfs_support then
for file in lfs.dir(dir) do
if finder(file) then
return file
end
end
else
for _, file in ipairs(os.listdir(dir)) do
if finder(file) then
return file
end
end
end
return nil -- not found
end
local lualib_search_in = os.get() == "win" and {getLuaInterpreterFolder(), "C:\\lua"} or {
"/usr/lib/x86_64-linux-gnu", "/usr/local/lib", "/usr/lib32", "/usr/lib64","/usr/share",
"/lib/x86_64-linux-gnu"
}
local function getLuaLibPath()
local sep = package.config:sub(1,1)
for i, folder in ipairs(lualib_search_in) do
if lfs_support and lfs.attributes(folder) or os.exists(folder) then
local name = luaLibPathExistsIn(folder)
if name then
return name, (folder .. sep .. name):gsub(sep.."+", sep)
end
end
end
return nil
end
local function removeObjFiles(dir)
local files = os.listdir(dir)
if not files then return nil end
for i, file in ipairs(files) do
if file:sub(-2, -1) == ".o" then
os.remove(dir.."/"..file)
end
end
return true
end
local function lnRemoveObjFiles()
removeObjFiles("src/core")
removeObjFiles("src/lua/src")
removeObjFiles("src/core/generic")
removeObjFiles("src/core/simd")
end
local function getFlags()
local flags = {}
for _, argv in ipairs(arg) do
local name, value = argv:match("(%w+)%=(%S+)")
if name and value then
flags[name] = value
end
end
return flags
end
-- Compiles LuaNdarray's source code
local function compile(cc, simd, lualib)
assert(lualib, "lualib is required")
lualib = lualib:gsub("%.so.+", ""):gsub("lib", ""):gsub("%.dll", "")
-- stores every file
local files = os.listdir("src/core")
if not files then
ERROR "error on compile LuaNdarray (core folder was not found)"
os.exit(1)
end
local fpic = (os.get() == "unix" and "-fPIC" or "") .. " -m" .. simd
local all_files = {}
for i, fs in ipairs({
generateObjFiles(cc, fpic, "src/core"),
generateObjFiles(cc, fpic .. " -Isrc/core -Isrc/lua/include", "src/lua/src"),
generateObjFiles(cc, fpic, "src/core/generic"),
generateObjFiles(cc, fpic, "src/core/simd")
}) do
for _, f in ipairs(fs) do
table.insert(all_files, f)
end
end
NOTICE("linking LuaNdarray...")
local flags = ("-fPIC -shared ")
for i, dir in ipairs(lualib_search_in) do
flags=flags .. "-L" .. dir .. " "
end
flags=flags .. "-l"..lualib..""
local dst = os.get() == "win" and "bin\\luandarray.dll" or "bin/luandarray.so"
if not cc:link(flags, dst, all_files) then
ERROR("Error on link LuaNdarray")
os.exit(1)
end
end
local function push_lua_files()
local files = os.listdir("src/luajit")
if not files then
ERROR("luajit files not found")
os.exit(1)
end
for i, file in ipairs(files) do
os.copy("src/luajit/"..file, "luandarray/luajit/"..file)
end
end
local function main(arg)
NOTICE("OS detected: <green_bold>" .. get_OS() .. "<none>")
NOTICE("Initalizing luandarray folder...")
init()
local flags = getFlags()
if not flags.lualib then
NOTICE("searching Lua library...")
flags.lualib = getLuaLibPath()
if flags.lualib then
NOTICE("Lua Library detected in <bold>" .. flags.lualib .."<none>")
else
ERROR("Lua library was not found. Please specify the Lua path by adding the flag: --lualib=\\<path>")
return 1
end
else
NOTICE("Lua library in use: " .. flags.lualib)
end
local CC, simd
if flags.cc then
if not compiler_exists(flags.cc) then
ERROR("C Compiler '" .. flags.cc.."' not found")
return 1
else
NOTICE("using C compiler <green_bold>'" .. flags.cc .. "'<none>")
CC = newCC(flags.cc)
end
end
if flags.simd then
NOTICE("using SIMD architecture <green_bold>'" .. flags.simd .. "'<none>")
simd = flags.simd
end
if cacheExists() then
local infos = getCacheInfos()
if not CC then
CC = newCC(infos.CC)
NOTICE("C compiler: <green_bold>" .. CC.name .."<none> (from cache)")
end
if not simd then
simd = infos.simd
NOTICE("simd: <green_bold>"..simd.."<none> (from cache)")
end
else
CC = detect_cc()
if not CC then
ERROR("No C Compiler detected")
return 1
end
NOTICE("C Compiler detected: <green_bold>" .. CC .."<none>")
simd = detect_simd(CC)
NOTICE("SIMD architecture detect: <green_bold>" .. simd .. "<none>")
createCache(CC, simd)
CC = newCC(CC)
end
NOTICE("compiling LuaNdarray...")
compile(CC, simd, flags.lualib)
NOTICE("copying lua files...")
push_lua_files()
NOTICE("removing object files...")
lnRemoveObjFiles()
return 0
end
local status = main(arg)
if status == 1 then
INSTALL_ERROR()
else
SUCCESSFULLY()
end