Skip to content

Commit 9733177

Browse files
committed
Merge pull request opencv#19105 from alalek:js_build_update
2 parents fa66514 + af71b03 commit 9733177

File tree

5 files changed

+48
-37
lines changed

5 files changed

+48
-37
lines changed

doc/js_tutorials/js_setup/js_intro/js_intro.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ OpenCV.js: OpenCV for the JavaScript programmer
1313

1414
Web is the most ubiquitous open computing platform. With HTML5 standards implemented in every browser, web applications are able to render online video with HTML5 video tags, capture webcam video via WebRTC API, and access each pixel of a video frame via canvas API. With abundance of available multimedia content, web developers are in need of a wide array of image and vision processing algorithms in JavaScript to build innovative applications. This requirement is even more essential for emerging applications on the web, such as Web Virtual Reality (WebVR) and Augmented Reality (WebAR). All of these use cases demand efficient implementations of computation-intensive vision kernels on web.
1515

16-
[Emscripten](http://kripken.github.io/emscripten-site) is an LLVM-to-JavaScript compiler. It takes LLVM bitcode - which can be generated from C/C++ using clang, and compiles that into asm.js or WebAssembly that can execute directly inside the web browsers. . Asm.js is a highly optimizable, low-level subset of JavaScript. Asm.js enables ahead-of-time compilation and optimization in JavaScript engine that provide near-to-native execution speed. WebAssembly is a new portable, size- and load-time-efficient binary format suitable for compilation to the web. WebAssembly aims to execute at native speed. WebAssembly is currently being designed as an open standard by W3C.
16+
[Emscripten](https://emscripten.org/) is an LLVM-to-JavaScript compiler. It takes LLVM bitcode - which can be generated from C/C++ using clang, and compiles that into asm.js or WebAssembly that can execute directly inside the web browsers. . Asm.js is a highly optimizable, low-level subset of JavaScript. Asm.js enables ahead-of-time compilation and optimization in JavaScript engine that provide near-to-native execution speed. WebAssembly is a new portable, size- and load-time-efficient binary format suitable for compilation to the web. WebAssembly aims to execute at native speed. WebAssembly is currently being designed as an open standard by W3C.
1717

1818
OpenCV.js is a JavaScript binding for selected subset of OpenCV functions for the web platform. It allows emerging web applications with multimedia processing to benefit from the wide variety of vision functions available in OpenCV. OpenCV.js leverages Emscripten to compile OpenCV functions into asm.js or WebAssembly targets, and provides a JavaScript APIs for web application to access them. The future versions of the library will take advantage of acceleration APIs that are available on the Web such as SIMD and multi-threaded execution.
1919

@@ -42,4 +42,4 @@ Below is the list of contributors of OpenCV.js bindings and tutorials.
4242
- Gang Song (GSoC student, Shanghai Jiao Tong University)
4343
- Wenyao Gan (Student intern, Shanghai Jiao Tong University)
4444
- Mohammad Reza Haghighat (Project initiator & sponsor, Intel Corporation)
45-
- Ningxin Hu (Students' supervisor, Intel Corporation)
45+
- Ningxin Hu (Students' supervisor, Intel Corporation)

doc/js_tutorials/js_setup/js_setup/js_setup.markdown

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ You don't have to build your own copy if you simply want to start using it. Refe
77
Installing Emscripten
88
-----------------------------
99

10-
[Emscripten](https://github.com/kripken/emscripten) is an LLVM-to-JavaScript compiler. We will use Emscripten to build OpenCV.js.
10+
[Emscripten](https://github.com/emscripten-core/emscripten) is an LLVM-to-JavaScript compiler. We will use Emscripten to build OpenCV.js.
1111

1212
@note
1313
While this describes installation of required tools from scratch, there's a section below also describing an alternative procedure to perform the same build using docker containers which is often easier.
1414

15-
To Install Emscripten, follow instructions of [Emscripten SDK](https://kripken.github.io/emscripten-site/docs/getting_started/downloads.html).
15+
To Install Emscripten, follow instructions of [Emscripten SDK](https://emscripten.org/docs/getting_started/downloads.html).
1616

1717
For example:
1818
@code{.bash}
@@ -21,24 +21,29 @@ For example:
2121
./emsdk activate latest
2222
@endcode
2323

24-
@note
25-
To compile to [WebAssembly](http://webassembly.org), you need to install and activate [Binaryen](https://github.com/WebAssembly/binaryen) with the `emsdk` command. Please refer to [Developer's Guide](http://webassembly.org/getting-started/developers-guide/) for more details.
2624

27-
After install, ensure the `EMSCRIPTEN` environment is setup correctly.
25+
After install, ensure the `EMSDK` environment is setup correctly.
2826

2927
For example:
3028
@code{.bash}
3129
source ./emsdk_env.sh
32-
echo ${EMSCRIPTEN}
30+
echo ${EMSDK}
31+
@endcode
32+
33+
Modern versions of Emscripten requires to use `emcmake` / `emmake` launchers:
34+
35+
@code{.bash}
36+
emcmake sh -c 'echo ${EMSCRIPTEN}'
3337
@endcode
3438

35-
The version 1.39.16 of emscripten is verified for latest WebAssembly. Please check the version of emscripten to use the newest features of WebAssembly.
39+
40+
The version 2.0.10 of emscripten is verified for latest WebAssembly. Please check the version of Emscripten to use the newest features of WebAssembly.
3641

3742
For example:
3843
@code{.bash}
3944
./emsdk update
40-
./emsdk install 1.39.16
41-
./emsdk activate 1.39.16
45+
./emsdk install 2.0.10
46+
./emsdk activate 2.0.10
4247
@endcode
4348

4449
Obtaining OpenCV Source Code
@@ -71,8 +76,7 @@ Building OpenCV.js from Source
7176

7277
For example, to build in `build_js` directory:
7378
@code{.bash}
74-
cd opencv
75-
python ./platforms/js/build_js.py build_js
79+
emcmake python ./opencv/platforms/js/build_js.py build_js
7680
@endcode
7781

7882
@note
@@ -82,14 +86,14 @@ Building OpenCV.js from Source
8286

8387
For example, to build wasm version in `build_wasm` directory:
8488
@code{.bash}
85-
python ./platforms/js/build_js.py build_wasm --build_wasm
89+
emcmake python ./opencv/platforms/js/build_js.py build_wasm --build_wasm
8690
@endcode
8791

8892
-# [Optional] To build the OpenCV.js loader, append `--build_loader`.
8993

9094
For example:
9195
@code{.bash}
92-
python ./platforms/js/build_js.py build_js --build_loader
96+
emcmake python ./opencv/platforms/js/build_js.py build_js --build_loader
9397
@endcode
9498

9599
@note
@@ -114,7 +118,7 @@ Building OpenCV.js from Source
114118

115119
For example:
116120
@code{.bash}
117-
python ./platforms/js/build_js.py build_js --build_doc
121+
emcmake python ./opencv/platforms/js/build_js.py build_js --build_doc
118122
@endcode
119123

120124
@note
@@ -124,7 +128,7 @@ Building OpenCV.js from Source
124128

125129
For example:
126130
@code{.bash}
127-
python ./platforms/js/build_js.py build_js --build_test
131+
emcmake python ./opencv/platforms/js/build_js.py build_js --build_test
128132
@endcode
129133

130134
Running OpenCV.js Tests
@@ -186,7 +190,7 @@ node tests.js
186190

187191
For example:
188192
@code{.bash}
189-
python ./platforms/js/build_js.py build_js --build_wasm --threads
193+
emcmake python ./opencv/platforms/js/build_js.py build_js --build_wasm --threads
190194
@endcode
191195

192196
The default threads number is the logic core number of your device. You can use `cv.parallel_pthreads_set_threads_num(number)` to set threads number by yourself and use `cv.parallel_pthreads_get_threads_num()` to get the current threads number.
@@ -198,7 +202,7 @@ node tests.js
198202

199203
For example:
200204
@code{.bash}
201-
python ./platforms/js/build_js.py build_js --build_wasm --simd
205+
emcmake python ./opencv/platforms/js/build_js.py build_js --build_wasm --simd
202206
@endcode
203207

204208
The simd optimization is experimental as wasm simd is still in development.
@@ -222,7 +226,7 @@ node tests.js
222226

223227
For example:
224228
@code{.bash}
225-
python ./platforms/js/build_js.py build_js --build_wasm --simd --build_wasm_intrin_test
229+
emcmake python ./opencv/platforms/js/build_js.py build_js --build_wasm --simd --build_wasm_intrin_test
226230
@endcode
227231

228232
For wasm intrinsics tests, you can use the following function to test all the cases:
@@ -250,7 +254,7 @@ node tests.js
250254

251255
For example:
252256
@code{.bash}
253-
python ./platforms/js/build_js.py build_js --build_perf
257+
emcmake python ./opencv/platforms/js/build_js.py build_js --build_perf
254258
@endcode
255259

256260
To run performance tests, launch a local web server in \<build_dir\>/bin folder. For example, node http-server which serves on `localhost:8080`.
@@ -271,36 +275,37 @@ Building OpenCV.js with Docker
271275

272276
Alternatively, the same build can be can be accomplished using [docker](https://www.docker.com/) containers which is often easier and more reliable, particularly in non linux systems. You only need to install [docker](https://www.docker.com/) on your system and use a popular container that provides a clean well tested environment for emscripten builds like this, that already has latest versions of all the necessary tools installed.
273277

274-
So, make sure [docker](https://www.docker.com/) is installed in your system and running. The following shell script should work in linux and MacOS:
278+
So, make sure [docker](https://www.docker.com/) is installed in your system and running. The following shell script should work in Linux and MacOS:
275279

276280
@code{.bash}
277281
git clone https://github.com/opencv/opencv.git
278282
cd opencv
279-
docker run --rm --workdir /code -v "$PWD":/code "trzeci/emscripten:latest" python ./platforms/js/build_js.py build
283+
docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) emscripten/emsdk emcmake python3 ./dev/platforms/js/build_js.py build_js
280284
@endcode
281285

282286
In Windows use the following PowerShell command:
283287

284288
@code{.bash}
285-
docker run --rm --workdir /code -v "$(get-location):/code" "trzeci/emscripten:latest" python ./platforms/js/build_js.py build
289+
docker run --rm --workdir /src -v "$(get-location):/src" "emscripten/emsdk" emcmake python3 ./dev/platforms/js/build_js.py build_js
286290
@endcode
287291

288292
@warning
289-
The example uses latest version of emscripten. If the build fails you should try a version that is known to work fine which is `1.38.32` using the following command:
293+
The example uses latest version of emscripten. If the build fails you should try a version that is known to work fine which is `2.0.10` using the following command:
290294

291295
@code{.bash}
292-
docker run --rm --workdir /code -v "$PWD":/code "trzeci/emscripten:sdk-tag-1.38.32-64bit" python ./platforms/js/build_js.py build
296+
docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) emscripten/emsdk:2.0.10 emcmake python3 ./dev/platforms/js/build_js.py build_js
293297
@endcode
294298

295299
### Building the documentation with Docker
296300

297301
To build the documentation `doxygen` needs to be installed. Create a file named `Dockerfile` with the following content:
298302

299303
```
300-
FROM trzeci/emscripten:sdk-tag-1.38.32-64bit
304+
FROM emscripten/emsdk:2.0.10
301305
302-
RUN apt-get update -y
303-
RUN apt-get install -y doxygen
306+
RUN apt-get update \
307+
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends doxygen \
308+
&& rm -rf /var/lib/apt/lists/*
304309
```
305310

306311
Then we build the docker image and name it `opencv-js-doc` with the following command (that needs to be run only once):
@@ -312,5 +317,5 @@ docker build . -t opencv-js-doc
312317
Now run the build command again, this time using the new image and passing `--build_doc`:
313318

314319
@code{.bash}
315-
docker run --rm --workdir /code -v "$PWD":/code "opencv-js-doc" python ./platforms/js/build_js.py build --build_doc
320+
docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) "opencv-js-doc" emcmake python3 ./dev/platforms/js/build_js.py build_js --build_doc
316321
@endcode

doc/js_tutorials/js_setup/js_usage/js_usage.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ function onOpenCvReady() {
129129
</html>
130130
@endcode
131131

132-
@note You have to call delete method of cv.Mat to free memory allocated in Emscripten's heap. Please refer to [Memory management of Emscripten](https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html#memory-management) for details.
132+
@note You have to call delete method of cv.Mat to free memory allocated in Emscripten's heap. Please refer to [Memory management of Emscripten](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html#memory-management) for details.
133133

134134
Try it
135135
------
136136
\htmlonly
137137
<iframe src="../../js_setup_usage.html" width="100%"
138138
onload="this.style.height=this.contentDocument.body.scrollHeight +'px';">
139139
</iframe>
140-
\endhtmlonly
140+
\endhtmlonly

platforms/js/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Building OpenCV.js by Emscripten
22
====================
33

4-
[Download and install Emscripten](https://kripken.github.io/emscripten-site/docs/getting_started/downloads.html).
4+
[Download and install Emscripten](https://emscripten.org/docs/getting_started/downloads.html).
55

66
Execute `build_js.py` script:
77
```
8-
python <opencv_src_dir>/platforms/js/build_js.py <build_dir>
8+
emcmake python <opencv_src_dir>/platforms/js/build_js.py <build_dir>
99
```
1010

1111
If everything is fine, a few minutes later you will get `<build_dir>/bin/opencv.js`. You can add this into your web pages.

platforms/js/build_js.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,19 @@ def build_loader(self):
206206
#===================================================================================================
207207

208208
if __name__ == "__main__":
209+
log.basicConfig(format='%(message)s', level=log.DEBUG)
210+
209211
opencv_dir = os.path.abspath(os.path.join(SCRIPT_DIR, '../..'))
210212
emscripten_dir = None
211213
if "EMSCRIPTEN" in os.environ:
212214
emscripten_dir = os.environ["EMSCRIPTEN"]
215+
else:
216+
log.warning("EMSCRIPTEN environment variable is not available. Please properly activate Emscripten SDK and consider using 'emcmake' launcher")
213217

214218
parser = argparse.ArgumentParser(description='Build OpenCV.js by Emscripten')
215219
parser.add_argument("build_dir", help="Building directory (and output)")
216220
parser.add_argument('--opencv_dir', default=opencv_dir, help='Opencv source directory (default is "../.." relative to script location)')
217-
parser.add_argument('--emscripten_dir', default=emscripten_dir, help="Path to Emscripten to use for build")
221+
parser.add_argument('--emscripten_dir', default=emscripten_dir, help="Path to Emscripten to use for build (deprecated in favor of 'emcmake' launcher)")
218222
parser.add_argument('--build_wasm', action="store_true", help="Build OpenCV.js in WebAssembly format")
219223
parser.add_argument('--disable_wasm', action="store_true", help="Build OpenCV.js in Asm.js format")
220224
parser.add_argument('--threads', action="store_true", help="Build OpenCV.js with threads optimization")
@@ -238,13 +242,15 @@ def build_loader(self):
238242

239243
args = parser.parse_args()
240244

241-
log.basicConfig(format='%(message)s', level=log.DEBUG)
242245
log.debug("Args: %s", args)
243246

244247
os.environ["OPENCV_JS_WHITELIST"] = args.config
245248

249+
if 'EMMAKEN_JUST_CONFIGURE' in os.environ:
250+
del os.environ['EMMAKEN_JUST_CONFIGURE'] # avoid linker errors with NODERAWFS message then using 'emcmake' launcher
251+
246252
if args.emscripten_dir is None:
247-
log.info("Cannot get Emscripten path, please specify it either by EMSCRIPTEN environment variable or --emscripten_dir option.")
253+
log.error("Cannot get Emscripten path, please use 'emcmake' launcher or specify it either by EMSCRIPTEN environment variable or --emscripten_dir option.")
248254
sys.exit(-1)
249255

250256
builder = Builder(args)

0 commit comments

Comments
 (0)