This repository was archived by the owner on Jul 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgenerator.lua
More file actions
152 lines (140 loc) · 4.91 KB
/
Copy pathgenerator.lua
File metadata and controls
152 lines (140 loc) · 4.91 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
local function strip(cad)
return cad:gsub("^%s*(.-)%s*$","%1") --remove initial and final spaces
end
local function save_data(filename,...)
local file = io.open(filename,"w")
for i=1, select('#', ...) do
local data = select(i, ...)
file:write(data)
end
file:close()
end
-- Set of known #defines that we don't need to include
INVALID_DEFINES = {GLAPI=1, APIENTRY=1, GLU_TESS_MAX_COORD=1, gluErrorStringWIN=1, WINGDIAPI=1, CALLBACK=1, GLAPIENTRY=1}
--iterates lines from a gcc/clang -E in a specific location
local function location(file,locpathT,defines)
local define_re = "^#define%s+([^%s]+)%s+([^%s]+)$"
local number_re = "^-?[0-9]+$"
local hex_re = "0x[0-9a-fA-F]+$"
local location_re
if COMPILER == "cl" then
location_re = '^#line (%d+) "([^"]*)"'
else --gcc, clang
location_re = '^# (%d+) "([^"]*)"'
end
local path_reT = {}
for i,locpath in ipairs(locpathT) do
table.insert(path_reT,'^(.*[\\/])('..locpath..')%.h$')
end
local in_location = false
local which_location = ""
local loc_num
local loc_num_incr
local lineold = ""
local which_locationold,loc_num_realold
local lastdumped = false
local function location_it()
repeat
local line = file:read"*l"
if not line then
if not lastdumped then
lastdumped = true
return lineold, which_locationold,loc_num_realold
else
return nil
end
end
if #line==0 then --nothing on emptyline
elseif not line:match("%S") then --nothing if only spaces
elseif line:sub(1,1) == "#" then
-- Is this a location pragma?
local loc_num_t,location_match = line:match(location_re)
if location_match then
in_location = false
for i,path_re in ipairs(path_reT) do
if location_match:match(path_re) then
in_location = true;
loc_num = loc_num_t
loc_num_incr = 0
which_location = locpathT[i]
break
end
end
elseif in_location then
local name,val = line:match(define_re)
if name then
while defines[val] do val = defines[val] end
if val:match(number_re) or val:match(hex_re) then
defines[name] = val
end
end
end
elseif in_location then
local loc_num_real = loc_num + loc_num_incr
loc_num_incr = loc_num_incr + 1
if loc_num_realold and loc_num_realold < loc_num_real then
--old line complete
local lineR,which_locationR,loc_num_realR = lineold, which_locationold,loc_num_realold
lineold, which_locationold,loc_num_realold = line,which_location,loc_num_real
return lineR,which_locationR,loc_num_realR
else
lineold=lineold..line
which_locationold,loc_num_realold = which_location,loc_num_real
--return line,loc_num_real, which_location
end
end
until false --forever
end
return location_it
end
local function define_str(name,defines)
local tab = {"local "..name.."= {"}
local defines_keys = {}
for k,v in pairs(defines) do
table.insert(defines_keys,k)
end
table.sort(defines_keys)
for i,k in ipairs(defines_keys) do
--print("\t%s = %s," % ("['"..k.."']", defines[k]))
table.insert(tab,string.format("\t%s = %s,","['"..k.."']", defines[k]))
end
table.insert(tab,"}")
return table.concat(tab,"\n")
end
local function copyfile(src,dst,blocksize)
blocksize = blocksize or 1024*4
print( "copyfile", src, dst)
local srcf, err = io.open(src,"rb")
if not srcf then error(err) end
local dstf, err = io.open(dst,"wb")
if not dstf then error(err) end
while true do
local data = srcf:read(blocksize)
if not data then break end
dstf:write(data)
end
srcf:close()
dstf:close()
end
------------------------------------------------------
local cdefs = {}
save_data("./outheader.h",[[#include <GLFW/glfw3.h>]])
local pipe,err = io.popen([[gcc -E -dD -I ../GLFW/include/ ./outheader.h]],"r")
if not pipe then
error("could not execute gcc "..err)
end
local defines = {}
for line in location(pipe,{"glfw.-"},defines) do
local line = strip(line)
table.insert(cdefs,line)
end
pipe:close()
os.remove"./outheader.h"
--save
local glfwstr = "local cdecl=[[\n"..table.concat(cdefs,"\n").."]]\n"
glfwstr = glfwstr..define_str("glfwc",defines)
local hfile = io.open("./glfw_base.lua","r")
local hstrfile = hfile:read"*a"
hfile:close()
save_data("./glfw.lua",glfwstr,hstrfile)
copyfile("./glfw.lua","../glfw.lua")