forked from openmv/openmv-ide
-
Notifications
You must be signed in to change notification settings - Fork 1
/
make.py
executable file
·354 lines (318 loc) · 14.7 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python
# by: Kwabena W. Agyeman - kwagyeman@openmv.io
import argparse, glob, multiprocessing, os, re, shutil, stat, sys, subprocess, errno
def match(d0, d1):
x = [x for x in os.listdir(d0) if re.match(d1, x)]
return os.path.join(d0, x[0]) if x else None
def search(d0, d1):
x = [x for x in os.listdir(d0) if re.search(d1, x)]
return os.path.join(d0, x[0]) if x else None
def find_qtdir(rpi):
if rpi:
os.environ["QTDIR"] = rpi
path = os.path.join(rpi, "bin") + ':'
os.environ["PATH"] = path + os.environ["PATH"]
return rpi
elif sys.platform.startswith('win'):
qtdir = match(os.sep, r"Qt")
if qtdir:
qtdir = match(qtdir, r"\d\.\d(\.\d)?")
if qtdir:
qtdir = search(qtdir, r"mingw")
if qtdir:
os.environ["QTDIR"] = qtdir
path = ';' + os.path.join(qtdir, "bin")
os.environ["PATH"] = os.environ["PATH"] + path
return qtdir
elif sys.platform.startswith('darwin'):
qtdir = match(os.path.expanduser('~'), r"Qt")
if qtdir:
qtdir = match(qtdir, r"\d\.\d(\.\d)?")
if qtdir:
qtdir = search(qtdir, r"clang")
if qtdir:
os.environ["QTDIR"] = qtdir
path = ':' + os.path.join(qtdir, "bin")
os.environ["PATH"] = os.environ["PATH"] + path
return qtdir
elif sys.platform.startswith('linux'):
qtdir = match(os.path.expanduser('~'), r"Qt")
if qtdir:
qtdir = match(qtdir, r"\d\.\d(\.\d)?")
if qtdir:
qtdir = search(qtdir, r"gcc")
if qtdir:
os.environ["QTDIR"] = qtdir
path = ':' + os.path.join(qtdir, "bin")
os.environ["PATH"] = os.environ["PATH"] + path
return qtdir
return None
def find_mingwdir():
if sys.platform.startswith('win'):
mingwdir = match(os.sep, r"Qt")
if mingwdir:
mingwdir = match(mingwdir, r"Tools")
if mingwdir:
mingwdir = search(mingwdir, r"mingw")
if mingwdir:
os.environ["MINGWDIR"] = mingwdir
path = ';' + os.path.join(mingwdir, "bin")
os.environ["PATH"] = os.environ["PATH"] + path
return mingwdir
return None
def find_qtcdir():
if sys.platform.startswith('win'):
qtcdir = match(os.sep, r"Qt")
if qtcdir:
qtcdir = match(qtcdir, r"Tools")
if qtcdir:
qtcdir = match(qtcdir, r"QtCreator")
if qtcdir:
os.environ["QTCDIR"] = qtcdir
path = ';' + os.path.join(qtcdir, "bin")
os.environ["PATH"] = os.environ["PATH"] + path
return qtcdir
return None
def find_ifdir():
if sys.platform.startswith('win'):
ifdir = match(os.sep, r"Qt")
if ifdir:
ifdir = search(ifdir, r"QtIFW")
if ifdir:
os.environ["IFDIR"] = ifdir
path = ';' + os.path.join(ifdir, "bin")
os.environ["PATH"] = os.environ["PATH"] + path
return ifdir
elif sys.platform.startswith('darwin'):
ifdir = match(os.path.expanduser('~'), r"Qt")
if ifdir:
ifdir = search(ifdir, r"QtIFW")
if ifdir:
os.environ["IFDIR"] = ifdir
path = ':' + os.path.join(ifdir, "bin")
os.environ["PATH"] = os.environ["PATH"] + path
return ifdir
elif sys.platform.startswith('linux'):
ifdir = match(os.path.expanduser('~'), r"Qt")
if ifdir:
ifdir = search(ifdir, r"QtIFW")
if ifdir:
os.environ["IFDIR"] = ifdir
path = ':' + os.path.join(ifdir, "bin")
os.environ["PATH"] = os.environ["PATH"] + path
return ifdir
return None
def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,
env=None):
"""Call the given command(s)."""
assert isinstance(commands, list)
p = None
for c in commands:
try:
dispcmd = str([c] + args)
# remember shell=False, so use git.cmd on windows, not just git
p = subprocess.Popen([c] + args, cwd=cwd, env=env,
stdout=subprocess.PIPE,
stderr=(subprocess.PIPE if hide_stderr
else None))
break
except EnvironmentError:
e = sys.exc_info()[1]
if e.errno == errno.ENOENT:
continue
if verbose:
print("unable to run %s" % dispcmd)
print(e)
return None, None
else:
if verbose:
print("unable to find command, tried %s" % (commands,))
return None, None
stdout = p.communicate()[0].strip()
if sys.version_info[0] >= 3:
stdout = stdout.decode()
if p.returncode != 0:
if verbose:
print("unable to run %s (error)" % dispcmd)
print("stdout was %s" % stdout)
return None, p.returncode
return stdout, p.returncode
def generate_git_rev():
GITS = ["git"]
if sys.platform == "win32":
GITS = ["git.cmd", "git.exe"]
describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty",
"--always", "--long",
]
)
# --long was added in git-1.5.5
if describe_out is None:
return "debug"
return describe_out
def make():
__folder__ = os.path.dirname(os.path.abspath(__file__))
parser = argparse.ArgumentParser(description =
"Make Script")
parser.add_argument("--rpi", nargs = '?',
help = "Cross Compile QTDIR for the Raspberry Pi")
parser.add_argument("-u", "--upload", nargs = '?',
help = "FTP Password")
args = parser.parse_args()
if args.rpi and not sys.platform.startswith('linux'):
sys.exit("Linux Only")
###########################################################################
git_version = generate_git_rev()
print("git_version:" + git_version)
cpus = multiprocessing.cpu_count()
qtdir = find_qtdir(args.rpi)
mingwdir = find_mingwdir()
qtcdir = find_qtcdir()
ifdir = find_ifdir()
builddir = os.path.join(__folder__, "build")
installdir = os.path.join(builddir, "install")
if not os.path.exists(builddir):
os.mkdir(builddir)
installer = ""
if args.rpi:
# Add Fonts...
if os.path.exists(os.path.join(installdir, "lib/Qt/lib/fonts")):
shutil.rmtree(os.path.join(installdir, "lib/Qt/lib/fonts"), ignore_errors = True)
shutil.copytree(os.path.join(__folder__, "dejavu-fonts/fonts/"),
os.path.join(installdir, "lib/Qt/lib/fonts"))
# Add README.txt...
with open(os.path.join(installdir, "README.txt"), 'w') as f:
f.write("Please run setup.sh to install OpenMV IDE dependencies... e.g.\n\n")
f.write("./setup.sh\n\n")
f.write("source ~/.profile\n\n")
f.write("./bin/openmvide\n\n")
# Add setup.sh...
with open(os.path.join(installdir, "setup.sh"), 'w') as f:
f.write("#! /bin/sh\n\n")
f.write("DIR=\"$(dirname \"$(readlink -f \"$0\")\")\"\n")
f.write("sudo apt-get install -y ibxcb* libGLES* libts* libsqlite* libodbc* libsybdb* libusb-1.0 python-pip libgles2-mesa-dev libpng12-dev qt5-default libts-dev\n")
f.write("sudo pip install pyusb\n\n")
f.write("sudo cp $( dirname \"$0\" )/share/qtcreator/pydfu/50-openmv.rules /etc/udev/rules.d/50-openmv.rules\n")
f.write("sudo udevadm control --reload-rules\n\n")
f.write("if [ -z \"${QT_QPA_PLATFORM}\" ]; then\n")
f.write(" echo >> ~/.profile\n")
f.write(" echo \"# Force Qt Apps to use xcb\" >> ~/.profile\n")
f.write(" echo \"export QT_QPA_PLATFORM=xcb\" >> ~/.profile\n")
f.write(" echo\n")
f.write(" echo Please type \"source ~/.profile\".\n")
f.write("fi\n\n")
f.write("# Make sure hard linked libts library is there\n\n")
f.write("LINK=/usr/lib/arm-linux-gnueabihf/libts-0.0.so.0\n")
f.write("if [ ! -e ${LINK} ]\n")
f.write("then\n")
f.write(" TSLIB=`find /usr/lib/arm-linux-gnueabihf/ -type f -name libts.so*`\n")
f.write(" if [ ! -z ${TSLIB} ]\n")
f.write(" then\n")
f.write(" sudo ln -s ${TSLIB} ${LINK}\n")
f.write(" else\n")
f.write(" echo \"Could not find libts library\"\n")
f.write(" exit 1\n")
f.write(" fi\n")
f.write("fi\n\n")
f.write("while true; do\n")
f.write(" read -r -p \"\nInstall Desktop Shortcut? [y/n] \" _response\n")
f.write(" case \"$_response\" in\n")
f.write(" [Yy][Ee][Ss]|[Yy])\n")
f.write(" cat > /home/$USER/Desktop/openmvide.desktop << EOM\n")
f.write("[Desktop Entry]\n")
f.write("Type=Application\n")
f.write("Comment=The IDE of choice for OpenMV Cam Development.\n")
f.write("Name=OpenMV IDE\n")
f.write("Exec=$DIR/bin/openmvide\n")
f.write("Icon=$DIR/share/icons/hicolor/512x512/apps/OpenMV-openmvide.png\n")
f.write("Terminal=false\n")
f.write("StartupNotify=true\n")
f.write("Categories=Development\n")
f.write("MimeType=text/x-python\n")
f.write("EOM\n")
f.write(" echo \"You must logout and login again for the desktop shortcut to work.\"\n")
f.write(" exit 0\n")
f.write(" ;;\n")
f.write(" [Nn][Oo]|[Nn])\n")
f.write(" exit 0\n")
f.write(" ;;\n")
f.write(" *)\n")
f.write(" ;;\n")
f.write(" esac\n")
f.write("done\n\n")
os.chmod(os.path.join(installdir, "setup.sh"),
os.stat(os.path.join(installdir, "setup.sh")).st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
# Build...
if os.system("cd " + builddir +
" && qmake ../qt-creator/qtcreator.pro -r OMV_IDE_GIT_VERSION=\"" + git_version + "\"" +
" && make -r -w -j" + str(cpus) +
" && make bindist INSTALL_ROOT="+installdir):
sys.exit("Make Failed...")
installer = glob.glob(os.path.join(builddir, "canmv-ide-*.tar.gz"))[0]
elif sys.platform.startswith('win'):
if os.system("cd " + builddir +
" && qmake ../qt-creator/qtcreator.pro -r -spec win32-g++ OMV_IDE_GIT_VERSION=\"" + git_version + "\"" +
" && jom -j" + str(cpus) +
" && jom installer INSTALL_ROOT="+installdir + " IFW_PATH="+ifdir):
sys.exit("Make Failed...")
installer = glob.glob(os.path.join(builddir, "canmv-ide-*.exe"))[0]
elif sys.platform.startswith('darwin'):
if os.system("cd " + builddir +
" && qmake ../qt-creator/qtcreator.pro -r -spec macx-clang CONFIG+=x86_64 OMV_IDE_GIT_VERSION=\"" + git_version + "\"" +
" && make -j" + str(cpus) +
" && make deployqt"):
sys.exit("Make Failed...")
os.system("cd " + builddir + " && make codesign SIGNING_IDENTITY=Application SIGNING_FLAGS=\"--force --options=runtime --timestamp\"")
if os.system("cd " + builddir +
" && ditto -c -k -rsrc --sequesterRsrc --keepParent bin/CanMV\\ IDE.app CanMV\\ IDE.zip"
" && xcrun notarytool submit CanMV\\ IDE.zip --keychain-profile \"AC_PASSWORD\" --wait"
" && xcrun stapler staple bin/CanMV\\ IDE.app"):
sys.exit("Make Failed...")
if os.system("cd " + builddir + " && make dmg"):
sys.exit("Make Failed...")
installer = glob.glob(os.path.join(builddir, "canmv-ide-*.dmg"))[0]
if os.system("cd " + builddir + " && xcrun notarytool submit " + installer + " --keychain-profile \"AC_PASSWORD\" --wait"
" && xcrun stapler staple " + installer):
sys.exit("Make Failed...")
elif sys.platform.startswith('linux'):
# Add Fonts...
if os.path.exists(os.path.join(installdir, "lib/Qt/lib/fonts")):
shutil.rmtree(os.path.join(installdir, "lib/Qt/lib/fonts"), ignore_errors = True)
shutil.copytree(os.path.join(__folder__, "dejavu-fonts/fonts/"),
os.path.join(installdir, "lib/Qt/lib/fonts"))
# Add README.txt...
with open(os.path.join(installdir, "README.txt"), 'w') as f:
f.write("Please run setup.sh to install CanMV IDE dependencies... e.g.\n\n")
f.write("./setup.sh\n\n")
f.write("./bin/canmvide.sh\n\n")
# Add setup.sh...
with open(os.path.join(installdir, "setup.sh"), 'w') as f:
f.write("#! /bin/sh\n\n")
f.write("sudo apt-get install -y libpng* libusb-1.0 python-pip\n")
f.write("sudo pip install pyusb\n\n")
f.write("sudo cp $( dirname \"$0\" )/share/qtcreator/pydfu/50-openmv.rules /etc/udev/rules.d/50-openmv.rules\n")
f.write("sudo udevadm control --reload-rules\n\n")
os.chmod(os.path.join(installdir, "setup.sh"),
os.stat(os.path.join(installdir, "setup.sh")).st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
# Build...
if os.system("cd " + builddir +
" && qmake ../qt-creator/qtcreator.pro -r -spec linux-g++ OMV_IDE_GIT_VERSION=\"" + git_version + "\"" +
" && make -r -w -j" + str(cpus) +
" && make installer INSTALL_ROOT="+installdir + " IFW_PATH="+str(ifdir)):
sys.exit("Make Failed...")
installer = glob.glob(os.path.join(builddir, "canmv-ide-*.run"))[0]
else:
sys.exit("Unknown Platform")
###########################################################################
if args.upload:
remotedir = os.path.splitext(os.path.basename(installer))[0]
if args.rpi: # Remove .tar
remotedir = os.path.splitext(remotedir)[0]
uploaddir = os.path.join(builddir, remotedir)
if not os.path.exists(uploaddir):
os.mkdir(uploaddir)
shutil.copy2(installer, uploaddir)
subprocess.check_call(["python", "ftpsync.py", "-u", "-l",
"ftp://upload@openmv.io:"+args.upload+"@ftp.openmv.io/"+remotedir,
uploaddir])
if __name__ == "__main__":
make()