Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/platforms/js/build_js.py
16337 views
1
#!/usr/bin/env python
2
3
import os, sys, subprocess, argparse, shutil, glob, re, multiprocessing
4
import logging as log
5
6
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
7
8
class Fail(Exception):
9
def __init__(self, text=None):
10
self.t = text
11
def __str__(self):
12
return "ERROR" if self.t is None else self.t
13
14
def execute(cmd, shell=False):
15
try:
16
log.info("Executing: %s" % cmd)
17
env = os.environ.copy()
18
env['VERBOSE'] = '1'
19
retcode = subprocess.call(cmd, shell=shell, env=env)
20
if retcode < 0:
21
raise Fail("Child was terminated by signal: %s" % -retcode)
22
elif retcode > 0:
23
raise Fail("Child returned: %s" % retcode)
24
except OSError as e:
25
raise Fail("Execution failed: %d / %s" % (e.errno, e.strerror))
26
27
def rm_one(d):
28
d = os.path.abspath(d)
29
if os.path.exists(d):
30
if os.path.isdir(d):
31
log.info("Removing dir: %s", d)
32
shutil.rmtree(d)
33
elif os.path.isfile(d):
34
log.info("Removing file: %s", d)
35
os.remove(d)
36
37
def check_dir(d, create=False, clean=False):
38
d = os.path.abspath(d)
39
log.info("Check dir %s (create: %s, clean: %s)", d, create, clean)
40
if os.path.exists(d):
41
if not os.path.isdir(d):
42
raise Fail("Not a directory: %s" % d)
43
if clean:
44
for x in glob.glob(os.path.join(d, "*")):
45
rm_one(x)
46
else:
47
if create:
48
os.makedirs(d)
49
return d
50
51
def check_file(d):
52
d = os.path.abspath(d)
53
if os.path.exists(d):
54
if os.path.isfile(d):
55
return True
56
else:
57
return False
58
return False
59
60
def find_file(name, path):
61
for root, dirs, files in os.walk(path):
62
if name in files:
63
return os.path.join(root, name)
64
65
class Builder:
66
def __init__(self, options):
67
self.options = options
68
self.build_dir = check_dir(options.build_dir, create=True)
69
self.opencv_dir = check_dir(options.opencv_dir)
70
self.emscripten_dir = check_dir(options.emscripten_dir)
71
72
def get_toolchain_file(self):
73
return os.path.join(self.emscripten_dir, "cmake", "Modules", "Platform", "Emscripten.cmake")
74
75
def clean_build_dir(self):
76
for d in ["CMakeCache.txt", "CMakeFiles/", "bin/", "libs/", "lib/", "modules"]:
77
rm_one(d)
78
79
def get_cmake_cmd(self):
80
cmd = ["cmake",
81
"-DCMAKE_BUILD_TYPE=Release",
82
"-DCMAKE_TOOLCHAIN_FILE='%s'" % self.get_toolchain_file(),
83
"-DCPU_BASELINE=''",
84
"-DCPU_DISPATCH=''",
85
"-DCV_TRACE=OFF",
86
"-DBUILD_SHARED_LIBS=OFF",
87
"-DWITH_1394=OFF",
88
"-DWITH_ADE=OFF",
89
"-DWITH_VTK=OFF",
90
"-DWITH_EIGEN=OFF",
91
"-DWITH_FFMPEG=OFF",
92
"-DWITH_GSTREAMER=OFF",
93
"-DWITH_GTK=OFF",
94
"-DWITH_GTK_2_X=OFF",
95
"-DWITH_IPP=OFF",
96
"-DWITH_JASPER=OFF",
97
"-DWITH_JPEG=OFF",
98
"-DWITH_WEBP=OFF",
99
"-DWITH_OPENEXR=OFF",
100
"-DWITH_OPENGL=OFF",
101
"-DWITH_OPENVX=OFF",
102
"-DWITH_OPENNI=OFF",
103
"-DWITH_OPENNI2=OFF",
104
"-DWITH_PNG=OFF",
105
"-DWITH_TBB=OFF",
106
"-DWITH_PTHREADS_PF=OFF",
107
"-DWITH_TIFF=OFF",
108
"-DWITH_V4L=OFF",
109
"-DWITH_OPENCL=OFF",
110
"-DWITH_OPENCL_SVM=OFF",
111
"-DWITH_OPENCLAMDFFT=OFF",
112
"-DWITH_OPENCLAMDBLAS=OFF",
113
"-DWITH_GPHOTO2=OFF",
114
"-DWITH_LAPACK=OFF",
115
"-DWITH_ITT=OFF",
116
"-DBUILD_ZLIB=ON",
117
"-DBUILD_opencv_apps=OFF",
118
"-DBUILD_opencv_calib3d=ON", # No bindings provided. This module is used as a dependency for other modules.
119
"-DBUILD_opencv_dnn=ON",
120
"-DBUILD_opencv_features2d=ON",
121
"-DBUILD_opencv_flann=ON", # No bindings provided. This module is used as a dependency for other modules.
122
"-DBUILD_opencv_gapi=OFF",
123
"-DBUILD_opencv_ml=OFF",
124
"-DBUILD_opencv_photo=OFF",
125
"-DBUILD_opencv_imgcodecs=OFF",
126
"-DBUILD_opencv_shape=OFF",
127
"-DBUILD_opencv_videoio=OFF",
128
"-DBUILD_opencv_videostab=OFF",
129
"-DBUILD_opencv_highgui=OFF",
130
"-DBUILD_opencv_superres=OFF",
131
"-DBUILD_opencv_stitching=OFF",
132
"-DBUILD_opencv_java=OFF",
133
"-DBUILD_opencv_js=ON",
134
"-DBUILD_opencv_python2=OFF",
135
"-DBUILD_opencv_python3=OFF",
136
"-DBUILD_EXAMPLES=OFF",
137
"-DBUILD_PACKAGE=OFF",
138
"-DBUILD_TESTS=OFF",
139
"-DBUILD_PERF_TESTS=OFF"]
140
if self.options.build_doc:
141
cmd.append("-DBUILD_DOCS=ON")
142
else:
143
cmd.append("-DBUILD_DOCS=OFF")
144
145
flags = self.get_build_flags()
146
if flags:
147
cmd += ["-DCMAKE_C_FLAGS='%s'" % flags,
148
"-DCMAKE_CXX_FLAGS='%s'" % flags]
149
return cmd
150
151
def get_build_flags(self):
152
flags = ""
153
if self.options.build_wasm:
154
flags += "-s WASM=1 "
155
elif self.options.disable_wasm:
156
flags += "-s WASM=0 "
157
if self.options.enable_exception:
158
flags += "-s DISABLE_EXCEPTION_CATCHING=0 "
159
return flags
160
161
def config(self):
162
cmd = self.get_cmake_cmd()
163
cmd.append(self.opencv_dir)
164
execute(cmd)
165
166
def build_opencvjs(self):
167
execute(["make", "-j", str(multiprocessing.cpu_count()), "opencv.js"])
168
169
def build_test(self):
170
execute(["make", "-j", str(multiprocessing.cpu_count()), "opencv_js_test"])
171
172
def build_doc(self):
173
execute(["make", "-j", str(multiprocessing.cpu_count()), "doxygen"])
174
175
176
#===================================================================================================
177
178
if __name__ == "__main__":
179
opencv_dir = os.path.abspath(os.path.join(SCRIPT_DIR, '../..'))
180
emscripten_dir = None
181
if "EMSCRIPTEN" in os.environ:
182
emscripten_dir = os.environ["EMSCRIPTEN"]
183
184
parser = argparse.ArgumentParser(description='Build OpenCV.js by Emscripten')
185
parser.add_argument("build_dir", help="Building directory (and output)")
186
parser.add_argument('--opencv_dir', default=opencv_dir, help='Opencv source directory (default is "../.." relative to script location)')
187
parser.add_argument('--emscripten_dir', default=emscripten_dir, help="Path to Emscripten to use for build")
188
parser.add_argument('--build_wasm', action="store_true", help="Build OpenCV.js in WebAssembly format")
189
parser.add_argument('--disable_wasm', action="store_true", help="Build OpenCV.js in Asm.js format")
190
parser.add_argument('--build_test', action="store_true", help="Build tests")
191
parser.add_argument('--build_doc', action="store_true", help="Build tutorials")
192
parser.add_argument('--clean_build_dir', action="store_true", help="Clean build dir")
193
parser.add_argument('--skip_config', action="store_true", help="Skip cmake config")
194
parser.add_argument('--config_only', action="store_true", help="Only do cmake config")
195
parser.add_argument('--enable_exception', action="store_true", help="Enable exception handling")
196
args = parser.parse_args()
197
198
log.basicConfig(format='%(message)s', level=log.DEBUG)
199
log.debug("Args: %s", args)
200
201
if args.emscripten_dir is None:
202
log.info("Cannot get Emscripten path, please specify it either by EMSCRIPTEN environment variable or --emscripten_dir option.")
203
sys.exit(-1)
204
205
builder = Builder(args)
206
207
os.chdir(builder.build_dir)
208
209
if args.clean_build_dir:
210
log.info("=====")
211
log.info("===== Clean build dir %s", builder.build_dir)
212
log.info("=====")
213
builder.clean_build_dir()
214
215
if not args.skip_config:
216
target = "default target"
217
if args.build_wasm:
218
target = "wasm"
219
elif args.disable_wasm:
220
target = "asm.js"
221
log.info("=====")
222
log.info("===== Config OpenCV.js build for %s" % target)
223
log.info("=====")
224
builder.config()
225
226
if args.config_only:
227
sys.exit(0)
228
229
log.info("=====")
230
log.info("===== Building OpenCV.js")
231
log.info("=====")
232
builder.build_opencvjs()
233
234
if args.build_test:
235
log.info("=====")
236
log.info("===== Building OpenCV.js tests")
237
log.info("=====")
238
builder.build_test()
239
240
if args.build_doc:
241
log.info("=====")
242
log.info("===== Building OpenCV.js tutorials")
243
log.info("=====")
244
builder.build_doc()
245
246
247
log.info("=====")
248
log.info("===== Build finished")
249
log.info("=====")
250
251
opencvjs_path = os.path.join(builder.build_dir, "bin", "opencv.js")
252
if check_file(opencvjs_path):
253
log.info("OpenCV.js location: %s", opencvjs_path)
254
255
if args.build_test:
256
opencvjs_test_path = os.path.join(builder.build_dir, "bin", "tests.html")
257
if check_file(opencvjs_test_path):
258
log.info("OpenCV.js tests location: %s", opencvjs_test_path)
259
260
if args.build_doc:
261
opencvjs_tutorial_path = find_file("tutorial_js_root.html", os.path.join(builder.build_dir, "doc", "doxygen", "html"))
262
if check_file(opencvjs_tutorial_path):
263
log.info("OpenCV.js tutorials location: %s", opencvjs_tutorial_path)
264
265