Unverified Commit 76bb3b26 authored by Artur Wojcik's avatar Artur Wojcik Committed by GitHub
Browse files

python: add 'make generate' OS agnostic (#2288)

parent df5f8c96
...@@ -22,4 +22,16 @@ ...@@ -22,4 +22,16 @@
# THE SOFTWARE. # THE SOFTWARE.
##################################################################################### #####################################################################################
add_custom_target(generate bash ${CMAKE_CURRENT_SOURCE_DIR}/generate.sh) find_package(Python 3 COMPONENTS Interpreter)
if(NOT Python_EXECUTABLE)
message(WARNING "Python 3 interpreter not found - skipping 'generate' target!")
return()
endif()
find_program(CLANG_FORMAT clang-format PATHS /opt/rocm/llvm $ENV{HIP_PATH})
if(NOT CLANG_FORMAT)
message(WARNING "clang-format not found - skipping 'generate' target!")
return()
endif()
add_custom_target(generate ${Python_EXECUTABLE} generate.py -f ${CLANG_FORMAT} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
...@@ -27,6 +27,7 @@ import re ...@@ -27,6 +27,7 @@ import re
import runpy import runpy
from functools import wraps from functools import wraps
from typing import Any, Callable, Dict, List, Optional, Tuple, Union from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from pathlib import Path
type_map: Dict[str, Callable[['Parameter'], None]] = {} type_map: Dict[str, Callable[['Parameter'], None]] = {}
cpp_type_map: Dict[str, str] = {} cpp_type_map: Dict[str, str] = {}
...@@ -1281,18 +1282,17 @@ def template_eval(template, **kwargs): ...@@ -1281,18 +1282,17 @@ def template_eval(template, **kwargs):
return template return template
def run(args: List[str]) -> None: def run(path: Union[Path, str]) -> str:
runpy.run_path(args[0]) return template_eval(open(path).read())
if len(args) > 1:
f = open(args[1]).read()
r = template_eval(f) if __name__ == "__main__":
sys.modules['api'] = sys.modules['__main__']
runpy.run_path(sys.argv[1])
if len(sys.argv) > 2:
r = run(sys.argv[2])
sys.stdout.write(r) sys.stdout.write(r)
else: else:
sys.stdout.write(generate_c_header()) sys.stdout.write(generate_c_header())
sys.stdout.write(generate_c_api_body()) sys.stdout.write(generate_c_api_body())
# sys.stdout.write(generate_cpp_header()) # sys.stdout.write(generate_cpp_header())
if __name__ == "__main__":
sys.modules['api'] = sys.modules['__main__']
run(sys.argv[1:])
##################################################################################### #####################################################################################
# The MIT License (MIT) # The MIT License (MIT)
# #
# Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved. # Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved.
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy # Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal # of this software and associated documentation files (the "Software"), to deal
...@@ -21,23 +21,68 @@ ...@@ -21,23 +21,68 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE. # THE SOFTWARE.
##################################################################################### #####################################################################################
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" import api, argparse, os, runpy, subprocess, sys, te
CLANG_FORMAT=/opt/rocm/llvm/bin/clang-format from pathlib import Path
SRC_DIR=$DIR/../src
PYTHON=python3 clang_format_path = Path('clang-format.exe' if os.name ==
if type -p python3.6 > /dev/null ; then 'nt' else '/opt/rocm/llvm/bin/clang-format')
PYTHON=python3.6 work_dir = Path().cwd()
fi src_dir = (work_dir / '../src').absolute()
if type -p python3.8 > /dev/null ; then migraphx_py_path = src_dir / 'api/migraphx.py'
PYTHON=python3.8
fi
ls -1 $DIR/include/ | xargs -n 1 -P $(nproc) -I{} -t bash -c "$PYTHON $DIR/te.py $DIR/include/{} | $CLANG_FORMAT -style=file > $SRC_DIR/include/migraphx/{}" def clang_format(buffer, **kwargs):
return subprocess.run(f'{clang_format_path} -style=file',
function api { capture_output=True,
$PYTHON $DIR/api.py $SRC_DIR/api/migraphx.py $1 | $CLANG_FORMAT -style=file > $2 shell=True,
} check=True,
input=buffer.encode('utf-8'),
api $DIR/api/migraphx.h $SRC_DIR/api/include/migraphx/migraphx.h cwd=work_dir,
echo "Finished generating header migraphx.h" **kwargs).stdout.decode('utf-8')
api $DIR/api/api.cpp $SRC_DIR/api/api.cpp
echo "Finished generating source api.cpp "
def api_generate(input_path: Path, output_path: Path):
with open(output_path, 'w') as f:
f.write(clang_format(api.run(input_path)))
def te_generate(input_path: Path, output_path: Path):
with open(output_path, 'w') as f:
f.write(clang_format(te.run(input_path)))
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--clang-format', type=Path)
args = parser.parse_args()
global clang_format_path
if args.clang_format:
clang_format_path = args.clang_format
if not clang_format_path.is_file():
print(f"{clang_format_path}: invalid path or not installed",
file=sys.stderr)
return
try:
files = Path('include').absolute().iterdir()
for f in [f for f in files if f.is_file()]:
te_generate(f, src_dir / f'include/migraphx/{f.name}')
runpy.run_path(str(migraphx_py_path))
api_generate(work_dir / 'api/migraphx.h',
src_dir / 'api/include/migraphx/migraphx.h')
print('Finished generating header migraphx.h')
api_generate(work_dir / 'api/api.cpp', src_dir / 'api/api.cpp')
print('Finished generating source api.cpp')
except subprocess.CalledProcessError as ex:
if ex.stdout:
print(ex.stdout.decode('utf-8'))
if ex.stderr:
print(ex.stdout.decode('utf-8'))
print(f"Command '{ex.cmd}' returned {ex.returncode}")
raise
if __name__ == "__main__":
main()
...@@ -431,6 +431,9 @@ def template_eval(template, **kwargs): ...@@ -431,6 +431,9 @@ def template_eval(template, **kwargs):
return template return template
f = open(sys.argv[1]).read() def run(p):
r = template_eval(f) return template_eval(open(p).read())
sys.stdout.write(r)
if __name__ == '__main__':
sys.stdout.write(run(sys.argv[1]))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment