-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_codal_target.py
More file actions
201 lines (154 loc) · 5.89 KB
/
create_codal_target.py
File metadata and controls
201 lines (154 loc) · 5.89 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import os
from os import walk
from os import listdir
from subprocess import call
from subprocess import check_output
#
# create_codal_target.py
#
# Takes a working build of an mbedos "blinky" and cretes a static library containing mbed functionality
# Also extracts neessary header files.
#
#
# Function to create a directory, if necessary.
#
def create_dir(directory):
if not os.path.exists(directory):
#print ("making1...." + directory)
#call(["mkdir", "-p", directory])
os.makedirs(directory)
#print ("making2....")
#
# Function to parse the link file descriptions, and return a list of object files.
#
def generate_lib(infilename, outfile) :
infile = open(infilename)
params = infile.readline().split(" ")
#cmd = ["arm-none-eabi-ar", "-rcs", outfile]
cmd = ["arm-none-eabi-ld", "-Ur", "-o", outfile]
objs = []
for item in params :
#print(item)
if item.endswith(".o") and not item.endswith("main.o") :
if item.startswith('"') and item.endswith('"') :
objs.append(item[1:-1])
else :
objs.append(item)
# We should no have filtered out all but the object files.
#for item in objs:
# print item
for item in objs:
shit = []
shit.append(item)
call(cmd + shit)
#
# Function to parse the link file descriptions, and return a list of object files.
#
def generate_o(infilename, outdir) :
infile = open(infilename)
params = infile.readline().split(" ")
for item in params :
if item.endswith(".o\"") and not item.endswith("/main.o\"") :
if item.startswith('"') and item.endswith('"') :
obj = item[1:-1]
else :
obj = item
#print(obj)
obj = obj.replace("/","\\")
outdir = outdir.replace("/","\\")
objNoExt = obj.replace(".o", "")
objNoExtPath = objNoExt.replace("BUILD\\NUCLEO_L452RE_P\\GCC_ARM-RELEASE\\", "..\\source\\")
if not os.path.exists(objNoExtPath + ".c") and not os.path.exists(objNoExtPath + ".cpp") and not os.path.exists(objNoExtPath + ".s"):
cmd = 'copy /y ' + obj + ' ' + outdir + ' > NUL'
os.system(cmd)
else:
print('Found ' + obj)
#cmd = ["copy /y ", obj , outdir ]
#print(cmd)
#call (cmd)
#cmd = 'copy /y ' + obj + ' ' + outdir + ' > NUL'
#print(cmd)
#os.system(cmd)
#shutil.copy('file1.txt', 'file3.txt')
#
# Function to extract a copy of all necessary include files.
#
def generate_includes(infilename, outdir) :
infile = open(infilename)
create_dir(outdir)
if not outdir.endswith("/") :
outdir = outdir + "/"
cwd = os.getcwd().replace("\\","/") + "/"
params = infile.readline().split(" ")
dirs = []
for item in params :
if item.startswith('"') and item.endswith('"') :
item = item[1:-1]
if item.startswith("-I") :
inc = item[2:]
if inc.startswith('"') and inc.endswith('"') :
inc = inc[1:-1]
inc = inc.replace("\\","/")
if inc.startswith(cwd) :
inc = inc[len(cwd):]
if os.path.exists(inc) :
dirs.append(inc)
# We should no have filtered out all but the object files.
for item in dirs :
out = outdir + item
if out.startswith('./codal-mbedos/inc/./mbed-os'):
out1 = out[28:]
out1 = 'RECURSIVE_FIND_DIR(INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/${CODAL_OUTPUT_NAME}/inc' + out1 + '" "*.h"'
#print(out1)
create_dir(out)
#cmd = ["sh", "-c", "cp " + item + "/*.h " + out]
agrs = item + "/*.h " + out
agrs = agrs.replace("/","\\")
cmd = 'copy /y ' + agrs + ' > NUL'
headers = [f for f in listdir(item) if f.endswith(".h")]
if (len(headers) > 0) :
#print(cmd)
#call(cmd)
os.system(cmd)
#
# Search the given folder structure for a file matching the given prefic and postfix.
# Return the first match we find.
#
def find_file(prefix, postfix, directory) :
for (dirpath, dirnames, filenames) in walk(directory) :
for (f) in filenames :
if f.startswith(prefix) and f.endswith(postfix) :
return dirpath + "/" + f;
# Determine our target
targetnamestring = check_output(["mbed","target"]).decode("utf-8")
#print (targetnamestring)
#targetname = targetnamestring[targetnamestring.index(" ") + 1:].strip()
#print ("This is targetname:")
#print (targetname)
#print ("End")
targetname = "NUCLEO_L452RE_P"
# targetname = "NUCLEO_L476RG"
#targetname = "NUCLEO_F401RE"
# The file containing the command line linker
#linkfilename = find_file (".link_files", ".txt", "BUILD/" + targetname + "/GCC_ARM")
linkfilename = find_file (".link_", ".txt", "BUILD/" + targetname + "/GCC_ARM-RELEASE")
print (linkfilename)
# The file containing the compile time include paths
includefilename = find_file (".includes_", ".txt", "BUILD/" + targetname + "/GCC_ARM-RELEASE")
print (includefilename)
# The directory to store our output in
outputdir = "./codal-mbedos"
#
# Create a subdirectory for export, and populate with a generated static library and include files.
#
create_dir(outputdir)
create_dir(outputdir+"/obj")
#
#print("Creating codal-mbedos library...")
#generate_lib(linkfilename, outputdir + "/mbedos.o")
print("Creating codal-mbedos object...")
generate_o(linkfilename, outputdir + "/obj")
#
print("Creating codal-mbedos includes...")
generate_includes(includefilename, outputdir + "/inc")
print("done.")