-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate.nim
120 lines (88 loc) · 2.81 KB
/
generate.nim
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
import osproc
import strutils
import sequtils
import os
import re
let base = "/usr/include/openssl"
var res: seq[string] = @[]
proc process(file: string, parent = "", depth = "") =
let file = file.extractFilename()
if not fileExists(base / file):
return
#echo depth & "Processing " & file
let data = readFile(base / file)
for inc in data.findAll(re"#[ ]*include[ ]?<(.*?)>"):
var inc = inc.replace(re"#[ ]*include[ ]?<", "")[0 .. ^2]
if "openssl" in inc:
inc = inc[8 .. ^1]
if inc == parent:
continue
if inc notin res:
process(inc, file, depth & " ")
if file notin res:
if parent.len == 0:
#echo depth & "Adding " & file
res.add file
else:
let idx = res.find(parent)
if idx != -1:
#echo depth & "Adding dep " & file
res.insert(file, idx)
else:
#echo depth & "Appending dep " & file
res.add file
var fp = commandLineParams()[2]
if "import os" notin readFile(fp):
var core = """
import os
import strutils
import nimterop/[build, cimport]
static:
cDebug()
cSkipSymbol @["filler"] # Skips
getHeader("openssl/""" & commandLineParams()[0] & """")
const basePath = """ & commandLineParams()[0].split(".")[0] & """Path.parentDir
cPlugin:
import strutils
proc onSymbol*(sym: var Symbol) {.exportc, dynlib.} =
sym.name = sym.name.strip(chars = {'_'}).replace("__", "_")
# Replacements here
type
tmp = object
# Objects here
# Starts
"""
for file in walkFiles(base / "*.h"):
process(file)
let tmp = ($readFile(commandLineParams()[1])).strip(chars={'\n'}).split("\n")
for i in res:
if i in tmp:
core.add("""
when fileExists(basePath/"$1"):
cImport(basePath/"$1", dynlib="$2LPath")
""" % @[i, commandLineParams()[0].split(".")[0]])
writeFile(fp, core)
while true:
var r = execProcess("nim c -d:sslStd -d:cryptoStd -r " & fp)
if "Error: undeclared identifier: " in r.split("\n")[^2]:
var text = readFile(fp)
var problem = r.split("\n")[^2].split("'")[^2]
echo problem
if " " & problem & " = object" notin text:
var tmp = @(text.split("\n"))
tmp.insert(@[" " & problem & " = object"], tmp.find(" # Objects here"))
writeFile(fp, tmp.join("\n"))
elif " is a stylistic duplicate of identifier " in r.split("\n")[^2]:
var t1 = r.split("\n")[^2].split("'")[1]
var t2 = r.split("\n")[^2].split("'")[3]
var text = readFile(fp)
var tmp = @(text.split("\n"))
tmp.insert(@[" if sym.name == \"" & t2 & "\":", " sym.name = \"C_" & t2 & "\""], tmp.find(" # Replacements here"))
writeFile(fp, tmp.join("\n"))
elif "redefinition of" in r.split("\n")[^2]:
var t1 = r.split("\n")[^2].split("'")[1]
var text = readFile(fp)
writeFile(fp, text.replace("] # Skips", ",\"" & t1 & "\"] # Skips"))
else:
echo r
break