Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
MIGraphX
Commits
60f60093
"...targets/git@developer.sourcefind.cn:gaoqiong/migraphx.git" did not exist on "88951790171a035fa1f87c32787e659c19ae2d59"
Commit
60f60093
authored
Oct 06, 2023
by
Artur Wojcik
Browse files
convert 'make generate' to Python
parent
a7b7ddfb
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
119 additions
and
7 deletions
+119
-7
tools/CMakeLists.txt
tools/CMakeLists.txt
+13
-1
tools/api.py
tools/api.py
+11
-3
tools/generate.py
tools/generate.py
+89
-0
tools/te.py
tools/te.py
+6
-3
No files found.
tools/CMakeLists.txt
View file @
60f60093
...
@@ -22,4 +22,16 @@
...
@@ -22,4 +22,16 @@
# THE SOFTWARE.
# THE SOFTWARE.
#####################################################################################
#####################################################################################
add_custom_target
(
generate
${
CMAKE_COMMAND
}
-P generate.cmake WORKING_DIRECTORY
${
CMAKE_CURRENT_SOURCE_DIR
}
)
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
}
)
tools/api.py
View file @
60f60093
...
@@ -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
]
=
{}
...
@@ -678,6 +679,10 @@ def add_function(name: str, *args, **kwargs) -> Function:
...
@@ -678,6 +679,10 @@ def add_function(name: str, *args, **kwargs) -> Function:
return
f
return
f
def
register_functions
(
path
:
Union
[
Path
,
str
])
->
None
:
runpy
.
run_path
(
path
if
isinstance
(
path
,
str
)
else
str
(
path
))
def
once
(
f
:
Callable
)
->
Any
:
def
once
(
f
:
Callable
)
->
Any
:
@
wraps
(
f
)
@
wraps
(
f
)
def
decorated
(
*
args
,
**
kwargs
):
def
decorated
(
*
args
,
**
kwargs
):
...
@@ -1281,11 +1286,14 @@ def template_eval(template, **kwargs):
...
@@ -1281,11 +1286,14 @@ def template_eval(template, **kwargs):
return
template
return
template
def
invoke
(
path
:
Union
[
Path
,
str
])
->
str
:
return
template_eval
(
open
(
path
).
read
())
def
run
(
args
:
List
[
str
])
->
None
:
def
run
(
args
:
List
[
str
])
->
None
:
r
unpy
.
run_path
(
args
[
0
])
r
egister_functions
(
args
[
0
])
if
len
(
args
)
>
1
:
if
len
(
args
)
>
1
:
f
=
open
(
args
[
1
]).
read
()
r
=
invoke
(
args
[
1
])
r
=
template_eval
(
f
)
sys
.
stdout
.
write
(
r
)
sys
.
stdout
.
write
(
r
)
else
:
else
:
sys
.
stdout
.
write
(
generate_c_header
())
sys
.
stdout
.
write
(
generate_c_header
())
...
...
tools/generate.
cmake
→
tools/generate.
py
View file @
60f60093
#####################################################################################
#####################################################################################
# The MIT License (MIT)
# The MIT License (MIT)
#
#
# Copyright (c) 2015-202
2
Advanced Micro Devices, Inc. All rights reserved.
# Copyright (c) 2015-202
3
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,62 +21,69 @@
...
@@ -21,62 +21,69 @@
# 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.
#####################################################################################
#####################################################################################
import
os
,
sys
,
argparse
,
subprocess
,
te
,
api
from
pathlib
import
Path
find_program
(
CLANG_FORMAT clang-format PATHS /opt/rocm/llvm $ENV{HIP_PATH}
)
clang_format_path
=
Path
(
'clang-format.exe'
if
os
.
name
==
if
(
NOT CLANG_FORMAT
)
'nt'
else
'/opt/rocm/llvm/bin/clang-format'
)
message
(
WARNING
"clang-format not found - skipping 'generate' target!"
)
work_dir
=
Path
().
cwd
(
)
return
()
src_dir
=
(
work_dir
/
'../src'
).
absolute
()
endif
()
migraphx_py_path
=
src_dir
/
'api/migraphx.py'
cmake_path
(
NATIVE_PATH CLANG_FORMAT NORMALIZE __clang_format
)
if
(
WIN32
)
set
(
__clang_format
"C:
${
__clang_format
}
"
)
endif
()
find_package
(
Python 3 COMPONENTS Interpreter
)
def
clang_format
(
buffer
,
**
kwargs
):
if
(
NOT Python_EXECUTABLE
)
return
subprocess
.
run
(
f
'
{
clang_format_path
}
-style=file'
,
message
(
WARNING
"Python 3 interpreter not found - skipping 'generate' target!"
)
capture_output
=
True
,
return
()
shell
=
True
,
endif
()
check
=
True
,
input
=
buffer
.
encode
(
'utf-8'
),
cwd
=
work_dir
,
**
kwargs
).
stdout
.
decode
(
'utf-8'
)
cmake_path
(
SET SRC_DIR NORMALIZE
${
CMAKE_CURRENT_LIST_DIR
}
/../src
)
file
(
GLOB __files
${
CMAKE_CURRENT_LIST_DIR
}
/include/*.hpp
)
def
api_generate
(
input_path
:
Path
,
output_path
:
Path
):
foreach
(
__file
${
__files
}
)
with
open
(
output_path
,
'w'
)
as
f
:
cmake_path
(
NATIVE_PATH __file NORMALIZE __input_native_path
)
f
.
write
(
clang_format
(
api
.
invoke
(
input_path
)))
cmake_path
(
GET __file FILENAME __file_name
)
cmake_path
(
SET __output_path
"
${
SRC_DIR
}
/include/migraphx/
${
__file_name
}
"
)
cmake_path
(
NATIVE_PATH __output_path __output_native_path
)
message
(
"Generating
${
__output_native_path
}
"
)
execute_process
(
COMMAND
${
Python_EXECUTABLE
}
te.py
${
__input_native_path
}
|
${
__clang_format
}
-style=file
OUTPUT_FILE
${
__output_native_path
}
ERROR_VARIABLE __error_code
WORKING_DIRECTORY
${
CMAKE_SOURCE_DIR
}
)
if
(
__error_code
)
message
(
WARNING
"
${
__error_code
}
"
)
endif
()
endforeach
()
function
(
generate_api __input_path __output_path
)
cmake_path
(
NATIVE_PATH __output_path NORMALIZE __output_native_path
)
cmake_path
(
NATIVE_PATH __input_path NORMALIZE __input_native_path
)
cmake_path
(
SET __migraphx_py_path
"
${
SRC_DIR
}
/api/migraphx.py"
)
cmake_path
(
NATIVE_PATH __migraphx_py_path __migraphx_py_native_path
)
cmake_path
(
SET __api_py_path
"
${
CMAKE_CURRENT_LIST_DIR
}
/api.py"
)
cmake_path
(
NATIVE_PATH __api_py_path __api_py_native_path
)
message
(
"Generating
${
__output_native_path
}
"
)
execute_process
(
COMMAND
${
Python_EXECUTABLE
}
${
__api_py_native_path
}
${
__migraphx_py_native_path
}
${
__input_native_path
}
|
${
__clang_format
}
-style=file
OUTPUT_FILE
${
__output_native_path
}
ERROR_VARIABLE __error_code
WORKING_DIRECTORY
${
CMAKE_SOURCE_DIR
}
)
if
(
__error_code
)
message
(
WARNING
"
${
__error_code
}
"
)
endif
()
endfunction
()
generate_api
(
"
${
CMAKE_CURRENT_LIST_DIR
}
/api/migraphx.h"
"
${
SRC_DIR
}
/api/include/migraphx/migraphx.h"
)
def
te_generate
(
input_path
:
Path
,
output_path
:
Path
):
generate_api
(
"
${
CMAKE_CURRENT_LIST_DIR
}
/api/api.cpp"
"
${
SRC_DIR
}
/api/api.cpp"
)
with
open
(
output_path
,
'w'
)
as
f
:
f
.
write
(
clang_format
(
te
.
invoke
(
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
:
for
f
in
[
f
for
f
in
Path
(
'include'
).
absolute
().
iterdir
()
if
f
.
is_file
()
]:
te_generate
(
f
,
src_dir
/
f
'include/migraphx/
{
f
.
name
}
'
)
api
.
register_functions
(
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
()
tools/te.py
View file @
60f60093
...
@@ -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
invoke
(
p
):
r
=
template_eval
(
f
)
return
template_eval
(
open
(
p
).
read
())
sys
.
stdout
.
write
(
r
)
if
__name__
==
'__main__'
:
sys
.
stdout
.
write
(
invoke
(
sys
.
argv
[
1
]))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment