aboutsummaryrefslogtreecommitdiff
path: root/nimgen.nim
blob: 9771683cb1ca2ff8154d0d3e347805efbb63a42a (plain)
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
import nre
import os
import ospaths
import osproc
import parsecfg
import pegs
import ropes
import sequtils
import streams
import strutils
import tables

var DONE: seq[string] = @[]
var DONE_INLINE: seq[string] = @[]

var CONFIG: Config
var FILTER = ""
var QUOTES = true
var OUTPUT = ""
var INCLUDES: seq[string] = @[]
var EXCLUDES: seq[string] = @[]
var RENAMES = initTable[string, string]()
var WILDCARDS = newConfig()

const DOC = """
Nimgen is a helper for c2nim to simpilfy and automate the wrapping of C libraries

Usage:
  nimgen [options] <file.cfg>...

Options:
  -f    delete all artifacts and regenerate
"""

# ###
# Helpers

proc execProc(cmd: string): string =
    var p = startProcess(cmd, options = {poStdErrToStdOut, poUsePath, poEvalCommand})

    result = ""
    var outp = outputStream(p)
    var line = newStringOfCap(120).TaintedString
    while true:
        if outp.readLine(line):
            result.add(line)
            result.add("\n")
        elif not running(p): break

    var x = p.peekExitCode()
    if x != 0:
        echo "Command failed: " & $x
        echo cmd
        echo result
        quit(1)

proc extractZip(zipfile: string) =
    var cmd = "unzip -o $#"
    if defined(Windows):
        cmd = "powershell -nologo -noprofile -command \"& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('$#', '.'); }\""

    setCurrentDir(OUTPUT)
    defer: setCurrentDir("..")

    echo "Extracting " & zipfile
    discard execProc(cmd % zipfile)

proc downloadUrl(url: string) =
    let file = url.extractFilename()
    let ext = file.splitFile().ext.toLowerAscii()

    var cmd = "curl $# -o $#"
    if defined(Windows):
        cmd = "powershell wget $# -OutFile $#"

    if not (ext == ".zip" and fileExists(OUTPUT/file)):
        echo "Downloading " & file
        discard execProc(cmd % [url, OUTPUT/file])

    if ext == ".zip":
        extractZip(file)

proc gitReset() =
    echo "Resetting Git repo"

    setCurrentDir(OUTPUT)
    defer: setCurrentDir("..")

    discard execProc("git reset --hard HEAD")

proc gitRemotePull(url: string, pull=true) =
    if dirExists(OUTPUT/".git"):
        if pull:
            gitReset()
        return

    setCurrentDir(OUTPUT)
    defer: setCurrentDir("..")

    echo "Setting up Git repo"
    discard execProc("git init .")
    discard execProc("git remote add origin " & url)

    if pull:
        echo "Checking out artifacts"
        discard execProc("git pull --depth=1 origin master")

proc gitSparseCheckout(plist: string) =
    let sparsefile = ".git/info/sparse-checkout"
    if fileExists(OUTPUT/sparsefile):
        gitReset()
        return

    setCurrentDir(OUTPUT)
    defer: setCurrentDir("..")

    discard execProc("git config core.sparsecheckout true")
    writeFile(sparsefile, plist)

    echo "Checking out artifacts"
    discard execProc("git pull --depth=1 origin master")

proc getKey(ukey: string): tuple[key: string, val: bool] =
    var kv = ukey.replace(re"\..*", "").split("-", 1)
    if kv.len() == 1:
        kv.add("")

    if (kv[1] == "") or
        (kv[1] == "win" and defined(Windows)) or
        (kv[1] == "lin" and defined(Linux)) or
        (kv[1] == "osx" and defined(MacOSX)):
        return (kv[0], true)

    return (kv[0], false)

# ###
# File loction

proc getNimout(file: string, rename=true): string =
    result = file.splitFile().name.replace(re"[\-\.]", "_") & ".nim"
    if OUTPUT != "":
        result = OUTPUT/result

    if not rename:
        return

    if RENAMES.hasKey(file):
        result = RENAMES[file]

    if not dirExists(parentDir(result)):
        createDir(parentDir(result))

proc exclude(file: string): bool =
    for excl in EXCLUDES:
        if excl in file:
            return true
    return false

proc search(file: string): string =
    if exclude(file):
        return ""

    result = file
    if file.splitFile().ext == ".nim":
        result = getNimout(file)
    elif not fileExists(result) and not dirExists(result):
        var found = false
        for inc in INCLUDES:
            result = inc/file
            if fileExists(result) or dirExists(result):
                found = true
                break
        if not found:
            echo "File doesn't exist: " & file
            quit(1)

    return result.replace(re"[\\/]", $DirSep)

# ###
# Loading / unloading

template withFile(file: string, body: untyped): untyped =
    if fileExists(file):
        var f: File
        while true:
            try:
                f = open(file)
                break
            except:
                sleep(100)

        var contentOrig = f.readAll()
        f.close()
        var content {.inject.} = contentOrig

        body

        if content != contentOrig:
            var f = open(file, fmWrite)
            write(f, content)
            f.close()
    else:
        echo "Missing file " & file

# ###
# Manipulating content

proc prepend(file: string, data: string, search="") =
    withFile(file):
        if search == "":
            content = data & content
        else:
            let idx = content.find(search)
            if idx != -1:
                content = content[0..<idx] & data & content[idx..<content.len()]

proc append(file: string, data: string, search="") =
    withFile(file):
        if search == "":
            content &= data
        else:
            let idx = content.find(search)
            let idy = idx + search.len()
            if idx != -1:
                content = content[0..<idy] & data & content[idy..<content.len()]

proc freplace(file: string, pattern: string, repl="") =
    withFile(file):
        if pattern in content:
            content = content.replace(pattern, repl)

proc freplace(file: string, pattern: Regex, repl="") =
    withFile(file):
        if content.find(pattern).isSome():
            if "$#" in repl:
                for m in content.findIter(pattern):
                    content = content.replace(m.match, repl % m.captures[0])
            else:
                content = content.replace(pattern, repl)

proc comment(file: string, pattern: string, numlines: string) =
    let ext = file.splitFile().ext.toLowerAscii()
    let cmtchar = if ext == ".nim": "#" else: "//"
    withFile(file):
        var idx = content.find(pattern)
        var num = 0
        try:
            num = numlines.parseInt()
        except ValueError:
            echo "Bad comment value, should be integer: " & numlines
        if idx != -1:
            for i in 0 .. num-1:
                if idx >= content.len():
                    break
                content = content[0..<idx] & cmtchar & content[idx..<content.len()]
                while idx < content.len():
                    idx += 1
                    if content[idx] == '\L':
                        idx += 1
                        break

proc rename(file: string, renfile: string) =
    if file.splitFile().ext == ".nim":
        return

    var
        nimout = getNimout(file, false)
        newname = renfile.replace("$nimout", extractFilename(nimout))

    #if newname =~ peg"(!\$.)*{'$replace'\s*'('\s*{(!\,\S)+}\s*','\s*{(!\)\S)+}\s*')'}":
    if newname =~ peg"(!\$.)*{'$replace'\s*'('\s*{(!\)\S)+}')'}":
        var final = nimout.extractFilename()
        for entry in matches[1].split(","):
            let spl = entry.split("=")
            if spl.len() != 2:
                echo "Bad replace syntax: " & renfile
                quit(1)

            var
                srch = spl[0].strip()
                repl = spl[1].strip()

            final = final.replace(srch, repl)
        newname = newname.replace(matches[0], final)

    RENAMES[file] = OUTPUT/newname

proc compile(dir="", file=""): string =
    proc fcompile(file: string): string =
        return "{.compile: \"$#\".}" % file.replace("\\", "/")

    var data = ""
    if dir != "" and dirExists(dir):
        for f in walkFiles(dir / "*.c"):
            data &= fcompile(f) & "\n"

    if file != "" and fileExists(file):
        data &= fcompile(file) & "\n"

    return data

proc fixFuncProtos(file: string) =
    withFile(file):
        for fp in content.findIter(re"(?m)(^.*?)[ ]*\(\*(.*?)\((.*?)\)\)[ \r\n]*\((.*?[\r\n]*.*?)\);"):
            var tdout = "typedef $# (*type_$#)($#);\n" % [fp.captures[0], fp.captures[1], fp.captures[3]] &
                "type_$# $#($#);" % [fp.captures[1], fp.captures[1], fp.captures[2]]
            content = content.replace(fp.match, tdout)

# ###
# Convert to Nim

proc getIncls(file: string, inline=false): seq[string] =
    result = @[]
    if inline and file in DONE_INLINE:
        return

    withFile(file):
        for f in content.findIter(re"(?m)^\s*#\s*include\s+(.*?)$"):
            var inc = f.captures[0].strip()
            if ((QUOTES and inc.contains("\"")) or (FILTER != "" and FILTER in inc)) and (not exclude(inc)):
                result.add(
                    inc.replace(re"""[<>"]""", "").replace(re"\/[\*\/].*$", "").strip())

        result = result.deduplicate()

    DONE_INLINE.add(file)

    if inline:
        var sres = newSeq[string]()
        for incl in result:
            let sincl = search(incl)
            if sincl == "":
                continue

            sres.add(getIncls(sincl, inline))
        result.add(sres)

    result = result.deduplicate()

proc getDefines(file: string, inline=false): string =
    result = ""
    if inline:
        var incls = getIncls(file, inline)
        for incl in incls:
            let sincl = search(incl)
            if sincl != "":
                echo "Inlining " & sincl
                result &= getDefines(sincl)
    withFile(file):
        for def in content.findIter(re"(?m)^(\s*#\s*define\s+[\w\d_]+\s+[\d\-.xf]+)(?:\r|//|/*).*?$"):
            result &= def.captures[0] & "\n"

proc runPreprocess(file, ppflags, flags: string, inline: bool): string =
    var pproc = "gcc"
    if flags.contains("cpp"):
        pproc = "g++"
    var cmd = "$# -E $# $#" % [pproc, ppflags, file]
    for inc in INCLUDES:
        cmd &= " -I " & inc

    # Run preprocessor
    var data = execProc(cmd)

    # Include content only from file
    var rdata: Rope
    var start = false
    var sfile = file.replace("\\", "/")
    if inline:
        sfile = sfile.parentDir()
    for line in data.splitLines():
        if line.strip() != "":
            if line[0] == '#' and not line.contains("#pragma"):
                start = false
                if sfile in line.replace("\\", "/").replace("//", "/"):
                    start = true
                if not ("\\" in line) and not ("/" in line) and extractFilename(sfile) in line:
                    start = true
            else:
                if start:
                    rdata.add(
                        line.replace("_Noreturn", "")
                            .replace("(())", "")
                            .replace("WINAPI", "")
                            .replace("__attribute__", "")
                            .replace("extern \"C\"", "")
                            .replace(re"\(\([_a-z]+?\)\)", "")
                            .replace(re"\(\(__format__[\s]*\(__[gnu_]*printf__, [\d]+, [\d]+\)\)\);", ";") & "\n"
                    )
    return $rdata

proc runCtags(file: string): string =
    var cmd = "ctags -o - --fields=+S+K --c-kinds=p --file-scope=no " & file
    var fps = execProc(cmd)

    var fdata = ""
    for line in fps.splitLines():
        var spl = line.split(re"\t")
        if spl.len() > 4:
            if spl[0] != "main":
                var fn = ""
                var match = spl[2].find(re"/\^(.*?)\(")
                if match.isSome():
                    fn = match.get().captures[0]
                    fn &= spl[4].replace("signature:", "") & ";"
                    fdata &= fn & "\n"

    return fdata

proc runFile(file: string, cfgin: OrderedTableRef)

proc c2nim(fl, outfile, flags, ppflags: string, recurse, inline, preprocess, ctags, defines: bool, dynlib, compile, pragma: seq[string] = @[]) =
    var file = search(fl)
    if file == "":
        return

    if file in DONE:
        return

    echo "Processing $# => $#" % [file, outfile]
    DONE.add(file)

    fixFuncProtos(file)

    var incout = ""
    if recurse:
        var incls = getIncls(file)
        for inc in incls:
            var cfg = newOrderedTable[string, string]()
            if flags != "": cfg["flags"] = flags
            if ppflags != "": cfg["ppflags"] = ppflags
            if recurse: cfg["recurse"] = $recurse
            if preprocess: cfg["preprocess"] = $preprocess
            if ctags: cfg["ctags"] = $ctags
            if defines: cfg["defines"] = $defines
            for i in dynlib:
                cfg["dynlib." & i] = i
            runFile(inc, cfg)

            incout &= "import $#\n" % inc.search().getNimout()[0 .. ^5] #inc.splitFile().name.replace(re"[\-\.]", "_") & "\n"

    var cfile = file
    if preprocess:
        cfile = "temp-$#.c" % [outfile.extractFilename()]
        writeFile(cfile, runPreprocess(file, ppflags, flags, inline))
    elif ctags:
        cfile = "temp-$#.c" % [outfile.extractFilename()]
        writeFile(cfile, runCtags(file))

    if defines and (preprocess or ctags):
        prepend(cfile, getDefines(file, inline))

    var extflags = ""
    var passC = ""
    var outlib = ""
    var outpragma = ""

    passC = "import strutils\n"
    for inc in INCLUDES:
        passC &= ("""{.passC: "-I\"" & gorge("nimble path $#").strip() & "/$#\"".}""" % [OUTPUT, inc]) & "\n"

    for prag in pragma:
        outpragma &= "{." & prag & ".}\n"

    let fname = file.splitFile().name.replace(re"[\.\-]", "_")
    if dynlib.len() != 0:
        let win = "when defined(Windows):\n"
        let lin = "when defined(Linux):\n"
        let osx = "when defined(MacOSX):\n"
        var winlib, linlib, osxlib: string = ""
        for dl in dynlib:
            let lib = "  const dynlib$# = \"$#\"\n" % [fname, dl]
            let ext = dl.splitFile().ext
            if ext == ".dll":
                winlib &= lib
            elif ext == ".so":
                linlib &= lib
            elif ext == ".dylib":
                osxlib &= lib

        if winlib != "":
            outlib &= win & winlib & "\n"
        if linlib != "":
            outlib &= lin & linlib & "\n"
        if osxlib != "":
            outlib &= osx & osxlib & "\n"

        if outlib != "":
            extflags &= " --dynlib:dynlib$#" % fname
    else:
        passC &= "const header$# = \"$#\"\n" % [fname, fl]
        extflags = "--header:header$#" % fname

    # Run c2nim on generated file
    var cmd = "c2nim $# $# --out:$# $#" % [flags, extflags, outfile, cfile]
    when defined(windows):
        cmd = "cmd /c " & cmd
    discard execProc(cmd)

    if preprocess or ctags:
        try:
            removeFile(cfile)
        except:
            discard

    # Import nim modules
    if recurse:
        prepend(outfile, incout)

    # Nim doesn't like {.cdecl.} for type proc()
    freplace(outfile, re"(?m)(.*? = proc.*?){.cdecl.}", "$#")
    freplace(outfile, " {.cdecl.})", ")")

    # Include {.compile.} directives
    for cpl in compile:
        let fcpl = search(cpl)
        if getFileInfo(fcpl).kind == pcFile:
            prepend(outfile, compile(file=fcpl))
        else:
            prepend(outfile, compile(dir=fcpl))

    # Add any pragmas
    if outpragma != "":
        prepend(outfile, outpragma)

    # Add header file and include paths
    if passC != "":
        prepend(outfile, passC)

    # Add dynamic library
    if outlib != "":
        prepend(outfile, outlib)

# ###
# Processor

proc runFile(file: string, cfgin: OrderedTableRef) =
    var cfg = cfgin
    var sfile = search(file)

    for pattern in WILDCARDS.keys():
        let pat = pattern.replace(".", "\\.").replace("*", ".*").replace("?", ".?")
        if file.find(re(pat)).isSome():
            echo "Appending " & file & " " & pattern
            for key in WILDCARDS[pattern].keys():
                cfg[key & "." & pattern] = WILDCARDS[pattern][key]

    var srch = ""
    var compile: seq[string] = @[]
    var dynlib: seq[string] = @[]
    var pragma: seq[string] = @[]
    for act in cfg.keys():
        let (action, val) = getKey(act)
        if val == true:
            if action == "create":
                createDir(file.splitPath().head)
                writeFile(file, cfg[act])
            elif action in @["prepend", "append", "replace", "comment", "rename", "compile", "dynlib", "pragma"] and sfile != "":
                if action == "prepend":
                    if srch != "":
                        prepend(sfile, cfg[act], cfg[srch])
                    else:
                        prepend(sfile, cfg[act])
                elif action == "append":
                    if srch != "":
                        append(sfile, cfg[act], cfg[srch])
                    else:
                        append(sfile, cfg[act])
                elif action == "replace":
                    if srch != "":
                        freplace(sfile, cfg[srch], cfg[act])
                elif action == "comment":
                    if srch != "":
                        comment(sfile, cfg[srch], cfg[act])
                elif action == "rename":
                    rename(sfile, cfg[act])
                elif action == "compile":
                    compile.add(cfg[act])
                elif action == "dynlib":
                    dynlib.add(cfg[act])
                elif action == "pragma":
                    pragma.add(cfg[act])
                srch = ""
            elif action == "search":
                srch = act

    if file.splitFile().ext != ".nim":
        var recurse = false
        var inline = false
        var preprocess = false
        var ctags = false
        var defines = false
        var noprocess = false
        var flags = "--stdcall"
        var ppflags = ""

        for act in cfg.keys():
            if cfg[act] == "true":
                if act == "recurse":
                    recurse = true
                elif act == "inline":
                    inline = true
                elif act == "preprocess":
                    preprocess = true
                elif act == "ctags":
                    ctags = true
                elif act == "defines":
                    defines = true
                elif act == "noprocess":
                    noprocess = true
            elif act == "flags":
                flags = cfg[act]
            elif act == "ppflags":
                ppflags = cfg[act]

        if recurse and inline:
            echo "Cannot use recurse and inline simultaneously"
            quit(1)

        if not noprocess:
            c2nim(file, getNimout(sfile), flags, ppflags, recurse, inline, preprocess, ctags, defines, dynlib, compile, pragma)

proc runCfg(cfg: string) =
    if not fileExists(cfg):
        echo "Config doesn't exist: " & cfg
        quit(1)

    CONFIG = loadConfig(cfg)

    if CONFIG.hasKey("n.global"):
        if CONFIG["n.global"].hasKey("output"):
            OUTPUT = CONFIG["n.global"]["output"]
            if dirExists(OUTPUT):
                if "-f" in commandLineParams():
                    try:
                        removeDir(OUTPUT)
                    except OSError:
                        echo "Directory in use: " & OUTPUT
                        quit(1)
                else:
                    for f in walkFiles(OUTPUT/"*.nim"):
                        try:
                            removeFile(f)
                        except OSError:
                            echo "Unable to delete: " & f
                            quit(1)
            createDir(OUTPUT)

        if CONFIG["n.global"].hasKey("filter"):
            FILTER = CONFIG["n.global"]["filter"]
        if CONFIG["n.global"].hasKey("quotes"):
            if CONFIG["n.global"]["quotes"] == "false":
                QUOTES = false

    if CONFIG.hasKey("n.include"):
        for inc in CONFIG["n.include"].keys():
            INCLUDES.add(inc)

    if CONFIG.hasKey("n.exclude"):
        for excl in CONFIG["n.exclude"].keys():
            EXCLUDES.add(excl)

    if CONFIG.hasKey("n.prepare"):
        for prep in CONFIG["n.prepare"].keys():
            let (key, val) = getKey(prep)
            if val == true:
                if key == "download":
                    downloadUrl(CONFIG["n.prepare"][prep])
                elif key == "extract":
                    extractZip(CONFIG["n.prepare"][prep])
                elif key == "git":
                    gitRemotePull(CONFIG["n.prepare"][prep])
                elif key == "gitremote":
                    gitRemotePull(CONFIG["n.prepare"][prep], false)
                elif key == "gitsparse":
                    gitSparseCheckout(CONFIG["n.prepare"][prep])
                elif key == "execute":
                    discard execProc(CONFIG["n.prepare"][prep])

    if CONFIG.hasKey("n.wildcard"):
        var wildcard = ""
        for wild in CONFIG["n.wildcard"].keys():
            let (key, val) = getKey(wild)
            if val == true:
                if key == "wildcard":
                    wildcard = CONFIG["n.wildcard"][wild]
                else:
                    WILDCARDS.setSectionKey(wildcard, wild, CONFIG["n.wildcard"][wild])

    for file in CONFIG.keys():
        if file in @["n.global", "n.include", "n.exclude", "n.prepare", "n.wildcard"]:
            continue

        runFile(file, CONFIG[file])

# ###
# Main loop

for i in commandLineParams():
    if i != "-f":
        runCfg(i)