-
Notifications
You must be signed in to change notification settings - Fork 0
/
genmake
executable file
·126 lines (99 loc) · 3.8 KB
/
genmake
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
#! /usr/bin/env python
import os
import re
import string
import sys
if len(sys.argv) < 2:
targetname = "LinearModel"
else:
targetname = sys.argv[1]
releaseflags = "-lsqlite3 -lgsl -lgslcblas -O2 -pipe -march=native"
debugflags = "-lsqlite3 -lgsl -lgslcblas -O2 -pg -fno-inline -pipe -march=native"
headers = []
sources = []
sources_suffix = []
# function that recursively grabs dependencies
def getDependencies(filename, dependencies):
print "Getting Header Dependencies for %s" % filename
filename = string.join(["src/",filename], "")
f = open(filename)
# Use regular expressions to get header file dependencies
reg = re.compile(r"\s*#include\s*[\"<](\w*[.]?\w*)[>\"].*$")
for line in f:
m = reg.match(line);
if m is not None:
header = m.group(1)
if header in headers:
print "%s depends on %s" % (filename, header)
if not header in dependencies:
dependencies.append(header)
getDependencies(header, dependencies)
f.close()
#look through the folder to find all source and headers
print "Searching Directory\n"
for filename in os.listdir("./src"):
#check the extensions and sort them into source and headers
if filename.endswith(".h") or filename.endswith(".hpp"):
print "Found header: %s" % filename
headers.append(filename)
if filename.endswith(".c"):
print "Found source: %s" % filename
sources.append(filename.replace(".c",""))
sources_suffix.append("c")
if filename.endswith(".cpp"):
print "Found source: %s" % filename
sources.append(filename.replace(".cpp",""))
sources_suffix.append("cpp")
makefile = open("Makefile","w");
#make release target, with dependencies on all object files compiled
#from sources
print "Generating Target %s" % targetname
print >> makefile, "%s:" % targetname,
for o in sources:
print >> makefile, "obj/%s.o" % o,
print >> makefile
print >> makefile, "\tg++ %s" % releaseflags ,
for o in sources:
print >> makefile, "obj/%s.o" % o,
print >> makefile, "-o %s" % targetname
print >> makefile
#make debug target, with dependencies on all object files compiled
#from sources
print "Generating debug targets"
print >> makefile, "debug: %s_debug" % targetname
print >> makefile
print >> makefile, "%s_debug:" % targetname,
for o in sources:
print >> makefile, "obj/%s_debug.o" % o,
print >> makefile
print >> makefile, "\tg++ %s" % debugflags ,
for o in sources:
print >> makefile, "obj/%s_debug.o" % o,
print >> makefile, "-o %s_debug" % targetname
print >> makefile
#Generate clean target
print "Generating Target Clean"
print >> makefile, "clean:"
for o in sources:
print >> makefile, "\trm -f obj/%s.o obj/%s_debug.o" % (o,o)
print >> makefile , "\trm -f %s %s_debug" % (targetname,targetname)
print >> makefile
#Generate targets for each object file
print >> makefile , "# Object Targets"
print "\n***Generating object targets***\n"
for index,s in enumerate(sources):
print "\n***Generating target for: %s***\n" % s
# Get dependencies
print "Getting Dependencies\n"
dependencies = [string.join([s,sources_suffix[index]],".")]
getDependencies(dependencies[0],dependencies)
for i, d in enumerate(dependencies):
dependencies[i] = string.join(["src/",d],"")
# Write the release target
print >> makefile , "obj/%s.o: %s" % (s,string.join(dependencies," "))
print >> makefile , "\tg++ -c %s src/%s.%s -o obj/%s.o" % (releaseflags,s,sources_suffix[index],s)
print >> makefile
# Write the debug target
print >> makefile , "obj/%s_debug.o: %s" % (s,string.join(dependencies," "))
print >> makefile , "\tg++ -c %s src/%s.%s -o obj/%s_debug.o" % (debugflags,s,sources_suffix[index],s)
print >> makefile