diff --git a/modules/js/src/helpers.js b/modules/js/src/helpers.js index 9f1934c279..962a0b4d90 100644 --- a/modules/js/src/helpers.js +++ b/modules/js/src/helpers.js @@ -42,6 +42,10 @@ if (typeof Module.FS === 'undefined' && typeof FS !== 'undefined') { Module.FS = FS; } +if (typeof cv === 'undefined') { + var cv = Module; +} + Module['imread'] = function(imageSource) { var img = null; if (typeof imageSource === 'string') { diff --git a/platforms/js/build_js.py b/platforms/js/build_js.py index 07fda10d35..f0701212af 100644 --- a/platforms/js/build_js.py +++ b/platforms/js/build_js.py @@ -84,7 +84,6 @@ class Builder: "-DPYTHON_DEFAULT_EXECUTABLE=%s" % sys.executable, "-DENABLE_PIC=FALSE", # To workaround emscripten upstream backend issue https://github.com/emscripten-core/emscripten/issues/8761 "-DCMAKE_BUILD_TYPE=Release", - "-DCMAKE_TOOLCHAIN_FILE='%s'" % self.get_toolchain_file(), "-DCPU_BASELINE=''", "-DCMAKE_INSTALL_PREFIX=/usr/local", "-DCPU_DISPATCH=''", @@ -145,6 +144,8 @@ class Builder: "-DBUILD_PERF_TESTS=ON"] if self.options.cmake_option: cmd += self.options.cmake_option + if not self.options.cmake_option or all(["-DCMAKE_TOOLCHAIN_FILE" not in opt for opt in self.options.cmake_option]): + cmd.append("-DCMAKE_TOOLCHAIN_FILE='%s'" % self.get_toolchain_file()) if self.options.build_doc: cmd.append("-DBUILD_DOCS=ON") else: @@ -194,6 +195,7 @@ class Builder: flags += self.options.build_flags if self.options.webnn: flags += "-s USE_WEBNN=1 " + flags += "-s EXPORTED_FUNCTIONS=\"['_malloc', '_free']\"" return flags def config(self): @@ -224,10 +226,12 @@ if __name__ == "__main__": opencv_dir = os.path.abspath(os.path.join(SCRIPT_DIR, '../..')) emscripten_dir = None - if "EMSCRIPTEN" in os.environ: + if "EMSDK" in os.environ: + emscripten_dir = os.path.join(os.environ["EMSDK"], "upstream", "emscripten") + elif "EMSCRIPTEN" in os.environ: emscripten_dir = os.environ["EMSCRIPTEN"] else: - log.warning("EMSCRIPTEN environment variable is not available. Please properly activate Emscripten SDK and consider using 'emcmake' launcher") + log.warning("EMSCRIPTEN/EMSDK environment variable is not available. Please properly activate Emscripten SDK and consider using 'emcmake' launcher") parser = argparse.ArgumentParser(description='Build OpenCV.js by Emscripten') parser.add_argument("build_dir", help="Building directory (and output)") @@ -256,7 +260,8 @@ if __name__ == "__main__": help="Specify configuration file with own list of exported into JS functions") parser.add_argument('--webnn', action="store_true", help="Enable WebNN Backend") - args = parser.parse_args() + transformed_args = ["--cmake_option=%s".format(arg) if arg[:2] == "-D" else arg for arg in sys.argv[1:]] + args = parser.parse_args(transformed_args) log.debug("Args: %s", args) @@ -266,7 +271,7 @@ if __name__ == "__main__": del os.environ['EMMAKEN_JUST_CONFIGURE'] # avoid linker errors with NODERAWFS message then using 'emcmake' launcher if args.emscripten_dir is None: - log.error("Cannot get Emscripten path, please use 'emcmake' launcher or specify it either by EMSCRIPTEN environment variable or --emscripten_dir option.") + log.error("Cannot get Emscripten path, please use 'emcmake' launcher or specify it either by EMSCRIPTEN/EMSDK environment variable or --emscripten_dir option.") sys.exit(-1) builder = Builder(args)