From 8b0f665cc54fde8f46a66bf7a91de2c12bdd9b55 Mon Sep 17 00:00:00 2001 From: "N. Engelhardt" Date: Thu, 28 Nov 2024 13:55:45 +0100 Subject: [PATCH 1/3] add setenv pass --- passes/cmds/Makefile.inc | 1 + passes/cmds/setenv.cc | 49 ++++++++++++++++++++++++++++++++++++++ tests/verific/setenv.flist | 1 + tests/verific/setenv.ys | 4 ++++ 4 files changed, 55 insertions(+) create mode 100644 passes/cmds/setenv.cc create mode 100644 tests/verific/setenv.flist create mode 100644 tests/verific/setenv.ys diff --git a/passes/cmds/Makefile.inc b/passes/cmds/Makefile.inc index 62924725f..5a2af4df5 100644 --- a/passes/cmds/Makefile.inc +++ b/passes/cmds/Makefile.inc @@ -52,3 +52,4 @@ OBJS += passes/cmds/box_derive.o OBJS += passes/cmds/example_dt.o OBJS += passes/cmds/portarcs.o OBJS += passes/cmds/wrapcell.o +OBJS += passes/cmds/setenv.o diff --git a/passes/cmds/setenv.cc b/passes/cmds/setenv.cc new file mode 100644 index 000000000..7d791cf76 --- /dev/null +++ b/passes/cmds/setenv.cc @@ -0,0 +1,49 @@ +/* + * yosys -- Yosys Open SYnthesis Suite + * + * Copyright (C) 2024 N. Engelhardt + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + */ + +#include "kernel/register.h" +#include "kernel/rtlil.h" +#include "kernel/log.h" +#include + +USING_YOSYS_NAMESPACE +PRIVATE_NAMESPACE_BEGIN +struct SetenvPass : public Pass { + SetenvPass() : Pass("setenv", "set an environment variable") { } + void help() override + { + // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---| + log("\n"); + log(" setenv name value\n"); + log("\n"); + log("Set the given environment variable on the current process. String values must be\n"); + log("passed in double quotes (\").\n"); + log("\n"); + } + void execute(std::vector args, [[maybe_unused]] RTLIL::Design *design) override + { + if(args.size() != 3) + log_cmd_error("Wrong number of arguments given.\n"); + + setenv(args[1].c_str(), args[2].c_str(), 1); + + } +} SetenvPass; + +PRIVATE_NAMESPACE_END diff --git a/tests/verific/setenv.flist b/tests/verific/setenv.flist new file mode 100644 index 000000000..c89eeffe5 --- /dev/null +++ b/tests/verific/setenv.flist @@ -0,0 +1 @@ +${filename} diff --git a/tests/verific/setenv.ys b/tests/verific/setenv.ys new file mode 100644 index 000000000..fa29000e6 --- /dev/null +++ b/tests/verific/setenv.ys @@ -0,0 +1,4 @@ +setenv filename case.sv +verific -f -sv setenv.flist +verific -import top +select -assert-mod-count 1 top From 1b403b82d79197e07103a313fe2e611c86cc7c8d Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Thu, 28 Nov 2024 14:39:49 +0100 Subject: [PATCH 2/3] Handle setting environment on Windows --- passes/cmds/setenv.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/passes/cmds/setenv.cc b/passes/cmds/setenv.cc index 7d791cf76..7593152a2 100644 --- a/passes/cmds/setenv.cc +++ b/passes/cmds/setenv.cc @@ -41,8 +41,12 @@ struct SetenvPass : public Pass { if(args.size() != 3) log_cmd_error("Wrong number of arguments given.\n"); +#if defined(_WIN32) + _putenv_s(args[1].c_str(), args[2].c_str()); +#else setenv(args[1].c_str(), args[2].c_str(), 1); - +#endif + } } SetenvPass; From 8557455411eb5d1ddbe7f73ac41315e1837d815e Mon Sep 17 00:00:00 2001 From: "N. Engelhardt" Date: Thu, 28 Nov 2024 15:11:04 +0100 Subject: [PATCH 3/3] handle quotes and check return value --- passes/cmds/setenv.cc | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/passes/cmds/setenv.cc b/passes/cmds/setenv.cc index 7593152a2..27f2eea28 100644 --- a/passes/cmds/setenv.cc +++ b/passes/cmds/setenv.cc @@ -32,21 +32,26 @@ struct SetenvPass : public Pass { log("\n"); log(" setenv name value\n"); log("\n"); - log("Set the given environment variable on the current process. String values must be\n"); - log("passed in double quotes (\").\n"); + log("Set the given environment variable on the current process. Values containing\n"); + log("whitespace must be passed in double quotes (\").\n"); log("\n"); } void execute(std::vector args, [[maybe_unused]] RTLIL::Design *design) override { if(args.size() != 3) log_cmd_error("Wrong number of arguments given.\n"); + + std::string name = args[1]; + std::string value = args[2]; + if (value.front() == '\"' && value.back() == '\"') value = value.substr(1, value.size() - 2); #if defined(_WIN32) - _putenv_s(args[1].c_str(), args[2].c_str()); + _putenv_s(name.c_str(), value.c_str()); #else - setenv(args[1].c_str(), args[2].c_str(), 1); + if (setenv(name.c_str(), value.c_str(), 1)) + log_cmd_error("Invalid name \"%s\".\n", name.c_str()); #endif - + } } SetenvPass;