import sys
import os
from pathlib import Path
import subprocess
import logging as log
def shell_exec(cmd, shell=False):
try:
ps = subprocess.Popen(cmd, shell)
ps.communicate(timeout=180)
except BaseException as e:
log.error(f"shell_exec error: {e}")
sys.exit(1)
def search_file(aclnn_cpp):
op_type = None
index = 0
with open(aclnn_cpp, 'r') as f:
for line in f.readlines():
index = index + 1
if "_op_resource.h" in line:
op_type = line.replace("_op_resource.h\"", "").replace("#include \"", "").strip()
if "EXTERN_OP_RESOURCE" in line or "namespace op {" in line:
break
return (op_type, index)
def modify_gen_aclnn(build_path):
auto_gen_cpps = Path(os.path.join(build_path, "autogen")).rglob("aclnn*.cpp")
for aclnn_cpp in auto_gen_cpps:
(op_type, index) = search_file(aclnn_cpp)
if op_type:
shell_exec(["bash", "-c", f"""sed -i 's/{op_type}_op_resource.h/op_resource.h/g' {aclnn_cpp}"""],
shell=False)
shell_exec(
["bash", "-c", f"""sed -i 's/{op_type}_RESOURCES/AUTO_GEN_OP_RESOURCE({op_type})/g' {aclnn_cpp}"""],
shell=False)
shell_exec(["bash", "-c", f"sed -i '{index}i\\EXTERN_OP_RESOURCE({op_type})' {aclnn_cpp}"], shell=False)
return
if __name__ == '__main__':
modify_gen_aclnn(sys.argv[1])