-
Notifications
You must be signed in to change notification settings - Fork 3
/
make_all.lua
executable file
·143 lines (131 loc) · 3.75 KB
/
make_all.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
#!/usr/bin/env luajit
local ffi = require 'ffi'
local path = require 'ext.path'
local table = require 'ext.table'
local os = require 'ext.os'
-- this holds the stuff thats working already
-- but it's a separate file for the sake of generate.lua looking to see what to replace with require()'s
local includeList = require 'include-list'
-- remove all those that pertain to other os/arch
includeList = includeList:filter(function(inc)
if inc.os ~= nil and inc.os ~= ffi.os then return end
if inc.arch ~= nil and inc.arch ~= ffi.arch then return end
return true
end)
local luajit = 'luajit'
local req = table{...}
-- see if we got any options ...
for i=#req,1,-1 do
if req[i]:sub(1,7) == 'luajit=' then
luajit = req[i]:sub(8)
print('setting luajit to '..luajit)
req:remove(i)
end
end
req = req[1]
if not req then error("make_all.lua all for all, or make_all.lua <some filename>") end
if req ~= 'all' then
-- TODO seems using <> or "" now is essential for excluding recursive require's
if req:sub(1,1) ~= '<' and req:sub(1,1) ~= '"' then
error('must be system (<...>) or user ("...") include space')
end
print('searching for '..req)
includeList = table.filter(includeList, function(inc)
--return inc.inc:match(req)
return inc.inc == req
end):setmetatable(nil)
if #includeList == 0 then
error("couldn't find "..req)
end
end
local outdirbase = 'results' -- outdir without ffi/
local outdir = outdirbase..'/ffi'
for _,inc in ipairs(includeList) do
if not inc.dontGen then
local outpath = outdir..'/'..inc.out
local dir, outfn = path(outpath):getdir()
dir:mkdir(true)
if inc.forcecode then
-- just write it , proly a split between dif os versions
path(outpath):write(inc.forcecode)
else
path(outpath):write[=[
local ffi = require 'ffi'
ffi.cdef[[
]=]
local cmd = table{
luajit,
'generate.lua'
}
if inc.flags then
cmd:insert(inc.flags)
end
local function addincarg(f)
if f:sub(1,1) == '"' then
cmd:insert(('%q'):format(f))
elseif f:sub(1,1) == '<' then
cmd:insert('"'..f..'"')
else
error'inc /moreincs needs <> or "" wrapper'
end
end
addincarg(inc.inc)
if inc.moreincs then
for _,f in ipairs(inc.moreincs) do
addincarg(f)
end
end
for _,f in ipairs(inc.silentincs or {}) do
cmd:insert'-silent'
addincarg(f)
end
for _,f in ipairs(inc.skipincs or {}) do
cmd:insert'-skip'
addincarg(f)
end
for _,m in ipairs(inc.includedirs or {}) do
cmd:insert('-I'..m)
end
for _,m in ipairs(inc.macros or {}) do
cmd:insert('-D'..m)
end
if inc.enumGenUnderscoreMacros then
cmd:insert'-enumGenUnderscoreMacros'
end
cmd:append{
'>>',
'"'..outpath..'"',
}
cmd = cmd:concat' '
print(os.exec(cmd))
path(outpath):append[=[
]]
]=]
-- if there's a final-pass on the code then do it
if inc.final then
assert(path(outpath):write(
assert(inc.final(
assert(path(outpath):read())
), "expected final() to return a string")
))
end
end
if ffi.os == 'Windows' then
-- in windows, the linux code writes \n's, but the windows >> writes \r\n's,
-- so unify them all here, pick whichever format you want
path(outpath):write((
path(outpath):read()
:gsub('\r\n', '\n')
))
end
-- verify it works
-- can't use -lext because that will load ffi/c stuff which could cause clashes in cdefs
-- luajit has loadfile, nice.
--[=[ use loadfile ... and all the old/original ffi locations
print(os.exec([[luajit -e "assert(loadfile(']]..outpath..[['))()"]]))
--]=]
-- [=[ use require, and base it in the output folder
print(os.exec([[luajit -e "package.path=']]..outdirbase..[[/?.lua;'..package.path require 'ffi.req' ']]..assert((inc.out:match('(.*)%.lua'))):gsub('/', '.')..[['"]]))
--]=]
end
end