run: write environment variables to cmd.sh for the glorious M5_PATH

This commit is contained in:
Ciro Santilli
2018-08-30 16:54:15 +01:00
parent 1c8eb2d838
commit 78a7eeaed8
2 changed files with 18 additions and 9 deletions

View File

@@ -135,10 +135,15 @@ around when you checkout between branches.
parser.set_defaults(**defaults) parser.set_defaults(**defaults)
return parser return parser
def print_cmd(cmd, cmd_file=None): def print_cmd(cmd, cmd_file=None, extra_env=None):
if extra_env is None:
extra_env = {}
newline_separator = ' \\\n'
out = [] out = []
for key in extra_env:
out.extend(['{}={}'.format(shlex.quote(key), shlex.quote(extra_env[key])), newline_separator])
for arg in cmd: for arg in cmd:
out.extend([shlex.quote(arg), ' \\\n']) out.extend([shlex.quote(arg), newline_separator])
out = ''.join(out) out = ''.join(out)
print(out) print(out)
if cmd_file is not None: if cmd_file is not None:
@@ -148,7 +153,7 @@ def print_cmd(cmd, cmd_file=None):
st = os.stat(cmd_file) st = os.stat(cmd_file)
os.chmod(cmd_file, st.st_mode | stat.S_IXUSR) os.chmod(cmd_file, st.st_mode | stat.S_IXUSR)
def run_cmd(cmd, cmd_file=None, out_file=None, **kwargs): def run_cmd(cmd, cmd_file=None, out_file=None, extra_env=None, **kwargs):
""" """
Run a command. Write the command to stdout before running it. Run a command. Write the command to stdout before running it.
@@ -159,19 +164,23 @@ def run_cmd(cmd, cmd_file=None, out_file=None, **kwargs):
- cmd_file is not None, write the command to the given file - cmd_file is not None, write the command to the given file
- out_file is not None, write the stdout and stderr of the command to the given file - out_file is not None, write the stdout and stderr of the command to the given file
""" """
if out_file: if out_file is not None:
stdout=subprocess.PIPE stdout=subprocess.PIPE
stderr=subprocess.STDOUT stderr=subprocess.STDOUT
else: else:
stdout=None stdout=None
stderr=None stderr=None
print_cmd(cmd, cmd_file) if extra_env is None:
extra_env = {}
env = os.environ.copy()
env.update(extra_env)
print_cmd(cmd, cmd_file, extra_env=extra_env)
# Otherwise Ctrl + C gives: # Otherwise Ctrl + C gives:
# - ugly Python stack trace for gem5 (QEMU takes over terminal and is fine). # - ugly Python stack trace for gem5 (QEMU takes over terminal and is fine).
# - kills Python, and that then kills GDB: https://stackoverflow.com/questions/19807134/does-python-always-raise-an-exception-if-you-do-ctrlc-when-a-subprocess-is-exec # - kills Python, and that then kills GDB: https://stackoverflow.com/questions/19807134/does-python-always-raise-an-exception-if-you-do-ctrlc-when-a-subprocess-is-exec
signal.signal(signal.SIGINT, signal.SIG_IGN) signal.signal(signal.SIGINT, signal.SIG_IGN)
# https://stackoverflow.com/questions/15535240/python-popen-write-to-stdout-and-log-file-simultaneously/52090802#52090802 # https://stackoverflow.com/questions/15535240/python-popen-write-to-stdout-and-log-file-simultaneously/52090802#52090802
with subprocess.Popen(cmd, stdout=stdout, stderr=stderr, **kwargs) as proc: with subprocess.Popen(cmd, stdout=stdout, stderr=stderr, env=env, **kwargs) as proc:
if out_file is not None: if out_file is not None:
with open(out_file, 'bw') as logfile: with open(out_file, 'bw') as logfile:
while True: while True:

6
run
View File

@@ -163,7 +163,6 @@ args = common.setup(parser)
kernel_cli_extra = 'console_msg_format=syslog nokaslr norandmaps panic=-1 printk.devkmsg=on printk.time=y' kernel_cli_extra = 'console_msg_format=syslog nokaslr norandmaps panic=-1 printk.devkmsg=on printk.time=y'
if args.kernel_cli_extra is not None: if args.kernel_cli_extra is not None:
kernel_cli_extra += ' {}'.format(args.kernel_cli_extra) kernel_cli_extra += ' {}'.format(args.kernel_cli_extra)
env = os.environ.copy()
kernel_cli_extra_after_dash = '' kernel_cli_extra_after_dash = ''
extra_emulator_args = args.extra_emulator_args.copy() extra_emulator_args = args.extra_emulator_args.copy()
extra_qemu_args = [] extra_qemu_args = []
@@ -200,6 +199,7 @@ if not args.graphic:
extra_qemu_args.append('-nographic') extra_qemu_args.append('-nographic')
if kernel_cli_extra_after_dash: if kernel_cli_extra_after_dash:
kernel_cli_extra += " -{}".format(kernel_cli_extra_after_dash) kernel_cli_extra += " -{}".format(kernel_cli_extra_after_dash)
extra_env = {}
# A dummy value that is already turned on by default and does not produce large output, # A dummy value that is already turned on by default and does not produce large output,
# just to prevent QEMU from emitting a warning that '' is not valid. # just to prevent QEMU from emitting a warning that '' is not valid.
@@ -210,7 +210,7 @@ if args.gem5:
gem5_exe_args = shlex.split(args.gem5_exe_args) gem5_exe_args = shlex.split(args.gem5_exe_args)
if args.trace is not None: if args.trace is not None:
gem5_exe_args.append('--debug-flags={}'.format(args.trace)) gem5_exe_args.append('--debug-flags={}'.format(args.trace))
env['M5_PATH'] = common.gem5_system_dir extra_env['M5_PATH'] = common.gem5_system_dir
cmd = ( cmd = (
debug_vm + debug_vm +
[ [
@@ -378,7 +378,7 @@ if debug_vm:
out_file = None out_file = None
else: else:
out_file = common.termout_file out_file = common.termout_file
returncode = common.run_cmd(cmd, cmd_file=common.run_cmd_file, out_file=out_file, env=env) returncode = common.run_cmd(cmd, cmd_file=common.run_cmd_file, out_file=out_file, extra_env=extra_env)
if returncode != 0: if returncode != 0:
common.error('simulator exited with status != 0') common.error('simulator exited with status != 0')
# Check if guest panicked. # Check if guest panicked.