/
pymake.py
164 lines (138 loc) · 6 KB
/
pymake.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
import json
from pprint import pprint
name=''
cflags=''
common_CFLAGS=''
cpu_includes=''
cppflags=''
cpu_sources=''
final_sources=''
ldflags=''
json_data = dict()
data = dict()
fileContents = ''
make_include = ''
def output():
print("Makefile Generated Successfully")
def read():
global data, fileContents, json_data
with open('Libcutils-Android.json') as json_file:
json_data = json.load(json_file)
def write(fileContents):
global data, json_data
filename = input("Enter the name of the makefile")
makefile = open(filename+'.mk','w')
makefile.write(fileContents)
makefile.close
def sources():
global data,fileContents,json_data,name,source,final_sources,cpu_sources,make_include
for data in json_data:
if 'cc_library' in data.keys():
name = 'NAME = '+data['cc_library']['name']
source = 'SOURCES = '
if 'srcs' in data['cc_library']:
data['cc_library']['srcs'].sort()
for j in data['cc_library']['srcs']:
source+= '\t\t\t'+j+'\\'+'\n'
if 'arch' in data['cc_library'].keys():
for j in data['cc_library']['arch'].keys():
cpu_sources+= j+'_SOURCES = '
for k in data['cc_library']['arch'][j]['srcs']:
cpu_sources+= '\t\t\t'+k+' '+'\\'+'\n'
final_sources = 'SOURCES+= $($($(DEB_HOST_ARCH)_SOURCES))'
if 'target' in data['cc_library'].keys():
if 'android' in data['cc_library']['target'].keys():
if 'srcs' in data['cc_library']['target']['android'].keys():
data['cc_library']['target']['android']['srcs'].sort()
for i in (data['cc_library']['target']['android']['srcs']):
source+= '\t\t\t'+i+'\\'+'\n'
print(name)
print(source)
#Can be added if wanted
#make_include = input('Enter name of the other makefiles to be includec')
fileContents+= name+'\n'+source+'\n'+cpu_sources+'\n'+final_sources
def ldFlags():
global data, fileContents, json_data,ldflags
ldflags+= 'LDFLAGS += -shared -Wl,-soname,'
#These LDFLAGS are found in almost all makefiles so I have written it by default
for data in json_data:
if 'cc_library' in data.keys():
if 'target' in data['cc_library'].keys():
if 'linux' in data['cc_library']['target'].keys():
ldflags = 'LDFLAGS+= '
if 'ldflags' in data['cc_library']['target']['linux'].keys():
for i in data['cc_library']['target']['linux']['ldflags']:
ldflags+= i + ' '
if 'host_ldlibs' in data['cc_library']['target']['linux'].keys():
for i in data['cc_library']['target']['linux']['host_ldlibs']:
ldflags+= i + ' '
print(ldflags)
fileContents+= '\n'+ldflags
def cFlags():
#Cflags - any changes write it here in comments
global data, fileContents, json_data,cflags,common_CFLAGS
for data in json_data:
if 'common_CFLAGS' in data.keys():
for i in data['common_CFLAGS']:
common_CFLAGS += ' ' + i
if 'cc_defaults' in data.keys():
if data['cc_defaults']['cflags'] == 'common_CFLAGS':
cflags += 'CFLAGS +=' + common_CFLAGS
else:
cflags = 'CFLAGS+= '
for j in data['cc_defaults']['cflags']:
cflags+= j + ' '
print(cflags)
fileContents+= '\n'+cflags
def cppFlags():
#Cppflags - there are certain cppflags in almost all makefiles so I have written it here. Changes in comments
global data, fileContents, json_data,cppflags,cpu_includes
cppflags+='CPPFLAGS +='
cppflags+= '-Iinclude -Idebian/include'
for data in json_data:
if 'cc_defaults' in data.keys():
if 'arch' in data['cc_defaults'].keys():
cpu_includes = ''
for j in data['cc_defaults']['arch'].keys():
cpu_includes+= j+'_SOURCES = '
for k in data['cc_defaults']['arch'][j]['local_include_dirs']:
cpu_includes+='\t\t\t'+k+' '+'\\'+'\n'
cppflags = 'CPPFLAGS+= $($(CPU)_INCLUDES)) '
if 'local_include_dirs' in data['cc_defaults'].keys():
for j in data['cc_defaults']['local_include_dirs']:
cppflags+= '-I'+j+' '
print(cppflags)
fileContents+= '\n'+cpu_includes+'\n'+cppflags
def Build():
#build-build file requires the directory where the target is to be created. So target directory is taken as input and build function is written
global data, fileContents, json_data,command
print('CSOURCES := $(foreach source,$(filter %.c,$(SOURCES), libcutils/$(source)) ')
print('CPPSOURCES := $(foreach source,$(filter %.cpp,$(SOURCES), libcutils/$(source)) ')
command = input("Enter the directory you want to build\n")
if command == '':
command = command
else :
command = command+'/'
print('build: ' + '$(SOURCES)' + '\n\t\t' + '$(CC) $^ -o ' + command + '$(NAME) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)')
fileContents+= '\n'+'CSOURCES := $(foreach source,$(filter %.c,$(SOURCES),'+command+'$(source)) '+'\n'+'CPPSOURCES := $(foreach source,$(filter %.cpp,$(SOURCES), '+command+'$(source)) '+'\n'+'build: ' + '$(SOURCES)' + '\n\t\t' + '$(CC) $^ -o ' + command + '$(NAME) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)'
#print('CPU_SOURCES',cpu_sources)
#print('FINAL_SOURCES',final_sources)
#print('CPU_INCLUDES',cpu_includes)
def Clean():
#clean- Changes required can be commented
global data, fileContents, json_data
print('\n'+'clean: $(RM) '+command+'$(NAME)')
fileContents+= '\n'+'clean: $(RM) '+command+'$(NAME)'
def start():
global data, fileContents, json_data
read()
sources()
cFlags()
cppFlags()
ldFlags()
Build()
Clean()
write(fileContents)
output()
exit(0)
start()