-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathffi_def_create_headers.lua
298 lines (263 loc) · 8.16 KB
/
ffi_def_create_headers.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
-- ffi_def_create_headers.lua
print()
print(" -- ffi_def_create_headers.lua start -- ")
if jit then
print(jit.version)
else
print(_VERSION)
end
print()
local arg = {...}
local noParse = false
if arg[1] then
noParse = arg[1] == "n"
end
local util = require "lib_util"
local osname
if true then -- keep ffi valid only inside if to make ZeroBrane debugger work
local ffi = require("ffi")
osname = string.lower(ffi.os)
end
osname = "windows" -- for running in osx
local timeUsed = util.seconds()
-- http://gcc.gnu.org/onlinedocs/gfortran/Preprocessing-Options.html
local addToWindowsInclude = '#define _WIN32_WINNT 0x0602 // inserted header for Lua, Windows 8 == 0x0602\n#define WINVER _WIN32_WINNT\n'
addToWindowsInclude = addToWindowsInclude..'#define WIN32_LEAN_AND_MEAN\n#pragma comment (lib, "Ws2_32.lib")\n'
-- use ONLY '\n', not '\r' in addToWindowsInclude
local target_path = "c_include/"..osname.."/"
local useCccIncludeDir = true -- comment sourcepaths to match this
local sourcepaths = {
--["windows"] = "C:/mingw64/mingw/include/",
["windows"] = "C:/Program Files (x86)/Microsoft SDKs/Windows/v7.0A/Include/",
["windows2"] = "C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/include/",
["osx"] = "/usr/include/",
--["osx2"] = "",
["linux"] = "/usr/include/",
["linux2"] = "/usr/include/i386-linux-gnu/"
}
sourcefiles = {
["windows"] = [[
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
]],
["osx"] = [[
#include <sys/types.h>
]],
["linux"] = [[
#include <sys/types.h>
]],
}
local prfFileName = "ffi_def_create_headers_prf.lua"
if util.file_exists(prfFileName) then
dofile(prfFileName)
end
local sourcepath = sourcepaths[osname]
local sourcepath2 = sourcepaths[osname.."2"]
local sourcefile = sourcefiles[osname]
sourcefile = sourcefile:gsub("#include <", "")
sourcefile = sourcefile:gsub(">", "")
local copy_commad = "cp "
local preprocessor_commad = "gcc -E -P -D".." " -- -dD -C = leave comments
if util.isWin then
copy_commad = "copy "
--preprocessor_commad = "CL /EP" -- /showIncludes /FI /D_WIN32_WINNT=0x0601
preprocessor_commad = "gcc.exe -E -dD -C"
if useCccIncludeDir then
preprocessor_commad = preprocessor_commad.." -I "..'"'..sourcepath..'"'
if sourcepath2 then
preprocessor_commad = preprocessor_commad.." -I "..'"'..sourcepath2..'"'
end
end
preprocessor_commad = preprocessor_commad.." "
-- -dD -dN -dI -D _WIN32_WINNT=0x0602
end
local define_length = #("#define ")
local define_pos = define_length + 1
local function defineLine(line)
local comment
local s,e = line:find("%/%/")
if not s then
s,e = line:find("%/%*")
end
if s then
comment = line:sub(s)
comment = " "..comment:match("^%s*(.-)%s*$") -- remove whitespaces
line = line:sub(1, s - 1)
end
local value = line:sub(define_pos)
local s,e = value:find(" ")
if not e then
value = ""
else
value = value:sub(e + 1)
end
if value == "" then
line = ""
else
local name = line:sub(define_pos, define_pos + s - 2)
name = name:match("^%s*(.-)%s*$") -- remove leading and trailing whitespace
value = value:match("^%s*(.-)%s*$") -- remove leading and trailing whitespace
if value:match('".*%"$') then
line = "static const char "..name.." = "..value..";"
elseif value:match(".*%dL$") then
line = "static const long "..name.." = "..value..";"
elseif value:match(".*%dLL$") then
line = "static const long long "..name.." = "..value..";"
elseif value:match(".*%dF$") then
line = "static const double "..name.." = "..value..";" -- float
elseif value:match(".*%dDF$") then
line = "static const double "..name.." = "..value..";"
elseif value:match(".*%dDD$") then
line = "static const double "..name.." = "..value..";"
elseif value:match(".*%dDL$") then
line = "static const long double "..name.." = "..value..";"
elseif value:match("%d%.%d") then
line = "static const double "..name.." = "..value..";"
else
line = "static const int "..name.." = "..value..";"
end
if comment then
line = line..comment
end
end
return line
end
local filecount = 0
local parsecount = 0
local define_found = false
for file in sourcefile:gmatch("[^\r\n]+") do
if file ~= "" and not util.string_starts(file, "//") then -- not empty and not commented lines
local destfile = file:gsub("/", "_")
if util.isWin then
file= file:gsub("/", "\\")
sourcepath = sourcepath:gsub("/", "\\")
if sourcepath2 then
sourcepath2 = sourcepath2:gsub("/", "\\")
end
end
local copypath = target_path.."original/"..destfile
if not util.file_exists(copypath) then
local cmd
if util.string_starts(file, "/") then
cmd = copy_commad..'"'..file..'" "'..util.currentPath()..copypath..'"'
else
cmd = copy_commad..'"'..sourcepath..file..'" "'..copypath..'"'
if not util.file_exists(sourcepath..file) then
if sourcepath2 then
cmd = copy_commad..'"'..sourcepath2..file..'" "'..copypath..'"'
end
end
end
if util.isWin then
cmd = cmd:gsub("/", "\\")
end
print(cmd)
os.execute(cmd)
if addToWindowsInclude then
print(" ... add prefix to file: "..copypath)
local code = util.readFile(copypath)
code = addToWindowsInclude..code
util.writeFile(copypath, code)
end
end
local destpath = target_path..destfile
if not util.file_exists(destpath) then
local cmd = preprocessor_commad..copypath.." > "..destpath
print(cmd)
os.execute(cmd)
filecount = filecount + 1
end
if not util.file_exists(destpath) then
print("*** file creation failed: "..destpath)
elseif not noParse then
parsecount = parsecount + 1
local filecomment = "\n\n// *** "..parsecount..". "..file.." ***"
local code = util.readFile(destpath)
print(destpath)
local crlfLen = #code
code = code:gsub("\r\n", "\n")
code = code:gsub("\n\n", "\n")
local _,linecount = code:gsub("\n", ".")
print(" ... empty lines removed: "..util.format_num(crlfLen - #code, 0)..", size: "..util.fileSize(#code, 2))
_ = nil -- release memory
collectgarbage()
print(" ... linecount: "..util.format_num(linecount, 0))
local codeout = {}
local i = 0
for line in code:gmatch("[^\r\n]+") do
i = i + 1
if i%5000 == 0 then
print(" ... line: "..util.format_num(i, 0).." / "..util.format_num(linecount, 0))
end
local is_define = false
--line:sub(1, define_length)
if line:find("^#define ") then
is_define = true
define_found = true
line = defineLine(line)
end
if line ~= "" and not line:find("^%s+$") and line ~= "\n" and line:find("#undef") ~= 1 then
codeout[#codeout+1] = line.."\n"
end
end
local definecount = 0
if not define_found then
-- read #define's from original header
print(" ... creating defines from original header...")
local definecode = util.readFile(destpath)
if not definecode:find(" --- defines\n\n") then
definecode = util.readFile(copypath)
local defineout = {}
local i = 0
for line in definecode:gmatch("[^\r\n]+") do
i = i + 1
if i%5000 == 0 then
print(" ... line: "..util.format_num(i, 0).." / "..util.format_num(linecount, 0))
end
if line:find("^#define ") then
line = defineLine(line)
if line ~= "" then
definecount = definecount +1
defineout[#defineout+1] = line.."\n"
end
end
end
codeout[#codeout+1] = filecomment.." --- defines\n\n"..table.concat(defineout)
end
end
define_found = false
codeout = table.concat(codeout)
util.appendFile(target_path.."ffi_types.h", filecomment.."\n"..codeout)
print(" ... final size: "..util.fileSize(#codeout, 2)..", defines: "..definecount)
print()
util.writeFile(destpath, codeout)
end
end
end
if codeout ~= "" then
-- add "ffi_types.h" to written filecount
filecount = filecount + 1
end
timeUsed = util.seconds(timeUsed)
print()
print("created: "..(filecount).." files in "..util.format_num(timeUsed, 3).." seconds")
print()
if jit then
print(jit.version)
else
print(_VERSION)
end
print(" -- ffi_def_create_headers.lua end -- ")
print()
--[[
32-bit: Single (binary32), decimal32
64-bit: Double (binary64), decimal64
128-bit: Quadruple (binary128), decimal128
_Decimal32 DF
_Decimal64 DD
_Decimal128 DL
long L
long long LL
char "
]]