mirror of
https://github.com/opencv/opencv.git
synced 2026-07-29 07:13:02 +04:00
feat: add conditional inclusion support to header parser
This commit is contained in:
+51
-11
@@ -2,6 +2,7 @@
|
||||
|
||||
from __future__ import print_function
|
||||
import hdr_parser, sys, re
|
||||
import json
|
||||
from string import Template
|
||||
from collections import namedtuple
|
||||
from itertools import chain
|
||||
@@ -1108,6 +1109,18 @@ class Namespace(object):
|
||||
|
||||
|
||||
class PythonWrapperGenerator(object):
|
||||
class Config:
|
||||
def __init__(self, headers, preprocessor_definitions = None):
|
||||
self.headers = headers
|
||||
if preprocessor_definitions is None:
|
||||
preprocessor_definitions = {}
|
||||
elif not isinstance(preprocessor_definitions, dict):
|
||||
raise TypeError(
|
||||
"preprocessor_definitions should rather dictionary or None. "
|
||||
"Got: {}".format(type(preprocessor_definitions).__name__)
|
||||
)
|
||||
self.preprocessor_definitions = preprocessor_definitions
|
||||
|
||||
def __init__(self):
|
||||
self.clear()
|
||||
|
||||
@@ -1324,13 +1337,16 @@ class PythonWrapperGenerator(object):
|
||||
f.write(buf.getvalue())
|
||||
|
||||
def save_json(self, path, name, value):
|
||||
import json
|
||||
with open(path + "/" + name, "wt") as f:
|
||||
json.dump(value, f)
|
||||
|
||||
def gen(self, srcfiles, output_path):
|
||||
def gen(self, srcfiles, output_path, preprocessor_definitions = None):
|
||||
self.clear()
|
||||
self.parser = hdr_parser.CppHeaderParser(generate_umat_decls=True, generate_gpumat_decls=True)
|
||||
self.parser = hdr_parser.CppHeaderParser(
|
||||
generate_umat_decls=True,
|
||||
generate_gpumat_decls=True,
|
||||
preprocessor_definitions=preprocessor_definitions
|
||||
)
|
||||
|
||||
|
||||
# step 1: scan the headers and build more descriptive maps of classes, consts, functions
|
||||
@@ -1502,12 +1518,36 @@ class PythonWrapperGenerator(object):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
srcfiles = hdr_parser.opencv_hdr_list
|
||||
dstdir = "/Users/vp/tmp"
|
||||
if len(sys.argv) > 1:
|
||||
dstdir = sys.argv[1]
|
||||
if len(sys.argv) > 2:
|
||||
with open(sys.argv[2], 'r') as f:
|
||||
srcfiles = [l.strip() for l in f.readlines()]
|
||||
import argparse
|
||||
import tempfile
|
||||
|
||||
arg_parser = argparse.ArgumentParser(
|
||||
description="OpenCV Python bindings generator"
|
||||
)
|
||||
arg_parser.add_argument(
|
||||
"-c", "--config",
|
||||
dest="config_json_path",
|
||||
required=False,
|
||||
help="Generator configuration file in .json format"
|
||||
"Refer to PythonWrapperGenerator.Config for available "
|
||||
"configuration keys"
|
||||
)
|
||||
arg_parser.add_argument(
|
||||
"-o", "--output_dir",
|
||||
dest="output_dir",
|
||||
default=tempfile.gettempdir(),
|
||||
help="Generated bindings output directory"
|
||||
)
|
||||
args = arg_parser.parse_args()
|
||||
if args.config_json_path is not None:
|
||||
with open(args.config_json_path, "r") as fh:
|
||||
config_json = json.load(fh)
|
||||
config = PythonWrapperGenerator.Config(**config_json)
|
||||
else:
|
||||
config = PythonWrapperGenerator.Config(
|
||||
headers=hdr_parser.opencv_hdr_list
|
||||
)
|
||||
|
||||
generator = PythonWrapperGenerator()
|
||||
generator.gen(srcfiles, dstdir)
|
||||
|
||||
generator.gen(config.headers, args.output_dir, config.preprocessor_definitions)
|
||||
|
||||
Reference in New Issue
Block a user