/
make.py
125 lines (93 loc) · 2.66 KB
/
make.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
##################################################
#### *** help you to create a makefile! *** ####
#
# author: kur0mi
# email: <1601631551@qq.com>
# date: 2017/11/18
#
# description:
#
# ** for use it, you may need a c/c++ compiler and python runtime.
# usage:
#
# ** first copy this script to your directory,
# ** second type 'python make.py' to create a makefile,
# ** then type 'make' to execute it!
###################################################
####################################################
# Custom part: modify it by yourself
#
# header file(.h) path
SRCDIRS = ["src"]
LIBDIRS = []
# which ones you wanna to compile
SRC_CXX = ["design/4.cpp"]
# C++ compile and link option
CXX = "g++"
FLAGS = "-g -O2 -Wall -std=c++1z"
#
#
###############################################################
###############################################################
# Stable part: please do not modify the code below
#
FILENAME = "Makefile"
HEAD = " ".join(['-I' + x for x in SRCDIRS] + ['-L' + x for x in LIBDIRS])
LINK = ""
DEP = "-MM"
COMPILE = CXX + ' ' + FLAGS + ' ' + HEAD
LINK = CXX + ' ' + FLAGS + ' ' + LINK
DEP = CXX + ' ' + DEP + ' ' + HEAD
#
#
###############################################################
import os
import sys
reload(sys)
sys.setdefaultencoding('utf8')
linkfile = []
allbase = []
def getHead():
# make the total target
return 'all: ' + ' '.join([x.replace('.cpp', '.exe')
for x in SRC_CXX]) + '\n'
def getPart(cpp):
r = ''
s0 = os.popen("{} {}".format(DEP, cpp)
).readline().strip()
s1 = "\n\t" + "{} -c $< -o $@".format(COMPILE) + "\n"
base = s0.split('.')[0]
if base not in allbase:
r += s0 + s1
allbase.append(base)
linkfile.append(base + '.o')
h = filter(lambda x: base not in x, s0.split(' '))
cpps = [x.replace('.h', '.cpp') for x in h]
for cpp in cpps:
r += getPart(cpp)
return r
def getLink(cpp):
exe = cpp.replace('.cpp', '.exe')
r = ''
r += exe + ': ' + ' '.join(linkfile)
r += '\n\t' + LINK + '$^ -o $@' + '\n'
return r
def getCoffee():
r = ''
r += '.PHONY: clean' + '\n'
r += 'clean: ' + '\n'
r += '\t' + 'powershell -c "rm ' + '*.o"' + '\n'
return r
def main():
with open(FILENAME, "w") as fp:
r = ''
r += getHead()
global linkfile
for x in SRC_CXX:
r += getPart(x)
r += getLink(x)
linkfile = []
r += getCoffee()
fp.write(r)
if __name__ == '__main__':
main()