-
Notifications
You must be signed in to change notification settings - Fork 7
/
configure.py
169 lines (137 loc) · 5.03 KB
/
configure.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
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
#! /usr/bin/python
#coding=utf-8
import os
from sys import platform
def filt_file_type(filename = ''):
a_relation = ''
if filename.endswith('.cpp'):
a_relation = '$(CXX) $(CXXFLAGS)'
elif filename.endswith('.c'):
a_relation = '$(CC) $(CFLAGS)'
return a_relation
# 区分C++ 和 C
def makeout(src_file):
a_relation = filt_file_type(src_file)
if '' == a_relation:
return None
obj_file = '$(OBJ)/' + src_file.replace('/', '_').replace('.', '_') + '.o'
return [obj_file, '%s : $(SRC)/%s\n\t%s $^ -o $@ -I $(INC)\n' % (obj_file, src_file, a_relation)]
class Makefile:
def __init__(self, prefix = '', framework = '', dirTree = {}):
self.framework = framework
self.root = os.path.split(os.path.realpath(__file__))[0]
self.prefix = prefix
self.dirTree = dirTree
def header(self):
shareflags = 'SHAREFLAGS = -shared -fPIC -lc -lpthread -lstdc++'
if 'darwin' != platform:
shareflags += ' -lrt'
return '\n'.join([
'#! /bin/make',
'PREFIX = %s' % self.prefix,
'ROOT = %s' % self.root,
'INC = $(ROOT)/include/kit',
'SRC = $(ROOT)',
'DIST = $(ROOT)/dist',
'OBJ = $(DIST)/objs',
'ASM = $(DIST)/asms',
'LIB = $(DIST)/lib',
'BIN = $(DIST)/bin',
'CXX = g++',
'CC = gcc',
'AR = ar',
'AS = as',
'LD = ld',
'CFLAGS = -c -O3 -Wall -fPIC --std=c11',
'CXXFLAGS = -c -O3 -Wall -fPIC --std=c++17',
'ARFLAGS = -crsv',
shareflags,
'\n'
])
def target(self):
# 编译framework
aoRelations = dict()
osRelations = dict()
export_share = dict()
for parent, dirnames, filenames in os.walk(self.framework):
if parent == self.framework:
# 一级目录
for dirname in dirnames:
# 目标静态库
aFile = '$(LIB)/lib' + dirname.capitalize() + '.a'
aoRelations[aFile] = []
osRelations[aFile] = []
for filename in filenames:
osr = makeout(os.path.join('framework', filename))
if osr:
export_share[osr[0]] = osr[1]
else:
# 分开一级目录和子目录
dirname = parent[1 + len(self.framework):].split('/', 1)
aName = dirname[0]
aFile = '$(LIB)/lib' + aName.capitalize() + '.a'
for filename in filenames:
srcFile = os.path.join('framework', aName, '_'.join(dirname[1:]), filename)
# 加入静态库依赖队列
osr = makeout(srcFile)
if osr:
aoRelations[aFile].append(osr[0])
osRelations[aFile].append(osr[1])
dllDeps = []
alibSegment = ''
# 导出部分
for dep in export_share:
dllDeps.append(dep)
alibSegment += export_share[dep] + '\n'
alibSegment += '\n'
# 静态库部分
for aLib in aoRelations:
aLibRelations = aoRelations[aLib]
if 0 == len(aLibRelations):
print("WARNING: " + aLib + " has no file.")
continue
dllDeps.append(aLib)
alibSegment += '%s : %s\n\t$(AR) $(ARFLAGS) $@ $^\n\n%s\n\n' % (aLib, ' '.join(aLibRelations), '\n'.join(osRelations[aLib]))
so = '$(LIB)/libFramework.so : %s\n\t$(CXX) $(SHAREFLAGS) -o $@ $^\n\n\n' % (' '.join(dllDeps)) + alibSegment
return so
def install(self):
return '\n'.join([
'install :',
'\tmv $(LIB)/*.a %s/lib/' % self.prefix,
'\tmv $(LIB)/*.so %s/lib/' % self.prefix,
'\n',
])
def clean(self):
foo = map(lambda word: '\trm -rf $(DIST)/' + word.name + '/*', self.dirTree.childs)
return '\n'.join(['clean :'] + list(foo) + ['\n'])
def __str__(self):
return '\n'.join([self.header(), self.target(), self.clean(), self.install()])
# 中序遍历目录树,创建目录
def makedirs(dirTree, path = ''):
if '' != path:
path += '/'
absPath = path + dirTree.name
if not os.path.exists(absPath):
os.makedirs(absPath)
for item in dirTree.childs:
makedirs(item, absPath)
class DirTreeNode:
def __init__(self, name = ''):
self.childs = []
self.name = name
def addChild(self, name = ''):
childNode = DirTreeNode(name)
self.childs.append(childNode)
def main():
distDir = DirTreeNode('dist')
distDir.addChild('objs')
distDir.addChild('asms')
distDir.addChild('lib')
makefile = Makefile('', './framework', distDir)
# 创建存放目标文件的目录
makedirs(distDir)
fd = open('makefile', 'w+')
fd.write('%s' % makefile)
fd.close()
if '__main__' == __name__:
main()