Skip to content

Commit

Permalink
[one-cmds] Support target option with command args (#13418)
Browse files Browse the repository at this point in the history
This commit supports target option with command args.

ONE-DCO-1.0-Signed-off-by: seongwoo <[email protected]>
  • Loading branch information
mhs4670go authored Jul 14, 2024
1 parent b4c1a77 commit 686f989
Show file tree
Hide file tree
Showing 16 changed files with 383 additions and 28 deletions.
30 changes: 19 additions & 11 deletions compiler/one-cmds/one-codegen
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ def _get_parser(backends_list):
def _verify_arg(parser, args, cfg_args, cfg_target_args, backend_args, unknown_args):
"""verify given arguments"""
cmd_backend_exist = oneutils.is_valid_attr(args, 'backend')
cmd_target_exist = oneutils.is_valid_attr(args, 'target')
if cmd_backend_exist and cmd_target_exist:
parser.error(
'\'backend\' option and \'target\' option cannot be used simultaneously.')
cfg_backend_exist = oneutils.is_valid_attr(cfg_args, 'backend')
cfg_backends_exist = oneutils.is_valid_attr(cfg_args, 'backends')
target_exist = oneutils.is_valid_attr(args, 'target') or oneutils.is_valid_attr(
cfg_target_args, 'target')
target_exist = cmd_target_exist or oneutils.is_valid_attr(cfg_target_args, 'target')
# check if required arguments is given
missing = []
Expand All @@ -77,12 +80,10 @@ def _verify_arg(parser, args, cfg_args, cfg_target_args, backend_args, unknown_a
if oneutils.is_valid_attr(args, 'target'):
target_to_run = args.target
given_backend = backends.get_backend_from_target_conf(target_to_run)
schema_exist = oneutils.get_arg_parser(
given_backend, cmd="codegen", target=target_to_run)
if not schema_exist:
missing.append('-b/--backend')
if not given_backend:
parser.error(f'Not found {target_to_run} target.')
else:
missing.append('-b/--backend')
missing.append('[-b/--backend | -T/--target]')
if len(missing):
parser.error('the following arguments are required: ' + ' '.join(missing))
Expand Down Expand Up @@ -127,11 +128,11 @@ def _parse_arg(parser):
if len(args) == 0:
codegen_args = parser.parse_args(codegen_args)
# one-codegen has two interfaces
# 1. one-codegen [-h] [-v] [-C CONFIG] [-b BACKEND] [COMMANDS FOR BACKEND]
# 1. one-codegen [-h] [-v] [-C CONFIG] [-b BACKEND | -T TARGET] [COMMANDS FOR BACKEND]
if len(args) == 1:
codegen_args = args[0]
codegen_args, unknown_args = parser.parse_known_args(codegen_args)
# 2. one-codegen [-h] [-v] [-C CONFIG] [-b BACKEND] -- [COMMANDS FOR BACKEND]
# 2. one-codegen [-h] [-v] [-C CONFIG] [-b BACKEND | -T TARGET] -- [COMMANDS FOR BACKEND]
if len(args) == 2:
codegen_args = args[0]
backend_args = args[1]
Expand Down Expand Up @@ -181,6 +182,8 @@ def main():
[12] one-codegen -C {cfg} (w/ target, w/o command schema)
[13] one-codegen -C {cfg} -T {target} (w/ command schema)
[14] one-codegen -C {cfg} -T {target} (w/o command schema)
[15] one-codegen -T {target} ${command} (ignore command schema)
[16] one-codegen -T {target} -- ${command} (ignore command schema)

All other cases are not allowed or an undefined behavior.
'''
Expand Down Expand Up @@ -237,10 +240,15 @@ def main():
# [4]
if oneutils.is_valid_attr(cfg_args, 'backends'):
given_backends = cfg_args.backends.split(',')
# [5], [6]
else:
assert (backend_args or unknown_args)
given_backends = [args.backend]
# [5], [6]
if oneutils.is_valid_attr(args, 'backend'):
given_backends = [args.backend]
# [15], [16]
else:
assert oneutils.is_valid_attr(args, 'target')
given_backends = [backends.get_backend_from_target_conf(target_to_run)]

# make commands
# 1. if command schema exists
Expand Down
30 changes: 19 additions & 11 deletions compiler/one-cmds/one-profile
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,13 @@ def _get_parser(backends_list):
def _verify_arg(parser, args, cfg_args, cfg_target_args, backend_args, unknown_args):
"""verify given arguments"""
cmd_backend_exist = oneutils.is_valid_attr(args, 'backend')
cmd_target_exist = oneutils.is_valid_attr(args, 'target')
if cmd_backend_exist and cmd_target_exist:
parser.error(
'\'backend\' option and \'target\' option cannot be used simultaneously.')
cfg_backend_exist = oneutils.is_valid_attr(cfg_args, 'backend')
cfg_backends_exist = oneutils.is_valid_attr(cfg_args, 'backends')
target_exist = oneutils.is_valid_attr(args, 'target') or oneutils.is_valid_attr(
cfg_target_args, 'target')
target_exist = cmd_target_exist or oneutils.is_valid_attr(cfg_target_args, 'target')
# check if required arguments is given
missing = []
Expand All @@ -116,12 +119,10 @@ def _verify_arg(parser, args, cfg_args, cfg_target_args, backend_args, unknown_a
if oneutils.is_valid_attr(args, 'target'):
target_to_run = args.target
given_backend = backends.get_backend_from_target_conf(target_to_run)
schema_exist = oneutils.get_arg_parser(
given_backend, cmd="profile", target=target_to_run)
if not schema_exist:
missing.append('-b/--backend')
if not given_backend:
parser.error(f'Not found {target_to_run} target.')
else:
missing.append('-b/--backend')
missing.append('[-b/--backend | -T/--target]')
if len(missing):
parser.error('the following arguments are required: ' + ' '.join(missing))
Expand Down Expand Up @@ -165,11 +166,11 @@ def _parse_arg(parser):
if len(args) == 0:
profile_args = parser.parse_args(profile_args)
# one-profile has two interfaces
# 1. one-profile [-h] [-v] [-C CONFIG] [-b BACKEND] [COMMANDS FOR BACKEND]
# 1. one-profile [-h] [-v] [-C CONFIG] [-b BACKEND | -T TARGET] [COMMANDS FOR BACKEND]
if len(args) == 1:
profile_args = args[0]
profile_args, unknown_args = parser.parse_known_args(profile_args)
# 2. one-profile [-h] [-v] [-C CONFIG] [-b BACKEND] -- [COMMANDS FOR BACKEND]
# 2. one-profile [-h] [-v] [-C CONFIG] [-b BACKEND | -T TARGET] -- [COMMANDS FOR BACKEND]
if len(args) == 2:
profile_args = args[0]
backend_args = args[1]
Expand Down Expand Up @@ -216,6 +217,8 @@ def main():
[12] one-profile -C {cfg} (w/ target, w/o command schema)
[13] one-profile -C {cfg} -T {target} (w/ command schema)
[14] one-profile -C {cfg} -T {target} (w/o command schema)
[15] one-profile -T {target} ${command}
[16] one-profile -T {target} -- ${command}

All other cases are not allowed or an undefined behavior.
'''
Expand Down Expand Up @@ -272,10 +275,15 @@ def main():
# [4]
if oneutils.is_valid_attr(cfg_args, 'backends'):
given_backends = cfg_args.backends.split(',')
# [5], [6]
else:
assert (backend_args or unknown_args)
given_backends = [args.backend]
# [5], [6]
if oneutils.is_valid_attr(args, 'backend'):
given_backends = [args.backend]
# [15], [16]
else:
assert oneutils.is_valid_attr(args, 'target')
given_backends = [backends.get_backend_from_target_conf(target_to_run)]

# make commands
# 1. if command schema exists
Expand Down
2 changes: 2 additions & 0 deletions compiler/one-cmds/tests/one-codegen_010.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TARGET=one-codegen_010
BACKEND=dummy
83 changes: 83 additions & 0 deletions compiler/one-cmds/tests/one-codegen_010.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/bin/bash

# Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# one-codegen -T {target} ${command}

: '
This test assumes below directories.
[one hierarchy]
one
├── backends
├── bin
├── doc
├── include
├── lib
├── optimization
├── target
└── test # pwd
'

TARGET_ALREADY_EXIST=true

filename_ext="$(basename -- $0)"
filename="${filename_ext%.*}"

inputfile="one-codegen_010.circle"
outputfile="one-codegen_010.tvn"
targetfile="one-codegen_010.ini"

clean_envir()
{
rm -rf ../bin/dummy-compile
rm -rf ../target/${targetfile}
if [ "$TARGET_ALREADY_EXIST" = false ]; then
rm -rf ../target/
fi
}

trap_err_onexit()
{
echo "${filename_ext} FAILED"
rm -rf ../bin/dummy-compile
exit 255
}

trap trap_err_onexit ERR

rm -rf ${outputfile}
rm -f ${filename}.log

if [ ! -d "../target/" ]; then
mkdir -p ../target/
TARGET_ALREADY_EXIST=false
fi

# copy dummy-compile to bin folder
cp dummy-compile ../bin/dummy-compile
cp ${targetfile} ../target/

# run test
one-codegen -T one-codegen_010 -o ${outputfile} ${inputfile} > ${filename}.log 2>&1

clean_envir

if grep -q "dummy-compile with one-codegen_010 target" "${outputfile}"; then
echo "${filename_ext} SUCCESS"
exit 0
fi

trap_err_onexit
2 changes: 2 additions & 0 deletions compiler/one-cmds/tests/one-codegen_011.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TARGET=one-codegen_011
BACKEND=dummy
84 changes: 84 additions & 0 deletions compiler/one-cmds/tests/one-codegen_011.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/bin/bash

# Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# one-codegen -T {target} -- ${command}

: '
This test assumes below directories.
[one hierarchy]
one
├── backends
├── bin
├── doc
├── include
├── lib
├── optimization
├── target
└── test # pwd
'

TARGET_ALREADY_EXIST=true

filename_ext="$(basename -- $0)"
filename="${filename_ext%.*}"

configfile="one-codegen_011.cfg"
inputfile="one-codegen_011.circle"
outputfile="one-codegen_011.tvn"
targetfile="one-codegen_011.ini"

clean_envir()
{
rm -rf ../bin/dummy-compile
rm -rf ../target/${targetfile}
if [ "$TARGET_ALREADY_EXIST" = false ]; then
rm -rf ../target/
fi
}

trap_err_onexit()
{
echo "${filename_ext} FAILED"
rm -rf ../bin/dummy-compile
exit 255
}

trap trap_err_onexit ERR

rm -rf ${outputfile}
rm -f ${filename}.log

if [ ! -d "../target/" ]; then
mkdir -p ../target/
TARGET_ALREADY_EXIST=false
fi

# copy dummy-compile to bin folder
cp dummy-compile ../bin/dummy-compile
cp ${targetfile} ../target/

# run test
one-codegen -T one-codegen_011 -- -o ${outputfile} ${inputfile} > ${filename}.log 2>&1

clean_envir

if grep -q "dummy-compile with one-codegen_011 target" "${outputfile}"; then
echo "${filename_ext} SUCCESS"
exit 0
fi

trap_err_onexit
2 changes: 1 addition & 1 deletion compiler/one-cmds/tests/one-codegen_neg_001.test
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ filename="${filename_ext%.*}"

trap_err_onexit()
{
if grep -q "error: the following arguments are required: -b/--backend" "${filename}.log"; then
if grep -q "error: the following arguments are required: \[-b/--backend | -T/--target\]" "${filename}.log"; then
echo "${filename_ext} SUCCESS"
exit 0
fi
Expand Down
2 changes: 1 addition & 1 deletion compiler/one-cmds/tests/one-codegen_neg_002.test
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ filename="${filename_ext%.*}"

trap_err_onexit()
{
if grep -q "error: the following arguments are required: -b/--backend" "${filename}.log"; then
if grep -q "error: the following arguments are required: \[-b/--backend | -T/--target\]" "${filename}.log"; then
echo "${filename_ext} SUCCESS"
exit 0
fi
Expand Down
2 changes: 1 addition & 1 deletion compiler/one-cmds/tests/one-codegen_neg_003.test
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ filename="${filename_ext%.*}"

trap_err_onexit()
{
if grep -q "error: the following arguments are required: -b/--backend" "${filename}.log"; then
if grep -q "error: the following arguments are required: \[-b/--backend | -T/--target\]" "${filename}.log"; then
echo "${filename_ext} SUCCESS"
exit 0
fi
Expand Down
2 changes: 2 additions & 0 deletions compiler/one-cmds/tests/one-profile_010.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TARGET=one-profile_010
BACKEND=dummy
Loading

0 comments on commit 686f989

Please sign in to comment.