|
- -- This file should be run using xmake, because it uses xmake's extended interface for string processing.
- -- 这个文件应当使用 xmake 进行运行,因为这其中使用了 xmake 针对字符串处理的拓展接口
-
- -- configs
- local c_source_file_path = "src/ncnn_mp.c"
- local output_file_path = "temp"
- local module_prefix = "ncnn_mp"
-
- local file = io.open(c_source_file_path, "r")
- if not file then
- print(string.format("Error: Could not open source file at '%s'", c_source_file_path))
- return
- end
- local content = file:read("*a")
- file:close()
-
- local functions = {}
-
- local pattern = "static MP_DEFINE_CONST_FUN_OBJ_[%w_]+%((.-),.-%);"
-
- for c_object_name in string.gmatch(content, pattern) do
- c_object_name = c_object_name:trim()
- local python_name = c_object_name:gsub("^" .. module_prefix .. "_", ""):gsub("_obj$", "")
-
- table.insert(functions, {py_name = python_name, c_obj = c_object_name})
- end
-
- print(string.format("Found %d function objects. Writing to '%s'...", #functions, output_file_path))
-
- local outfile = io.open(output_file_path, "w")
- if not outfile then
- print(string.format("Error: Could not open output file at '%s'", output_file_path))
- return
- end
-
- outfile:write("// --- Generated by generate_globals.lua ---\n")
- outfile:write("// Please review and copy this into your ncnn_mp.c file.\n\n")
- outfile:write("static const mp_rom_map_elem_t ncnn_mp_module_globals_table[] = {\n")
- outfile:write(" { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_" .. module_prefix .. ") },\n\n")
-
- for _, func in ipairs(functions) do
- local line = string.format(" { MP_ROM_QSTR(MP_QSTR_%s), MP_ROM_PTR(&%s) },\n", func.py_name, func.c_obj)
- outfile:write(line)
- end
-
- outfile:write("\n // --- Manually add constants below ---\n")
- outfile:write(" // { MP_ROM_QSTR(MP_QSTR_MAT_PIXEL_BGR), MP_ROM_INT(2) },\n")
- outfile:write(" // ...\n")
- outfile:write("};\n")
- outfile:write("static MP_DEFINE_CONST_DICT(ncnn_mp_module_globals, ncnn_mp_module_globals_table);\n")
-
- outfile:close()
-
- print("Done.")
|