-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake.py
More file actions
108 lines (93 loc) · 3.58 KB
/
make.py
File metadata and controls
108 lines (93 loc) · 3.58 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
#!/usr/bin/env python
# coding=utf8
import subprocess
import sys
import os
class COLORS_CLASS:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def __iter__(self):
for attr, value in COLORS_CLASS.__dict__.iteritems():
if (attr.startswith('__')):
continue
yield attr, value
COLORS = COLORS_CLASS()
def call(cmd, print_cmd=False):
if print_cmd:
print(cmd)
for color, value in COLORS:
cmd = cmd.replace(value, '')
if subprocess.call(cmd, shell=True, stderr=sys.stderr):
sys.exit(1)
def compile(filename, to_path=None, tags=[]):
suffix = '.' + '.'.join(filename.split('.')[-1:])
filename = '.'.join(filename.split('.')[:-1])
if not to_path:
to_path = os.path.dirname(filename)
to_path = os.path.join(to_path, os.path.basename(filename))
cmd = 'gcc ' + COLORS.UNDERLINE + COLORS.BOLD + '%s%s'%(filename, suffix) + COLORS.ENDC + ' -o %s.o %s'%(to_path, ' '.join(tags))
print(COLORS.OKBLUE + '[COMPILE]' + COLORS.ENDC + ' -> %s'%(cmd))
call(cmd)
return to_path + '.o'
def link(objects, output_filename, tags=[]):
cmd = 'gcc '
for obj in objects:
cmd += COLORS.UNDERLINE + COLORS.BOLD + obj + COLORS.ENDC + ' '
cmd += '-o %s'%output_filename
cmd += ' %s' % (' '.join(tags))
print(COLORS.OKGREEN + '[LINKING]' + COLORS.ENDC + ' -> %s'%(cmd))
call(cmd)
def lib_static(objects, output_filename):
cmd = 'ar rcs %s '%output_filename
for obj in objects:
cmd += COLORS.UNDERLINE + COLORS.BOLD + obj + COLORS.ENDC + ' '
print(COLORS.OKGREEN + '[ARCHIVE]' + COLORS.ENDC + ' -> %s'%(cmd))
call(cmd)
def make_debug():
objs = []
for root, dirs, files in os.walk('src'):
for f in [i for i in files if i.endswith('.cpp')]:
objs.append(compile(os.path.join(root, f), 'objects', ['-Wall', '-g', '-c', '-lstdc++', '-std=c++11']))
link(objs, 'a.out', ['-ldl', '-lstdc++', '-std=c++11'])
def make_static():
objs = []
for root, dirs, files in os.walk('src'):
for f in [i for i in files if i.endswith('.cpp') and i != 'main.cpp']:
objs.append(compile(os.path.join(root, f), 'objects', ['-Wall', '-g', '-c', '-lstdc++', '-std=c++11']))
lib_static(objs, 'lib/libco.a')
def make_dynamic():
objs = []
for root, dirs, files in os.walk('src'):
for f in [i for i in files if i.endswith('.cpp') and i != 'main.cpp']:
objs.append(compile(os.path.join(root, f), 'objects', ['-fPIC', '-Wall', '-g', '-c', '-lstdc++', '-std=c++11']))
link(objs, 'lib/libco.so', ['-shared', '-ldl', '-lstdc++', '-std=c++11'])
def make_test():
make_dynamic()
objs = []
for root, dirs, files in os.walk('test'):
for f in [i for i in files if i.endswith('.cpp')]:
objs.append(compile(os.path.join(root, f), 'objects', ['-Wall', '-g', '-c', '-lstdc++', '-std=c++11']))
link(objs, 'test.out', ['-L./lib', '-lco', '-lstdc++', '-std=c++11', '-Wl,-rpath=./lib'])
def make_clean():
call('rm -f objects/*.o', print_cmd=True)
call('rm -f lib/*.so', print_cmd=True)
call('rm -f *.out', print_cmd=True)
call('rm -f lib/*.a', print_cmd=True)
if __name__=='__main__':
if len(sys.argv) < 2:
make_dynamic()
sys.exit(0)
if 'clean' == sys.argv[1]:
make_clean()
elif 'static' == sys.argv[1]:
make_static()
elif 'dynamic' == sys.argv[1]:
make_dynamic()
elif 'test' == sys.argv[1]:
make_test()