1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 07:43:03 +04:00

feat: add conditional inclusion support to header parser

This commit is contained in:
Vadim Levin
2025-05-16 12:26:10 +03:00
parent 1c421be489
commit b3e17ea9d4
10 changed files with 466 additions and 64 deletions
+40 -6
View File
@@ -30,7 +30,14 @@ ocv_list_filterout(opencv_hdrs "modules/core/include/opencv2/core/utils/*.privat
ocv_list_filterout(opencv_hdrs "modules/core/include/opencv2/core/utils/instrumentation.hpp")
ocv_list_filterout(opencv_hdrs "modules/core/include/opencv2/core/utils/trace*")
ocv_update_file("${CMAKE_CURRENT_BINARY_DIR}/headers.txt" "${opencv_hdrs}")
set(config_json_headers_list "")
foreach(header IN LISTS opencv_hdrs)
if(NOT config_json_headers_list STREQUAL "")
set(config_json_headers_list "${config_json_headers_list},\n\"${header}\"")
else()
set(config_json_headers_list "\"${header}\"")
endif()
endforeach()
set(bindings_cpp "${OPENCV_JS_BINDINGS_DIR}/gen/bindings.cpp")
@@ -55,16 +62,42 @@ else()
message(STATUS "Use autogenerated whitelist ${OPENCV_JS_WHITELIST_FILE}")
endif()
include("${OpenCV_SOURCE_DIR}/cmake/OpenCVBindingsPreprocessorDefinitions.cmake")
ocv_bindings_generator_populate_preprocessor_definitions(
OPENCV_MODULES_BUILD
opencv_preprocessor_defs
)
set(__config_str
"{
\"headers\": [
${config_json_headers_list}
],
\"preprocessor_definitions\": {
${opencv_preprocessor_defs}
},
\"core_bindings_file_path\": \"${JS_SOURCE_DIR}/src/core_bindings.cpp\"
}")
set(JSON_CONFIG_FILE_PATH "${CMAKE_CURRENT_BINARY_DIR}/gen_js_config.json")
if(EXISTS "${JSON_CONFIG_FILE_PATH}")
file(READ "${JSON_CONFIG_FILE_PATH}" __content)
else()
set(__content "")
endif()
if(NOT "${__content}" STREQUAL "${__config_str}")
file(WRITE "${JSON_CONFIG_FILE_PATH}" "${__config_str}")
endif()
unset(__config_str)
add_custom_command(
OUTPUT ${bindings_cpp} "${OPENCV_DEPHELPER}/gen_opencv_js_source"
COMMAND
${PYTHON_DEFAULT_EXECUTABLE}
"${CMAKE_CURRENT_SOURCE_DIR}/embindgen.py"
"${scripts_hdr_parser}"
"${bindings_cpp}"
"${CMAKE_CURRENT_BINARY_DIR}/headers.txt"
"${JS_SOURCE_DIR}/src/core_bindings.cpp"
"${OPENCV_JS_WHITELIST_FILE}"
--parser "${scripts_hdr_parser}"
--output_file "${bindings_cpp}"
--config "${JSON_CONFIG_FILE_PATH}"
--whitelist "${OPENCV_JS_WHITELIST_FILE}"
COMMAND
${CMAKE_COMMAND} -E touch "${OPENCV_DEPHELPER}/gen_opencv_js_source"
WORKING_DIRECTORY
@@ -73,6 +106,7 @@ add_custom_command(
${JS_SOURCE_DIR}/src/core_bindings.cpp
${CMAKE_CURRENT_SOURCE_DIR}/embindgen.py
${CMAKE_CURRENT_SOURCE_DIR}/templates.py
${JSON_CONFIG_FILE_PATH}
"${OPENCV_JS_WHITELIST_FILE}"
${scripts_hdr_parser}
#(not needed - generated by CMake) ${CMAKE_CURRENT_BINARY_DIR}/headers.txt
+53 -23
View File
@@ -319,7 +319,7 @@ class Namespace(object):
class JSWrapperGenerator(object):
def __init__(self):
def __init__(self, preprocessor_definitions=None):
self.bindings = []
self.wrapper_funcs = []
@@ -328,7 +328,9 @@ class JSWrapperGenerator(object):
self.namespaces = {}
self.enums = {} # FIXIT 'enums' should belong to 'namespaces'
self.parser = hdr_parser.CppHeaderParser()
self.parser = hdr_parser.CppHeaderParser(
preprocessor_definitions=preprocessor_definitions
)
self.class_idx = 0
def add_class(self, stype, name, decl):
@@ -962,41 +964,69 @@ class JSWrapperGenerator(object):
if __name__ == "__main__":
if len(sys.argv) < 5:
print("Usage:\n", \
os.path.basename(sys.argv[0]), \
"<full path to hdr_parser.py> <bindings.cpp> <headers.txt> <core_bindings.cpp> <whitelist.json or opencv_js.config.py>")
print("Current args are: ", ", ".join(["'"+a+"'" for a in sys.argv]))
exit(1)
import argparse
dstdir = "."
hdr_parser_path = os.path.abspath(sys.argv[1])
arg_parser = argparse.ArgumentParser(
description="OpenCV JavaScript bindings generator"
)
arg_parser.add_argument(
"-p", "--parser",
required=True,
help="Full path to OpenCV header parser `hdr_parser.py`"
)
arg_parser.add_argument(
"-o", "--output_file",
dest="output_file_path",
required=True,
help="Path to output file containing js bindings"
)
arg_parser.add_argument(
"-c", "--config",
dest="config_json_path",
required=True,
help="Path to generator configuration file in .json format"
)
arg_parser.add_argument(
"--whitelist",
dest="whitelist_file_path",
required=True,
help="Path to whitelist.js or opencv_js.config.py"
)
args = arg_parser.parse_args()
# import header parser
hdr_parser_path = os.path.abspath(args.parser)
if hdr_parser_path.endswith(".py"):
hdr_parser_path = os.path.dirname(hdr_parser_path)
sys.path.append(hdr_parser_path)
import hdr_parser
bindingsCpp = sys.argv[2]
headers = open(sys.argv[3], 'r').read().split(';')
coreBindings = sys.argv[4]
whiteListFile = sys.argv[5]
with open(args.config_json_path, "r") as fh:
config_json = json.load(fh)
headers = config_json.get("headers", ())
if whiteListFile.endswith(".json") or whiteListFile.endswith(".JSON"):
with open(whiteListFile) as f:
bindings_cpp = args.output_file_path
core_bindings_path = config_json["core_bindings_file_path"]
whitelist_file_path = args.whitelist_file_path
if whitelist_file_path.endswith(".json") or whitelist_file_path.endswith(".JSON"):
with open(whitelist_file_path) as f:
gen_dict = json.load(f)
f.close()
white_list = makeWhiteListJson(gen_dict)
namespace_prefix_override = makeNamespacePrefixOverride(gen_dict)
elif whiteListFile.endswith(".py") or whiteListFile.endswith(".PY"):
exec(open(whiteListFile).read())
assert(white_list)
elif whitelist_file_path.endswith(".py") or whitelist_file_path.endswith(".PY"):
with open(whitelist_file_path) as fh:
exec(fh.read())
assert white_list
namespace_prefix_override = {
'dnn' : '',
'aruco' : '',
}
else:
print("Unexpected format of OpenCV config file", whiteListFile)
print("Unexpected format of OpenCV config file", whitelist_file_path)
exit(1)
generator = JSWrapperGenerator()
generator.gen(bindingsCpp, headers, coreBindings)
generator = JSWrapperGenerator(
preprocessor_definitions=config_json.get("preprocessor_definitions", None)
)
generator.gen(bindings_cpp, headers, core_bindings_path)