From 069f3b02c4b9f301e153201e0911f3ad07611193 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Harboe?= Date: Fri, 12 Jul 2024 14:38:24 +0200 Subject: [PATCH] logging: quote arguments when there is a space in the arg when logging cmds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Øyvind Harboe --- src/main/scala/chiseltest/simulator/IcarusSimulator.scala | 4 ++-- src/main/scala/chiseltest/simulator/Util.scala | 5 +++++ src/main/scala/chiseltest/simulator/VerilatorSimulator.scala | 2 +- .../scala/chiseltest/simulator/ipc/IPCSimulatorContext.scala | 4 ++-- 4 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 src/main/scala/chiseltest/simulator/Util.scala diff --git a/src/main/scala/chiseltest/simulator/IcarusSimulator.scala b/src/main/scala/chiseltest/simulator/IcarusSimulator.scala index 92f57c678..29b13ab94 100644 --- a/src/main/scala/chiseltest/simulator/IcarusSimulator.scala +++ b/src/main/scala/chiseltest/simulator/IcarusSimulator.scala @@ -113,7 +113,7 @@ private object IcarusSimulator extends Simulator with LazyLogging { val success = ret.exitCode == 0 && os.exists(lib) assert( success, - s"failed to compiler VPI shared library for circuit ${topName} in work dir $compileDir\n" + cmd.mkString(" ") + s"failed to compiler VPI shared library for circuit ${topName} in work dir $compileDir\n" + Util.quoteCmdArgs(cmd) ) lib } @@ -141,7 +141,7 @@ private object IcarusSimulator extends Simulator with LazyLogging { val ret = os.proc(cmd).call(cwd = os.pwd, check = false) val success = ret.exitCode == 0 && os.exists(os.pwd / simBinary) - assert(success, s"iverilog command failed on circuit ${topName} in work dir $targetDir\n" + cmd.mkString(" ")) + assert(success, s"iverilog command failed on circuit ${topName} in work dir $targetDir\n" + Util.quoteCmdArgs(cmd)) Seq("vvp", simBinary.toString()) } diff --git a/src/main/scala/chiseltest/simulator/Util.scala b/src/main/scala/chiseltest/simulator/Util.scala new file mode 100644 index 000000000..d6cd9dd57 --- /dev/null +++ b/src/main/scala/chiseltest/simulator/Util.scala @@ -0,0 +1,5 @@ +object Utils { + def quoteCmdArgs(cmd: Seq[String]): String = { + cmd.map(arg => if (arg.contains(" ")) s""""$arg"""" else arg).mkString(" ") + } +} diff --git a/src/main/scala/chiseltest/simulator/VerilatorSimulator.scala b/src/main/scala/chiseltest/simulator/VerilatorSimulator.scala index 1720e1767..45d21b877 100644 --- a/src/main/scala/chiseltest/simulator/VerilatorSimulator.scala +++ b/src/main/scala/chiseltest/simulator/VerilatorSimulator.scala @@ -219,7 +219,7 @@ private object VerilatorSimulator extends Simulator { private def run(cmd: Seq[String], cwd: os.Path, verbose: Boolean): os.CommandResult = { if (verbose) { // print the command and pipe the output to stdout - println(cmd.mkString(" ")) + println(Util.quoteCmdArgs(cmd)) os.proc(cmd) .call(cwd = cwd, stdout = os.ProcessOutput.Readlines(println), stderr = os.ProcessOutput.Readlines(println)) } else { diff --git a/src/main/scala/chiseltest/simulator/ipc/IPCSimulatorContext.scala b/src/main/scala/chiseltest/simulator/ipc/IPCSimulatorContext.scala index 646a807a5..3346c4183 100644 --- a/src/main/scala/chiseltest/simulator/ipc/IPCSimulatorContext.scala +++ b/src/main/scala/chiseltest/simulator/ipc/IPCSimulatorContext.scala @@ -321,7 +321,7 @@ private[chiseltest] class IPCSimulatorContext( private def start(): Unit = { if (verbose) - println(s"""STARTING ${cmd.mkString(" ")}""") + println(s"""STARTING ${Util.quoteCmdArgs(cmd)}""") mwhile(!recvOutputs) {} isRunning = true } @@ -417,7 +417,7 @@ private[chiseltest] class IPCSimulatorContext( private object TesterProcess { def apply(cmd: Seq[String], logs: ArrayBuffer[String], verbose: Boolean): Process = { require(new java.io.File(cmd.head).exists, s"${cmd.head} doesn't exist") - val processBuilder = Process(cmd.mkString(" ")) + val processBuilder = Process(Util.quoteCmdArgs(cmd)) // This makes everything written to stderr get added as lines to logs val processLogger = ProcessLogger( { str => if (verbose) println(str) },