-
Notifications
You must be signed in to change notification settings - Fork 4
/
a2lib.py
54 lines (47 loc) · 2.01 KB
/
a2lib.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Convert .a to .lib by generating a list of exported functions from
DLL into .def file and generating import library based on that .def
file. Run this tool from VS command prompt."""
import argparse
import os,re,sys,shutil
from os.path import join, getsize
from subprocess import Popen, PIPE
def gen(dll,lib,d):
output = Popen(["dumpbin", "-exports", dll], stdout=PIPE).communicate()[0]
with open(d, "wb") as f:
f.write(b"EXPORTS\n")
lines = output.splitlines()
for line in lines[19:]:
if line != "" and line[0] == " ":
cols = line.split()
if len(cols)==4 and cols[0].isdigit():
# msvcrt.dll on windows misses secure versions of common CRT functions
if not ("msvcrt.dll" == dll and cols[3].endswith(b"_s")):
f.write(cols[3] + b"\n")
f.write(str.encode("LIBRARY %s\n" % dll))
def walk(root):
os.chdir(root + "\\lib")
for root, dirs, files in os.walk(root + "\\bin"):
for f in files:
if (re.search(".dll", f)):
name = re.sub("^lib", "", f)
name = re.sub("\\.dll$", "", name)
name = re.sub("(?:-\\d+)$", "", name)
d = "%s.def" % name
if not os.path.exists(d):
print("Working on %s to produce %s\n" % (f, d))
lib = "lib%s.dll.a" % name
gen(join(root, f), lib, d)
Popen(["lib", "/def:%s" % d, "/name:%s" % f]).communicate()
def get_parser():
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--root',
default="C:/msys32/mingw32",
help='Root directory for MINGW. The one that has /bin and /lib.')
return parser
if __name__ == '__main__':
p = get_parser()
args = p.parse_args()
walk(args.root)