From 686f9897aecd0f873f5afa28d1142e7d80da18de Mon Sep 17 00:00:00 2001 From: seongwoo chae Date: Sun, 14 Jul 2024 21:29:52 +0900 Subject: [PATCH] [one-cmds] Support target option with command args (#13418) This commit supports target option with command args. ONE-DCO-1.0-Signed-off-by: seongwoo --- compiler/one-cmds/one-codegen | 30 ++++--- compiler/one-cmds/one-profile | 30 ++++--- compiler/one-cmds/tests/one-codegen_010.ini | 2 + compiler/one-cmds/tests/one-codegen_010.test | 83 ++++++++++++++++++ compiler/one-cmds/tests/one-codegen_011.ini | 2 + compiler/one-cmds/tests/one-codegen_011.test | 84 +++++++++++++++++++ .../one-cmds/tests/one-codegen_neg_001.test | 2 +- .../one-cmds/tests/one-codegen_neg_002.test | 2 +- .../one-cmds/tests/one-codegen_neg_003.test | 2 +- compiler/one-cmds/tests/one-profile_010.ini | 2 + compiler/one-cmds/tests/one-profile_010.test | 82 ++++++++++++++++++ compiler/one-cmds/tests/one-profile_011.ini | 2 + compiler/one-cmds/tests/one-profile_011.test | 82 ++++++++++++++++++ .../one-cmds/tests/one-profile_neg_001.test | 2 +- .../one-cmds/tests/one-profile_neg_002.test | 2 +- .../one-cmds/tests/one-profile_neg_003.test | 2 +- 16 files changed, 383 insertions(+), 28 deletions(-) create mode 100644 compiler/one-cmds/tests/one-codegen_010.ini create mode 100644 compiler/one-cmds/tests/one-codegen_010.test create mode 100644 compiler/one-cmds/tests/one-codegen_011.ini create mode 100644 compiler/one-cmds/tests/one-codegen_011.test create mode 100644 compiler/one-cmds/tests/one-profile_010.ini create mode 100644 compiler/one-cmds/tests/one-profile_010.test create mode 100644 compiler/one-cmds/tests/one-profile_011.ini create mode 100644 compiler/one-cmds/tests/one-profile_011.test diff --git a/compiler/one-cmds/one-codegen b/compiler/one-cmds/one-codegen index f7a7d70b09a..1081146e233 100644 --- a/compiler/one-cmds/one-codegen +++ b/compiler/one-cmds/one-codegen @@ -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 = [] @@ -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)) @@ -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] @@ -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. ''' @@ -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 diff --git a/compiler/one-cmds/one-profile b/compiler/one-cmds/one-profile index 418b68b4732..9296e519792 100644 --- a/compiler/one-cmds/one-profile +++ b/compiler/one-cmds/one-profile @@ -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 = [] @@ -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)) @@ -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] @@ -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. ''' @@ -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 diff --git a/compiler/one-cmds/tests/one-codegen_010.ini b/compiler/one-cmds/tests/one-codegen_010.ini new file mode 100644 index 00000000000..3a99b893bcb --- /dev/null +++ b/compiler/one-cmds/tests/one-codegen_010.ini @@ -0,0 +1,2 @@ +TARGET=one-codegen_010 +BACKEND=dummy diff --git a/compiler/one-cmds/tests/one-codegen_010.test b/compiler/one-cmds/tests/one-codegen_010.test new file mode 100644 index 00000000000..a81310e2899 --- /dev/null +++ b/compiler/one-cmds/tests/one-codegen_010.test @@ -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 diff --git a/compiler/one-cmds/tests/one-codegen_011.ini b/compiler/one-cmds/tests/one-codegen_011.ini new file mode 100644 index 00000000000..d514a46a641 --- /dev/null +++ b/compiler/one-cmds/tests/one-codegen_011.ini @@ -0,0 +1,2 @@ +TARGET=one-codegen_011 +BACKEND=dummy diff --git a/compiler/one-cmds/tests/one-codegen_011.test b/compiler/one-cmds/tests/one-codegen_011.test new file mode 100644 index 00000000000..0b7f9174fdb --- /dev/null +++ b/compiler/one-cmds/tests/one-codegen_011.test @@ -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 diff --git a/compiler/one-cmds/tests/one-codegen_neg_001.test b/compiler/one-cmds/tests/one-codegen_neg_001.test index 137a3f90d97..e5c18659429 100644 --- a/compiler/one-cmds/tests/one-codegen_neg_001.test +++ b/compiler/one-cmds/tests/one-codegen_neg_001.test @@ -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 diff --git a/compiler/one-cmds/tests/one-codegen_neg_002.test b/compiler/one-cmds/tests/one-codegen_neg_002.test index 28dfa920cf3..f54f3ebf35b 100644 --- a/compiler/one-cmds/tests/one-codegen_neg_002.test +++ b/compiler/one-cmds/tests/one-codegen_neg_002.test @@ -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 diff --git a/compiler/one-cmds/tests/one-codegen_neg_003.test b/compiler/one-cmds/tests/one-codegen_neg_003.test index 0622b6a0792..015b7262ce9 100644 --- a/compiler/one-cmds/tests/one-codegen_neg_003.test +++ b/compiler/one-cmds/tests/one-codegen_neg_003.test @@ -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 diff --git a/compiler/one-cmds/tests/one-profile_010.ini b/compiler/one-cmds/tests/one-profile_010.ini new file mode 100644 index 00000000000..22ae64a4252 --- /dev/null +++ b/compiler/one-cmds/tests/one-profile_010.ini @@ -0,0 +1,2 @@ +TARGET=one-profile_010 +BACKEND=dummy diff --git a/compiler/one-cmds/tests/one-profile_010.test b/compiler/one-cmds/tests/one-profile_010.test new file mode 100644 index 00000000000..681b9d6dc75 --- /dev/null +++ b/compiler/one-cmds/tests/one-profile_010.test @@ -0,0 +1,82 @@ +#!/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-profile -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-profile_010.cfg" +inputfile="one-profile_010.tvn" +targetfile="one-profile_010.ini" + +clean_envir() +{ + rm -rf ../bin/dummy-profile + 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-profile + exit 255 +} + +trap trap_err_onexit ERR + +rm -f ${filename}.log + +if [ ! -d "../target/" ]; then + mkdir -p ../target/ + TARGET_ALREADY_EXIST=false +fi + +# copy dummy-profile to bin folder +cp dummy-profile ../bin/dummy-profile +cp ${targetfile} ../target/ + +# run test +one-profile -T one-profile_010 ${inputfile} > ${filename}.log 2>&1 + +clean_envir + +if grep -q "dummy-profile with one-profile_010 target" "${filename}.log"; then + echo "${filename_ext} SUCCESS" + exit 0 +fi + +trap_err_onexit diff --git a/compiler/one-cmds/tests/one-profile_011.ini b/compiler/one-cmds/tests/one-profile_011.ini new file mode 100644 index 00000000000..ccb146d03c1 --- /dev/null +++ b/compiler/one-cmds/tests/one-profile_011.ini @@ -0,0 +1,2 @@ +TARGET=one-profile_011 +BACKEND=dummy diff --git a/compiler/one-cmds/tests/one-profile_011.test b/compiler/one-cmds/tests/one-profile_011.test new file mode 100644 index 00000000000..db2e1c81196 --- /dev/null +++ b/compiler/one-cmds/tests/one-profile_011.test @@ -0,0 +1,82 @@ +#!/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-profile -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-profile_011.cfg" +inputfile="one-profile_011.tvn" +targetfile="one-profile_011.ini" + +clean_envir() +{ + rm -rf ../bin/dummy-profile + 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-profile + exit 255 +} + +trap trap_err_onexit ERR + +rm -f ${filename}.log + +if [ ! -d "../target/" ]; then + mkdir -p ../target/ + TARGET_ALREADY_EXIST=false +fi + +# copy dummy-profile to bin folder +cp dummy-profile ../bin/dummy-profile +cp ${targetfile} ../target/ + +# run test +one-profile -T one-profile_011 -- ${inputfile} > ${filename}.log 2>&1 + +clean_envir + +if grep -q "dummy-profile with one-profile_011 target" "${filename}.log"; then + echo "${filename_ext} SUCCESS" + exit 0 +fi + +trap_err_onexit diff --git a/compiler/one-cmds/tests/one-profile_neg_001.test b/compiler/one-cmds/tests/one-profile_neg_001.test index f5b2dce0288..59a089f5e8f 100644 --- a/compiler/one-cmds/tests/one-profile_neg_001.test +++ b/compiler/one-cmds/tests/one-profile_neg_001.test @@ -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 diff --git a/compiler/one-cmds/tests/one-profile_neg_002.test b/compiler/one-cmds/tests/one-profile_neg_002.test index 6964312a3c5..6afbbe21f5f 100644 --- a/compiler/one-cmds/tests/one-profile_neg_002.test +++ b/compiler/one-cmds/tests/one-profile_neg_002.test @@ -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 diff --git a/compiler/one-cmds/tests/one-profile_neg_003.test b/compiler/one-cmds/tests/one-profile_neg_003.test index a00215452ee..4efac34a0aa 100644 --- a/compiler/one-cmds/tests/one-profile_neg_003.test +++ b/compiler/one-cmds/tests/one-profile_neg_003.test @@ -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